include/opcode/
[external/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
4    Free Software Foundation, Inc.
5    Contributed by the OSF and Ralph Campbell.
6    Written by Keith Knowles and Ralph Campbell, working independently.
7    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8    Support.
9
10    This file is part of GAS.
11
12    GAS is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3, or (at your option)
15    any later version.
16
17    GAS is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with GAS; see the file COPYING.  If not, write to the Free
24    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25    02110-1301, USA.  */
26
27 #include "as.h"
28 #include "config.h"
29 #include "subsegs.h"
30 #include "safe-ctype.h"
31
32 #include "opcode/mips.h"
33 #include "itbl-ops.h"
34 #include "dwarf2dbg.h"
35 #include "dw2gencfi.h"
36
37 /* Check assumptions made in this file.  */
38 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
39 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
40
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #define SKIP_SPACE_TABS(S) \
48   do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
50 /* Clean up namespace so we can include obj-elf.h too.  */
51 static int mips_output_flavor (void);
52 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
53 #undef OBJ_PROCESS_STAB
54 #undef OUTPUT_FLAVOR
55 #undef S_GET_ALIGN
56 #undef S_GET_SIZE
57 #undef S_SET_ALIGN
58 #undef S_SET_SIZE
59 #undef obj_frob_file
60 #undef obj_frob_file_after_relocs
61 #undef obj_frob_symbol
62 #undef obj_pop_insert
63 #undef obj_sec_sym_ok_for_reloc
64 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66 #include "obj-elf.h"
67 /* Fix any of them that we actually care about.  */
68 #undef OUTPUT_FLAVOR
69 #define OUTPUT_FLAVOR mips_output_flavor()
70
71 #include "elf/mips.h"
72
73 #ifndef ECOFF_DEBUGGING
74 #define NO_ECOFF_DEBUGGING
75 #define ECOFF_DEBUGGING 0
76 #endif
77
78 int mips_flag_mdebug = -1;
79
80 /* Control generation of .pdr sections.  Off by default on IRIX: the native
81    linker doesn't know about and discards them, but relocations against them
82    remain, leading to rld crashes.  */
83 #ifdef TE_IRIX
84 int mips_flag_pdr = FALSE;
85 #else
86 int mips_flag_pdr = TRUE;
87 #endif
88
89 #include "ecoff.h"
90
91 static char *mips_regmask_frag;
92
93 #define ZERO 0
94 #define ATREG 1
95 #define S0  16
96 #define S7  23
97 #define TREG 24
98 #define PIC_CALL_REG 25
99 #define KT0 26
100 #define KT1 27
101 #define GP  28
102 #define SP  29
103 #define FP  30
104 #define RA  31
105
106 #define ILLEGAL_REG (32)
107
108 #define AT  mips_opts.at
109
110 extern int target_big_endian;
111
112 /* The name of the readonly data section.  */
113 #define RDATA_SECTION_NAME ".rodata"
114
115 /* Ways in which an instruction can be "appended" to the output.  */
116 enum append_method {
117   /* Just add it normally.  */
118   APPEND_ADD,
119
120   /* Add it normally and then add a nop.  */
121   APPEND_ADD_WITH_NOP,
122
123   /* Turn an instruction with a delay slot into a "compact" version.  */
124   APPEND_ADD_COMPACT,
125
126   /* Insert the instruction before the last one.  */
127   APPEND_SWAP
128 };
129
130 /* Information about an instruction, including its format, operands
131    and fixups.  */
132 struct mips_cl_insn
133 {
134   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
135   const struct mips_opcode *insn_mo;
136
137   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
138      a copy of INSN_MO->match with the operands filled in.  If we have
139      decided to use an extended MIPS16 instruction, this includes the
140      extension.  */
141   unsigned long insn_opcode;
142
143   /* The frag that contains the instruction.  */
144   struct frag *frag;
145
146   /* The offset into FRAG of the first instruction byte.  */
147   long where;
148
149   /* The relocs associated with the instruction, if any.  */
150   fixS *fixp[3];
151
152   /* True if this entry cannot be moved from its current position.  */
153   unsigned int fixed_p : 1;
154
155   /* True if this instruction occurred in a .set noreorder block.  */
156   unsigned int noreorder_p : 1;
157
158   /* True for mips16 instructions that jump to an absolute address.  */
159   unsigned int mips16_absolute_jump_p : 1;
160
161   /* True if this instruction is complete.  */
162   unsigned int complete_p : 1;
163
164   /* True if this instruction is cleared from history by unconditional
165      branch.  */
166   unsigned int cleared_p : 1;
167 };
168
169 /* The ABI to use.  */
170 enum mips_abi_level
171 {
172   NO_ABI = 0,
173   O32_ABI,
174   O64_ABI,
175   N32_ABI,
176   N64_ABI,
177   EABI_ABI
178 };
179
180 /* MIPS ABI we are using for this output file.  */
181 static enum mips_abi_level mips_abi = NO_ABI;
182
183 /* Whether or not we have code that can call pic code.  */
184 int mips_abicalls = FALSE;
185
186 /* Whether or not we have code which can be put into a shared
187    library.  */
188 static bfd_boolean mips_in_shared = TRUE;
189
190 /* This is the set of options which may be modified by the .set
191    pseudo-op.  We use a struct so that .set push and .set pop are more
192    reliable.  */
193
194 struct mips_set_options
195 {
196   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
197      if it has not been initialized.  Changed by `.set mipsN', and the
198      -mipsN command line option, and the default CPU.  */
199   int isa;
200   /* Enabled Application Specific Extensions (ASEs).  Changed by `.set
201      <asename>', by command line options, and based on the default
202      architecture.  */
203   int ase;
204   /* Whether we are assembling for the mips16 processor.  0 if we are
205      not, 1 if we are, and -1 if the value has not been initialized.
206      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
207      -nomips16 command line options, and the default CPU.  */
208   int mips16;
209   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
210      1 if we are, and -1 if the value has not been initialized.  Changed
211      by `.set micromips' and `.set nomicromips', and the -mmicromips
212      and -mno-micromips command line options, and the default CPU.  */
213   int micromips;
214   /* Non-zero if we should not reorder instructions.  Changed by `.set
215      reorder' and `.set noreorder'.  */
216   int noreorder;
217   /* Non-zero if we should not permit the register designated "assembler
218      temporary" to be used in instructions.  The value is the register
219      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
220      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
221   unsigned int at;
222   /* Non-zero if we should warn when a macro instruction expands into
223      more than one machine instruction.  Changed by `.set nomacro' and
224      `.set macro'.  */
225   int warn_about_macros;
226   /* Non-zero if we should not move instructions.  Changed by `.set
227      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
228   int nomove;
229   /* Non-zero if we should not optimize branches by moving the target
230      of the branch into the delay slot.  Actually, we don't perform
231      this optimization anyhow.  Changed by `.set bopt' and `.set
232      nobopt'.  */
233   int nobopt;
234   /* Non-zero if we should not autoextend mips16 instructions.
235      Changed by `.set autoextend' and `.set noautoextend'.  */
236   int noautoextend;
237   /* True if we should only emit 32-bit microMIPS instructions.
238      Changed by `.set insn32' and `.set noinsn32', and the -minsn32
239      and -mno-insn32 command line options.  */
240   bfd_boolean insn32;
241   /* Restrict general purpose registers and floating point registers
242      to 32 bit.  This is initially determined when -mgp32 or -mfp32
243      is passed but can changed if the assembler code uses .set mipsN.  */
244   int gp32;
245   int fp32;
246   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
247      command line option, and the default CPU.  */
248   int arch;
249   /* True if ".set sym32" is in effect.  */
250   bfd_boolean sym32;
251   /* True if floating-point operations are not allowed.  Changed by .set
252      softfloat or .set hardfloat, by command line options -msoft-float or
253      -mhard-float.  The default is false.  */
254   bfd_boolean soft_float;
255
256   /* True if only single-precision floating-point operations are allowed.
257      Changed by .set singlefloat or .set doublefloat, command-line options
258      -msingle-float or -mdouble-float.  The default is false.  */
259   bfd_boolean single_float;
260 };
261
262 /* This is the struct we use to hold the current set of options.  Note
263    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
264    -1 to indicate that they have not been initialized.  */
265
266 /* True if -mgp32 was passed.  */
267 static int file_mips_gp32 = -1;
268
269 /* True if -mfp32 was passed.  */
270 static int file_mips_fp32 = -1;
271
272 /* 1 if -msoft-float, 0 if -mhard-float.  The default is 0.  */
273 static int file_mips_soft_float = 0;
274
275 /* 1 if -msingle-float, 0 if -mdouble-float.  The default is 0.   */
276 static int file_mips_single_float = 0;
277
278 /* True if -mnan=2008, false if -mnan=legacy.  */
279 static bfd_boolean mips_flag_nan2008 = FALSE;
280
281 static struct mips_set_options mips_opts =
282 {
283   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
284   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
285   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
286   /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
287   /* soft_float */ FALSE, /* single_float */ FALSE
288 };
289
290 /* The set of ASEs that were selected on the command line, either
291    explicitly via ASE options or implicitly through things like -march.  */
292 static unsigned int file_ase;
293
294 /* Which bits of file_ase were explicitly set or cleared by ASE options.  */
295 static unsigned int file_ase_explicit;
296
297 /* These variables are filled in with the masks of registers used.
298    The object format code reads them and puts them in the appropriate
299    place.  */
300 unsigned long mips_gprmask;
301 unsigned long mips_cprmask[4];
302
303 /* MIPS ISA we are using for this output file.  */
304 static int file_mips_isa = ISA_UNKNOWN;
305
306 /* True if any MIPS16 code was produced.  */
307 static int file_ase_mips16;
308
309 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32               \
310                               || mips_opts.isa == ISA_MIPS32R2          \
311                               || mips_opts.isa == ISA_MIPS64            \
312                               || mips_opts.isa == ISA_MIPS64R2)
313
314 /* True if any microMIPS code was produced.  */
315 static int file_ase_micromips;
316
317 /* True if we want to create R_MIPS_JALR for jalr $25.  */
318 #ifdef TE_IRIX
319 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
320 #else
321 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
322    because there's no place for any addend, the only acceptable
323    expression is a bare symbol.  */
324 #define MIPS_JALR_HINT_P(EXPR) \
325   (!HAVE_IN_PLACE_ADDENDS \
326    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
327 #endif
328
329 /* The argument of the -march= flag.  The architecture we are assembling.  */
330 static int file_mips_arch = CPU_UNKNOWN;
331 static const char *mips_arch_string;
332
333 /* The argument of the -mtune= flag.  The architecture for which we
334    are optimizing.  */
335 static int mips_tune = CPU_UNKNOWN;
336 static const char *mips_tune_string;
337
338 /* True when generating 32-bit code for a 64-bit processor.  */
339 static int mips_32bitmode = 0;
340
341 /* True if the given ABI requires 32-bit registers.  */
342 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
343
344 /* Likewise 64-bit registers.  */
345 #define ABI_NEEDS_64BIT_REGS(ABI)       \
346   ((ABI) == N32_ABI                     \
347    || (ABI) == N64_ABI                  \
348    || (ABI) == O64_ABI)
349
350 /*  Return true if ISA supports 64 bit wide gp registers.  */
351 #define ISA_HAS_64BIT_REGS(ISA)         \
352   ((ISA) == ISA_MIPS3                   \
353    || (ISA) == ISA_MIPS4                \
354    || (ISA) == ISA_MIPS5                \
355    || (ISA) == ISA_MIPS64               \
356    || (ISA) == ISA_MIPS64R2)
357
358 /*  Return true if ISA supports 64 bit wide float registers.  */
359 #define ISA_HAS_64BIT_FPRS(ISA)         \
360   ((ISA) == ISA_MIPS3                   \
361    || (ISA) == ISA_MIPS4                \
362    || (ISA) == ISA_MIPS5                \
363    || (ISA) == ISA_MIPS32R2             \
364    || (ISA) == ISA_MIPS64               \
365    || (ISA) == ISA_MIPS64R2)
366
367 /* Return true if ISA supports 64-bit right rotate (dror et al.)
368    instructions.  */
369 #define ISA_HAS_DROR(ISA)               \
370   ((ISA) == ISA_MIPS64R2                \
371    || (mips_opts.micromips              \
372        && ISA_HAS_64BIT_REGS (ISA))     \
373    )
374
375 /* Return true if ISA supports 32-bit right rotate (ror et al.)
376    instructions.  */
377 #define ISA_HAS_ROR(ISA)                \
378   ((ISA) == ISA_MIPS32R2                \
379    || (ISA) == ISA_MIPS64R2             \
380    || (mips_opts.ase & ASE_SMARTMIPS)   \
381    || mips_opts.micromips               \
382    )
383
384 /* Return true if ISA supports single-precision floats in odd registers.  */
385 #define ISA_HAS_ODD_SINGLE_FPR(ISA)     \
386   ((ISA) == ISA_MIPS32                  \
387    || (ISA) == ISA_MIPS32R2             \
388    || (ISA) == ISA_MIPS64               \
389    || (ISA) == ISA_MIPS64R2)
390
391 /* Return true if ISA supports move to/from high part of a 64-bit
392    floating-point register. */
393 #define ISA_HAS_MXHC1(ISA)              \
394   ((ISA) == ISA_MIPS32R2                \
395    || (ISA) == ISA_MIPS64R2)
396
397 #define HAVE_32BIT_GPRS                            \
398     (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
399
400 #define HAVE_32BIT_FPRS                            \
401     (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
402
403 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
404 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
405
406 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
407
408 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
409
410 /* True if relocations are stored in-place.  */
411 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
412
413 /* The ABI-derived address size.  */
414 #define HAVE_64BIT_ADDRESSES \
415   (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
416 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
417
418 /* The size of symbolic constants (i.e., expressions of the form
419    "SYMBOL" or "SYMBOL + OFFSET").  */
420 #define HAVE_32BIT_SYMBOLS \
421   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
422 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
423
424 /* Addresses are loaded in different ways, depending on the address size
425    in use.  The n32 ABI Documentation also mandates the use of additions
426    with overflow checking, but existing implementations don't follow it.  */
427 #define ADDRESS_ADD_INSN                                                \
428    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
429
430 #define ADDRESS_ADDI_INSN                                               \
431    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
432
433 #define ADDRESS_LOAD_INSN                                               \
434    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
435
436 #define ADDRESS_STORE_INSN                                              \
437    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
438
439 /* Return true if the given CPU supports the MIPS16 ASE.  */
440 #define CPU_HAS_MIPS16(cpu)                                             \
441    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
442     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
443
444 /* Return true if the given CPU supports the microMIPS ASE.  */
445 #define CPU_HAS_MICROMIPS(cpu)  0
446
447 /* True if CPU has a dror instruction.  */
448 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
449
450 /* True if CPU has a ror instruction.  */
451 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
452
453 /* True if CPU is in the Octeon family */
454 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
455
456 /* True if CPU has seq/sne and seqi/snei instructions.  */
457 #define CPU_HAS_SEQ(CPU)        (CPU_IS_OCTEON (CPU))
458
459 /* True, if CPU has support for ldc1 and sdc1. */
460 #define CPU_HAS_LDC1_SDC1(CPU)  \
461    ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
462
463 /* True if mflo and mfhi can be immediately followed by instructions
464    which write to the HI and LO registers.
465
466    According to MIPS specifications, MIPS ISAs I, II, and III need
467    (at least) two instructions between the reads of HI/LO and
468    instructions which write them, and later ISAs do not.  Contradicting
469    the MIPS specifications, some MIPS IV processor user manuals (e.g.
470    the UM for the NEC Vr5000) document needing the instructions between
471    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
472    MIPS64 and later ISAs to have the interlocks, plus any specific
473    earlier-ISA CPUs for which CPU documentation declares that the
474    instructions are really interlocked.  */
475 #define hilo_interlocks \
476   (mips_opts.isa == ISA_MIPS32                        \
477    || mips_opts.isa == ISA_MIPS32R2                   \
478    || mips_opts.isa == ISA_MIPS64                     \
479    || mips_opts.isa == ISA_MIPS64R2                   \
480    || mips_opts.arch == CPU_R4010                     \
481    || mips_opts.arch == CPU_R5900                     \
482    || mips_opts.arch == CPU_R10000                    \
483    || mips_opts.arch == CPU_R12000                    \
484    || mips_opts.arch == CPU_R14000                    \
485    || mips_opts.arch == CPU_R16000                    \
486    || mips_opts.arch == CPU_RM7000                    \
487    || mips_opts.arch == CPU_VR5500                    \
488    || mips_opts.micromips                             \
489    )
490
491 /* Whether the processor uses hardware interlocks to protect reads
492    from the GPRs after they are loaded from memory, and thus does not
493    require nops to be inserted.  This applies to instructions marked
494    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
495    level I and microMIPS mode instructions are always interlocked.  */
496 #define gpr_interlocks                                \
497   (mips_opts.isa != ISA_MIPS1                         \
498    || mips_opts.arch == CPU_R3900                     \
499    || mips_opts.arch == CPU_R5900                     \
500    || mips_opts.micromips                             \
501    )
502
503 /* Whether the processor uses hardware interlocks to avoid delays
504    required by coprocessor instructions, and thus does not require
505    nops to be inserted.  This applies to instructions marked
506    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
507    between instructions marked INSN_WRITE_COND_CODE and ones marked
508    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
509    levels I, II, and III and microMIPS mode instructions are always
510    interlocked.  */
511 /* Itbl support may require additional care here.  */
512 #define cop_interlocks                                \
513   ((mips_opts.isa != ISA_MIPS1                        \
514     && mips_opts.isa != ISA_MIPS2                     \
515     && mips_opts.isa != ISA_MIPS3)                    \
516    || mips_opts.arch == CPU_R4300                     \
517    || mips_opts.micromips                             \
518    )
519
520 /* Whether the processor uses hardware interlocks to protect reads
521    from coprocessor registers after they are loaded from memory, and
522    thus does not require nops to be inserted.  This applies to
523    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
524    requires at MIPS ISA level I and microMIPS mode instructions are
525    always interlocked.  */
526 #define cop_mem_interlocks                            \
527   (mips_opts.isa != ISA_MIPS1                         \
528    || mips_opts.micromips                             \
529    )
530
531 /* Is this a mfhi or mflo instruction?  */
532 #define MF_HILO_INSN(PINFO) \
533   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
534
535 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
536    has been selected.  This implies, in particular, that addresses of text
537    labels have their LSB set.  */
538 #define HAVE_CODE_COMPRESSION                                           \
539   ((mips_opts.mips16 | mips_opts.micromips) != 0)
540
541 /* The minimum and maximum signed values that can be stored in a GPR.  */
542 #define GPR_SMAX ((offsetT) (((valueT) 1 << (HAVE_64BIT_GPRS ? 63 : 31)) - 1))
543 #define GPR_SMIN (-GPR_SMAX - 1)
544
545 /* MIPS PIC level.  */
546
547 enum mips_pic_level mips_pic;
548
549 /* 1 if we should generate 32 bit offsets from the $gp register in
550    SVR4_PIC mode.  Currently has no meaning in other modes.  */
551 static int mips_big_got = 0;
552
553 /* 1 if trap instructions should used for overflow rather than break
554    instructions.  */
555 static int mips_trap = 0;
556
557 /* 1 if double width floating point constants should not be constructed
558    by assembling two single width halves into two single width floating
559    point registers which just happen to alias the double width destination
560    register.  On some architectures this aliasing can be disabled by a bit
561    in the status register, and the setting of this bit cannot be determined
562    automatically at assemble time.  */
563 static int mips_disable_float_construction;
564
565 /* Non-zero if any .set noreorder directives were used.  */
566
567 static int mips_any_noreorder;
568
569 /* Non-zero if nops should be inserted when the register referenced in
570    an mfhi/mflo instruction is read in the next two instructions.  */
571 static int mips_7000_hilo_fix;
572
573 /* The size of objects in the small data section.  */
574 static unsigned int g_switch_value = 8;
575 /* Whether the -G option was used.  */
576 static int g_switch_seen = 0;
577
578 #define N_RMASK 0xc4
579 #define N_VFP   0xd4
580
581 /* If we can determine in advance that GP optimization won't be
582    possible, we can skip the relaxation stuff that tries to produce
583    GP-relative references.  This makes delay slot optimization work
584    better.
585
586    This function can only provide a guess, but it seems to work for
587    gcc output.  It needs to guess right for gcc, otherwise gcc
588    will put what it thinks is a GP-relative instruction in a branch
589    delay slot.
590
591    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
592    fixed it for the non-PIC mode.  KR 95/04/07  */
593 static int nopic_need_relax (symbolS *, int);
594
595 /* handle of the OPCODE hash table */
596 static struct hash_control *op_hash = NULL;
597
598 /* The opcode hash table we use for the mips16.  */
599 static struct hash_control *mips16_op_hash = NULL;
600
601 /* The opcode hash table we use for the microMIPS ASE.  */
602 static struct hash_control *micromips_op_hash = NULL;
603
604 /* This array holds the chars that always start a comment.  If the
605     pre-processor is disabled, these aren't very useful */
606 const char comment_chars[] = "#";
607
608 /* This array holds the chars that only start a comment at the beginning of
609    a line.  If the line seems to have the form '# 123 filename'
610    .line and .file directives will appear in the pre-processed output */
611 /* Note that input_file.c hand checks for '#' at the beginning of the
612    first line of the input file.  This is because the compiler outputs
613    #NO_APP at the beginning of its output.  */
614 /* Also note that C style comments are always supported.  */
615 const char line_comment_chars[] = "#";
616
617 /* This array holds machine specific line separator characters.  */
618 const char line_separator_chars[] = ";";
619
620 /* Chars that can be used to separate mant from exp in floating point nums */
621 const char EXP_CHARS[] = "eE";
622
623 /* Chars that mean this number is a floating point constant */
624 /* As in 0f12.456 */
625 /* or    0d1.2345e12 */
626 const char FLT_CHARS[] = "rRsSfFdDxXpP";
627
628 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
629    changed in read.c .  Ideally it shouldn't have to know about it at all,
630    but nothing is ideal around here.
631  */
632
633 static char *insn_error;
634
635 static int auto_align = 1;
636
637 /* When outputting SVR4 PIC code, the assembler needs to know the
638    offset in the stack frame from which to restore the $gp register.
639    This is set by the .cprestore pseudo-op, and saved in this
640    variable.  */
641 static offsetT mips_cprestore_offset = -1;
642
643 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
644    more optimizations, it can use a register value instead of a memory-saved
645    offset and even an other register than $gp as global pointer.  */
646 static offsetT mips_cpreturn_offset = -1;
647 static int mips_cpreturn_register = -1;
648 static int mips_gp_register = GP;
649 static int mips_gprel_offset = 0;
650
651 /* Whether mips_cprestore_offset has been set in the current function
652    (or whether it has already been warned about, if not).  */
653 static int mips_cprestore_valid = 0;
654
655 /* This is the register which holds the stack frame, as set by the
656    .frame pseudo-op.  This is needed to implement .cprestore.  */
657 static int mips_frame_reg = SP;
658
659 /* Whether mips_frame_reg has been set in the current function
660    (or whether it has already been warned about, if not).  */
661 static int mips_frame_reg_valid = 0;
662
663 /* To output NOP instructions correctly, we need to keep information
664    about the previous two instructions.  */
665
666 /* Whether we are optimizing.  The default value of 2 means to remove
667    unneeded NOPs and swap branch instructions when possible.  A value
668    of 1 means to not swap branches.  A value of 0 means to always
669    insert NOPs.  */
670 static int mips_optimize = 2;
671
672 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
673    equivalent to seeing no -g option at all.  */
674 static int mips_debug = 0;
675
676 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
677 #define MAX_VR4130_NOPS 4
678
679 /* The maximum number of NOPs needed to fill delay slots.  */
680 #define MAX_DELAY_NOPS 2
681
682 /* The maximum number of NOPs needed for any purpose.  */
683 #define MAX_NOPS 4
684
685 /* A list of previous instructions, with index 0 being the most recent.
686    We need to look back MAX_NOPS instructions when filling delay slots
687    or working around processor errata.  We need to look back one
688    instruction further if we're thinking about using history[0] to
689    fill a branch delay slot.  */
690 static struct mips_cl_insn history[1 + MAX_NOPS];
691
692 /* Nop instructions used by emit_nop.  */
693 static struct mips_cl_insn nop_insn;
694 static struct mips_cl_insn mips16_nop_insn;
695 static struct mips_cl_insn micromips_nop16_insn;
696 static struct mips_cl_insn micromips_nop32_insn;
697
698 /* The appropriate nop for the current mode.  */
699 #define NOP_INSN (mips_opts.mips16                                      \
700                   ? &mips16_nop_insn                                    \
701                   : (mips_opts.micromips                                \
702                      ? (mips_opts.insn32                                \
703                         ? &micromips_nop32_insn                         \
704                         : &micromips_nop16_insn)                        \
705                      : &nop_insn))
706
707 /* The size of NOP_INSN in bytes.  */
708 #define NOP_INSN_SIZE ((mips_opts.mips16                                \
709                         || (mips_opts.micromips && !mips_opts.insn32))  \
710                        ? 2 : 4)
711
712 /* If this is set, it points to a frag holding nop instructions which
713    were inserted before the start of a noreorder section.  If those
714    nops turn out to be unnecessary, the size of the frag can be
715    decreased.  */
716 static fragS *prev_nop_frag;
717
718 /* The number of nop instructions we created in prev_nop_frag.  */
719 static int prev_nop_frag_holds;
720
721 /* The number of nop instructions that we know we need in
722    prev_nop_frag.  */
723 static int prev_nop_frag_required;
724
725 /* The number of instructions we've seen since prev_nop_frag.  */
726 static int prev_nop_frag_since;
727
728 /* Relocations against symbols are sometimes done in two parts, with a HI
729    relocation and a LO relocation.  Each relocation has only 16 bits of
730    space to store an addend.  This means that in order for the linker to
731    handle carries correctly, it must be able to locate both the HI and
732    the LO relocation.  This means that the relocations must appear in
733    order in the relocation table.
734
735    In order to implement this, we keep track of each unmatched HI
736    relocation.  We then sort them so that they immediately precede the
737    corresponding LO relocation.  */
738
739 struct mips_hi_fixup
740 {
741   /* Next HI fixup.  */
742   struct mips_hi_fixup *next;
743   /* This fixup.  */
744   fixS *fixp;
745   /* The section this fixup is in.  */
746   segT seg;
747 };
748
749 /* The list of unmatched HI relocs.  */
750
751 static struct mips_hi_fixup *mips_hi_fixup_list;
752
753 /* The frag containing the last explicit relocation operator.
754    Null if explicit relocations have not been used.  */
755
756 static fragS *prev_reloc_op_frag;
757
758 /* Map mips16 register numbers to normal MIPS register numbers.  */
759
760 static const unsigned int mips16_to_32_reg_map[] =
761 {
762   16, 17, 2, 3, 4, 5, 6, 7
763 };
764
765 /* Map microMIPS register numbers to normal MIPS register numbers.  */
766
767 #define micromips_to_32_reg_b_map       mips16_to_32_reg_map
768 #define micromips_to_32_reg_c_map       mips16_to_32_reg_map
769 #define micromips_to_32_reg_d_map       mips16_to_32_reg_map
770 #define micromips_to_32_reg_e_map       mips16_to_32_reg_map
771 #define micromips_to_32_reg_f_map       mips16_to_32_reg_map
772 #define micromips_to_32_reg_g_map       mips16_to_32_reg_map
773
774 /* The microMIPS registers with type h.  */
775 static const unsigned int micromips_to_32_reg_h_map1[] =
776 {
777   5, 5, 6, 4, 4, 4, 4, 4
778 };
779 static const unsigned int micromips_to_32_reg_h_map2[] =
780 {
781   6, 7, 7, 21, 22, 5, 6, 7
782 };
783
784 #define micromips_to_32_reg_l_map       mips16_to_32_reg_map
785
786 /* The microMIPS registers with type m.  */
787 static const unsigned int micromips_to_32_reg_m_map[] =
788 {
789   0, 17, 2, 3, 16, 18, 19, 20
790 };
791
792 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
793
794 /* The microMIPS registers with type q.  */
795 static const unsigned int micromips_to_32_reg_q_map[] =
796 {
797   0, 17, 2, 3, 4, 5, 6, 7
798 };
799
800 /* Classifies the kind of instructions we're interested in when
801    implementing -mfix-vr4120.  */
802 enum fix_vr4120_class
803 {
804   FIX_VR4120_MACC,
805   FIX_VR4120_DMACC,
806   FIX_VR4120_MULT,
807   FIX_VR4120_DMULT,
808   FIX_VR4120_DIV,
809   FIX_VR4120_MTHILO,
810   NUM_FIX_VR4120_CLASSES
811 };
812
813 /* ...likewise -mfix-loongson2f-jump.  */
814 static bfd_boolean mips_fix_loongson2f_jump;
815
816 /* ...likewise -mfix-loongson2f-nop.  */
817 static bfd_boolean mips_fix_loongson2f_nop;
818
819 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
820 static bfd_boolean mips_fix_loongson2f;
821
822 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
823    there must be at least one other instruction between an instruction
824    of type X and an instruction of type Y.  */
825 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
826
827 /* True if -mfix-vr4120 is in force.  */
828 static int mips_fix_vr4120;
829
830 /* ...likewise -mfix-vr4130.  */
831 static int mips_fix_vr4130;
832
833 /* ...likewise -mfix-24k.  */
834 static int mips_fix_24k;
835
836 /* ...likewise -mfix-cn63xxp1 */
837 static bfd_boolean mips_fix_cn63xxp1;
838
839 /* We don't relax branches by default, since this causes us to expand
840    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
841    fail to compute the offset before expanding the macro to the most
842    efficient expansion.  */
843
844 static int mips_relax_branch;
845 \f
846 /* The expansion of many macros depends on the type of symbol that
847    they refer to.  For example, when generating position-dependent code,
848    a macro that refers to a symbol may have two different expansions,
849    one which uses GP-relative addresses and one which uses absolute
850    addresses.  When generating SVR4-style PIC, a macro may have
851    different expansions for local and global symbols.
852
853    We handle these situations by generating both sequences and putting
854    them in variant frags.  In position-dependent code, the first sequence
855    will be the GP-relative one and the second sequence will be the
856    absolute one.  In SVR4 PIC, the first sequence will be for global
857    symbols and the second will be for local symbols.
858
859    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
860    SECOND are the lengths of the two sequences in bytes.  These fields
861    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
862    the subtype has the following flags:
863
864    RELAX_USE_SECOND
865         Set if it has been decided that we should use the second
866         sequence instead of the first.
867
868    RELAX_SECOND_LONGER
869         Set in the first variant frag if the macro's second implementation
870         is longer than its first.  This refers to the macro as a whole,
871         not an individual relaxation.
872
873    RELAX_NOMACRO
874         Set in the first variant frag if the macro appeared in a .set nomacro
875         block and if one alternative requires a warning but the other does not.
876
877    RELAX_DELAY_SLOT
878         Like RELAX_NOMACRO, but indicates that the macro appears in a branch
879         delay slot.
880
881    RELAX_DELAY_SLOT_16BIT
882         Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
883         16-bit instruction.
884
885    RELAX_DELAY_SLOT_SIZE_FIRST
886         Like RELAX_DELAY_SLOT, but indicates that the first implementation of
887         the macro is of the wrong size for the branch delay slot.
888
889    RELAX_DELAY_SLOT_SIZE_SECOND
890         Like RELAX_DELAY_SLOT, but indicates that the second implementation of
891         the macro is of the wrong size for the branch delay slot.
892
893    The frag's "opcode" points to the first fixup for relaxable code.
894
895    Relaxable macros are generated using a sequence such as:
896
897       relax_start (SYMBOL);
898       ... generate first expansion ...
899       relax_switch ();
900       ... generate second expansion ...
901       relax_end ();
902
903    The code and fixups for the unwanted alternative are discarded
904    by md_convert_frag.  */
905 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
906
907 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
908 #define RELAX_SECOND(X) ((X) & 0xff)
909 #define RELAX_USE_SECOND 0x10000
910 #define RELAX_SECOND_LONGER 0x20000
911 #define RELAX_NOMACRO 0x40000
912 #define RELAX_DELAY_SLOT 0x80000
913 #define RELAX_DELAY_SLOT_16BIT 0x100000
914 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
915 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
916
917 /* Branch without likely bit.  If label is out of range, we turn:
918
919         beq reg1, reg2, label
920         delay slot
921
922    into
923
924         bne reg1, reg2, 0f
925         nop
926         j label
927      0: delay slot
928
929    with the following opcode replacements:
930
931         beq <-> bne
932         blez <-> bgtz
933         bltz <-> bgez
934         bc1f <-> bc1t
935
936         bltzal <-> bgezal  (with jal label instead of j label)
937
938    Even though keeping the delay slot instruction in the delay slot of
939    the branch would be more efficient, it would be very tricky to do
940    correctly, because we'd have to introduce a variable frag *after*
941    the delay slot instruction, and expand that instead.  Let's do it
942    the easy way for now, even if the branch-not-taken case now costs
943    one additional instruction.  Out-of-range branches are not supposed
944    to be common, anyway.
945
946    Branch likely.  If label is out of range, we turn:
947
948         beql reg1, reg2, label
949         delay slot (annulled if branch not taken)
950
951    into
952
953         beql reg1, reg2, 1f
954         nop
955         beql $0, $0, 2f
956         nop
957      1: j[al] label
958         delay slot (executed only if branch taken)
959      2:
960
961    It would be possible to generate a shorter sequence by losing the
962    likely bit, generating something like:
963
964         bne reg1, reg2, 0f
965         nop
966         j[al] label
967         delay slot (executed only if branch taken)
968      0:
969
970         beql -> bne
971         bnel -> beq
972         blezl -> bgtz
973         bgtzl -> blez
974         bltzl -> bgez
975         bgezl -> bltz
976         bc1fl -> bc1t
977         bc1tl -> bc1f
978
979         bltzall -> bgezal  (with jal label instead of j label)
980         bgezall -> bltzal  (ditto)
981
982
983    but it's not clear that it would actually improve performance.  */
984 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar)   \
985   ((relax_substateT)                                            \
986    (0xc0000000                                                  \
987     | ((at) & 0x1f)                                             \
988     | ((toofar) ? 0x20 : 0)                                     \
989     | ((link) ? 0x40 : 0)                                       \
990     | ((likely) ? 0x80 : 0)                                     \
991     | ((uncond) ? 0x100 : 0)))
992 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
993 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
994 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
995 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
996 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
997 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
998
999 /* For mips16 code, we use an entirely different form of relaxation.
1000    mips16 supports two versions of most instructions which take
1001    immediate values: a small one which takes some small value, and a
1002    larger one which takes a 16 bit value.  Since branches also follow
1003    this pattern, relaxing these values is required.
1004
1005    We can assemble both mips16 and normal MIPS code in a single
1006    object.  Therefore, we need to support this type of relaxation at
1007    the same time that we support the relaxation described above.  We
1008    use the high bit of the subtype field to distinguish these cases.
1009
1010    The information we store for this type of relaxation is the
1011    argument code found in the opcode file for this relocation, whether
1012    the user explicitly requested a small or extended form, and whether
1013    the relocation is in a jump or jal delay slot.  That tells us the
1014    size of the value, and how it should be stored.  We also store
1015    whether the fragment is considered to be extended or not.  We also
1016    store whether this is known to be a branch to a different section,
1017    whether we have tried to relax this frag yet, and whether we have
1018    ever extended a PC relative fragment because of a shift count.  */
1019 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1020   (0x80000000                                                   \
1021    | ((type) & 0xff)                                            \
1022    | ((small) ? 0x100 : 0)                                      \
1023    | ((ext) ? 0x200 : 0)                                        \
1024    | ((dslot) ? 0x400 : 0)                                      \
1025    | ((jal_dslot) ? 0x800 : 0))
1026 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1027 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1028 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1029 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1030 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1031 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1032 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1033 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1034 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1035 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1036 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1037 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1038
1039 /* For microMIPS code, we use relaxation similar to one we use for
1040    MIPS16 code.  Some instructions that take immediate values support
1041    two encodings: a small one which takes some small value, and a
1042    larger one which takes a 16 bit value.  As some branches also follow
1043    this pattern, relaxing these values is required.
1044
1045    We can assemble both microMIPS and normal MIPS code in a single
1046    object.  Therefore, we need to support this type of relaxation at
1047    the same time that we support the relaxation described above.  We
1048    use one of the high bits of the subtype field to distinguish these
1049    cases.
1050
1051    The information we store for this type of relaxation is the argument
1052    code found in the opcode file for this relocation, the register
1053    selected as the assembler temporary, whether the branch is
1054    unconditional, whether it is compact, whether it stores the link
1055    address implicitly in $ra, whether relaxation of out-of-range 32-bit
1056    branches to a sequence of instructions is enabled, and whether the
1057    displacement of a branch is too large to fit as an immediate argument
1058    of a 16-bit and a 32-bit branch, respectively.  */
1059 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1060                                relax32, toofar16, toofar32)     \
1061   (0x40000000                                                   \
1062    | ((type) & 0xff)                                            \
1063    | (((at) & 0x1f) << 8)                                       \
1064    | ((uncond) ? 0x2000 : 0)                                    \
1065    | ((compact) ? 0x4000 : 0)                                   \
1066    | ((link) ? 0x8000 : 0)                                      \
1067    | ((relax32) ? 0x10000 : 0)                                  \
1068    | ((toofar16) ? 0x20000 : 0)                                 \
1069    | ((toofar32) ? 0x40000 : 0))
1070 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1071 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1072 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1073 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1074 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1075 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1076 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1077
1078 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1079 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1080 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1081 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1082 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1083 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1084
1085 /* Sign-extend 16-bit value X.  */
1086 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1087
1088 /* Is the given value a sign-extended 32-bit value?  */
1089 #define IS_SEXT_32BIT_NUM(x)                                            \
1090   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
1091    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1092
1093 /* Is the given value a sign-extended 16-bit value?  */
1094 #define IS_SEXT_16BIT_NUM(x)                                            \
1095   (((x) &~ (offsetT) 0x7fff) == 0                                       \
1096    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1097
1098 /* Is the given value a sign-extended 12-bit value?  */
1099 #define IS_SEXT_12BIT_NUM(x)                                            \
1100   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1101
1102 /* Is the given value a sign-extended 9-bit value?  */
1103 #define IS_SEXT_9BIT_NUM(x)                                             \
1104   (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1105
1106 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1107 #define IS_ZEXT_32BIT_NUM(x)                                            \
1108   (((x) &~ (offsetT) 0xffffffff) == 0                                   \
1109    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1110
1111 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1112    SHIFT places.  */
1113 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1114   (((STRUCT) >> (SHIFT)) & (MASK))
1115
1116 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1117 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1118   (!(MICROMIPS) \
1119    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1120    : EXTRACT_BITS ((INSN).insn_opcode, \
1121                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1122 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1123   EXTRACT_BITS ((INSN).insn_opcode, \
1124                 MIPS16OP_MASK_##FIELD, \
1125                 MIPS16OP_SH_##FIELD)
1126
1127 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1128 #define MIPS16_EXTEND (0xf000U << 16)
1129 \f
1130 /* Whether or not we are emitting a branch-likely macro.  */
1131 static bfd_boolean emit_branch_likely_macro = FALSE;
1132
1133 /* Global variables used when generating relaxable macros.  See the
1134    comment above RELAX_ENCODE for more details about how relaxation
1135    is used.  */
1136 static struct {
1137   /* 0 if we're not emitting a relaxable macro.
1138      1 if we're emitting the first of the two relaxation alternatives.
1139      2 if we're emitting the second alternative.  */
1140   int sequence;
1141
1142   /* The first relaxable fixup in the current frag.  (In other words,
1143      the first fixup that refers to relaxable code.)  */
1144   fixS *first_fixup;
1145
1146   /* sizes[0] says how many bytes of the first alternative are stored in
1147      the current frag.  Likewise sizes[1] for the second alternative.  */
1148   unsigned int sizes[2];
1149
1150   /* The symbol on which the choice of sequence depends.  */
1151   symbolS *symbol;
1152 } mips_relax;
1153 \f
1154 /* Global variables used to decide whether a macro needs a warning.  */
1155 static struct {
1156   /* True if the macro is in a branch delay slot.  */
1157   bfd_boolean delay_slot_p;
1158
1159   /* Set to the length in bytes required if the macro is in a delay slot
1160      that requires a specific length of instruction, otherwise zero.  */
1161   unsigned int delay_slot_length;
1162
1163   /* For relaxable macros, sizes[0] is the length of the first alternative
1164      in bytes and sizes[1] is the length of the second alternative.
1165      For non-relaxable macros, both elements give the length of the
1166      macro in bytes.  */
1167   unsigned int sizes[2];
1168
1169   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1170      instruction of the first alternative in bytes and first_insn_sizes[1]
1171      is the length of the first instruction of the second alternative.
1172      For non-relaxable macros, both elements give the length of the first
1173      instruction in bytes.
1174
1175      Set to zero if we haven't yet seen the first instruction.  */
1176   unsigned int first_insn_sizes[2];
1177
1178   /* For relaxable macros, insns[0] is the number of instructions for the
1179      first alternative and insns[1] is the number of instructions for the
1180      second alternative.
1181
1182      For non-relaxable macros, both elements give the number of
1183      instructions for the macro.  */
1184   unsigned int insns[2];
1185
1186   /* The first variant frag for this macro.  */
1187   fragS *first_frag;
1188 } mips_macro_warning;
1189 \f
1190 /* Prototypes for static functions.  */
1191
1192 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1193
1194 static void append_insn
1195   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1196    bfd_boolean expansionp);
1197 static void mips_no_prev_insn (void);
1198 static void macro_build (expressionS *, const char *, const char *, ...);
1199 static void mips16_macro_build
1200   (expressionS *, const char *, const char *, va_list *);
1201 static void load_register (int, expressionS *, int);
1202 static void macro_start (void);
1203 static void macro_end (void);
1204 static void macro (struct mips_cl_insn *ip, char *str);
1205 static void mips16_macro (struct mips_cl_insn * ip);
1206 static void mips_ip (char *str, struct mips_cl_insn * ip);
1207 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1208 static void mips16_immed
1209   (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1210    unsigned int, unsigned long *);
1211 static size_t my_getSmallExpression
1212   (expressionS *, bfd_reloc_code_real_type *, char *);
1213 static void my_getExpression (expressionS *, char *);
1214 static void s_align (int);
1215 static void s_change_sec (int);
1216 static void s_change_section (int);
1217 static void s_cons (int);
1218 static void s_float_cons (int);
1219 static void s_mips_globl (int);
1220 static void s_option (int);
1221 static void s_mipsset (int);
1222 static void s_abicalls (int);
1223 static void s_cpload (int);
1224 static void s_cpsetup (int);
1225 static void s_cplocal (int);
1226 static void s_cprestore (int);
1227 static void s_cpreturn (int);
1228 static void s_dtprelword (int);
1229 static void s_dtpreldword (int);
1230 static void s_tprelword (int);
1231 static void s_tpreldword (int);
1232 static void s_gpvalue (int);
1233 static void s_gpword (int);
1234 static void s_gpdword (int);
1235 static void s_ehword (int);
1236 static void s_cpadd (int);
1237 static void s_insn (int);
1238 static void s_nan (int);
1239 static void md_obj_begin (void);
1240 static void md_obj_end (void);
1241 static void s_mips_ent (int);
1242 static void s_mips_end (int);
1243 static void s_mips_frame (int);
1244 static void s_mips_mask (int reg_type);
1245 static void s_mips_stab (int);
1246 static void s_mips_weakext (int);
1247 static void s_mips_file (int);
1248 static void s_mips_loc (int);
1249 static bfd_boolean pic_need_relax (symbolS *, asection *);
1250 static int relaxed_branch_length (fragS *, asection *, int);
1251 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1252 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1253
1254 /* Table and functions used to map between CPU/ISA names, and
1255    ISA levels, and CPU numbers.  */
1256
1257 struct mips_cpu_info
1258 {
1259   const char *name;           /* CPU or ISA name.  */
1260   int flags;                  /* MIPS_CPU_* flags.  */
1261   int ase;                    /* Set of ASEs implemented by the CPU.  */
1262   int isa;                    /* ISA level.  */
1263   int cpu;                    /* CPU number (default CPU if ISA).  */
1264 };
1265
1266 #define MIPS_CPU_IS_ISA         0x0001  /* Is this an ISA?  (If 0, a CPU.) */
1267
1268 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1269 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1270 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1271 \f
1272 /* Command-line options.  */
1273 const char *md_shortopts = "O::g::G:";
1274
1275 enum options
1276   {
1277     OPTION_MARCH = OPTION_MD_BASE,
1278     OPTION_MTUNE,
1279     OPTION_MIPS1,
1280     OPTION_MIPS2,
1281     OPTION_MIPS3,
1282     OPTION_MIPS4,
1283     OPTION_MIPS5,
1284     OPTION_MIPS32,
1285     OPTION_MIPS64,
1286     OPTION_MIPS32R2,
1287     OPTION_MIPS64R2,
1288     OPTION_MIPS16,
1289     OPTION_NO_MIPS16,
1290     OPTION_MIPS3D,
1291     OPTION_NO_MIPS3D,
1292     OPTION_MDMX,
1293     OPTION_NO_MDMX,
1294     OPTION_DSP,
1295     OPTION_NO_DSP,
1296     OPTION_MT,
1297     OPTION_NO_MT,
1298     OPTION_VIRT,
1299     OPTION_NO_VIRT,
1300     OPTION_SMARTMIPS,
1301     OPTION_NO_SMARTMIPS,
1302     OPTION_DSPR2,
1303     OPTION_NO_DSPR2,
1304     OPTION_EVA,
1305     OPTION_NO_EVA,
1306     OPTION_MICROMIPS,
1307     OPTION_NO_MICROMIPS,
1308     OPTION_MCU,
1309     OPTION_NO_MCU,
1310     OPTION_COMPAT_ARCH_BASE,
1311     OPTION_M4650,
1312     OPTION_NO_M4650,
1313     OPTION_M4010,
1314     OPTION_NO_M4010,
1315     OPTION_M4100,
1316     OPTION_NO_M4100,
1317     OPTION_M3900,
1318     OPTION_NO_M3900,
1319     OPTION_M7000_HILO_FIX,
1320     OPTION_MNO_7000_HILO_FIX,
1321     OPTION_FIX_24K,
1322     OPTION_NO_FIX_24K,
1323     OPTION_FIX_LOONGSON2F_JUMP,
1324     OPTION_NO_FIX_LOONGSON2F_JUMP,
1325     OPTION_FIX_LOONGSON2F_NOP,
1326     OPTION_NO_FIX_LOONGSON2F_NOP,
1327     OPTION_FIX_VR4120,
1328     OPTION_NO_FIX_VR4120,
1329     OPTION_FIX_VR4130,
1330     OPTION_NO_FIX_VR4130,
1331     OPTION_FIX_CN63XXP1,
1332     OPTION_NO_FIX_CN63XXP1,
1333     OPTION_TRAP,
1334     OPTION_BREAK,
1335     OPTION_EB,
1336     OPTION_EL,
1337     OPTION_FP32,
1338     OPTION_GP32,
1339     OPTION_CONSTRUCT_FLOATS,
1340     OPTION_NO_CONSTRUCT_FLOATS,
1341     OPTION_FP64,
1342     OPTION_GP64,
1343     OPTION_RELAX_BRANCH,
1344     OPTION_NO_RELAX_BRANCH,
1345     OPTION_INSN32,
1346     OPTION_NO_INSN32,
1347     OPTION_MSHARED,
1348     OPTION_MNO_SHARED,
1349     OPTION_MSYM32,
1350     OPTION_MNO_SYM32,
1351     OPTION_SOFT_FLOAT,
1352     OPTION_HARD_FLOAT,
1353     OPTION_SINGLE_FLOAT,
1354     OPTION_DOUBLE_FLOAT,
1355     OPTION_32,
1356     OPTION_CALL_SHARED,
1357     OPTION_CALL_NONPIC,
1358     OPTION_NON_SHARED,
1359     OPTION_XGOT,
1360     OPTION_MABI,
1361     OPTION_N32,
1362     OPTION_64,
1363     OPTION_MDEBUG,
1364     OPTION_NO_MDEBUG,
1365     OPTION_PDR,
1366     OPTION_NO_PDR,
1367     OPTION_MVXWORKS_PIC,
1368     OPTION_NAN,
1369     OPTION_END_OF_ENUM
1370   };
1371
1372 struct option md_longopts[] =
1373 {
1374   /* Options which specify architecture.  */
1375   {"march", required_argument, NULL, OPTION_MARCH},
1376   {"mtune", required_argument, NULL, OPTION_MTUNE},
1377   {"mips0", no_argument, NULL, OPTION_MIPS1},
1378   {"mips1", no_argument, NULL, OPTION_MIPS1},
1379   {"mips2", no_argument, NULL, OPTION_MIPS2},
1380   {"mips3", no_argument, NULL, OPTION_MIPS3},
1381   {"mips4", no_argument, NULL, OPTION_MIPS4},
1382   {"mips5", no_argument, NULL, OPTION_MIPS5},
1383   {"mips32", no_argument, NULL, OPTION_MIPS32},
1384   {"mips64", no_argument, NULL, OPTION_MIPS64},
1385   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1386   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1387
1388   /* Options which specify Application Specific Extensions (ASEs).  */
1389   {"mips16", no_argument, NULL, OPTION_MIPS16},
1390   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1391   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1392   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1393   {"mdmx", no_argument, NULL, OPTION_MDMX},
1394   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1395   {"mdsp", no_argument, NULL, OPTION_DSP},
1396   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1397   {"mmt", no_argument, NULL, OPTION_MT},
1398   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1399   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1400   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1401   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1402   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1403   {"meva", no_argument, NULL, OPTION_EVA},
1404   {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1405   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1406   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1407   {"mmcu", no_argument, NULL, OPTION_MCU},
1408   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1409   {"mvirt", no_argument, NULL, OPTION_VIRT},
1410   {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1411
1412   /* Old-style architecture options.  Don't add more of these.  */
1413   {"m4650", no_argument, NULL, OPTION_M4650},
1414   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1415   {"m4010", no_argument, NULL, OPTION_M4010},
1416   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1417   {"m4100", no_argument, NULL, OPTION_M4100},
1418   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1419   {"m3900", no_argument, NULL, OPTION_M3900},
1420   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1421
1422   /* Options which enable bug fixes.  */
1423   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1424   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1425   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1426   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1427   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1428   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1429   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1430   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
1431   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1432   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
1433   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1434   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
1435   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1436   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1437   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1438
1439   /* Miscellaneous options.  */
1440   {"trap", no_argument, NULL, OPTION_TRAP},
1441   {"no-break", no_argument, NULL, OPTION_TRAP},
1442   {"break", no_argument, NULL, OPTION_BREAK},
1443   {"no-trap", no_argument, NULL, OPTION_BREAK},
1444   {"EB", no_argument, NULL, OPTION_EB},
1445   {"EL", no_argument, NULL, OPTION_EL},
1446   {"mfp32", no_argument, NULL, OPTION_FP32},
1447   {"mgp32", no_argument, NULL, OPTION_GP32},
1448   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1449   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1450   {"mfp64", no_argument, NULL, OPTION_FP64},
1451   {"mgp64", no_argument, NULL, OPTION_GP64},
1452   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1453   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1454   {"minsn32", no_argument, NULL, OPTION_INSN32},
1455   {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1456   {"mshared", no_argument, NULL, OPTION_MSHARED},
1457   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1458   {"msym32", no_argument, NULL, OPTION_MSYM32},
1459   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1460   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1461   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1462   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1463   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1464
1465   /* Strictly speaking this next option is ELF specific,
1466      but we allow it for other ports as well in order to
1467      make testing easier.  */
1468   {"32", no_argument, NULL, OPTION_32},
1469
1470   /* ELF-specific options.  */
1471   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1472   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1473   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1474   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
1475   {"xgot", no_argument, NULL, OPTION_XGOT},
1476   {"mabi", required_argument, NULL, OPTION_MABI},
1477   {"n32", no_argument, NULL, OPTION_N32},
1478   {"64", no_argument, NULL, OPTION_64},
1479   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1480   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1481   {"mpdr", no_argument, NULL, OPTION_PDR},
1482   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1483   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1484   {"mnan", required_argument, NULL, OPTION_NAN},
1485
1486   {NULL, no_argument, NULL, 0}
1487 };
1488 size_t md_longopts_size = sizeof (md_longopts);
1489 \f
1490 /* Information about either an Application Specific Extension or an
1491    optional architecture feature that, for simplicity, we treat in the
1492    same way as an ASE.  */
1493 struct mips_ase
1494 {
1495   /* The name of the ASE, used in both the command-line and .set options.  */
1496   const char *name;
1497
1498   /* The associated ASE_* flags.  If the ASE is available on both 32-bit
1499      and 64-bit architectures, the flags here refer to the subset that
1500      is available on both.  */
1501   unsigned int flags;
1502
1503   /* The ASE_* flag used for instructions that are available on 64-bit
1504      architectures but that are not included in FLAGS.  */
1505   unsigned int flags64;
1506
1507   /* The command-line options that turn the ASE on and off.  */
1508   int option_on;
1509   int option_off;
1510
1511   /* The minimum required architecture revisions for MIPS32, MIPS64,
1512      microMIPS32 and microMIPS64, or -1 if the extension isn't supported.  */
1513   int mips32_rev;
1514   int mips64_rev;
1515   int micromips32_rev;
1516   int micromips64_rev;
1517 };
1518
1519 /* A table of all supported ASEs.  */
1520 static const struct mips_ase mips_ases[] = {
1521   { "dsp", ASE_DSP, ASE_DSP64,
1522     OPTION_DSP, OPTION_NO_DSP,
1523     2, 2, 2, 2 },
1524
1525   { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1526     OPTION_DSPR2, OPTION_NO_DSPR2,
1527     2, 2, 2, 2 },
1528
1529   { "eva", ASE_EVA, 0,
1530     OPTION_EVA, OPTION_NO_EVA,
1531     2, 2, 2, 2 },
1532
1533   { "mcu", ASE_MCU, 0,
1534     OPTION_MCU, OPTION_NO_MCU,
1535     2, 2, 2, 2 },
1536
1537   /* Deprecated in MIPS64r5, but we don't implement that yet.  */
1538   { "mdmx", ASE_MDMX, 0,
1539     OPTION_MDMX, OPTION_NO_MDMX,
1540     -1, 1, -1, -1 },
1541
1542   /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2.  */
1543   { "mips3d", ASE_MIPS3D, 0,
1544     OPTION_MIPS3D, OPTION_NO_MIPS3D,
1545     2, 1, -1, -1 },
1546
1547   { "mt", ASE_MT, 0,
1548     OPTION_MT, OPTION_NO_MT,
1549     2, 2, -1, -1 },
1550
1551   { "smartmips", ASE_SMARTMIPS, 0,
1552     OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1553     1, -1, -1, -1 },
1554
1555   { "virt", ASE_VIRT, ASE_VIRT64,
1556     OPTION_VIRT, OPTION_NO_VIRT,
1557     2, 2, 2, 2 }
1558 };
1559
1560 /* The set of ASEs that require -mfp64.  */
1561 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1562
1563 /* Groups of ASE_* flags that represent different revisions of an ASE.  */
1564 static const unsigned int mips_ase_groups[] = {
1565   ASE_DSP | ASE_DSPR2
1566 };
1567 \f
1568 /* Pseudo-op table.
1569
1570    The following pseudo-ops from the Kane and Heinrich MIPS book
1571    should be defined here, but are currently unsupported: .alias,
1572    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1573
1574    The following pseudo-ops from the Kane and Heinrich MIPS book are
1575    specific to the type of debugging information being generated, and
1576    should be defined by the object format: .aent, .begin, .bend,
1577    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1578    .vreg.
1579
1580    The following pseudo-ops from the Kane and Heinrich MIPS book are
1581    not MIPS CPU specific, but are also not specific to the object file
1582    format.  This file is probably the best place to define them, but
1583    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1584
1585 static const pseudo_typeS mips_pseudo_table[] =
1586 {
1587   /* MIPS specific pseudo-ops.  */
1588   {"option", s_option, 0},
1589   {"set", s_mipsset, 0},
1590   {"rdata", s_change_sec, 'r'},
1591   {"sdata", s_change_sec, 's'},
1592   {"livereg", s_ignore, 0},
1593   {"abicalls", s_abicalls, 0},
1594   {"cpload", s_cpload, 0},
1595   {"cpsetup", s_cpsetup, 0},
1596   {"cplocal", s_cplocal, 0},
1597   {"cprestore", s_cprestore, 0},
1598   {"cpreturn", s_cpreturn, 0},
1599   {"dtprelword", s_dtprelword, 0},
1600   {"dtpreldword", s_dtpreldword, 0},
1601   {"tprelword", s_tprelword, 0},
1602   {"tpreldword", s_tpreldword, 0},
1603   {"gpvalue", s_gpvalue, 0},
1604   {"gpword", s_gpword, 0},
1605   {"gpdword", s_gpdword, 0},
1606   {"ehword", s_ehword, 0},
1607   {"cpadd", s_cpadd, 0},
1608   {"insn", s_insn, 0},
1609   {"nan", s_nan, 0},
1610
1611   /* Relatively generic pseudo-ops that happen to be used on MIPS
1612      chips.  */
1613   {"asciiz", stringer, 8 + 1},
1614   {"bss", s_change_sec, 'b'},
1615   {"err", s_err, 0},
1616   {"half", s_cons, 1},
1617   {"dword", s_cons, 3},
1618   {"weakext", s_mips_weakext, 0},
1619   {"origin", s_org, 0},
1620   {"repeat", s_rept, 0},
1621
1622   /* For MIPS this is non-standard, but we define it for consistency.  */
1623   {"sbss", s_change_sec, 'B'},
1624
1625   /* These pseudo-ops are defined in read.c, but must be overridden
1626      here for one reason or another.  */
1627   {"align", s_align, 0},
1628   {"byte", s_cons, 0},
1629   {"data", s_change_sec, 'd'},
1630   {"double", s_float_cons, 'd'},
1631   {"float", s_float_cons, 'f'},
1632   {"globl", s_mips_globl, 0},
1633   {"global", s_mips_globl, 0},
1634   {"hword", s_cons, 1},
1635   {"int", s_cons, 2},
1636   {"long", s_cons, 2},
1637   {"octa", s_cons, 4},
1638   {"quad", s_cons, 3},
1639   {"section", s_change_section, 0},
1640   {"short", s_cons, 1},
1641   {"single", s_float_cons, 'f'},
1642   {"stabd", s_mips_stab, 'd'},
1643   {"stabn", s_mips_stab, 'n'},
1644   {"stabs", s_mips_stab, 's'},
1645   {"text", s_change_sec, 't'},
1646   {"word", s_cons, 2},
1647
1648   { "extern", ecoff_directive_extern, 0},
1649
1650   { NULL, NULL, 0 },
1651 };
1652
1653 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1654 {
1655   /* These pseudo-ops should be defined by the object file format.
1656      However, a.out doesn't support them, so we have versions here.  */
1657   {"aent", s_mips_ent, 1},
1658   {"bgnb", s_ignore, 0},
1659   {"end", s_mips_end, 0},
1660   {"endb", s_ignore, 0},
1661   {"ent", s_mips_ent, 0},
1662   {"file", s_mips_file, 0},
1663   {"fmask", s_mips_mask, 'F'},
1664   {"frame", s_mips_frame, 0},
1665   {"loc", s_mips_loc, 0},
1666   {"mask", s_mips_mask, 'R'},
1667   {"verstamp", s_ignore, 0},
1668   { NULL, NULL, 0 },
1669 };
1670
1671 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1672    purpose of the `.dc.a' internal pseudo-op.  */
1673
1674 int
1675 mips_address_bytes (void)
1676 {
1677   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1678 }
1679
1680 extern void pop_insert (const pseudo_typeS *);
1681
1682 void
1683 mips_pop_insert (void)
1684 {
1685   pop_insert (mips_pseudo_table);
1686   if (! ECOFF_DEBUGGING)
1687     pop_insert (mips_nonecoff_pseudo_table);
1688 }
1689 \f
1690 /* Symbols labelling the current insn.  */
1691
1692 struct insn_label_list
1693 {
1694   struct insn_label_list *next;
1695   symbolS *label;
1696 };
1697
1698 static struct insn_label_list *free_insn_labels;
1699 #define label_list tc_segment_info_data.labels
1700
1701 static void mips_clear_insn_labels (void);
1702 static void mips_mark_labels (void);
1703 static void mips_compressed_mark_labels (void);
1704
1705 static inline void
1706 mips_clear_insn_labels (void)
1707 {
1708   register struct insn_label_list **pl;
1709   segment_info_type *si;
1710
1711   if (now_seg)
1712     {
1713       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1714         ;
1715       
1716       si = seg_info (now_seg);
1717       *pl = si->label_list;
1718       si->label_list = NULL;
1719     }
1720 }
1721
1722 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1723
1724 static inline void
1725 mips_mark_labels (void)
1726 {
1727   if (HAVE_CODE_COMPRESSION)
1728     mips_compressed_mark_labels ();
1729 }
1730 \f
1731 static char *expr_end;
1732
1733 /* Expressions which appear in macro instructions.  These are set by
1734    mips_ip and read by macro.  */
1735
1736 static expressionS imm_expr;
1737 static expressionS imm2_expr;
1738
1739 /* The relocatable field in an instruction and the relocs associated
1740    with it.  These variables are used for instructions like LUI and
1741    JAL as well as true offsets.  They are also used for address
1742    operands in macros.  */
1743
1744 static expressionS offset_expr;
1745 static bfd_reloc_code_real_type offset_reloc[3]
1746   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1747
1748 /* This is set to the resulting size of the instruction to be produced
1749    by mips16_ip if an explicit extension is used or by mips_ip if an
1750    explicit size is supplied.  */
1751
1752 static unsigned int forced_insn_length;
1753
1754 /* True if we are assembling an instruction.  All dot symbols defined during
1755    this time should be treated as code labels.  */
1756
1757 static bfd_boolean mips_assembling_insn;
1758
1759 /* The pdr segment for per procedure frame/regmask info.  Not used for
1760    ECOFF debugging.  */
1761
1762 static segT pdr_seg;
1763
1764 /* The default target format to use.  */
1765
1766 #if defined (TE_FreeBSD)
1767 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1768 #elif defined (TE_TMIPS)
1769 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1770 #else
1771 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1772 #endif
1773
1774 const char *
1775 mips_target_format (void)
1776 {
1777   switch (OUTPUT_FLAVOR)
1778     {
1779     case bfd_target_elf_flavour:
1780 #ifdef TE_VXWORKS
1781       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1782         return (target_big_endian
1783                 ? "elf32-bigmips-vxworks"
1784                 : "elf32-littlemips-vxworks");
1785 #endif
1786       return (target_big_endian
1787               ? (HAVE_64BIT_OBJECTS
1788                  ? ELF_TARGET ("elf64-", "big")
1789                  : (HAVE_NEWABI
1790                     ? ELF_TARGET ("elf32-n", "big")
1791                     : ELF_TARGET ("elf32-", "big")))
1792               : (HAVE_64BIT_OBJECTS
1793                  ? ELF_TARGET ("elf64-", "little")
1794                  : (HAVE_NEWABI
1795                     ? ELF_TARGET ("elf32-n", "little")
1796                     : ELF_TARGET ("elf32-", "little"))));
1797     default:
1798       abort ();
1799       return NULL;
1800     }
1801 }
1802
1803 /* Return the ISA revision that is currently in use, or 0 if we are
1804    generating code for MIPS V or below.  */
1805
1806 static int
1807 mips_isa_rev (void)
1808 {
1809   if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1810     return 2;
1811
1812   /* microMIPS implies revision 2 or above.  */
1813   if (mips_opts.micromips)
1814     return 2;
1815
1816   if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1817     return 1;
1818
1819   return 0;
1820 }
1821
1822 /* Return the mask of all ASEs that are revisions of those in FLAGS.  */
1823
1824 static unsigned int
1825 mips_ase_mask (unsigned int flags)
1826 {
1827   unsigned int i;
1828
1829   for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1830     if (flags & mips_ase_groups[i])
1831       flags |= mips_ase_groups[i];
1832   return flags;
1833 }
1834
1835 /* Check whether the current ISA supports ASE.  Issue a warning if
1836    appropriate.  */
1837
1838 static void
1839 mips_check_isa_supports_ase (const struct mips_ase *ase)
1840 {
1841   const char *base;
1842   int min_rev, size;
1843   static unsigned int warned_isa;
1844   static unsigned int warned_fp32;
1845
1846   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1847     min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1848   else
1849     min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1850   if ((min_rev < 0 || mips_isa_rev () < min_rev)
1851       && (warned_isa & ase->flags) != ase->flags)
1852     {
1853       warned_isa |= ase->flags;
1854       base = mips_opts.micromips ? "microMIPS" : "MIPS";
1855       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1856       if (min_rev < 0)
1857         as_warn (_("The %d-bit %s architecture does not support the"
1858                    " `%s' extension"), size, base, ase->name);
1859       else
1860         as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1861                  ase->name, base, size, min_rev);
1862     }
1863   if ((ase->flags & FP64_ASES)
1864       && mips_opts.fp32
1865       && (warned_fp32 & ase->flags) != ase->flags)
1866     {
1867       warned_fp32 |= ase->flags;
1868       as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1869     }
1870 }
1871
1872 /* Check all enabled ASEs to see whether they are supported by the
1873    chosen architecture.  */
1874
1875 static void
1876 mips_check_isa_supports_ases (void)
1877 {
1878   unsigned int i, mask;
1879
1880   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1881     {
1882       mask = mips_ase_mask (mips_ases[i].flags);
1883       if ((mips_opts.ase & mask) == mips_ases[i].flags)
1884         mips_check_isa_supports_ase (&mips_ases[i]);
1885     }
1886 }
1887
1888 /* Set the state of ASE to ENABLED_P.  Return the mask of ASE_* flags
1889    that were affected.  */
1890
1891 static unsigned int
1892 mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1893 {
1894   unsigned int mask;
1895
1896   mask = mips_ase_mask (ase->flags);
1897   mips_opts.ase &= ~mask;
1898   if (enabled_p)
1899     mips_opts.ase |= ase->flags;
1900   return mask;
1901 }
1902
1903 /* Return the ASE called NAME, or null if none.  */
1904
1905 static const struct mips_ase *
1906 mips_lookup_ase (const char *name)
1907 {
1908   unsigned int i;
1909
1910   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1911     if (strcmp (name, mips_ases[i].name) == 0)
1912       return &mips_ases[i];
1913   return NULL;
1914 }
1915
1916 /* Return the length of a microMIPS instruction in bytes.  If bits of
1917    the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1918    Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1919    major opcode) will require further modifications to the opcode
1920    table.  */
1921
1922 static inline unsigned int
1923 micromips_insn_length (const struct mips_opcode *mo)
1924 {
1925   return (mo->mask >> 16) == 0 ? 2 : 4;
1926 }
1927
1928 /* Return the length of MIPS16 instruction OPCODE.  */
1929
1930 static inline unsigned int
1931 mips16_opcode_length (unsigned long opcode)
1932 {
1933   return (opcode >> 16) == 0 ? 2 : 4;
1934 }
1935
1936 /* Return the length of instruction INSN.  */
1937
1938 static inline unsigned int
1939 insn_length (const struct mips_cl_insn *insn)
1940 {
1941   if (mips_opts.micromips)
1942     return micromips_insn_length (insn->insn_mo);
1943   else if (mips_opts.mips16)
1944     return mips16_opcode_length (insn->insn_opcode);
1945   else
1946     return 4;
1947 }
1948
1949 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
1950
1951 static void
1952 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1953 {
1954   size_t i;
1955
1956   insn->insn_mo = mo;
1957   insn->insn_opcode = mo->match;
1958   insn->frag = NULL;
1959   insn->where = 0;
1960   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1961     insn->fixp[i] = NULL;
1962   insn->fixed_p = (mips_opts.noreorder > 0);
1963   insn->noreorder_p = (mips_opts.noreorder > 0);
1964   insn->mips16_absolute_jump_p = 0;
1965   insn->complete_p = 0;
1966   insn->cleared_p = 0;
1967 }
1968
1969 /* Install UVAL as the value of OPERAND in INSN.  */
1970
1971 static inline void
1972 insn_insert_operand (struct mips_cl_insn *insn,
1973                      const struct mips_operand *operand, unsigned int uval)
1974 {
1975   insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
1976 }
1977
1978 /* Record the current MIPS16/microMIPS mode in now_seg.  */
1979
1980 static void
1981 mips_record_compressed_mode (void)
1982 {
1983   segment_info_type *si;
1984
1985   si = seg_info (now_seg);
1986   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1987     si->tc_segment_info_data.mips16 = mips_opts.mips16;
1988   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1989     si->tc_segment_info_data.micromips = mips_opts.micromips;
1990 }
1991
1992 /* Read a standard MIPS instruction from BUF.  */
1993
1994 static unsigned long
1995 read_insn (char *buf)
1996 {
1997   if (target_big_endian)
1998     return bfd_getb32 ((bfd_byte *) buf);
1999   else
2000     return bfd_getl32 ((bfd_byte *) buf);
2001 }
2002
2003 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
2004    the next byte.  */
2005
2006 static char *
2007 write_insn (char *buf, unsigned int insn)
2008 {
2009   md_number_to_chars (buf, insn, 4);
2010   return buf + 4;
2011 }
2012
2013 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2014    has length LENGTH.  */
2015
2016 static unsigned long
2017 read_compressed_insn (char *buf, unsigned int length)
2018 {
2019   unsigned long insn;
2020   unsigned int i;
2021
2022   insn = 0;
2023   for (i = 0; i < length; i += 2)
2024     {
2025       insn <<= 16;
2026       if (target_big_endian)
2027         insn |= bfd_getb16 ((char *) buf);
2028       else
2029         insn |= bfd_getl16 ((char *) buf);
2030       buf += 2;
2031     }
2032   return insn;
2033 }
2034
2035 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2036    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
2037
2038 static char *
2039 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2040 {
2041   unsigned int i;
2042
2043   for (i = 0; i < length; i += 2)
2044     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2045   return buf + length;
2046 }
2047
2048 /* Install INSN at the location specified by its "frag" and "where" fields.  */
2049
2050 static void
2051 install_insn (const struct mips_cl_insn *insn)
2052 {
2053   char *f = insn->frag->fr_literal + insn->where;
2054   if (HAVE_CODE_COMPRESSION)
2055     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2056   else
2057     write_insn (f, insn->insn_opcode);
2058   mips_record_compressed_mode ();
2059 }
2060
2061 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
2062    and install the opcode in the new location.  */
2063
2064 static void
2065 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2066 {
2067   size_t i;
2068
2069   insn->frag = frag;
2070   insn->where = where;
2071   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2072     if (insn->fixp[i] != NULL)
2073       {
2074         insn->fixp[i]->fx_frag = frag;
2075         insn->fixp[i]->fx_where = where;
2076       }
2077   install_insn (insn);
2078 }
2079
2080 /* Add INSN to the end of the output.  */
2081
2082 static void
2083 add_fixed_insn (struct mips_cl_insn *insn)
2084 {
2085   char *f = frag_more (insn_length (insn));
2086   move_insn (insn, frag_now, f - frag_now->fr_literal);
2087 }
2088
2089 /* Start a variant frag and move INSN to the start of the variant part,
2090    marking it as fixed.  The other arguments are as for frag_var.  */
2091
2092 static void
2093 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2094                   relax_substateT subtype, symbolS *symbol, offsetT offset)
2095 {
2096   frag_grow (max_chars);
2097   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2098   insn->fixed_p = 1;
2099   frag_var (rs_machine_dependent, max_chars, var,
2100             subtype, symbol, offset, NULL);
2101 }
2102
2103 /* Insert N copies of INSN into the history buffer, starting at
2104    position FIRST.  Neither FIRST nor N need to be clipped.  */
2105
2106 static void
2107 insert_into_history (unsigned int first, unsigned int n,
2108                      const struct mips_cl_insn *insn)
2109 {
2110   if (mips_relax.sequence != 2)
2111     {
2112       unsigned int i;
2113
2114       for (i = ARRAY_SIZE (history); i-- > first;)
2115         if (i >= first + n)
2116           history[i] = history[i - n];
2117         else
2118           history[i] = *insn;
2119     }
2120 }
2121
2122 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
2123    the idea is to make it obvious at a glance that each errata is
2124    included.  */
2125
2126 static void
2127 init_vr4120_conflicts (void)
2128 {
2129 #define CONFLICT(FIRST, SECOND) \
2130     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2131
2132   /* Errata 21 - [D]DIV[U] after [D]MACC */
2133   CONFLICT (MACC, DIV);
2134   CONFLICT (DMACC, DIV);
2135
2136   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
2137   CONFLICT (DMULT, DMULT);
2138   CONFLICT (DMULT, DMACC);
2139   CONFLICT (DMACC, DMULT);
2140   CONFLICT (DMACC, DMACC);
2141
2142   /* Errata 24 - MT{LO,HI} after [D]MACC */
2143   CONFLICT (MACC, MTHILO);
2144   CONFLICT (DMACC, MTHILO);
2145
2146   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2147      instruction is executed immediately after a MACC or DMACC
2148      instruction, the result of [either instruction] is incorrect."  */
2149   CONFLICT (MACC, MULT);
2150   CONFLICT (MACC, DMULT);
2151   CONFLICT (DMACC, MULT);
2152   CONFLICT (DMACC, DMULT);
2153
2154   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2155      executed immediately after a DMULT, DMULTU, DIV, DIVU,
2156      DDIV or DDIVU instruction, the result of the MACC or
2157      DMACC instruction is incorrect.".  */
2158   CONFLICT (DMULT, MACC);
2159   CONFLICT (DMULT, DMACC);
2160   CONFLICT (DIV, MACC);
2161   CONFLICT (DIV, DMACC);
2162
2163 #undef CONFLICT
2164 }
2165
2166 struct regname {
2167   const char *name;
2168   unsigned int num;
2169 };
2170
2171 #define RTYPE_MASK      0x1ff00
2172 #define RTYPE_NUM       0x00100
2173 #define RTYPE_FPU       0x00200
2174 #define RTYPE_FCC       0x00400
2175 #define RTYPE_VEC       0x00800
2176 #define RTYPE_GP        0x01000
2177 #define RTYPE_CP0       0x02000
2178 #define RTYPE_PC        0x04000
2179 #define RTYPE_ACC       0x08000
2180 #define RTYPE_CCC       0x10000
2181 #define RNUM_MASK       0x000ff
2182 #define RWARN           0x80000
2183
2184 #define GENERIC_REGISTER_NUMBERS \
2185     {"$0",      RTYPE_NUM | 0},  \
2186     {"$1",      RTYPE_NUM | 1},  \
2187     {"$2",      RTYPE_NUM | 2},  \
2188     {"$3",      RTYPE_NUM | 3},  \
2189     {"$4",      RTYPE_NUM | 4},  \
2190     {"$5",      RTYPE_NUM | 5},  \
2191     {"$6",      RTYPE_NUM | 6},  \
2192     {"$7",      RTYPE_NUM | 7},  \
2193     {"$8",      RTYPE_NUM | 8},  \
2194     {"$9",      RTYPE_NUM | 9},  \
2195     {"$10",     RTYPE_NUM | 10}, \
2196     {"$11",     RTYPE_NUM | 11}, \
2197     {"$12",     RTYPE_NUM | 12}, \
2198     {"$13",     RTYPE_NUM | 13}, \
2199     {"$14",     RTYPE_NUM | 14}, \
2200     {"$15",     RTYPE_NUM | 15}, \
2201     {"$16",     RTYPE_NUM | 16}, \
2202     {"$17",     RTYPE_NUM | 17}, \
2203     {"$18",     RTYPE_NUM | 18}, \
2204     {"$19",     RTYPE_NUM | 19}, \
2205     {"$20",     RTYPE_NUM | 20}, \
2206     {"$21",     RTYPE_NUM | 21}, \
2207     {"$22",     RTYPE_NUM | 22}, \
2208     {"$23",     RTYPE_NUM | 23}, \
2209     {"$24",     RTYPE_NUM | 24}, \
2210     {"$25",     RTYPE_NUM | 25}, \
2211     {"$26",     RTYPE_NUM | 26}, \
2212     {"$27",     RTYPE_NUM | 27}, \
2213     {"$28",     RTYPE_NUM | 28}, \
2214     {"$29",     RTYPE_NUM | 29}, \
2215     {"$30",     RTYPE_NUM | 30}, \
2216     {"$31",     RTYPE_NUM | 31} 
2217
2218 #define FPU_REGISTER_NAMES       \
2219     {"$f0",     RTYPE_FPU | 0},  \
2220     {"$f1",     RTYPE_FPU | 1},  \
2221     {"$f2",     RTYPE_FPU | 2},  \
2222     {"$f3",     RTYPE_FPU | 3},  \
2223     {"$f4",     RTYPE_FPU | 4},  \
2224     {"$f5",     RTYPE_FPU | 5},  \
2225     {"$f6",     RTYPE_FPU | 6},  \
2226     {"$f7",     RTYPE_FPU | 7},  \
2227     {"$f8",     RTYPE_FPU | 8},  \
2228     {"$f9",     RTYPE_FPU | 9},  \
2229     {"$f10",    RTYPE_FPU | 10}, \
2230     {"$f11",    RTYPE_FPU | 11}, \
2231     {"$f12",    RTYPE_FPU | 12}, \
2232     {"$f13",    RTYPE_FPU | 13}, \
2233     {"$f14",    RTYPE_FPU | 14}, \
2234     {"$f15",    RTYPE_FPU | 15}, \
2235     {"$f16",    RTYPE_FPU | 16}, \
2236     {"$f17",    RTYPE_FPU | 17}, \
2237     {"$f18",    RTYPE_FPU | 18}, \
2238     {"$f19",    RTYPE_FPU | 19}, \
2239     {"$f20",    RTYPE_FPU | 20}, \
2240     {"$f21",    RTYPE_FPU | 21}, \
2241     {"$f22",    RTYPE_FPU | 22}, \
2242     {"$f23",    RTYPE_FPU | 23}, \
2243     {"$f24",    RTYPE_FPU | 24}, \
2244     {"$f25",    RTYPE_FPU | 25}, \
2245     {"$f26",    RTYPE_FPU | 26}, \
2246     {"$f27",    RTYPE_FPU | 27}, \
2247     {"$f28",    RTYPE_FPU | 28}, \
2248     {"$f29",    RTYPE_FPU | 29}, \
2249     {"$f30",    RTYPE_FPU | 30}, \
2250     {"$f31",    RTYPE_FPU | 31}
2251
2252 #define FPU_CONDITION_CODE_NAMES \
2253     {"$fcc0",   RTYPE_FCC | 0},  \
2254     {"$fcc1",   RTYPE_FCC | 1},  \
2255     {"$fcc2",   RTYPE_FCC | 2},  \
2256     {"$fcc3",   RTYPE_FCC | 3},  \
2257     {"$fcc4",   RTYPE_FCC | 4},  \
2258     {"$fcc5",   RTYPE_FCC | 5},  \
2259     {"$fcc6",   RTYPE_FCC | 6},  \
2260     {"$fcc7",   RTYPE_FCC | 7}
2261
2262 #define COPROC_CONDITION_CODE_NAMES         \
2263     {"$cc0",    RTYPE_FCC | RTYPE_CCC | 0}, \
2264     {"$cc1",    RTYPE_FCC | RTYPE_CCC | 1}, \
2265     {"$cc2",    RTYPE_FCC | RTYPE_CCC | 2}, \
2266     {"$cc3",    RTYPE_FCC | RTYPE_CCC | 3}, \
2267     {"$cc4",    RTYPE_FCC | RTYPE_CCC | 4}, \
2268     {"$cc5",    RTYPE_FCC | RTYPE_CCC | 5}, \
2269     {"$cc6",    RTYPE_FCC | RTYPE_CCC | 6}, \
2270     {"$cc7",    RTYPE_FCC | RTYPE_CCC | 7}
2271
2272 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2273     {"$a4",     RTYPE_GP | 8},  \
2274     {"$a5",     RTYPE_GP | 9},  \
2275     {"$a6",     RTYPE_GP | 10}, \
2276     {"$a7",     RTYPE_GP | 11}, \
2277     {"$ta0",    RTYPE_GP | 8},  /* alias for $a4 */ \
2278     {"$ta1",    RTYPE_GP | 9},  /* alias for $a5 */ \
2279     {"$ta2",    RTYPE_GP | 10}, /* alias for $a6 */ \
2280     {"$ta3",    RTYPE_GP | 11}, /* alias for $a7 */ \
2281     {"$t0",     RTYPE_GP | 12}, \
2282     {"$t1",     RTYPE_GP | 13}, \
2283     {"$t2",     RTYPE_GP | 14}, \
2284     {"$t3",     RTYPE_GP | 15}
2285
2286 #define O32_SYMBOLIC_REGISTER_NAMES \
2287     {"$t0",     RTYPE_GP | 8},  \
2288     {"$t1",     RTYPE_GP | 9},  \
2289     {"$t2",     RTYPE_GP | 10}, \
2290     {"$t3",     RTYPE_GP | 11}, \
2291     {"$t4",     RTYPE_GP | 12}, \
2292     {"$t5",     RTYPE_GP | 13}, \
2293     {"$t6",     RTYPE_GP | 14}, \
2294     {"$t7",     RTYPE_GP | 15}, \
2295     {"$ta0",    RTYPE_GP | 12}, /* alias for $t4 */ \
2296     {"$ta1",    RTYPE_GP | 13}, /* alias for $t5 */ \
2297     {"$ta2",    RTYPE_GP | 14}, /* alias for $t6 */ \
2298     {"$ta3",    RTYPE_GP | 15}  /* alias for $t7 */ 
2299
2300 /* Remaining symbolic register names */
2301 #define SYMBOLIC_REGISTER_NAMES \
2302     {"$zero",   RTYPE_GP | 0},  \
2303     {"$at",     RTYPE_GP | 1},  \
2304     {"$AT",     RTYPE_GP | 1},  \
2305     {"$v0",     RTYPE_GP | 2},  \
2306     {"$v1",     RTYPE_GP | 3},  \
2307     {"$a0",     RTYPE_GP | 4},  \
2308     {"$a1",     RTYPE_GP | 5},  \
2309     {"$a2",     RTYPE_GP | 6},  \
2310     {"$a3",     RTYPE_GP | 7},  \
2311     {"$s0",     RTYPE_GP | 16}, \
2312     {"$s1",     RTYPE_GP | 17}, \
2313     {"$s2",     RTYPE_GP | 18}, \
2314     {"$s3",     RTYPE_GP | 19}, \
2315     {"$s4",     RTYPE_GP | 20}, \
2316     {"$s5",     RTYPE_GP | 21}, \
2317     {"$s6",     RTYPE_GP | 22}, \
2318     {"$s7",     RTYPE_GP | 23}, \
2319     {"$t8",     RTYPE_GP | 24}, \
2320     {"$t9",     RTYPE_GP | 25}, \
2321     {"$k0",     RTYPE_GP | 26}, \
2322     {"$kt0",    RTYPE_GP | 26}, \
2323     {"$k1",     RTYPE_GP | 27}, \
2324     {"$kt1",    RTYPE_GP | 27}, \
2325     {"$gp",     RTYPE_GP | 28}, \
2326     {"$sp",     RTYPE_GP | 29}, \
2327     {"$s8",     RTYPE_GP | 30}, \
2328     {"$fp",     RTYPE_GP | 30}, \
2329     {"$ra",     RTYPE_GP | 31}
2330
2331 #define MIPS16_SPECIAL_REGISTER_NAMES \
2332     {"$pc",     RTYPE_PC | 0}
2333
2334 #define MDMX_VECTOR_REGISTER_NAMES \
2335     /* {"$v0",  RTYPE_VEC | 0},  clash with REG 2 above */ \
2336     /* {"$v1",  RTYPE_VEC | 1},  clash with REG 3 above */ \
2337     {"$v2",     RTYPE_VEC | 2},  \
2338     {"$v3",     RTYPE_VEC | 3},  \
2339     {"$v4",     RTYPE_VEC | 4},  \
2340     {"$v5",     RTYPE_VEC | 5},  \
2341     {"$v6",     RTYPE_VEC | 6},  \
2342     {"$v7",     RTYPE_VEC | 7},  \
2343     {"$v8",     RTYPE_VEC | 8},  \
2344     {"$v9",     RTYPE_VEC | 9},  \
2345     {"$v10",    RTYPE_VEC | 10}, \
2346     {"$v11",    RTYPE_VEC | 11}, \
2347     {"$v12",    RTYPE_VEC | 12}, \
2348     {"$v13",    RTYPE_VEC | 13}, \
2349     {"$v14",    RTYPE_VEC | 14}, \
2350     {"$v15",    RTYPE_VEC | 15}, \
2351     {"$v16",    RTYPE_VEC | 16}, \
2352     {"$v17",    RTYPE_VEC | 17}, \
2353     {"$v18",    RTYPE_VEC | 18}, \
2354     {"$v19",    RTYPE_VEC | 19}, \
2355     {"$v20",    RTYPE_VEC | 20}, \
2356     {"$v21",    RTYPE_VEC | 21}, \
2357     {"$v22",    RTYPE_VEC | 22}, \
2358     {"$v23",    RTYPE_VEC | 23}, \
2359     {"$v24",    RTYPE_VEC | 24}, \
2360     {"$v25",    RTYPE_VEC | 25}, \
2361     {"$v26",    RTYPE_VEC | 26}, \
2362     {"$v27",    RTYPE_VEC | 27}, \
2363     {"$v28",    RTYPE_VEC | 28}, \
2364     {"$v29",    RTYPE_VEC | 29}, \
2365     {"$v30",    RTYPE_VEC | 30}, \
2366     {"$v31",    RTYPE_VEC | 31}
2367
2368 #define MIPS_DSP_ACCUMULATOR_NAMES \
2369     {"$ac0",    RTYPE_ACC | 0}, \
2370     {"$ac1",    RTYPE_ACC | 1}, \
2371     {"$ac2",    RTYPE_ACC | 2}, \
2372     {"$ac3",    RTYPE_ACC | 3}
2373
2374 static const struct regname reg_names[] = {
2375   GENERIC_REGISTER_NUMBERS,
2376   FPU_REGISTER_NAMES,
2377   FPU_CONDITION_CODE_NAMES,
2378   COPROC_CONDITION_CODE_NAMES,
2379
2380   /* The $txx registers depends on the abi,
2381      these will be added later into the symbol table from
2382      one of the tables below once mips_abi is set after 
2383      parsing of arguments from the command line. */
2384   SYMBOLIC_REGISTER_NAMES,
2385
2386   MIPS16_SPECIAL_REGISTER_NAMES,
2387   MDMX_VECTOR_REGISTER_NAMES,
2388   MIPS_DSP_ACCUMULATOR_NAMES,
2389   {0, 0}
2390 };
2391
2392 static const struct regname reg_names_o32[] = {
2393   O32_SYMBOLIC_REGISTER_NAMES,
2394   {0, 0}
2395 };
2396
2397 static const struct regname reg_names_n32n64[] = {
2398   N32N64_SYMBOLIC_REGISTER_NAMES,
2399   {0, 0}
2400 };
2401
2402 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2403    interpreted as vector registers 0 and 1.  If SYMVAL is the value of one
2404    of these register symbols, return the associated vector register,
2405    otherwise return SYMVAL itself.  */
2406
2407 static unsigned int
2408 mips_prefer_vec_regno (unsigned int symval)
2409 {
2410   if ((symval & -2) == (RTYPE_GP | 2))
2411     return RTYPE_VEC | (symval & 1);
2412   return symval;
2413 }
2414
2415 /* Return true if the string at *SPTR is a valid register name.  If so,
2416    move *SPTR past the register and store the register's symbol value
2417    in *SYMVAL.  This symbol value includes the register number
2418    (RNUM_MASK) and register type (RTYPE_MASK).  */
2419
2420 static bfd_boolean
2421 mips_parse_register (char **sptr, unsigned int *symval)
2422 {
2423   symbolS *symbol;
2424   char *s, *e;
2425   char save_c;
2426
2427   /* Find end of name.  */
2428   s = e = *sptr;
2429   if (is_name_beginner (*e))
2430     ++e;
2431   while (is_part_of_name (*e))
2432     ++e;
2433
2434   /* Terminate name.  */
2435   save_c = *e;
2436   *e = '\0';
2437
2438   /* Look up the name.  */
2439   symbol = symbol_find (s);
2440   *e = save_c;
2441
2442   if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2443     return FALSE;
2444
2445   *sptr = e;
2446   *symval = S_GET_VALUE (symbol);
2447   return TRUE;
2448 }
2449
2450 /* Check if SPTR points at a valid register specifier according to TYPES.
2451    If so, then return 1, advance S to consume the specifier and store
2452    the register's number in REGNOP, otherwise return 0.  */
2453
2454 static int
2455 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2456 {
2457   unsigned int regno;
2458
2459   if (mips_parse_register (s, &regno))
2460     {
2461       if (types & RTYPE_VEC)
2462         regno = mips_prefer_vec_regno (regno);
2463       if (regno & types)
2464         regno &= RNUM_MASK;
2465       else
2466         regno = ~0;
2467     }
2468   else
2469     {
2470       if (types & RWARN)
2471         as_warn (_("Unrecognized register name `%s'"), *s);
2472       regno = ~0;
2473     }
2474   if (regnop)
2475     *regnop = regno;
2476   return regno <= RNUM_MASK;
2477 }
2478
2479 /* Token types for parsed operand lists.  */
2480 enum mips_operand_token_type {
2481   /* A plain register, e.g. $f2.  */
2482   OT_REG,
2483
2484   /* An element of a vector, e.g. $v0[1].  */
2485   OT_REG_ELEMENT,
2486
2487   /* A continuous range of registers, e.g. $s0-$s4.  */
2488   OT_REG_RANGE,
2489
2490   /* A (possibly relocated) expression.  */
2491   OT_INTEGER,
2492
2493   /* A floating-point value.  */
2494   OT_FLOAT,
2495
2496   /* A single character.  This can be '(', ')' or ',', but '(' only appears
2497      before OT_REGs.  */
2498   OT_CHAR,
2499
2500   /* The end of the operand list.  */
2501   OT_END
2502 };
2503
2504 /* A parsed operand token.  */
2505 struct mips_operand_token
2506 {
2507   /* The type of token.  */
2508   enum mips_operand_token_type type;
2509   union
2510   {
2511     /* The register symbol value for an OT_REG.  */
2512     unsigned int regno;
2513
2514     /* The register symbol value and index for an OT_REG_ELEMENT.  */
2515     struct {
2516       unsigned int regno;
2517       addressT index;
2518     } reg_element;
2519
2520     /* The two register symbol values involved in an OT_REG_RANGE.  */
2521     struct {
2522       unsigned int regno1;
2523       unsigned int regno2;
2524     } reg_range;
2525
2526     /* The value of an OT_INTEGER.  The value is represented as an
2527        expression and the relocation operators that were applied to
2528        that expression.  The reloc entries are BFD_RELOC_UNUSED if no
2529        relocation operators were used.  */
2530     struct {
2531       expressionS value;
2532       bfd_reloc_code_real_type relocs[3];
2533     } integer;
2534
2535     /* The binary data for an OT_FLOAT constant, and the number of bytes
2536        in the constant.  */
2537     struct {
2538       unsigned char data[8];
2539       int length;
2540     } flt;
2541
2542     /* The character represented by an OT_CHAR.  */
2543     char ch;
2544   } u;
2545 };
2546
2547 /* An obstack used to construct lists of mips_operand_tokens.  */
2548 static struct obstack mips_operand_tokens;
2549
2550 /* Give TOKEN type TYPE and add it to mips_operand_tokens.  */
2551
2552 static void
2553 mips_add_token (struct mips_operand_token *token,
2554                 enum mips_operand_token_type type)
2555 {
2556   token->type = type;
2557   obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2558 }
2559
2560 /* Check whether S is '(' followed by a register name.  Add OT_CHAR
2561    and OT_REG tokens for them if so, and return a pointer to the first
2562    unconsumed character.  Return null otherwise.  */
2563
2564 static char *
2565 mips_parse_base_start (char *s)
2566 {
2567   struct mips_operand_token token;
2568   unsigned int regno;
2569
2570   if (*s != '(')
2571     return 0;
2572
2573   ++s;
2574   SKIP_SPACE_TABS (s);
2575   if (!mips_parse_register (&s, &regno))
2576     return 0;
2577
2578   token.u.ch = '(';
2579   mips_add_token (&token, OT_CHAR);
2580
2581   token.u.regno = regno;
2582   mips_add_token (&token, OT_REG);
2583
2584   return s;
2585 }
2586
2587 /* Parse one or more tokens from S.  Return a pointer to the first
2588    unconsumed character on success.  Return null if an error was found
2589    and store the error text in insn_error.  FLOAT_FORMAT is as for
2590    mips_parse_arguments.  */
2591
2592 static char *
2593 mips_parse_argument_token (char *s, char float_format)
2594 {
2595   char *end, *save_in, *err;
2596   unsigned int regno1, regno2;
2597   struct mips_operand_token token;
2598
2599   /* First look for "($reg", since we want to treat that as an
2600      OT_CHAR and OT_REG rather than an expression.  */
2601   end = mips_parse_base_start (s);
2602   if (end)
2603     return end;
2604
2605   /* Handle other characters that end up as OT_CHARs.  */
2606   if (*s == ')' || *s == ',')
2607     {
2608       token.u.ch = *s;
2609       mips_add_token (&token, OT_CHAR);
2610       ++s;
2611       return s;
2612     }
2613
2614   /* Handle tokens that start with a register.  */
2615   if (mips_parse_register (&s, &regno1))
2616     {
2617       SKIP_SPACE_TABS (s);
2618       if (*s == '-')
2619         {
2620           /* A register range.  */
2621           ++s;
2622           SKIP_SPACE_TABS (s);
2623           if (!mips_parse_register (&s, &regno2))
2624             {
2625               insn_error = _("Invalid register range");
2626               return 0;
2627             }
2628
2629           token.u.reg_range.regno1 = regno1;
2630           token.u.reg_range.regno2 = regno2;
2631           mips_add_token (&token, OT_REG_RANGE);
2632           return s;
2633         }
2634       else if (*s == '[')
2635         {
2636           /* A vector element.  */
2637           expressionS element;
2638
2639           ++s;
2640           SKIP_SPACE_TABS (s);
2641           my_getExpression (&element, s);
2642           if (element.X_op != O_constant)
2643             {
2644               insn_error = _("Vector element must be constant");
2645               return 0;
2646             }
2647           s = expr_end;
2648           SKIP_SPACE_TABS (s);
2649           if (*s != ']')
2650             {
2651               insn_error = _("Missing `]'");
2652               return 0;
2653             }
2654           ++s;
2655
2656           token.u.reg_element.regno = regno1;
2657           token.u.reg_element.index = element.X_add_number;
2658           mips_add_token (&token, OT_REG_ELEMENT);
2659           return s;
2660         }
2661
2662       /* Looks like just a plain register.  */
2663       token.u.regno = regno1;
2664       mips_add_token (&token, OT_REG);
2665       return s;
2666     }
2667
2668   if (float_format)
2669     {
2670       /* First try to treat expressions as floats.  */
2671       save_in = input_line_pointer;
2672       input_line_pointer = s;
2673       err = md_atof (float_format, (char *) token.u.flt.data,
2674                      &token.u.flt.length);
2675       end = input_line_pointer;
2676       input_line_pointer = save_in;
2677       if (err && *err)
2678         {
2679           insn_error = err;
2680           return 0;
2681         }
2682       if (s != end)
2683         {
2684           mips_add_token (&token, OT_FLOAT);
2685           return end;
2686         }
2687     }
2688
2689   /* Treat everything else as an integer expression.  */
2690   token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
2691   token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
2692   token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
2693   my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
2694   s = expr_end;
2695   mips_add_token (&token, OT_INTEGER);
2696   return s;
2697 }
2698
2699 /* S points to the operand list for an instruction.  FLOAT_FORMAT is 'f'
2700    if expressions should be treated as 32-bit floating-point constants,
2701    'd' if they should be treated as 64-bit floating-point constants,
2702    or 0 if they should be treated as integer expressions (the usual case).
2703
2704    Return a list of tokens on success, otherwise return 0.  The caller
2705    must obstack_free the list after use.  */
2706
2707 static struct mips_operand_token *
2708 mips_parse_arguments (char *s, char float_format)
2709 {
2710   struct mips_operand_token token;
2711
2712   SKIP_SPACE_TABS (s);
2713   while (*s)
2714     {
2715       s = mips_parse_argument_token (s, float_format);
2716       if (!s)
2717         {
2718           obstack_free (&mips_operand_tokens,
2719                         obstack_finish (&mips_operand_tokens));
2720           return 0;
2721         }
2722       SKIP_SPACE_TABS (s);
2723     }
2724   mips_add_token (&token, OT_END);
2725   return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
2726 }
2727
2728 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2729    and architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
2730
2731 static bfd_boolean
2732 is_opcode_valid (const struct mips_opcode *mo)
2733 {
2734   int isa = mips_opts.isa;
2735   int ase = mips_opts.ase;
2736   int fp_s, fp_d;
2737   unsigned int i;
2738
2739   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2740     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2741       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2742         ase |= mips_ases[i].flags64;
2743
2744   if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
2745     return FALSE;
2746
2747   /* Check whether the instruction or macro requires single-precision or
2748      double-precision floating-point support.  Note that this information is
2749      stored differently in the opcode table for insns and macros.  */
2750   if (mo->pinfo == INSN_MACRO)
2751     {
2752       fp_s = mo->pinfo2 & INSN2_M_FP_S;
2753       fp_d = mo->pinfo2 & INSN2_M_FP_D;
2754     }
2755   else
2756     {
2757       fp_s = mo->pinfo & FP_S;
2758       fp_d = mo->pinfo & FP_D;
2759     }
2760
2761   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2762     return FALSE;
2763
2764   if (fp_s && mips_opts.soft_float)
2765     return FALSE;
2766
2767   return TRUE;
2768 }
2769
2770 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2771    selected ISA and architecture.  */
2772
2773 static bfd_boolean
2774 is_opcode_valid_16 (const struct mips_opcode *mo)
2775 {
2776   return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
2777 }
2778
2779 /* Return TRUE if the size of the microMIPS opcode MO matches one
2780    explicitly requested.  Always TRUE in the standard MIPS mode.  */
2781
2782 static bfd_boolean
2783 is_size_valid (const struct mips_opcode *mo)
2784 {
2785   if (!mips_opts.micromips)
2786     return TRUE;
2787
2788   if (mips_opts.insn32)
2789     {
2790       if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2791         return FALSE;
2792       if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2793         return FALSE;
2794     }
2795   if (!forced_insn_length)
2796     return TRUE;
2797   if (mo->pinfo == INSN_MACRO)
2798     return FALSE;
2799   return forced_insn_length == micromips_insn_length (mo);
2800 }
2801
2802 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2803    of the preceding instruction.  Always TRUE in the standard MIPS mode.
2804
2805    We don't accept macros in 16-bit delay slots to avoid a case where
2806    a macro expansion fails because it relies on a preceding 32-bit real
2807    instruction to have matched and does not handle the operands correctly.
2808    The only macros that may expand to 16-bit instructions are JAL that
2809    cannot be placed in a delay slot anyway, and corner cases of BALIGN
2810    and BGT (that likewise cannot be placed in a delay slot) that decay to
2811    a NOP.  In all these cases the macros precede any corresponding real
2812    instruction definitions in the opcode table, so they will match in the
2813    second pass where the size of the delay slot is ignored and therefore
2814    produce correct code.  */
2815
2816 static bfd_boolean
2817 is_delay_slot_valid (const struct mips_opcode *mo)
2818 {
2819   if (!mips_opts.micromips)
2820     return TRUE;
2821
2822   if (mo->pinfo == INSN_MACRO)
2823     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
2824   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2825       && micromips_insn_length (mo) != 4)
2826     return FALSE;
2827   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2828       && micromips_insn_length (mo) != 2)
2829     return FALSE;
2830
2831   return TRUE;
2832 }
2833
2834 /* For consistency checking, verify that all bits of OPCODE are
2835    specified either by the match/mask part of the instruction
2836    definition, or by the operand list.  INSN_BITS says which
2837    bits of the instruction are significant and DECODE_OPERAND
2838    provides the mips_operand description of each operand.  */
2839
2840 static int
2841 validate_mips_insn (const struct mips_opcode *opcode,
2842                     unsigned long insn_bits,
2843                     const struct mips_operand *(*decode_operand) (const char *))
2844 {
2845   const char *s;
2846   unsigned long used_bits, doubled, undefined;
2847   const struct mips_operand *operand;
2848
2849   if ((opcode->mask & opcode->match) != opcode->match)
2850     {
2851       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
2852               opcode->name, opcode->args);
2853       return 0;
2854     }
2855   used_bits = 0;
2856   for (s = opcode->args; *s; ++s)
2857     switch (*s)
2858       {
2859       case ',':
2860       case '(':
2861       case ')':
2862         break;
2863
2864       default:
2865         operand = decode_operand (s);
2866         if (!operand)
2867           {
2868             as_bad (_("internal: unknown operand type: %s %s"),
2869                     opcode->name, opcode->args);
2870             return 0;
2871           }
2872         used_bits |= ((1 << operand->size) - 1) << operand->lsb;
2873         if (operand->type == OP_MDMX_IMM_REG)
2874           /* Bit 5 is the format selector (OB vs QH).  The opcode table
2875              has separate entries for each format.  */
2876           used_bits &= ~(1 << (operand->lsb + 5));
2877         /* Skip prefix characters.  */
2878         if (*s == '+' || *s == 'm')
2879           ++s;
2880         break;
2881       }
2882   doubled = used_bits & opcode->mask & insn_bits;
2883   if (doubled)
2884     {
2885       as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
2886                 " %s %s"), doubled, opcode->name, opcode->args);
2887       return 0;
2888     }
2889   used_bits |= opcode->mask;
2890   undefined = ~used_bits & insn_bits;
2891   if (undefined)
2892     {
2893       as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
2894               undefined, opcode->name, opcode->args);
2895       return 0;
2896     }
2897   used_bits &= ~insn_bits;
2898   if (used_bits)
2899     {
2900       as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
2901               used_bits, opcode->name, opcode->args);
2902       return 0;
2903     }
2904   return 1;
2905 }
2906
2907 /* The microMIPS version of validate_mips_insn.  */
2908
2909 static int
2910 validate_micromips_insn (const struct mips_opcode *opc)
2911 {
2912   unsigned long insn_bits;
2913   unsigned long major;
2914   unsigned int length;
2915
2916   length = micromips_insn_length (opc);
2917   if (length != 2 && length != 4)
2918     {
2919       as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
2920                 "%s %s"), length, opc->name, opc->args);
2921       return 0;
2922     }
2923   major = opc->match >> (10 + 8 * (length - 2));
2924   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
2925       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
2926     {
2927       as_bad (_("Internal error: bad microMIPS opcode "
2928                 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
2929       return 0;
2930     }
2931
2932   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
2933   insn_bits = 1 << 4 * length;
2934   insn_bits <<= 4 * length;
2935   insn_bits -= 1;
2936   return validate_mips_insn (opc, insn_bits, decode_micromips_operand);
2937 }
2938
2939 /* This function is called once, at assembler startup time.  It should set up
2940    all the tables, etc. that the MD part of the assembler will need.  */
2941
2942 void
2943 md_begin (void)
2944 {
2945   const char *retval = NULL;
2946   int i = 0;
2947   int broken = 0;
2948
2949   if (mips_pic != NO_PIC)
2950     {
2951       if (g_switch_seen && g_switch_value != 0)
2952         as_bad (_("-G may not be used in position-independent code"));
2953       g_switch_value = 0;
2954     }
2955
2956   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2957     as_warn (_("Could not set architecture and machine"));
2958
2959   op_hash = hash_new ();
2960
2961   for (i = 0; i < NUMOPCODES;)
2962     {
2963       const char *name = mips_opcodes[i].name;
2964
2965       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2966       if (retval != NULL)
2967         {
2968           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2969                    mips_opcodes[i].name, retval);
2970           /* Probably a memory allocation problem?  Give up now.  */
2971           as_fatal (_("Broken assembler.  No assembly attempted."));
2972         }
2973       do
2974         {
2975           if (mips_opcodes[i].pinfo != INSN_MACRO)
2976             {
2977               if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
2978                                        decode_mips_operand))
2979                 broken = 1;
2980               if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2981                 {
2982                   create_insn (&nop_insn, mips_opcodes + i);
2983                   if (mips_fix_loongson2f_nop)
2984                     nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2985                   nop_insn.fixed_p = 1;
2986                 }
2987             }
2988           ++i;
2989         }
2990       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2991     }
2992
2993   mips16_op_hash = hash_new ();
2994
2995   i = 0;
2996   while (i < bfd_mips16_num_opcodes)
2997     {
2998       const char *name = mips16_opcodes[i].name;
2999
3000       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3001       if (retval != NULL)
3002         as_fatal (_("internal: can't hash `%s': %s"),
3003                   mips16_opcodes[i].name, retval);
3004       do
3005         {
3006           if (mips16_opcodes[i].pinfo != INSN_MACRO
3007               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
3008                   != mips16_opcodes[i].match))
3009             {
3010               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
3011                        mips16_opcodes[i].name, mips16_opcodes[i].args);
3012               broken = 1;
3013             }
3014           if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3015             {
3016               create_insn (&mips16_nop_insn, mips16_opcodes + i);
3017               mips16_nop_insn.fixed_p = 1;
3018             }
3019           ++i;
3020         }
3021       while (i < bfd_mips16_num_opcodes
3022              && strcmp (mips16_opcodes[i].name, name) == 0);
3023     }
3024
3025   micromips_op_hash = hash_new ();
3026
3027   i = 0;
3028   while (i < bfd_micromips_num_opcodes)
3029     {
3030       const char *name = micromips_opcodes[i].name;
3031
3032       retval = hash_insert (micromips_op_hash, name,
3033                             (void *) &micromips_opcodes[i]);
3034       if (retval != NULL)
3035         as_fatal (_("internal: can't hash `%s': %s"),
3036                   micromips_opcodes[i].name, retval);
3037       do
3038         if (micromips_opcodes[i].pinfo != INSN_MACRO)
3039           {
3040             struct mips_cl_insn *micromips_nop_insn;
3041
3042             if (!validate_micromips_insn (&micromips_opcodes[i]))
3043               broken = 1;
3044
3045             if (micromips_insn_length (micromips_opcodes + i) == 2)
3046               micromips_nop_insn = &micromips_nop16_insn;
3047             else if (micromips_insn_length (micromips_opcodes + i) == 4)
3048               micromips_nop_insn = &micromips_nop32_insn;
3049             else
3050               continue;
3051
3052             if (micromips_nop_insn->insn_mo == NULL
3053                 && strcmp (name, "nop") == 0)
3054               {
3055                 create_insn (micromips_nop_insn, micromips_opcodes + i);
3056                 micromips_nop_insn->fixed_p = 1;
3057               }
3058           }
3059       while (++i < bfd_micromips_num_opcodes
3060              && strcmp (micromips_opcodes[i].name, name) == 0);
3061     }
3062
3063   if (broken)
3064     as_fatal (_("Broken assembler.  No assembly attempted."));
3065
3066   /* We add all the general register names to the symbol table.  This
3067      helps us detect invalid uses of them.  */
3068   for (i = 0; reg_names[i].name; i++) 
3069     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3070                                      reg_names[i].num, /* & RNUM_MASK, */
3071                                      &zero_address_frag));
3072   if (HAVE_NEWABI)
3073     for (i = 0; reg_names_n32n64[i].name; i++) 
3074       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3075                                        reg_names_n32n64[i].num, /* & RNUM_MASK, */
3076                                        &zero_address_frag));
3077   else
3078     for (i = 0; reg_names_o32[i].name; i++) 
3079       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3080                                        reg_names_o32[i].num, /* & RNUM_MASK, */
3081                                        &zero_address_frag));
3082
3083   obstack_init (&mips_operand_tokens);
3084
3085   mips_no_prev_insn ();
3086
3087   mips_gprmask = 0;
3088   mips_cprmask[0] = 0;
3089   mips_cprmask[1] = 0;
3090   mips_cprmask[2] = 0;
3091   mips_cprmask[3] = 0;
3092
3093   /* set the default alignment for the text section (2**2) */
3094   record_alignment (text_section, 2);
3095
3096   bfd_set_gp_size (stdoutput, g_switch_value);
3097
3098   /* On a native system other than VxWorks, sections must be aligned
3099      to 16 byte boundaries.  When configured for an embedded ELF
3100      target, we don't bother.  */
3101   if (strncmp (TARGET_OS, "elf", 3) != 0
3102       && strncmp (TARGET_OS, "vxworks", 7) != 0)
3103     {
3104       (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3105       (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3106       (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3107     }
3108
3109   /* Create a .reginfo section for register masks and a .mdebug
3110      section for debugging information.  */
3111   {
3112     segT seg;
3113     subsegT subseg;
3114     flagword flags;
3115     segT sec;
3116
3117     seg = now_seg;
3118     subseg = now_subseg;
3119
3120     /* The ABI says this section should be loaded so that the
3121        running program can access it.  However, we don't load it
3122        if we are configured for an embedded target */
3123     flags = SEC_READONLY | SEC_DATA;
3124     if (strncmp (TARGET_OS, "elf", 3) != 0)
3125       flags |= SEC_ALLOC | SEC_LOAD;
3126
3127     if (mips_abi != N64_ABI)
3128       {
3129         sec = subseg_new (".reginfo", (subsegT) 0);
3130
3131         bfd_set_section_flags (stdoutput, sec, flags);
3132         bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3133
3134         mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3135       }
3136     else
3137       {
3138         /* The 64-bit ABI uses a .MIPS.options section rather than
3139            .reginfo section.  */
3140         sec = subseg_new (".MIPS.options", (subsegT) 0);
3141         bfd_set_section_flags (stdoutput, sec, flags);
3142         bfd_set_section_alignment (stdoutput, sec, 3);
3143
3144         /* Set up the option header.  */
3145         {
3146           Elf_Internal_Options opthdr;
3147           char *f;
3148
3149           opthdr.kind = ODK_REGINFO;
3150           opthdr.size = (sizeof (Elf_External_Options)
3151                          + sizeof (Elf64_External_RegInfo));
3152           opthdr.section = 0;
3153           opthdr.info = 0;
3154           f = frag_more (sizeof (Elf_External_Options));
3155           bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3156                                          (Elf_External_Options *) f);
3157
3158           mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3159         }
3160       }
3161
3162     if (ECOFF_DEBUGGING)
3163       {
3164         sec = subseg_new (".mdebug", (subsegT) 0);
3165         (void) bfd_set_section_flags (stdoutput, sec,
3166                                       SEC_HAS_CONTENTS | SEC_READONLY);
3167         (void) bfd_set_section_alignment (stdoutput, sec, 2);
3168       }
3169     else if (mips_flag_pdr)
3170       {
3171         pdr_seg = subseg_new (".pdr", (subsegT) 0);
3172         (void) bfd_set_section_flags (stdoutput, pdr_seg,
3173                                       SEC_READONLY | SEC_RELOC
3174                                       | SEC_DEBUGGING);
3175         (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3176       }
3177
3178     subseg_set (seg, subseg);
3179   }
3180
3181   if (! ECOFF_DEBUGGING)
3182     md_obj_begin ();
3183
3184   if (mips_fix_vr4120)
3185     init_vr4120_conflicts ();
3186 }
3187
3188 void
3189 md_mips_end (void)
3190 {
3191   mips_emit_delays ();
3192   if (! ECOFF_DEBUGGING)
3193     md_obj_end ();
3194 }
3195
3196 void
3197 md_assemble (char *str)
3198 {
3199   struct mips_cl_insn insn;
3200   bfd_reloc_code_real_type unused_reloc[3]
3201     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3202
3203   imm_expr.X_op = O_absent;
3204   imm2_expr.X_op = O_absent;
3205   offset_expr.X_op = O_absent;
3206   offset_reloc[0] = BFD_RELOC_UNUSED;
3207   offset_reloc[1] = BFD_RELOC_UNUSED;
3208   offset_reloc[2] = BFD_RELOC_UNUSED;
3209
3210   mips_mark_labels ();
3211   mips_assembling_insn = TRUE;
3212
3213   if (mips_opts.mips16)
3214     mips16_ip (str, &insn);
3215   else
3216     {
3217       mips_ip (str, &insn);
3218       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
3219             str, insn.insn_opcode));
3220     }
3221
3222   if (insn_error)
3223     as_bad ("%s `%s'", insn_error, str);
3224   else if (insn.insn_mo->pinfo == INSN_MACRO)
3225     {
3226       macro_start ();
3227       if (mips_opts.mips16)
3228         mips16_macro (&insn);
3229       else
3230         macro (&insn, str);
3231       macro_end ();
3232     }
3233   else
3234     {
3235       if (offset_expr.X_op != O_absent)
3236         append_insn (&insn, &offset_expr, offset_reloc, FALSE);
3237       else
3238         append_insn (&insn, NULL, unused_reloc, FALSE);
3239     }
3240
3241   mips_assembling_insn = FALSE;
3242 }
3243
3244 /* Convenience functions for abstracting away the differences between
3245    MIPS16 and non-MIPS16 relocations.  */
3246
3247 static inline bfd_boolean
3248 mips16_reloc_p (bfd_reloc_code_real_type reloc)
3249 {
3250   switch (reloc)
3251     {
3252     case BFD_RELOC_MIPS16_JMP:
3253     case BFD_RELOC_MIPS16_GPREL:
3254     case BFD_RELOC_MIPS16_GOT16:
3255     case BFD_RELOC_MIPS16_CALL16:
3256     case BFD_RELOC_MIPS16_HI16_S:
3257     case BFD_RELOC_MIPS16_HI16:
3258     case BFD_RELOC_MIPS16_LO16:
3259       return TRUE;
3260
3261     default:
3262       return FALSE;
3263     }
3264 }
3265
3266 static inline bfd_boolean
3267 micromips_reloc_p (bfd_reloc_code_real_type reloc)
3268 {
3269   switch (reloc)
3270     {
3271     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3272     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3273     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3274     case BFD_RELOC_MICROMIPS_GPREL16:
3275     case BFD_RELOC_MICROMIPS_JMP:
3276     case BFD_RELOC_MICROMIPS_HI16:
3277     case BFD_RELOC_MICROMIPS_HI16_S:
3278     case BFD_RELOC_MICROMIPS_LO16:
3279     case BFD_RELOC_MICROMIPS_LITERAL:
3280     case BFD_RELOC_MICROMIPS_GOT16:
3281     case BFD_RELOC_MICROMIPS_CALL16:
3282     case BFD_RELOC_MICROMIPS_GOT_HI16:
3283     case BFD_RELOC_MICROMIPS_GOT_LO16:
3284     case BFD_RELOC_MICROMIPS_CALL_HI16:
3285     case BFD_RELOC_MICROMIPS_CALL_LO16:
3286     case BFD_RELOC_MICROMIPS_SUB:
3287     case BFD_RELOC_MICROMIPS_GOT_PAGE:
3288     case BFD_RELOC_MICROMIPS_GOT_OFST:
3289     case BFD_RELOC_MICROMIPS_GOT_DISP:
3290     case BFD_RELOC_MICROMIPS_HIGHEST:
3291     case BFD_RELOC_MICROMIPS_HIGHER:
3292     case BFD_RELOC_MICROMIPS_SCN_DISP:
3293     case BFD_RELOC_MICROMIPS_JALR:
3294       return TRUE;
3295
3296     default:
3297       return FALSE;
3298     }
3299 }
3300
3301 static inline bfd_boolean
3302 jmp_reloc_p (bfd_reloc_code_real_type reloc)
3303 {
3304   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3305 }
3306
3307 static inline bfd_boolean
3308 got16_reloc_p (bfd_reloc_code_real_type reloc)
3309 {
3310   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
3311           || reloc == BFD_RELOC_MICROMIPS_GOT16);
3312 }
3313
3314 static inline bfd_boolean
3315 hi16_reloc_p (bfd_reloc_code_real_type reloc)
3316 {
3317   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
3318           || reloc == BFD_RELOC_MICROMIPS_HI16_S);
3319 }
3320
3321 static inline bfd_boolean
3322 lo16_reloc_p (bfd_reloc_code_real_type reloc)
3323 {
3324   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
3325           || reloc == BFD_RELOC_MICROMIPS_LO16);
3326 }
3327
3328 static inline bfd_boolean
3329 jalr_reloc_p (bfd_reloc_code_real_type reloc)
3330 {
3331   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
3332 }
3333
3334 static inline bfd_boolean
3335 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
3336 {
3337   return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
3338           || reloc == BFD_RELOC_MICROMIPS_GPREL16);
3339 }
3340
3341 /* Return true if RELOC is a PC-relative relocation that does not have
3342    full address range.  */
3343
3344 static inline bfd_boolean
3345 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3346 {
3347   switch (reloc)
3348     {
3349     case BFD_RELOC_16_PCREL_S2:
3350     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3351     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3352     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3353       return TRUE;
3354
3355     case BFD_RELOC_32_PCREL:
3356       return HAVE_64BIT_ADDRESSES;
3357
3358     default:
3359       return FALSE;
3360     }
3361 }
3362
3363 /* Return true if the given relocation might need a matching %lo().
3364    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3365    need a matching %lo() when applied to local symbols.  */
3366
3367 static inline bfd_boolean
3368 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
3369 {
3370   return (HAVE_IN_PLACE_ADDENDS
3371           && (hi16_reloc_p (reloc)
3372               /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3373                  all GOT16 relocations evaluate to "G".  */
3374               || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3375 }
3376
3377 /* Return the type of %lo() reloc needed by RELOC, given that
3378    reloc_needs_lo_p.  */
3379
3380 static inline bfd_reloc_code_real_type
3381 matching_lo_reloc (bfd_reloc_code_real_type reloc)
3382 {
3383   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3384           : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3385              : BFD_RELOC_LO16));
3386 }
3387
3388 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
3389    relocation.  */
3390
3391 static inline bfd_boolean
3392 fixup_has_matching_lo_p (fixS *fixp)
3393 {
3394   return (fixp->fx_next != NULL
3395           && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
3396           && fixp->fx_addsy == fixp->fx_next->fx_addsy
3397           && fixp->fx_offset == fixp->fx_next->fx_offset);
3398 }
3399
3400 /* Move all labels in LABELS to the current insertion point.  TEXT_P
3401    says whether the labels refer to text or data.  */
3402
3403 static void
3404 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
3405 {
3406   struct insn_label_list *l;
3407   valueT val;
3408
3409   for (l = labels; l != NULL; l = l->next)
3410     {
3411       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
3412       symbol_set_frag (l->label, frag_now);
3413       val = (valueT) frag_now_fix ();
3414       /* MIPS16/microMIPS text labels are stored as odd.  */
3415       if (text_p && HAVE_CODE_COMPRESSION)
3416         ++val;
3417       S_SET_VALUE (l->label, val);
3418     }
3419 }
3420
3421 /* Move all labels in insn_labels to the current insertion point
3422    and treat them as text labels.  */
3423
3424 static void
3425 mips_move_text_labels (void)
3426 {
3427   mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3428 }
3429
3430 static bfd_boolean
3431 s_is_linkonce (symbolS *sym, segT from_seg)
3432 {
3433   bfd_boolean linkonce = FALSE;
3434   segT symseg = S_GET_SEGMENT (sym);
3435
3436   if (symseg != from_seg && !S_IS_LOCAL (sym))
3437     {
3438       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3439         linkonce = TRUE;
3440       /* The GNU toolchain uses an extension for ELF: a section
3441          beginning with the magic string .gnu.linkonce is a
3442          linkonce section.  */
3443       if (strncmp (segment_name (symseg), ".gnu.linkonce",
3444                    sizeof ".gnu.linkonce" - 1) == 0)
3445         linkonce = TRUE;
3446     }
3447   return linkonce;
3448 }
3449
3450 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
3451    linker to handle them specially, such as generating jalx instructions
3452    when needed.  We also make them odd for the duration of the assembly,
3453    in order to generate the right sort of code.  We will make them even
3454    in the adjust_symtab routine, while leaving them marked.  This is
3455    convenient for the debugger and the disassembler.  The linker knows
3456    to make them odd again.  */
3457
3458 static void
3459 mips_compressed_mark_label (symbolS *label)
3460 {
3461   gas_assert (HAVE_CODE_COMPRESSION);
3462
3463   if (mips_opts.mips16)
3464     S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3465   else
3466     S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
3467   if ((S_GET_VALUE (label) & 1) == 0
3468       /* Don't adjust the address if the label is global or weak, or
3469          in a link-once section, since we'll be emitting symbol reloc
3470          references to it which will be patched up by the linker, and
3471          the final value of the symbol may or may not be MIPS16/microMIPS.  */
3472       && !S_IS_WEAK (label)
3473       && !S_IS_EXTERNAL (label)
3474       && !s_is_linkonce (label, now_seg))
3475     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3476 }
3477
3478 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
3479
3480 static void
3481 mips_compressed_mark_labels (void)
3482 {
3483   struct insn_label_list *l;
3484
3485   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3486     mips_compressed_mark_label (l->label);
3487 }
3488
3489 /* End the current frag.  Make it a variant frag and record the
3490    relaxation info.  */
3491
3492 static void
3493 relax_close_frag (void)
3494 {
3495   mips_macro_warning.first_frag = frag_now;
3496   frag_var (rs_machine_dependent, 0, 0,
3497             RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
3498             mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3499
3500   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3501   mips_relax.first_fixup = 0;
3502 }
3503
3504 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
3505    See the comment above RELAX_ENCODE for more details.  */
3506
3507 static void
3508 relax_start (symbolS *symbol)
3509 {
3510   gas_assert (mips_relax.sequence == 0);
3511   mips_relax.sequence = 1;
3512   mips_relax.symbol = symbol;
3513 }
3514
3515 /* Start generating the second version of a relaxable sequence.
3516    See the comment above RELAX_ENCODE for more details.  */
3517
3518 static void
3519 relax_switch (void)
3520 {
3521   gas_assert (mips_relax.sequence == 1);
3522   mips_relax.sequence = 2;
3523 }
3524
3525 /* End the current relaxable sequence.  */
3526
3527 static void
3528 relax_end (void)
3529 {
3530   gas_assert (mips_relax.sequence == 2);
3531   relax_close_frag ();
3532   mips_relax.sequence = 0;
3533 }
3534
3535 /* Return true if IP is a delayed branch or jump.  */
3536
3537 static inline bfd_boolean
3538 delayed_branch_p (const struct mips_cl_insn *ip)
3539 {
3540   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3541                                 | INSN_COND_BRANCH_DELAY
3542                                 | INSN_COND_BRANCH_LIKELY)) != 0;
3543 }
3544
3545 /* Return true if IP is a compact branch or jump.  */
3546
3547 static inline bfd_boolean
3548 compact_branch_p (const struct mips_cl_insn *ip)
3549 {
3550   return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3551                                  | INSN2_COND_BRANCH)) != 0;
3552 }
3553
3554 /* Return true if IP is an unconditional branch or jump.  */
3555
3556 static inline bfd_boolean
3557 uncond_branch_p (const struct mips_cl_insn *ip)
3558 {
3559   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3560           || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
3561 }
3562
3563 /* Return true if IP is a branch-likely instruction.  */
3564
3565 static inline bfd_boolean
3566 branch_likely_p (const struct mips_cl_insn *ip)
3567 {
3568   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3569 }
3570
3571 /* Return the type of nop that should be used to fill the delay slot
3572    of delayed branch IP.  */
3573
3574 static struct mips_cl_insn *
3575 get_delay_slot_nop (const struct mips_cl_insn *ip)
3576 {
3577   if (mips_opts.micromips
3578       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3579     return &micromips_nop32_insn;
3580   return NOP_INSN;
3581 }
3582
3583 /* Return the mask of core registers that IP reads or writes.  */
3584
3585 static unsigned int
3586 gpr_mod_mask (const struct mips_cl_insn *ip)
3587 {
3588   unsigned long pinfo2;
3589   unsigned int mask;
3590
3591   mask = 0;
3592   pinfo2 = ip->insn_mo->pinfo2;
3593   if (mips_opts.micromips)
3594     {
3595       if (pinfo2 & INSN2_MOD_GPR_MD)
3596         mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
3597       if (pinfo2 & INSN2_MOD_GPR_MF)
3598         mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
3599     }
3600   if (pinfo2 & INSN2_MOD_SP)
3601     mask |= 1 << SP;
3602   return mask;
3603 }
3604
3605 /* Return the mask of core registers that IP reads.  */
3606
3607 static unsigned int
3608 gpr_read_mask (const struct mips_cl_insn *ip)
3609 {
3610   unsigned long pinfo, pinfo2;
3611   unsigned int mask;
3612
3613   mask = gpr_mod_mask (ip);
3614   pinfo = ip->insn_mo->pinfo;
3615   pinfo2 = ip->insn_mo->pinfo2;
3616   if (mips_opts.mips16)
3617     {
3618       if (pinfo & MIPS16_INSN_READ_X)
3619         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3620       if (pinfo & MIPS16_INSN_READ_Y)
3621         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3622       if (pinfo & MIPS16_INSN_READ_T)
3623         mask |= 1 << TREG;
3624       if (pinfo & MIPS16_INSN_READ_SP)
3625         mask |= 1 << SP;
3626       if (pinfo & MIPS16_INSN_READ_Z)
3627         mask |= 1 << (mips16_to_32_reg_map
3628                       [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3629       if (pinfo & MIPS16_INSN_READ_GPR_X)
3630         mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3631     }
3632   else
3633     {
3634       if (pinfo2 & INSN2_READ_GPR_D)
3635         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3636       if (pinfo & INSN_READ_GPR_T)
3637         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3638       if (pinfo & INSN_READ_GPR_S)
3639         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3640       if (pinfo2 & INSN2_READ_GP)
3641         mask |= 1 << GP;
3642       if (pinfo2 & INSN2_READ_GPR_Z)
3643         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3644     }
3645   if (pinfo2 & INSN2_READ_GPR_31)
3646     mask |= 1 << RA;
3647   if (mips_opts.micromips)
3648     {
3649       if (pinfo2 & INSN2_READ_GPR_MC)
3650         mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3651       if (pinfo2 & INSN2_READ_GPR_ME)
3652         mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3653       if (pinfo2 & INSN2_READ_GPR_MG)
3654         mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3655       if (pinfo2 & INSN2_READ_GPR_MJ)
3656         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3657       if (pinfo2 & INSN2_READ_GPR_MMN)
3658         {
3659           mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3660           mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3661         }
3662       if (pinfo2 & INSN2_READ_GPR_MP)
3663         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3664       if (pinfo2 & INSN2_READ_GPR_MQ)
3665         mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3666     }
3667   /* Don't include register 0.  */
3668   return mask & ~1;
3669 }
3670
3671 /* Return the mask of core registers that IP writes.  */
3672
3673 static unsigned int
3674 gpr_write_mask (const struct mips_cl_insn *ip)
3675 {
3676   unsigned long pinfo, pinfo2;
3677   unsigned int mask;
3678
3679   mask = gpr_mod_mask (ip);
3680   pinfo = ip->insn_mo->pinfo;
3681   pinfo2 = ip->insn_mo->pinfo2;
3682   if (mips_opts.mips16)
3683     {
3684       if (pinfo & MIPS16_INSN_WRITE_X)
3685         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3686       if (pinfo & MIPS16_INSN_WRITE_Y)
3687         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3688       if (pinfo & MIPS16_INSN_WRITE_Z)
3689         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3690       if (pinfo & MIPS16_INSN_WRITE_T)
3691         mask |= 1 << TREG;
3692       if (pinfo & MIPS16_INSN_WRITE_31)
3693         mask |= 1 << RA;
3694       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3695         mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3696     }
3697   else
3698     {
3699       if (pinfo & INSN_WRITE_GPR_D)
3700         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3701       if (pinfo & INSN_WRITE_GPR_T)
3702         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3703       if (pinfo & INSN_WRITE_GPR_S)
3704         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3705       if (pinfo & INSN_WRITE_GPR_31)
3706         mask |= 1 << RA;
3707       if (pinfo2 & INSN2_WRITE_GPR_Z)
3708         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3709     }
3710   if (mips_opts.micromips)
3711     {
3712       if (pinfo2 & INSN2_WRITE_GPR_MB)
3713         mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3714       if (pinfo2 & INSN2_WRITE_GPR_MH)
3715         {
3716           mask |= 1 << micromips_to_32_reg_h_map1[EXTRACT_OPERAND (1, MH, *ip)];
3717           mask |= 1 << micromips_to_32_reg_h_map2[EXTRACT_OPERAND (1, MH, *ip)];
3718         }
3719       if (pinfo2 & INSN2_WRITE_GPR_MJ)
3720         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3721       if (pinfo2 & INSN2_WRITE_GPR_MP)
3722         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3723     }
3724   /* Don't include register 0.  */
3725   return mask & ~1;
3726 }
3727
3728 /* Return the mask of floating-point registers that IP reads.  */
3729
3730 static unsigned int
3731 fpr_read_mask (const struct mips_cl_insn *ip)
3732 {
3733   unsigned long pinfo, pinfo2;
3734   unsigned int mask;
3735
3736   mask = 0;
3737   pinfo = ip->insn_mo->pinfo;
3738   pinfo2 = ip->insn_mo->pinfo2;
3739   if (!mips_opts.mips16)
3740     {
3741       if (pinfo2 & INSN2_READ_FPR_D)
3742         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3743       if (pinfo & INSN_READ_FPR_S)
3744         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3745       if (pinfo & INSN_READ_FPR_T)
3746         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3747       if (pinfo & INSN_READ_FPR_R)
3748         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3749       if (pinfo2 & INSN2_READ_FPR_Z)
3750         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3751     }
3752   /* Conservatively treat all operands to an FP_D instruction are doubles.
3753      (This is overly pessimistic for things like cvt.d.s.)  */
3754   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3755     mask |= mask << 1;
3756   return mask;
3757 }
3758
3759 /* Return the mask of floating-point registers that IP writes.  */
3760
3761 static unsigned int
3762 fpr_write_mask (const struct mips_cl_insn *ip)
3763 {
3764   unsigned long pinfo, pinfo2;
3765   unsigned int mask;
3766
3767   mask = 0;
3768   pinfo = ip->insn_mo->pinfo;
3769   pinfo2 = ip->insn_mo->pinfo2;
3770   if (!mips_opts.mips16)
3771     {
3772       if (pinfo & INSN_WRITE_FPR_D)
3773         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3774       if (pinfo & INSN_WRITE_FPR_S)
3775         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3776       if (pinfo & INSN_WRITE_FPR_T)
3777         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3778       if (pinfo2 & INSN2_WRITE_FPR_Z)
3779         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3780     }
3781   /* Conservatively treat all operands to an FP_D instruction are doubles.
3782      (This is overly pessimistic for things like cvt.s.d.)  */
3783   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3784     mask |= mask << 1;
3785   return mask;
3786 }
3787
3788 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
3789    Check whether that is allowed.  */
3790
3791 static bfd_boolean
3792 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
3793 {
3794   const char *s = insn->name;
3795
3796   if (insn->pinfo == INSN_MACRO)
3797     /* Let a macro pass, we'll catch it later when it is expanded.  */
3798     return TRUE;
3799
3800   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || mips_opts.arch == CPU_R5900)
3801     {
3802       /* Allow odd registers for single-precision ops.  */
3803       switch (insn->pinfo & (FP_S | FP_D))
3804         {
3805         case FP_S:
3806         case 0:
3807           return TRUE;
3808         case FP_D:
3809           return FALSE;
3810         default:
3811           break;
3812         }
3813
3814       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
3815       s = strchr (insn->name, '.');
3816       if (s != NULL && opnum == 2)
3817         s = strchr (s + 1, '.');
3818       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
3819     }
3820
3821   /* Single-precision coprocessor loads and moves are OK too.  */
3822   if ((insn->pinfo & FP_S)
3823       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
3824                          | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
3825     return TRUE;
3826
3827   return FALSE;
3828 }
3829
3830 /* Report that user-supplied argument ARGNUM for INSN was VAL, but should
3831    have been in the range [MIN_VAL, MAX_VAL].  PRINT_HEX says whether
3832    this operand is normally printed in hex or decimal.  */
3833
3834 static void
3835 report_bad_range (struct mips_cl_insn *insn, int argnum,
3836                   offsetT val, int min_val, int max_val,
3837                   bfd_boolean print_hex)
3838 {
3839   if (print_hex && val >= 0)
3840     as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
3841               " was 0x%lx."),
3842             argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
3843   else if (print_hex)
3844     as_bad (_("Operand %d of `%s' must be in the range [0x%x, 0x%x],"
3845               " was %ld."),
3846             argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
3847   else
3848     as_bad (_("Operand %d of `%s' must be in the range [%d, %d],"
3849               " was %ld."),
3850             argnum, insn->insn_mo->name, min_val, max_val, (unsigned long) val);
3851 }
3852
3853 /* Report an invalid combination of position and size operands for a bitfield
3854    operation.  POS and SIZE are the values that were given.  */
3855
3856 static void
3857 report_bad_field (offsetT pos, offsetT size)
3858 {
3859   as_bad (_("Invalid field specification (position %ld, size %ld)"),
3860           (unsigned long) pos, (unsigned long) size);
3861 }
3862
3863 /* Information about an instruction argument that we're trying to match.  */
3864 struct mips_arg_info
3865 {
3866   /* The instruction so far.  */
3867   struct mips_cl_insn *insn;
3868
3869   /* The first unconsumed operand token.  */
3870   struct mips_operand_token *token;
3871
3872   /* The 1-based operand number, in terms of insn->insn_mo->args.  */
3873   int opnum;
3874
3875   /* The 1-based argument number, for error reporting.  This does not
3876      count elided optional registers, etc..  */
3877   int argnum;
3878
3879   /* The last OP_REG operand seen, or ILLEGAL_REG if none.  */
3880   unsigned int last_regno;
3881
3882   /* If the first operand was an OP_REG, this is the register that it
3883      specified, otherwise it is ILLEGAL_REG.  */
3884   unsigned int dest_regno;
3885
3886   /* The value of the last OP_INT operand.  Only used for OP_MSB,
3887      where it gives the lsb position.  */
3888   unsigned int last_op_int;
3889
3890   /* If true, match routines should silently reject invalid arguments.
3891      If false, match routines can accept invalid arguments as long as
3892      they report an appropriate error.  They still have the option of
3893      silently rejecting arguments, in which case a generic "Invalid operands"
3894      style of error will be used instead.  */
3895   bfd_boolean soft_match;
3896
3897   /* If true, the OP_INT match routine should treat plain symbolic operands
3898      as if a relocation operator like %lo(...) had been used.  This is only
3899      ever true if the operand can be relocated.  */
3900   bfd_boolean allow_nonconst;
3901
3902   /* When true, the OP_INT match routine should allow unsigned N-bit
3903      arguments to be used where a signed N-bit operand is expected.  */
3904   bfd_boolean lax_max;
3905
3906   /* True if a reference to the current AT register was seen.  */
3907   bfd_boolean seen_at;
3908 };
3909
3910 /* Try to match an OT_CHAR token for character CH.  Consume the token
3911    and return true on success, otherwise return false.  */
3912
3913 static bfd_boolean
3914 match_char (struct mips_arg_info *arg, char ch)
3915 {
3916   if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
3917     {
3918       ++arg->token;
3919       if (ch == ',')
3920         arg->argnum += 1;
3921       return TRUE;
3922     }
3923   return FALSE;
3924 }
3925
3926 /* Try to get an expression from the next tokens in ARG.  Consume the
3927    tokens and return true on success, storing the expression value in
3928    VALUE and relocation types in R.  */
3929
3930 static bfd_boolean
3931 match_expression (struct mips_arg_info *arg, expressionS *value,
3932                   bfd_reloc_code_real_type *r)
3933 {
3934   if (arg->token->type == OT_INTEGER)
3935     {
3936       *value = arg->token->u.integer.value;
3937       memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
3938       ++arg->token;
3939       return TRUE;
3940     }
3941
3942   /* Error-reporting is more consistent if we treat registers as O_register
3943      rather than rejecting them outright.  "$1", "($1)" and "(($1))" are
3944      then handled in the same way.  */
3945   if (arg->token->type == OT_REG)
3946     {
3947       value->X_add_number = arg->token->u.regno;
3948       ++arg->token;
3949     }
3950   else if (arg->token[0].type == OT_CHAR
3951            && arg->token[0].u.ch == '('
3952            && arg->token[1].type == OT_REG
3953            && arg->token[2].type == OT_CHAR
3954            && arg->token[2].u.ch == ')')
3955     {
3956       value->X_add_number = arg->token[1].u.regno;
3957       arg->token += 3;
3958     }
3959   else
3960     return FALSE;
3961
3962   value->X_op = O_register;
3963   r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
3964   return TRUE;
3965 }
3966
3967 /* Try to get a constant expression from the next tokens in ARG.  Consume
3968    the tokens and return return true on success, storing the constant value
3969    in *VALUE.  Use FALLBACK as the value if the match succeeded with an
3970    error.  */
3971
3972 static bfd_boolean
3973 match_const_int (struct mips_arg_info *arg, offsetT *value, offsetT fallback)
3974 {
3975   expressionS ex;
3976   bfd_reloc_code_real_type r[3];
3977
3978   if (!match_expression (arg, &ex, r))
3979     return FALSE;
3980
3981   if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
3982     *value = ex.X_add_number;
3983   else
3984     {
3985       if (arg->soft_match)
3986         return FALSE;
3987       as_bad (_("Operand %d of `%s' must be constant"),
3988               arg->argnum, arg->insn->insn_mo->name);
3989       *value = fallback;
3990     }
3991   return TRUE;
3992 }
3993
3994 /* Return the RTYPE_* flags for a register operand of type TYPE that
3995    appears in instruction OPCODE.  */
3996
3997 static unsigned int
3998 convert_reg_type (const struct mips_opcode *opcode,
3999                   enum mips_reg_operand_type type)
4000 {
4001   switch (type)
4002     {
4003     case OP_REG_GP:
4004       return RTYPE_NUM | RTYPE_GP;
4005
4006     case OP_REG_FP:
4007       /* Allow vector register names for MDMX if the instruction is a 64-bit
4008          FPR load, store or move (including moves to and from GPRs).  */
4009       if ((mips_opts.ase & ASE_MDMX)
4010           && (opcode->pinfo & FP_D)
4011           && (opcode->pinfo & (INSN_COPROC_MOVE_DELAY
4012                                | INSN_COPROC_MEMORY_DELAY
4013                                | INSN_LOAD_COPROC_DELAY
4014                                | INSN_LOAD_MEMORY_DELAY
4015                                | INSN_STORE_MEMORY)))
4016         return RTYPE_FPU | RTYPE_VEC;
4017       return RTYPE_FPU;
4018
4019     case OP_REG_CCC:
4020       if (opcode->pinfo & (FP_D | FP_S))
4021         return RTYPE_CCC | RTYPE_FCC;
4022       return RTYPE_CCC;
4023
4024     case OP_REG_VEC:
4025       if (opcode->membership & INSN_5400)
4026         return RTYPE_FPU;
4027       return RTYPE_FPU | RTYPE_VEC;
4028
4029     case OP_REG_ACC:
4030       return RTYPE_ACC;
4031
4032     case OP_REG_COPRO:
4033       if (opcode->name[strlen (opcode->name) - 1] == '0')
4034         return RTYPE_NUM | RTYPE_CP0;
4035       return RTYPE_NUM;
4036
4037     case OP_REG_HW:
4038       return RTYPE_NUM;
4039     }
4040   abort ();
4041 }
4042
4043 /* ARG is register REGNO, of type TYPE.  Warn about any dubious registers.  */
4044
4045 static void
4046 check_regno (struct mips_arg_info *arg,
4047              enum mips_reg_operand_type type, unsigned int regno)
4048 {
4049   if (AT && type == OP_REG_GP && regno == AT)
4050     arg->seen_at = TRUE;
4051
4052   if (type == OP_REG_FP
4053       && (regno & 1) != 0
4054       && HAVE_32BIT_FPRS
4055       && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
4056     as_warn (_("Float register should be even, was %d"), regno);
4057
4058   if (type == OP_REG_CCC)
4059     {
4060       const char *name;
4061       size_t length;
4062
4063       name = arg->insn->insn_mo->name;
4064       length = strlen (name);
4065       if ((regno & 1) != 0
4066           && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4067               || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
4068         as_warn (_("Condition code register should be even for %s, was %d"),
4069                  name, regno);
4070
4071       if ((regno & 3) != 0
4072           && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
4073         as_warn (_("Condition code register should be 0 or 4 for %s, was %d"),
4074                  name, regno);
4075     }
4076 }
4077
4078 /* ARG is a register with symbol value SYMVAL.  Try to interpret it as
4079    a register of type TYPE.  Return true on success, storing the register
4080    number in *REGNO and warning about any dubious uses.  */
4081
4082 static bfd_boolean
4083 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4084              unsigned int symval, unsigned int *regno)
4085 {
4086   if (type == OP_REG_VEC)
4087     symval = mips_prefer_vec_regno (symval);
4088   if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4089     return FALSE;
4090
4091   *regno = symval & RNUM_MASK;
4092   check_regno (arg, type, *regno);
4093   return TRUE;
4094 }
4095
4096 /* Try to interpret the next token in ARG as a register of type TYPE.
4097    Consume the token and return true on success, storing the register
4098    number in *REGNO.  Return false on failure.  */
4099
4100 static bfd_boolean
4101 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4102            unsigned int *regno)
4103 {
4104   if (arg->token->type == OT_REG
4105       && match_regno (arg, type, arg->token->u.regno, regno))
4106     {
4107       ++arg->token;
4108       return TRUE;
4109     }
4110   return FALSE;
4111 }
4112
4113 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
4114    Consume the token and return true on success, storing the register numbers
4115    in *REGNO1 and *REGNO2.  Return false on failure.  */
4116
4117 static bfd_boolean
4118 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4119                  unsigned int *regno1, unsigned int *regno2)
4120 {
4121   if (match_reg (arg, type, regno1))
4122     {
4123       *regno2 = *regno1;
4124       return TRUE;
4125     }
4126   if (arg->token->type == OT_REG_RANGE
4127       && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4128       && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4129       && *regno1 <= *regno2)
4130     {
4131       ++arg->token;
4132       return TRUE;
4133     }
4134   return FALSE;
4135 }
4136
4137 /* OP_INT matcher.  */
4138
4139 static bfd_boolean
4140 match_int_operand (struct mips_arg_info *arg,
4141                    const struct mips_operand *operand_base)
4142 {
4143   const struct mips_int_operand *operand;
4144   unsigned int uval, mask;
4145   int min_val, max_val, factor;
4146   offsetT sval;
4147   bfd_boolean print_hex;
4148
4149   operand = (const struct mips_int_operand *) operand_base;
4150   factor = 1 << operand->shift;
4151   mask = (1 << operand_base->size) - 1;
4152   max_val = (operand->max_val + operand->bias) << operand->shift;
4153   min_val = max_val - (mask << operand->shift);
4154   if (arg->lax_max)
4155     max_val = mask << operand->shift;
4156
4157   if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4158     /* Assume we have an elided offset.  The later match will fail
4159        if this turns out to be wrong.  */
4160     sval = 0;
4161   else if (operand_base->lsb == 0
4162            && operand_base->size == 16
4163            && operand->shift == 0
4164            && operand->bias == 0
4165            && (operand->max_val == 32767 || operand->max_val == 65535))
4166     {
4167       /* The operand can be relocated.  */
4168       if (!match_expression (arg, &offset_expr, offset_reloc))
4169         return FALSE;
4170
4171       if (offset_reloc[0] != BFD_RELOC_UNUSED)
4172         /* Relocation operators were used.  Accept the arguent and
4173            leave the relocation value in offset_expr and offset_relocs
4174            for the caller to process.  */
4175         return TRUE;
4176
4177       if (offset_expr.X_op != O_constant)
4178         {
4179           /* If non-constant operands are allowed then leave them for
4180              the caller to process, otherwise fail the match.  */
4181           if (!arg->allow_nonconst)
4182             return FALSE;
4183           offset_reloc[0] = BFD_RELOC_LO16;
4184           return TRUE;
4185         }
4186
4187       /* Clear the global state; we're going to install the operand
4188          ourselves.  */
4189       sval = offset_expr.X_add_number;
4190       offset_expr.X_op = O_absent;
4191     }
4192   else
4193     {
4194       if (!match_const_int (arg, &sval, min_val))
4195         return FALSE;
4196     }
4197
4198   arg->last_op_int = sval;
4199
4200   /* Check the range.  If there's a problem, record the lowest acceptable
4201      value in arg->last_op_int in order to prevent an unhelpful error
4202      from OP_MSB too.
4203
4204      Bit counts have traditionally been printed in hex by the disassembler
4205      but printed as decimal in error messages.  Only resort to hex if
4206      the operand is bigger than 6 bits.  */
4207   print_hex = operand->print_hex && operand_base->size > 6;
4208   if (sval < min_val || sval > max_val)
4209     {
4210       if (arg->soft_match)
4211         return FALSE;
4212       report_bad_range (arg->insn, arg->argnum, sval, min_val, max_val,
4213                         print_hex);
4214       arg->last_op_int = min_val;
4215     }
4216   else if (sval % factor)
4217     {
4218       if (arg->soft_match)
4219         return FALSE;
4220       as_bad (print_hex && sval >= 0
4221               ? _("Operand %d of `%s' must be a factor of %d, was 0x%lx.")
4222               : _("Operand %d of `%s' must be a factor of %d, was %ld."),
4223               arg->argnum, arg->insn->insn_mo->name, factor,
4224               (unsigned long) sval);
4225       arg->last_op_int = min_val;
4226     }
4227
4228   uval = (unsigned int) sval >> operand->shift;
4229   uval -= operand->bias;
4230
4231   /* Handle -mfix-cn63xxp1.  */
4232   if (arg->opnum == 1
4233       && mips_fix_cn63xxp1
4234       && !mips_opts.micromips
4235       && strcmp ("pref", arg->insn->insn_mo->name) == 0)
4236     switch (uval)
4237       {
4238       case 5:
4239       case 25:
4240       case 26:
4241       case 27:
4242       case 28:
4243       case 29:
4244       case 30:
4245       case 31:
4246         /* These are ok.  */
4247         break;
4248
4249       default:
4250         /* The rest must be changed to 28.  */
4251         uval = 28;
4252         break;
4253       }
4254
4255   insn_insert_operand (arg->insn, operand_base, uval);
4256   return TRUE;
4257 }
4258
4259 /* OP_MAPPED_INT matcher.  */
4260
4261 static bfd_boolean
4262 match_mapped_int_operand (struct mips_arg_info *arg,
4263                           const struct mips_operand *operand_base)
4264 {
4265   const struct mips_mapped_int_operand *operand;
4266   unsigned int uval, num_vals;
4267   offsetT sval;
4268
4269   operand = (const struct mips_mapped_int_operand *) operand_base;
4270   if (!match_const_int (arg, &sval, operand->int_map[0]))
4271     return FALSE;
4272
4273   num_vals = 1 << operand_base->size;
4274   for (uval = 0; uval < num_vals; uval++)
4275     if (operand->int_map[uval] == sval)
4276       break;
4277   if (uval == num_vals)
4278     return FALSE;
4279
4280   insn_insert_operand (arg->insn, operand_base, uval);
4281   return TRUE;
4282 }
4283
4284 /* OP_MSB matcher.  */
4285
4286 static bfd_boolean
4287 match_msb_operand (struct mips_arg_info *arg,
4288                    const struct mips_operand *operand_base)
4289 {
4290   const struct mips_msb_operand *operand;
4291   int min_val, max_val, max_high;
4292   offsetT size, sval, high;
4293
4294   operand = (const struct mips_msb_operand *) operand_base;
4295   min_val = operand->bias;
4296   max_val = min_val + (1 << operand_base->size) - 1;
4297   max_high = operand->opsize;
4298
4299   if (!match_const_int (arg, &size, 1))
4300     return FALSE;
4301
4302   high = size + arg->last_op_int;
4303   sval = operand->add_lsb ? high : size;
4304
4305   if (size < 0 || high > max_high || sval < min_val || sval > max_val)
4306     {
4307       if (arg->soft_match)
4308         return FALSE;
4309       report_bad_field (arg->last_op_int, size);
4310       sval = min_val;
4311     }
4312   insn_insert_operand (arg->insn, operand_base, sval - min_val);
4313   return TRUE;
4314 }
4315
4316 /* OP_REG matcher.  */
4317
4318 static bfd_boolean
4319 match_reg_operand (struct mips_arg_info *arg,
4320                    const struct mips_operand *operand_base)
4321 {
4322   const struct mips_reg_operand *operand;
4323   unsigned int regno, uval, num_vals;
4324
4325   operand = (const struct mips_reg_operand *) operand_base;
4326   if (!match_reg (arg, operand->reg_type, &regno))
4327     return FALSE;
4328
4329   if (operand->reg_map)
4330     {
4331       num_vals = 1 << operand->root.size;
4332       for (uval = 0; uval < num_vals; uval++)
4333         if (operand->reg_map[uval] == regno)
4334           break;
4335       if (num_vals == uval)
4336         return FALSE;
4337     }
4338   else
4339     uval = regno;
4340
4341   arg->last_regno = regno;
4342   if (arg->opnum == 1)
4343     arg->dest_regno = regno;
4344   insn_insert_operand (arg->insn, operand_base, uval);
4345   return TRUE;
4346 }
4347
4348 /* OP_REG_PAIR matcher.  */
4349
4350 static bfd_boolean
4351 match_reg_pair_operand (struct mips_arg_info *arg,
4352                         const struct mips_operand *operand_base)
4353 {
4354   const struct mips_reg_pair_operand *operand;
4355   unsigned int regno1, regno2, uval, num_vals;
4356
4357   operand = (const struct mips_reg_pair_operand *) operand_base;
4358   if (!match_reg (arg, operand->reg_type, &regno1)
4359       || !match_char (arg, ',')
4360       || !match_reg (arg, operand->reg_type, &regno2))
4361     return FALSE;
4362
4363   num_vals = 1 << operand_base->size;
4364   for (uval = 0; uval < num_vals; uval++)
4365     if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
4366       break;
4367   if (uval == num_vals)
4368     return FALSE;
4369
4370   insn_insert_operand (arg->insn, operand_base, uval);
4371   return TRUE;
4372 }
4373
4374 /* OP_PCREL matcher.  The caller chooses the relocation type.  */
4375
4376 static bfd_boolean
4377 match_pcrel_operand (struct mips_arg_info *arg)
4378 {
4379   bfd_reloc_code_real_type r[3];
4380
4381   return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
4382 }
4383
4384 /* OP_PERF_REG matcher.  */
4385
4386 static bfd_boolean
4387 match_perf_reg_operand (struct mips_arg_info *arg,
4388                         const struct mips_operand *operand)
4389 {
4390   offsetT sval;
4391
4392   if (!match_const_int (arg, &sval, 0))
4393     return FALSE;
4394
4395   if (sval != 0
4396       && (sval != 1
4397           || (mips_opts.arch == CPU_R5900
4398               && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
4399                   || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
4400     {
4401       if (arg->soft_match)
4402         return FALSE;
4403       as_bad (_("Invalid performance register (%ld)"), (unsigned long) sval);
4404     }
4405
4406   insn_insert_operand (arg->insn, operand, sval);
4407   return TRUE;
4408 }
4409
4410 /* OP_ADDIUSP matcher.  */
4411
4412 static bfd_boolean
4413 match_addiusp_operand (struct mips_arg_info *arg,
4414                        const struct mips_operand *operand)
4415 {
4416   offsetT sval;
4417   unsigned int uval;
4418
4419   if (!match_const_int (arg, &sval, -256))
4420     return FALSE;
4421
4422   if (sval % 4)
4423     return FALSE;
4424
4425   sval /= 4;
4426   if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
4427     return FALSE;
4428
4429   uval = (unsigned int) sval;
4430   uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
4431   insn_insert_operand (arg->insn, operand, uval);
4432   return TRUE;
4433 }
4434
4435 /* OP_CLO_CLZ_DEST matcher.  */
4436
4437 static bfd_boolean
4438 match_clo_clz_dest_operand (struct mips_arg_info *arg,
4439                             const struct mips_operand *operand)
4440 {
4441   unsigned int regno;
4442
4443   if (!match_reg (arg, OP_REG_GP, &regno))
4444     return FALSE;
4445
4446   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
4447   return TRUE;
4448 }
4449
4450 /* OP_LWM_SWM_LIST matcher.  */
4451
4452 static bfd_boolean
4453 match_lwm_swm_list_operand (struct mips_arg_info *arg,
4454                             const struct mips_operand *operand)
4455 {
4456   unsigned int reglist, sregs, ra, regno1, regno2;
4457   struct mips_arg_info reset;
4458
4459   reglist = 0;
4460   if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4461     return FALSE;
4462   do
4463     {
4464       if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
4465         {
4466           reglist |= 1 << FP;
4467           regno2 = S7;
4468         }
4469       reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
4470       reset = *arg;
4471     }
4472   while (match_char (arg, ',')
4473          && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
4474   *arg = reset;
4475
4476   if (operand->size == 2)
4477     {
4478       /* The list must include both ra and s0-sN, for 0 <= N <= 3.  E.g.:
4479
4480          s0, ra
4481          s0, s1, ra, s2, s3
4482          s0-s2, ra
4483
4484          and any permutations of these.  */
4485       if ((reglist & 0xfff1ffff) != 0x80010000)
4486         return FALSE;
4487
4488       sregs = (reglist >> 17) & 7;
4489       ra = 0;
4490     }
4491   else
4492     {
4493       /* The list must include at least one of ra and s0-sN,
4494          for 0 <= N <= 8.  (Note that there is a gap between s7 and s8,
4495          which are $23 and $30 respectively.)  E.g.:
4496
4497          ra
4498          s0
4499          ra, s0, s1, s2
4500          s0-s8
4501          s0-s5, ra
4502
4503          and any permutations of these.  */
4504       if ((reglist & 0x3f00ffff) != 0)
4505         return FALSE;
4506
4507       ra = (reglist >> 27) & 0x10;
4508       sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
4509     }
4510   sregs += 1;
4511   if ((sregs & -sregs) != sregs)
4512     return FALSE;
4513
4514   insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
4515   return TRUE;
4516 }
4517
4518 /* OP_ENTRY_EXIT_LIST matcher.  */
4519
4520 static unsigned int
4521 match_entry_exit_operand (struct mips_arg_info *arg,
4522                           const struct mips_operand *operand)
4523 {
4524   unsigned int mask;
4525   bfd_boolean is_exit;
4526
4527   /* The format is the same for both ENTRY and EXIT, but the constraints
4528      are different.  */
4529   is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
4530   mask = (is_exit ? 7 << 3 : 0);
4531   do
4532     {
4533       unsigned int regno1, regno2;
4534       bfd_boolean is_freg;
4535
4536       if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4537         is_freg = FALSE;
4538       else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
4539         is_freg = TRUE;
4540       else
4541         return FALSE;
4542
4543       if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
4544         {
4545           mask &= ~(7 << 3);
4546           mask |= (5 + regno2) << 3;
4547         }
4548       else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
4549         mask |= (regno2 - 3) << 3;
4550       else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
4551         mask |= (regno2 - 15) << 1;
4552       else if (regno1 == RA && regno2 == RA)
4553         mask |= 1;
4554       else
4555         return FALSE;
4556     }
4557   while (match_char (arg, ','));
4558
4559   insn_insert_operand (arg->insn, operand, mask);
4560   return TRUE;
4561 }
4562
4563 /* OP_SAVE_RESTORE_LIST matcher.  */
4564
4565 static bfd_boolean
4566 match_save_restore_list_operand (struct mips_arg_info *arg)
4567 {
4568   unsigned int opcode, args, statics, sregs;
4569   unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
4570   offsetT frame_size;
4571   const char *error;
4572
4573   error = 0;
4574   opcode = arg->insn->insn_opcode;
4575   frame_size = 0;
4576   num_frame_sizes = 0;
4577   args = 0;
4578   statics = 0;
4579   sregs = 0;
4580   do
4581     {
4582       unsigned int regno1, regno2;
4583
4584       if (arg->token->type == OT_INTEGER)
4585         {
4586           /* Handle the frame size.  */
4587           if (!match_const_int (arg, &frame_size, 0))
4588             return FALSE;
4589           num_frame_sizes += 1;
4590         }
4591       else
4592         {
4593           if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
4594             return FALSE;
4595
4596           while (regno1 <= regno2)
4597             {
4598               if (regno1 >= 4 && regno1 <= 7)
4599                 {
4600                   if (num_frame_sizes == 0)
4601                     /* args $a0-$a3 */
4602                     args |= 1 << (regno1 - 4);
4603                   else
4604                     /* statics $a0-$a3 */
4605                     statics |= 1 << (regno1 - 4);
4606                 }
4607               else if (regno1 >= 16 && regno1 <= 23)
4608                 /* $s0-$s7 */
4609                 sregs |= 1 << (regno1 - 16);
4610               else if (regno1 == 30)
4611                 /* $s8 */
4612                 sregs |= 1 << 8;
4613               else if (regno1 == 31)
4614                 /* Add $ra to insn.  */
4615                 opcode |= 0x40;
4616               else
4617                 return FALSE;
4618               regno1 += 1;
4619               if (regno1 == 24)
4620                 regno1 = 30;
4621             }
4622         }
4623     }
4624   while (match_char (arg, ','));
4625
4626   /* Encode args/statics combination.  */
4627   if (args & statics)
4628     return FALSE;
4629   else if (args == 0xf)
4630     /* All $a0-$a3 are args.  */
4631     opcode |= MIPS16_ALL_ARGS << 16;
4632   else if (statics == 0xf)
4633     /* All $a0-$a3 are statics.  */
4634     opcode |= MIPS16_ALL_STATICS << 16;
4635   else
4636     {
4637       /* Count arg registers.  */
4638       num_args = 0;
4639       while (args & 0x1)
4640         {
4641           args >>= 1;
4642           num_args += 1;
4643         }
4644       if (args != 0)
4645         return FALSE;
4646
4647       /* Count static registers.  */
4648       num_statics = 0;
4649       while (statics & 0x8)
4650         {
4651           statics = (statics << 1) & 0xf;
4652           num_statics += 1;
4653         }
4654       if (statics != 0)
4655         return FALSE;
4656
4657       /* Encode args/statics.  */
4658       opcode |= ((num_args << 2) | num_statics) << 16;
4659     }
4660
4661   /* Encode $s0/$s1.  */
4662   if (sregs & (1 << 0))         /* $s0 */
4663     opcode |= 0x20;
4664   if (sregs & (1 << 1))         /* $s1 */
4665     opcode |= 0x10;
4666   sregs >>= 2;
4667
4668   /* Encode $s2-$s8. */
4669   num_sregs = 0;
4670   while (sregs & 1)
4671     {
4672       sregs >>= 1;
4673       num_sregs += 1;
4674     }
4675   if (sregs != 0)
4676     return FALSE;
4677   opcode |= num_sregs << 24;
4678
4679   /* Encode frame size.  */
4680   if (num_frame_sizes == 0)
4681     error = _("Missing frame size");
4682   else if (num_frame_sizes > 1)
4683     error = _("Frame size specified twice");
4684   else if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
4685     error = _("Invalid frame size");
4686   else if (frame_size != 128 || (opcode >> 16) != 0)
4687     {
4688       frame_size /= 8;
4689       opcode |= (((frame_size & 0xf0) << 16)
4690                  | (frame_size & 0x0f));
4691     }
4692
4693   if (error)
4694     {
4695       if (arg->soft_match)
4696         return FALSE;
4697       as_bad ("%s", error);
4698     }
4699
4700   /* Finally build the instruction.  */
4701   if ((opcode >> 16) != 0 || frame_size == 0)
4702     opcode |= MIPS16_EXTEND;
4703   arg->insn->insn_opcode = opcode;
4704   return TRUE;
4705 }
4706
4707 /* OP_MDMX_IMM_REG matcher.  */
4708
4709 static bfd_boolean
4710 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
4711                             const struct mips_operand *operand)
4712 {
4713   unsigned int regno, uval;
4714   bfd_boolean is_qh;
4715   const struct mips_opcode *opcode;
4716
4717   /* The mips_opcode records whether this is an octobyte or quadhalf
4718      instruction.  Start out with that bit in place.  */
4719   opcode = arg->insn->insn_mo;
4720   uval = mips_extract_operand (operand, opcode->match);
4721   is_qh = (uval != 0);
4722
4723   if (arg->token->type == OT_REG || arg->token->type == OT_REG_ELEMENT)
4724     {
4725       if ((opcode->membership & INSN_5400)
4726           && strcmp (opcode->name, "rzu.ob") == 0)
4727         {
4728           if (arg->soft_match)
4729             return FALSE;
4730           as_bad (_("Operand %d of `%s' must be an immediate"),
4731                   arg->argnum, opcode->name);
4732         }
4733
4734       /* Check whether this is a vector register or a broadcast of
4735          a single element.  */
4736       if (arg->token->type == OT_REG_ELEMENT)
4737         {
4738           if (!match_regno (arg, OP_REG_VEC, arg->token->u.reg_element.regno,
4739                             &regno))
4740             return FALSE;
4741           if (arg->token->u.reg_element.index > (is_qh ? 3 : 7))
4742             {
4743               if (arg->soft_match)
4744                 return FALSE;
4745               as_bad (_("Invalid element selector"));
4746             }
4747           else
4748             uval |= arg->token->u.reg_element.index << (is_qh ? 2 : 1) << 5;
4749         }
4750       else
4751         {
4752           /* A full vector.  */
4753           if ((opcode->membership & INSN_5400)
4754               && (strcmp (opcode->name, "sll.ob") == 0
4755                   || strcmp (opcode->name, "srl.ob") == 0))
4756             {
4757               if (arg->soft_match)
4758                 return FALSE;
4759               as_bad (_("Operand %d of `%s' must be scalar"),
4760                       arg->argnum, opcode->name);
4761             }
4762
4763           if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
4764             return FALSE;
4765           if (is_qh)
4766             uval |= MDMX_FMTSEL_VEC_QH << 5;
4767           else
4768             uval |= MDMX_FMTSEL_VEC_OB << 5;
4769         }
4770       uval |= regno;
4771       ++arg->token;
4772     }
4773   else
4774     {
4775       offsetT sval;
4776
4777       if (!match_const_int (arg, &sval, 0))
4778         return FALSE;
4779       if (sval < 0 || sval > 31)
4780         {
4781           if (arg->soft_match)
4782             return FALSE;
4783           report_bad_range (arg->insn, arg->argnum, sval, 0, 31, FALSE);
4784         }
4785       uval |= (sval & 31);
4786       if (is_qh)
4787         uval |= MDMX_FMTSEL_IMM_QH << 5;
4788       else
4789         uval |= MDMX_FMTSEL_IMM_OB << 5;
4790     }
4791   insn_insert_operand (arg->insn, operand, uval);
4792   return TRUE;
4793 }
4794
4795 /* OP_PC matcher.  */
4796
4797 static bfd_boolean
4798 match_pc_operand (struct mips_arg_info *arg)
4799 {
4800   if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
4801     {
4802       ++arg->token;
4803       return TRUE;
4804     }
4805   return FALSE;
4806 }
4807
4808 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher.  OTHER_REGNO is the
4809    register that we need to match.  */
4810
4811 static bfd_boolean
4812 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
4813 {
4814   unsigned int regno;
4815
4816   return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
4817 }
4818
4819 /* Read a floating-point constant from S for LI.S or LI.D.  LENGTH is
4820    the length of the value in bytes (4 for float, 8 for double) and
4821    USING_GPRS says whether the destination is a GPR rather than an FPR.
4822
4823    Return the constant in IMM and OFFSET as follows:
4824
4825    - If the constant should be loaded via memory, set IMM to O_absent and
4826      OFFSET to the memory address.
4827
4828    - Otherwise, if the constant should be loaded into two 32-bit registers,
4829      set IMM to the O_constant to load into the high register and OFFSET
4830      to the corresponding value for the low register.
4831
4832    - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
4833
4834    These constants only appear as the last operand in an instruction,
4835    and every instruction that accepts them in any variant accepts them
4836    in all variants.  This means we don't have to worry about backing out
4837    any changes if the instruction does not match.  We just match
4838    unconditionally and report an error if the constant is invalid.  */
4839
4840 static bfd_boolean
4841 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
4842                       expressionS *offset, int length, bfd_boolean using_gprs)
4843 {
4844   char *p;
4845   segT seg, new_seg;
4846   subsegT subseg;
4847   const char *newname;
4848   unsigned char *data;
4849
4850   /* Where the constant is placed is based on how the MIPS assembler
4851      does things:
4852
4853      length == 4 && using_gprs  -- immediate value only
4854      length == 8 && using_gprs  -- .rdata or immediate value
4855      length == 4 && !using_gprs -- .lit4 or immediate value
4856      length == 8 && !using_gprs -- .lit8 or immediate value
4857
4858      The .lit4 and .lit8 sections are only used if permitted by the
4859      -G argument.  */
4860   if (arg->token->type != OT_FLOAT)
4861     return FALSE;
4862
4863   gas_assert (arg->token->u.flt.length == length);
4864   data = arg->token->u.flt.data;
4865   ++arg->token;
4866
4867   /* Handle 32-bit constants for which an immediate value is best.  */
4868   if (length == 4
4869       && (using_gprs
4870           || g_switch_value < 4
4871           || (data[0] == 0 && data[1] == 0)
4872           || (data[2] == 0 && data[3] == 0)))
4873     {
4874       imm->X_op = O_constant;
4875       if (!target_big_endian)
4876         imm->X_add_number = bfd_getl32 (data);
4877       else
4878         imm->X_add_number = bfd_getb32 (data);
4879       offset->X_op = O_absent;
4880       return TRUE;
4881     }
4882
4883   /* Handle 64-bit constants for which an immediate value is best.  */
4884   if (length == 8
4885       && !mips_disable_float_construction
4886       /* Constants can only be constructed in GPRs and copied
4887          to FPRs if the GPRs are at least as wide as the FPRs.
4888          Force the constant into memory if we are using 64-bit FPRs
4889          but the GPRs are only 32 bits wide.  */
4890       /* ??? No longer true with the addition of MTHC1, but this
4891          is legacy code...  */
4892       && (using_gprs || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
4893       && ((data[0] == 0 && data[1] == 0)
4894           || (data[2] == 0 && data[3] == 0))
4895       && ((data[4] == 0 && data[5] == 0)
4896           || (data[6] == 0 && data[7] == 0)))
4897     {
4898       /* The value is simple enough to load with a couple of instructions.
4899          If using 32-bit registers, set IMM to the high order 32 bits and
4900          OFFSET to the low order 32 bits.  Otherwise, set IMM to the entire
4901          64 bit constant.  */
4902       if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
4903         {
4904           imm->X_op = O_constant;
4905           offset->X_op = O_constant;
4906           if (!target_big_endian)
4907             {
4908               imm->X_add_number = bfd_getl32 (data + 4);
4909               offset->X_add_number = bfd_getl32 (data);
4910             }
4911           else
4912             {
4913               imm->X_add_number = bfd_getb32 (data);
4914               offset->X_add_number = bfd_getb32 (data + 4);
4915             }
4916           if (offset->X_add_number == 0)
4917             offset->X_op = O_absent;
4918         }
4919       else
4920         {
4921           imm->X_op = O_constant;
4922           if (!target_big_endian)
4923             imm->X_add_number = bfd_getl64 (data);
4924           else
4925             imm->X_add_number = bfd_getb64 (data);
4926           offset->X_op = O_absent;
4927         }
4928       return TRUE;
4929     }
4930
4931   /* Switch to the right section.  */
4932   seg = now_seg;
4933   subseg = now_subseg;
4934   if (length == 4)
4935     {
4936       gas_assert (!using_gprs && g_switch_value >= 4);
4937       newname = ".lit4";
4938     }
4939   else
4940     {
4941       if (using_gprs || g_switch_value < 8)
4942         newname = RDATA_SECTION_NAME;
4943       else
4944         newname = ".lit8";
4945     }
4946
4947   new_seg = subseg_new (newname, (subsegT) 0);
4948   bfd_set_section_flags (stdoutput, new_seg,
4949                          SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
4950   frag_align (length == 4 ? 2 : 3, 0, 0);
4951   if (strncmp (TARGET_OS, "elf", 3) != 0)
4952     record_alignment (new_seg, 4);
4953   else
4954     record_alignment (new_seg, length == 4 ? 2 : 3);
4955   if (seg == now_seg)
4956     as_bad (_("Can't use floating point insn in this section"));
4957
4958   /* Set the argument to the current address in the section.  */
4959   imm->X_op = O_absent;
4960   offset->X_op = O_symbol;
4961   offset->X_add_symbol = symbol_temp_new_now ();
4962   offset->X_add_number = 0;
4963
4964   /* Put the floating point number into the section.  */
4965   p = frag_more (length);
4966   memcpy (p, data, length);
4967
4968   /* Switch back to the original section.  */
4969   subseg_set (seg, subseg);
4970   return TRUE;
4971 }
4972
4973 /* S is the text seen for ARG.  Match it against OPERAND.  Return the end
4974    of the argument text if the match is successful, otherwise return null.  */
4975
4976 static bfd_boolean
4977 match_operand (struct mips_arg_info *arg,
4978                const struct mips_operand *operand)
4979 {
4980   switch (operand->type)
4981     {
4982     case OP_INT:
4983       return match_int_operand (arg, operand);
4984
4985     case OP_MAPPED_INT:
4986       return match_mapped_int_operand (arg, operand);
4987
4988     case OP_MSB:
4989       return match_msb_operand (arg, operand);
4990
4991     case OP_REG:
4992       return match_reg_operand (arg, operand);
4993
4994     case OP_REG_PAIR:
4995       return match_reg_pair_operand (arg, operand);
4996
4997     case OP_PCREL:
4998       return match_pcrel_operand (arg);
4999
5000     case OP_PERF_REG:
5001       return match_perf_reg_operand (arg, operand);
5002
5003     case OP_ADDIUSP_INT:
5004       return match_addiusp_operand (arg, operand);
5005
5006     case OP_CLO_CLZ_DEST:
5007       return match_clo_clz_dest_operand (arg, operand);
5008
5009     case OP_LWM_SWM_LIST:
5010       return match_lwm_swm_list_operand (arg, operand);
5011
5012     case OP_ENTRY_EXIT_LIST:
5013       return match_entry_exit_operand (arg, operand);
5014
5015     case OP_SAVE_RESTORE_LIST:
5016       return match_save_restore_list_operand (arg);
5017
5018     case OP_MDMX_IMM_REG:
5019       return match_mdmx_imm_reg_operand (arg, operand);
5020
5021     case OP_REPEAT_DEST_REG:
5022       return match_tied_reg_operand (arg, arg->dest_regno);
5023
5024     case OP_REPEAT_PREV_REG:
5025       return match_tied_reg_operand (arg, arg->last_regno);
5026
5027     case OP_PC:
5028       return match_pc_operand (arg);
5029     }
5030   abort ();
5031 }
5032
5033 /* ARG is the state after successfully matching an instruction.
5034    Issue any queued-up warnings.  */
5035
5036 static void
5037 check_completed_insn (struct mips_arg_info *arg)
5038 {
5039   if (arg->seen_at)
5040     {
5041       if (AT == ATREG)
5042         as_warn (_("Used $at without \".set noat\""));
5043       else
5044         as_warn (_("Used $%u with \".set at=$%u\""), AT, AT);
5045     }
5046 }
5047
5048 /* Return true if modifying general-purpose register REG needs a delay.  */
5049
5050 static bfd_boolean
5051 reg_needs_delay (unsigned int reg)
5052 {
5053   unsigned long prev_pinfo;
5054
5055   prev_pinfo = history[0].insn_mo->pinfo;
5056   if (!mips_opts.noreorder
5057       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY) && !gpr_interlocks)
5058           || ((prev_pinfo & INSN_LOAD_COPROC_DELAY) && !cop_interlocks))
5059       && (gpr_write_mask (&history[0]) & (1 << reg)))
5060     return TRUE;
5061
5062   return FALSE;
5063 }
5064
5065 /* Classify an instruction according to the FIX_VR4120_* enumeration.
5066    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
5067    by VR4120 errata.  */
5068
5069 static unsigned int
5070 classify_vr4120_insn (const char *name)
5071 {
5072   if (strncmp (name, "macc", 4) == 0)
5073     return FIX_VR4120_MACC;
5074   if (strncmp (name, "dmacc", 5) == 0)
5075     return FIX_VR4120_DMACC;
5076   if (strncmp (name, "mult", 4) == 0)
5077     return FIX_VR4120_MULT;
5078   if (strncmp (name, "dmult", 5) == 0)
5079     return FIX_VR4120_DMULT;
5080   if (strstr (name, "div"))
5081     return FIX_VR4120_DIV;
5082   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
5083     return FIX_VR4120_MTHILO;
5084   return NUM_FIX_VR4120_CLASSES;
5085 }
5086
5087 #define INSN_ERET  0x42000018
5088 #define INSN_DERET 0x4200001f
5089
5090 /* Return the number of instructions that must separate INSN1 and INSN2,
5091    where INSN1 is the earlier instruction.  Return the worst-case value
5092    for any INSN2 if INSN2 is null.  */
5093
5094 static unsigned int
5095 insns_between (const struct mips_cl_insn *insn1,
5096                const struct mips_cl_insn *insn2)
5097 {
5098   unsigned long pinfo1, pinfo2;
5099   unsigned int mask;
5100
5101   /* If INFO2 is null, pessimistically assume that all flags are set for
5102      the second instruction.  */
5103   pinfo1 = insn1->insn_mo->pinfo;
5104   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
5105
5106   /* For most targets, write-after-read dependencies on the HI and LO
5107      registers must be separated by at least two instructions.  */
5108   if (!hilo_interlocks)
5109     {
5110       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
5111         return 2;
5112       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
5113         return 2;
5114     }
5115
5116   /* If we're working around r7000 errata, there must be two instructions
5117      between an mfhi or mflo and any instruction that uses the result.  */
5118   if (mips_7000_hilo_fix
5119       && !mips_opts.micromips
5120       && MF_HILO_INSN (pinfo1)
5121       && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
5122     return 2;
5123
5124   /* If we're working around 24K errata, one instruction is required
5125      if an ERET or DERET is followed by a branch instruction.  */
5126   if (mips_fix_24k && !mips_opts.micromips)
5127     {
5128       if (insn1->insn_opcode == INSN_ERET
5129           || insn1->insn_opcode == INSN_DERET)
5130         {
5131           if (insn2 == NULL
5132               || insn2->insn_opcode == INSN_ERET
5133               || insn2->insn_opcode == INSN_DERET
5134               || delayed_branch_p (insn2))
5135             return 1;
5136         }
5137     }
5138
5139   /* If working around VR4120 errata, check for combinations that need
5140      a single intervening instruction.  */
5141   if (mips_fix_vr4120 && !mips_opts.micromips)
5142     {
5143       unsigned int class1, class2;
5144
5145       class1 = classify_vr4120_insn (insn1->insn_mo->name);
5146       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
5147         {
5148           if (insn2 == NULL)
5149             return 1;
5150           class2 = classify_vr4120_insn (insn2->insn_mo->name);
5151           if (vr4120_conflicts[class1] & (1 << class2))
5152             return 1;
5153         }
5154     }
5155
5156   if (!HAVE_CODE_COMPRESSION)
5157     {
5158       /* Check for GPR or coprocessor load delays.  All such delays
5159          are on the RT register.  */
5160       /* Itbl support may require additional care here.  */
5161       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
5162           || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
5163         {
5164           if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
5165             return 1;
5166         }
5167
5168       /* Check for generic coprocessor hazards.
5169
5170          This case is not handled very well.  There is no special
5171          knowledge of CP0 handling, and the coprocessors other than
5172          the floating point unit are not distinguished at all.  */
5173       /* Itbl support may require additional care here. FIXME!
5174          Need to modify this to include knowledge about
5175          user specified delays!  */
5176       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
5177                || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
5178         {
5179           /* Handle cases where INSN1 writes to a known general coprocessor
5180              register.  There must be a one instruction delay before INSN2
5181              if INSN2 reads that register, otherwise no delay is needed.  */
5182           mask = fpr_write_mask (insn1);
5183           if (mask != 0)
5184             {
5185               if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
5186                 return 1;
5187             }
5188           else
5189             {
5190               /* Read-after-write dependencies on the control registers
5191                  require a two-instruction gap.  */
5192               if ((pinfo1 & INSN_WRITE_COND_CODE)
5193                   && (pinfo2 & INSN_READ_COND_CODE))
5194                 return 2;
5195
5196               /* We don't know exactly what INSN1 does.  If INSN2 is
5197                  also a coprocessor instruction, assume there must be
5198                  a one instruction gap.  */
5199               if (pinfo2 & INSN_COP)
5200                 return 1;
5201             }
5202         }
5203
5204       /* Check for read-after-write dependencies on the coprocessor
5205          control registers in cases where INSN1 does not need a general
5206          coprocessor delay.  This means that INSN1 is a floating point
5207          comparison instruction.  */
5208       /* Itbl support may require additional care here.  */
5209       else if (!cop_interlocks
5210                && (pinfo1 & INSN_WRITE_COND_CODE)
5211                && (pinfo2 & INSN_READ_COND_CODE))
5212         return 1;
5213     }
5214
5215   return 0;
5216 }
5217
5218 /* Return the number of nops that would be needed to work around the
5219    VR4130 mflo/mfhi errata if instruction INSN immediately followed
5220    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
5221    that are contained within the first IGNORE instructions of HIST.  */
5222
5223 static int
5224 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
5225                  const struct mips_cl_insn *insn)
5226 {
5227   int i, j;
5228   unsigned int mask;
5229
5230   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
5231      are not affected by the errata.  */
5232   if (insn != 0
5233       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
5234           || strcmp (insn->insn_mo->name, "mtlo") == 0
5235           || strcmp (insn->insn_mo->name, "mthi") == 0))
5236     return 0;
5237
5238   /* Search for the first MFLO or MFHI.  */
5239   for (i = 0; i < MAX_VR4130_NOPS; i++)
5240     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
5241       {
5242         /* Extract the destination register.  */
5243         mask = gpr_write_mask (&hist[i]);
5244
5245         /* No nops are needed if INSN reads that register.  */
5246         if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
5247           return 0;
5248
5249         /* ...or if any of the intervening instructions do.  */
5250         for (j = 0; j < i; j++)
5251           if (gpr_read_mask (&hist[j]) & mask)
5252             return 0;
5253
5254         if (i >= ignore)
5255           return MAX_VR4130_NOPS - i;
5256       }
5257   return 0;
5258 }
5259
5260 #define BASE_REG_EQ(INSN1, INSN2)       \
5261   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
5262       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
5263
5264 /* Return the minimum alignment for this store instruction.  */
5265
5266 static int
5267 fix_24k_align_to (const struct mips_opcode *mo)
5268 {
5269   if (strcmp (mo->name, "sh") == 0)
5270     return 2;
5271
5272   if (strcmp (mo->name, "swc1") == 0
5273       || strcmp (mo->name, "swc2") == 0
5274       || strcmp (mo->name, "sw") == 0
5275       || strcmp (mo->name, "sc") == 0
5276       || strcmp (mo->name, "s.s") == 0)
5277     return 4;
5278
5279   if (strcmp (mo->name, "sdc1") == 0
5280       || strcmp (mo->name, "sdc2") == 0
5281       || strcmp (mo->name, "s.d") == 0)
5282     return 8;
5283
5284   /* sb, swl, swr */
5285   return 1;
5286 }
5287
5288 struct fix_24k_store_info
5289   {
5290     /* Immediate offset, if any, for this store instruction.  */
5291     short off;
5292     /* Alignment required by this store instruction.  */
5293     int align_to;
5294     /* True for register offsets.  */
5295     int register_offset;
5296   };
5297
5298 /* Comparison function used by qsort.  */
5299
5300 static int
5301 fix_24k_sort (const void *a, const void *b)
5302 {
5303   const struct fix_24k_store_info *pos1 = a;
5304   const struct fix_24k_store_info *pos2 = b;
5305
5306   return (pos1->off - pos2->off);
5307 }
5308
5309 /* INSN is a store instruction.  Try to record the store information
5310    in STINFO.  Return false if the information isn't known.  */
5311
5312 static bfd_boolean
5313 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
5314                            const struct mips_cl_insn *insn)
5315 {
5316   /* The instruction must have a known offset.  */
5317   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5318     return FALSE;
5319
5320   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
5321   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5322   return TRUE;
5323 }
5324
5325 /* Return the number of nops that would be needed to work around the 24k
5326    "lost data on stores during refill" errata if instruction INSN
5327    immediately followed the 2 instructions described by HIST.
5328    Ignore hazards that are contained within the first IGNORE
5329    instructions of HIST.
5330
5331    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
5332    for the data cache refills and store data. The following describes
5333    the scenario where the store data could be lost.
5334
5335    * A data cache miss, due to either a load or a store, causing fill
5336      data to be supplied by the memory subsystem
5337    * The first three doublewords of fill data are returned and written
5338      into the cache
5339    * A sequence of four stores occurs in consecutive cycles around the
5340      final doubleword of the fill:
5341    * Store A
5342    * Store B
5343    * Store C
5344    * Zero, One or more instructions
5345    * Store D
5346
5347    The four stores A-D must be to different doublewords of the line that
5348    is being filled. The fourth instruction in the sequence above permits
5349    the fill of the final doubleword to be transferred from the FSB into
5350    the cache. In the sequence above, the stores may be either integer
5351    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
5352    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
5353    different doublewords on the line. If the floating point unit is
5354    running in 1:2 mode, it is not possible to create the sequence above
5355    using only floating point store instructions.
5356
5357    In this case, the cache line being filled is incorrectly marked
5358    invalid, thereby losing the data from any store to the line that
5359    occurs between the original miss and the completion of the five
5360    cycle sequence shown above.
5361
5362    The workarounds are:
5363
5364    * Run the data cache in write-through mode.
5365    * Insert a non-store instruction between
5366      Store A and Store B or Store B and Store C.  */
5367   
5368 static int
5369 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
5370               const struct mips_cl_insn *insn)
5371 {
5372   struct fix_24k_store_info pos[3];
5373   int align, i, base_offset;
5374
5375   if (ignore >= 2)
5376     return 0;
5377
5378   /* If the previous instruction wasn't a store, there's nothing to
5379      worry about.  */
5380   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5381     return 0;
5382
5383   /* If the instructions after the previous one are unknown, we have
5384      to assume the worst.  */
5385   if (!insn)
5386     return 1;
5387
5388   /* Check whether we are dealing with three consecutive stores.  */
5389   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
5390       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
5391     return 0;
5392
5393   /* If we don't know the relationship between the store addresses,
5394      assume the worst.  */
5395   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
5396       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
5397     return 1;
5398
5399   if (!fix_24k_record_store_info (&pos[0], insn)
5400       || !fix_24k_record_store_info (&pos[1], &hist[0])
5401       || !fix_24k_record_store_info (&pos[2], &hist[1]))
5402     return 1;
5403
5404   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
5405
5406   /* Pick a value of ALIGN and X such that all offsets are adjusted by
5407      X bytes and such that the base register + X is known to be aligned
5408      to align bytes.  */
5409
5410   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
5411     align = 8;
5412   else
5413     {
5414       align = pos[0].align_to;
5415       base_offset = pos[0].off;
5416       for (i = 1; i < 3; i++)
5417         if (align < pos[i].align_to)
5418           {
5419             align = pos[i].align_to;
5420             base_offset = pos[i].off;
5421           }
5422       for (i = 0; i < 3; i++)
5423         pos[i].off -= base_offset;
5424     }
5425
5426   pos[0].off &= ~align + 1;
5427   pos[1].off &= ~align + 1;
5428   pos[2].off &= ~align + 1;
5429
5430   /* If any two stores write to the same chunk, they also write to the
5431      same doubleword.  The offsets are still sorted at this point.  */
5432   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
5433     return 0;
5434
5435   /* A range of at least 9 bytes is needed for the stores to be in
5436      non-overlapping doublewords.  */
5437   if (pos[2].off - pos[0].off <= 8)
5438     return 0;
5439
5440   if (pos[2].off - pos[1].off >= 24
5441       || pos[1].off - pos[0].off >= 24
5442       || pos[2].off - pos[0].off >= 32)
5443     return 0;
5444
5445   return 1;
5446 }
5447
5448 /* Return the number of nops that would be needed if instruction INSN
5449    immediately followed the MAX_NOPS instructions given by HIST,
5450    where HIST[0] is the most recent instruction.  Ignore hazards
5451    between INSN and the first IGNORE instructions in HIST.
5452
5453    If INSN is null, return the worse-case number of nops for any
5454    instruction.  */
5455
5456 static int
5457 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
5458                const struct mips_cl_insn *insn)
5459 {
5460   int i, nops, tmp_nops;
5461
5462   nops = 0;
5463   for (i = ignore; i < MAX_DELAY_NOPS; i++)
5464     {
5465       tmp_nops = insns_between (hist + i, insn) - i;
5466       if (tmp_nops > nops)
5467         nops = tmp_nops;
5468     }
5469
5470   if (mips_fix_vr4130 && !mips_opts.micromips)
5471     {
5472       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
5473       if (tmp_nops > nops)
5474         nops = tmp_nops;
5475     }
5476
5477   if (mips_fix_24k && !mips_opts.micromips)
5478     {
5479       tmp_nops = nops_for_24k (ignore, hist, insn);
5480       if (tmp_nops > nops)
5481         nops = tmp_nops;
5482     }
5483
5484   return nops;
5485 }
5486
5487 /* The variable arguments provide NUM_INSNS extra instructions that
5488    might be added to HIST.  Return the largest number of nops that
5489    would be needed after the extended sequence, ignoring hazards
5490    in the first IGNORE instructions.  */
5491
5492 static int
5493 nops_for_sequence (int num_insns, int ignore,
5494                    const struct mips_cl_insn *hist, ...)
5495 {
5496   va_list args;
5497   struct mips_cl_insn buffer[MAX_NOPS];
5498   struct mips_cl_insn *cursor;
5499   int nops;
5500
5501   va_start (args, hist);
5502   cursor = buffer + num_insns;
5503   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
5504   while (cursor > buffer)
5505     *--cursor = *va_arg (args, const struct mips_cl_insn *);
5506
5507   nops = nops_for_insn (ignore, buffer, NULL);
5508   va_end (args);
5509   return nops;
5510 }
5511
5512 /* Like nops_for_insn, but if INSN is a branch, take into account the
5513    worst-case delay for the branch target.  */
5514
5515 static int
5516 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
5517                          const struct mips_cl_insn *insn)
5518 {
5519   int nops, tmp_nops;
5520
5521   nops = nops_for_insn (ignore, hist, insn);
5522   if (delayed_branch_p (insn))
5523     {
5524       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
5525                                     hist, insn, get_delay_slot_nop (insn));
5526       if (tmp_nops > nops)
5527         nops = tmp_nops;
5528     }
5529   else if (compact_branch_p (insn))
5530     {
5531       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
5532       if (tmp_nops > nops)
5533         nops = tmp_nops;
5534     }
5535   return nops;
5536 }
5537
5538 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
5539
5540 static void
5541 fix_loongson2f_nop (struct mips_cl_insn * ip)
5542 {
5543   gas_assert (!HAVE_CODE_COMPRESSION);
5544   if (strcmp (ip->insn_mo->name, "nop") == 0)
5545     ip->insn_opcode = LOONGSON2F_NOP_INSN;
5546 }
5547
5548 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
5549                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
5550
5551 static void
5552 fix_loongson2f_jump (struct mips_cl_insn * ip)
5553 {
5554   gas_assert (!HAVE_CODE_COMPRESSION);
5555   if (strcmp (ip->insn_mo->name, "j") == 0
5556       || strcmp (ip->insn_mo->name, "jr") == 0
5557       || strcmp (ip->insn_mo->name, "jalr") == 0)
5558     {
5559       int sreg;
5560       expressionS ep;
5561
5562       if (! mips_opts.at)
5563         return;
5564
5565       sreg = EXTRACT_OPERAND (0, RS, *ip);
5566       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
5567         return;
5568
5569       ep.X_op = O_constant;
5570       ep.X_add_number = 0xcfff0000;
5571       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
5572       ep.X_add_number = 0xffff;
5573       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
5574       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
5575     }
5576 }
5577
5578 static void
5579 fix_loongson2f (struct mips_cl_insn * ip)
5580 {
5581   if (mips_fix_loongson2f_nop)
5582     fix_loongson2f_nop (ip);
5583
5584   if (mips_fix_loongson2f_jump)
5585     fix_loongson2f_jump (ip);
5586 }
5587
5588 /* IP is a branch that has a delay slot, and we need to fill it
5589    automatically.   Return true if we can do that by swapping IP
5590    with the previous instruction.
5591    ADDRESS_EXPR is an operand of the instruction to be used with
5592    RELOC_TYPE.  */
5593
5594 static bfd_boolean
5595 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
5596                    bfd_reloc_code_real_type *reloc_type)
5597 {
5598   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
5599   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
5600
5601   /* -O2 and above is required for this optimization.  */
5602   if (mips_optimize < 2)
5603     return FALSE;
5604
5605   /* If we have seen .set volatile or .set nomove, don't optimize.  */
5606   if (mips_opts.nomove)
5607     return FALSE;
5608
5609   /* We can't swap if the previous instruction's position is fixed.  */
5610   if (history[0].fixed_p)
5611     return FALSE;
5612
5613   /* If the previous previous insn was in a .set noreorder, we can't
5614      swap.  Actually, the MIPS assembler will swap in this situation.
5615      However, gcc configured -with-gnu-as will generate code like
5616
5617         .set    noreorder
5618         lw      $4,XXX
5619         .set    reorder
5620         INSN
5621         bne     $4,$0,foo
5622
5623      in which we can not swap the bne and INSN.  If gcc is not configured
5624      -with-gnu-as, it does not output the .set pseudo-ops.  */
5625   if (history[1].noreorder_p)
5626     return FALSE;
5627
5628   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
5629      This means that the previous instruction was a 4-byte one anyhow.  */
5630   if (mips_opts.mips16 && history[0].fixp[0])
5631     return FALSE;
5632
5633   /* If the branch is itself the target of a branch, we can not swap.
5634      We cheat on this; all we check for is whether there is a label on
5635      this instruction.  If there are any branches to anything other than
5636      a label, users must use .set noreorder.  */
5637   if (seg_info (now_seg)->label_list)
5638     return FALSE;
5639
5640   /* If the previous instruction is in a variant frag other than this
5641      branch's one, we cannot do the swap.  This does not apply to
5642      MIPS16 code, which uses variant frags for different purposes.  */
5643   if (!mips_opts.mips16
5644       && history[0].frag
5645       && history[0].frag->fr_type == rs_machine_dependent)
5646     return FALSE;
5647
5648   /* We do not swap with instructions that cannot architecturally
5649      be placed in a branch delay slot, such as SYNC or ERET.  We
5650      also refrain from swapping with a trap instruction, since it
5651      complicates trap handlers to have the trap instruction be in
5652      a delay slot.  */
5653   prev_pinfo = history[0].insn_mo->pinfo;
5654   if (prev_pinfo & INSN_NO_DELAY_SLOT)
5655     return FALSE;
5656
5657   /* Check for conflicts between the branch and the instructions
5658      before the candidate delay slot.  */
5659   if (nops_for_insn (0, history + 1, ip) > 0)
5660     return FALSE;
5661
5662   /* Check for conflicts between the swapped sequence and the
5663      target of the branch.  */
5664   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
5665     return FALSE;
5666
5667   /* If the branch reads a register that the previous
5668      instruction sets, we can not swap.  */
5669   gpr_read = gpr_read_mask (ip);
5670   prev_gpr_write = gpr_write_mask (&history[0]);
5671   if (gpr_read & prev_gpr_write)
5672     return FALSE;
5673
5674   /* If the branch writes a register that the previous
5675      instruction sets, we can not swap.  */
5676   gpr_write = gpr_write_mask (ip);
5677   if (gpr_write & prev_gpr_write)
5678     return FALSE;
5679
5680   /* If the branch writes a register that the previous
5681      instruction reads, we can not swap.  */
5682   prev_gpr_read = gpr_read_mask (&history[0]);
5683   if (gpr_write & prev_gpr_read)
5684     return FALSE;
5685
5686   /* If one instruction sets a condition code and the
5687      other one uses a condition code, we can not swap.  */
5688   pinfo = ip->insn_mo->pinfo;
5689   if ((pinfo & INSN_READ_COND_CODE)
5690       && (prev_pinfo & INSN_WRITE_COND_CODE))
5691     return FALSE;
5692   if ((pinfo & INSN_WRITE_COND_CODE)
5693       && (prev_pinfo & INSN_READ_COND_CODE))
5694     return FALSE;
5695
5696   /* If the previous instruction uses the PC, we can not swap.  */
5697   prev_pinfo2 = history[0].insn_mo->pinfo2;
5698   if (prev_pinfo2 & INSN2_READ_PC)
5699     return FALSE;
5700
5701   /* If the previous instruction has an incorrect size for a fixed
5702      branch delay slot in microMIPS mode, we cannot swap.  */
5703   pinfo2 = ip->insn_mo->pinfo2;
5704   if (mips_opts.micromips
5705       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
5706       && insn_length (history) != 2)
5707     return FALSE;
5708   if (mips_opts.micromips
5709       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
5710       && insn_length (history) != 4)
5711     return FALSE;
5712
5713   /* On R5900 short loops need to be fixed by inserting a nop in
5714      the branch delay slots.
5715      A short loop can be terminated too early.  */
5716   if (mips_opts.arch == CPU_R5900
5717       /* Check if instruction has a parameter, ignore "j $31". */
5718       && (address_expr != NULL)
5719       /* Parameter must be 16 bit. */
5720       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
5721       /* Branch to same segment. */
5722       && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
5723       /* Branch to same code fragment. */
5724       && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
5725       /* Can only calculate branch offset if value is known. */
5726       && symbol_constant_p(address_expr->X_add_symbol)
5727       /* Check if branch is really conditional. */
5728       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
5729         || (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
5730         || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
5731     {
5732       int distance;
5733       /* Check if loop is shorter than 6 instructions including
5734          branch and delay slot.  */
5735       distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
5736       if (distance <= 20)
5737         {
5738           int i;
5739           int rv;
5740
5741           rv = FALSE;
5742           /* When the loop includes branches or jumps,
5743              it is not a short loop. */
5744           for (i = 0; i < (distance / 4); i++)
5745             {
5746               if ((history[i].cleared_p)
5747                   || delayed_branch_p(&history[i]))
5748                 {
5749                   rv = TRUE;
5750                   break;
5751                 }
5752             }
5753           if (rv == FALSE)
5754             {
5755               /* Insert nop after branch to fix short loop. */
5756               return FALSE;
5757             }
5758         }
5759     }
5760
5761   return TRUE;
5762 }
5763
5764 /* Decide how we should add IP to the instruction stream.
5765    ADDRESS_EXPR is an operand of the instruction to be used with
5766    RELOC_TYPE.  */
5767
5768 static enum append_method
5769 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
5770                    bfd_reloc_code_real_type *reloc_type)
5771 {
5772   unsigned long pinfo, pinfo2;
5773
5774   /* The relaxed version of a macro sequence must be inherently
5775      hazard-free.  */
5776   if (mips_relax.sequence == 2)
5777     return APPEND_ADD;
5778
5779   /* We must not dabble with instructions in a ".set norerorder" block.  */
5780   if (mips_opts.noreorder)
5781     return APPEND_ADD;
5782
5783   /* Otherwise, it's our responsibility to fill branch delay slots.  */
5784   if (delayed_branch_p (ip))
5785     {
5786       if (!branch_likely_p (ip)
5787           && can_swap_branch_p (ip, address_expr, reloc_type))
5788         return APPEND_SWAP;
5789
5790       pinfo = ip->insn_mo->pinfo;
5791       pinfo2 = ip->insn_mo->pinfo2;
5792       if (mips_opts.mips16
5793           && ISA_SUPPORTS_MIPS16E
5794           && ((pinfo & MIPS16_INSN_READ_X) != 0
5795               || (pinfo2 & INSN2_READ_GPR_31) != 0))
5796         return APPEND_ADD_COMPACT;
5797
5798       return APPEND_ADD_WITH_NOP;
5799     }
5800
5801   return APPEND_ADD;
5802 }
5803
5804 /* IP is a MIPS16 instruction whose opcode we have just changed.
5805    Point IP->insn_mo to the new opcode's definition.  */
5806
5807 static void
5808 find_altered_mips16_opcode (struct mips_cl_insn *ip)
5809 {
5810   const struct mips_opcode *mo, *end;
5811
5812   end = &mips16_opcodes[bfd_mips16_num_opcodes];
5813   for (mo = ip->insn_mo; mo < end; mo++)
5814     if ((ip->insn_opcode & mo->mask) == mo->match)
5815       {
5816         ip->insn_mo = mo;
5817         return;
5818       }
5819   abort ();
5820 }
5821
5822 /* For microMIPS macros, we need to generate a local number label
5823    as the target of branches.  */
5824 #define MICROMIPS_LABEL_CHAR            '\037'
5825 static unsigned long micromips_target_label;
5826 static char micromips_target_name[32];
5827
5828 static char *
5829 micromips_label_name (void)
5830 {
5831   char *p = micromips_target_name;
5832   char symbol_name_temporary[24];
5833   unsigned long l;
5834   int i;
5835
5836   if (*p)
5837     return p;
5838
5839   i = 0;
5840   l = micromips_target_label;
5841 #ifdef LOCAL_LABEL_PREFIX
5842   *p++ = LOCAL_LABEL_PREFIX;
5843 #endif
5844   *p++ = 'L';
5845   *p++ = MICROMIPS_LABEL_CHAR;
5846   do
5847     {
5848       symbol_name_temporary[i++] = l % 10 + '0';
5849       l /= 10;
5850     }
5851   while (l != 0);
5852   while (i > 0)
5853     *p++ = symbol_name_temporary[--i];
5854   *p = '\0';
5855
5856   return micromips_target_name;
5857 }
5858
5859 static void
5860 micromips_label_expr (expressionS *label_expr)
5861 {
5862   label_expr->X_op = O_symbol;
5863   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
5864   label_expr->X_add_number = 0;
5865 }
5866
5867 static void
5868 micromips_label_inc (void)
5869 {
5870   micromips_target_label++;
5871   *micromips_target_name = '\0';
5872 }
5873
5874 static void
5875 micromips_add_label (void)
5876 {
5877   symbolS *s;
5878
5879   s = colon (micromips_label_name ());
5880   micromips_label_inc ();
5881   S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
5882 }
5883
5884 /* If assembling microMIPS code, then return the microMIPS reloc
5885    corresponding to the requested one if any.  Otherwise return
5886    the reloc unchanged.  */
5887
5888 static bfd_reloc_code_real_type
5889 micromips_map_reloc (bfd_reloc_code_real_type reloc)
5890 {
5891   static const bfd_reloc_code_real_type relocs[][2] =
5892     {
5893       /* Keep sorted incrementally by the left-hand key.  */
5894       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
5895       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
5896       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
5897       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
5898       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
5899       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
5900       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
5901       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
5902       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
5903       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
5904       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
5905       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
5906       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
5907       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
5908       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
5909       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
5910       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
5911       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
5912       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
5913       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
5914       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
5915       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
5916       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
5917       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
5918       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
5919       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
5920       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
5921     };
5922   bfd_reloc_code_real_type r;
5923   size_t i;
5924
5925   if (!mips_opts.micromips)
5926     return reloc;
5927   for (i = 0; i < ARRAY_SIZE (relocs); i++)
5928     {
5929       r = relocs[i][0];
5930       if (r > reloc)
5931         return reloc;
5932       if (r == reloc)
5933         return relocs[i][1];
5934     }
5935   return reloc;
5936 }
5937
5938 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
5939    Return true on success, storing the resolved value in RESULT.  */
5940
5941 static bfd_boolean
5942 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
5943                  offsetT *result)
5944 {
5945   switch (reloc)
5946     {
5947     case BFD_RELOC_MIPS_HIGHEST:
5948     case BFD_RELOC_MICROMIPS_HIGHEST:
5949       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
5950       return TRUE;
5951
5952     case BFD_RELOC_MIPS_HIGHER:
5953     case BFD_RELOC_MICROMIPS_HIGHER:
5954       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
5955       return TRUE;
5956
5957     case BFD_RELOC_HI16_S:
5958     case BFD_RELOC_MICROMIPS_HI16_S:
5959     case BFD_RELOC_MIPS16_HI16_S:
5960       *result = ((operand + 0x8000) >> 16) & 0xffff;
5961       return TRUE;
5962
5963     case BFD_RELOC_HI16:
5964     case BFD_RELOC_MICROMIPS_HI16:
5965     case BFD_RELOC_MIPS16_HI16:
5966       *result = (operand >> 16) & 0xffff;
5967       return TRUE;
5968
5969     case BFD_RELOC_LO16:
5970     case BFD_RELOC_MICROMIPS_LO16:
5971     case BFD_RELOC_MIPS16_LO16:
5972       *result = operand & 0xffff;
5973       return TRUE;
5974
5975     case BFD_RELOC_UNUSED:
5976       *result = operand;
5977       return TRUE;
5978
5979     default:
5980       return FALSE;
5981     }
5982 }
5983
5984 /* Output an instruction.  IP is the instruction information.
5985    ADDRESS_EXPR is an operand of the instruction to be used with
5986    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
5987    a macro expansion.  */
5988
5989 static void
5990 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
5991              bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
5992 {
5993   unsigned long prev_pinfo2, pinfo;
5994   bfd_boolean relaxed_branch = FALSE;
5995   enum append_method method;
5996   bfd_boolean relax32;
5997   int branch_disp;
5998
5999   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
6000     fix_loongson2f (ip);
6001
6002   file_ase_mips16 |= mips_opts.mips16;
6003   file_ase_micromips |= mips_opts.micromips;
6004
6005   prev_pinfo2 = history[0].insn_mo->pinfo2;
6006   pinfo = ip->insn_mo->pinfo;
6007
6008   if (mips_opts.micromips
6009       && !expansionp
6010       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
6011            && micromips_insn_length (ip->insn_mo) != 2)
6012           || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
6013               && micromips_insn_length (ip->insn_mo) != 4)))
6014     as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
6015              (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
6016
6017   if (address_expr == NULL)
6018     ip->complete_p = 1;
6019   else if (reloc_type[0] <= BFD_RELOC_UNUSED
6020            && reloc_type[1] == BFD_RELOC_UNUSED
6021            && reloc_type[2] == BFD_RELOC_UNUSED
6022            && address_expr->X_op == O_constant)
6023     {
6024       switch (*reloc_type)
6025         {
6026         case BFD_RELOC_MIPS_JMP:
6027           {
6028             int shift;
6029
6030             shift = mips_opts.micromips ? 1 : 2;
6031             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6032               as_bad (_("jump to misaligned address (0x%lx)"),
6033                       (unsigned long) address_expr->X_add_number);
6034             ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6035                                 & 0x3ffffff);
6036             ip->complete_p = 1;
6037           }
6038           break;
6039
6040         case BFD_RELOC_MIPS16_JMP:
6041           if ((address_expr->X_add_number & 3) != 0)
6042             as_bad (_("jump to misaligned address (0x%lx)"),
6043                     (unsigned long) address_expr->X_add_number);
6044           ip->insn_opcode |=
6045             (((address_expr->X_add_number & 0x7c0000) << 3)
6046                | ((address_expr->X_add_number & 0xf800000) >> 7)
6047                | ((address_expr->X_add_number & 0x3fffc) >> 2));
6048           ip->complete_p = 1;
6049           break;
6050
6051         case BFD_RELOC_16_PCREL_S2:
6052           {
6053             int shift;
6054
6055             shift = mips_opts.micromips ? 1 : 2;
6056             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
6057               as_bad (_("branch to misaligned address (0x%lx)"),
6058                       (unsigned long) address_expr->X_add_number);
6059             if (!mips_relax_branch)
6060               {
6061                 if ((address_expr->X_add_number + (1 << (shift + 15)))
6062                     & ~((1 << (shift + 16)) - 1))
6063                   as_bad (_("branch address range overflow (0x%lx)"),
6064                           (unsigned long) address_expr->X_add_number);
6065                 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
6066                                     & 0xffff);
6067               }
6068           }
6069           break;
6070
6071         default:
6072           {
6073             offsetT value;
6074
6075             if (calculate_reloc (*reloc_type, address_expr->X_add_number,
6076                                  &value))
6077               {
6078                 ip->insn_opcode |= value & 0xffff;
6079                 ip->complete_p = 1;
6080               }
6081           }
6082           break;
6083         }
6084     }
6085
6086   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
6087     {
6088       /* There are a lot of optimizations we could do that we don't.
6089          In particular, we do not, in general, reorder instructions.
6090          If you use gcc with optimization, it will reorder
6091          instructions and generally do much more optimization then we
6092          do here; repeating all that work in the assembler would only
6093          benefit hand written assembly code, and does not seem worth
6094          it.  */
6095       int nops = (mips_optimize == 0
6096                   ? nops_for_insn (0, history, NULL)
6097                   : nops_for_insn_or_target (0, history, ip));
6098       if (nops > 0)
6099         {
6100           fragS *old_frag;
6101           unsigned long old_frag_offset;
6102           int i;
6103
6104           old_frag = frag_now;
6105           old_frag_offset = frag_now_fix ();
6106
6107           for (i = 0; i < nops; i++)
6108             add_fixed_insn (NOP_INSN);
6109           insert_into_history (0, nops, NOP_INSN);
6110
6111           if (listing)
6112             {
6113               listing_prev_line ();
6114               /* We may be at the start of a variant frag.  In case we
6115                  are, make sure there is enough space for the frag
6116                  after the frags created by listing_prev_line.  The
6117                  argument to frag_grow here must be at least as large
6118                  as the argument to all other calls to frag_grow in
6119                  this file.  We don't have to worry about being in the
6120                  middle of a variant frag, because the variants insert
6121                  all needed nop instructions themselves.  */
6122               frag_grow (40);
6123             }
6124
6125           mips_move_text_labels ();
6126
6127 #ifndef NO_ECOFF_DEBUGGING
6128           if (ECOFF_DEBUGGING)
6129             ecoff_fix_loc (old_frag, old_frag_offset);
6130 #endif
6131         }
6132     }
6133   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
6134     {
6135       int nops;
6136
6137       /* Work out how many nops in prev_nop_frag are needed by IP,
6138          ignoring hazards generated by the first prev_nop_frag_since
6139          instructions.  */
6140       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
6141       gas_assert (nops <= prev_nop_frag_holds);
6142
6143       /* Enforce NOPS as a minimum.  */
6144       if (nops > prev_nop_frag_required)
6145         prev_nop_frag_required = nops;
6146
6147       if (prev_nop_frag_holds == prev_nop_frag_required)
6148         {
6149           /* Settle for the current number of nops.  Update the history
6150              accordingly (for the benefit of any future .set reorder code).  */
6151           prev_nop_frag = NULL;
6152           insert_into_history (prev_nop_frag_since,
6153                                prev_nop_frag_holds, NOP_INSN);
6154         }
6155       else
6156         {
6157           /* Allow this instruction to replace one of the nops that was
6158              tentatively added to prev_nop_frag.  */
6159           prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
6160           prev_nop_frag_holds--;
6161           prev_nop_frag_since++;
6162         }
6163     }
6164
6165   method = get_append_method (ip, address_expr, reloc_type);
6166   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
6167
6168   dwarf2_emit_insn (0);
6169   /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
6170      so "move" the instruction address accordingly.
6171
6172      Also, it doesn't seem appropriate for the assembler to reorder .loc
6173      entries.  If this instruction is a branch that we are going to swap
6174      with the previous instruction, the two instructions should be
6175      treated as a unit, and the debug information for both instructions
6176      should refer to the start of the branch sequence.  Using the
6177      current position is certainly wrong when swapping a 32-bit branch
6178      and a 16-bit delay slot, since the current position would then be
6179      in the middle of a branch.  */
6180   dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
6181
6182   relax32 = (mips_relax_branch
6183              /* Don't try branch relaxation within .set nomacro, or within
6184                 .set noat if we use $at for PIC computations.  If it turns
6185                 out that the branch was out-of-range, we'll get an error.  */
6186              && !mips_opts.warn_about_macros
6187              && (mips_opts.at || mips_pic == NO_PIC)
6188              /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
6189                 as they have no complementing branches.  */
6190              && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
6191
6192   if (!HAVE_CODE_COMPRESSION
6193       && address_expr
6194       && relax32
6195       && *reloc_type == BFD_RELOC_16_PCREL_S2
6196       && delayed_branch_p (ip))
6197     {
6198       relaxed_branch = TRUE;
6199       add_relaxed_insn (ip, (relaxed_branch_length
6200                              (NULL, NULL,
6201                               uncond_branch_p (ip) ? -1
6202                               : branch_likely_p (ip) ? 1
6203                               : 0)), 4,
6204                         RELAX_BRANCH_ENCODE
6205                         (AT,
6206                          uncond_branch_p (ip),
6207                          branch_likely_p (ip),
6208                          pinfo & INSN_WRITE_GPR_31,
6209                          0),
6210                         address_expr->X_add_symbol,
6211                         address_expr->X_add_number);
6212       *reloc_type = BFD_RELOC_UNUSED;
6213     }
6214   else if (mips_opts.micromips
6215            && address_expr
6216            && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
6217                || *reloc_type > BFD_RELOC_UNUSED)
6218            && (delayed_branch_p (ip) || compact_branch_p (ip))
6219            /* Don't try branch relaxation when users specify
6220               16-bit/32-bit instructions.  */
6221            && !forced_insn_length)
6222     {
6223       bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
6224       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
6225       int uncond = uncond_branch_p (ip) ? -1 : 0;
6226       int compact = compact_branch_p (ip);
6227       int al = pinfo & INSN_WRITE_GPR_31;
6228       int length32;
6229
6230       gas_assert (address_expr != NULL);
6231       gas_assert (!mips_relax.sequence);
6232
6233       relaxed_branch = TRUE;
6234       length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
6235       add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
6236                         RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
6237                                                 relax32, 0, 0),
6238                         address_expr->X_add_symbol,
6239                         address_expr->X_add_number);
6240       *reloc_type = BFD_RELOC_UNUSED;
6241     }
6242   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
6243     {
6244       /* We need to set up a variant frag.  */
6245       gas_assert (address_expr != NULL);
6246       add_relaxed_insn (ip, 4, 0,
6247                         RELAX_MIPS16_ENCODE
6248                         (*reloc_type - BFD_RELOC_UNUSED,
6249                          forced_insn_length == 2, forced_insn_length == 4,
6250                          delayed_branch_p (&history[0]),
6251                          history[0].mips16_absolute_jump_p),
6252                         make_expr_symbol (address_expr), 0);
6253     }
6254   else if (mips_opts.mips16 && insn_length (ip) == 2)
6255     {
6256       if (!delayed_branch_p (ip))
6257         /* Make sure there is enough room to swap this instruction with
6258            a following jump instruction.  */
6259         frag_grow (6);
6260       add_fixed_insn (ip);
6261     }
6262   else
6263     {
6264       if (mips_opts.mips16
6265           && mips_opts.noreorder
6266           && delayed_branch_p (&history[0]))
6267         as_warn (_("extended instruction in delay slot"));
6268
6269       if (mips_relax.sequence)
6270         {
6271           /* If we've reached the end of this frag, turn it into a variant
6272              frag and record the information for the instructions we've
6273              written so far.  */
6274           if (frag_room () < 4)
6275             relax_close_frag ();
6276           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
6277         }
6278
6279       if (mips_relax.sequence != 2)
6280         {
6281           if (mips_macro_warning.first_insn_sizes[0] == 0)
6282             mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
6283           mips_macro_warning.sizes[0] += insn_length (ip);
6284           mips_macro_warning.insns[0]++;
6285         }
6286       if (mips_relax.sequence != 1)
6287         {
6288           if (mips_macro_warning.first_insn_sizes[1] == 0)
6289             mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
6290           mips_macro_warning.sizes[1] += insn_length (ip);
6291           mips_macro_warning.insns[1]++;
6292         }
6293
6294       if (mips_opts.mips16)
6295         {
6296           ip->fixed_p = 1;
6297           ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
6298         }
6299       add_fixed_insn (ip);
6300     }
6301
6302   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
6303     {
6304       bfd_reloc_code_real_type final_type[3];
6305       reloc_howto_type *howto0;
6306       reloc_howto_type *howto;
6307       int i;
6308
6309       /* Perform any necessary conversion to microMIPS relocations
6310          and find out how many relocations there actually are.  */
6311       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
6312         final_type[i] = micromips_map_reloc (reloc_type[i]);
6313
6314       /* In a compound relocation, it is the final (outermost)
6315          operator that determines the relocated field.  */
6316       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
6317       if (!howto)
6318         abort ();
6319
6320       if (i > 1)
6321         howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
6322       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
6323                                  bfd_get_reloc_size (howto),
6324                                  address_expr,
6325                                  howto0 && howto0->pc_relative,
6326                                  final_type[0]);
6327
6328       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
6329       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
6330         *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
6331
6332       /* These relocations can have an addend that won't fit in
6333          4 octets for 64bit assembly.  */
6334       if (HAVE_64BIT_GPRS
6335           && ! howto->partial_inplace
6336           && (reloc_type[0] == BFD_RELOC_16
6337               || reloc_type[0] == BFD_RELOC_32
6338               || reloc_type[0] == BFD_RELOC_MIPS_JMP
6339               || reloc_type[0] == BFD_RELOC_GPREL16
6340               || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
6341               || reloc_type[0] == BFD_RELOC_GPREL32
6342               || reloc_type[0] == BFD_RELOC_64
6343               || reloc_type[0] == BFD_RELOC_CTOR
6344               || reloc_type[0] == BFD_RELOC_MIPS_SUB
6345               || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
6346               || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
6347               || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
6348               || reloc_type[0] == BFD_RELOC_MIPS_REL16
6349               || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
6350               || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
6351               || hi16_reloc_p (reloc_type[0])
6352               || lo16_reloc_p (reloc_type[0])))
6353         ip->fixp[0]->fx_no_overflow = 1;
6354
6355       /* These relocations can have an addend that won't fit in 2 octets.  */
6356       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
6357           || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
6358         ip->fixp[0]->fx_no_overflow = 1;
6359
6360       if (mips_relax.sequence)
6361         {
6362           if (mips_relax.first_fixup == 0)
6363             mips_relax.first_fixup = ip->fixp[0];
6364         }
6365       else if (reloc_needs_lo_p (*reloc_type))
6366         {
6367           struct mips_hi_fixup *hi_fixup;
6368
6369           /* Reuse the last entry if it already has a matching %lo.  */
6370           hi_fixup = mips_hi_fixup_list;
6371           if (hi_fixup == 0
6372               || !fixup_has_matching_lo_p (hi_fixup->fixp))
6373             {
6374               hi_fixup = ((struct mips_hi_fixup *)
6375                           xmalloc (sizeof (struct mips_hi_fixup)));
6376               hi_fixup->next = mips_hi_fixup_list;
6377               mips_hi_fixup_list = hi_fixup;
6378             }
6379           hi_fixup->fixp = ip->fixp[0];
6380           hi_fixup->seg = now_seg;
6381         }
6382
6383       /* Add fixups for the second and third relocations, if given.
6384          Note that the ABI allows the second relocation to be
6385          against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
6386          moment we only use RSS_UNDEF, but we could add support
6387          for the others if it ever becomes necessary.  */
6388       for (i = 1; i < 3; i++)
6389         if (reloc_type[i] != BFD_RELOC_UNUSED)
6390           {
6391             ip->fixp[i] = fix_new (ip->frag, ip->where,
6392                                    ip->fixp[0]->fx_size, NULL, 0,
6393                                    FALSE, final_type[i]);
6394
6395             /* Use fx_tcbit to mark compound relocs.  */
6396             ip->fixp[0]->fx_tcbit = 1;
6397             ip->fixp[i]->fx_tcbit = 1;
6398           }
6399     }
6400   install_insn (ip);
6401
6402   /* Update the register mask information.  */
6403   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
6404   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
6405
6406   switch (method)
6407     {
6408     case APPEND_ADD:
6409       insert_into_history (0, 1, ip);
6410       break;
6411
6412     case APPEND_ADD_WITH_NOP:
6413       {
6414         struct mips_cl_insn *nop;
6415
6416         insert_into_history (0, 1, ip);
6417         nop = get_delay_slot_nop (ip);
6418         add_fixed_insn (nop);
6419         insert_into_history (0, 1, nop);
6420         if (mips_relax.sequence)
6421           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
6422       }
6423       break;
6424
6425     case APPEND_ADD_COMPACT:
6426       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
6427       gas_assert (mips_opts.mips16);
6428       ip->insn_opcode |= 0x0080;
6429       find_altered_mips16_opcode (ip);
6430       install_insn (ip);
6431       insert_into_history (0, 1, ip);
6432       break;
6433
6434     case APPEND_SWAP:
6435       {
6436         struct mips_cl_insn delay = history[0];
6437         if (mips_opts.mips16)
6438           {
6439             know (delay.frag == ip->frag);
6440             move_insn (ip, delay.frag, delay.where);
6441             move_insn (&delay, ip->frag, ip->where + insn_length (ip));
6442           }
6443         else if (relaxed_branch || delay.frag != ip->frag)
6444           {
6445             /* Add the delay slot instruction to the end of the
6446                current frag and shrink the fixed part of the
6447                original frag.  If the branch occupies the tail of
6448                the latter, move it backwards to cover the gap.  */
6449             delay.frag->fr_fix -= branch_disp;
6450             if (delay.frag == ip->frag)
6451               move_insn (ip, ip->frag, ip->where - branch_disp);
6452             add_fixed_insn (&delay);
6453           }
6454         else
6455           {
6456             move_insn (&delay, ip->frag,
6457                        ip->where - branch_disp + insn_length (ip));
6458             move_insn (ip, history[0].frag, history[0].where);
6459           }
6460         history[0] = *ip;
6461         delay.fixed_p = 1;
6462         insert_into_history (0, 1, &delay);
6463       }
6464       break;
6465     }
6466
6467   /* If we have just completed an unconditional branch, clear the history.  */
6468   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
6469       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
6470     {
6471       unsigned int i;
6472
6473       mips_no_prev_insn ();
6474
6475       for (i = 0; i < ARRAY_SIZE (history); i++)
6476         history[i].cleared_p = 1;
6477     }
6478
6479   /* We need to emit a label at the end of branch-likely macros.  */
6480   if (emit_branch_likely_macro)
6481     {
6482       emit_branch_likely_macro = FALSE;
6483       micromips_add_label ();
6484     }
6485
6486   /* We just output an insn, so the next one doesn't have a label.  */
6487   mips_clear_insn_labels ();
6488 }
6489
6490 /* Forget that there was any previous instruction or label.
6491    When BRANCH is true, the branch history is also flushed.  */
6492
6493 static void
6494 mips_no_prev_insn (void)
6495 {
6496   prev_nop_frag = NULL;
6497   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
6498   mips_clear_insn_labels ();
6499 }
6500
6501 /* This function must be called before we emit something other than
6502    instructions.  It is like mips_no_prev_insn except that it inserts
6503    any NOPS that might be needed by previous instructions.  */
6504
6505 void
6506 mips_emit_delays (void)
6507 {
6508   if (! mips_opts.noreorder)
6509     {
6510       int nops = nops_for_insn (0, history, NULL);
6511       if (nops > 0)
6512         {
6513           while (nops-- > 0)
6514             add_fixed_insn (NOP_INSN);
6515           mips_move_text_labels ();
6516         }
6517     }
6518   mips_no_prev_insn ();
6519 }
6520
6521 /* Start a (possibly nested) noreorder block.  */
6522
6523 static void
6524 start_noreorder (void)
6525 {
6526   if (mips_opts.noreorder == 0)
6527     {
6528       unsigned int i;
6529       int nops;
6530
6531       /* None of the instructions before the .set noreorder can be moved.  */
6532       for (i = 0; i < ARRAY_SIZE (history); i++)
6533         history[i].fixed_p = 1;
6534
6535       /* Insert any nops that might be needed between the .set noreorder
6536          block and the previous instructions.  We will later remove any
6537          nops that turn out not to be needed.  */
6538       nops = nops_for_insn (0, history, NULL);
6539       if (nops > 0)
6540         {
6541           if (mips_optimize != 0)
6542             {
6543               /* Record the frag which holds the nop instructions, so
6544                  that we can remove them if we don't need them.  */
6545               frag_grow (nops * NOP_INSN_SIZE);
6546               prev_nop_frag = frag_now;
6547               prev_nop_frag_holds = nops;
6548               prev_nop_frag_required = 0;
6549               prev_nop_frag_since = 0;
6550             }
6551
6552           for (; nops > 0; --nops)
6553             add_fixed_insn (NOP_INSN);
6554
6555           /* Move on to a new frag, so that it is safe to simply
6556              decrease the size of prev_nop_frag.  */
6557           frag_wane (frag_now);
6558           frag_new (0);
6559           mips_move_text_labels ();
6560         }
6561       mips_mark_labels ();
6562       mips_clear_insn_labels ();
6563     }
6564   mips_opts.noreorder++;
6565   mips_any_noreorder = 1;
6566 }
6567
6568 /* End a nested noreorder block.  */
6569
6570 static void
6571 end_noreorder (void)
6572 {
6573   mips_opts.noreorder--;
6574   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
6575     {
6576       /* Commit to inserting prev_nop_frag_required nops and go back to
6577          handling nop insertion the .set reorder way.  */
6578       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
6579                                 * NOP_INSN_SIZE);
6580       insert_into_history (prev_nop_frag_since,
6581                            prev_nop_frag_required, NOP_INSN);
6582       prev_nop_frag = NULL;
6583     }
6584 }
6585
6586 /* Set up global variables for the start of a new macro.  */
6587
6588 static void
6589 macro_start (void)
6590 {
6591   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
6592   memset (&mips_macro_warning.first_insn_sizes, 0,
6593           sizeof (mips_macro_warning.first_insn_sizes));
6594   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
6595   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
6596                                      && delayed_branch_p (&history[0]));
6597   switch (history[0].insn_mo->pinfo2
6598           & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
6599     {
6600     case INSN2_BRANCH_DELAY_32BIT:
6601       mips_macro_warning.delay_slot_length = 4;
6602       break;
6603     case INSN2_BRANCH_DELAY_16BIT:
6604       mips_macro_warning.delay_slot_length = 2;
6605       break;
6606     default:
6607       mips_macro_warning.delay_slot_length = 0;
6608       break;
6609     }
6610   mips_macro_warning.first_frag = NULL;
6611 }
6612
6613 /* Given that a macro is longer than one instruction or of the wrong size,
6614    return the appropriate warning for it.  Return null if no warning is
6615    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
6616    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
6617    and RELAX_NOMACRO.  */
6618
6619 static const char *
6620 macro_warning (relax_substateT subtype)
6621 {
6622   if (subtype & RELAX_DELAY_SLOT)
6623     return _("Macro instruction expanded into multiple instructions"
6624              " in a branch delay slot");
6625   else if (subtype & RELAX_NOMACRO)
6626     return _("Macro instruction expanded into multiple instructions");
6627   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
6628                       | RELAX_DELAY_SLOT_SIZE_SECOND))
6629     return ((subtype & RELAX_DELAY_SLOT_16BIT)
6630             ? _("Macro instruction expanded into a wrong size instruction"
6631                 " in a 16-bit branch delay slot")
6632             : _("Macro instruction expanded into a wrong size instruction"
6633                 " in a 32-bit branch delay slot"));
6634   else
6635     return 0;
6636 }
6637
6638 /* Finish up a macro.  Emit warnings as appropriate.  */
6639
6640 static void
6641 macro_end (void)
6642 {
6643   /* Relaxation warning flags.  */
6644   relax_substateT subtype = 0;
6645
6646   /* Check delay slot size requirements.  */
6647   if (mips_macro_warning.delay_slot_length == 2)
6648     subtype |= RELAX_DELAY_SLOT_16BIT;
6649   if (mips_macro_warning.delay_slot_length != 0)
6650     {
6651       if (mips_macro_warning.delay_slot_length
6652           != mips_macro_warning.first_insn_sizes[0])
6653         subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
6654       if (mips_macro_warning.delay_slot_length
6655           != mips_macro_warning.first_insn_sizes[1])
6656         subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
6657     }
6658
6659   /* Check instruction count requirements.  */
6660   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
6661     {
6662       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
6663         subtype |= RELAX_SECOND_LONGER;
6664       if (mips_opts.warn_about_macros)
6665         subtype |= RELAX_NOMACRO;
6666       if (mips_macro_warning.delay_slot_p)
6667         subtype |= RELAX_DELAY_SLOT;
6668     }
6669
6670   /* If both alternatives fail to fill a delay slot correctly,
6671      emit the warning now.  */
6672   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
6673       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
6674     {
6675       relax_substateT s;
6676       const char *msg;
6677
6678       s = subtype & (RELAX_DELAY_SLOT_16BIT
6679                      | RELAX_DELAY_SLOT_SIZE_FIRST
6680                      | RELAX_DELAY_SLOT_SIZE_SECOND);
6681       msg = macro_warning (s);
6682       if (msg != NULL)
6683         as_warn ("%s", msg);
6684       subtype &= ~s;
6685     }
6686
6687   /* If both implementations are longer than 1 instruction, then emit the
6688      warning now.  */
6689   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
6690     {
6691       relax_substateT s;
6692       const char *msg;
6693
6694       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
6695       msg = macro_warning (s);
6696       if (msg != NULL)
6697         as_warn ("%s", msg);
6698       subtype &= ~s;
6699     }
6700
6701   /* If any flags still set, then one implementation might need a warning
6702      and the other either will need one of a different kind or none at all.
6703      Pass any remaining flags over to relaxation.  */
6704   if (mips_macro_warning.first_frag != NULL)
6705     mips_macro_warning.first_frag->fr_subtype |= subtype;
6706 }
6707
6708 /* Instruction operand formats used in macros that vary between
6709    standard MIPS and microMIPS code.  */
6710
6711 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
6712 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
6713 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
6714 static const char * const lui_fmt[2] = { "t,u", "s,u" };
6715 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
6716 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
6717 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
6718 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
6719
6720 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
6721 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
6722 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
6723 #define LUI_FMT (lui_fmt[mips_opts.micromips])
6724 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
6725 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
6726 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
6727 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
6728
6729 /* Read a macro's relocation codes from *ARGS and store them in *R.
6730    The first argument in *ARGS will be either the code for a single
6731    relocation or -1 followed by the three codes that make up a
6732    composite relocation.  */
6733
6734 static void
6735 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
6736 {
6737   int i, next;
6738
6739   next = va_arg (*args, int);
6740   if (next >= 0)
6741     r[0] = (bfd_reloc_code_real_type) next;
6742   else
6743     {
6744       for (i = 0; i < 3; i++)
6745         r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
6746       /* This function is only used for 16-bit relocation fields.
6747          To make the macro code simpler, treat an unrelocated value
6748          in the same way as BFD_RELOC_LO16.  */
6749       if (r[0] == BFD_RELOC_UNUSED)
6750         r[0] = BFD_RELOC_LO16;
6751     }
6752 }
6753
6754 /* Build an instruction created by a macro expansion.  This is passed
6755    a pointer to the count of instructions created so far, an
6756    expression, the name of the instruction to build, an operand format
6757    string, and corresponding arguments.  */
6758
6759 static void
6760 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
6761 {
6762   const struct mips_opcode *mo = NULL;
6763   bfd_reloc_code_real_type r[3];
6764   const struct mips_opcode *amo;
6765   const struct mips_operand *operand;
6766   struct hash_control *hash;
6767   struct mips_cl_insn insn;
6768   va_list args;
6769   unsigned int uval;
6770
6771   va_start (args, fmt);
6772
6773   if (mips_opts.mips16)
6774     {
6775       mips16_macro_build (ep, name, fmt, &args);
6776       va_end (args);
6777       return;
6778     }
6779
6780   r[0] = BFD_RELOC_UNUSED;
6781   r[1] = BFD_RELOC_UNUSED;
6782   r[2] = BFD_RELOC_UNUSED;
6783   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
6784   amo = (struct mips_opcode *) hash_find (hash, name);
6785   gas_assert (amo);
6786   gas_assert (strcmp (name, amo->name) == 0);
6787
6788   do
6789     {
6790       /* Search until we get a match for NAME.  It is assumed here that
6791          macros will never generate MDMX, MIPS-3D, or MT instructions.
6792          We try to match an instruction that fulfils the branch delay
6793          slot instruction length requirement (if any) of the previous
6794          instruction.  While doing this we record the first instruction
6795          seen that matches all the other conditions and use it anyway
6796          if the requirement cannot be met; we will issue an appropriate
6797          warning later on.  */
6798       if (strcmp (fmt, amo->args) == 0
6799           && amo->pinfo != INSN_MACRO
6800           && is_opcode_valid (amo)
6801           && is_size_valid (amo))
6802         {
6803           if (is_delay_slot_valid (amo))
6804             {
6805               mo = amo;
6806               break;
6807             }
6808           else if (!mo)
6809             mo = amo;
6810         }
6811
6812       ++amo;
6813       gas_assert (amo->name);
6814     }
6815   while (strcmp (name, amo->name) == 0);
6816
6817   gas_assert (mo);
6818   create_insn (&insn, mo);
6819   for (; *fmt; ++fmt)
6820     {
6821       switch (*fmt)
6822         {
6823         case ',':
6824         case '(':
6825         case ')':
6826         case 'z':
6827           break;
6828
6829         case 'i':
6830         case 'j':
6831           macro_read_relocs (&args, r);
6832           gas_assert (*r == BFD_RELOC_GPREL16
6833                       || *r == BFD_RELOC_MIPS_HIGHER
6834                       || *r == BFD_RELOC_HI16_S
6835                       || *r == BFD_RELOC_LO16
6836                       || *r == BFD_RELOC_MIPS_GOT_OFST);
6837           break;
6838
6839         case 'o':
6840           macro_read_relocs (&args, r);
6841           break;
6842
6843         case 'u':
6844           macro_read_relocs (&args, r);
6845           gas_assert (ep != NULL
6846                       && (ep->X_op == O_constant
6847                           || (ep->X_op == O_symbol
6848                               && (*r == BFD_RELOC_MIPS_HIGHEST
6849                                   || *r == BFD_RELOC_HI16_S
6850                                   || *r == BFD_RELOC_HI16
6851                                   || *r == BFD_RELOC_GPREL16
6852                                   || *r == BFD_RELOC_MIPS_GOT_HI16
6853                                   || *r == BFD_RELOC_MIPS_CALL_HI16))));
6854           break;
6855
6856         case 'p':
6857           gas_assert (ep != NULL);
6858
6859           /*
6860            * This allows macro() to pass an immediate expression for
6861            * creating short branches without creating a symbol.
6862            *
6863            * We don't allow branch relaxation for these branches, as
6864            * they should only appear in ".set nomacro" anyway.
6865            */
6866           if (ep->X_op == O_constant)
6867             {
6868               /* For microMIPS we always use relocations for branches.
6869                  So we should not resolve immediate values.  */
6870               gas_assert (!mips_opts.micromips);
6871
6872               if ((ep->X_add_number & 3) != 0)
6873                 as_bad (_("branch to misaligned address (0x%lx)"),
6874                         (unsigned long) ep->X_add_number);
6875               if ((ep->X_add_number + 0x20000) & ~0x3ffff)
6876                 as_bad (_("branch address range overflow (0x%lx)"),
6877                         (unsigned long) ep->X_add_number);
6878               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
6879               ep = NULL;
6880             }
6881           else
6882             *r = BFD_RELOC_16_PCREL_S2;
6883           break;
6884
6885         case 'a':
6886           gas_assert (ep != NULL);
6887           *r = BFD_RELOC_MIPS_JMP;
6888           break;
6889
6890         default:
6891           operand = (mips_opts.micromips
6892                      ? decode_micromips_operand (fmt)
6893                      : decode_mips_operand (fmt));
6894           if (!operand)
6895             abort ();
6896
6897           uval = va_arg (args, int);
6898           if (operand->type == OP_CLO_CLZ_DEST)
6899             uval |= (uval << 5);
6900           insn_insert_operand (&insn, operand, uval);
6901
6902           if (*fmt == '+' || *fmt == 'm')
6903             ++fmt;
6904           break;
6905         }
6906     }
6907   va_end (args);
6908   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
6909
6910   append_insn (&insn, ep, r, TRUE);
6911 }
6912
6913 static void
6914 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
6915                     va_list *args)
6916 {
6917   struct mips_opcode *mo;
6918   struct mips_cl_insn insn;
6919   const struct mips_operand *operand;
6920   bfd_reloc_code_real_type r[3]
6921     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
6922
6923   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
6924   gas_assert (mo);
6925   gas_assert (strcmp (name, mo->name) == 0);
6926
6927   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
6928     {
6929       ++mo;
6930       gas_assert (mo->name);
6931       gas_assert (strcmp (name, mo->name) == 0);
6932     }
6933
6934   create_insn (&insn, mo);
6935   for (; *fmt; ++fmt)
6936     {
6937       int c;
6938
6939       c = *fmt;
6940       switch (c)
6941         {
6942         case ',':
6943         case '(':
6944         case ')':
6945           break;
6946
6947         case '0':
6948         case 'S':
6949         case 'P':
6950         case 'R':
6951           break;
6952
6953         case '<':
6954         case '>':
6955         case '4':
6956         case '5':
6957         case 'H':
6958         case 'W':
6959         case 'D':
6960         case 'j':
6961         case '8':
6962         case 'V':
6963         case 'C':
6964         case 'U':
6965         case 'k':
6966         case 'K':
6967         case 'p':
6968         case 'q':
6969           {
6970             offsetT value;
6971
6972             gas_assert (ep != NULL);
6973
6974             if (ep->X_op != O_constant)
6975               *r = (int) BFD_RELOC_UNUSED + c;
6976             else if (calculate_reloc (*r, ep->X_add_number, &value))
6977               {
6978                 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
6979                 ep = NULL;
6980                 *r = BFD_RELOC_UNUSED;
6981               }
6982           }
6983           break;
6984
6985         default:
6986           operand = decode_mips16_operand (c, FALSE);
6987           if (!operand)
6988             abort ();
6989
6990           insn_insert_operand (&insn, operand, va_arg (*args, int));
6991           break;
6992         }
6993     }
6994
6995   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
6996
6997   append_insn (&insn, ep, r, TRUE);
6998 }
6999
7000 /*
7001  * Sign-extend 32-bit mode constants that have bit 31 set and all
7002  * higher bits unset.
7003  */
7004 static void
7005 normalize_constant_expr (expressionS *ex)
7006 {
7007   if (ex->X_op == O_constant
7008       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7009     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7010                         - 0x80000000);
7011 }
7012
7013 /*
7014  * Sign-extend 32-bit mode address offsets that have bit 31 set and
7015  * all higher bits unset.
7016  */
7017 static void
7018 normalize_address_expr (expressionS *ex)
7019 {
7020   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7021         || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7022       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7023     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7024                         - 0x80000000);
7025 }
7026
7027 /*
7028  * Generate a "jalr" instruction with a relocation hint to the called
7029  * function.  This occurs in NewABI PIC code.
7030  */
7031 static void
7032 macro_build_jalr (expressionS *ep, int cprestore)
7033 {
7034   static const bfd_reloc_code_real_type jalr_relocs[2]
7035     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
7036   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
7037   const char *jalr;
7038   char *f = NULL;
7039
7040   if (MIPS_JALR_HINT_P (ep))
7041     {
7042       frag_grow (8);
7043       f = frag_more (0);
7044     }
7045   if (mips_opts.micromips)
7046     {
7047       jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
7048               ? "jalr" : "jalrs");
7049       if (MIPS_JALR_HINT_P (ep)
7050           || mips_opts.insn32
7051           || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7052         macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
7053       else
7054         macro_build (NULL, jalr, "mj", PIC_CALL_REG);
7055     }
7056   else
7057     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
7058   if (MIPS_JALR_HINT_P (ep))
7059     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
7060 }
7061
7062 /*
7063  * Generate a "lui" instruction.
7064  */
7065 static void
7066 macro_build_lui (expressionS *ep, int regnum)
7067 {
7068   gas_assert (! mips_opts.mips16);
7069
7070   if (ep->X_op != O_constant)
7071     {
7072       gas_assert (ep->X_op == O_symbol);
7073       /* _gp_disp is a special case, used from s_cpload.
7074          __gnu_local_gp is used if mips_no_shared.  */
7075       gas_assert (mips_pic == NO_PIC
7076               || (! HAVE_NEWABI
7077                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
7078               || (! mips_in_shared
7079                   && strcmp (S_GET_NAME (ep->X_add_symbol),
7080                              "__gnu_local_gp") == 0));
7081     }
7082
7083   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
7084 }
7085
7086 /* Generate a sequence of instructions to do a load or store from a constant
7087    offset off of a base register (breg) into/from a target register (treg),
7088    using AT if necessary.  */
7089 static void
7090 macro_build_ldst_constoffset (expressionS *ep, const char *op,
7091                               int treg, int breg, int dbl)
7092 {
7093   gas_assert (ep->X_op == O_constant);
7094
7095   /* Sign-extending 32-bit constants makes their handling easier.  */
7096   if (!dbl)
7097     normalize_constant_expr (ep);
7098
7099   /* Right now, this routine can only handle signed 32-bit constants.  */
7100   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
7101     as_warn (_("operand overflow"));
7102
7103   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
7104     {
7105       /* Signed 16-bit offset will fit in the op.  Easy!  */
7106       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7107     }
7108   else
7109     {
7110       /* 32-bit offset, need multiple instructions and AT, like:
7111            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
7112            addu     $tempreg,$tempreg,$breg
7113            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
7114          to handle the complete offset.  */
7115       macro_build_lui (ep, AT);
7116       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7117       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7118
7119       if (!mips_opts.at)
7120         as_bad (_("Macro used $at after \".set noat\""));
7121     }
7122 }
7123
7124 /*                      set_at()
7125  * Generates code to set the $at register to true (one)
7126  * if reg is less than the immediate expression.
7127  */
7128 static void
7129 set_at (int reg, int unsignedp)
7130 {
7131   if (imm_expr.X_op == O_constant
7132       && imm_expr.X_add_number >= -0x8000
7133       && imm_expr.X_add_number < 0x8000)
7134     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
7135                  AT, reg, BFD_RELOC_LO16);
7136   else
7137     {
7138       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7139       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
7140     }
7141 }
7142
7143 /* Count the leading zeroes by performing a binary chop. This is a
7144    bulky bit of source, but performance is a LOT better for the
7145    majority of values than a simple loop to count the bits:
7146        for (lcnt = 0; (lcnt < 32); lcnt++)
7147          if ((v) & (1 << (31 - lcnt)))
7148            break;
7149   However it is not code size friendly, and the gain will drop a bit
7150   on certain cached systems.
7151 */
7152 #define COUNT_TOP_ZEROES(v)             \
7153   (((v) & ~0xffff) == 0                 \
7154    ? ((v) & ~0xff) == 0                 \
7155      ? ((v) & ~0xf) == 0                \
7156        ? ((v) & ~0x3) == 0              \
7157          ? ((v) & ~0x1) == 0            \
7158            ? !(v)                       \
7159              ? 32                       \
7160              : 31                       \
7161            : 30                         \
7162          : ((v) & ~0x7) == 0            \
7163            ? 29                         \
7164            : 28                         \
7165        : ((v) & ~0x3f) == 0             \
7166          ? ((v) & ~0x1f) == 0           \
7167            ? 27                         \
7168            : 26                         \
7169          : ((v) & ~0x7f) == 0           \
7170            ? 25                         \
7171            : 24                         \
7172      : ((v) & ~0xfff) == 0              \
7173        ? ((v) & ~0x3ff) == 0            \
7174          ? ((v) & ~0x1ff) == 0          \
7175            ? 23                         \
7176            : 22                         \
7177          : ((v) & ~0x7ff) == 0          \
7178            ? 21                         \
7179            : 20                         \
7180        : ((v) & ~0x3fff) == 0           \
7181          ? ((v) & ~0x1fff) == 0         \
7182            ? 19                         \
7183            : 18                         \
7184          : ((v) & ~0x7fff) == 0         \
7185            ? 17                         \
7186            : 16                         \
7187    : ((v) & ~0xffffff) == 0             \
7188      ? ((v) & ~0xfffff) == 0            \
7189        ? ((v) & ~0x3ffff) == 0          \
7190          ? ((v) & ~0x1ffff) == 0        \
7191            ? 15                         \
7192            : 14                         \
7193          : ((v) & ~0x7ffff) == 0        \
7194            ? 13                         \
7195            : 12                         \
7196        : ((v) & ~0x3fffff) == 0         \
7197          ? ((v) & ~0x1fffff) == 0       \
7198            ? 11                         \
7199            : 10                         \
7200          : ((v) & ~0x7fffff) == 0       \
7201            ? 9                          \
7202            : 8                          \
7203      : ((v) & ~0xfffffff) == 0          \
7204        ? ((v) & ~0x3ffffff) == 0        \
7205          ? ((v) & ~0x1ffffff) == 0      \
7206            ? 7                          \
7207            : 6                          \
7208          : ((v) & ~0x7ffffff) == 0      \
7209            ? 5                          \
7210            : 4                          \
7211        : ((v) & ~0x3fffffff) == 0       \
7212          ? ((v) & ~0x1fffffff) == 0     \
7213            ? 3                          \
7214            : 2                          \
7215          : ((v) & ~0x7fffffff) == 0     \
7216            ? 1                          \
7217            : 0)
7218
7219 /*                      load_register()
7220  *  This routine generates the least number of instructions necessary to load
7221  *  an absolute expression value into a register.
7222  */
7223 static void
7224 load_register (int reg, expressionS *ep, int dbl)
7225 {
7226   int freg;
7227   expressionS hi32, lo32;
7228
7229   if (ep->X_op != O_big)
7230     {
7231       gas_assert (ep->X_op == O_constant);
7232
7233       /* Sign-extending 32-bit constants makes their handling easier.  */
7234       if (!dbl)
7235         normalize_constant_expr (ep);
7236
7237       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
7238         {
7239           /* We can handle 16 bit signed values with an addiu to
7240              $zero.  No need to ever use daddiu here, since $zero and
7241              the result are always correct in 32 bit mode.  */
7242           macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
7243           return;
7244         }
7245       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
7246         {
7247           /* We can handle 16 bit unsigned values with an ori to
7248              $zero.  */
7249           macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
7250           return;
7251         }
7252       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
7253         {
7254           /* 32 bit values require an lui.  */
7255           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
7256           if ((ep->X_add_number & 0xffff) != 0)
7257             macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
7258           return;
7259         }
7260     }
7261
7262   /* The value is larger than 32 bits.  */
7263
7264   if (!dbl || HAVE_32BIT_GPRS)
7265     {
7266       char value[32];
7267
7268       sprintf_vma (value, ep->X_add_number);
7269       as_bad (_("Number (0x%s) larger than 32 bits"), value);
7270       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
7271       return;
7272     }
7273
7274   if (ep->X_op != O_big)
7275     {
7276       hi32 = *ep;
7277       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
7278       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
7279       hi32.X_add_number &= 0xffffffff;
7280       lo32 = *ep;
7281       lo32.X_add_number &= 0xffffffff;
7282     }
7283   else
7284     {
7285       gas_assert (ep->X_add_number > 2);
7286       if (ep->X_add_number == 3)
7287         generic_bignum[3] = 0;
7288       else if (ep->X_add_number > 4)
7289         as_bad (_("Number larger than 64 bits"));
7290       lo32.X_op = O_constant;
7291       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
7292       hi32.X_op = O_constant;
7293       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
7294     }
7295
7296   if (hi32.X_add_number == 0)
7297     freg = 0;
7298   else
7299     {
7300       int shift, bit;
7301       unsigned long hi, lo;
7302
7303       if (hi32.X_add_number == (offsetT) 0xffffffff)
7304         {
7305           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
7306             {
7307               macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
7308               return;
7309             }
7310           if (lo32.X_add_number & 0x80000000)
7311             {
7312               macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
7313               if (lo32.X_add_number & 0xffff)
7314                 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
7315               return;
7316             }
7317         }
7318
7319       /* Check for 16bit shifted constant.  We know that hi32 is
7320          non-zero, so start the mask on the first bit of the hi32
7321          value.  */
7322       shift = 17;
7323       do
7324         {
7325           unsigned long himask, lomask;
7326
7327           if (shift < 32)
7328             {
7329               himask = 0xffff >> (32 - shift);
7330               lomask = (0xffff << shift) & 0xffffffff;
7331             }
7332           else
7333             {
7334               himask = 0xffff << (shift - 32);
7335               lomask = 0;
7336             }
7337           if ((hi32.X_add_number & ~(offsetT) himask) == 0
7338               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
7339             {
7340               expressionS tmp;
7341
7342               tmp.X_op = O_constant;
7343               if (shift < 32)
7344                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
7345                                     | (lo32.X_add_number >> shift));
7346               else
7347                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
7348               macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
7349               macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
7350                            reg, reg, (shift >= 32) ? shift - 32 : shift);
7351               return;
7352             }
7353           ++shift;
7354         }
7355       while (shift <= (64 - 16));
7356
7357       /* Find the bit number of the lowest one bit, and store the
7358          shifted value in hi/lo.  */
7359       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
7360       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
7361       if (lo != 0)
7362         {
7363           bit = 0;
7364           while ((lo & 1) == 0)
7365             {
7366               lo >>= 1;
7367               ++bit;
7368             }
7369           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
7370           hi >>= bit;
7371         }
7372       else
7373         {
7374           bit = 32;
7375           while ((hi & 1) == 0)
7376             {
7377               hi >>= 1;
7378               ++bit;
7379             }
7380           lo = hi;
7381           hi = 0;
7382         }
7383
7384       /* Optimize if the shifted value is a (power of 2) - 1.  */
7385       if ((hi == 0 && ((lo + 1) & lo) == 0)
7386           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
7387         {
7388           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
7389           if (shift != 0)
7390             {
7391               expressionS tmp;
7392
7393               /* This instruction will set the register to be all
7394                  ones.  */
7395               tmp.X_op = O_constant;
7396               tmp.X_add_number = (offsetT) -1;
7397               macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
7398               if (bit != 0)
7399                 {
7400                   bit += shift;
7401                   macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
7402                                reg, reg, (bit >= 32) ? bit - 32 : bit);
7403                 }
7404               macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
7405                            reg, reg, (shift >= 32) ? shift - 32 : shift);
7406               return;
7407             }
7408         }
7409
7410       /* Sign extend hi32 before calling load_register, because we can
7411          generally get better code when we load a sign extended value.  */
7412       if ((hi32.X_add_number & 0x80000000) != 0)
7413         hi32.X_add_number |= ~(offsetT) 0xffffffff;
7414       load_register (reg, &hi32, 0);
7415       freg = reg;
7416     }
7417   if ((lo32.X_add_number & 0xffff0000) == 0)
7418     {
7419       if (freg != 0)
7420         {
7421           macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
7422           freg = reg;
7423         }
7424     }
7425   else
7426     {
7427       expressionS mid16;
7428
7429       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
7430         {
7431           macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
7432           macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
7433           return;
7434         }
7435
7436       if (freg != 0)
7437         {
7438           macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
7439           freg = reg;
7440         }
7441       mid16 = lo32;
7442       mid16.X_add_number >>= 16;
7443       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
7444       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
7445       freg = reg;
7446     }
7447   if ((lo32.X_add_number & 0xffff) != 0)
7448     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
7449 }
7450
7451 static inline void
7452 load_delay_nop (void)
7453 {
7454   if (!gpr_interlocks)
7455     macro_build (NULL, "nop", "");
7456 }
7457
7458 /* Load an address into a register.  */
7459
7460 static void
7461 load_address (int reg, expressionS *ep, int *used_at)
7462 {
7463   if (ep->X_op != O_constant
7464       && ep->X_op != O_symbol)
7465     {
7466       as_bad (_("expression too complex"));
7467       ep->X_op = O_constant;
7468     }
7469
7470   if (ep->X_op == O_constant)
7471     {
7472       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
7473       return;
7474     }
7475
7476   if (mips_pic == NO_PIC)
7477     {
7478       /* If this is a reference to a GP relative symbol, we want
7479            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
7480          Otherwise we want
7481            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
7482            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
7483          If we have an addend, we always use the latter form.
7484
7485          With 64bit address space and a usable $at we want
7486            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
7487            lui          $at,<sym>               (BFD_RELOC_HI16_S)
7488            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
7489            daddiu       $at,<sym>               (BFD_RELOC_LO16)
7490            dsll32       $reg,0
7491            daddu        $reg,$reg,$at
7492
7493          If $at is already in use, we use a path which is suboptimal
7494          on superscalar processors.
7495            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
7496            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
7497            dsll         $reg,16
7498            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
7499            dsll         $reg,16
7500            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
7501
7502          For GP relative symbols in 64bit address space we can use
7503          the same sequence as in 32bit address space.  */
7504       if (HAVE_64BIT_SYMBOLS)
7505         {
7506           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
7507               && !nopic_need_relax (ep->X_add_symbol, 1))
7508             {
7509               relax_start (ep->X_add_symbol);
7510               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
7511                            mips_gp_register, BFD_RELOC_GPREL16);
7512               relax_switch ();
7513             }
7514
7515           if (*used_at == 0 && mips_opts.at)
7516             {
7517               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
7518               macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
7519               macro_build (ep, "daddiu", "t,r,j", reg, reg,
7520                            BFD_RELOC_MIPS_HIGHER);
7521               macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
7522               macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
7523               macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
7524               *used_at = 1;
7525             }
7526           else
7527             {
7528               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
7529               macro_build (ep, "daddiu", "t,r,j", reg, reg,
7530                            BFD_RELOC_MIPS_HIGHER);
7531               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
7532               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
7533               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
7534               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
7535             }
7536
7537           if (mips_relax.sequence)
7538             relax_end ();
7539         }
7540       else
7541         {
7542           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
7543               && !nopic_need_relax (ep->X_add_symbol, 1))
7544             {
7545               relax_start (ep->X_add_symbol);
7546               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
7547                            mips_gp_register, BFD_RELOC_GPREL16);
7548               relax_switch ();
7549             }
7550           macro_build_lui (ep, reg);
7551           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
7552                        reg, reg, BFD_RELOC_LO16);
7553           if (mips_relax.sequence)
7554             relax_end ();
7555         }
7556     }
7557   else if (!mips_big_got)
7558     {
7559       expressionS ex;
7560
7561       /* If this is a reference to an external symbol, we want
7562            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
7563          Otherwise we want
7564            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
7565            nop
7566            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
7567          If there is a constant, it must be added in after.
7568
7569          If we have NewABI, we want
7570            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7571          unless we're referencing a global symbol with a non-zero
7572          offset, in which case cst must be added separately.  */
7573       if (HAVE_NEWABI)
7574         {
7575           if (ep->X_add_number)
7576             {
7577               ex.X_add_number = ep->X_add_number;
7578               ep->X_add_number = 0;
7579               relax_start (ep->X_add_symbol);
7580               macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7581                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7582               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7583                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7584               ex.X_op = O_constant;
7585               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
7586                            reg, reg, BFD_RELOC_LO16);
7587               ep->X_add_number = ex.X_add_number;
7588               relax_switch ();
7589             }
7590           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7591                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7592           if (mips_relax.sequence)
7593             relax_end ();
7594         }
7595       else
7596         {
7597           ex.X_add_number = ep->X_add_number;
7598           ep->X_add_number = 0;
7599           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7600                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
7601           load_delay_nop ();
7602           relax_start (ep->X_add_symbol);
7603           relax_switch ();
7604           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7605                        BFD_RELOC_LO16);
7606           relax_end ();
7607
7608           if (ex.X_add_number != 0)
7609             {
7610               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7611                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7612               ex.X_op = O_constant;
7613               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
7614                            reg, reg, BFD_RELOC_LO16);
7615             }
7616         }
7617     }
7618   else if (mips_big_got)
7619     {
7620       expressionS ex;
7621
7622       /* This is the large GOT case.  If this is a reference to an
7623          external symbol, we want
7624            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
7625            addu         $reg,$reg,$gp
7626            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
7627
7628          Otherwise, for a reference to a local symbol in old ABI, we want
7629            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
7630            nop
7631            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
7632          If there is a constant, it must be added in after.
7633
7634          In the NewABI, for local symbols, with or without offsets, we want:
7635            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
7636            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
7637       */
7638       if (HAVE_NEWABI)
7639         {
7640           ex.X_add_number = ep->X_add_number;
7641           ep->X_add_number = 0;
7642           relax_start (ep->X_add_symbol);
7643           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
7644           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7645                        reg, reg, mips_gp_register);
7646           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
7647                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
7648           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7649             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7650           else if (ex.X_add_number)
7651             {
7652               ex.X_op = O_constant;
7653               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7654                            BFD_RELOC_LO16);
7655             }
7656
7657           ep->X_add_number = ex.X_add_number;
7658           relax_switch ();
7659           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7660                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7661           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7662                        BFD_RELOC_MIPS_GOT_OFST);
7663           relax_end ();
7664         }
7665       else
7666         {
7667           ex.X_add_number = ep->X_add_number;
7668           ep->X_add_number = 0;
7669           relax_start (ep->X_add_symbol);
7670           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
7671           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7672                        reg, reg, mips_gp_register);
7673           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
7674                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
7675           relax_switch ();
7676           if (reg_needs_delay (mips_gp_register))
7677             {
7678               /* We need a nop before loading from $gp.  This special
7679                  check is required because the lui which starts the main
7680                  instruction stream does not refer to $gp, and so will not
7681                  insert the nop which may be required.  */
7682               macro_build (NULL, "nop", "");
7683             }
7684           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
7685                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
7686           load_delay_nop ();
7687           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7688                        BFD_RELOC_LO16);
7689           relax_end ();
7690
7691           if (ex.X_add_number != 0)
7692             {
7693               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
7694                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7695               ex.X_op = O_constant;
7696               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
7697                            BFD_RELOC_LO16);
7698             }
7699         }
7700     }
7701   else
7702     abort ();
7703
7704   if (!mips_opts.at && *used_at == 1)
7705     as_bad (_("Macro used $at after \".set noat\""));
7706 }
7707
7708 /* Move the contents of register SOURCE into register DEST.  */
7709
7710 static void
7711 move_register (int dest, int source)
7712 {
7713   /* Prefer to use a 16-bit microMIPS instruction unless the previous
7714      instruction specifically requires a 32-bit one.  */
7715   if (mips_opts.micromips
7716       && !mips_opts.insn32
7717       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7718     macro_build (NULL, "move", "mp,mj", dest, source);
7719   else
7720     macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
7721                  dest, source, 0);
7722 }
7723
7724 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
7725    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
7726    The two alternatives are:
7727
7728    Global symbol                Local sybmol
7729    -------------                ------------
7730    lw DEST,%got(SYMBOL)         lw DEST,%got(SYMBOL + OFFSET)
7731    ...                          ...
7732    addiu DEST,DEST,OFFSET       addiu DEST,DEST,%lo(SYMBOL + OFFSET)
7733
7734    load_got_offset emits the first instruction and add_got_offset
7735    emits the second for a 16-bit offset or add_got_offset_hilo emits
7736    a sequence to add a 32-bit offset using a scratch register.  */
7737
7738 static void
7739 load_got_offset (int dest, expressionS *local)
7740 {
7741   expressionS global;
7742
7743   global = *local;
7744   global.X_add_number = 0;
7745
7746   relax_start (local->X_add_symbol);
7747   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
7748                BFD_RELOC_MIPS_GOT16, mips_gp_register);
7749   relax_switch ();
7750   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
7751                BFD_RELOC_MIPS_GOT16, mips_gp_register);
7752   relax_end ();
7753 }
7754
7755 static void
7756 add_got_offset (int dest, expressionS *local)
7757 {
7758   expressionS global;
7759
7760   global.X_op = O_constant;
7761   global.X_op_symbol = NULL;
7762   global.X_add_symbol = NULL;
7763   global.X_add_number = local->X_add_number;
7764
7765   relax_start (local->X_add_symbol);
7766   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
7767                dest, dest, BFD_RELOC_LO16);
7768   relax_switch ();
7769   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
7770   relax_end ();
7771 }
7772
7773 static void
7774 add_got_offset_hilo (int dest, expressionS *local, int tmp)
7775 {
7776   expressionS global;
7777   int hold_mips_optimize;
7778
7779   global.X_op = O_constant;
7780   global.X_op_symbol = NULL;
7781   global.X_add_symbol = NULL;
7782   global.X_add_number = local->X_add_number;
7783
7784   relax_start (local->X_add_symbol);
7785   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
7786   relax_switch ();
7787   /* Set mips_optimize around the lui instruction to avoid
7788      inserting an unnecessary nop after the lw.  */
7789   hold_mips_optimize = mips_optimize;
7790   mips_optimize = 2;
7791   macro_build_lui (&global, tmp);
7792   mips_optimize = hold_mips_optimize;
7793   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
7794   relax_end ();
7795
7796   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
7797 }
7798
7799 /* Emit a sequence of instructions to emulate a branch likely operation.
7800    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
7801    is its complementing branch with the original condition negated.
7802    CALL is set if the original branch specified the link operation.
7803    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
7804
7805    Code like this is produced in the noreorder mode:
7806
7807         BRNEG   <args>, 1f
7808          nop
7809         b       <sym>
7810          delay slot (executed only if branch taken)
7811     1:
7812
7813    or, if CALL is set:
7814
7815         BRNEG   <args>, 1f
7816          nop
7817         bal     <sym>
7818          delay slot (executed only if branch taken)
7819     1:
7820
7821    In the reorder mode the delay slot would be filled with a nop anyway,
7822    so code produced is simply:
7823
7824         BR      <args>, <sym>
7825          nop
7826
7827    This function is used when producing code for the microMIPS ASE that
7828    does not implement branch likely instructions in hardware.  */
7829
7830 static void
7831 macro_build_branch_likely (const char *br, const char *brneg,
7832                            int call, expressionS *ep, const char *fmt,
7833                            unsigned int sreg, unsigned int treg)
7834 {
7835   int noreorder = mips_opts.noreorder;
7836   expressionS expr1;
7837
7838   gas_assert (mips_opts.micromips);
7839   start_noreorder ();
7840   if (noreorder)
7841     {
7842       micromips_label_expr (&expr1);
7843       macro_build (&expr1, brneg, fmt, sreg, treg);
7844       macro_build (NULL, "nop", "");
7845       macro_build (ep, call ? "bal" : "b", "p");
7846
7847       /* Set to true so that append_insn adds a label.  */
7848       emit_branch_likely_macro = TRUE;
7849     }
7850   else
7851     {
7852       macro_build (ep, br, fmt, sreg, treg);
7853       macro_build (NULL, "nop", "");
7854     }
7855   end_noreorder ();
7856 }
7857
7858 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
7859    the condition code tested.  EP specifies the branch target.  */
7860
7861 static void
7862 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
7863 {
7864   const int call = 0;
7865   const char *brneg;
7866   const char *br;
7867
7868   switch (type)
7869     {
7870     case M_BC1FL:
7871       br = "bc1f";
7872       brneg = "bc1t";
7873       break;
7874     case M_BC1TL:
7875       br = "bc1t";
7876       brneg = "bc1f";
7877       break;
7878     case M_BC2FL:
7879       br = "bc2f";
7880       brneg = "bc2t";
7881       break;
7882     case M_BC2TL:
7883       br = "bc2t";
7884       brneg = "bc2f";
7885       break;
7886     default:
7887       abort ();
7888     }
7889   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
7890 }
7891
7892 /* Emit a two-argument branch macro specified by TYPE, using SREG as
7893    the register tested.  EP specifies the branch target.  */
7894
7895 static void
7896 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
7897 {
7898   const char *brneg = NULL;
7899   const char *br;
7900   int call = 0;
7901
7902   switch (type)
7903     {
7904     case M_BGEZ:
7905       br = "bgez";
7906       break;
7907     case M_BGEZL:
7908       br = mips_opts.micromips ? "bgez" : "bgezl";
7909       brneg = "bltz";
7910       break;
7911     case M_BGEZALL:
7912       gas_assert (mips_opts.micromips);
7913       br = mips_opts.insn32 ? "bgezal" : "bgezals";
7914       brneg = "bltz";
7915       call = 1;
7916       break;
7917     case M_BGTZ:
7918       br = "bgtz";
7919       break;
7920     case M_BGTZL:
7921       br = mips_opts.micromips ? "bgtz" : "bgtzl";
7922       brneg = "blez";
7923       break;
7924     case M_BLEZ:
7925       br = "blez";
7926       break;
7927     case M_BLEZL:
7928       br = mips_opts.micromips ? "blez" : "blezl";
7929       brneg = "bgtz";
7930       break;
7931     case M_BLTZ:
7932       br = "bltz";
7933       break;
7934     case M_BLTZL:
7935       br = mips_opts.micromips ? "bltz" : "bltzl";
7936       brneg = "bgez";
7937       break;
7938     case M_BLTZALL:
7939       gas_assert (mips_opts.micromips);
7940       br = mips_opts.insn32 ? "bltzal" : "bltzals";
7941       brneg = "bgez";
7942       call = 1;
7943       break;
7944     default:
7945       abort ();
7946     }
7947   if (mips_opts.micromips && brneg)
7948     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
7949   else
7950     macro_build (ep, br, "s,p", sreg);
7951 }
7952
7953 /* Emit a three-argument branch macro specified by TYPE, using SREG and
7954    TREG as the registers tested.  EP specifies the branch target.  */
7955
7956 static void
7957 macro_build_branch_rsrt (int type, expressionS *ep,
7958                          unsigned int sreg, unsigned int treg)
7959 {
7960   const char *brneg = NULL;
7961   const int call = 0;
7962   const char *br;
7963
7964   switch (type)
7965     {
7966     case M_BEQ:
7967     case M_BEQ_I:
7968       br = "beq";
7969       break;
7970     case M_BEQL:
7971     case M_BEQL_I:
7972       br = mips_opts.micromips ? "beq" : "beql";
7973       brneg = "bne";
7974       break;
7975     case M_BNE:
7976     case M_BNE_I:
7977       br = "bne";
7978       break;
7979     case M_BNEL:
7980     case M_BNEL_I:
7981       br = mips_opts.micromips ? "bne" : "bnel";
7982       brneg = "beq";
7983       break;
7984     default:
7985       abort ();
7986     }
7987   if (mips_opts.micromips && brneg)
7988     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
7989   else
7990     macro_build (ep, br, "s,t,p", sreg, treg);
7991 }
7992
7993 /* Return the high part that should be loaded in order to make the low
7994    part of VALUE accessible using an offset of OFFBITS bits.  */
7995
7996 static offsetT
7997 offset_high_part (offsetT value, unsigned int offbits)
7998 {
7999   offsetT bias;
8000   addressT low_mask;
8001
8002   if (offbits == 0)
8003     return value;
8004   bias = 1 << (offbits - 1);
8005   low_mask = bias * 2 - 1;
8006   return (value + bias) & ~low_mask;
8007 }
8008
8009 /* Return true if the value stored in offset_expr and offset_reloc
8010    fits into a signed offset of OFFBITS bits.  RANGE is the maximum
8011    amount that the caller wants to add without inducing overflow
8012    and ALIGN is the known alignment of the value in bytes.  */
8013
8014 static bfd_boolean
8015 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
8016 {
8017   if (offbits == 16)
8018     {
8019       /* Accept any relocation operator if overflow isn't a concern.  */
8020       if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
8021         return TRUE;
8022
8023       /* These relocations are guaranteed not to overflow in correct links.  */
8024       if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
8025           || gprel16_reloc_p (*offset_reloc))
8026         return TRUE;
8027     }
8028   if (offset_expr.X_op == O_constant
8029       && offset_high_part (offset_expr.X_add_number, offbits) == 0
8030       && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
8031     return TRUE;
8032   return FALSE;
8033 }
8034
8035 /*
8036  *                      Build macros
8037  *   This routine implements the seemingly endless macro or synthesized
8038  * instructions and addressing modes in the mips assembly language. Many
8039  * of these macros are simple and are similar to each other. These could
8040  * probably be handled by some kind of table or grammar approach instead of
8041  * this verbose method. Others are not simple macros but are more like
8042  * optimizing code generation.
8043  *   One interesting optimization is when several store macros appear
8044  * consecutively that would load AT with the upper half of the same address.
8045  * The ensuing load upper instructions are ommited. This implies some kind
8046  * of global optimization. We currently only optimize within a single macro.
8047  *   For many of the load and store macros if the address is specified as a
8048  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
8049  * first load register 'at' with zero and use it as the base register. The
8050  * mips assembler simply uses register $zero. Just one tiny optimization
8051  * we're missing.
8052  */
8053 static void
8054 macro (struct mips_cl_insn *ip, char *str)
8055 {
8056   unsigned int treg, sreg, dreg, breg;
8057   unsigned int tempreg;
8058   int mask;
8059   int used_at = 0;
8060   expressionS label_expr;
8061   expressionS expr1;
8062   expressionS *ep;
8063   const char *s;
8064   const char *s2;
8065   const char *fmt;
8066   int likely = 0;
8067   int coproc = 0;
8068   int offbits = 16;
8069   int call = 0;
8070   int jals = 0;
8071   int dbl = 0;
8072   int imm = 0;
8073   int ust = 0;
8074   int lp = 0;
8075   bfd_boolean large_offset;
8076   int off;
8077   int hold_mips_optimize;
8078   unsigned int align;
8079
8080   gas_assert (! mips_opts.mips16);
8081
8082   treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
8083   dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
8084   sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
8085   mask = ip->insn_mo->mask;
8086
8087   label_expr.X_op = O_constant;
8088   label_expr.X_op_symbol = NULL;
8089   label_expr.X_add_symbol = NULL;
8090   label_expr.X_add_number = 0;
8091
8092   expr1.X_op = O_constant;
8093   expr1.X_op_symbol = NULL;
8094   expr1.X_add_symbol = NULL;
8095   expr1.X_add_number = 1;
8096   align = 1;
8097
8098   switch (mask)
8099     {
8100     case M_DABS:
8101       dbl = 1;
8102     case M_ABS:
8103       /*    bgez    $a0,1f
8104             move    v0,$a0
8105             sub     v0,$zero,$a0
8106          1:
8107        */
8108
8109       start_noreorder ();
8110
8111       if (mips_opts.micromips)
8112         micromips_label_expr (&label_expr);
8113       else
8114         label_expr.X_add_number = 8;
8115       macro_build (&label_expr, "bgez", "s,p", sreg);
8116       if (dreg == sreg)
8117         macro_build (NULL, "nop", "");
8118       else
8119         move_register (dreg, sreg);
8120       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
8121       if (mips_opts.micromips)
8122         micromips_add_label ();
8123
8124       end_noreorder ();
8125       break;
8126
8127     case M_ADD_I:
8128       s = "addi";
8129       s2 = "add";
8130       goto do_addi;
8131     case M_ADDU_I:
8132       s = "addiu";
8133       s2 = "addu";
8134       goto do_addi;
8135     case M_DADD_I:
8136       dbl = 1;
8137       s = "daddi";
8138       s2 = "dadd";
8139       if (!mips_opts.micromips)
8140         goto do_addi;
8141       if (imm_expr.X_op == O_constant
8142           && imm_expr.X_add_number >= -0x200
8143           && imm_expr.X_add_number < 0x200)
8144         {
8145           macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
8146           break;
8147         }
8148       goto do_addi_i;
8149     case M_DADDU_I:
8150       dbl = 1;
8151       s = "daddiu";
8152       s2 = "daddu";
8153     do_addi:
8154       if (imm_expr.X_op == O_constant
8155           && imm_expr.X_add_number >= -0x8000
8156           && imm_expr.X_add_number < 0x8000)
8157         {
8158           macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8159           break;
8160         }
8161     do_addi_i:
8162       used_at = 1;
8163       load_register (AT, &imm_expr, dbl);
8164       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
8165       break;
8166
8167     case M_AND_I:
8168       s = "andi";
8169       s2 = "and";
8170       goto do_bit;
8171     case M_OR_I:
8172       s = "ori";
8173       s2 = "or";
8174       goto do_bit;
8175     case M_NOR_I:
8176       s = "";
8177       s2 = "nor";
8178       goto do_bit;
8179     case M_XOR_I:
8180       s = "xori";
8181       s2 = "xor";
8182     do_bit:
8183       if (imm_expr.X_op == O_constant
8184           && imm_expr.X_add_number >= 0
8185           && imm_expr.X_add_number < 0x10000)
8186         {
8187           if (mask != M_NOR_I)
8188             macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
8189           else
8190             {
8191               macro_build (&imm_expr, "ori", "t,r,i",
8192                            treg, sreg, BFD_RELOC_LO16);
8193               macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
8194             }
8195           break;
8196         }
8197
8198       used_at = 1;
8199       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
8200       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
8201       break;
8202
8203     case M_BALIGN:
8204       switch (imm_expr.X_add_number)
8205         {
8206         case 0:
8207           macro_build (NULL, "nop", "");
8208           break;
8209         case 2:
8210           macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
8211           break;
8212         case 1:
8213         case 3:
8214           macro_build (NULL, "balign", "t,s,2", treg, sreg,
8215                        (int) imm_expr.X_add_number);
8216           break;
8217         default:
8218           as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
8219                   (unsigned long) imm_expr.X_add_number);
8220           break;
8221         }
8222       break;
8223
8224     case M_BC1FL:
8225     case M_BC1TL:
8226     case M_BC2FL:
8227     case M_BC2TL:
8228       gas_assert (mips_opts.micromips);
8229       macro_build_branch_ccl (mask, &offset_expr,
8230                               EXTRACT_OPERAND (1, BCC, *ip));
8231       break;
8232
8233     case M_BEQ_I:
8234     case M_BEQL_I:
8235     case M_BNE_I:
8236     case M_BNEL_I:
8237       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8238         treg = 0;
8239       else
8240         {
8241           treg = AT;
8242           used_at = 1;
8243           load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
8244         }
8245       /* Fall through.  */
8246     case M_BEQL:
8247     case M_BNEL:
8248       macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
8249       break;
8250
8251     case M_BGEL:
8252       likely = 1;
8253     case M_BGE:
8254       if (treg == 0)
8255         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
8256       else if (sreg == 0)
8257         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
8258       else
8259         {
8260           used_at = 1;
8261           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
8262           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8263                                    &offset_expr, AT, ZERO);
8264         }
8265       break;
8266
8267     case M_BGEZL:
8268     case M_BGEZALL:
8269     case M_BGTZL:
8270     case M_BLEZL:
8271     case M_BLTZL:
8272     case M_BLTZALL:
8273       macro_build_branch_rs (mask, &offset_expr, sreg);
8274       break;
8275
8276     case M_BGTL_I:
8277       likely = 1;
8278     case M_BGT_I:
8279       /* Check for > max integer.  */
8280       if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
8281         {
8282         do_false:
8283           /* Result is always false.  */
8284           if (! likely)
8285             macro_build (NULL, "nop", "");
8286           else
8287             macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8288           break;
8289         }
8290       if (imm_expr.X_op != O_constant)
8291         as_bad (_("Unsupported large constant"));
8292       ++imm_expr.X_add_number;
8293       /* FALLTHROUGH */
8294     case M_BGE_I:
8295     case M_BGEL_I:
8296       if (mask == M_BGEL_I)
8297         likely = 1;
8298       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8299         {
8300           macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
8301                                  &offset_expr, sreg);
8302           break;
8303         }
8304       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8305         {
8306           macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
8307                                  &offset_expr, sreg);
8308           break;
8309         }
8310       if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
8311         {
8312         do_true:
8313           /* result is always true */
8314           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
8315           macro_build (&offset_expr, "b", "p");
8316           break;
8317         }
8318       used_at = 1;
8319       set_at (sreg, 0);
8320       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8321                                &offset_expr, AT, ZERO);
8322       break;
8323
8324     case M_BGEUL:
8325       likely = 1;
8326     case M_BGEU:
8327       if (treg == 0)
8328         goto do_true;
8329       else if (sreg == 0)
8330         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8331                                  &offset_expr, ZERO, treg);
8332       else
8333         {
8334           used_at = 1;
8335           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
8336           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8337                                    &offset_expr, AT, ZERO);
8338         }
8339       break;
8340
8341     case M_BGTUL_I:
8342       likely = 1;
8343     case M_BGTU_I:
8344       if (sreg == 0
8345           || (HAVE_32BIT_GPRS
8346               && imm_expr.X_op == O_constant
8347               && imm_expr.X_add_number == -1))
8348         goto do_false;
8349       if (imm_expr.X_op != O_constant)
8350         as_bad (_("Unsupported large constant"));
8351       ++imm_expr.X_add_number;
8352       /* FALLTHROUGH */
8353     case M_BGEU_I:
8354     case M_BGEUL_I:
8355       if (mask == M_BGEUL_I)
8356         likely = 1;
8357       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8358         goto do_true;
8359       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8360         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8361                                  &offset_expr, sreg, ZERO);
8362       else
8363         {
8364           used_at = 1;
8365           set_at (sreg, 1);
8366           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8367                                    &offset_expr, AT, ZERO);
8368         }
8369       break;
8370
8371     case M_BGTL:
8372       likely = 1;
8373     case M_BGT:
8374       if (treg == 0)
8375         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
8376       else if (sreg == 0)
8377         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
8378       else
8379         {
8380           used_at = 1;
8381           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
8382           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8383                                    &offset_expr, AT, ZERO);
8384         }
8385       break;
8386
8387     case M_BGTUL:
8388       likely = 1;
8389     case M_BGTU:
8390       if (treg == 0)
8391         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8392                                  &offset_expr, sreg, ZERO);
8393       else if (sreg == 0)
8394         goto do_false;
8395       else
8396         {
8397           used_at = 1;
8398           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
8399           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8400                                    &offset_expr, AT, ZERO);
8401         }
8402       break;
8403
8404     case M_BLEL:
8405       likely = 1;
8406     case M_BLE:
8407       if (treg == 0)
8408         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
8409       else if (sreg == 0)
8410         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
8411       else
8412         {
8413           used_at = 1;
8414           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
8415           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8416                                    &offset_expr, AT, ZERO);
8417         }
8418       break;
8419
8420     case M_BLEL_I:
8421       likely = 1;
8422     case M_BLE_I:
8423       if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
8424         goto do_true;
8425       if (imm_expr.X_op != O_constant)
8426         as_bad (_("Unsupported large constant"));
8427       ++imm_expr.X_add_number;
8428       /* FALLTHROUGH */
8429     case M_BLT_I:
8430     case M_BLTL_I:
8431       if (mask == M_BLTL_I)
8432         likely = 1;
8433       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8434         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
8435       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8436         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
8437       else
8438         {
8439           used_at = 1;
8440           set_at (sreg, 0);
8441           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8442                                    &offset_expr, AT, ZERO);
8443         }
8444       break;
8445
8446     case M_BLEUL:
8447       likely = 1;
8448     case M_BLEU:
8449       if (treg == 0)
8450         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8451                                  &offset_expr, sreg, ZERO);
8452       else if (sreg == 0)
8453         goto do_true;
8454       else
8455         {
8456           used_at = 1;
8457           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
8458           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8459                                    &offset_expr, AT, ZERO);
8460         }
8461       break;
8462
8463     case M_BLEUL_I:
8464       likely = 1;
8465     case M_BLEU_I:
8466       if (sreg == 0
8467           || (HAVE_32BIT_GPRS
8468               && imm_expr.X_op == O_constant
8469               && imm_expr.X_add_number == -1))
8470         goto do_true;
8471       if (imm_expr.X_op != O_constant)
8472         as_bad (_("Unsupported large constant"));
8473       ++imm_expr.X_add_number;
8474       /* FALLTHROUGH */
8475     case M_BLTU_I:
8476     case M_BLTUL_I:
8477       if (mask == M_BLTUL_I)
8478         likely = 1;
8479       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8480         goto do_false;
8481       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8482         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
8483                                  &offset_expr, sreg, ZERO);
8484       else
8485         {
8486           used_at = 1;
8487           set_at (sreg, 1);
8488           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8489                                    &offset_expr, AT, ZERO);
8490         }
8491       break;
8492
8493     case M_BLTL:
8494       likely = 1;
8495     case M_BLT:
8496       if (treg == 0)
8497         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
8498       else if (sreg == 0)
8499         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
8500       else
8501         {
8502           used_at = 1;
8503           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
8504           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8505                                    &offset_expr, AT, ZERO);
8506         }
8507       break;
8508
8509     case M_BLTUL:
8510       likely = 1;
8511     case M_BLTU:
8512       if (treg == 0)
8513         goto do_false;
8514       else if (sreg == 0)
8515         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8516                                  &offset_expr, ZERO, treg);
8517       else
8518         {
8519           used_at = 1;
8520           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
8521           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
8522                                    &offset_expr, AT, ZERO);
8523         }
8524       break;
8525
8526     case M_DEXT:
8527       {
8528         /* Use unsigned arithmetic.  */
8529         addressT pos;
8530         addressT size;
8531
8532         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
8533           {
8534             as_bad (_("Unsupported large constant"));
8535             pos = size = 1;
8536           }
8537         else
8538           {
8539             pos = imm_expr.X_add_number;
8540             size = imm2_expr.X_add_number;
8541           }
8542
8543         if (pos > 63)
8544           {
8545             report_bad_range (ip, 3, pos, 0, 63, FALSE);
8546             pos = 1;
8547           }
8548         if (size == 0 || size > 64 || (pos + size - 1) > 63)
8549           {
8550             report_bad_field (pos, size);
8551             size = 1;
8552           }
8553
8554         if (size <= 32 && pos < 32)
8555           {
8556             s = "dext";
8557             fmt = "t,r,+A,+C";
8558           }
8559         else if (size <= 32)
8560           {
8561             s = "dextu";
8562             fmt = "t,r,+E,+H";
8563           }
8564         else
8565           {
8566             s = "dextm";
8567             fmt = "t,r,+A,+G";
8568           }
8569         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
8570                      (int) (size - 1));
8571       }
8572       break;
8573
8574     case M_DINS:
8575       {
8576         /* Use unsigned arithmetic.  */
8577         addressT pos;
8578         addressT size;
8579
8580         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
8581           {
8582             as_bad (_("Unsupported large constant"));
8583             pos = size = 1;
8584           }
8585         else
8586           {
8587             pos = imm_expr.X_add_number;
8588             size = imm2_expr.X_add_number;
8589           }
8590
8591         if (pos > 63)
8592           {
8593             report_bad_range (ip, 3, pos, 0, 63, FALSE);
8594             pos = 1;
8595           }
8596         if (size == 0 || size > 64 || (pos + size - 1) > 63)
8597           {
8598             report_bad_field (pos, size);
8599             size = 1;
8600           }
8601
8602         if (pos < 32 && (pos + size - 1) < 32)
8603           {
8604             s = "dins";
8605             fmt = "t,r,+A,+B";
8606           }
8607         else if (pos >= 32)
8608           {
8609             s = "dinsu";
8610             fmt = "t,r,+E,+F";
8611           }
8612         else
8613           {
8614             s = "dinsm";
8615             fmt = "t,r,+A,+F";
8616           }
8617         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
8618                      (int) (pos + size - 1));
8619       }
8620       break;
8621
8622     case M_DDIV_3:
8623       dbl = 1;
8624     case M_DIV_3:
8625       s = "mflo";
8626       goto do_div3;
8627     case M_DREM_3:
8628       dbl = 1;
8629     case M_REM_3:
8630       s = "mfhi";
8631     do_div3:
8632       if (treg == 0)
8633         {
8634           as_warn (_("Divide by zero."));
8635           if (mips_trap)
8636             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
8637           else
8638             macro_build (NULL, "break", BRK_FMT, 7);
8639           break;
8640         }
8641
8642       start_noreorder ();
8643       if (mips_trap)
8644         {
8645           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
8646           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
8647         }
8648       else
8649         {
8650           if (mips_opts.micromips)
8651             micromips_label_expr (&label_expr);
8652           else
8653             label_expr.X_add_number = 8;
8654           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
8655           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
8656           macro_build (NULL, "break", BRK_FMT, 7);
8657           if (mips_opts.micromips)
8658             micromips_add_label ();
8659         }
8660       expr1.X_add_number = -1;
8661       used_at = 1;
8662       load_register (AT, &expr1, dbl);
8663       if (mips_opts.micromips)
8664         micromips_label_expr (&label_expr);
8665       else
8666         label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
8667       macro_build (&label_expr, "bne", "s,t,p", treg, AT);
8668       if (dbl)
8669         {
8670           expr1.X_add_number = 1;
8671           load_register (AT, &expr1, dbl);
8672           macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
8673         }
8674       else
8675         {
8676           expr1.X_add_number = 0x80000000;
8677           macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
8678         }
8679       if (mips_trap)
8680         {
8681           macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
8682           /* We want to close the noreorder block as soon as possible, so
8683              that later insns are available for delay slot filling.  */
8684           end_noreorder ();
8685         }
8686       else
8687         {
8688           if (mips_opts.micromips)
8689             micromips_label_expr (&label_expr);
8690           else
8691             label_expr.X_add_number = 8;
8692           macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
8693           macro_build (NULL, "nop", "");
8694
8695           /* We want to close the noreorder block as soon as possible, so
8696              that later insns are available for delay slot filling.  */
8697           end_noreorder ();
8698
8699           macro_build (NULL, "break", BRK_FMT, 6);
8700         }
8701       if (mips_opts.micromips)
8702         micromips_add_label ();
8703       macro_build (NULL, s, MFHL_FMT, dreg);
8704       break;
8705
8706     case M_DIV_3I:
8707       s = "div";
8708       s2 = "mflo";
8709       goto do_divi;
8710     case M_DIVU_3I:
8711       s = "divu";
8712       s2 = "mflo";
8713       goto do_divi;
8714     case M_REM_3I:
8715       s = "div";
8716       s2 = "mfhi";
8717       goto do_divi;
8718     case M_REMU_3I:
8719       s = "divu";
8720       s2 = "mfhi";
8721       goto do_divi;
8722     case M_DDIV_3I:
8723       dbl = 1;
8724       s = "ddiv";
8725       s2 = "mflo";
8726       goto do_divi;
8727     case M_DDIVU_3I:
8728       dbl = 1;
8729       s = "ddivu";
8730       s2 = "mflo";
8731       goto do_divi;
8732     case M_DREM_3I:
8733       dbl = 1;
8734       s = "ddiv";
8735       s2 = "mfhi";
8736       goto do_divi;
8737     case M_DREMU_3I:
8738       dbl = 1;
8739       s = "ddivu";
8740       s2 = "mfhi";
8741     do_divi:
8742       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
8743         {
8744           as_warn (_("Divide by zero."));
8745           if (mips_trap)
8746             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
8747           else
8748             macro_build (NULL, "break", BRK_FMT, 7);
8749           break;
8750         }
8751       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
8752         {
8753           if (strcmp (s2, "mflo") == 0)
8754             move_register (dreg, sreg);
8755           else
8756             move_register (dreg, ZERO);
8757           break;
8758         }
8759       if (imm_expr.X_op == O_constant
8760           && imm_expr.X_add_number == -1
8761           && s[strlen (s) - 1] != 'u')
8762         {
8763           if (strcmp (s2, "mflo") == 0)
8764             {
8765               macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
8766             }
8767           else
8768             move_register (dreg, ZERO);
8769           break;
8770         }
8771
8772       used_at = 1;
8773       load_register (AT, &imm_expr, dbl);
8774       macro_build (NULL, s, "z,s,t", sreg, AT);
8775       macro_build (NULL, s2, MFHL_FMT, dreg);
8776       break;
8777
8778     case M_DIVU_3:
8779       s = "divu";
8780       s2 = "mflo";
8781       goto do_divu3;
8782     case M_REMU_3:
8783       s = "divu";
8784       s2 = "mfhi";
8785       goto do_divu3;
8786     case M_DDIVU_3:
8787       s = "ddivu";
8788       s2 = "mflo";
8789       goto do_divu3;
8790     case M_DREMU_3:
8791       s = "ddivu";
8792       s2 = "mfhi";
8793     do_divu3:
8794       start_noreorder ();
8795       if (mips_trap)
8796         {
8797           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
8798           macro_build (NULL, s, "z,s,t", sreg, treg);
8799           /* We want to close the noreorder block as soon as possible, so
8800              that later insns are available for delay slot filling.  */
8801           end_noreorder ();
8802         }
8803       else
8804         {
8805           if (mips_opts.micromips)
8806             micromips_label_expr (&label_expr);
8807           else
8808             label_expr.X_add_number = 8;
8809           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
8810           macro_build (NULL, s, "z,s,t", sreg, treg);
8811
8812           /* We want to close the noreorder block as soon as possible, so
8813              that later insns are available for delay slot filling.  */
8814           end_noreorder ();
8815           macro_build (NULL, "break", BRK_FMT, 7);
8816           if (mips_opts.micromips)
8817             micromips_add_label ();
8818         }
8819       macro_build (NULL, s2, MFHL_FMT, dreg);
8820       break;
8821
8822     case M_DLCA_AB:
8823       dbl = 1;
8824     case M_LCA_AB:
8825       call = 1;
8826       goto do_la;
8827     case M_DLA_AB:
8828       dbl = 1;
8829     case M_LA_AB:
8830     do_la:
8831       /* Load the address of a symbol into a register.  If breg is not
8832          zero, we then add a base register to it.  */
8833
8834       if (dbl && HAVE_32BIT_GPRS)
8835         as_warn (_("dla used to load 32-bit register"));
8836
8837       if (!dbl && HAVE_64BIT_OBJECTS)
8838         as_warn (_("la used to load 64-bit address"));
8839
8840       if (small_offset_p (0, align, 16))
8841         {
8842           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", treg, breg,
8843                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8844           break;
8845         }
8846
8847       if (mips_opts.at && (treg == breg))
8848         {
8849           tempreg = AT;
8850           used_at = 1;
8851         }
8852       else
8853         {
8854           tempreg = treg;
8855         }
8856
8857       if (offset_expr.X_op != O_symbol
8858           && offset_expr.X_op != O_constant)
8859         {
8860           as_bad (_("Expression too complex"));
8861           offset_expr.X_op = O_constant;
8862         }
8863
8864       if (offset_expr.X_op == O_constant)
8865         load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
8866       else if (mips_pic == NO_PIC)
8867         {
8868           /* If this is a reference to a GP relative symbol, we want
8869                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
8870              Otherwise we want
8871                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8872                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8873              If we have a constant, we need two instructions anyhow,
8874              so we may as well always use the latter form.
8875
8876              With 64bit address space and a usable $at we want
8877                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8878                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8879                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8880                daddiu   $at,<sym>               (BFD_RELOC_LO16)
8881                dsll32   $tempreg,0
8882                daddu    $tempreg,$tempreg,$at
8883
8884              If $at is already in use, we use a path which is suboptimal
8885              on superscalar processors.
8886                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8887                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8888                dsll     $tempreg,16
8889                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8890                dsll     $tempreg,16
8891                daddiu   $tempreg,<sym>          (BFD_RELOC_LO16)
8892
8893              For GP relative symbols in 64bit address space we can use
8894              the same sequence as in 32bit address space.  */
8895           if (HAVE_64BIT_SYMBOLS)
8896             {
8897               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8898                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8899                 {
8900                   relax_start (offset_expr.X_add_symbol);
8901                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8902                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
8903                   relax_switch ();
8904                 }
8905
8906               if (used_at == 0 && mips_opts.at)
8907                 {
8908                   macro_build (&offset_expr, "lui", LUI_FMT,
8909                                tempreg, BFD_RELOC_MIPS_HIGHEST);
8910                   macro_build (&offset_expr, "lui", LUI_FMT,
8911                                AT, BFD_RELOC_HI16_S);
8912                   macro_build (&offset_expr, "daddiu", "t,r,j",
8913                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
8914                   macro_build (&offset_expr, "daddiu", "t,r,j",
8915                                AT, AT, BFD_RELOC_LO16);
8916                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8917                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8918                   used_at = 1;
8919                 }
8920               else
8921                 {
8922                   macro_build (&offset_expr, "lui", LUI_FMT,
8923                                tempreg, BFD_RELOC_MIPS_HIGHEST);
8924                   macro_build (&offset_expr, "daddiu", "t,r,j",
8925                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
8926                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8927                   macro_build (&offset_expr, "daddiu", "t,r,j",
8928                                tempreg, tempreg, BFD_RELOC_HI16_S);
8929                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8930                   macro_build (&offset_expr, "daddiu", "t,r,j",
8931                                tempreg, tempreg, BFD_RELOC_LO16);
8932                 }
8933
8934               if (mips_relax.sequence)
8935                 relax_end ();
8936             }
8937           else
8938             {
8939               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8940                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8941                 {
8942                   relax_start (offset_expr.X_add_symbol);
8943                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8944                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
8945                   relax_switch ();
8946                 }
8947               if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8948                 as_bad (_("Offset too large"));
8949               macro_build_lui (&offset_expr, tempreg);
8950               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8951                            tempreg, tempreg, BFD_RELOC_LO16);
8952               if (mips_relax.sequence)
8953                 relax_end ();
8954             }
8955         }
8956       else if (!mips_big_got && !HAVE_NEWABI)
8957         {
8958           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
8959
8960           /* If this is a reference to an external symbol, and there
8961              is no constant, we want
8962                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8963              or for lca or if tempreg is PIC_CALL_REG
8964                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
8965              For a local symbol, we want
8966                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8967                nop
8968                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8969
8970              If we have a small constant, and this is a reference to
8971              an external symbol, we want
8972                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8973                nop
8974                addiu    $tempreg,$tempreg,<constant>
8975              For a local symbol, we want the same instruction
8976              sequence, but we output a BFD_RELOC_LO16 reloc on the
8977              addiu instruction.
8978
8979              If we have a large constant, and this is a reference to
8980              an external symbol, we want
8981                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8982                lui      $at,<hiconstant>
8983                addiu    $at,$at,<loconstant>
8984                addu     $tempreg,$tempreg,$at
8985              For a local symbol, we want the same instruction
8986              sequence, but we output a BFD_RELOC_LO16 reloc on the
8987              addiu instruction.
8988            */
8989
8990           if (offset_expr.X_add_number == 0)
8991             {
8992               if (mips_pic == SVR4_PIC
8993                   && breg == 0
8994                   && (call || tempreg == PIC_CALL_REG))
8995                 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
8996
8997               relax_start (offset_expr.X_add_symbol);
8998               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8999                            lw_reloc_type, mips_gp_register);
9000               if (breg != 0)
9001                 {
9002                   /* We're going to put in an addu instruction using
9003                      tempreg, so we may as well insert the nop right
9004                      now.  */
9005                   load_delay_nop ();
9006                 }
9007               relax_switch ();
9008               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9009                            tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
9010               load_delay_nop ();
9011               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9012                            tempreg, tempreg, BFD_RELOC_LO16);
9013               relax_end ();
9014               /* FIXME: If breg == 0, and the next instruction uses
9015                  $tempreg, then if this variant case is used an extra
9016                  nop will be generated.  */
9017             }
9018           else if (offset_expr.X_add_number >= -0x8000
9019                    && offset_expr.X_add_number < 0x8000)
9020             {
9021               load_got_offset (tempreg, &offset_expr);
9022               load_delay_nop ();
9023               add_got_offset (tempreg, &offset_expr);
9024             }
9025           else
9026             {
9027               expr1.X_add_number = offset_expr.X_add_number;
9028               offset_expr.X_add_number =
9029                 SEXT_16BIT (offset_expr.X_add_number);
9030               load_got_offset (tempreg, &offset_expr);
9031               offset_expr.X_add_number = expr1.X_add_number;
9032               /* If we are going to add in a base register, and the
9033                  target register and the base register are the same,
9034                  then we are using AT as a temporary register.  Since
9035                  we want to load the constant into AT, we add our
9036                  current AT (from the global offset table) and the
9037                  register into the register now, and pretend we were
9038                  not using a base register.  */
9039               if (breg == treg)
9040                 {
9041                   load_delay_nop ();
9042                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9043                                treg, AT, breg);
9044                   breg = 0;
9045                   tempreg = treg;
9046                 }
9047               add_got_offset_hilo (tempreg, &offset_expr, AT);
9048               used_at = 1;
9049             }
9050         }
9051       else if (!mips_big_got && HAVE_NEWABI)
9052         {
9053           int add_breg_early = 0;
9054
9055           /* If this is a reference to an external, and there is no
9056              constant, or local symbol (*), with or without a
9057              constant, we want
9058                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
9059              or for lca or if tempreg is PIC_CALL_REG
9060                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
9061
9062              If we have a small constant, and this is a reference to
9063              an external symbol, we want
9064                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
9065                addiu    $tempreg,$tempreg,<constant>
9066
9067              If we have a large constant, and this is a reference to
9068              an external symbol, we want
9069                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
9070                lui      $at,<hiconstant>
9071                addiu    $at,$at,<loconstant>
9072                addu     $tempreg,$tempreg,$at
9073
9074              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
9075              local symbols, even though it introduces an additional
9076              instruction.  */
9077
9078           if (offset_expr.X_add_number)
9079             {
9080               expr1.X_add_number = offset_expr.X_add_number;
9081               offset_expr.X_add_number = 0;
9082
9083               relax_start (offset_expr.X_add_symbol);
9084               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9085                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9086
9087               if (expr1.X_add_number >= -0x8000
9088                   && expr1.X_add_number < 0x8000)
9089                 {
9090                   macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
9091                                tempreg, tempreg, BFD_RELOC_LO16);
9092                 }
9093               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
9094                 {
9095                   /* If we are going to add in a base register, and the
9096                      target register and the base register are the same,
9097                      then we are using AT as a temporary register.  Since
9098                      we want to load the constant into AT, we add our
9099                      current AT (from the global offset table) and the
9100                      register into the register now, and pretend we were
9101                      not using a base register.  */
9102                   if (breg != treg)
9103                     dreg = tempreg;
9104                   else
9105                     {
9106                       gas_assert (tempreg == AT);
9107                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9108                                    treg, AT, breg);
9109                       dreg = treg;
9110                       add_breg_early = 1;
9111                     }
9112
9113                   load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
9114                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9115                                dreg, dreg, AT);
9116
9117                   used_at = 1;
9118                 }
9119               else
9120                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9121
9122               relax_switch ();
9123               offset_expr.X_add_number = expr1.X_add_number;
9124
9125               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9126                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9127               if (add_breg_early)
9128                 {
9129                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9130                                treg, tempreg, breg);
9131                   breg = 0;
9132                   tempreg = treg;
9133                 }
9134               relax_end ();
9135             }
9136           else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
9137             {
9138               relax_start (offset_expr.X_add_symbol);
9139               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9140                            BFD_RELOC_MIPS_CALL16, mips_gp_register);
9141               relax_switch ();
9142               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9143                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9144               relax_end ();
9145             }
9146           else
9147             {
9148               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9149                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9150             }
9151         }
9152       else if (mips_big_got && !HAVE_NEWABI)
9153         {
9154           int gpdelay;
9155           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
9156           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
9157           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
9158
9159           /* This is the large GOT case.  If this is a reference to an
9160              external symbol, and there is no constant, we want
9161                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9162                addu     $tempreg,$tempreg,$gp
9163                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9164              or for lca or if tempreg is PIC_CALL_REG
9165                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
9166                addu     $tempreg,$tempreg,$gp
9167                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
9168              For a local symbol, we want
9169                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
9170                nop
9171                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9172
9173              If we have a small constant, and this is a reference to
9174              an external symbol, we want
9175                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9176                addu     $tempreg,$tempreg,$gp
9177                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9178                nop
9179                addiu    $tempreg,$tempreg,<constant>
9180              For a local symbol, we want
9181                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
9182                nop
9183                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
9184
9185              If we have a large constant, and this is a reference to
9186              an external symbol, we want
9187                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9188                addu     $tempreg,$tempreg,$gp
9189                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9190                lui      $at,<hiconstant>
9191                addiu    $at,$at,<loconstant>
9192                addu     $tempreg,$tempreg,$at
9193              For a local symbol, we want
9194                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
9195                lui      $at,<hiconstant>
9196                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
9197                addu     $tempreg,$tempreg,$at
9198           */
9199
9200           expr1.X_add_number = offset_expr.X_add_number;
9201           offset_expr.X_add_number = 0;
9202           relax_start (offset_expr.X_add_symbol);
9203           gpdelay = reg_needs_delay (mips_gp_register);
9204           if (expr1.X_add_number == 0 && breg == 0
9205               && (call || tempreg == PIC_CALL_REG))
9206             {
9207               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
9208               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
9209             }
9210           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
9211           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9212                        tempreg, tempreg, mips_gp_register);
9213           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9214                        tempreg, lw_reloc_type, tempreg);
9215           if (expr1.X_add_number == 0)
9216             {
9217               if (breg != 0)
9218                 {
9219                   /* We're going to put in an addu instruction using
9220                      tempreg, so we may as well insert the nop right
9221                      now.  */
9222                   load_delay_nop ();
9223                 }
9224             }
9225           else if (expr1.X_add_number >= -0x8000
9226                    && expr1.X_add_number < 0x8000)
9227             {
9228               load_delay_nop ();
9229               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
9230                            tempreg, tempreg, BFD_RELOC_LO16);
9231             }
9232           else
9233             {
9234               /* If we are going to add in a base register, and the
9235                  target register and the base register are the same,
9236                  then we are using AT as a temporary register.  Since
9237                  we want to load the constant into AT, we add our
9238                  current AT (from the global offset table) and the
9239                  register into the register now, and pretend we were
9240                  not using a base register.  */
9241               if (breg != treg)
9242                 dreg = tempreg;
9243               else
9244                 {
9245                   gas_assert (tempreg == AT);
9246                   load_delay_nop ();
9247                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9248                                treg, AT, breg);
9249                   dreg = treg;
9250                 }
9251
9252               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
9253               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
9254
9255               used_at = 1;
9256             }
9257           offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
9258           relax_switch ();
9259
9260           if (gpdelay)
9261             {
9262               /* This is needed because this instruction uses $gp, but
9263                  the first instruction on the main stream does not.  */
9264               macro_build (NULL, "nop", "");
9265             }
9266
9267           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9268                        local_reloc_type, mips_gp_register);
9269           if (expr1.X_add_number >= -0x8000
9270               && expr1.X_add_number < 0x8000)
9271             {
9272               load_delay_nop ();
9273               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9274                            tempreg, tempreg, BFD_RELOC_LO16);
9275               /* FIXME: If add_number is 0, and there was no base
9276                  register, the external symbol case ended with a load,
9277                  so if the symbol turns out to not be external, and
9278                  the next instruction uses tempreg, an unnecessary nop
9279                  will be inserted.  */
9280             }
9281           else
9282             {
9283               if (breg == treg)
9284                 {
9285                   /* We must add in the base register now, as in the
9286                      external symbol case.  */
9287                   gas_assert (tempreg == AT);
9288                   load_delay_nop ();
9289                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9290                                treg, AT, breg);
9291                   tempreg = treg;
9292                   /* We set breg to 0 because we have arranged to add
9293                      it in in both cases.  */
9294                   breg = 0;
9295                 }
9296
9297               macro_build_lui (&expr1, AT);
9298               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9299                            AT, AT, BFD_RELOC_LO16);
9300               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9301                            tempreg, tempreg, AT);
9302               used_at = 1;
9303             }
9304           relax_end ();
9305         }
9306       else if (mips_big_got && HAVE_NEWABI)
9307         {
9308           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
9309           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
9310           int add_breg_early = 0;
9311
9312           /* This is the large GOT case.  If this is a reference to an
9313              external symbol, and there is no constant, we want
9314                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9315                add      $tempreg,$tempreg,$gp
9316                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9317              or for lca or if tempreg is PIC_CALL_REG
9318                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
9319                add      $tempreg,$tempreg,$gp
9320                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
9321
9322              If we have a small constant, and this is a reference to
9323              an external symbol, we want
9324                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9325                add      $tempreg,$tempreg,$gp
9326                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9327                addi     $tempreg,$tempreg,<constant>
9328
9329              If we have a large constant, and this is a reference to
9330              an external symbol, we want
9331                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
9332                addu     $tempreg,$tempreg,$gp
9333                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9334                lui      $at,<hiconstant>
9335                addi     $at,$at,<loconstant>
9336                add      $tempreg,$tempreg,$at
9337
9338              If we have NewABI, and we know it's a local symbol, we want
9339                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
9340                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
9341              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
9342
9343           relax_start (offset_expr.X_add_symbol);
9344
9345           expr1.X_add_number = offset_expr.X_add_number;
9346           offset_expr.X_add_number = 0;
9347
9348           if (expr1.X_add_number == 0 && breg == 0
9349               && (call || tempreg == PIC_CALL_REG))
9350             {
9351               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
9352               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
9353             }
9354           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
9355           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9356                        tempreg, tempreg, mips_gp_register);
9357           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9358                        tempreg, lw_reloc_type, tempreg);
9359
9360           if (expr1.X_add_number == 0)
9361             ;
9362           else if (expr1.X_add_number >= -0x8000
9363                    && expr1.X_add_number < 0x8000)
9364             {
9365               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
9366                            tempreg, tempreg, BFD_RELOC_LO16);
9367             }
9368           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
9369             {
9370               /* If we are going to add in a base register, and the
9371                  target register and the base register are the same,
9372                  then we are using AT as a temporary register.  Since
9373                  we want to load the constant into AT, we add our
9374                  current AT (from the global offset table) and the
9375                  register into the register now, and pretend we were
9376                  not using a base register.  */
9377               if (breg != treg)
9378                 dreg = tempreg;
9379               else
9380                 {
9381                   gas_assert (tempreg == AT);
9382                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9383                                treg, AT, breg);
9384                   dreg = treg;
9385                   add_breg_early = 1;
9386                 }
9387
9388               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
9389               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
9390
9391               used_at = 1;
9392             }
9393           else
9394             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
9395
9396           relax_switch ();
9397           offset_expr.X_add_number = expr1.X_add_number;
9398           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9399                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9400           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9401                        tempreg, BFD_RELOC_MIPS_GOT_OFST);
9402           if (add_breg_early)
9403             {
9404               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9405                            treg, tempreg, breg);
9406               breg = 0;
9407               tempreg = treg;
9408             }
9409           relax_end ();
9410         }
9411       else
9412         abort ();
9413
9414       if (breg != 0)
9415         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
9416       break;
9417
9418     case M_MSGSND:
9419       gas_assert (!mips_opts.micromips);
9420       macro_build (NULL, "c2", "C", (treg << 16) | 0x01);
9421       break;
9422
9423     case M_MSGLD:
9424       gas_assert (!mips_opts.micromips);
9425       macro_build (NULL, "c2", "C", 0x02);
9426       break;
9427
9428     case M_MSGLD_T:
9429       gas_assert (!mips_opts.micromips);
9430       macro_build (NULL, "c2", "C", (treg << 16) | 0x02);
9431       break;
9432
9433     case M_MSGWAIT:
9434       gas_assert (!mips_opts.micromips);
9435       macro_build (NULL, "c2", "C", 3);
9436       break;
9437
9438     case M_MSGWAIT_T:
9439       gas_assert (!mips_opts.micromips);
9440       macro_build (NULL, "c2", "C", (treg << 16) | 0x03);
9441       break;
9442
9443     case M_J_A:
9444       /* The j instruction may not be used in PIC code, since it
9445          requires an absolute address.  We convert it to a b
9446          instruction.  */
9447       if (mips_pic == NO_PIC)
9448         macro_build (&offset_expr, "j", "a");
9449       else
9450         macro_build (&offset_expr, "b", "p");
9451       break;
9452
9453       /* The jal instructions must be handled as macros because when
9454          generating PIC code they expand to multi-instruction
9455          sequences.  Normally they are simple instructions.  */
9456     case M_JALS_1:
9457       dreg = RA;
9458       /* Fall through.  */
9459     case M_JALS_2:
9460       gas_assert (mips_opts.micromips);
9461       if (mips_opts.insn32)
9462         {
9463           as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
9464           break;
9465         }
9466       jals = 1;
9467       goto jal;
9468     case M_JAL_1:
9469       dreg = RA;
9470       /* Fall through.  */
9471     case M_JAL_2:
9472     jal:
9473       if (mips_pic == NO_PIC)
9474         {
9475           s = jals ? "jalrs" : "jalr";
9476           if (mips_opts.micromips
9477               && !mips_opts.insn32
9478               && dreg == RA
9479               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9480             macro_build (NULL, s, "mj", sreg);
9481           else
9482             macro_build (NULL, s, JALR_FMT, dreg, sreg);
9483         }
9484       else
9485         {
9486           int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
9487                            && mips_cprestore_offset >= 0);
9488
9489           if (sreg != PIC_CALL_REG)
9490             as_warn (_("MIPS PIC call to register other than $25"));
9491
9492           s = ((mips_opts.micromips
9493                 && !mips_opts.insn32
9494                 && (!mips_opts.noreorder || cprestore))
9495                ? "jalrs" : "jalr");
9496           if (mips_opts.micromips
9497               && !mips_opts.insn32
9498               && dreg == RA
9499               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9500             macro_build (NULL, s, "mj", sreg);
9501           else
9502             macro_build (NULL, s, JALR_FMT, dreg, sreg);
9503           if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
9504             {
9505               if (mips_cprestore_offset < 0)
9506                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9507               else
9508                 {
9509                   if (!mips_frame_reg_valid)
9510                     {
9511                       as_warn (_("No .frame pseudo-op used in PIC code"));
9512                       /* Quiet this warning.  */
9513                       mips_frame_reg_valid = 1;
9514                     }
9515                   if (!mips_cprestore_valid)
9516                     {
9517                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
9518                       /* Quiet this warning.  */
9519                       mips_cprestore_valid = 1;
9520                     }
9521                   if (mips_opts.noreorder)
9522                     macro_build (NULL, "nop", "");
9523                   expr1.X_add_number = mips_cprestore_offset;
9524                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
9525                                                 mips_gp_register,
9526                                                 mips_frame_reg,
9527                                                 HAVE_64BIT_ADDRESSES);
9528                 }
9529             }
9530         }
9531
9532       break;
9533
9534     case M_JALS_A:
9535       gas_assert (mips_opts.micromips);
9536       if (mips_opts.insn32)
9537         {
9538           as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
9539           break;
9540         }
9541       jals = 1;
9542       /* Fall through.  */
9543     case M_JAL_A:
9544       if (mips_pic == NO_PIC)
9545         macro_build (&offset_expr, jals ? "jals" : "jal", "a");
9546       else if (mips_pic == SVR4_PIC)
9547         {
9548           /* If this is a reference to an external symbol, and we are
9549              using a small GOT, we want
9550                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
9551                nop
9552                jalr     $ra,$25
9553                nop
9554                lw       $gp,cprestore($sp)
9555              The cprestore value is set using the .cprestore
9556              pseudo-op.  If we are using a big GOT, we want
9557                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
9558                addu     $25,$25,$gp
9559                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
9560                nop
9561                jalr     $ra,$25
9562                nop
9563                lw       $gp,cprestore($sp)
9564              If the symbol is not external, we want
9565                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9566                nop
9567                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
9568                jalr     $ra,$25
9569                nop
9570                lw $gp,cprestore($sp)
9571
9572              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
9573              sequences above, minus nops, unless the symbol is local,
9574              which enables us to use GOT_PAGE/GOT_OFST (big got) or
9575              GOT_DISP.  */
9576           if (HAVE_NEWABI)
9577             {
9578               if (!mips_big_got)
9579                 {
9580                   relax_start (offset_expr.X_add_symbol);
9581                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9582                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
9583                                mips_gp_register);
9584                   relax_switch ();
9585                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9586                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
9587                                mips_gp_register);
9588                   relax_end ();
9589                 }
9590               else
9591                 {
9592                   relax_start (offset_expr.X_add_symbol);
9593                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
9594                                BFD_RELOC_MIPS_CALL_HI16);
9595                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
9596                                PIC_CALL_REG, mips_gp_register);
9597                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9598                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
9599                                PIC_CALL_REG);
9600                   relax_switch ();
9601                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9602                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
9603                                mips_gp_register);
9604                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9605                                PIC_CALL_REG, PIC_CALL_REG,
9606                                BFD_RELOC_MIPS_GOT_OFST);
9607                   relax_end ();
9608                 }
9609
9610               macro_build_jalr (&offset_expr, 0);
9611             }
9612           else
9613             {
9614               relax_start (offset_expr.X_add_symbol);
9615               if (!mips_big_got)
9616                 {
9617                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9618                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
9619                                mips_gp_register);
9620                   load_delay_nop ();
9621                   relax_switch ();
9622                 }
9623               else
9624                 {
9625                   int gpdelay;
9626
9627                   gpdelay = reg_needs_delay (mips_gp_register);
9628                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
9629                                BFD_RELOC_MIPS_CALL_HI16);
9630                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
9631                                PIC_CALL_REG, mips_gp_register);
9632                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9633                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
9634                                PIC_CALL_REG);
9635                   load_delay_nop ();
9636                   relax_switch ();
9637                   if (gpdelay)
9638                     macro_build (NULL, "nop", "");
9639                 }
9640               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9641                            PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
9642                            mips_gp_register);
9643               load_delay_nop ();
9644               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
9645                            PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
9646               relax_end ();
9647               macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
9648
9649               if (mips_cprestore_offset < 0)
9650                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
9651               else
9652                 {
9653                   if (!mips_frame_reg_valid)
9654                     {
9655                       as_warn (_("No .frame pseudo-op used in PIC code"));
9656                       /* Quiet this warning.  */
9657                       mips_frame_reg_valid = 1;
9658                     }
9659                   if (!mips_cprestore_valid)
9660                     {
9661                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
9662                       /* Quiet this warning.  */
9663                       mips_cprestore_valid = 1;
9664                     }
9665                   if (mips_opts.noreorder)
9666                     macro_build (NULL, "nop", "");
9667                   expr1.X_add_number = mips_cprestore_offset;
9668                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
9669                                                 mips_gp_register,
9670                                                 mips_frame_reg,
9671                                                 HAVE_64BIT_ADDRESSES);
9672                 }
9673             }
9674         }
9675       else if (mips_pic == VXWORKS_PIC)
9676         as_bad (_("Non-PIC jump used in PIC library"));
9677       else
9678         abort ();
9679
9680       break;
9681
9682     case M_LBUE_AB:
9683       s = "lbue";
9684       fmt = "t,+j(b)";
9685       offbits = 9;
9686       goto ld_st;
9687     case M_LHUE_AB:
9688       s = "lhue";
9689       fmt = "t,+j(b)";
9690       offbits = 9;
9691       goto ld_st;
9692     case M_LBE_AB:
9693       s = "lbe";
9694       fmt = "t,+j(b)";
9695       offbits = 9;
9696       goto ld_st;
9697     case M_LHE_AB:
9698       s = "lhe";
9699       fmt = "t,+j(b)";
9700       offbits = 9;
9701       goto ld_st;
9702     case M_LLE_AB:
9703       s = "lle";
9704       fmt = "t,+j(b)";
9705       offbits = 9;
9706       goto ld_st;
9707     case M_LWE_AB:
9708       s = "lwe";
9709       fmt = "t,+j(b)";
9710       offbits = 9;
9711       goto ld_st;
9712     case M_LWLE_AB:
9713       s = "lwle";
9714       fmt = "t,+j(b)";
9715       offbits = 9;
9716       goto ld_st;
9717     case M_LWRE_AB:
9718       s = "lwre";
9719       fmt = "t,+j(b)";
9720       offbits = 9;
9721       goto ld_st;
9722     case M_SBE_AB:
9723       s = "sbe";
9724       fmt = "t,+j(b)";
9725       offbits = 9;
9726       goto ld_st;
9727     case M_SCE_AB:
9728       s = "sce";
9729       fmt = "t,+j(b)";
9730       offbits = 9;
9731       goto ld_st;
9732     case M_SHE_AB:
9733       s = "she";
9734       fmt = "t,+j(b)";
9735       offbits = 9;
9736       goto ld_st;
9737     case M_SWE_AB:
9738       s = "swe";
9739       fmt = "t,+j(b)";
9740       offbits = 9;
9741       goto ld_st;
9742     case M_SWLE_AB:
9743       s = "swle";
9744       fmt = "t,+j(b)";
9745       offbits = 9;
9746       goto ld_st;
9747     case M_SWRE_AB:
9748       s = "swre";
9749       fmt = "t,+j(b)";
9750       offbits = 9;
9751       goto ld_st;
9752     case M_ACLR_AB:
9753       s = "aclr";
9754       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
9755       fmt = "\\,~(b)";
9756       offbits = 12;
9757       goto ld_st;
9758     case M_ASET_AB:
9759       s = "aset";
9760       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
9761       fmt = "\\,~(b)";
9762       offbits = 12;
9763       goto ld_st;
9764     case M_LB_AB:
9765       s = "lb";
9766       fmt = "t,o(b)";
9767       goto ld;
9768     case M_LBU_AB:
9769       s = "lbu";
9770       fmt = "t,o(b)";
9771       goto ld;
9772     case M_LH_AB:
9773       s = "lh";
9774       fmt = "t,o(b)";
9775       goto ld;
9776     case M_LHU_AB:
9777       s = "lhu";
9778       fmt = "t,o(b)";
9779       goto ld;
9780     case M_LW_AB:
9781       s = "lw";
9782       fmt = "t,o(b)";
9783       goto ld;
9784     case M_LWC0_AB:
9785       gas_assert (!mips_opts.micromips);
9786       s = "lwc0";
9787       fmt = "E,o(b)";
9788       /* Itbl support may require additional care here.  */
9789       coproc = 1;
9790       goto ld_st;
9791     case M_LWC1_AB:
9792       s = "lwc1";
9793       fmt = "T,o(b)";
9794       /* Itbl support may require additional care here.  */
9795       coproc = 1;
9796       goto ld_st;
9797     case M_LWC2_AB:
9798       s = "lwc2";
9799       fmt = COP12_FMT;
9800       offbits = (mips_opts.micromips ? 12 : 16);
9801       /* Itbl support may require additional care here.  */
9802       coproc = 1;
9803       goto ld_st;
9804     case M_LWC3_AB:
9805       gas_assert (!mips_opts.micromips);
9806       s = "lwc3";
9807       fmt = "E,o(b)";
9808       /* Itbl support may require additional care here.  */
9809       coproc = 1;
9810       goto ld_st;
9811     case M_LWL_AB:
9812       s = "lwl";
9813       fmt = MEM12_FMT;
9814       offbits = (mips_opts.micromips ? 12 : 16);
9815       goto ld_st;
9816     case M_LWR_AB:
9817       s = "lwr";
9818       fmt = MEM12_FMT;
9819       offbits = (mips_opts.micromips ? 12 : 16);
9820       goto ld_st;
9821     case M_LDC1_AB:
9822       s = "ldc1";
9823       fmt = "T,o(b)";
9824       /* Itbl support may require additional care here.  */
9825       coproc = 1;
9826       goto ld_st;
9827     case M_LDC2_AB:
9828       s = "ldc2";
9829       fmt = COP12_FMT;
9830       offbits = (mips_opts.micromips ? 12 : 16);
9831       /* Itbl support may require additional care here.  */
9832       coproc = 1;
9833       goto ld_st;
9834     case M_LQC2_AB:
9835       s = "lqc2";
9836       fmt = "E,o(b)";
9837       /* Itbl support may require additional care here.  */
9838       coproc = 1;
9839       goto ld_st;
9840     case M_LDC3_AB:
9841       s = "ldc3";
9842       fmt = "E,o(b)";
9843       /* Itbl support may require additional care here.  */
9844       coproc = 1;
9845       goto ld_st;
9846     case M_LDL_AB:
9847       s = "ldl";
9848       fmt = MEM12_FMT;
9849       offbits = (mips_opts.micromips ? 12 : 16);
9850       goto ld_st;
9851     case M_LDR_AB:
9852       s = "ldr";
9853       fmt = MEM12_FMT;
9854       offbits = (mips_opts.micromips ? 12 : 16);
9855       goto ld_st;
9856     case M_LL_AB:
9857       s = "ll";
9858       fmt = MEM12_FMT;
9859       offbits = (mips_opts.micromips ? 12 : 16);
9860       goto ld;
9861     case M_LLD_AB:
9862       s = "lld";
9863       fmt = MEM12_FMT;
9864       offbits = (mips_opts.micromips ? 12 : 16);
9865       goto ld;
9866     case M_LWU_AB:
9867       s = "lwu";
9868       fmt = MEM12_FMT;
9869       offbits = (mips_opts.micromips ? 12 : 16);
9870       goto ld;
9871     case M_LWP_AB:
9872       gas_assert (mips_opts.micromips);
9873       s = "lwp";
9874       fmt = "t,~(b)";
9875       offbits = 12;
9876       lp = 1;
9877       goto ld;
9878     case M_LDP_AB:
9879       gas_assert (mips_opts.micromips);
9880       s = "ldp";
9881       fmt = "t,~(b)";
9882       offbits = 12;
9883       lp = 1;
9884       goto ld;
9885     case M_LWM_AB:
9886       gas_assert (mips_opts.micromips);
9887       s = "lwm";
9888       fmt = "n,~(b)";
9889       offbits = 12;
9890       goto ld_st;
9891     case M_LDM_AB:
9892       gas_assert (mips_opts.micromips);
9893       s = "ldm";
9894       fmt = "n,~(b)";
9895       offbits = 12;
9896       goto ld_st;
9897
9898     ld:
9899       /* We don't want to use $0 as tempreg.  */
9900       if (breg == treg + lp || treg + lp == ZERO)
9901         goto ld_st;
9902       else
9903         tempreg = treg + lp;
9904       goto ld_noat;
9905
9906     case M_SB_AB:
9907       s = "sb";
9908       fmt = "t,o(b)";
9909       goto ld_st;
9910     case M_SH_AB:
9911       s = "sh";
9912       fmt = "t,o(b)";
9913       goto ld_st;
9914     case M_SW_AB:
9915       s = "sw";
9916       fmt = "t,o(b)";
9917       goto ld_st;
9918     case M_SWC0_AB:
9919       gas_assert (!mips_opts.micromips);
9920       s = "swc0";
9921       fmt = "E,o(b)";
9922       /* Itbl support may require additional care here.  */
9923       coproc = 1;
9924       goto ld_st;
9925     case M_SWC1_AB:
9926       s = "swc1";
9927       fmt = "T,o(b)";
9928       /* Itbl support may require additional care here.  */
9929       coproc = 1;
9930       goto ld_st;
9931     case M_SWC2_AB:
9932       s = "swc2";
9933       fmt = COP12_FMT;
9934       offbits = (mips_opts.micromips ? 12 : 16);
9935       /* Itbl support may require additional care here.  */
9936       coproc = 1;
9937       goto ld_st;
9938     case M_SWC3_AB:
9939       gas_assert (!mips_opts.micromips);
9940       s = "swc3";
9941       fmt = "E,o(b)";
9942       /* Itbl support may require additional care here.  */
9943       coproc = 1;
9944       goto ld_st;
9945     case M_SWL_AB:
9946       s = "swl";
9947       fmt = MEM12_FMT;
9948       offbits = (mips_opts.micromips ? 12 : 16);
9949       goto ld_st;
9950     case M_SWR_AB:
9951       s = "swr";
9952       fmt = MEM12_FMT;
9953       offbits = (mips_opts.micromips ? 12 : 16);
9954       goto ld_st;
9955     case M_SC_AB:
9956       s = "sc";
9957       fmt = MEM12_FMT;
9958       offbits = (mips_opts.micromips ? 12 : 16);
9959       goto ld_st;
9960     case M_SCD_AB:
9961       s = "scd";
9962       fmt = MEM12_FMT;
9963       offbits = (mips_opts.micromips ? 12 : 16);
9964       goto ld_st;
9965     case M_CACHE_AB:
9966       s = "cache";
9967       fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
9968       offbits = (mips_opts.micromips ? 12 : 16);
9969       goto ld_st;
9970     case M_CACHEE_AB:
9971       s = "cachee";
9972       fmt = "k,+j(b)";
9973       offbits = 9;
9974       goto ld_st;
9975     case M_PREF_AB:
9976       s = "pref";
9977       fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
9978       offbits = (mips_opts.micromips ? 12 : 16);
9979       goto ld_st;
9980     case M_PREFE_AB:
9981       s = "prefe";
9982       fmt = "k,+j(b)";
9983       offbits = 9;
9984       goto ld_st;
9985     case M_SDC1_AB:
9986       s = "sdc1";
9987       fmt = "T,o(b)";
9988       coproc = 1;
9989       /* Itbl support may require additional care here.  */
9990       goto ld_st;
9991     case M_SDC2_AB:
9992       s = "sdc2";
9993       fmt = COP12_FMT;
9994       offbits = (mips_opts.micromips ? 12 : 16);
9995       /* Itbl support may require additional care here.  */
9996       coproc = 1;
9997       goto ld_st;
9998     case M_SQC2_AB:
9999       s = "sqc2";
10000       fmt = "E,o(b)";
10001       /* Itbl support may require additional care here.  */
10002       coproc = 1;
10003       goto ld_st;
10004     case M_SDC3_AB:
10005       gas_assert (!mips_opts.micromips);
10006       s = "sdc3";
10007       fmt = "E,o(b)";
10008       /* Itbl support may require additional care here.  */
10009       coproc = 1;
10010       goto ld_st;
10011     case M_SDL_AB:
10012       s = "sdl";
10013       fmt = MEM12_FMT;
10014       offbits = (mips_opts.micromips ? 12 : 16);
10015       goto ld_st;
10016     case M_SDR_AB:
10017       s = "sdr";
10018       fmt = MEM12_FMT;
10019       offbits = (mips_opts.micromips ? 12 : 16);
10020       goto ld_st;
10021     case M_SWP_AB:
10022       gas_assert (mips_opts.micromips);
10023       s = "swp";
10024       fmt = "t,~(b)";
10025       offbits = 12;
10026       goto ld_st;
10027     case M_SDP_AB:
10028       gas_assert (mips_opts.micromips);
10029       s = "sdp";
10030       fmt = "t,~(b)";
10031       offbits = 12;
10032       goto ld_st;
10033     case M_SWM_AB:
10034       gas_assert (mips_opts.micromips);
10035       s = "swm";
10036       fmt = "n,~(b)";
10037       offbits = 12;
10038       goto ld_st;
10039     case M_SDM_AB:
10040       gas_assert (mips_opts.micromips);
10041       s = "sdm";
10042       fmt = "n,~(b)";
10043       offbits = 12;
10044
10045     ld_st:
10046       tempreg = AT;
10047     ld_noat:
10048       if (small_offset_p (0, align, 16))
10049         {
10050           /* The first case exists for M_LD_AB and M_SD_AB, which are
10051              macros for o32 but which should act like normal instructions
10052              otherwise.  */
10053           if (offbits == 16)
10054             macro_build (&offset_expr, s, fmt, treg, -1, offset_reloc[0],
10055                          offset_reloc[1], offset_reloc[2], breg);
10056           else if (small_offset_p (0, align, offbits))
10057             {
10058               if (offbits == 0)
10059                 macro_build (NULL, s, fmt, treg, breg);
10060               else
10061                 macro_build (NULL, s, fmt, treg,
10062                              (int) offset_expr.X_add_number, breg);
10063             }
10064           else
10065             {
10066               if (tempreg == AT)
10067                 used_at = 1;
10068               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10069                            tempreg, breg, -1, offset_reloc[0],
10070                            offset_reloc[1], offset_reloc[2]);
10071               if (offbits == 0)
10072                 macro_build (NULL, s, fmt, treg, tempreg);
10073               else
10074                 macro_build (NULL, s, fmt, treg, 0, tempreg);
10075             }
10076           break;
10077         }
10078
10079       if (tempreg == AT)
10080         used_at = 1;
10081
10082       if (offset_expr.X_op != O_constant
10083           && offset_expr.X_op != O_symbol)
10084         {
10085           as_bad (_("Expression too complex"));
10086           offset_expr.X_op = O_constant;
10087         }
10088
10089       if (HAVE_32BIT_ADDRESSES
10090           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10091         {
10092           char value [32];
10093
10094           sprintf_vma (value, offset_expr.X_add_number);
10095           as_bad (_("Number (0x%s) larger than 32 bits"), value);
10096         }
10097
10098       /* A constant expression in PIC code can be handled just as it
10099          is in non PIC code.  */
10100       if (offset_expr.X_op == O_constant)
10101         {
10102           expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
10103                                                  offbits == 0 ? 16 : offbits);
10104           offset_expr.X_add_number -= expr1.X_add_number;
10105
10106           load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
10107           if (breg != 0)
10108             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10109                          tempreg, tempreg, breg);
10110           if (offbits == 0)
10111             {
10112               if (offset_expr.X_add_number != 0)
10113                 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
10114                              "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
10115               macro_build (NULL, s, fmt, treg, tempreg);
10116             }
10117           else if (offbits == 16)
10118             macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
10119           else
10120             macro_build (NULL, s, fmt, treg,
10121                          (int) offset_expr.X_add_number, tempreg);
10122         }
10123       else if (offbits != 16)
10124         {
10125           /* The offset field is too narrow to be used for a low-part
10126              relocation, so load the whole address into the auxillary
10127              register.  */
10128           load_address (tempreg, &offset_expr, &used_at);
10129           if (breg != 0)
10130             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10131                          tempreg, tempreg, breg);
10132           if (offbits == 0)
10133             macro_build (NULL, s, fmt, treg, tempreg);
10134           else
10135             macro_build (NULL, s, fmt, treg, 0, tempreg);
10136         }
10137       else if (mips_pic == NO_PIC)
10138         {
10139           /* If this is a reference to a GP relative symbol, and there
10140              is no base register, we want
10141                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
10142              Otherwise, if there is no base register, we want
10143                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
10144                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
10145              If we have a constant, we need two instructions anyhow,
10146              so we always use the latter form.
10147
10148              If we have a base register, and this is a reference to a
10149              GP relative symbol, we want
10150                addu     $tempreg,$breg,$gp
10151                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
10152              Otherwise we want
10153                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
10154                addu     $tempreg,$tempreg,$breg
10155                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
10156              With a constant we always use the latter case.
10157
10158              With 64bit address space and no base register and $at usable,
10159              we want
10160                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
10161                lui      $at,<sym>               (BFD_RELOC_HI16_S)
10162                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
10163                dsll32   $tempreg,0
10164                daddu    $tempreg,$at
10165                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
10166              If we have a base register, we want
10167                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
10168                lui      $at,<sym>               (BFD_RELOC_HI16_S)
10169                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
10170                daddu    $at,$breg
10171                dsll32   $tempreg,0
10172                daddu    $tempreg,$at
10173                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
10174
10175              Without $at we can't generate the optimal path for superscalar
10176              processors here since this would require two temporary registers.
10177                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
10178                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
10179                dsll     $tempreg,16
10180                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
10181                dsll     $tempreg,16
10182                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
10183              If we have a base register, we want
10184                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
10185                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
10186                dsll     $tempreg,16
10187                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
10188                dsll     $tempreg,16
10189                daddu    $tempreg,$tempreg,$breg
10190                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
10191
10192              For GP relative symbols in 64bit address space we can use
10193              the same sequence as in 32bit address space.  */
10194           if (HAVE_64BIT_SYMBOLS)
10195             {
10196               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10197                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10198                 {
10199                   relax_start (offset_expr.X_add_symbol);
10200                   if (breg == 0)
10201                     {
10202                       macro_build (&offset_expr, s, fmt, treg,
10203                                    BFD_RELOC_GPREL16, mips_gp_register);
10204                     }
10205                   else
10206                     {
10207                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10208                                    tempreg, breg, mips_gp_register);
10209                       macro_build (&offset_expr, s, fmt, treg,
10210                                    BFD_RELOC_GPREL16, tempreg);
10211                     }
10212                   relax_switch ();
10213                 }
10214
10215               if (used_at == 0 && mips_opts.at)
10216                 {
10217                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
10218                                BFD_RELOC_MIPS_HIGHEST);
10219                   macro_build (&offset_expr, "lui", LUI_FMT, AT,
10220                                BFD_RELOC_HI16_S);
10221                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10222                                tempreg, BFD_RELOC_MIPS_HIGHER);
10223                   if (breg != 0)
10224                     macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
10225                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10226                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10227                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
10228                                tempreg);
10229                   used_at = 1;
10230                 }
10231               else
10232                 {
10233                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
10234                                BFD_RELOC_MIPS_HIGHEST);
10235                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10236                                tempreg, BFD_RELOC_MIPS_HIGHER);
10237                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10238                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
10239                                tempreg, BFD_RELOC_HI16_S);
10240                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10241                   if (breg != 0)
10242                     macro_build (NULL, "daddu", "d,v,t",
10243                                  tempreg, tempreg, breg);
10244                   macro_build (&offset_expr, s, fmt, treg,
10245                                BFD_RELOC_LO16, tempreg);
10246                 }
10247
10248               if (mips_relax.sequence)
10249                 relax_end ();
10250               break;
10251             }
10252
10253           if (breg == 0)
10254             {
10255               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10256                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10257                 {
10258                   relax_start (offset_expr.X_add_symbol);
10259                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
10260                                mips_gp_register);
10261                   relax_switch ();
10262                 }
10263               macro_build_lui (&offset_expr, tempreg);
10264               macro_build (&offset_expr, s, fmt, treg,
10265                            BFD_RELOC_LO16, tempreg);
10266               if (mips_relax.sequence)
10267                 relax_end ();
10268             }
10269           else
10270             {
10271               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10272                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10273                 {
10274                   relax_start (offset_expr.X_add_symbol);
10275                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10276                                tempreg, breg, mips_gp_register);
10277                   macro_build (&offset_expr, s, fmt, treg,
10278                                BFD_RELOC_GPREL16, tempreg);
10279                   relax_switch ();
10280                 }
10281               macro_build_lui (&offset_expr, tempreg);
10282               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10283                            tempreg, tempreg, breg);
10284               macro_build (&offset_expr, s, fmt, treg,
10285                            BFD_RELOC_LO16, tempreg);
10286               if (mips_relax.sequence)
10287                 relax_end ();
10288             }
10289         }
10290       else if (!mips_big_got)
10291         {
10292           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10293
10294           /* If this is a reference to an external symbol, we want
10295                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
10296                nop
10297                <op>     $treg,0($tempreg)
10298              Otherwise we want
10299                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
10300                nop
10301                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10302                <op>     $treg,0($tempreg)
10303
10304              For NewABI, we want
10305                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
10306                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
10307
10308              If there is a base register, we add it to $tempreg before
10309              the <op>.  If there is a constant, we stick it in the
10310              <op> instruction.  We don't handle constants larger than
10311              16 bits, because we have no way to load the upper 16 bits
10312              (actually, we could handle them for the subset of cases
10313              in which we are not using $at).  */
10314           gas_assert (offset_expr.X_op == O_symbol);
10315           if (HAVE_NEWABI)
10316             {
10317               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10318                            BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10319               if (breg != 0)
10320                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10321                              tempreg, tempreg, breg);
10322               macro_build (&offset_expr, s, fmt, treg,
10323                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
10324               break;
10325             }
10326           expr1.X_add_number = offset_expr.X_add_number;
10327           offset_expr.X_add_number = 0;
10328           if (expr1.X_add_number < -0x8000
10329               || expr1.X_add_number >= 0x8000)
10330             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
10331           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10332                        lw_reloc_type, mips_gp_register);
10333           load_delay_nop ();
10334           relax_start (offset_expr.X_add_symbol);
10335           relax_switch ();
10336           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10337                        tempreg, BFD_RELOC_LO16);
10338           relax_end ();
10339           if (breg != 0)
10340             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10341                          tempreg, tempreg, breg);
10342           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
10343         }
10344       else if (mips_big_got && !HAVE_NEWABI)
10345         {
10346           int gpdelay;
10347
10348           /* If this is a reference to an external symbol, we want
10349                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
10350                addu     $tempreg,$tempreg,$gp
10351                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10352                <op>     $treg,0($tempreg)
10353              Otherwise we want
10354                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
10355                nop
10356                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10357                <op>     $treg,0($tempreg)
10358              If there is a base register, we add it to $tempreg before
10359              the <op>.  If there is a constant, we stick it in the
10360              <op> instruction.  We don't handle constants larger than
10361              16 bits, because we have no way to load the upper 16 bits
10362              (actually, we could handle them for the subset of cases
10363              in which we are not using $at).  */
10364           gas_assert (offset_expr.X_op == O_symbol);
10365           expr1.X_add_number = offset_expr.X_add_number;
10366           offset_expr.X_add_number = 0;
10367           if (expr1.X_add_number < -0x8000
10368               || expr1.X_add_number >= 0x8000)
10369             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
10370           gpdelay = reg_needs_delay (mips_gp_register);
10371           relax_start (offset_expr.X_add_symbol);
10372           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
10373                        BFD_RELOC_MIPS_GOT_HI16);
10374           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
10375                        mips_gp_register);
10376           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10377                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
10378           relax_switch ();
10379           if (gpdelay)
10380             macro_build (NULL, "nop", "");
10381           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10382                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
10383           load_delay_nop ();
10384           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10385                        tempreg, BFD_RELOC_LO16);
10386           relax_end ();
10387
10388           if (breg != 0)
10389             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10390                          tempreg, tempreg, breg);
10391           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
10392         }
10393       else if (mips_big_got && HAVE_NEWABI)
10394         {
10395           /* If this is a reference to an external symbol, we want
10396                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
10397                add      $tempreg,$tempreg,$gp
10398                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10399                <op>     $treg,<ofst>($tempreg)
10400              Otherwise, for local symbols, we want:
10401                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
10402                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
10403           gas_assert (offset_expr.X_op == O_symbol);
10404           expr1.X_add_number = offset_expr.X_add_number;
10405           offset_expr.X_add_number = 0;
10406           if (expr1.X_add_number < -0x8000
10407               || expr1.X_add_number >= 0x8000)
10408             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
10409           relax_start (offset_expr.X_add_symbol);
10410           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
10411                        BFD_RELOC_MIPS_GOT_HI16);
10412           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
10413                        mips_gp_register);
10414           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10415                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
10416           if (breg != 0)
10417             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10418                          tempreg, tempreg, breg);
10419           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
10420
10421           relax_switch ();
10422           offset_expr.X_add_number = expr1.X_add_number;
10423           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10424                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10425           if (breg != 0)
10426             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10427                          tempreg, tempreg, breg);
10428           macro_build (&offset_expr, s, fmt, treg,
10429                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
10430           relax_end ();
10431         }
10432       else
10433         abort ();
10434
10435       break;
10436
10437     case M_JRADDIUSP:
10438       gas_assert (mips_opts.micromips);
10439       gas_assert (mips_opts.insn32);
10440       start_noreorder ();
10441       macro_build (NULL, "jr", "s", RA);
10442       expr1.X_add_number = EXTRACT_OPERAND (1, IMMP, *ip) << 2;
10443       macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
10444       end_noreorder ();
10445       break;
10446
10447     case M_JRC:
10448       gas_assert (mips_opts.micromips);
10449       gas_assert (mips_opts.insn32);
10450       macro_build (NULL, "jr", "s", sreg);
10451       if (mips_opts.noreorder)
10452         macro_build (NULL, "nop", "");
10453       break;
10454
10455     case M_LI:
10456     case M_LI_S:
10457       load_register (treg, &imm_expr, 0);
10458       break;
10459
10460     case M_DLI:
10461       load_register (treg, &imm_expr, 1);
10462       break;
10463
10464     case M_LI_SS:
10465       if (imm_expr.X_op == O_constant)
10466         {
10467           used_at = 1;
10468           load_register (AT, &imm_expr, 0);
10469           macro_build (NULL, "mtc1", "t,G", AT, treg);
10470           break;
10471         }
10472       else
10473         {
10474           gas_assert (offset_expr.X_op == O_symbol
10475                       && strcmp (segment_name (S_GET_SEGMENT
10476                                                (offset_expr.X_add_symbol)),
10477                                  ".lit4") == 0
10478                       && offset_expr.X_add_number == 0);
10479           macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
10480                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
10481           break;
10482         }
10483
10484     case M_LI_D:
10485       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
10486          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
10487          order 32 bits of the value and the low order 32 bits are either
10488          zero or in OFFSET_EXPR.  */
10489       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
10490         {
10491           if (HAVE_64BIT_GPRS)
10492             load_register (treg, &imm_expr, 1);
10493           else
10494             {
10495               int hreg, lreg;
10496
10497               if (target_big_endian)
10498                 {
10499                   hreg = treg;
10500                   lreg = treg + 1;
10501                 }
10502               else
10503                 {
10504                   hreg = treg + 1;
10505                   lreg = treg;
10506                 }
10507
10508               if (hreg <= 31)
10509                 load_register (hreg, &imm_expr, 0);
10510               if (lreg <= 31)
10511                 {
10512                   if (offset_expr.X_op == O_absent)
10513                     move_register (lreg, 0);
10514                   else
10515                     {
10516                       gas_assert (offset_expr.X_op == O_constant);
10517                       load_register (lreg, &offset_expr, 0);
10518                     }
10519                 }
10520             }
10521           break;
10522         }
10523
10524       /* We know that sym is in the .rdata section.  First we get the
10525          upper 16 bits of the address.  */
10526       if (mips_pic == NO_PIC)
10527         {
10528           macro_build_lui (&offset_expr, AT);
10529           used_at = 1;
10530         }
10531       else
10532         {
10533           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
10534                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
10535           used_at = 1;
10536         }
10537
10538       /* Now we load the register(s).  */
10539       if (HAVE_64BIT_GPRS)
10540         {
10541           used_at = 1;
10542           macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
10543         }
10544       else
10545         {
10546           used_at = 1;
10547           macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
10548           if (treg != RA)
10549             {
10550               /* FIXME: How in the world do we deal with the possible
10551                  overflow here?  */
10552               offset_expr.X_add_number += 4;
10553               macro_build (&offset_expr, "lw", "t,o(b)",
10554                            treg + 1, BFD_RELOC_LO16, AT);
10555             }
10556         }
10557       break;
10558
10559     case M_LI_DD:
10560       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
10561          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
10562          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
10563          the value and the low order 32 bits are either zero or in
10564          OFFSET_EXPR.  */
10565       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
10566         {
10567           used_at = 1;
10568           load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
10569           if (HAVE_64BIT_FPRS)
10570             {
10571               gas_assert (HAVE_64BIT_GPRS);
10572               macro_build (NULL, "dmtc1", "t,S", AT, treg);
10573             }
10574           else
10575             {
10576               macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
10577               if (offset_expr.X_op == O_absent)
10578                 macro_build (NULL, "mtc1", "t,G", 0, treg);
10579               else
10580                 {
10581                   gas_assert (offset_expr.X_op == O_constant);
10582                   load_register (AT, &offset_expr, 0);
10583                   macro_build (NULL, "mtc1", "t,G", AT, treg);
10584                 }
10585             }
10586           break;
10587         }
10588
10589       gas_assert (offset_expr.X_op == O_symbol
10590                   && offset_expr.X_add_number == 0);
10591       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
10592       if (strcmp (s, ".lit8") == 0)
10593         {
10594           breg = mips_gp_register;
10595           offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
10596           offset_reloc[1] = BFD_RELOC_UNUSED;
10597           offset_reloc[2] = BFD_RELOC_UNUSED;
10598         }
10599       else
10600         {
10601           gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
10602           used_at = 1;
10603           if (mips_pic != NO_PIC)
10604             macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
10605                          BFD_RELOC_MIPS_GOT16, mips_gp_register);
10606           else
10607             {
10608               /* FIXME: This won't work for a 64 bit address.  */
10609               macro_build_lui (&offset_expr, AT);
10610             }
10611
10612           breg = AT;
10613           offset_reloc[0] = BFD_RELOC_LO16;
10614           offset_reloc[1] = BFD_RELOC_UNUSED;
10615           offset_reloc[2] = BFD_RELOC_UNUSED;
10616         }
10617       align = 8;
10618       /* Fall through */
10619
10620     case M_L_DAB:
10621       /*
10622        * The MIPS assembler seems to check for X_add_number not
10623        * being double aligned and generating:
10624        *        lui     at,%hi(foo+1)
10625        *        addu    at,at,v1
10626        *        addiu   at,at,%lo(foo+1)
10627        *        lwc1    f2,0(at)
10628        *        lwc1    f3,4(at)
10629        * But, the resulting address is the same after relocation so why
10630        * generate the extra instruction?
10631        */
10632       /* Itbl support may require additional care here.  */
10633       coproc = 1;
10634       fmt = "T,o(b)";
10635       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
10636         {
10637           s = "ldc1";
10638           goto ld_st;
10639         }
10640       s = "lwc1";
10641       goto ldd_std;
10642
10643     case M_S_DAB:
10644       gas_assert (!mips_opts.micromips);
10645       /* Itbl support may require additional care here.  */
10646       coproc = 1;
10647       fmt = "T,o(b)";
10648       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
10649         {
10650           s = "sdc1";
10651           goto ld_st;
10652         }
10653       s = "swc1";
10654       goto ldd_std;
10655
10656     case M_LQ_AB:
10657       fmt = "t,o(b)";
10658       s = "lq";
10659       goto ld;
10660
10661     case M_SQ_AB:
10662       fmt = "t,o(b)";
10663       s = "sq";
10664       goto ld_st;
10665
10666     case M_LD_AB:
10667       fmt = "t,o(b)";
10668       if (HAVE_64BIT_GPRS)
10669         {
10670           s = "ld";
10671           goto ld;
10672         }
10673       s = "lw";
10674       goto ldd_std;
10675
10676     case M_SD_AB:
10677       fmt = "t,o(b)";
10678       if (HAVE_64BIT_GPRS)
10679         {
10680           s = "sd";
10681           goto ld_st;
10682         }
10683       s = "sw";
10684
10685     ldd_std:
10686       /* Even on a big endian machine $fn comes before $fn+1.  We have
10687          to adjust when loading from memory.  We set coproc if we must
10688          load $fn+1 first.  */
10689       /* Itbl support may require additional care here.  */
10690       if (!target_big_endian)
10691         coproc = 0;
10692
10693       if (small_offset_p (0, align, 16))
10694         {
10695           ep = &offset_expr;
10696           if (!small_offset_p (4, align, 16))
10697             {
10698               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
10699                            -1, offset_reloc[0], offset_reloc[1],
10700                            offset_reloc[2]);
10701               expr1.X_add_number = 0;
10702               ep = &expr1;
10703               breg = AT;
10704               used_at = 1;
10705               offset_reloc[0] = BFD_RELOC_LO16;
10706               offset_reloc[1] = BFD_RELOC_UNUSED;
10707               offset_reloc[2] = BFD_RELOC_UNUSED;
10708             }
10709           if (strcmp (s, "lw") == 0 && treg == breg)
10710             {
10711               ep->X_add_number += 4;
10712               macro_build (ep, s, fmt, treg + 1, -1, offset_reloc[0],
10713                            offset_reloc[1], offset_reloc[2], breg);
10714               ep->X_add_number -= 4;
10715               macro_build (ep, s, fmt, treg, -1, offset_reloc[0],
10716                            offset_reloc[1], offset_reloc[2], breg);
10717             }
10718           else
10719             {
10720               macro_build (ep, s, fmt, coproc ? treg + 1 : treg, -1,
10721                            offset_reloc[0], offset_reloc[1], offset_reloc[2],
10722                            breg);
10723               ep->X_add_number += 4;
10724               macro_build (ep, s, fmt, coproc ? treg : treg + 1, -1,
10725                            offset_reloc[0], offset_reloc[1], offset_reloc[2],
10726                            breg);
10727             }
10728           break;
10729         }
10730
10731       if (offset_expr.X_op != O_symbol
10732           && offset_expr.X_op != O_constant)
10733         {
10734           as_bad (_("Expression too complex"));
10735           offset_expr.X_op = O_constant;
10736         }
10737
10738       if (HAVE_32BIT_ADDRESSES
10739           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10740         {
10741           char value [32];
10742
10743           sprintf_vma (value, offset_expr.X_add_number);
10744           as_bad (_("Number (0x%s) larger than 32 bits"), value);
10745         }
10746
10747       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
10748         {
10749           /* If this is a reference to a GP relative symbol, we want
10750                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
10751                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
10752              If we have a base register, we use this
10753                addu     $at,$breg,$gp
10754                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
10755                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
10756              If this is not a GP relative symbol, we want
10757                lui      $at,<sym>               (BFD_RELOC_HI16_S)
10758                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
10759                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
10760              If there is a base register, we add it to $at after the
10761              lui instruction.  If there is a constant, we always use
10762              the last case.  */
10763           if (offset_expr.X_op == O_symbol
10764               && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10765               && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10766             {
10767               relax_start (offset_expr.X_add_symbol);
10768               if (breg == 0)
10769                 {
10770                   tempreg = mips_gp_register;
10771                 }
10772               else
10773                 {
10774                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10775                                AT, breg, mips_gp_register);
10776                   tempreg = AT;
10777                   used_at = 1;
10778                 }
10779
10780               /* Itbl support may require additional care here.  */
10781               macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
10782                            BFD_RELOC_GPREL16, tempreg);
10783               offset_expr.X_add_number += 4;
10784
10785               /* Set mips_optimize to 2 to avoid inserting an
10786                  undesired nop.  */
10787               hold_mips_optimize = mips_optimize;
10788               mips_optimize = 2;
10789               /* Itbl support may require additional care here.  */
10790               macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
10791                            BFD_RELOC_GPREL16, tempreg);
10792               mips_optimize = hold_mips_optimize;
10793
10794               relax_switch ();
10795
10796               offset_expr.X_add_number -= 4;
10797             }
10798           used_at = 1;
10799           if (offset_high_part (offset_expr.X_add_number, 16)
10800               != offset_high_part (offset_expr.X_add_number + 4, 16))
10801             {
10802               load_address (AT, &offset_expr, &used_at);
10803               offset_expr.X_op = O_constant;
10804               offset_expr.X_add_number = 0;
10805             }
10806           else
10807             macro_build_lui (&offset_expr, AT);
10808           if (breg != 0)
10809             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
10810           /* Itbl support may require additional care here.  */
10811           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
10812                        BFD_RELOC_LO16, AT);
10813           /* FIXME: How do we handle overflow here?  */
10814           offset_expr.X_add_number += 4;
10815           /* Itbl support may require additional care here.  */
10816           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
10817                        BFD_RELOC_LO16, AT);
10818           if (mips_relax.sequence)
10819             relax_end ();
10820         }
10821       else if (!mips_big_got)
10822         {
10823           /* If this is a reference to an external symbol, we want
10824                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
10825                nop
10826                <op>     $treg,0($at)
10827                <op>     $treg+1,4($at)
10828              Otherwise we want
10829                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
10830                nop
10831                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
10832                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
10833              If there is a base register we add it to $at before the
10834              lwc1 instructions.  If there is a constant we include it
10835              in the lwc1 instructions.  */
10836           used_at = 1;
10837           expr1.X_add_number = offset_expr.X_add_number;
10838           if (expr1.X_add_number < -0x8000
10839               || expr1.X_add_number >= 0x8000 - 4)
10840             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
10841           load_got_offset (AT, &offset_expr);
10842           load_delay_nop ();
10843           if (breg != 0)
10844             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
10845
10846           /* Set mips_optimize to 2 to avoid inserting an undesired
10847              nop.  */
10848           hold_mips_optimize = mips_optimize;
10849           mips_optimize = 2;
10850
10851           /* Itbl support may require additional care here.  */
10852           relax_start (offset_expr.X_add_symbol);
10853           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
10854                        BFD_RELOC_LO16, AT);
10855           expr1.X_add_number += 4;
10856           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
10857                        BFD_RELOC_LO16, AT);
10858           relax_switch ();
10859           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
10860                        BFD_RELOC_LO16, AT);
10861           offset_expr.X_add_number += 4;
10862           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
10863                        BFD_RELOC_LO16, AT);
10864           relax_end ();
10865
10866           mips_optimize = hold_mips_optimize;
10867         }
10868       else if (mips_big_got)
10869         {
10870           int gpdelay;
10871
10872           /* If this is a reference to an external symbol, we want
10873                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
10874                addu     $at,$at,$gp
10875                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
10876                nop
10877                <op>     $treg,0($at)
10878                <op>     $treg+1,4($at)
10879              Otherwise we want
10880                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
10881                nop
10882                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
10883                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
10884              If there is a base register we add it to $at before the
10885              lwc1 instructions.  If there is a constant we include it
10886              in the lwc1 instructions.  */
10887           used_at = 1;
10888           expr1.X_add_number = offset_expr.X_add_number;
10889           offset_expr.X_add_number = 0;
10890           if (expr1.X_add_number < -0x8000
10891               || expr1.X_add_number >= 0x8000 - 4)
10892             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
10893           gpdelay = reg_needs_delay (mips_gp_register);
10894           relax_start (offset_expr.X_add_symbol);
10895           macro_build (&offset_expr, "lui", LUI_FMT,
10896                        AT, BFD_RELOC_MIPS_GOT_HI16);
10897           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10898                        AT, AT, mips_gp_register);
10899           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10900                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
10901           load_delay_nop ();
10902           if (breg != 0)
10903             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
10904           /* Itbl support may require additional care here.  */
10905           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
10906                        BFD_RELOC_LO16, AT);
10907           expr1.X_add_number += 4;
10908
10909           /* Set mips_optimize to 2 to avoid inserting an undesired
10910              nop.  */
10911           hold_mips_optimize = mips_optimize;
10912           mips_optimize = 2;
10913           /* Itbl support may require additional care here.  */
10914           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
10915                        BFD_RELOC_LO16, AT);
10916           mips_optimize = hold_mips_optimize;
10917           expr1.X_add_number -= 4;
10918
10919           relax_switch ();
10920           offset_expr.X_add_number = expr1.X_add_number;
10921           if (gpdelay)
10922             macro_build (NULL, "nop", "");
10923           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
10924                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
10925           load_delay_nop ();
10926           if (breg != 0)
10927             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
10928           /* Itbl support may require additional care here.  */
10929           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
10930                        BFD_RELOC_LO16, AT);
10931           offset_expr.X_add_number += 4;
10932
10933           /* Set mips_optimize to 2 to avoid inserting an undesired
10934              nop.  */
10935           hold_mips_optimize = mips_optimize;
10936           mips_optimize = 2;
10937           /* Itbl support may require additional care here.  */
10938           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
10939                        BFD_RELOC_LO16, AT);
10940           mips_optimize = hold_mips_optimize;
10941           relax_end ();
10942         }
10943       else
10944         abort ();
10945
10946       break;
10947         
10948     case M_SAA_AB:
10949       s = "saa";
10950       offbits = 0;
10951       fmt = "t,(b)";
10952       goto ld_st;
10953     case M_SAAD_AB:
10954       s = "saad";
10955       offbits = 0;
10956       fmt = "t,(b)";
10957       goto ld_st;
10958
10959    /* New code added to support COPZ instructions.
10960       This code builds table entries out of the macros in mip_opcodes.
10961       R4000 uses interlocks to handle coproc delays.
10962       Other chips (like the R3000) require nops to be inserted for delays.
10963
10964       FIXME: Currently, we require that the user handle delays.
10965       In order to fill delay slots for non-interlocked chips,
10966       we must have a way to specify delays based on the coprocessor.
10967       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
10968       What are the side-effects of the cop instruction?
10969       What cache support might we have and what are its effects?
10970       Both coprocessor & memory require delays. how long???
10971       What registers are read/set/modified?
10972
10973       If an itbl is provided to interpret cop instructions,
10974       this knowledge can be encoded in the itbl spec.  */
10975
10976     case M_COP0:
10977       s = "c0";
10978       goto copz;
10979     case M_COP1:
10980       s = "c1";
10981       goto copz;
10982     case M_COP2:
10983       s = "c2";
10984       goto copz;
10985     case M_COP3:
10986       s = "c3";
10987     copz:
10988       gas_assert (!mips_opts.micromips);
10989       /* For now we just do C (same as Cz).  The parameter will be
10990          stored in insn_opcode by mips_ip.  */
10991       macro_build (NULL, s, "C", (int) ip->insn_opcode);
10992       break;
10993
10994     case M_MOVE:
10995       move_register (dreg, sreg);
10996       break;
10997
10998     case M_MOVEP:
10999       gas_assert (mips_opts.micromips);
11000       gas_assert (mips_opts.insn32);
11001       dreg = micromips_to_32_reg_h_map1[EXTRACT_OPERAND (1, MH, *ip)];
11002       breg = micromips_to_32_reg_h_map2[EXTRACT_OPERAND (1, MH, *ip)];
11003       sreg = micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
11004       treg = micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
11005       move_register (dreg, sreg);
11006       move_register (breg, treg);
11007       break;
11008
11009     case M_DMUL:
11010       dbl = 1;
11011     case M_MUL:
11012       if (mips_opts.arch == CPU_R5900)
11013         {
11014           macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
11015         }
11016       else
11017         {
11018       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
11019       macro_build (NULL, "mflo", MFHL_FMT, dreg);
11020         }
11021       break;
11022
11023     case M_DMUL_I:
11024       dbl = 1;
11025     case M_MUL_I:
11026       /* The MIPS assembler some times generates shifts and adds.  I'm
11027          not trying to be that fancy. GCC should do this for us
11028          anyway.  */
11029       used_at = 1;
11030       load_register (AT, &imm_expr, dbl);
11031       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
11032       macro_build (NULL, "mflo", MFHL_FMT, dreg);
11033       break;
11034
11035     case M_DMULO_I:
11036       dbl = 1;
11037     case M_MULO_I:
11038       imm = 1;
11039       goto do_mulo;
11040
11041     case M_DMULO:
11042       dbl = 1;
11043     case M_MULO:
11044     do_mulo:
11045       start_noreorder ();
11046       used_at = 1;
11047       if (imm)
11048         load_register (AT, &imm_expr, dbl);
11049       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
11050       macro_build (NULL, "mflo", MFHL_FMT, dreg);
11051       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
11052       macro_build (NULL, "mfhi", MFHL_FMT, AT);
11053       if (mips_trap)
11054         macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
11055       else
11056         {
11057           if (mips_opts.micromips)
11058             micromips_label_expr (&label_expr);
11059           else
11060             label_expr.X_add_number = 8;
11061           macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
11062           macro_build (NULL, "nop", "");
11063           macro_build (NULL, "break", BRK_FMT, 6);
11064           if (mips_opts.micromips)
11065             micromips_add_label ();
11066         }
11067       end_noreorder ();
11068       macro_build (NULL, "mflo", MFHL_FMT, dreg);
11069       break;
11070
11071     case M_DMULOU_I:
11072       dbl = 1;
11073     case M_MULOU_I:
11074       imm = 1;
11075       goto do_mulou;
11076
11077     case M_DMULOU:
11078       dbl = 1;
11079     case M_MULOU:
11080     do_mulou:
11081       start_noreorder ();
11082       used_at = 1;
11083       if (imm)
11084         load_register (AT, &imm_expr, dbl);
11085       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
11086                    sreg, imm ? AT : treg);
11087       macro_build (NULL, "mfhi", MFHL_FMT, AT);
11088       macro_build (NULL, "mflo", MFHL_FMT, dreg);
11089       if (mips_trap)
11090         macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
11091       else
11092         {
11093           if (mips_opts.micromips)
11094             micromips_label_expr (&label_expr);
11095           else
11096             label_expr.X_add_number = 8;
11097           macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
11098           macro_build (NULL, "nop", "");
11099           macro_build (NULL, "break", BRK_FMT, 6);
11100           if (mips_opts.micromips)
11101             micromips_add_label ();
11102         }
11103       end_noreorder ();
11104       break;
11105
11106     case M_DROL:
11107       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
11108         {
11109           if (dreg == sreg)
11110             {
11111               tempreg = AT;
11112               used_at = 1;
11113             }
11114           else
11115             {
11116               tempreg = dreg;
11117             }
11118           macro_build (NULL, "dnegu", "d,w", tempreg, treg);
11119           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
11120           break;
11121         }
11122       used_at = 1;
11123       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
11124       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
11125       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
11126       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11127       break;
11128
11129     case M_ROL:
11130       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
11131         {
11132           if (dreg == sreg)
11133             {
11134               tempreg = AT;
11135               used_at = 1;
11136             }
11137           else
11138             {
11139               tempreg = dreg;
11140             }
11141           macro_build (NULL, "negu", "d,w", tempreg, treg);
11142           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
11143           break;
11144         }
11145       used_at = 1;
11146       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
11147       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
11148       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
11149       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11150       break;
11151
11152     case M_DROL_I:
11153       {
11154         unsigned int rot;
11155         char *l;
11156         char *rr;
11157
11158         if (imm_expr.X_op != O_constant)
11159           as_bad (_("Improper rotate count"));
11160         rot = imm_expr.X_add_number & 0x3f;
11161         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
11162           {
11163             rot = (64 - rot) & 0x3f;
11164             if (rot >= 32)
11165               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
11166             else
11167               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
11168             break;
11169           }
11170         if (rot == 0)
11171           {
11172             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
11173             break;
11174           }
11175         l = (rot < 0x20) ? "dsll" : "dsll32";
11176         rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
11177         rot &= 0x1f;
11178         used_at = 1;
11179         macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
11180         macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
11181         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11182       }
11183       break;
11184
11185     case M_ROL_I:
11186       {
11187         unsigned int rot;
11188
11189         if (imm_expr.X_op != O_constant)
11190           as_bad (_("Improper rotate count"));
11191         rot = imm_expr.X_add_number & 0x1f;
11192         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
11193           {
11194             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
11195             break;
11196           }
11197         if (rot == 0)
11198           {
11199             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
11200             break;
11201           }
11202         used_at = 1;
11203         macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
11204         macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
11205         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11206       }
11207       break;
11208
11209     case M_DROR:
11210       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
11211         {
11212           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
11213           break;
11214         }
11215       used_at = 1;
11216       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
11217       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
11218       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
11219       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11220       break;
11221
11222     case M_ROR:
11223       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
11224         {
11225           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
11226           break;
11227         }
11228       used_at = 1;
11229       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
11230       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
11231       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
11232       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11233       break;
11234
11235     case M_DROR_I:
11236       {
11237         unsigned int rot;
11238         char *l;
11239         char *rr;
11240
11241         if (imm_expr.X_op != O_constant)
11242           as_bad (_("Improper rotate count"));
11243         rot = imm_expr.X_add_number & 0x3f;
11244         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
11245           {
11246             if (rot >= 32)
11247               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
11248             else
11249               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
11250             break;
11251           }
11252         if (rot == 0)
11253           {
11254             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
11255             break;
11256           }
11257         rr = (rot < 0x20) ? "dsrl" : "dsrl32";
11258         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
11259         rot &= 0x1f;
11260         used_at = 1;
11261         macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
11262         macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
11263         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11264       }
11265       break;
11266
11267     case M_ROR_I:
11268       {
11269         unsigned int rot;
11270
11271         if (imm_expr.X_op != O_constant)
11272           as_bad (_("Improper rotate count"));
11273         rot = imm_expr.X_add_number & 0x1f;
11274         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
11275           {
11276             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
11277             break;
11278           }
11279         if (rot == 0)
11280           {
11281             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
11282             break;
11283           }
11284         used_at = 1;
11285         macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
11286         macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
11287         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
11288       }
11289       break;
11290
11291     case M_SEQ:
11292       if (sreg == 0)
11293         macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
11294       else if (treg == 0)
11295         macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
11296       else
11297         {
11298           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
11299           macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
11300         }
11301       break;
11302
11303     case M_SEQ_I:
11304       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
11305         {
11306           macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
11307           break;
11308         }
11309       if (sreg == 0)
11310         {
11311           as_warn (_("Instruction %s: result is always false"),
11312                    ip->insn_mo->name);
11313           move_register (dreg, 0);
11314           break;
11315         }
11316       if (CPU_HAS_SEQ (mips_opts.arch)
11317           && -512 <= imm_expr.X_add_number
11318           && imm_expr.X_add_number < 512)
11319         {
11320           macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
11321                        (int) imm_expr.X_add_number);
11322           break;
11323         }
11324       if (imm_expr.X_op == O_constant
11325           && imm_expr.X_add_number >= 0
11326           && imm_expr.X_add_number < 0x10000)
11327         {
11328           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
11329         }
11330       else if (imm_expr.X_op == O_constant
11331                && imm_expr.X_add_number > -0x8000
11332                && imm_expr.X_add_number < 0)
11333         {
11334           imm_expr.X_add_number = -imm_expr.X_add_number;
11335           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
11336                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
11337         }
11338       else if (CPU_HAS_SEQ (mips_opts.arch))
11339         {
11340           used_at = 1;
11341           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11342           macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
11343           break;
11344         }
11345       else
11346         {
11347           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11348           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
11349           used_at = 1;
11350         }
11351       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
11352       break;
11353
11354     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
11355       s = "slt";
11356       goto sge;
11357     case M_SGEU:
11358       s = "sltu";
11359     sge:
11360       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
11361       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
11362       break;
11363
11364     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
11365     case M_SGEU_I:
11366       if (imm_expr.X_op == O_constant
11367           && imm_expr.X_add_number >= -0x8000
11368           && imm_expr.X_add_number < 0x8000)
11369         {
11370           macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
11371                        dreg, sreg, BFD_RELOC_LO16);
11372         }
11373       else
11374         {
11375           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11376           macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
11377                        dreg, sreg, AT);
11378           used_at = 1;
11379         }
11380       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
11381       break;
11382
11383     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
11384       s = "slt";
11385       goto sgt;
11386     case M_SGTU:
11387       s = "sltu";
11388     sgt:
11389       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
11390       break;
11391
11392     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
11393       s = "slt";
11394       goto sgti;
11395     case M_SGTU_I:
11396       s = "sltu";
11397     sgti:
11398       used_at = 1;
11399       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11400       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
11401       break;
11402
11403     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
11404       s = "slt";
11405       goto sle;
11406     case M_SLEU:
11407       s = "sltu";
11408     sle:
11409       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
11410       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
11411       break;
11412
11413     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
11414       s = "slt";
11415       goto slei;
11416     case M_SLEU_I:
11417       s = "sltu";
11418     slei:
11419       used_at = 1;
11420       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11421       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
11422       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
11423       break;
11424
11425     case M_SLT_I:
11426       if (imm_expr.X_op == O_constant
11427           && imm_expr.X_add_number >= -0x8000
11428           && imm_expr.X_add_number < 0x8000)
11429         {
11430           macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
11431           break;
11432         }
11433       used_at = 1;
11434       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11435       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
11436       break;
11437
11438     case M_SLTU_I:
11439       if (imm_expr.X_op == O_constant
11440           && imm_expr.X_add_number >= -0x8000
11441           && imm_expr.X_add_number < 0x8000)
11442         {
11443           macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
11444                        BFD_RELOC_LO16);
11445           break;
11446         }
11447       used_at = 1;
11448       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11449       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
11450       break;
11451
11452     case M_SNE:
11453       if (sreg == 0)
11454         macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
11455       else if (treg == 0)
11456         macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
11457       else
11458         {
11459           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
11460           macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
11461         }
11462       break;
11463
11464     case M_SNE_I:
11465       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
11466         {
11467           macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
11468           break;
11469         }
11470       if (sreg == 0)
11471         {
11472           as_warn (_("Instruction %s: result is always true"),
11473                    ip->insn_mo->name);
11474           macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
11475                        dreg, 0, BFD_RELOC_LO16);
11476           break;
11477         }
11478       if (CPU_HAS_SEQ (mips_opts.arch)
11479           && -512 <= imm_expr.X_add_number
11480           && imm_expr.X_add_number < 512)
11481         {
11482           macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
11483                        (int) imm_expr.X_add_number);
11484           break;
11485         }
11486       if (imm_expr.X_op == O_constant
11487           && imm_expr.X_add_number >= 0
11488           && imm_expr.X_add_number < 0x10000)
11489         {
11490           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
11491         }
11492       else if (imm_expr.X_op == O_constant
11493                && imm_expr.X_add_number > -0x8000
11494                && imm_expr.X_add_number < 0)
11495         {
11496           imm_expr.X_add_number = -imm_expr.X_add_number;
11497           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
11498                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
11499         }
11500       else if (CPU_HAS_SEQ (mips_opts.arch))
11501         {
11502           used_at = 1;
11503           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11504           macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
11505           break;
11506         }
11507       else
11508         {
11509           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11510           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
11511           used_at = 1;
11512         }
11513       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
11514       break;
11515
11516     case M_SUB_I:
11517       s = "addi";
11518       s2 = "sub";
11519       goto do_subi;
11520     case M_SUBU_I:
11521       s = "addiu";
11522       s2 = "subu";
11523       goto do_subi;
11524     case M_DSUB_I:
11525       dbl = 1;
11526       s = "daddi";
11527       s2 = "dsub";
11528       if (!mips_opts.micromips)
11529         goto do_subi;
11530       if (imm_expr.X_op == O_constant
11531           && imm_expr.X_add_number > -0x200
11532           && imm_expr.X_add_number <= 0x200)
11533         {
11534           macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
11535           break;
11536         }
11537       goto do_subi_i;
11538     case M_DSUBU_I:
11539       dbl = 1;
11540       s = "daddiu";
11541       s2 = "dsubu";
11542     do_subi:
11543       if (imm_expr.X_op == O_constant
11544           && imm_expr.X_add_number > -0x8000
11545           && imm_expr.X_add_number <= 0x8000)
11546         {
11547           imm_expr.X_add_number = -imm_expr.X_add_number;
11548           macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
11549           break;
11550         }
11551     do_subi_i:
11552       used_at = 1;
11553       load_register (AT, &imm_expr, dbl);
11554       macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
11555       break;
11556
11557     case M_TEQ_I:
11558       s = "teq";
11559       goto trap;
11560     case M_TGE_I:
11561       s = "tge";
11562       goto trap;
11563     case M_TGEU_I:
11564       s = "tgeu";
11565       goto trap;
11566     case M_TLT_I:
11567       s = "tlt";
11568       goto trap;
11569     case M_TLTU_I:
11570       s = "tltu";
11571       goto trap;
11572     case M_TNE_I:
11573       s = "tne";
11574     trap:
11575       used_at = 1;
11576       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
11577       macro_build (NULL, s, "s,t", sreg, AT);
11578       break;
11579
11580     case M_TRUNCWS:
11581     case M_TRUNCWD:
11582       gas_assert (!mips_opts.micromips);
11583       gas_assert (mips_opts.isa == ISA_MIPS1);
11584       used_at = 1;
11585       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
11586       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
11587
11588       /*
11589        * Is the double cfc1 instruction a bug in the mips assembler;
11590        * or is there a reason for it?
11591        */
11592       start_noreorder ();
11593       macro_build (NULL, "cfc1", "t,G", treg, RA);
11594       macro_build (NULL, "cfc1", "t,G", treg, RA);
11595       macro_build (NULL, "nop", "");
11596       expr1.X_add_number = 3;
11597       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
11598       expr1.X_add_number = 2;
11599       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
11600       macro_build (NULL, "ctc1", "t,G", AT, RA);
11601       macro_build (NULL, "nop", "");
11602       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
11603                    dreg, sreg);
11604       macro_build (NULL, "ctc1", "t,G", treg, RA);
11605       macro_build (NULL, "nop", "");
11606       end_noreorder ();
11607       break;
11608
11609     case M_ULH_AB:
11610       s = "lb";
11611       s2 = "lbu";
11612       off = 1;
11613       goto uld_st;
11614     case M_ULHU_AB:
11615       s = "lbu";
11616       s2 = "lbu";
11617       off = 1;
11618       goto uld_st;
11619     case M_ULW_AB:
11620       s = "lwl";
11621       s2 = "lwr";
11622       offbits = (mips_opts.micromips ? 12 : 16);
11623       off = 3;
11624       goto uld_st;
11625     case M_ULD_AB:
11626       s = "ldl";
11627       s2 = "ldr";
11628       offbits = (mips_opts.micromips ? 12 : 16);
11629       off = 7;
11630       goto uld_st;
11631     case M_USH_AB:
11632       s = "sb";
11633       s2 = "sb";
11634       off = 1;
11635       ust = 1;
11636       goto uld_st;
11637     case M_USW_AB:
11638       s = "swl";
11639       s2 = "swr";
11640       offbits = (mips_opts.micromips ? 12 : 16);
11641       off = 3;
11642       ust = 1;
11643       goto uld_st;
11644     case M_USD_AB:
11645       s = "sdl";
11646       s2 = "sdr";
11647       offbits = (mips_opts.micromips ? 12 : 16);
11648       off = 7;
11649       ust = 1;
11650
11651     uld_st:
11652       large_offset = !small_offset_p (off, align, offbits);
11653       ep = &offset_expr;
11654       expr1.X_add_number = 0;
11655       if (large_offset)
11656         {
11657           used_at = 1;
11658           tempreg = AT;
11659           if (small_offset_p (0, align, 16))
11660             macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
11661                          offset_reloc[0], offset_reloc[1], offset_reloc[2]);
11662           else
11663             {
11664               load_address (tempreg, ep, &used_at);
11665               if (breg != 0)
11666                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11667                              tempreg, tempreg, breg);
11668             }
11669           offset_reloc[0] = BFD_RELOC_LO16;
11670           offset_reloc[1] = BFD_RELOC_UNUSED;
11671           offset_reloc[2] = BFD_RELOC_UNUSED;
11672           breg = tempreg;
11673           tempreg = treg;
11674           ep = &expr1;
11675         }
11676       else if (!ust && treg == breg)
11677         {
11678           used_at = 1;
11679           tempreg = AT;
11680         }
11681       else
11682         tempreg = treg;
11683
11684       if (off == 1)
11685         goto ulh_sh;
11686
11687       if (!target_big_endian)
11688         ep->X_add_number += off;
11689       if (offbits == 12)
11690         macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
11691       else
11692         macro_build (ep, s, "t,o(b)", tempreg, -1,
11693                      offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
11694
11695       if (!target_big_endian)
11696         ep->X_add_number -= off;
11697       else
11698         ep->X_add_number += off;
11699       if (offbits == 12)
11700         macro_build (NULL, s2, "t,~(b)",
11701                      tempreg, (int) ep->X_add_number, breg);
11702       else
11703         macro_build (ep, s2, "t,o(b)", tempreg, -1,
11704                      offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
11705
11706       /* If necessary, move the result in tempreg to the final destination.  */
11707       if (!ust && treg != tempreg)
11708         {
11709           /* Protect second load's delay slot.  */
11710           load_delay_nop ();
11711           move_register (treg, tempreg);
11712         }
11713       break;
11714
11715     ulh_sh:
11716       used_at = 1;
11717       if (target_big_endian == ust)
11718         ep->X_add_number += off;
11719       tempreg = ust || large_offset ? treg : AT;
11720       macro_build (ep, s, "t,o(b)", tempreg, -1,
11721                    offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
11722
11723       /* For halfword transfers we need a temporary register to shuffle
11724          bytes.  Unfortunately for M_USH_A we have none available before
11725          the next store as AT holds the base address.  We deal with this
11726          case by clobbering TREG and then restoring it as with ULH.  */
11727       tempreg = ust == large_offset ? treg : AT;
11728       if (ust)
11729         macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
11730
11731       if (target_big_endian == ust)
11732         ep->X_add_number -= off;
11733       else
11734         ep->X_add_number += off;
11735       macro_build (ep, s2, "t,o(b)", tempreg, -1,
11736                    offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
11737
11738       /* For M_USH_A re-retrieve the LSB.  */
11739       if (ust && large_offset)
11740         {
11741           if (target_big_endian)
11742             ep->X_add_number += off;
11743           else
11744             ep->X_add_number -= off;
11745           macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
11746                        offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
11747         }
11748       /* For ULH and M_USH_A OR the LSB in.  */
11749       if (!ust || large_offset)
11750         {
11751           tempreg = !large_offset ? AT : treg;
11752           macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
11753           macro_build (NULL, "or", "d,v,t", treg, treg, AT);
11754         }
11755       break;
11756
11757     default:
11758       /* FIXME: Check if this is one of the itbl macros, since they
11759          are added dynamically.  */
11760       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
11761       break;
11762     }
11763   if (!mips_opts.at && used_at)
11764     as_bad (_("Macro used $at after \".set noat\""));
11765 }
11766
11767 /* Implement macros in mips16 mode.  */
11768
11769 static void
11770 mips16_macro (struct mips_cl_insn *ip)
11771 {
11772   int mask;
11773   int xreg, yreg, zreg, tmp;
11774   expressionS expr1;
11775   int dbl;
11776   const char *s, *s2, *s3;
11777
11778   mask = ip->insn_mo->mask;
11779
11780   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
11781   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
11782   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
11783
11784   expr1.X_op = O_constant;
11785   expr1.X_op_symbol = NULL;
11786   expr1.X_add_symbol = NULL;
11787   expr1.X_add_number = 1;
11788
11789   dbl = 0;
11790
11791   switch (mask)
11792     {
11793     default:
11794       abort ();
11795
11796     case M_DDIV_3:
11797       dbl = 1;
11798     case M_DIV_3:
11799       s = "mflo";
11800       goto do_div3;
11801     case M_DREM_3:
11802       dbl = 1;
11803     case M_REM_3:
11804       s = "mfhi";
11805     do_div3:
11806       start_noreorder ();
11807       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
11808       expr1.X_add_number = 2;
11809       macro_build (&expr1, "bnez", "x,p", yreg);
11810       macro_build (NULL, "break", "6", 7);
11811
11812       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
11813          since that causes an overflow.  We should do that as well,
11814          but I don't see how to do the comparisons without a temporary
11815          register.  */
11816       end_noreorder ();
11817       macro_build (NULL, s, "x", zreg);
11818       break;
11819
11820     case M_DIVU_3:
11821       s = "divu";
11822       s2 = "mflo";
11823       goto do_divu3;
11824     case M_REMU_3:
11825       s = "divu";
11826       s2 = "mfhi";
11827       goto do_divu3;
11828     case M_DDIVU_3:
11829       s = "ddivu";
11830       s2 = "mflo";
11831       goto do_divu3;
11832     case M_DREMU_3:
11833       s = "ddivu";
11834       s2 = "mfhi";
11835     do_divu3:
11836       start_noreorder ();
11837       macro_build (NULL, s, "0,x,y", xreg, yreg);
11838       expr1.X_add_number = 2;
11839       macro_build (&expr1, "bnez", "x,p", yreg);
11840       macro_build (NULL, "break", "6", 7);
11841       end_noreorder ();
11842       macro_build (NULL, s2, "x", zreg);
11843       break;
11844
11845     case M_DMUL:
11846       dbl = 1;
11847     case M_MUL:
11848       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
11849       macro_build (NULL, "mflo", "x", zreg);
11850       break;
11851
11852     case M_DSUBU_I:
11853       dbl = 1;
11854       goto do_subu;
11855     case M_SUBU_I:
11856     do_subu:
11857       if (imm_expr.X_op != O_constant)
11858         as_bad (_("Unsupported large constant"));
11859       imm_expr.X_add_number = -imm_expr.X_add_number;
11860       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
11861       break;
11862
11863     case M_SUBU_I_2:
11864       if (imm_expr.X_op != O_constant)
11865         as_bad (_("Unsupported large constant"));
11866       imm_expr.X_add_number = -imm_expr.X_add_number;
11867       macro_build (&imm_expr, "addiu", "x,k", xreg);
11868       break;
11869
11870     case M_DSUBU_I_2:
11871       if (imm_expr.X_op != O_constant)
11872         as_bad (_("Unsupported large constant"));
11873       imm_expr.X_add_number = -imm_expr.X_add_number;
11874       macro_build (&imm_expr, "daddiu", "y,j", yreg);
11875       break;
11876
11877     case M_BEQ:
11878       s = "cmp";
11879       s2 = "bteqz";
11880       goto do_branch;
11881     case M_BNE:
11882       s = "cmp";
11883       s2 = "btnez";
11884       goto do_branch;
11885     case M_BLT:
11886       s = "slt";
11887       s2 = "btnez";
11888       goto do_branch;
11889     case M_BLTU:
11890       s = "sltu";
11891       s2 = "btnez";
11892       goto do_branch;
11893     case M_BLE:
11894       s = "slt";
11895       s2 = "bteqz";
11896       goto do_reverse_branch;
11897     case M_BLEU:
11898       s = "sltu";
11899       s2 = "bteqz";
11900       goto do_reverse_branch;
11901     case M_BGE:
11902       s = "slt";
11903       s2 = "bteqz";
11904       goto do_branch;
11905     case M_BGEU:
11906       s = "sltu";
11907       s2 = "bteqz";
11908       goto do_branch;
11909     case M_BGT:
11910       s = "slt";
11911       s2 = "btnez";
11912       goto do_reverse_branch;
11913     case M_BGTU:
11914       s = "sltu";
11915       s2 = "btnez";
11916
11917     do_reverse_branch:
11918       tmp = xreg;
11919       xreg = yreg;
11920       yreg = tmp;
11921
11922     do_branch:
11923       macro_build (NULL, s, "x,y", xreg, yreg);
11924       macro_build (&offset_expr, s2, "p");
11925       break;
11926
11927     case M_BEQ_I:
11928       s = "cmpi";
11929       s2 = "bteqz";
11930       s3 = "x,U";
11931       goto do_branch_i;
11932     case M_BNE_I:
11933       s = "cmpi";
11934       s2 = "btnez";
11935       s3 = "x,U";
11936       goto do_branch_i;
11937     case M_BLT_I:
11938       s = "slti";
11939       s2 = "btnez";
11940       s3 = "x,8";
11941       goto do_branch_i;
11942     case M_BLTU_I:
11943       s = "sltiu";
11944       s2 = "btnez";
11945       s3 = "x,8";
11946       goto do_branch_i;
11947     case M_BLE_I:
11948       s = "slti";
11949       s2 = "btnez";
11950       s3 = "x,8";
11951       goto do_addone_branch_i;
11952     case M_BLEU_I:
11953       s = "sltiu";
11954       s2 = "btnez";
11955       s3 = "x,8";
11956       goto do_addone_branch_i;
11957     case M_BGE_I:
11958       s = "slti";
11959       s2 = "bteqz";
11960       s3 = "x,8";
11961       goto do_branch_i;
11962     case M_BGEU_I:
11963       s = "sltiu";
11964       s2 = "bteqz";
11965       s3 = "x,8";
11966       goto do_branch_i;
11967     case M_BGT_I:
11968       s = "slti";
11969       s2 = "bteqz";
11970       s3 = "x,8";
11971       goto do_addone_branch_i;
11972     case M_BGTU_I:
11973       s = "sltiu";
11974       s2 = "bteqz";
11975       s3 = "x,8";
11976
11977     do_addone_branch_i:
11978       if (imm_expr.X_op != O_constant)
11979         as_bad (_("Unsupported large constant"));
11980       ++imm_expr.X_add_number;
11981
11982     do_branch_i:
11983       macro_build (&imm_expr, s, s3, xreg);
11984       macro_build (&offset_expr, s2, "p");
11985       break;
11986
11987     case M_ABS:
11988       expr1.X_add_number = 0;
11989       macro_build (&expr1, "slti", "x,8", yreg);
11990       if (xreg != yreg)
11991         move_register (xreg, yreg);
11992       expr1.X_add_number = 2;
11993       macro_build (&expr1, "bteqz", "p");
11994       macro_build (NULL, "neg", "x,w", xreg, xreg);
11995     }
11996 }
11997
11998 /* Assemble an instruction into its binary format.  If the instruction
11999    is a macro, set imm_expr, imm2_expr and offset_expr to the values
12000    associated with "I", "+I" and "A" operands respectively.  Otherwise
12001    store the value of the relocatable field (if any) in offset_expr.
12002    In both cases set offset_reloc to the relocation operators applied
12003    to offset_expr.  */
12004
12005 static void
12006 mips_ip (char *str, struct mips_cl_insn *ip)
12007 {
12008   bfd_boolean wrong_delay_slot_insns = FALSE;
12009   bfd_boolean need_delay_slot_ok = TRUE;
12010   struct mips_opcode *firstinsn = NULL;
12011   const struct mips_opcode *past;
12012   struct hash_control *hash;
12013   const char *args;
12014   char c = 0;
12015   struct mips_opcode *insn;
12016   long opend;
12017   char *name;
12018   char *dot;
12019   char format;
12020   long end;
12021   const struct mips_operand *operand;
12022   struct mips_arg_info arg;
12023   struct mips_operand_token *tokens;
12024   bfd_boolean optional_reg;
12025
12026   insn_error = NULL;
12027
12028   if (mips_opts.micromips)
12029     {
12030       hash = micromips_op_hash;
12031       past = &micromips_opcodes[bfd_micromips_num_opcodes];
12032     }
12033   else
12034     {
12035       hash = op_hash;
12036       past = &mips_opcodes[NUMOPCODES];
12037     }
12038   forced_insn_length = 0;
12039   insn = NULL;
12040
12041   /* We first try to match an instruction up to a space or to the end.  */
12042   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
12043     continue;
12044
12045   /* Make a copy of the instruction so that we can fiddle with it.  */
12046   name = alloca (end + 1);
12047   memcpy (name, str, end);
12048   name[end] = '\0';
12049
12050   for (;;)
12051     {
12052       insn = (struct mips_opcode *) hash_find (hash, name);
12053
12054       if (insn != NULL || !mips_opts.micromips)
12055         break;
12056       if (forced_insn_length)
12057         break;
12058
12059       /* See if there's an instruction size override suffix,
12060          either `16' or `32', at the end of the mnemonic proper,
12061          that defines the operation, i.e. before the first `.'
12062          character if any.  Strip it and retry.  */
12063       dot = strchr (name, '.');
12064       opend = dot != NULL ? dot - name : end;
12065       if (opend < 3)
12066         break;
12067       if (name[opend - 2] == '1' && name[opend - 1] == '6')
12068         forced_insn_length = 2;
12069       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
12070         forced_insn_length = 4;
12071       else
12072         break;
12073       memcpy (name + opend - 2, name + opend, end - opend + 1);
12074     }
12075   if (insn == NULL)
12076     {
12077       insn_error = _("Unrecognized opcode");
12078       return;
12079     }
12080
12081   if (strcmp (name, "li.s") == 0)
12082     format = 'f';
12083   else if (strcmp (name, "li.d") == 0)
12084     format = 'd';
12085   else
12086     format = 0;
12087   tokens = mips_parse_arguments (str + end, format);
12088   if (!tokens)
12089     return;
12090
12091   /* For microMIPS instructions placed in a fixed-length branch delay slot
12092      we make up to two passes over the relevant fragment of the opcode
12093      table.  First we try instructions that meet the delay slot's length
12094      requirement.  If none matched, then we retry with the remaining ones
12095      and if one matches, then we use it and then issue an appropriate
12096      warning later on.  */
12097   for (;;)
12098     {
12099       bfd_boolean delay_slot_ok;
12100       bfd_boolean size_ok;
12101       bfd_boolean ok;
12102       bfd_boolean more_alts;
12103
12104       gas_assert (strcmp (insn->name, name) == 0);
12105
12106       ok = is_opcode_valid (insn);
12107       size_ok = is_size_valid (insn);
12108       delay_slot_ok = is_delay_slot_valid (insn);
12109       if (!delay_slot_ok && !wrong_delay_slot_insns)
12110         {
12111           firstinsn = insn;
12112           wrong_delay_slot_insns = TRUE;
12113         }
12114       more_alts = (insn + 1 < past
12115                    && strcmp (insn[0].name, insn[1].name) == 0);
12116       if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
12117         {
12118           static char buf[256];
12119
12120           if (more_alts)
12121             {
12122               ++insn;
12123               continue;
12124             }
12125           if (wrong_delay_slot_insns && need_delay_slot_ok)
12126             {
12127               gas_assert (firstinsn);
12128               need_delay_slot_ok = FALSE;
12129               past = insn + 1;
12130               insn = firstinsn;
12131               continue;
12132             }
12133
12134           obstack_free (&mips_operand_tokens, tokens);
12135           if (insn_error)
12136             return;
12137
12138           if (!ok)
12139             sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
12140                      mips_cpu_info_from_arch (mips_opts.arch)->name,
12141                      mips_cpu_info_from_isa (mips_opts.isa)->name);
12142           else if (mips_opts.insn32)
12143             sprintf (buf, _("Opcode not supported in the `insn32' mode"));
12144           else
12145             sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
12146                      8 * forced_insn_length);
12147           insn_error = buf;
12148
12149           return;
12150         }
12151
12152       imm_expr.X_op = O_absent;
12153       imm2_expr.X_op = O_absent;
12154       offset_expr.X_op = O_absent;
12155       offset_reloc[0] = BFD_RELOC_UNUSED;
12156       offset_reloc[1] = BFD_RELOC_UNUSED;
12157       offset_reloc[2] = BFD_RELOC_UNUSED;
12158
12159       create_insn (ip, insn);
12160       insn_error = NULL;
12161       memset (&arg, 0, sizeof (arg));
12162       arg.insn = ip;
12163       arg.token = tokens;
12164       arg.argnum = 1;
12165       arg.last_regno = ILLEGAL_REG;
12166       arg.dest_regno = ILLEGAL_REG;
12167       arg.soft_match = (more_alts
12168                         || (wrong_delay_slot_insns && need_delay_slot_ok));
12169       for (args = insn->args;; ++args)
12170         {
12171           if (arg.token->type == OT_END)
12172             {
12173               /* Handle unary instructions in which only one operand is given.
12174                  The source is then the same as the destination.  */
12175               if (arg.opnum == 1 && *args == ',')
12176                 switch (args[1])
12177                   {
12178                   case 'r':
12179                   case 'v':
12180                   case 'w':
12181                   case 'W':
12182                   case 'V':
12183                     arg.token = tokens;
12184                     arg.argnum = 1;
12185                     continue;
12186                   }
12187
12188               /* Treat elided base registers as $0.  */
12189               if (strcmp (args, "(b)") == 0)
12190                 args += 3;
12191
12192               /* Fail the match if there were too few operands.  */
12193               if (*args)
12194                 break;
12195
12196               /* Successful match.  */
12197               if (arg.dest_regno == arg.last_regno
12198                   && strncmp (ip->insn_mo->name, "jalr", 4) == 0)
12199                 {
12200                   if (arg.opnum == 2)
12201                     as_bad (_("Source and destination must be different"));
12202                   else if (arg.last_regno == 31)
12203                     as_bad (_("A destination register must be supplied"));
12204                 }
12205               check_completed_insn (&arg);
12206               obstack_free (&mips_operand_tokens, tokens);
12207               return;
12208             }
12209
12210           /* Fail the match if the line has too many operands.   */
12211           if (*args == 0)
12212             break;
12213
12214           /* Handle characters that need to match exactly.  */
12215           if (*args == '(' || *args == ')' || *args == ',')
12216             {
12217               if (match_char (&arg, *args))
12218                 continue;
12219               break;
12220             }
12221
12222           /* Handle special macro operands.  Work out the properties of
12223              other operands.  */
12224           arg.opnum += 1;
12225           arg.lax_max = FALSE;
12226           optional_reg = FALSE;
12227           switch (*args)
12228             {
12229             case '+':
12230               switch (args[1])
12231                 {
12232                 case '1':
12233                 case '2':
12234                 case '3':
12235                 case '4':
12236                 case 'B':
12237                 case 'C':
12238                 case 'F':
12239                 case 'G':
12240                 case 'H':
12241                 case 'J':
12242                 case 'Q':
12243                 case 'S':
12244                 case 's':
12245                   /* If these integer forms come last, there is no other
12246                      form of the instruction that could match.  Prefer to
12247                      give detailed error messages where possible.  */
12248                   if (args[2] == 0)
12249                     arg.soft_match = FALSE;
12250                   break;
12251
12252                 case 'I':
12253                   /* "+I" is like "I", except that imm2_expr is used.  */
12254                   if (match_const_int (&arg, &imm2_expr.X_add_number, 0))
12255                     imm2_expr.X_op = O_constant;
12256                   else
12257                     insn_error = _("absolute expression required");
12258                   if (HAVE_32BIT_GPRS)
12259                     normalize_constant_expr (&imm2_expr);
12260                   ++args;
12261                   continue;
12262
12263                 case 'i':
12264                   *offset_reloc = BFD_RELOC_MIPS_JMP;
12265                   break;
12266                 }
12267               break;
12268
12269             case '\'':
12270             case ':':
12271             case '@':
12272             case '^':
12273             case '$':
12274             case '\\':
12275             case '%':
12276             case '|':
12277             case '0':
12278             case '1':
12279             case '2':
12280             case '3':
12281             case '4':
12282             case '5':
12283             case '6':
12284             case '8':
12285             case 'B':
12286             case 'C':
12287             case 'J':
12288             case 'O':
12289             case 'P':
12290             case 'Q':
12291             case 'c':
12292             case 'h':
12293             case 'q':
12294               /* If these integer forms come last, there is no other
12295                  form of the instruction that could match.  Prefer to
12296                  give detailed error messages where possible.  */
12297               if (args[1] == 0)
12298                 arg.soft_match = FALSE;
12299               break;
12300
12301             case 'r':
12302             case 'v':
12303             case 'w':
12304             case 'W':
12305             case 'V':
12306               /* We have already matched a comma by this point, so the register
12307                  is only optional if there is another operand to come.  */
12308               gas_assert (arg.opnum == 2);
12309               optional_reg = (args[1] == ',');
12310               break;
12311
12312             case 'I':
12313               if (match_const_int (&arg, &imm_expr.X_add_number, 0))
12314                 imm_expr.X_op = O_constant;
12315               else
12316                 insn_error = _("absolute expression required");
12317               if (HAVE_32BIT_GPRS)
12318                 normalize_constant_expr (&imm_expr);
12319               continue;
12320
12321             case 'A':
12322               if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
12323                 {
12324                   /* Assume that the offset has been elided and that what
12325                      we saw was a base register.  The match will fail later
12326                      if that assumption turns out to be wrong.  */
12327                   offset_expr.X_op = O_constant;
12328                   offset_expr.X_add_number = 0;
12329                 }
12330               else if (match_expression (&arg, &offset_expr, offset_reloc))
12331                 normalize_address_expr (&offset_expr);
12332               else
12333                 insn_error = _("absolute expression required");
12334               continue;
12335
12336             case 'F':
12337               if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12338                                          8, TRUE))
12339                 insn_error = _("floating-point expression required");
12340               continue;
12341
12342             case 'L':
12343               if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12344                                          8, FALSE))
12345                 insn_error = _("floating-point expression required");
12346               continue;
12347
12348             case 'f':
12349               if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12350                                          4, TRUE))
12351                 insn_error = _("floating-point expression required");
12352               continue;
12353
12354             case 'l':
12355               if (!match_float_constant (&arg, &imm_expr, &offset_expr,
12356                                          4, FALSE))
12357                 insn_error = _("floating-point expression required");
12358               continue;
12359
12360               /* ??? This is the traditional behavior, but is flaky if
12361                  there are alternative versions of the same instruction
12362                  for different subarchitectures.  The next alternative
12363                  might not be suitable.  */
12364             case 'j':
12365               /* For compatibility with older assemblers, we accept
12366                  0x8000-0xffff as signed 16-bit numbers when only
12367                  signed numbers are allowed.  */
12368               arg.lax_max = !more_alts;
12369             case 'i':
12370               /* Only accept non-constant operands if this is the
12371                  final alternative.  Later alternatives might include
12372                  a macro implementation.  */
12373               arg.allow_nonconst = !more_alts;
12374               break;
12375
12376             case 'u':
12377               /* There are no macro implementations for out-of-range values.  */
12378               arg.allow_nonconst = TRUE;
12379               break;
12380
12381             case 'o':
12382               /* There should always be a macro implementation.  */
12383               arg.allow_nonconst = FALSE;
12384               break;
12385
12386             case 'p':
12387               *offset_reloc = BFD_RELOC_16_PCREL_S2;
12388               break;
12389
12390             case 'a':
12391               *offset_reloc = BFD_RELOC_MIPS_JMP;
12392               break;
12393
12394             case 'm':
12395               gas_assert (mips_opts.micromips);
12396               c = args[1];
12397               switch (c)
12398                 {
12399                 case 't':
12400                 case 'c':
12401                 case 'e':
12402                   /* We have already matched a comma by this point,
12403                      so the register is only optional if there is another
12404                      operand to come.  */
12405                   gas_assert (arg.opnum == 2);
12406                   optional_reg = (args[2] == ',');
12407                   break;
12408
12409                 case 'D':
12410                 case 'E':
12411                   if (!forced_insn_length)
12412                     *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12413                   else if (c == 'D')
12414                     *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12415                   else
12416                     *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
12417                   break;
12418                 }
12419               break;
12420             }
12421
12422           operand = (mips_opts.micromips
12423                      ? decode_micromips_operand (args)
12424                      : decode_mips_operand (args));
12425           if (!operand)
12426             abort ();
12427
12428           if (optional_reg
12429               && (arg.token[0].type != OT_REG
12430                   || arg.token[1].type == OT_END))
12431             {
12432               /* Assume that the register has been elided and is the
12433                  same as the first operand.  */
12434               arg.token = tokens;
12435               arg.argnum = 1;
12436             }
12437
12438           if (!match_operand (&arg, operand))
12439             break;
12440
12441           /* Skip prefixes.  */
12442           if (*args == '+' || *args == 'm')
12443             args++;
12444
12445           continue;
12446         }
12447       /* Args don't match.  */
12448       insn_error = _("Illegal operands");
12449       if (more_alts)
12450         {
12451           ++insn;
12452           continue;
12453         }
12454       if (wrong_delay_slot_insns && need_delay_slot_ok)
12455         {
12456           gas_assert (firstinsn);
12457           need_delay_slot_ok = FALSE;
12458           past = insn + 1;
12459           insn = firstinsn;
12460           continue;
12461         }
12462       obstack_free (&mips_operand_tokens, tokens);
12463       return;
12464     }
12465 }
12466
12467 /* As for mips_ip, but used when assembling MIPS16 code.
12468    Also set forced_insn_length to the resulting instruction size in
12469    bytes if the user explicitly requested a small or extended instruction.  */
12470
12471 static void
12472 mips16_ip (char *str, struct mips_cl_insn *ip)
12473 {
12474   char *s;
12475   const char *args;
12476   struct mips_opcode *insn;
12477   const struct mips_operand *operand;
12478   const struct mips_operand *ext_operand;
12479   struct mips_arg_info arg;
12480   struct mips_operand_token *tokens;
12481   bfd_boolean optional_reg;
12482
12483   insn_error = NULL;
12484
12485   forced_insn_length = 0;
12486
12487   for (s = str; ISLOWER (*s); ++s)
12488     ;
12489   switch (*s)
12490     {
12491     case '\0':
12492       break;
12493
12494     case ' ':
12495       *s++ = '\0';
12496       break;
12497
12498     case '.':
12499       if (s[1] == 't' && s[2] == ' ')
12500         {
12501           *s = '\0';
12502           forced_insn_length = 2;
12503           s += 3;
12504           break;
12505         }
12506       else if (s[1] == 'e' && s[2] == ' ')
12507         {
12508           *s = '\0';
12509           forced_insn_length = 4;
12510           s += 3;
12511           break;
12512         }
12513       /* Fall through.  */
12514     default:
12515       insn_error = _("unknown opcode");
12516       return;
12517     }
12518
12519   if (mips_opts.noautoextend && !forced_insn_length)
12520     forced_insn_length = 2;
12521
12522   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
12523     {
12524       insn_error = _("unrecognized opcode");
12525       return;
12526     }
12527
12528   tokens = mips_parse_arguments (s, 0);
12529   if (!tokens)
12530     return;
12531
12532   for (;;)
12533     {
12534       bfd_boolean ok;
12535       bfd_boolean more_alts;
12536       char relax_char;
12537
12538       gas_assert (strcmp (insn->name, str) == 0);
12539
12540       ok = is_opcode_valid_16 (insn);
12541       more_alts = (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
12542                    && strcmp (insn[0].name, insn[1].name) == 0);
12543       if (! ok)
12544         {
12545           if (more_alts)
12546             {
12547               ++insn;
12548               continue;
12549             }
12550           else
12551             {
12552               if (!insn_error)
12553                 {
12554                   static char buf[100];
12555                   sprintf (buf,
12556                            _("Opcode not supported on this processor: %s (%s)"),
12557                            mips_cpu_info_from_arch (mips_opts.arch)->name,
12558                            mips_cpu_info_from_isa (mips_opts.isa)->name);
12559                   insn_error = buf;
12560                 }
12561               obstack_free (&mips_operand_tokens, tokens);
12562               return;
12563             }
12564         }
12565
12566       create_insn (ip, insn);
12567       imm_expr.X_op = O_absent;
12568       imm2_expr.X_op = O_absent;
12569       offset_expr.X_op = O_absent;
12570       offset_reloc[0] = BFD_RELOC_UNUSED;
12571       offset_reloc[1] = BFD_RELOC_UNUSED;
12572       offset_reloc[2] = BFD_RELOC_UNUSED;
12573       relax_char = 0;
12574
12575       memset (&arg, 0, sizeof (arg));
12576       arg.insn = ip;
12577       arg.token = tokens;
12578       arg.argnum = 1;
12579       arg.last_regno = ILLEGAL_REG;
12580       arg.dest_regno = ILLEGAL_REG;
12581       arg.soft_match = more_alts;
12582       relax_char = 0;
12583       for (args = insn->args; 1; ++args)
12584         {
12585           int c;
12586
12587           if (arg.token->type == OT_END)
12588             {
12589               offsetT value;
12590
12591               /* Handle unary instructions in which only one operand is given.
12592                  The source is then the same as the destination.  */
12593               if (arg.opnum == 1 && *args == ',')
12594                 switch (args[1])
12595                   {
12596                   case 'v':
12597                   case 'w':
12598                     arg.token = tokens;
12599                     arg.argnum = 1;
12600                     continue;
12601                   }
12602
12603               /* Fail the match if there were too few operands.  */
12604               if (*args)
12605                 break;
12606
12607               /* Successful match.  Stuff the immediate value in now, if
12608                  we can.  */
12609               if (insn->pinfo == INSN_MACRO)
12610                 {
12611                   gas_assert (relax_char == 0);
12612                   gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
12613                 }
12614               else if (relax_char
12615                        && offset_expr.X_op == O_constant
12616                        && calculate_reloc (*offset_reloc,
12617                                            offset_expr.X_add_number,
12618                                            &value))
12619                 {
12620                   mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
12621                                 forced_insn_length, &ip->insn_opcode);
12622                   offset_expr.X_op = O_absent;
12623                   *offset_reloc = BFD_RELOC_UNUSED;
12624                 }
12625               else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
12626                 {
12627                   if (forced_insn_length == 2)
12628                     as_bad (_("invalid unextended operand value"));
12629                   forced_insn_length = 4;
12630                   ip->insn_opcode |= MIPS16_EXTEND;
12631                 }
12632               else if (relax_char)
12633                 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
12634
12635               check_completed_insn (&arg);
12636               obstack_free (&mips_operand_tokens, tokens);
12637               return;
12638             }
12639
12640           /* Fail the match if the line has too many operands.   */
12641           if (*args == 0)
12642             break;
12643
12644           /* Handle characters that need to match exactly.  */
12645           if (*args == '(' || *args == ')' || *args == ',')
12646             {
12647               if (match_char (&arg, *args))
12648                 continue;
12649               break;
12650             }
12651
12652           arg.opnum += 1;
12653           optional_reg = FALSE;
12654           c = *args;
12655           switch (c)
12656             {
12657             case 'v':
12658             case 'w':
12659               optional_reg = (args[1] == ',');
12660               break;
12661
12662             case 'p':
12663             case 'q':
12664             case 'A':
12665             case 'B':
12666             case 'E':
12667               relax_char = c;
12668               break;
12669
12670             case 'I':
12671               if (match_const_int (&arg, &imm_expr.X_add_number, 0))
12672                 imm_expr.X_op = O_constant;
12673               else
12674                 insn_error = _("absolute expression required");
12675               if (HAVE_32BIT_GPRS)
12676                 normalize_constant_expr (&imm_expr);
12677               continue;
12678
12679             case 'a':
12680             case 'i':
12681               *offset_reloc = BFD_RELOC_MIPS16_JMP;
12682               ip->insn_opcode <<= 16;
12683               break;
12684             }
12685
12686           operand = decode_mips16_operand (c, FALSE);
12687           if (!operand)
12688             abort ();
12689
12690           /* '6' is a special case.  It is used for BREAK and SDBBP,
12691              whose operands are only meaningful to the software that decodes
12692              them.  This means that there is no architectural reason why
12693              they cannot be prefixed by EXTEND, but in practice,
12694              exception handlers will only look at the instruction
12695              itself.  We therefore allow '6' to be extended when
12696              disassembling but not when assembling.  */
12697           if (operand->type != OP_PCREL && c != '6')
12698             {
12699               ext_operand = decode_mips16_operand (c, TRUE);
12700               if (operand != ext_operand)
12701                 {
12702                   if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
12703                     {
12704                       offset_expr.X_op = O_constant;
12705                       offset_expr.X_add_number = 0;
12706                       relax_char = c;
12707                       continue;
12708                     }
12709
12710                   /* We need the OT_INTEGER check because some MIPS16
12711                      immediate variants are listed before the register ones.  */
12712                   if (arg.token->type != OT_INTEGER
12713                       || !match_expression (&arg, &offset_expr, offset_reloc))
12714                     break;
12715
12716                   /* '8' is used for SLTI(U) and has traditionally not
12717                      been allowed to take relocation operators.  */
12718                   if (offset_reloc[0] != BFD_RELOC_UNUSED
12719                       && (ext_operand->size != 16 || c == '8'))
12720                     break;
12721
12722                   relax_char = c;
12723                   continue;
12724                 }
12725             }
12726
12727           if (optional_reg
12728               && (arg.token[0].type != OT_REG
12729                   || arg.token[1].type == OT_END))
12730             {
12731               /* Assume that the register has been elided and is the
12732                  same as the first operand.  */
12733               arg.token = tokens;
12734               arg.argnum = 1;
12735             }
12736
12737           if (!match_operand (&arg, operand))
12738             break;
12739           continue;
12740         }
12741
12742       /* Args don't match.  */
12743       if (more_alts)
12744         {
12745           ++insn;
12746           continue;
12747         }
12748
12749       insn_error = _("illegal operands");
12750
12751       obstack_free (&mips_operand_tokens, tokens);
12752       return;
12753     }
12754 }
12755
12756 /* This structure holds information we know about a mips16 immediate
12757    argument type.  */
12758
12759 struct mips16_immed_operand
12760 {
12761   /* The type code used in the argument string in the opcode table.  */
12762   int type;
12763   /* The number of bits in the short form of the opcode.  */
12764   int nbits;
12765   /* The number of bits in the extended form of the opcode.  */
12766   int extbits;
12767   /* The amount by which the short form is shifted when it is used;
12768      for example, the sw instruction has a shift count of 2.  */
12769   int shift;
12770   /* The amount by which the short form is shifted when it is stored
12771      into the instruction code.  */
12772   int op_shift;
12773   /* Non-zero if the short form is unsigned.  */
12774   int unsp;
12775   /* Non-zero if the extended form is unsigned.  */
12776   int extu;
12777   /* Non-zero if the value is PC relative.  */
12778   int pcrel;
12779 };
12780
12781 /* The mips16 immediate operand types.  */
12782
12783 static const struct mips16_immed_operand mips16_immed_operands[] =
12784 {
12785   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
12786   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
12787   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
12788   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
12789   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
12790   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
12791   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
12792   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
12793   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
12794   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
12795   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
12796   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
12797   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
12798   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
12799   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
12800   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
12801   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
12802   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
12803   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
12804   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
12805   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
12806 };
12807
12808 #define MIPS16_NUM_IMMED \
12809   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
12810
12811 /* Marshal immediate value VAL for an extended MIPS16 instruction.
12812    NBITS is the number of significant bits in VAL.  */
12813
12814 static unsigned long
12815 mips16_immed_extend (offsetT val, unsigned int nbits)
12816 {
12817   int extval;
12818   if (nbits == 16)
12819     {
12820       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
12821       val &= 0x1f;
12822     }
12823   else if (nbits == 15)
12824     {
12825       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
12826       val &= 0xf;
12827     }
12828   else
12829     {
12830       extval = ((val & 0x1f) << 6) | (val & 0x20);
12831       val = 0;
12832     }
12833   return (extval << 16) | val;
12834 }
12835
12836 /* Install immediate value VAL into MIPS16 instruction *INSN,
12837    extending it if necessary.  The instruction in *INSN may
12838    already be extended.
12839
12840    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
12841    if none.  In the former case, VAL is a 16-bit number with no
12842    defined signedness.
12843
12844    TYPE is the type of the immediate field.  USER_INSN_LENGTH
12845    is the length that the user requested, or 0 if none.  */
12846
12847 static void
12848 mips16_immed (char *file, unsigned int line, int type,
12849               bfd_reloc_code_real_type reloc, offsetT val,
12850               unsigned int user_insn_length, unsigned long *insn)
12851 {
12852   const struct mips16_immed_operand *op;
12853   int mintiny, maxtiny;
12854
12855   op = mips16_immed_operands;
12856   while (op->type != type)
12857     {
12858       ++op;
12859       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12860     }
12861
12862   if (op->unsp)
12863     {
12864       if (type == '<' || type == '>' || type == '[' || type == ']')
12865         {
12866           mintiny = 1;
12867           maxtiny = 1 << op->nbits;
12868         }
12869       else
12870         {
12871           mintiny = 0;
12872           maxtiny = (1 << op->nbits) - 1;
12873         }
12874       if (reloc != BFD_RELOC_UNUSED)
12875         val &= 0xffff;
12876     }
12877   else
12878     {
12879       mintiny = - (1 << (op->nbits - 1));
12880       maxtiny = (1 << (op->nbits - 1)) - 1;
12881       if (reloc != BFD_RELOC_UNUSED)
12882         val = SEXT_16BIT (val);
12883     }
12884
12885   /* Branch offsets have an implicit 0 in the lowest bit.  */
12886   if (type == 'p' || type == 'q')
12887     val /= 2;
12888
12889   if ((val & ((1 << op->shift) - 1)) != 0
12890       || val < (mintiny << op->shift)
12891       || val > (maxtiny << op->shift))
12892     {
12893       /* We need an extended instruction.  */
12894       if (user_insn_length == 2)
12895         as_bad_where (file, line, _("invalid unextended operand value"));
12896       else
12897         *insn |= MIPS16_EXTEND;
12898     }
12899   else if (user_insn_length == 4)
12900     {
12901       /* The operand doesn't force an unextended instruction to be extended.
12902          Warn if the user wanted an extended instruction anyway.  */
12903       *insn |= MIPS16_EXTEND;
12904       as_warn_where (file, line,
12905                      _("extended operand requested but not required"));
12906     }
12907
12908   if (mips16_opcode_length (*insn) == 2)
12909     {
12910       int insnval;
12911
12912       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
12913       insnval <<= op->op_shift;
12914       *insn |= insnval;
12915     }
12916   else
12917     {
12918       long minext, maxext;
12919
12920       if (reloc == BFD_RELOC_UNUSED)
12921         {
12922           if (op->extu)
12923             {
12924               minext = 0;
12925               maxext = (1 << op->extbits) - 1;
12926             }
12927           else
12928             {
12929               minext = - (1 << (op->extbits - 1));
12930               maxext = (1 << (op->extbits - 1)) - 1;
12931             }
12932           if (val < minext || val > maxext)
12933             as_bad_where (file, line,
12934                           _("operand value out of range for instruction"));
12935         }
12936
12937       *insn |= mips16_immed_extend (val, op->extbits);
12938     }
12939 }
12940 \f
12941 struct percent_op_match
12942 {
12943   const char *str;
12944   bfd_reloc_code_real_type reloc;
12945 };
12946
12947 static const struct percent_op_match mips_percent_op[] =
12948 {
12949   {"%lo", BFD_RELOC_LO16},
12950   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
12951   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
12952   {"%call16", BFD_RELOC_MIPS_CALL16},
12953   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
12954   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
12955   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
12956   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
12957   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
12958   {"%got", BFD_RELOC_MIPS_GOT16},
12959   {"%gp_rel", BFD_RELOC_GPREL16},
12960   {"%half", BFD_RELOC_16},
12961   {"%highest", BFD_RELOC_MIPS_HIGHEST},
12962   {"%higher", BFD_RELOC_MIPS_HIGHER},
12963   {"%neg", BFD_RELOC_MIPS_SUB},
12964   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
12965   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
12966   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
12967   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
12968   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
12969   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
12970   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
12971   {"%hi", BFD_RELOC_HI16_S}
12972 };
12973
12974 static const struct percent_op_match mips16_percent_op[] =
12975 {
12976   {"%lo", BFD_RELOC_MIPS16_LO16},
12977   {"%gprel", BFD_RELOC_MIPS16_GPREL},
12978   {"%got", BFD_RELOC_MIPS16_GOT16},
12979   {"%call16", BFD_RELOC_MIPS16_CALL16},
12980   {"%hi", BFD_RELOC_MIPS16_HI16_S},
12981   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
12982   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
12983   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
12984   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
12985   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
12986   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
12987   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
12988 };
12989
12990
12991 /* Return true if *STR points to a relocation operator.  When returning true,
12992    move *STR over the operator and store its relocation code in *RELOC.
12993    Leave both *STR and *RELOC alone when returning false.  */
12994
12995 static bfd_boolean
12996 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
12997 {
12998   const struct percent_op_match *percent_op;
12999   size_t limit, i;
13000
13001   if (mips_opts.mips16)
13002     {
13003       percent_op = mips16_percent_op;
13004       limit = ARRAY_SIZE (mips16_percent_op);
13005     }
13006   else
13007     {
13008       percent_op = mips_percent_op;
13009       limit = ARRAY_SIZE (mips_percent_op);
13010     }
13011
13012   for (i = 0; i < limit; i++)
13013     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
13014       {
13015         int len = strlen (percent_op[i].str);
13016
13017         if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13018           continue;
13019
13020         *str += strlen (percent_op[i].str);
13021         *reloc = percent_op[i].reloc;
13022
13023         /* Check whether the output BFD supports this relocation.
13024            If not, issue an error and fall back on something safe.  */
13025         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
13026           {
13027             as_bad (_("relocation %s isn't supported by the current ABI"),
13028                     percent_op[i].str);
13029             *reloc = BFD_RELOC_UNUSED;
13030           }
13031         return TRUE;
13032       }
13033   return FALSE;
13034 }
13035
13036
13037 /* Parse string STR as a 16-bit relocatable operand.  Store the
13038    expression in *EP and the relocations in the array starting
13039    at RELOC.  Return the number of relocation operators used.
13040
13041    On exit, EXPR_END points to the first character after the expression.  */
13042
13043 static size_t
13044 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13045                        char *str)
13046 {
13047   bfd_reloc_code_real_type reversed_reloc[3];
13048   size_t reloc_index, i;
13049   int crux_depth, str_depth;
13050   char *crux;
13051
13052   /* Search for the start of the main expression, recoding relocations
13053      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
13054      of the main expression and with CRUX_DEPTH containing the number
13055      of open brackets at that point.  */
13056   reloc_index = -1;
13057   str_depth = 0;
13058   do
13059     {
13060       reloc_index++;
13061       crux = str;
13062       crux_depth = str_depth;
13063
13064       /* Skip over whitespace and brackets, keeping count of the number
13065          of brackets.  */
13066       while (*str == ' ' || *str == '\t' || *str == '(')
13067         if (*str++ == '(')
13068           str_depth++;
13069     }
13070   while (*str == '%'
13071          && reloc_index < (HAVE_NEWABI ? 3 : 1)
13072          && parse_relocation (&str, &reversed_reloc[reloc_index]));
13073
13074   my_getExpression (ep, crux);
13075   str = expr_end;
13076
13077   /* Match every open bracket.  */
13078   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
13079     if (*str++ == ')')
13080       crux_depth--;
13081
13082   if (crux_depth > 0)
13083     as_bad (_("unclosed '('"));
13084
13085   expr_end = str;
13086
13087   if (reloc_index != 0)
13088     {
13089       prev_reloc_op_frag = frag_now;
13090       for (i = 0; i < reloc_index; i++)
13091         reloc[i] = reversed_reloc[reloc_index - 1 - i];
13092     }
13093
13094   return reloc_index;
13095 }
13096
13097 static void
13098 my_getExpression (expressionS *ep, char *str)
13099 {
13100   char *save_in;
13101
13102   save_in = input_line_pointer;
13103   input_line_pointer = str;
13104   expression (ep);
13105   expr_end = input_line_pointer;
13106   input_line_pointer = save_in;
13107 }
13108
13109 char *
13110 md_atof (int type, char *litP, int *sizeP)
13111 {
13112   return ieee_md_atof (type, litP, sizeP, target_big_endian);
13113 }
13114
13115 void
13116 md_number_to_chars (char *buf, valueT val, int n)
13117 {
13118   if (target_big_endian)
13119     number_to_chars_bigendian (buf, val, n);
13120   else
13121     number_to_chars_littleendian (buf, val, n);
13122 }
13123 \f
13124 static int support_64bit_objects(void)
13125 {
13126   const char **list, **l;
13127   int yes;
13128
13129   list = bfd_target_list ();
13130   for (l = list; *l != NULL; l++)
13131     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
13132         || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
13133       break;
13134   yes = (*l != NULL);
13135   free (list);
13136   return yes;
13137 }
13138
13139 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
13140    NEW_VALUE.  Warn if another value was already specified.  Note:
13141    we have to defer parsing the -march and -mtune arguments in order
13142    to handle 'from-abi' correctly, since the ABI might be specified
13143    in a later argument.  */
13144
13145 static void
13146 mips_set_option_string (const char **string_ptr, const char *new_value)
13147 {
13148   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
13149     as_warn (_("A different %s was already specified, is now %s"),
13150              string_ptr == &mips_arch_string ? "-march" : "-mtune",
13151              new_value);
13152
13153   *string_ptr = new_value;
13154 }
13155
13156 int
13157 md_parse_option (int c, char *arg)
13158 {
13159   unsigned int i;
13160
13161   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
13162     if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
13163       {
13164         file_ase_explicit |= mips_set_ase (&mips_ases[i],
13165                                            c == mips_ases[i].option_on);
13166         return 1;
13167       }
13168
13169   switch (c)
13170     {
13171     case OPTION_CONSTRUCT_FLOATS:
13172       mips_disable_float_construction = 0;
13173       break;
13174
13175     case OPTION_NO_CONSTRUCT_FLOATS:
13176       mips_disable_float_construction = 1;
13177       break;
13178
13179     case OPTION_TRAP:
13180       mips_trap = 1;
13181       break;
13182
13183     case OPTION_BREAK:
13184       mips_trap = 0;
13185       break;
13186
13187     case OPTION_EB:
13188       target_big_endian = 1;
13189       break;
13190
13191     case OPTION_EL:
13192       target_big_endian = 0;
13193       break;
13194
13195     case 'O':
13196       if (arg == NULL)
13197         mips_optimize = 1;
13198       else if (arg[0] == '0')
13199         mips_optimize = 0;
13200       else if (arg[0] == '1')
13201         mips_optimize = 1;
13202       else
13203         mips_optimize = 2;
13204       break;
13205
13206     case 'g':
13207       if (arg == NULL)
13208         mips_debug = 2;
13209       else
13210         mips_debug = atoi (arg);
13211       break;
13212
13213     case OPTION_MIPS1:
13214       file_mips_isa = ISA_MIPS1;
13215       break;
13216
13217     case OPTION_MIPS2:
13218       file_mips_isa = ISA_MIPS2;
13219       break;
13220
13221     case OPTION_MIPS3:
13222       file_mips_isa = ISA_MIPS3;
13223       break;
13224
13225     case OPTION_MIPS4:
13226       file_mips_isa = ISA_MIPS4;
13227       break;
13228
13229     case OPTION_MIPS5:
13230       file_mips_isa = ISA_MIPS5;
13231       break;
13232
13233     case OPTION_MIPS32:
13234       file_mips_isa = ISA_MIPS32;
13235       break;
13236
13237     case OPTION_MIPS32R2:
13238       file_mips_isa = ISA_MIPS32R2;
13239       break;
13240
13241     case OPTION_MIPS64R2:
13242       file_mips_isa = ISA_MIPS64R2;
13243       break;
13244
13245     case OPTION_MIPS64:
13246       file_mips_isa = ISA_MIPS64;
13247       break;
13248
13249     case OPTION_MTUNE:
13250       mips_set_option_string (&mips_tune_string, arg);
13251       break;
13252
13253     case OPTION_MARCH:
13254       mips_set_option_string (&mips_arch_string, arg);
13255       break;
13256
13257     case OPTION_M4650:
13258       mips_set_option_string (&mips_arch_string, "4650");
13259       mips_set_option_string (&mips_tune_string, "4650");
13260       break;
13261
13262     case OPTION_NO_M4650:
13263       break;
13264
13265     case OPTION_M4010:
13266       mips_set_option_string (&mips_arch_string, "4010");
13267       mips_set_option_string (&mips_tune_string, "4010");
13268       break;
13269
13270     case OPTION_NO_M4010:
13271       break;
13272
13273     case OPTION_M4100:
13274       mips_set_option_string (&mips_arch_string, "4100");
13275       mips_set_option_string (&mips_tune_string, "4100");
13276       break;
13277
13278     case OPTION_NO_M4100:
13279       break;
13280
13281     case OPTION_M3900:
13282       mips_set_option_string (&mips_arch_string, "3900");
13283       mips_set_option_string (&mips_tune_string, "3900");
13284       break;
13285
13286     case OPTION_NO_M3900:
13287       break;
13288
13289     case OPTION_MICROMIPS:
13290       if (mips_opts.mips16 == 1)
13291         {
13292           as_bad (_("-mmicromips cannot be used with -mips16"));
13293           return 0;
13294         }
13295       mips_opts.micromips = 1;
13296       mips_no_prev_insn ();
13297       break;
13298
13299     case OPTION_NO_MICROMIPS:
13300       mips_opts.micromips = 0;
13301       mips_no_prev_insn ();
13302       break;
13303
13304     case OPTION_MIPS16:
13305       if (mips_opts.micromips == 1)
13306         {
13307           as_bad (_("-mips16 cannot be used with -micromips"));
13308           return 0;
13309         }
13310       mips_opts.mips16 = 1;
13311       mips_no_prev_insn ();
13312       break;
13313
13314     case OPTION_NO_MIPS16:
13315       mips_opts.mips16 = 0;
13316       mips_no_prev_insn ();
13317       break;
13318
13319     case OPTION_FIX_24K:
13320       mips_fix_24k = 1;
13321       break;
13322
13323     case OPTION_NO_FIX_24K:
13324       mips_fix_24k = 0;
13325       break;
13326
13327     case OPTION_FIX_LOONGSON2F_JUMP:
13328       mips_fix_loongson2f_jump = TRUE;
13329       break;
13330
13331     case OPTION_NO_FIX_LOONGSON2F_JUMP:
13332       mips_fix_loongson2f_jump = FALSE;
13333       break;
13334
13335     case OPTION_FIX_LOONGSON2F_NOP:
13336       mips_fix_loongson2f_nop = TRUE;
13337       break;
13338
13339     case OPTION_NO_FIX_LOONGSON2F_NOP:
13340       mips_fix_loongson2f_nop = FALSE;
13341       break;
13342
13343     case OPTION_FIX_VR4120:
13344       mips_fix_vr4120 = 1;
13345       break;
13346
13347     case OPTION_NO_FIX_VR4120:
13348       mips_fix_vr4120 = 0;
13349       break;
13350
13351     case OPTION_FIX_VR4130:
13352       mips_fix_vr4130 = 1;
13353       break;
13354
13355     case OPTION_NO_FIX_VR4130:
13356       mips_fix_vr4130 = 0;
13357       break;
13358
13359     case OPTION_FIX_CN63XXP1:
13360       mips_fix_cn63xxp1 = TRUE;
13361       break;
13362
13363     case OPTION_NO_FIX_CN63XXP1:
13364       mips_fix_cn63xxp1 = FALSE;
13365       break;
13366
13367     case OPTION_RELAX_BRANCH:
13368       mips_relax_branch = 1;
13369       break;
13370
13371     case OPTION_NO_RELAX_BRANCH:
13372       mips_relax_branch = 0;
13373       break;
13374
13375     case OPTION_INSN32:
13376       mips_opts.insn32 = TRUE;
13377       break;
13378
13379     case OPTION_NO_INSN32:
13380       mips_opts.insn32 = FALSE;
13381       break;
13382
13383     case OPTION_MSHARED:
13384       mips_in_shared = TRUE;
13385       break;
13386
13387     case OPTION_MNO_SHARED:
13388       mips_in_shared = FALSE;
13389       break;
13390
13391     case OPTION_MSYM32:
13392       mips_opts.sym32 = TRUE;
13393       break;
13394
13395     case OPTION_MNO_SYM32:
13396       mips_opts.sym32 = FALSE;
13397       break;
13398
13399       /* When generating ELF code, we permit -KPIC and -call_shared to
13400          select SVR4_PIC, and -non_shared to select no PIC.  This is
13401          intended to be compatible with Irix 5.  */
13402     case OPTION_CALL_SHARED:
13403       mips_pic = SVR4_PIC;
13404       mips_abicalls = TRUE;
13405       break;
13406
13407     case OPTION_CALL_NONPIC:
13408       mips_pic = NO_PIC;
13409       mips_abicalls = TRUE;
13410       break;
13411
13412     case OPTION_NON_SHARED:
13413       mips_pic = NO_PIC;
13414       mips_abicalls = FALSE;
13415       break;
13416
13417       /* The -xgot option tells the assembler to use 32 bit offsets
13418          when accessing the got in SVR4_PIC mode.  It is for Irix
13419          compatibility.  */
13420     case OPTION_XGOT:
13421       mips_big_got = 1;
13422       break;
13423
13424     case 'G':
13425       g_switch_value = atoi (arg);
13426       g_switch_seen = 1;
13427       break;
13428
13429       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
13430          and -mabi=64.  */
13431     case OPTION_32:
13432       mips_abi = O32_ABI;
13433       break;
13434
13435     case OPTION_N32:
13436       mips_abi = N32_ABI;
13437       break;
13438
13439     case OPTION_64:
13440       mips_abi = N64_ABI;
13441       if (!support_64bit_objects())
13442         as_fatal (_("No compiled in support for 64 bit object file format"));
13443       break;
13444
13445     case OPTION_GP32:
13446       file_mips_gp32 = 1;
13447       break;
13448
13449     case OPTION_GP64:
13450       file_mips_gp32 = 0;
13451       break;
13452
13453     case OPTION_FP32:
13454       file_mips_fp32 = 1;
13455       break;
13456
13457     case OPTION_FP64:
13458       file_mips_fp32 = 0;
13459       break;
13460
13461     case OPTION_SINGLE_FLOAT:
13462       file_mips_single_float = 1;
13463       break;
13464
13465     case OPTION_DOUBLE_FLOAT:
13466       file_mips_single_float = 0;
13467       break;
13468
13469     case OPTION_SOFT_FLOAT:
13470       file_mips_soft_float = 1;
13471       break;
13472
13473     case OPTION_HARD_FLOAT:
13474       file_mips_soft_float = 0;
13475       break;
13476
13477     case OPTION_MABI:
13478       if (strcmp (arg, "32") == 0)
13479         mips_abi = O32_ABI;
13480       else if (strcmp (arg, "o64") == 0)
13481         mips_abi = O64_ABI;
13482       else if (strcmp (arg, "n32") == 0)
13483         mips_abi = N32_ABI;
13484       else if (strcmp (arg, "64") == 0)
13485         {
13486           mips_abi = N64_ABI;
13487           if (! support_64bit_objects())
13488             as_fatal (_("No compiled in support for 64 bit object file "
13489                         "format"));
13490         }
13491       else if (strcmp (arg, "eabi") == 0)
13492         mips_abi = EABI_ABI;
13493       else
13494         {
13495           as_fatal (_("invalid abi -mabi=%s"), arg);
13496           return 0;
13497         }
13498       break;
13499
13500     case OPTION_M7000_HILO_FIX:
13501       mips_7000_hilo_fix = TRUE;
13502       break;
13503
13504     case OPTION_MNO_7000_HILO_FIX:
13505       mips_7000_hilo_fix = FALSE;
13506       break;
13507
13508     case OPTION_MDEBUG:
13509       mips_flag_mdebug = TRUE;
13510       break;
13511
13512     case OPTION_NO_MDEBUG:
13513       mips_flag_mdebug = FALSE;
13514       break;
13515
13516     case OPTION_PDR:
13517       mips_flag_pdr = TRUE;
13518       break;
13519
13520     case OPTION_NO_PDR:
13521       mips_flag_pdr = FALSE;
13522       break;
13523
13524     case OPTION_MVXWORKS_PIC:
13525       mips_pic = VXWORKS_PIC;
13526       break;
13527
13528     case OPTION_NAN:
13529       if (strcmp (arg, "2008") == 0)
13530         mips_flag_nan2008 = TRUE;
13531       else if (strcmp (arg, "legacy") == 0)
13532         mips_flag_nan2008 = FALSE;
13533       else
13534         {
13535           as_fatal (_("Invalid NaN setting -mnan=%s"), arg);
13536           return 0;
13537         }
13538       break;
13539
13540     default:
13541       return 0;
13542     }
13543
13544     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
13545
13546   return 1;
13547 }
13548 \f
13549 /* Set up globals to generate code for the ISA or processor
13550    described by INFO.  */
13551
13552 static void
13553 mips_set_architecture (const struct mips_cpu_info *info)
13554 {
13555   if (info != 0)
13556     {
13557       file_mips_arch = info->cpu;
13558       mips_opts.arch = info->cpu;
13559       mips_opts.isa = info->isa;
13560     }
13561 }
13562
13563
13564 /* Likewise for tuning.  */
13565
13566 static void
13567 mips_set_tune (const struct mips_cpu_info *info)
13568 {
13569   if (info != 0)
13570     mips_tune = info->cpu;
13571 }
13572
13573
13574 void
13575 mips_after_parse_args (void)
13576 {
13577   const struct mips_cpu_info *arch_info = 0;
13578   const struct mips_cpu_info *tune_info = 0;
13579
13580   /* GP relative stuff not working for PE */
13581   if (strncmp (TARGET_OS, "pe", 2) == 0)
13582     {
13583       if (g_switch_seen && g_switch_value != 0)
13584         as_bad (_("-G not supported in this configuration."));
13585       g_switch_value = 0;
13586     }
13587
13588   if (mips_abi == NO_ABI)
13589     mips_abi = MIPS_DEFAULT_ABI;
13590
13591   /* The following code determines the architecture and register size.
13592      Similar code was added to GCC 3.3 (see override_options() in
13593      config/mips/mips.c).  The GAS and GCC code should be kept in sync
13594      as much as possible.  */
13595
13596   if (mips_arch_string != 0)
13597     arch_info = mips_parse_cpu ("-march", mips_arch_string);
13598
13599   if (file_mips_isa != ISA_UNKNOWN)
13600     {
13601       /* Handle -mipsN.  At this point, file_mips_isa contains the
13602          ISA level specified by -mipsN, while arch_info->isa contains
13603          the -march selection (if any).  */
13604       if (arch_info != 0)
13605         {
13606           /* -march takes precedence over -mipsN, since it is more descriptive.
13607              There's no harm in specifying both as long as the ISA levels
13608              are the same.  */
13609           if (file_mips_isa != arch_info->isa)
13610             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
13611                     mips_cpu_info_from_isa (file_mips_isa)->name,
13612                     mips_cpu_info_from_isa (arch_info->isa)->name);
13613         }
13614       else
13615         arch_info = mips_cpu_info_from_isa (file_mips_isa);
13616     }
13617
13618   if (arch_info == 0)
13619     {
13620       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
13621       gas_assert (arch_info);
13622     }
13623
13624   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
13625     as_bad (_("-march=%s is not compatible with the selected ABI"),
13626             arch_info->name);
13627
13628   mips_set_architecture (arch_info);
13629
13630   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
13631   if (mips_tune_string != 0)
13632     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
13633
13634   if (tune_info == 0)
13635     mips_set_tune (arch_info);
13636   else
13637     mips_set_tune (tune_info);
13638
13639   if (file_mips_gp32 >= 0)
13640     {
13641       /* The user specified the size of the integer registers.  Make sure
13642          it agrees with the ABI and ISA.  */
13643       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
13644         as_bad (_("-mgp64 used with a 32-bit processor"));
13645       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
13646         as_bad (_("-mgp32 used with a 64-bit ABI"));
13647       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
13648         as_bad (_("-mgp64 used with a 32-bit ABI"));
13649     }
13650   else
13651     {
13652       /* Infer the integer register size from the ABI and processor.
13653          Restrict ourselves to 32-bit registers if that's all the
13654          processor has, or if the ABI cannot handle 64-bit registers.  */
13655       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
13656                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
13657     }
13658
13659   switch (file_mips_fp32)
13660     {
13661     default:
13662     case -1:
13663       /* No user specified float register size.
13664          ??? GAS treats single-float processors as though they had 64-bit
13665          float registers (although it complains when double-precision
13666          instructions are used).  As things stand, saying they have 32-bit
13667          registers would lead to spurious "register must be even" messages.
13668          So here we assume float registers are never smaller than the
13669          integer ones.  */
13670       if (file_mips_gp32 == 0)
13671         /* 64-bit integer registers implies 64-bit float registers.  */
13672         file_mips_fp32 = 0;
13673       else if ((mips_opts.ase & FP64_ASES)
13674                && ISA_HAS_64BIT_FPRS (mips_opts.isa))
13675         /* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
13676         file_mips_fp32 = 0;
13677       else
13678         /* 32-bit float registers.  */
13679         file_mips_fp32 = 1;
13680       break;
13681
13682     /* The user specified the size of the float registers.  Check if it
13683        agrees with the ABI and ISA.  */
13684     case 0:
13685       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
13686         as_bad (_("-mfp64 used with a 32-bit fpu"));
13687       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
13688                && !ISA_HAS_MXHC1 (mips_opts.isa))
13689         as_warn (_("-mfp64 used with a 32-bit ABI"));
13690       break;
13691     case 1:
13692       if (ABI_NEEDS_64BIT_REGS (mips_abi))
13693         as_warn (_("-mfp32 used with a 64-bit ABI"));
13694       break;
13695     }
13696
13697   /* End of GCC-shared inference code.  */
13698
13699   /* This flag is set when we have a 64-bit capable CPU but use only
13700      32-bit wide registers.  Note that EABI does not use it.  */
13701   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
13702       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
13703           || mips_abi == O32_ABI))
13704     mips_32bitmode = 1;
13705
13706   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
13707     as_bad (_("trap exception not supported at ISA 1"));
13708
13709   /* If the selected architecture includes support for ASEs, enable
13710      generation of code for them.  */
13711   if (mips_opts.mips16 == -1)
13712     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
13713   if (mips_opts.micromips == -1)
13714     mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
13715
13716   /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
13717      ASEs from being selected implicitly.  */
13718   if (file_mips_fp32 == 1)
13719     file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
13720
13721   /* If the user didn't explicitly select or deselect a particular ASE,
13722      use the default setting for the CPU.  */
13723   mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
13724
13725   file_mips_isa = mips_opts.isa;
13726   file_ase = mips_opts.ase;
13727   mips_opts.gp32 = file_mips_gp32;
13728   mips_opts.fp32 = file_mips_fp32;
13729   mips_opts.soft_float = file_mips_soft_float;
13730   mips_opts.single_float = file_mips_single_float;
13731
13732   mips_check_isa_supports_ases ();
13733
13734   if (mips_flag_mdebug < 0)
13735     mips_flag_mdebug = 0;
13736 }
13737 \f
13738 void
13739 mips_init_after_args (void)
13740 {
13741   /* initialize opcodes */
13742   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
13743   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
13744 }
13745
13746 long
13747 md_pcrel_from (fixS *fixP)
13748 {
13749   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
13750   switch (fixP->fx_r_type)
13751     {
13752     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
13753     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
13754       /* Return the address of the delay slot.  */
13755       return addr + 2;
13756
13757     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
13758     case BFD_RELOC_MICROMIPS_JMP:
13759     case BFD_RELOC_16_PCREL_S2:
13760     case BFD_RELOC_MIPS_JMP:
13761       /* Return the address of the delay slot.  */
13762       return addr + 4;
13763
13764     case BFD_RELOC_32_PCREL:
13765       return addr;
13766
13767     default:
13768       /* We have no relocation type for PC relative MIPS16 instructions.  */
13769       if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
13770         as_bad_where (fixP->fx_file, fixP->fx_line,
13771                       _("PC relative MIPS16 instruction references a different section"));
13772       return addr;
13773     }
13774 }
13775
13776 /* This is called before the symbol table is processed.  In order to
13777    work with gcc when using mips-tfile, we must keep all local labels.
13778    However, in other cases, we want to discard them.  If we were
13779    called with -g, but we didn't see any debugging information, it may
13780    mean that gcc is smuggling debugging information through to
13781    mips-tfile, in which case we must generate all local labels.  */
13782
13783 void
13784 mips_frob_file_before_adjust (void)
13785 {
13786 #ifndef NO_ECOFF_DEBUGGING
13787   if (ECOFF_DEBUGGING
13788       && mips_debug != 0
13789       && ! ecoff_debugging_seen)
13790     flag_keep_locals = 1;
13791 #endif
13792 }
13793
13794 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
13795    the corresponding LO16 reloc.  This is called before md_apply_fix and
13796    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
13797    relocation operators.
13798
13799    For our purposes, a %lo() expression matches a %got() or %hi()
13800    expression if:
13801
13802       (a) it refers to the same symbol; and
13803       (b) the offset applied in the %lo() expression is no lower than
13804           the offset applied in the %got() or %hi().
13805
13806    (b) allows us to cope with code like:
13807
13808         lui     $4,%hi(foo)
13809         lh      $4,%lo(foo+2)($4)
13810
13811    ...which is legal on RELA targets, and has a well-defined behaviour
13812    if the user knows that adding 2 to "foo" will not induce a carry to
13813    the high 16 bits.
13814
13815    When several %lo()s match a particular %got() or %hi(), we use the
13816    following rules to distinguish them:
13817
13818      (1) %lo()s with smaller offsets are a better match than %lo()s with
13819          higher offsets.
13820
13821      (2) %lo()s with no matching %got() or %hi() are better than those
13822          that already have a matching %got() or %hi().
13823
13824      (3) later %lo()s are better than earlier %lo()s.
13825
13826    These rules are applied in order.
13827
13828    (1) means, among other things, that %lo()s with identical offsets are
13829    chosen if they exist.
13830
13831    (2) means that we won't associate several high-part relocations with
13832    the same low-part relocation unless there's no alternative.  Having
13833    several high parts for the same low part is a GNU extension; this rule
13834    allows careful users to avoid it.
13835
13836    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
13837    with the last high-part relocation being at the front of the list.
13838    It therefore makes sense to choose the last matching low-part
13839    relocation, all other things being equal.  It's also easier
13840    to code that way.  */
13841
13842 void
13843 mips_frob_file (void)
13844 {
13845   struct mips_hi_fixup *l;
13846   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
13847
13848   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
13849     {
13850       segment_info_type *seginfo;
13851       bfd_boolean matched_lo_p;
13852       fixS **hi_pos, **lo_pos, **pos;
13853
13854       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
13855
13856       /* If a GOT16 relocation turns out to be against a global symbol,
13857          there isn't supposed to be a matching LO.  Ignore %gots against
13858          constants; we'll report an error for those later.  */
13859       if (got16_reloc_p (l->fixp->fx_r_type)
13860           && !(l->fixp->fx_addsy
13861                && pic_need_relax (l->fixp->fx_addsy, l->seg)))
13862         continue;
13863
13864       /* Check quickly whether the next fixup happens to be a matching %lo.  */
13865       if (fixup_has_matching_lo_p (l->fixp))
13866         continue;
13867
13868       seginfo = seg_info (l->seg);
13869
13870       /* Set HI_POS to the position of this relocation in the chain.
13871          Set LO_POS to the position of the chosen low-part relocation.
13872          MATCHED_LO_P is true on entry to the loop if *POS is a low-part
13873          relocation that matches an immediately-preceding high-part
13874          relocation.  */
13875       hi_pos = NULL;
13876       lo_pos = NULL;
13877       matched_lo_p = FALSE;
13878       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
13879
13880       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
13881         {
13882           if (*pos == l->fixp)
13883             hi_pos = pos;
13884
13885           if ((*pos)->fx_r_type == looking_for_rtype
13886               && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
13887               && (*pos)->fx_offset >= l->fixp->fx_offset
13888               && (lo_pos == NULL
13889                   || (*pos)->fx_offset < (*lo_pos)->fx_offset
13890                   || (!matched_lo_p
13891                       && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
13892             lo_pos = pos;
13893
13894           matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
13895                           && fixup_has_matching_lo_p (*pos));
13896         }
13897
13898       /* If we found a match, remove the high-part relocation from its
13899          current position and insert it before the low-part relocation.
13900          Make the offsets match so that fixup_has_matching_lo_p()
13901          will return true.
13902
13903          We don't warn about unmatched high-part relocations since some
13904          versions of gcc have been known to emit dead "lui ...%hi(...)"
13905          instructions.  */
13906       if (lo_pos != NULL)
13907         {
13908           l->fixp->fx_offset = (*lo_pos)->fx_offset;
13909           if (l->fixp->fx_next != *lo_pos)
13910             {
13911               *hi_pos = l->fixp->fx_next;
13912               l->fixp->fx_next = *lo_pos;
13913               *lo_pos = l->fixp;
13914             }
13915         }
13916     }
13917 }
13918
13919 int
13920 mips_force_relocation (fixS *fixp)
13921 {
13922   if (generic_force_reloc (fixp))
13923     return 1;
13924
13925   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
13926      so that the linker relaxation can update targets.  */
13927   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
13928       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
13929       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
13930     return 1;
13931
13932   return 0;
13933 }
13934
13935 /* Read the instruction associated with RELOC from BUF.  */
13936
13937 static unsigned int
13938 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
13939 {
13940   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
13941     return read_compressed_insn (buf, 4);
13942   else
13943     return read_insn (buf);
13944 }
13945
13946 /* Write instruction INSN to BUF, given that it has been relocated
13947    by RELOC.  */
13948
13949 static void
13950 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
13951                   unsigned long insn)
13952 {
13953   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
13954     write_compressed_insn (buf, insn, 4);
13955   else
13956     write_insn (buf, insn);
13957 }
13958
13959 /* Apply a fixup to the object file.  */
13960
13961 void
13962 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
13963 {
13964   char *buf;
13965   unsigned long insn;
13966   reloc_howto_type *howto;
13967
13968   /* We ignore generic BFD relocations we don't know about.  */
13969   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
13970   if (! howto)
13971     return;
13972
13973   gas_assert (fixP->fx_size == 2
13974               || fixP->fx_size == 4
13975               || fixP->fx_r_type == BFD_RELOC_16
13976               || fixP->fx_r_type == BFD_RELOC_64
13977               || fixP->fx_r_type == BFD_RELOC_CTOR
13978               || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
13979               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
13980               || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13981               || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
13982               || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
13983
13984   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
13985
13986   gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
13987               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
13988               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
13989               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
13990               || fixP->fx_r_type == BFD_RELOC_32_PCREL);
13991
13992   /* Don't treat parts of a composite relocation as done.  There are two
13993      reasons for this:
13994
13995      (1) The second and third parts will be against 0 (RSS_UNDEF) but
13996          should nevertheless be emitted if the first part is.
13997
13998      (2) In normal usage, composite relocations are never assembly-time
13999          constants.  The easiest way of dealing with the pathological
14000          exceptions is to generate a relocation against STN_UNDEF and
14001          leave everything up to the linker.  */
14002   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
14003     fixP->fx_done = 1;
14004
14005   switch (fixP->fx_r_type)
14006     {
14007     case BFD_RELOC_MIPS_TLS_GD:
14008     case BFD_RELOC_MIPS_TLS_LDM:
14009     case BFD_RELOC_MIPS_TLS_DTPREL32:
14010     case BFD_RELOC_MIPS_TLS_DTPREL64:
14011     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14012     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14013     case BFD_RELOC_MIPS_TLS_GOTTPREL:
14014     case BFD_RELOC_MIPS_TLS_TPREL32:
14015     case BFD_RELOC_MIPS_TLS_TPREL64:
14016     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14017     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
14018     case BFD_RELOC_MICROMIPS_TLS_GD:
14019     case BFD_RELOC_MICROMIPS_TLS_LDM:
14020     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14021     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14022     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14023     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14024     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
14025     case BFD_RELOC_MIPS16_TLS_GD:
14026     case BFD_RELOC_MIPS16_TLS_LDM:
14027     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14028     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14029     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14030     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14031     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
14032       if (!fixP->fx_addsy)
14033         {
14034           as_bad_where (fixP->fx_file, fixP->fx_line,
14035                         _("TLS relocation against a constant"));
14036           break;
14037         }
14038       S_SET_THREAD_LOCAL (fixP->fx_addsy);
14039       /* fall through */
14040
14041     case BFD_RELOC_MIPS_JMP:
14042     case BFD_RELOC_MIPS_SHIFT5:
14043     case BFD_RELOC_MIPS_SHIFT6:
14044     case BFD_RELOC_MIPS_GOT_DISP:
14045     case BFD_RELOC_MIPS_GOT_PAGE:
14046     case BFD_RELOC_MIPS_GOT_OFST:
14047     case BFD_RELOC_MIPS_SUB:
14048     case BFD_RELOC_MIPS_INSERT_A:
14049     case BFD_RELOC_MIPS_INSERT_B:
14050     case BFD_RELOC_MIPS_DELETE:
14051     case BFD_RELOC_MIPS_HIGHEST:
14052     case BFD_RELOC_MIPS_HIGHER:
14053     case BFD_RELOC_MIPS_SCN_DISP:
14054     case BFD_RELOC_MIPS_REL16:
14055     case BFD_RELOC_MIPS_RELGOT:
14056     case BFD_RELOC_MIPS_JALR:
14057     case BFD_RELOC_HI16:
14058     case BFD_RELOC_HI16_S:
14059     case BFD_RELOC_LO16:
14060     case BFD_RELOC_GPREL16:
14061     case BFD_RELOC_MIPS_LITERAL:
14062     case BFD_RELOC_MIPS_CALL16:
14063     case BFD_RELOC_MIPS_GOT16:
14064     case BFD_RELOC_GPREL32:
14065     case BFD_RELOC_MIPS_GOT_HI16:
14066     case BFD_RELOC_MIPS_GOT_LO16:
14067     case BFD_RELOC_MIPS_CALL_HI16:
14068     case BFD_RELOC_MIPS_CALL_LO16:
14069     case BFD_RELOC_MIPS16_GPREL:
14070     case BFD_RELOC_MIPS16_GOT16:
14071     case BFD_RELOC_MIPS16_CALL16:
14072     case BFD_RELOC_MIPS16_HI16:
14073     case BFD_RELOC_MIPS16_HI16_S:
14074     case BFD_RELOC_MIPS16_LO16:
14075     case BFD_RELOC_MIPS16_JMP:
14076     case BFD_RELOC_MICROMIPS_JMP:
14077     case BFD_RELOC_MICROMIPS_GOT_DISP:
14078     case BFD_RELOC_MICROMIPS_GOT_PAGE:
14079     case BFD_RELOC_MICROMIPS_GOT_OFST:
14080     case BFD_RELOC_MICROMIPS_SUB:
14081     case BFD_RELOC_MICROMIPS_HIGHEST:
14082     case BFD_RELOC_MICROMIPS_HIGHER:
14083     case BFD_RELOC_MICROMIPS_SCN_DISP:
14084     case BFD_RELOC_MICROMIPS_JALR:
14085     case BFD_RELOC_MICROMIPS_HI16:
14086     case BFD_RELOC_MICROMIPS_HI16_S:
14087     case BFD_RELOC_MICROMIPS_LO16:
14088     case BFD_RELOC_MICROMIPS_GPREL16:
14089     case BFD_RELOC_MICROMIPS_LITERAL:
14090     case BFD_RELOC_MICROMIPS_CALL16:
14091     case BFD_RELOC_MICROMIPS_GOT16:
14092     case BFD_RELOC_MICROMIPS_GOT_HI16:
14093     case BFD_RELOC_MICROMIPS_GOT_LO16:
14094     case BFD_RELOC_MICROMIPS_CALL_HI16:
14095     case BFD_RELOC_MICROMIPS_CALL_LO16:
14096     case BFD_RELOC_MIPS_EH:
14097       if (fixP->fx_done)
14098         {
14099           offsetT value;
14100
14101           if (calculate_reloc (fixP->fx_r_type, *valP, &value))
14102             {
14103               insn = read_reloc_insn (buf, fixP->fx_r_type);
14104               if (mips16_reloc_p (fixP->fx_r_type))
14105                 insn |= mips16_immed_extend (value, 16);
14106               else
14107                 insn |= (value & 0xffff);
14108               write_reloc_insn (buf, fixP->fx_r_type, insn);
14109             }
14110           else
14111             as_bad_where (fixP->fx_file, fixP->fx_line,
14112                           _("Unsupported constant in relocation"));
14113         }
14114       break;
14115
14116     case BFD_RELOC_64:
14117       /* This is handled like BFD_RELOC_32, but we output a sign
14118          extended value if we are only 32 bits.  */
14119       if (fixP->fx_done)
14120         {
14121           if (8 <= sizeof (valueT))
14122             md_number_to_chars (buf, *valP, 8);
14123           else
14124             {
14125               valueT hiv;
14126
14127               if ((*valP & 0x80000000) != 0)
14128                 hiv = 0xffffffff;
14129               else
14130                 hiv = 0;
14131               md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
14132               md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
14133             }
14134         }
14135       break;
14136
14137     case BFD_RELOC_RVA:
14138     case BFD_RELOC_32:
14139     case BFD_RELOC_32_PCREL:
14140     case BFD_RELOC_16:
14141       /* If we are deleting this reloc entry, we must fill in the
14142          value now.  This can happen if we have a .word which is not
14143          resolved when it appears but is later defined.  */
14144       if (fixP->fx_done)
14145         md_number_to_chars (buf, *valP, fixP->fx_size);
14146       break;
14147
14148     case BFD_RELOC_16_PCREL_S2:
14149       if ((*valP & 0x3) != 0)
14150         as_bad_where (fixP->fx_file, fixP->fx_line,
14151                       _("Branch to misaligned address (%lx)"), (long) *valP);
14152
14153       /* We need to save the bits in the instruction since fixup_segment()
14154          might be deleting the relocation entry (i.e., a branch within
14155          the current segment).  */
14156       if (! fixP->fx_done)
14157         break;
14158
14159       /* Update old instruction data.  */
14160       insn = read_insn (buf);
14161
14162       if (*valP + 0x20000 <= 0x3ffff)
14163         {
14164           insn |= (*valP >> 2) & 0xffff;
14165           write_insn (buf, insn);
14166         }
14167       else if (mips_pic == NO_PIC
14168                && fixP->fx_done
14169                && fixP->fx_frag->fr_address >= text_section->vma
14170                && (fixP->fx_frag->fr_address
14171                    < text_section->vma + bfd_get_section_size (text_section))
14172                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
14173                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
14174                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
14175         {
14176           /* The branch offset is too large.  If this is an
14177              unconditional branch, and we are not generating PIC code,
14178              we can convert it to an absolute jump instruction.  */
14179           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
14180             insn = 0x0c000000;  /* jal */
14181           else
14182             insn = 0x08000000;  /* j */
14183           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
14184           fixP->fx_done = 0;
14185           fixP->fx_addsy = section_symbol (text_section);
14186           *valP += md_pcrel_from (fixP);
14187           write_insn (buf, insn);
14188         }
14189       else
14190         {
14191           /* If we got here, we have branch-relaxation disabled,
14192              and there's nothing we can do to fix this instruction
14193              without turning it into a longer sequence.  */
14194           as_bad_where (fixP->fx_file, fixP->fx_line,
14195                         _("Branch out of range"));
14196         }
14197       break;
14198
14199     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14200     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14201     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14202       /* We adjust the offset back to even.  */
14203       if ((*valP & 0x1) != 0)
14204         --(*valP);
14205
14206       if (! fixP->fx_done)
14207         break;
14208
14209       /* Should never visit here, because we keep the relocation.  */
14210       abort ();
14211       break;
14212
14213     case BFD_RELOC_VTABLE_INHERIT:
14214       fixP->fx_done = 0;
14215       if (fixP->fx_addsy
14216           && !S_IS_DEFINED (fixP->fx_addsy)
14217           && !S_IS_WEAK (fixP->fx_addsy))
14218         S_SET_WEAK (fixP->fx_addsy);
14219       break;
14220
14221     case BFD_RELOC_VTABLE_ENTRY:
14222       fixP->fx_done = 0;
14223       break;
14224
14225     default:
14226       abort ();
14227     }
14228
14229   /* Remember value for tc_gen_reloc.  */
14230   fixP->fx_addnumber = *valP;
14231 }
14232
14233 static symbolS *
14234 get_symbol (void)
14235 {
14236   int c;
14237   char *name;
14238   symbolS *p;
14239
14240   name = input_line_pointer;
14241   c = get_symbol_end ();
14242   p = (symbolS *) symbol_find_or_make (name);
14243   *input_line_pointer = c;
14244   return p;
14245 }
14246
14247 /* Align the current frag to a given power of two.  If a particular
14248    fill byte should be used, FILL points to an integer that contains
14249    that byte, otherwise FILL is null.
14250
14251    This function used to have the comment:
14252
14253       The MIPS assembler also automatically adjusts any preceding label.
14254
14255    The implementation therefore applied the adjustment to a maximum of
14256    one label.  However, other label adjustments are applied to batches
14257    of labels, and adjusting just one caused problems when new labels
14258    were added for the sake of debugging or unwind information.
14259    We therefore adjust all preceding labels (given as LABELS) instead.  */
14260
14261 static void
14262 mips_align (int to, int *fill, struct insn_label_list *labels)
14263 {
14264   mips_emit_delays ();
14265   mips_record_compressed_mode ();
14266   if (fill == NULL && subseg_text_p (now_seg))
14267     frag_align_code (to, 0);
14268   else
14269     frag_align (to, fill ? *fill : 0, 0);
14270   record_alignment (now_seg, to);
14271   mips_move_labels (labels, FALSE);
14272 }
14273
14274 /* Align to a given power of two.  .align 0 turns off the automatic
14275    alignment used by the data creating pseudo-ops.  */
14276
14277 static void
14278 s_align (int x ATTRIBUTE_UNUSED)
14279 {
14280   int temp, fill_value, *fill_ptr;
14281   long max_alignment = 28;
14282
14283   /* o Note that the assembler pulls down any immediately preceding label
14284        to the aligned address.
14285      o It's not documented but auto alignment is reinstated by
14286        a .align pseudo instruction.
14287      o Note also that after auto alignment is turned off the mips assembler
14288        issues an error on attempt to assemble an improperly aligned data item.
14289        We don't.  */
14290
14291   temp = get_absolute_expression ();
14292   if (temp > max_alignment)
14293     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
14294   else if (temp < 0)
14295     {
14296       as_warn (_("Alignment negative: 0 assumed."));
14297       temp = 0;
14298     }
14299   if (*input_line_pointer == ',')
14300     {
14301       ++input_line_pointer;
14302       fill_value = get_absolute_expression ();
14303       fill_ptr = &fill_value;
14304     }
14305   else
14306     fill_ptr = 0;
14307   if (temp)
14308     {
14309       segment_info_type *si = seg_info (now_seg);
14310       struct insn_label_list *l = si->label_list;
14311       /* Auto alignment should be switched on by next section change.  */
14312       auto_align = 1;
14313       mips_align (temp, fill_ptr, l);
14314     }
14315   else
14316     {
14317       auto_align = 0;
14318     }
14319
14320   demand_empty_rest_of_line ();
14321 }
14322
14323 static void
14324 s_change_sec (int sec)
14325 {
14326   segT seg;
14327
14328   /* The ELF backend needs to know that we are changing sections, so
14329      that .previous works correctly.  We could do something like check
14330      for an obj_section_change_hook macro, but that might be confusing
14331      as it would not be appropriate to use it in the section changing
14332      functions in read.c, since obj-elf.c intercepts those.  FIXME:
14333      This should be cleaner, somehow.  */
14334   obj_elf_section_change_hook ();
14335
14336   mips_emit_delays ();
14337
14338   switch (sec)
14339     {
14340     case 't':
14341       s_text (0);
14342       break;
14343     case 'd':
14344       s_data (0);
14345       break;
14346     case 'b':
14347       subseg_set (bss_section, (subsegT) get_absolute_expression ());
14348       demand_empty_rest_of_line ();
14349       break;
14350
14351     case 'r':
14352       seg = subseg_new (RDATA_SECTION_NAME,
14353                         (subsegT) get_absolute_expression ());
14354       bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
14355                                               | SEC_READONLY | SEC_RELOC
14356                                               | SEC_DATA));
14357       if (strncmp (TARGET_OS, "elf", 3) != 0)
14358         record_alignment (seg, 4);
14359       demand_empty_rest_of_line ();
14360       break;
14361
14362     case 's':
14363       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
14364       bfd_set_section_flags (stdoutput, seg,
14365                              SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
14366       if (strncmp (TARGET_OS, "elf", 3) != 0)
14367         record_alignment (seg, 4);
14368       demand_empty_rest_of_line ();
14369       break;
14370
14371     case 'B':
14372       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
14373       bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
14374       if (strncmp (TARGET_OS, "elf", 3) != 0)
14375         record_alignment (seg, 4);
14376       demand_empty_rest_of_line ();
14377       break;
14378     }
14379
14380   auto_align = 1;
14381 }
14382
14383 void
14384 s_change_section (int ignore ATTRIBUTE_UNUSED)
14385 {
14386   char *section_name;
14387   char c;
14388   char next_c = 0;
14389   int section_type;
14390   int section_flag;
14391   int section_entry_size;
14392   int section_alignment;
14393
14394   section_name = input_line_pointer;
14395   c = get_symbol_end ();
14396   if (c)
14397     next_c = *(input_line_pointer + 1);
14398
14399   /* Do we have .section Name<,"flags">?  */
14400   if (c != ',' || (c == ',' && next_c == '"'))
14401     {
14402       /* just after name is now '\0'.  */
14403       *input_line_pointer = c;
14404       input_line_pointer = section_name;
14405       obj_elf_section (ignore);
14406       return;
14407     }
14408   input_line_pointer++;
14409
14410   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
14411   if (c == ',')
14412     section_type = get_absolute_expression ();
14413   else
14414     section_type = 0;
14415   if (*input_line_pointer++ == ',')
14416     section_flag = get_absolute_expression ();
14417   else
14418     section_flag = 0;
14419   if (*input_line_pointer++ == ',')
14420     section_entry_size = get_absolute_expression ();
14421   else
14422     section_entry_size = 0;
14423   if (*input_line_pointer++ == ',')
14424     section_alignment = get_absolute_expression ();
14425   else
14426     section_alignment = 0;
14427   /* FIXME: really ignore?  */
14428   (void) section_alignment;
14429
14430   section_name = xstrdup (section_name);
14431
14432   /* When using the generic form of .section (as implemented by obj-elf.c),
14433      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
14434      traditionally had to fall back on the more common @progbits instead.
14435
14436      There's nothing really harmful in this, since bfd will correct
14437      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
14438      means that, for backwards compatibility, the special_section entries
14439      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
14440
14441      Even so, we shouldn't force users of the MIPS .section syntax to
14442      incorrectly label the sections as SHT_PROGBITS.  The best compromise
14443      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
14444      generic type-checking code.  */
14445   if (section_type == SHT_MIPS_DWARF)
14446     section_type = SHT_PROGBITS;
14447
14448   obj_elf_change_section (section_name, section_type, section_flag,
14449                           section_entry_size, 0, 0, 0);
14450
14451   if (now_seg->name != section_name)
14452     free (section_name);
14453 }
14454
14455 void
14456 mips_enable_auto_align (void)
14457 {
14458   auto_align = 1;
14459 }
14460
14461 static void
14462 s_cons (int log_size)
14463 {
14464   segment_info_type *si = seg_info (now_seg);
14465   struct insn_label_list *l = si->label_list;
14466
14467   mips_emit_delays ();
14468   if (log_size > 0 && auto_align)
14469     mips_align (log_size, 0, l);
14470   cons (1 << log_size);
14471   mips_clear_insn_labels ();
14472 }
14473
14474 static void
14475 s_float_cons (int type)
14476 {
14477   segment_info_type *si = seg_info (now_seg);
14478   struct insn_label_list *l = si->label_list;
14479
14480   mips_emit_delays ();
14481
14482   if (auto_align)
14483     {
14484       if (type == 'd')
14485         mips_align (3, 0, l);
14486       else
14487         mips_align (2, 0, l);
14488     }
14489
14490   float_cons (type);
14491   mips_clear_insn_labels ();
14492 }
14493
14494 /* Handle .globl.  We need to override it because on Irix 5 you are
14495    permitted to say
14496        .globl foo .text
14497    where foo is an undefined symbol, to mean that foo should be
14498    considered to be the address of a function.  */
14499
14500 static void
14501 s_mips_globl (int x ATTRIBUTE_UNUSED)
14502 {
14503   char *name;
14504   int c;
14505   symbolS *symbolP;
14506   flagword flag;
14507
14508   do
14509     {
14510       name = input_line_pointer;
14511       c = get_symbol_end ();
14512       symbolP = symbol_find_or_make (name);
14513       S_SET_EXTERNAL (symbolP);
14514
14515       *input_line_pointer = c;
14516       SKIP_WHITESPACE ();
14517
14518       /* On Irix 5, every global symbol that is not explicitly labelled as
14519          being a function is apparently labelled as being an object.  */
14520       flag = BSF_OBJECT;
14521
14522       if (!is_end_of_line[(unsigned char) *input_line_pointer]
14523           && (*input_line_pointer != ','))
14524         {
14525           char *secname;
14526           asection *sec;
14527
14528           secname = input_line_pointer;
14529           c = get_symbol_end ();
14530           sec = bfd_get_section_by_name (stdoutput, secname);
14531           if (sec == NULL)
14532             as_bad (_("%s: no such section"), secname);
14533           *input_line_pointer = c;
14534
14535           if (sec != NULL && (sec->flags & SEC_CODE) != 0)
14536             flag = BSF_FUNCTION;
14537         }
14538
14539       symbol_get_bfdsym (symbolP)->flags |= flag;
14540
14541       c = *input_line_pointer;
14542       if (c == ',')
14543         {
14544           input_line_pointer++;
14545           SKIP_WHITESPACE ();
14546           if (is_end_of_line[(unsigned char) *input_line_pointer])
14547             c = '\n';
14548         }
14549     }
14550   while (c == ',');
14551
14552   demand_empty_rest_of_line ();
14553 }
14554
14555 static void
14556 s_option (int x ATTRIBUTE_UNUSED)
14557 {
14558   char *opt;
14559   char c;
14560
14561   opt = input_line_pointer;
14562   c = get_symbol_end ();
14563
14564   if (*opt == 'O')
14565     {
14566       /* FIXME: What does this mean?  */
14567     }
14568   else if (strncmp (opt, "pic", 3) == 0)
14569     {
14570       int i;
14571
14572       i = atoi (opt + 3);
14573       if (i == 0)
14574         mips_pic = NO_PIC;
14575       else if (i == 2)
14576         {
14577           mips_pic = SVR4_PIC;
14578           mips_abicalls = TRUE;
14579         }
14580       else
14581         as_bad (_(".option pic%d not supported"), i);
14582
14583       if (mips_pic == SVR4_PIC)
14584         {
14585           if (g_switch_seen && g_switch_value != 0)
14586             as_warn (_("-G may not be used with SVR4 PIC code"));
14587           g_switch_value = 0;
14588           bfd_set_gp_size (stdoutput, 0);
14589         }
14590     }
14591   else
14592     as_warn (_("Unrecognized option \"%s\""), opt);
14593
14594   *input_line_pointer = c;
14595   demand_empty_rest_of_line ();
14596 }
14597
14598 /* This structure is used to hold a stack of .set values.  */
14599
14600 struct mips_option_stack
14601 {
14602   struct mips_option_stack *next;
14603   struct mips_set_options options;
14604 };
14605
14606 static struct mips_option_stack *mips_opts_stack;
14607
14608 /* Handle the .set pseudo-op.  */
14609
14610 static void
14611 s_mipsset (int x ATTRIBUTE_UNUSED)
14612 {
14613   char *name = input_line_pointer, ch;
14614   const struct mips_ase *ase;
14615
14616   while (!is_end_of_line[(unsigned char) *input_line_pointer])
14617     ++input_line_pointer;
14618   ch = *input_line_pointer;
14619   *input_line_pointer = '\0';
14620
14621   if (strcmp (name, "reorder") == 0)
14622     {
14623       if (mips_opts.noreorder)
14624         end_noreorder ();
14625     }
14626   else if (strcmp (name, "noreorder") == 0)
14627     {
14628       if (!mips_opts.noreorder)
14629         start_noreorder ();
14630     }
14631   else if (strncmp (name, "at=", 3) == 0)
14632     {
14633       char *s = name + 3;
14634
14635       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
14636         as_bad (_("Unrecognized register name `%s'"), s);
14637     }
14638   else if (strcmp (name, "at") == 0)
14639     {
14640       mips_opts.at = ATREG;
14641     }
14642   else if (strcmp (name, "noat") == 0)
14643     {
14644       mips_opts.at = ZERO;
14645     }
14646   else if (strcmp (name, "macro") == 0)
14647     {
14648       mips_opts.warn_about_macros = 0;
14649     }
14650   else if (strcmp (name, "nomacro") == 0)
14651     {
14652       if (mips_opts.noreorder == 0)
14653         as_bad (_("`noreorder' must be set before `nomacro'"));
14654       mips_opts.warn_about_macros = 1;
14655     }
14656   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
14657     {
14658       mips_opts.nomove = 0;
14659     }
14660   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
14661     {
14662       mips_opts.nomove = 1;
14663     }
14664   else if (strcmp (name, "bopt") == 0)
14665     {
14666       mips_opts.nobopt = 0;
14667     }
14668   else if (strcmp (name, "nobopt") == 0)
14669     {
14670       mips_opts.nobopt = 1;
14671     }
14672   else if (strcmp (name, "gp=default") == 0)
14673     mips_opts.gp32 = file_mips_gp32;
14674   else if (strcmp (name, "gp=32") == 0)
14675     mips_opts.gp32 = 1;
14676   else if (strcmp (name, "gp=64") == 0)
14677     {
14678       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
14679         as_warn (_("%s isa does not support 64-bit registers"),
14680                  mips_cpu_info_from_isa (mips_opts.isa)->name);
14681       mips_opts.gp32 = 0;
14682     }
14683   else if (strcmp (name, "fp=default") == 0)
14684     mips_opts.fp32 = file_mips_fp32;
14685   else if (strcmp (name, "fp=32") == 0)
14686     mips_opts.fp32 = 1;
14687   else if (strcmp (name, "fp=64") == 0)
14688     {
14689       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
14690         as_warn (_("%s isa does not support 64-bit floating point registers"),
14691                  mips_cpu_info_from_isa (mips_opts.isa)->name);
14692       mips_opts.fp32 = 0;
14693     }
14694   else if (strcmp (name, "softfloat") == 0)
14695     mips_opts.soft_float = 1;
14696   else if (strcmp (name, "hardfloat") == 0)
14697     mips_opts.soft_float = 0;
14698   else if (strcmp (name, "singlefloat") == 0)
14699     mips_opts.single_float = 1;
14700   else if (strcmp (name, "doublefloat") == 0)
14701     mips_opts.single_float = 0;
14702   else if (strcmp (name, "mips16") == 0
14703            || strcmp (name, "MIPS-16") == 0)
14704     {
14705       if (mips_opts.micromips == 1)
14706         as_fatal (_("`mips16' cannot be used with `micromips'"));
14707       mips_opts.mips16 = 1;
14708     }
14709   else if (strcmp (name, "nomips16") == 0
14710            || strcmp (name, "noMIPS-16") == 0)
14711     mips_opts.mips16 = 0;
14712   else if (strcmp (name, "micromips") == 0)
14713     {
14714       if (mips_opts.mips16 == 1)
14715         as_fatal (_("`micromips' cannot be used with `mips16'"));
14716       mips_opts.micromips = 1;
14717     }
14718   else if (strcmp (name, "nomicromips") == 0)
14719     mips_opts.micromips = 0;
14720   else if (name[0] == 'n'
14721            && name[1] == 'o'
14722            && (ase = mips_lookup_ase (name + 2)))
14723     mips_set_ase (ase, FALSE);
14724   else if ((ase = mips_lookup_ase (name)))
14725     mips_set_ase (ase, TRUE);
14726   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
14727     {
14728       int reset = 0;
14729
14730       /* Permit the user to change the ISA and architecture on the fly.
14731          Needless to say, misuse can cause serious problems.  */
14732       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
14733         {
14734           reset = 1;
14735           mips_opts.isa = file_mips_isa;
14736           mips_opts.arch = file_mips_arch;
14737         }
14738       else if (strncmp (name, "arch=", 5) == 0)
14739         {
14740           const struct mips_cpu_info *p;
14741
14742           p = mips_parse_cpu("internal use", name + 5);
14743           if (!p)
14744             as_bad (_("unknown architecture %s"), name + 5);
14745           else
14746             {
14747               mips_opts.arch = p->cpu;
14748               mips_opts.isa = p->isa;
14749             }
14750         }
14751       else if (strncmp (name, "mips", 4) == 0)
14752         {
14753           const struct mips_cpu_info *p;
14754
14755           p = mips_parse_cpu("internal use", name);
14756           if (!p)
14757             as_bad (_("unknown ISA level %s"), name + 4);
14758           else
14759             {
14760               mips_opts.arch = p->cpu;
14761               mips_opts.isa = p->isa;
14762             }
14763         }
14764       else
14765         as_bad (_("unknown ISA or architecture %s"), name);
14766
14767       switch (mips_opts.isa)
14768         {
14769         case  0:
14770           break;
14771         case ISA_MIPS1:
14772         case ISA_MIPS2:
14773         case ISA_MIPS32:
14774         case ISA_MIPS32R2:
14775           mips_opts.gp32 = 1;
14776           mips_opts.fp32 = 1;
14777           break;
14778         case ISA_MIPS3:
14779         case ISA_MIPS4:
14780         case ISA_MIPS5:
14781         case ISA_MIPS64:
14782         case ISA_MIPS64R2:
14783           mips_opts.gp32 = 0;
14784           if (mips_opts.arch == CPU_R5900)
14785             {
14786                 mips_opts.fp32 = 1;
14787             }
14788           else
14789             {
14790           mips_opts.fp32 = 0;
14791             }
14792           break;
14793         default:
14794           as_bad (_("unknown ISA level %s"), name + 4);
14795           break;
14796         }
14797       if (reset)
14798         {
14799           mips_opts.gp32 = file_mips_gp32;
14800           mips_opts.fp32 = file_mips_fp32;
14801         }
14802     }
14803   else if (strcmp (name, "autoextend") == 0)
14804     mips_opts.noautoextend = 0;
14805   else if (strcmp (name, "noautoextend") == 0)
14806     mips_opts.noautoextend = 1;
14807   else if (strcmp (name, "insn32") == 0)
14808     mips_opts.insn32 = TRUE;
14809   else if (strcmp (name, "noinsn32") == 0)
14810     mips_opts.insn32 = FALSE;
14811   else if (strcmp (name, "push") == 0)
14812     {
14813       struct mips_option_stack *s;
14814
14815       s = (struct mips_option_stack *) xmalloc (sizeof *s);
14816       s->next = mips_opts_stack;
14817       s->options = mips_opts;
14818       mips_opts_stack = s;
14819     }
14820   else if (strcmp (name, "pop") == 0)
14821     {
14822       struct mips_option_stack *s;
14823
14824       s = mips_opts_stack;
14825       if (s == NULL)
14826         as_bad (_(".set pop with no .set push"));
14827       else
14828         {
14829           /* If we're changing the reorder mode we need to handle
14830              delay slots correctly.  */
14831           if (s->options.noreorder && ! mips_opts.noreorder)
14832             start_noreorder ();
14833           else if (! s->options.noreorder && mips_opts.noreorder)
14834             end_noreorder ();
14835
14836           mips_opts = s->options;
14837           mips_opts_stack = s->next;
14838           free (s);
14839         }
14840     }
14841   else if (strcmp (name, "sym32") == 0)
14842     mips_opts.sym32 = TRUE;
14843   else if (strcmp (name, "nosym32") == 0)
14844     mips_opts.sym32 = FALSE;
14845   else if (strchr (name, ','))
14846     {
14847       /* Generic ".set" directive; use the generic handler.  */
14848       *input_line_pointer = ch;
14849       input_line_pointer = name;
14850       s_set (0);
14851       return;
14852     }
14853   else
14854     {
14855       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
14856     }
14857   mips_check_isa_supports_ases ();
14858   *input_line_pointer = ch;
14859   demand_empty_rest_of_line ();
14860 }
14861
14862 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
14863    .option pic2.  It means to generate SVR4 PIC calls.  */
14864
14865 static void
14866 s_abicalls (int ignore ATTRIBUTE_UNUSED)
14867 {
14868   mips_pic = SVR4_PIC;
14869   mips_abicalls = TRUE;
14870
14871   if (g_switch_seen && g_switch_value != 0)
14872     as_warn (_("-G may not be used with SVR4 PIC code"));
14873   g_switch_value = 0;
14874
14875   bfd_set_gp_size (stdoutput, 0);
14876   demand_empty_rest_of_line ();
14877 }
14878
14879 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
14880    PIC code.  It sets the $gp register for the function based on the
14881    function address, which is in the register named in the argument.
14882    This uses a relocation against _gp_disp, which is handled specially
14883    by the linker.  The result is:
14884         lui     $gp,%hi(_gp_disp)
14885         addiu   $gp,$gp,%lo(_gp_disp)
14886         addu    $gp,$gp,.cpload argument
14887    The .cpload argument is normally $25 == $t9.
14888
14889    The -mno-shared option changes this to:
14890         lui     $gp,%hi(__gnu_local_gp)
14891         addiu   $gp,$gp,%lo(__gnu_local_gp)
14892    and the argument is ignored.  This saves an instruction, but the
14893    resulting code is not position independent; it uses an absolute
14894    address for __gnu_local_gp.  Thus code assembled with -mno-shared
14895    can go into an ordinary executable, but not into a shared library.  */
14896
14897 static void
14898 s_cpload (int ignore ATTRIBUTE_UNUSED)
14899 {
14900   expressionS ex;
14901   int reg;
14902   int in_shared;
14903
14904   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
14905      .cpload is ignored.  */
14906   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
14907     {
14908       s_ignore (0);
14909       return;
14910     }
14911
14912   if (mips_opts.mips16)
14913     {
14914       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
14915       ignore_rest_of_line ();
14916       return;
14917     }
14918
14919   /* .cpload should be in a .set noreorder section.  */
14920   if (mips_opts.noreorder == 0)
14921     as_warn (_(".cpload not in noreorder section"));
14922
14923   reg = tc_get_register (0);
14924
14925   /* If we need to produce a 64-bit address, we are better off using
14926      the default instruction sequence.  */
14927   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
14928
14929   ex.X_op = O_symbol;
14930   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
14931                                          "__gnu_local_gp");
14932   ex.X_op_symbol = NULL;
14933   ex.X_add_number = 0;
14934
14935   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
14936   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
14937
14938   mips_mark_labels ();
14939   mips_assembling_insn = TRUE;
14940
14941   macro_start ();
14942   macro_build_lui (&ex, mips_gp_register);
14943   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
14944                mips_gp_register, BFD_RELOC_LO16);
14945   if (in_shared)
14946     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
14947                  mips_gp_register, reg);
14948   macro_end ();
14949
14950   mips_assembling_insn = FALSE;
14951   demand_empty_rest_of_line ();
14952 }
14953
14954 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
14955      .cpsetup $reg1, offset|$reg2, label
14956
14957    If offset is given, this results in:
14958      sd         $gp, offset($sp)
14959      lui        $gp, %hi(%neg(%gp_rel(label)))
14960      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
14961      daddu      $gp, $gp, $reg1
14962
14963    If $reg2 is given, this results in:
14964      daddu      $reg2, $gp, $0
14965      lui        $gp, %hi(%neg(%gp_rel(label)))
14966      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
14967      daddu      $gp, $gp, $reg1
14968    $reg1 is normally $25 == $t9.
14969
14970    The -mno-shared option replaces the last three instructions with
14971         lui     $gp,%hi(_gp)
14972         addiu   $gp,$gp,%lo(_gp)  */
14973
14974 static void
14975 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
14976 {
14977   expressionS ex_off;
14978   expressionS ex_sym;
14979   int reg1;
14980
14981   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
14982      We also need NewABI support.  */
14983   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
14984     {
14985       s_ignore (0);
14986       return;
14987     }
14988
14989   if (mips_opts.mips16)
14990     {
14991       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
14992       ignore_rest_of_line ();
14993       return;
14994     }
14995
14996   reg1 = tc_get_register (0);
14997   SKIP_WHITESPACE ();
14998   if (*input_line_pointer != ',')
14999     {
15000       as_bad (_("missing argument separator ',' for .cpsetup"));
15001       return;
15002     }
15003   else
15004     ++input_line_pointer;
15005   SKIP_WHITESPACE ();
15006   if (*input_line_pointer == '$')
15007     {
15008       mips_cpreturn_register = tc_get_register (0);
15009       mips_cpreturn_offset = -1;
15010     }
15011   else
15012     {
15013       mips_cpreturn_offset = get_absolute_expression ();
15014       mips_cpreturn_register = -1;
15015     }
15016   SKIP_WHITESPACE ();
15017   if (*input_line_pointer != ',')
15018     {
15019       as_bad (_("missing argument separator ',' for .cpsetup"));
15020       return;
15021     }
15022   else
15023     ++input_line_pointer;
15024   SKIP_WHITESPACE ();
15025   expression (&ex_sym);
15026
15027   mips_mark_labels ();
15028   mips_assembling_insn = TRUE;
15029
15030   macro_start ();
15031   if (mips_cpreturn_register == -1)
15032     {
15033       ex_off.X_op = O_constant;
15034       ex_off.X_add_symbol = NULL;
15035       ex_off.X_op_symbol = NULL;
15036       ex_off.X_add_number = mips_cpreturn_offset;
15037
15038       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
15039                    BFD_RELOC_LO16, SP);
15040     }
15041   else
15042     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
15043                  mips_gp_register, 0);
15044
15045   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
15046     {
15047       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
15048                    -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
15049                    BFD_RELOC_HI16_S);
15050
15051       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
15052                    mips_gp_register, -1, BFD_RELOC_GPREL16,
15053                    BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
15054
15055       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
15056                    mips_gp_register, reg1);
15057     }
15058   else
15059     {
15060       expressionS ex;
15061
15062       ex.X_op = O_symbol;
15063       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
15064       ex.X_op_symbol = NULL;
15065       ex.X_add_number = 0;
15066
15067       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
15068       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
15069
15070       macro_build_lui (&ex, mips_gp_register);
15071       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
15072                    mips_gp_register, BFD_RELOC_LO16);
15073     }
15074
15075   macro_end ();
15076
15077   mips_assembling_insn = FALSE;
15078   demand_empty_rest_of_line ();
15079 }
15080
15081 static void
15082 s_cplocal (int ignore ATTRIBUTE_UNUSED)
15083 {
15084   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
15085      .cplocal is ignored.  */
15086   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15087     {
15088       s_ignore (0);
15089       return;
15090     }
15091
15092   if (mips_opts.mips16)
15093     {
15094       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
15095       ignore_rest_of_line ();
15096       return;
15097     }
15098
15099   mips_gp_register = tc_get_register (0);
15100   demand_empty_rest_of_line ();
15101 }
15102
15103 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
15104    offset from $sp.  The offset is remembered, and after making a PIC
15105    call $gp is restored from that location.  */
15106
15107 static void
15108 s_cprestore (int ignore ATTRIBUTE_UNUSED)
15109 {
15110   expressionS ex;
15111
15112   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15113      .cprestore is ignored.  */
15114   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
15115     {
15116       s_ignore (0);
15117       return;
15118     }
15119
15120   if (mips_opts.mips16)
15121     {
15122       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
15123       ignore_rest_of_line ();
15124       return;
15125     }
15126
15127   mips_cprestore_offset = get_absolute_expression ();
15128   mips_cprestore_valid = 1;
15129
15130   ex.X_op = O_constant;
15131   ex.X_add_symbol = NULL;
15132   ex.X_op_symbol = NULL;
15133   ex.X_add_number = mips_cprestore_offset;
15134
15135   mips_mark_labels ();
15136   mips_assembling_insn = TRUE;
15137
15138   macro_start ();
15139   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
15140                                 SP, HAVE_64BIT_ADDRESSES);
15141   macro_end ();
15142
15143   mips_assembling_insn = FALSE;
15144   demand_empty_rest_of_line ();
15145 }
15146
15147 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
15148    was given in the preceding .cpsetup, it results in:
15149      ld         $gp, offset($sp)
15150
15151    If a register $reg2 was given there, it results in:
15152      daddu      $gp, $reg2, $0  */
15153
15154 static void
15155 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
15156 {
15157   expressionS ex;
15158
15159   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
15160      We also need NewABI support.  */
15161   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15162     {
15163       s_ignore (0);
15164       return;
15165     }
15166
15167   if (mips_opts.mips16)
15168     {
15169       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
15170       ignore_rest_of_line ();
15171       return;
15172     }
15173
15174   mips_mark_labels ();
15175   mips_assembling_insn = TRUE;
15176
15177   macro_start ();
15178   if (mips_cpreturn_register == -1)
15179     {
15180       ex.X_op = O_constant;
15181       ex.X_add_symbol = NULL;
15182       ex.X_op_symbol = NULL;
15183       ex.X_add_number = mips_cpreturn_offset;
15184
15185       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
15186     }
15187   else
15188     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
15189                  mips_cpreturn_register, 0);
15190   macro_end ();
15191
15192   mips_assembling_insn = FALSE;
15193   demand_empty_rest_of_line ();
15194 }
15195
15196 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
15197    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
15198    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
15199    debug information or MIPS16 TLS.  */
15200
15201 static void
15202 s_tls_rel_directive (const size_t bytes, const char *dirstr,
15203                      bfd_reloc_code_real_type rtype)
15204 {
15205   expressionS ex;
15206   char *p;
15207
15208   expression (&ex);
15209
15210   if (ex.X_op != O_symbol)
15211     {
15212       as_bad (_("Unsupported use of %s"), dirstr);
15213       ignore_rest_of_line ();
15214     }
15215
15216   p = frag_more (bytes);
15217   md_number_to_chars (p, 0, bytes);
15218   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
15219   demand_empty_rest_of_line ();
15220   mips_clear_insn_labels ();
15221 }
15222
15223 /* Handle .dtprelword.  */
15224
15225 static void
15226 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
15227 {
15228   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
15229 }
15230
15231 /* Handle .dtpreldword.  */
15232
15233 static void
15234 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
15235 {
15236   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
15237 }
15238
15239 /* Handle .tprelword.  */
15240
15241 static void
15242 s_tprelword (int ignore ATTRIBUTE_UNUSED)
15243 {
15244   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
15245 }
15246
15247 /* Handle .tpreldword.  */
15248
15249 static void
15250 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
15251 {
15252   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
15253 }
15254
15255 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
15256    code.  It sets the offset to use in gp_rel relocations.  */
15257
15258 static void
15259 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
15260 {
15261   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
15262      We also need NewABI support.  */
15263   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
15264     {
15265       s_ignore (0);
15266       return;
15267     }
15268
15269   mips_gprel_offset = get_absolute_expression ();
15270
15271   demand_empty_rest_of_line ();
15272 }
15273
15274 /* Handle the .gpword pseudo-op.  This is used when generating PIC
15275    code.  It generates a 32 bit GP relative reloc.  */
15276
15277 static void
15278 s_gpword (int ignore ATTRIBUTE_UNUSED)
15279 {
15280   segment_info_type *si;
15281   struct insn_label_list *l;
15282   expressionS ex;
15283   char *p;
15284
15285   /* When not generating PIC code, this is treated as .word.  */
15286   if (mips_pic != SVR4_PIC)
15287     {
15288       s_cons (2);
15289       return;
15290     }
15291
15292   si = seg_info (now_seg);
15293   l = si->label_list;
15294   mips_emit_delays ();
15295   if (auto_align)
15296     mips_align (2, 0, l);
15297
15298   expression (&ex);
15299   mips_clear_insn_labels ();
15300
15301   if (ex.X_op != O_symbol || ex.X_add_number != 0)
15302     {
15303       as_bad (_("Unsupported use of .gpword"));
15304       ignore_rest_of_line ();
15305     }
15306
15307   p = frag_more (4);
15308   md_number_to_chars (p, 0, 4);
15309   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15310                BFD_RELOC_GPREL32);
15311
15312   demand_empty_rest_of_line ();
15313 }
15314
15315 static void
15316 s_gpdword (int ignore ATTRIBUTE_UNUSED)
15317 {
15318   segment_info_type *si;
15319   struct insn_label_list *l;
15320   expressionS ex;
15321   char *p;
15322
15323   /* When not generating PIC code, this is treated as .dword.  */
15324   if (mips_pic != SVR4_PIC)
15325     {
15326       s_cons (3);
15327       return;
15328     }
15329
15330   si = seg_info (now_seg);
15331   l = si->label_list;
15332   mips_emit_delays ();
15333   if (auto_align)
15334     mips_align (3, 0, l);
15335
15336   expression (&ex);
15337   mips_clear_insn_labels ();
15338
15339   if (ex.X_op != O_symbol || ex.X_add_number != 0)
15340     {
15341       as_bad (_("Unsupported use of .gpdword"));
15342       ignore_rest_of_line ();
15343     }
15344
15345   p = frag_more (8);
15346   md_number_to_chars (p, 0, 8);
15347   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15348                BFD_RELOC_GPREL32)->fx_tcbit = 1;
15349
15350   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
15351   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
15352            FALSE, BFD_RELOC_64)->fx_tcbit = 1;
15353
15354   demand_empty_rest_of_line ();
15355 }
15356
15357 /* Handle the .ehword pseudo-op.  This is used when generating unwinding
15358    tables.  It generates a R_MIPS_EH reloc.  */
15359
15360 static void
15361 s_ehword (int ignore ATTRIBUTE_UNUSED)
15362 {
15363   expressionS ex;
15364   char *p;
15365
15366   mips_emit_delays ();
15367
15368   expression (&ex);
15369   mips_clear_insn_labels ();
15370
15371   if (ex.X_op != O_symbol || ex.X_add_number != 0)
15372     {
15373       as_bad (_("Unsupported use of .ehword"));
15374       ignore_rest_of_line ();
15375     }
15376
15377   p = frag_more (4);
15378   md_number_to_chars (p, 0, 4);
15379   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
15380                BFD_RELOC_MIPS_EH);
15381
15382   demand_empty_rest_of_line ();
15383 }
15384
15385 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
15386    tables in SVR4 PIC code.  */
15387
15388 static void
15389 s_cpadd (int ignore ATTRIBUTE_UNUSED)
15390 {
15391   int reg;
15392
15393   /* This is ignored when not generating SVR4 PIC code.  */
15394   if (mips_pic != SVR4_PIC)
15395     {
15396       s_ignore (0);
15397       return;
15398     }
15399
15400   mips_mark_labels ();
15401   mips_assembling_insn = TRUE;
15402
15403   /* Add $gp to the register named as an argument.  */
15404   macro_start ();
15405   reg = tc_get_register (0);
15406   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
15407   macro_end ();
15408
15409   mips_assembling_insn = FALSE;
15410   demand_empty_rest_of_line ();
15411 }
15412
15413 /* Handle the .insn pseudo-op.  This marks instruction labels in
15414    mips16/micromips mode.  This permits the linker to handle them specially,
15415    such as generating jalx instructions when needed.  We also make
15416    them odd for the duration of the assembly, in order to generate the
15417    right sort of code.  We will make them even in the adjust_symtab
15418    routine, while leaving them marked.  This is convenient for the
15419    debugger and the disassembler.  The linker knows to make them odd
15420    again.  */
15421
15422 static void
15423 s_insn (int ignore ATTRIBUTE_UNUSED)
15424 {
15425   mips_mark_labels ();
15426
15427   demand_empty_rest_of_line ();
15428 }
15429
15430 /* Handle the .nan pseudo-op.  */
15431
15432 static void
15433 s_nan (int ignore ATTRIBUTE_UNUSED)
15434 {
15435   static const char str_legacy[] = "legacy";
15436   static const char str_2008[] = "2008";
15437   size_t i;
15438
15439   for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
15440
15441   if (i == sizeof (str_2008) - 1
15442       && memcmp (input_line_pointer, str_2008, i) == 0)
15443     mips_flag_nan2008 = TRUE;
15444   else if (i == sizeof (str_legacy) - 1
15445            && memcmp (input_line_pointer, str_legacy, i) == 0)
15446     mips_flag_nan2008 = FALSE;
15447   else
15448     as_bad (_("Bad .nan directive"));
15449
15450   input_line_pointer += i;
15451   demand_empty_rest_of_line ();
15452 }
15453
15454 /* Handle a .stab[snd] directive.  Ideally these directives would be
15455    implemented in a transparent way, so that removing them would not
15456    have any effect on the generated instructions.  However, s_stab
15457    internally changes the section, so in practice we need to decide
15458    now whether the preceding label marks compressed code.  We do not
15459    support changing the compression mode of a label after a .stab*
15460    directive, such as in:
15461
15462    foo:
15463         .stabs ...
15464         .set mips16
15465
15466    so the current mode wins.  */
15467
15468 static void
15469 s_mips_stab (int type)
15470 {
15471   mips_mark_labels ();
15472   s_stab (type);
15473 }
15474
15475 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
15476
15477 static void
15478 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
15479 {
15480   char *name;
15481   int c;
15482   symbolS *symbolP;
15483   expressionS exp;
15484
15485   name = input_line_pointer;
15486   c = get_symbol_end ();
15487   symbolP = symbol_find_or_make (name);
15488   S_SET_WEAK (symbolP);
15489   *input_line_pointer = c;
15490
15491   SKIP_WHITESPACE ();
15492
15493   if (! is_end_of_line[(unsigned char) *input_line_pointer])
15494     {
15495       if (S_IS_DEFINED (symbolP))
15496         {
15497           as_bad (_("ignoring attempt to redefine symbol %s"),
15498                   S_GET_NAME (symbolP));
15499           ignore_rest_of_line ();
15500           return;
15501         }
15502
15503       if (*input_line_pointer == ',')
15504         {
15505           ++input_line_pointer;
15506           SKIP_WHITESPACE ();
15507         }
15508
15509       expression (&exp);
15510       if (exp.X_op != O_symbol)
15511         {
15512           as_bad (_("bad .weakext directive"));
15513           ignore_rest_of_line ();
15514           return;
15515         }
15516       symbol_set_value_expression (symbolP, &exp);
15517     }
15518
15519   demand_empty_rest_of_line ();
15520 }
15521
15522 /* Parse a register string into a number.  Called from the ECOFF code
15523    to parse .frame.  The argument is non-zero if this is the frame
15524    register, so that we can record it in mips_frame_reg.  */
15525
15526 int
15527 tc_get_register (int frame)
15528 {
15529   unsigned int reg;
15530
15531   SKIP_WHITESPACE ();
15532   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
15533     reg = 0;
15534   if (frame)
15535     {
15536       mips_frame_reg = reg != 0 ? reg : SP;
15537       mips_frame_reg_valid = 1;
15538       mips_cprestore_valid = 0;
15539     }
15540   return reg;
15541 }
15542
15543 valueT
15544 md_section_align (asection *seg, valueT addr)
15545 {
15546   int align = bfd_get_section_alignment (stdoutput, seg);
15547
15548   /* We don't need to align ELF sections to the full alignment.
15549      However, Irix 5 may prefer that we align them at least to a 16
15550      byte boundary.  We don't bother to align the sections if we
15551      are targeted for an embedded system.  */
15552   if (strncmp (TARGET_OS, "elf", 3) == 0)
15553     return addr;
15554   if (align > 4)
15555     align = 4;
15556
15557   return ((addr + (1 << align) - 1) & (-1 << align));
15558 }
15559
15560 /* Utility routine, called from above as well.  If called while the
15561    input file is still being read, it's only an approximation.  (For
15562    example, a symbol may later become defined which appeared to be
15563    undefined earlier.)  */
15564
15565 static int
15566 nopic_need_relax (symbolS *sym, int before_relaxing)
15567 {
15568   if (sym == 0)
15569     return 0;
15570
15571   if (g_switch_value > 0)
15572     {
15573       const char *symname;
15574       int change;
15575
15576       /* Find out whether this symbol can be referenced off the $gp
15577          register.  It can be if it is smaller than the -G size or if
15578          it is in the .sdata or .sbss section.  Certain symbols can
15579          not be referenced off the $gp, although it appears as though
15580          they can.  */
15581       symname = S_GET_NAME (sym);
15582       if (symname != (const char *) NULL
15583           && (strcmp (symname, "eprol") == 0
15584               || strcmp (symname, "etext") == 0
15585               || strcmp (symname, "_gp") == 0
15586               || strcmp (symname, "edata") == 0
15587               || strcmp (symname, "_fbss") == 0
15588               || strcmp (symname, "_fdata") == 0
15589               || strcmp (symname, "_ftext") == 0
15590               || strcmp (symname, "end") == 0
15591               || strcmp (symname, "_gp_disp") == 0))
15592         change = 1;
15593       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
15594                && (0
15595 #ifndef NO_ECOFF_DEBUGGING
15596                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
15597                        && (symbol_get_obj (sym)->ecoff_extern_size
15598                            <= g_switch_value))
15599 #endif
15600                    /* We must defer this decision until after the whole
15601                       file has been read, since there might be a .extern
15602                       after the first use of this symbol.  */
15603                    || (before_relaxing
15604 #ifndef NO_ECOFF_DEBUGGING
15605                        && symbol_get_obj (sym)->ecoff_extern_size == 0
15606 #endif
15607                        && S_GET_VALUE (sym) == 0)
15608                    || (S_GET_VALUE (sym) != 0
15609                        && S_GET_VALUE (sym) <= g_switch_value)))
15610         change = 0;
15611       else
15612         {
15613           const char *segname;
15614
15615           segname = segment_name (S_GET_SEGMENT (sym));
15616           gas_assert (strcmp (segname, ".lit8") != 0
15617                   && strcmp (segname, ".lit4") != 0);
15618           change = (strcmp (segname, ".sdata") != 0
15619                     && strcmp (segname, ".sbss") != 0
15620                     && strncmp (segname, ".sdata.", 7) != 0
15621                     && strncmp (segname, ".sbss.", 6) != 0
15622                     && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
15623                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
15624         }
15625       return change;
15626     }
15627   else
15628     /* We are not optimizing for the $gp register.  */
15629     return 1;
15630 }
15631
15632
15633 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
15634
15635 static bfd_boolean
15636 pic_need_relax (symbolS *sym, asection *segtype)
15637 {
15638   asection *symsec;
15639
15640   /* Handle the case of a symbol equated to another symbol.  */
15641   while (symbol_equated_reloc_p (sym))
15642     {
15643       symbolS *n;
15644
15645       /* It's possible to get a loop here in a badly written program.  */
15646       n = symbol_get_value_expression (sym)->X_add_symbol;
15647       if (n == sym)
15648         break;
15649       sym = n;
15650     }
15651
15652   if (symbol_section_p (sym))
15653     return TRUE;
15654
15655   symsec = S_GET_SEGMENT (sym);
15656
15657   /* This must duplicate the test in adjust_reloc_syms.  */
15658   return (!bfd_is_und_section (symsec)
15659           && !bfd_is_abs_section (symsec)
15660           && !bfd_is_com_section (symsec)
15661           && !s_is_linkonce (sym, segtype)
15662           /* A global or weak symbol is treated as external.  */
15663           && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
15664 }
15665
15666
15667 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
15668    extended opcode.  SEC is the section the frag is in.  */
15669
15670 static int
15671 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
15672 {
15673   int type;
15674   const struct mips16_immed_operand *op;
15675   offsetT val;
15676   int mintiny, maxtiny;
15677   segT symsec;
15678   fragS *sym_frag;
15679
15680   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
15681     return 0;
15682   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
15683     return 1;
15684
15685   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
15686   op = mips16_immed_operands;
15687   while (op->type != type)
15688     {
15689       ++op;
15690       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
15691     }
15692
15693   if (op->unsp)
15694     {
15695       if (type == '<' || type == '>' || type == '[' || type == ']')
15696         {
15697           mintiny = 1;
15698           maxtiny = 1 << op->nbits;
15699         }
15700       else
15701         {
15702           mintiny = 0;
15703           maxtiny = (1 << op->nbits) - 1;
15704         }
15705     }
15706   else
15707     {
15708       mintiny = - (1 << (op->nbits - 1));
15709       maxtiny = (1 << (op->nbits - 1)) - 1;
15710     }
15711
15712   sym_frag = symbol_get_frag (fragp->fr_symbol);
15713   val = S_GET_VALUE (fragp->fr_symbol);
15714   symsec = S_GET_SEGMENT (fragp->fr_symbol);
15715
15716   if (op->pcrel)
15717     {
15718       addressT addr;
15719
15720       /* We won't have the section when we are called from
15721          mips_relax_frag.  However, we will always have been called
15722          from md_estimate_size_before_relax first.  If this is a
15723          branch to a different section, we mark it as such.  If SEC is
15724          NULL, and the frag is not marked, then it must be a branch to
15725          the same section.  */
15726       if (sec == NULL)
15727         {
15728           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
15729             return 1;
15730         }
15731       else
15732         {
15733           /* Must have been called from md_estimate_size_before_relax.  */
15734           if (symsec != sec)
15735             {
15736               fragp->fr_subtype =
15737                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
15738
15739               /* FIXME: We should support this, and let the linker
15740                  catch branches and loads that are out of range.  */
15741               as_bad_where (fragp->fr_file, fragp->fr_line,
15742                             _("unsupported PC relative reference to different section"));
15743
15744               return 1;
15745             }
15746           if (fragp != sym_frag && sym_frag->fr_address == 0)
15747             /* Assume non-extended on the first relaxation pass.
15748                The address we have calculated will be bogus if this is
15749                a forward branch to another frag, as the forward frag
15750                will have fr_address == 0.  */
15751             return 0;
15752         }
15753
15754       /* In this case, we know for sure that the symbol fragment is in
15755          the same section.  If the relax_marker of the symbol fragment
15756          differs from the relax_marker of this fragment, we have not
15757          yet adjusted the symbol fragment fr_address.  We want to add
15758          in STRETCH in order to get a better estimate of the address.
15759          This particularly matters because of the shift bits.  */
15760       if (stretch != 0
15761           && sym_frag->relax_marker != fragp->relax_marker)
15762         {
15763           fragS *f;
15764
15765           /* Adjust stretch for any alignment frag.  Note that if have
15766              been expanding the earlier code, the symbol may be
15767              defined in what appears to be an earlier frag.  FIXME:
15768              This doesn't handle the fr_subtype field, which specifies
15769              a maximum number of bytes to skip when doing an
15770              alignment.  */
15771           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
15772             {
15773               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
15774                 {
15775                   if (stretch < 0)
15776                     stretch = - ((- stretch)
15777                                  & ~ ((1 << (int) f->fr_offset) - 1));
15778                   else
15779                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
15780                   if (stretch == 0)
15781                     break;
15782                 }
15783             }
15784           if (f != NULL)
15785             val += stretch;
15786         }
15787
15788       addr = fragp->fr_address + fragp->fr_fix;
15789
15790       /* The base address rules are complicated.  The base address of
15791          a branch is the following instruction.  The base address of a
15792          PC relative load or add is the instruction itself, but if it
15793          is in a delay slot (in which case it can not be extended) use
15794          the address of the instruction whose delay slot it is in.  */
15795       if (type == 'p' || type == 'q')
15796         {
15797           addr += 2;
15798
15799           /* If we are currently assuming that this frag should be
15800              extended, then, the current address is two bytes
15801              higher.  */
15802           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
15803             addr += 2;
15804
15805           /* Ignore the low bit in the target, since it will be set
15806              for a text label.  */
15807           if ((val & 1) != 0)
15808             --val;
15809         }
15810       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
15811         addr -= 4;
15812       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
15813         addr -= 2;
15814
15815       val -= addr & ~ ((1 << op->shift) - 1);
15816
15817       /* Branch offsets have an implicit 0 in the lowest bit.  */
15818       if (type == 'p' || type == 'q')
15819         val /= 2;
15820
15821       /* If any of the shifted bits are set, we must use an extended
15822          opcode.  If the address depends on the size of this
15823          instruction, this can lead to a loop, so we arrange to always
15824          use an extended opcode.  We only check this when we are in
15825          the main relaxation loop, when SEC is NULL.  */
15826       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
15827         {
15828           fragp->fr_subtype =
15829             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
15830           return 1;
15831         }
15832
15833       /* If we are about to mark a frag as extended because the value
15834          is precisely maxtiny + 1, then there is a chance of an
15835          infinite loop as in the following code:
15836              la $4,foo
15837              .skip      1020
15838              .align     2
15839            foo:
15840          In this case when the la is extended, foo is 0x3fc bytes
15841          away, so the la can be shrunk, but then foo is 0x400 away, so
15842          the la must be extended.  To avoid this loop, we mark the
15843          frag as extended if it was small, and is about to become
15844          extended with a value of maxtiny + 1.  */
15845       if (val == ((maxtiny + 1) << op->shift)
15846           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
15847           && sec == NULL)
15848         {
15849           fragp->fr_subtype =
15850             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
15851           return 1;
15852         }
15853     }
15854   else if (symsec != absolute_section && sec != NULL)
15855     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
15856
15857   if ((val & ((1 << op->shift) - 1)) != 0
15858       || val < (mintiny << op->shift)
15859       || val > (maxtiny << op->shift))
15860     return 1;
15861   else
15862     return 0;
15863 }
15864
15865 /* Compute the length of a branch sequence, and adjust the
15866    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
15867    worst-case length is computed, with UPDATE being used to indicate
15868    whether an unconditional (-1), branch-likely (+1) or regular (0)
15869    branch is to be computed.  */
15870 static int
15871 relaxed_branch_length (fragS *fragp, asection *sec, int update)
15872 {
15873   bfd_boolean toofar;
15874   int length;
15875
15876   if (fragp
15877       && S_IS_DEFINED (fragp->fr_symbol)
15878       && sec == S_GET_SEGMENT (fragp->fr_symbol))
15879     {
15880       addressT addr;
15881       offsetT val;
15882
15883       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
15884
15885       addr = fragp->fr_address + fragp->fr_fix + 4;
15886
15887       val -= addr;
15888
15889       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
15890     }
15891   else if (fragp)
15892     /* If the symbol is not defined or it's in a different segment,
15893        assume the user knows what's going on and emit a short
15894        branch.  */
15895     toofar = FALSE;
15896   else
15897     toofar = TRUE;
15898
15899   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
15900     fragp->fr_subtype
15901       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
15902                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
15903                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
15904                              RELAX_BRANCH_LINK (fragp->fr_subtype),
15905                              toofar);
15906
15907   length = 4;
15908   if (toofar)
15909     {
15910       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
15911         length += 8;
15912
15913       if (mips_pic != NO_PIC)
15914         {
15915           /* Additional space for PIC loading of target address.  */
15916           length += 8;
15917           if (mips_opts.isa == ISA_MIPS1)
15918             /* Additional space for $at-stabilizing nop.  */
15919             length += 4;
15920         }
15921
15922       /* If branch is conditional.  */
15923       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
15924         length += 8;
15925     }
15926
15927   return length;
15928 }
15929
15930 /* Compute the length of a branch sequence, and adjust the
15931    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
15932    worst-case length is computed, with UPDATE being used to indicate
15933    whether an unconditional (-1), or regular (0) branch is to be
15934    computed.  */
15935
15936 static int
15937 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
15938 {
15939   bfd_boolean toofar;
15940   int length;
15941
15942   if (fragp
15943       && S_IS_DEFINED (fragp->fr_symbol)
15944       && sec == S_GET_SEGMENT (fragp->fr_symbol))
15945     {
15946       addressT addr;
15947       offsetT val;
15948
15949       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
15950       /* Ignore the low bit in the target, since it will be set
15951          for a text label.  */
15952       if ((val & 1) != 0)
15953         --val;
15954
15955       addr = fragp->fr_address + fragp->fr_fix + 4;
15956
15957       val -= addr;
15958
15959       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
15960     }
15961   else if (fragp)
15962     /* If the symbol is not defined or it's in a different segment,
15963        assume the user knows what's going on and emit a short
15964        branch.  */
15965     toofar = FALSE;
15966   else
15967     toofar = TRUE;
15968
15969   if (fragp && update
15970       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
15971     fragp->fr_subtype = (toofar
15972                          ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
15973                          : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
15974
15975   length = 4;
15976   if (toofar)
15977     {
15978       bfd_boolean compact_known = fragp != NULL;
15979       bfd_boolean compact = FALSE;
15980       bfd_boolean uncond;
15981
15982       if (compact_known)
15983         compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
15984       if (fragp)
15985         uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
15986       else
15987         uncond = update < 0;
15988
15989       /* If label is out of range, we turn branch <br>:
15990
15991                 <br>    label                   # 4 bytes
15992             0:
15993
15994          into:
15995
15996                 j       label                   # 4 bytes
15997                 nop                             # 2 bytes if compact && !PIC
15998             0:
15999        */
16000       if (mips_pic == NO_PIC && (!compact_known || compact))
16001         length += 2;
16002
16003       /* If assembling PIC code, we further turn:
16004
16005                         j       label                   # 4 bytes
16006
16007          into:
16008
16009                         lw/ld   at, %got(label)(gp)     # 4 bytes
16010                         d/addiu at, %lo(label)          # 4 bytes
16011                         jr/c    at                      # 2 bytes
16012        */
16013       if (mips_pic != NO_PIC)
16014         length += 6;
16015
16016       /* If branch <br> is conditional, we prepend negated branch <brneg>:
16017
16018                         <brneg> 0f                      # 4 bytes
16019                         nop                             # 2 bytes if !compact
16020        */
16021       if (!uncond)
16022         length += (compact_known && compact) ? 4 : 6;
16023     }
16024
16025   return length;
16026 }
16027
16028 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
16029    bit accordingly.  */
16030
16031 static int
16032 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
16033 {
16034   bfd_boolean toofar;
16035
16036   if (fragp
16037       && S_IS_DEFINED (fragp->fr_symbol)
16038       && sec == S_GET_SEGMENT (fragp->fr_symbol))
16039     {
16040       addressT addr;
16041       offsetT val;
16042       int type;
16043
16044       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16045       /* Ignore the low bit in the target, since it will be set
16046          for a text label.  */
16047       if ((val & 1) != 0)
16048         --val;
16049
16050       /* Assume this is a 2-byte branch.  */
16051       addr = fragp->fr_address + fragp->fr_fix + 2;
16052
16053       /* We try to avoid the infinite loop by not adding 2 more bytes for
16054          long branches.  */
16055
16056       val -= addr;
16057
16058       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16059       if (type == 'D')
16060         toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
16061       else if (type == 'E')
16062         toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
16063       else
16064         abort ();
16065     }
16066   else
16067     /* If the symbol is not defined or it's in a different segment,
16068        we emit a normal 32-bit branch.  */
16069     toofar = TRUE;
16070
16071   if (fragp && update
16072       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16073     fragp->fr_subtype
16074       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
16075                : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
16076
16077   if (toofar)
16078     return 4;
16079
16080   return 2;
16081 }
16082
16083 /* Estimate the size of a frag before relaxing.  Unless this is the
16084    mips16, we are not really relaxing here, and the final size is
16085    encoded in the subtype information.  For the mips16, we have to
16086    decide whether we are using an extended opcode or not.  */
16087
16088 int
16089 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
16090 {
16091   int change;
16092
16093   if (RELAX_BRANCH_P (fragp->fr_subtype))
16094     {
16095
16096       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
16097
16098       return fragp->fr_var;
16099     }
16100
16101   if (RELAX_MIPS16_P (fragp->fr_subtype))
16102     /* We don't want to modify the EXTENDED bit here; it might get us
16103        into infinite loops.  We change it only in mips_relax_frag().  */
16104     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
16105
16106   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16107     {
16108       int length = 4;
16109
16110       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16111         length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
16112       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16113         length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
16114       fragp->fr_var = length;
16115
16116       return length;
16117     }
16118
16119   if (mips_pic == NO_PIC)
16120     change = nopic_need_relax (fragp->fr_symbol, 0);
16121   else if (mips_pic == SVR4_PIC)
16122     change = pic_need_relax (fragp->fr_symbol, segtype);
16123   else if (mips_pic == VXWORKS_PIC)
16124     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
16125     change = 0;
16126   else
16127     abort ();
16128
16129   if (change)
16130     {
16131       fragp->fr_subtype |= RELAX_USE_SECOND;
16132       return -RELAX_FIRST (fragp->fr_subtype);
16133     }
16134   else
16135     return -RELAX_SECOND (fragp->fr_subtype);
16136 }
16137
16138 /* This is called to see whether a reloc against a defined symbol
16139    should be converted into a reloc against a section.  */
16140
16141 int
16142 mips_fix_adjustable (fixS *fixp)
16143 {
16144   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
16145       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16146     return 0;
16147
16148   if (fixp->fx_addsy == NULL)
16149     return 1;
16150
16151   /* If symbol SYM is in a mergeable section, relocations of the form
16152      SYM + 0 can usually be made section-relative.  The mergeable data
16153      is then identified by the section offset rather than by the symbol.
16154
16155      However, if we're generating REL LO16 relocations, the offset is split
16156      between the LO16 and parterning high part relocation.  The linker will
16157      need to recalculate the complete offset in order to correctly identify
16158      the merge data.
16159
16160      The linker has traditionally not looked for the parterning high part
16161      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
16162      placed anywhere.  Rather than break backwards compatibility by changing
16163      this, it seems better not to force the issue, and instead keep the
16164      original symbol.  This will work with either linker behavior.  */
16165   if ((lo16_reloc_p (fixp->fx_r_type)
16166        || reloc_needs_lo_p (fixp->fx_r_type))
16167       && HAVE_IN_PLACE_ADDENDS
16168       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
16169     return 0;
16170
16171   /* There is no place to store an in-place offset for JALR relocations.
16172      Likewise an in-range offset of limited PC-relative relocations may
16173      overflow the in-place relocatable field if recalculated against the
16174      start address of the symbol's containing section.  */
16175   if (HAVE_IN_PLACE_ADDENDS
16176       && (limited_pcrel_reloc_p (fixp->fx_r_type)
16177           || jalr_reloc_p (fixp->fx_r_type)))
16178     return 0;
16179
16180   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
16181      to a floating-point stub.  The same is true for non-R_MIPS16_26
16182      relocations against MIPS16 functions; in this case, the stub becomes
16183      the function's canonical address.
16184
16185      Floating-point stubs are stored in unique .mips16.call.* or
16186      .mips16.fn.* sections.  If a stub T for function F is in section S,
16187      the first relocation in section S must be against F; this is how the
16188      linker determines the target function.  All relocations that might
16189      resolve to T must also be against F.  We therefore have the following
16190      restrictions, which are given in an intentionally-redundant way:
16191
16192        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
16193           symbols.
16194
16195        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
16196           if that stub might be used.
16197
16198        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
16199           symbols.
16200
16201        4. We cannot reduce a stub's relocations against MIPS16 symbols if
16202           that stub might be used.
16203
16204      There is a further restriction:
16205
16206        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
16207           R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
16208           targets with in-place addends; the relocation field cannot
16209           encode the low bit.
16210
16211      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
16212      against a MIPS16 symbol.  We deal with (5) by by not reducing any
16213      such relocations on REL targets.
16214
16215      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
16216      relocation against some symbol R, no relocation against R may be
16217      reduced.  (Note that this deals with (2) as well as (1) because
16218      relocations against global symbols will never be reduced on ELF
16219      targets.)  This approach is a little simpler than trying to detect
16220      stub sections, and gives the "all or nothing" per-symbol consistency
16221      that we have for MIPS16 symbols.  */
16222   if (fixp->fx_subsy == NULL
16223       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
16224           || *symbol_get_tc (fixp->fx_addsy)
16225           || (HAVE_IN_PLACE_ADDENDS
16226               && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
16227               && jmp_reloc_p (fixp->fx_r_type))))
16228     return 0;
16229
16230   return 1;
16231 }
16232
16233 /* Translate internal representation of relocation info to BFD target
16234    format.  */
16235
16236 arelent **
16237 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
16238 {
16239   static arelent *retval[4];
16240   arelent *reloc;
16241   bfd_reloc_code_real_type code;
16242
16243   memset (retval, 0, sizeof(retval));
16244   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
16245   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
16246   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
16247   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
16248
16249   if (fixp->fx_pcrel)
16250     {
16251       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
16252                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
16253                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
16254                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
16255                   || fixp->fx_r_type == BFD_RELOC_32_PCREL);
16256
16257       /* At this point, fx_addnumber is "symbol offset - pcrel address".
16258          Relocations want only the symbol offset.  */
16259       reloc->addend = fixp->fx_addnumber + reloc->address;
16260     }
16261   else
16262     reloc->addend = fixp->fx_addnumber;
16263
16264   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
16265      entry to be used in the relocation's section offset.  */
16266   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
16267     {
16268       reloc->address = reloc->addend;
16269       reloc->addend = 0;
16270     }
16271
16272   code = fixp->fx_r_type;
16273
16274   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
16275   if (reloc->howto == NULL)
16276     {
16277       as_bad_where (fixp->fx_file, fixp->fx_line,
16278                     _("Can not represent %s relocation in this object file format"),
16279                     bfd_get_reloc_code_name (code));
16280       retval[0] = NULL;
16281     }
16282
16283   return retval;
16284 }
16285
16286 /* Relax a machine dependent frag.  This returns the amount by which
16287    the current size of the frag should change.  */
16288
16289 int
16290 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
16291 {
16292   if (RELAX_BRANCH_P (fragp->fr_subtype))
16293     {
16294       offsetT old_var = fragp->fr_var;
16295
16296       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
16297
16298       return fragp->fr_var - old_var;
16299     }
16300
16301   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16302     {
16303       offsetT old_var = fragp->fr_var;
16304       offsetT new_var = 4;
16305
16306       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
16307         new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
16308       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
16309         new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
16310       fragp->fr_var = new_var;
16311
16312       return new_var - old_var;
16313     }
16314
16315   if (! RELAX_MIPS16_P (fragp->fr_subtype))
16316     return 0;
16317
16318   if (mips16_extended_frag (fragp, NULL, stretch))
16319     {
16320       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16321         return 0;
16322       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
16323       return 2;
16324     }
16325   else
16326     {
16327       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16328         return 0;
16329       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
16330       return -2;
16331     }
16332
16333   return 0;
16334 }
16335
16336 /* Convert a machine dependent frag.  */
16337
16338 void
16339 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
16340 {
16341   if (RELAX_BRANCH_P (fragp->fr_subtype))
16342     {
16343       char *buf;
16344       unsigned long insn;
16345       expressionS exp;
16346       fixS *fixp;
16347
16348       buf = fragp->fr_literal + fragp->fr_fix;
16349       insn = read_insn (buf);
16350
16351       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16352         {
16353           /* We generate a fixup instead of applying it right now
16354              because, if there are linker relaxations, we're going to
16355              need the relocations.  */
16356           exp.X_op = O_symbol;
16357           exp.X_add_symbol = fragp->fr_symbol;
16358           exp.X_add_number = fragp->fr_offset;
16359
16360           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16361                               BFD_RELOC_16_PCREL_S2);
16362           fixp->fx_file = fragp->fr_file;
16363           fixp->fx_line = fragp->fr_line;
16364
16365           buf = write_insn (buf, insn);
16366         }
16367       else
16368         {
16369           int i;
16370
16371           as_warn_where (fragp->fr_file, fragp->fr_line,
16372                          _("Relaxed out-of-range branch into a jump"));
16373
16374           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
16375             goto uncond;
16376
16377           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16378             {
16379               /* Reverse the branch.  */
16380               switch ((insn >> 28) & 0xf)
16381                 {
16382                 case 4:
16383                   /* bc[0-3][tf]l? instructions can have the condition
16384                      reversed by tweaking a single TF bit, and their
16385                      opcodes all have 0x4???????.  */
16386                   gas_assert ((insn & 0xf3e00000) == 0x41000000);
16387                   insn ^= 0x00010000;
16388                   break;
16389
16390                 case 0:
16391                   /* bltz       0x04000000      bgez    0x04010000
16392                      bltzal     0x04100000      bgezal  0x04110000  */
16393                   gas_assert ((insn & 0xfc0e0000) == 0x04000000);
16394                   insn ^= 0x00010000;
16395                   break;
16396
16397                 case 1:
16398                   /* beq        0x10000000      bne     0x14000000
16399                      blez       0x18000000      bgtz    0x1c000000  */
16400                   insn ^= 0x04000000;
16401                   break;
16402
16403                 default:
16404                   abort ();
16405                 }
16406             }
16407
16408           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16409             {
16410               /* Clear the and-link bit.  */
16411               gas_assert ((insn & 0xfc1c0000) == 0x04100000);
16412
16413               /* bltzal         0x04100000      bgezal  0x04110000
16414                  bltzall        0x04120000      bgezall 0x04130000  */
16415               insn &= ~0x00100000;
16416             }
16417
16418           /* Branch over the branch (if the branch was likely) or the
16419              full jump (not likely case).  Compute the offset from the
16420              current instruction to branch to.  */
16421           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16422             i = 16;
16423           else
16424             {
16425               /* How many bytes in instructions we've already emitted?  */
16426               i = buf - fragp->fr_literal - fragp->fr_fix;
16427               /* How many bytes in instructions from here to the end?  */
16428               i = fragp->fr_var - i;
16429             }
16430           /* Convert to instruction count.  */
16431           i >>= 2;
16432           /* Branch counts from the next instruction.  */
16433           i--;
16434           insn |= i;
16435           /* Branch over the jump.  */
16436           buf = write_insn (buf, insn);
16437
16438           /* nop */
16439           buf = write_insn (buf, 0);
16440
16441           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
16442             {
16443               /* beql $0, $0, 2f */
16444               insn = 0x50000000;
16445               /* Compute the PC offset from the current instruction to
16446                  the end of the variable frag.  */
16447               /* How many bytes in instructions we've already emitted?  */
16448               i = buf - fragp->fr_literal - fragp->fr_fix;
16449               /* How many bytes in instructions from here to the end?  */
16450               i = fragp->fr_var - i;
16451               /* Convert to instruction count.  */
16452               i >>= 2;
16453               /* Don't decrement i, because we want to branch over the
16454                  delay slot.  */
16455               insn |= i;
16456
16457               buf = write_insn (buf, insn);
16458               buf = write_insn (buf, 0);
16459             }
16460
16461         uncond:
16462           if (mips_pic == NO_PIC)
16463             {
16464               /* j or jal.  */
16465               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
16466                       ? 0x0c000000 : 0x08000000);
16467               exp.X_op = O_symbol;
16468               exp.X_add_symbol = fragp->fr_symbol;
16469               exp.X_add_number = fragp->fr_offset;
16470
16471               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16472                                   FALSE, BFD_RELOC_MIPS_JMP);
16473               fixp->fx_file = fragp->fr_file;
16474               fixp->fx_line = fragp->fr_line;
16475
16476               buf = write_insn (buf, insn);
16477             }
16478           else
16479             {
16480               unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
16481
16482               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
16483               insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
16484               insn |= at << OP_SH_RT;
16485               exp.X_op = O_symbol;
16486               exp.X_add_symbol = fragp->fr_symbol;
16487               exp.X_add_number = fragp->fr_offset;
16488
16489               if (fragp->fr_offset)
16490                 {
16491                   exp.X_add_symbol = make_expr_symbol (&exp);
16492                   exp.X_add_number = 0;
16493                 }
16494
16495               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16496                                   FALSE, BFD_RELOC_MIPS_GOT16);
16497               fixp->fx_file = fragp->fr_file;
16498               fixp->fx_line = fragp->fr_line;
16499
16500               buf = write_insn (buf, insn);
16501
16502               if (mips_opts.isa == ISA_MIPS1)
16503                 /* nop */
16504                 buf = write_insn (buf, 0);
16505
16506               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
16507               insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
16508               insn |= at << OP_SH_RS | at << OP_SH_RT;
16509
16510               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
16511                                   FALSE, BFD_RELOC_LO16);
16512               fixp->fx_file = fragp->fr_file;
16513               fixp->fx_line = fragp->fr_line;
16514
16515               buf = write_insn (buf, insn);
16516
16517               /* j(al)r $at.  */
16518               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
16519                 insn = 0x0000f809;
16520               else
16521                 insn = 0x00000008;
16522               insn |= at << OP_SH_RS;
16523
16524               buf = write_insn (buf, insn);
16525             }
16526         }
16527
16528       fragp->fr_fix += fragp->fr_var;
16529       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
16530       return;
16531     }
16532
16533   /* Relax microMIPS branches.  */
16534   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
16535     {
16536       char *buf = fragp->fr_literal + fragp->fr_fix;
16537       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16538       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
16539       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
16540       bfd_boolean short_ds;
16541       unsigned long insn;
16542       expressionS exp;
16543       fixS *fixp;
16544
16545       exp.X_op = O_symbol;
16546       exp.X_add_symbol = fragp->fr_symbol;
16547       exp.X_add_number = fragp->fr_offset;
16548
16549       fragp->fr_fix += fragp->fr_var;
16550
16551       /* Handle 16-bit branches that fit or are forced to fit.  */
16552       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
16553         {
16554           /* We generate a fixup instead of applying it right now,
16555              because if there is linker relaxation, we're going to
16556              need the relocations.  */
16557           if (type == 'D')
16558             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
16559                                 BFD_RELOC_MICROMIPS_10_PCREL_S1);
16560           else if (type == 'E')
16561             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
16562                                 BFD_RELOC_MICROMIPS_7_PCREL_S1);
16563           else
16564             abort ();
16565
16566           fixp->fx_file = fragp->fr_file;
16567           fixp->fx_line = fragp->fr_line;
16568
16569           /* These relocations can have an addend that won't fit in
16570              2 octets.  */
16571           fixp->fx_no_overflow = 1;
16572
16573           return;
16574         }
16575
16576       /* Handle 32-bit branches that fit or are forced to fit.  */
16577       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16578           || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16579         {
16580           /* We generate a fixup instead of applying it right now,
16581              because if there is linker relaxation, we're going to
16582              need the relocations.  */
16583           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
16584                               BFD_RELOC_MICROMIPS_16_PCREL_S1);
16585           fixp->fx_file = fragp->fr_file;
16586           fixp->fx_line = fragp->fr_line;
16587
16588           if (type == 0)
16589             return;
16590         }
16591
16592       /* Relax 16-bit branches to 32-bit branches.  */
16593       if (type != 0)
16594         {
16595           insn = read_compressed_insn (buf, 2);
16596
16597           if ((insn & 0xfc00) == 0xcc00)                /* b16  */
16598             insn = 0x94000000;                          /* beq  */
16599           else if ((insn & 0xdc00) == 0x8c00)           /* beqz16/bnez16  */
16600             {
16601               unsigned long regno;
16602
16603               regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
16604               regno = micromips_to_32_reg_d_map [regno];
16605               insn = ((insn & 0x2000) << 16) | 0x94000000;      /* beq/bne  */
16606               insn |= regno << MICROMIPSOP_SH_RS;
16607             }
16608           else
16609             abort ();
16610
16611           /* Nothing else to do, just write it out.  */
16612           if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
16613               || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16614             {
16615               buf = write_compressed_insn (buf, insn, 4);
16616               gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
16617               return;
16618             }
16619         }
16620       else
16621         insn = read_compressed_insn (buf, 4);
16622
16623       /* Relax 32-bit branches to a sequence of instructions.  */
16624       as_warn_where (fragp->fr_file, fragp->fr_line,
16625                      _("Relaxed out-of-range branch into a jump"));
16626
16627       /* Set the short-delay-slot bit.  */
16628       short_ds = al && (insn & 0x02000000) != 0;
16629
16630       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
16631         {
16632           symbolS *l;
16633
16634           /* Reverse the branch.  */
16635           if ((insn & 0xfc000000) == 0x94000000                 /* beq  */
16636               || (insn & 0xfc000000) == 0xb4000000)             /* bne  */
16637             insn ^= 0x20000000;
16638           else if ((insn & 0xffe00000) == 0x40000000            /* bltz  */
16639                    || (insn & 0xffe00000) == 0x40400000         /* bgez  */
16640                    || (insn & 0xffe00000) == 0x40800000         /* blez  */
16641                    || (insn & 0xffe00000) == 0x40c00000         /* bgtz  */
16642                    || (insn & 0xffe00000) == 0x40a00000         /* bnezc  */
16643                    || (insn & 0xffe00000) == 0x40e00000         /* beqzc  */
16644                    || (insn & 0xffe00000) == 0x40200000         /* bltzal  */
16645                    || (insn & 0xffe00000) == 0x40600000         /* bgezal  */
16646                    || (insn & 0xffe00000) == 0x42200000         /* bltzals  */
16647                    || (insn & 0xffe00000) == 0x42600000)        /* bgezals  */
16648             insn ^= 0x00400000;
16649           else if ((insn & 0xffe30000) == 0x43800000            /* bc1f  */
16650                    || (insn & 0xffe30000) == 0x43a00000         /* bc1t  */
16651                    || (insn & 0xffe30000) == 0x42800000         /* bc2f  */
16652                    || (insn & 0xffe30000) == 0x42a00000)        /* bc2t  */
16653             insn ^= 0x00200000;
16654           else
16655             abort ();
16656
16657           if (al)
16658             {
16659               /* Clear the and-link and short-delay-slot bits.  */
16660               gas_assert ((insn & 0xfda00000) == 0x40200000);
16661
16662               /* bltzal  0x40200000     bgezal  0x40600000  */
16663               /* bltzals 0x42200000     bgezals 0x42600000  */
16664               insn &= ~0x02200000;
16665             }
16666
16667           /* Make a label at the end for use with the branch.  */
16668           l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
16669           micromips_label_inc ();
16670           S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
16671
16672           /* Refer to it.  */
16673           fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
16674                           BFD_RELOC_MICROMIPS_16_PCREL_S1);
16675           fixp->fx_file = fragp->fr_file;
16676           fixp->fx_line = fragp->fr_line;
16677
16678           /* Branch over the jump.  */
16679           buf = write_compressed_insn (buf, insn, 4);
16680           if (!compact)
16681             /* nop */
16682             buf = write_compressed_insn (buf, 0x0c00, 2);
16683         }
16684
16685       if (mips_pic == NO_PIC)
16686         {
16687           unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
16688
16689           /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
16690           insn = al ? jal : 0xd4000000;
16691
16692           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16693                               BFD_RELOC_MICROMIPS_JMP);
16694           fixp->fx_file = fragp->fr_file;
16695           fixp->fx_line = fragp->fr_line;
16696
16697           buf = write_compressed_insn (buf, insn, 4);
16698           if (compact)
16699             /* nop */
16700             buf = write_compressed_insn (buf, 0x0c00, 2);
16701         }
16702       else
16703         {
16704           unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
16705           unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;      /* jalr/s  */
16706           unsigned long jr = compact ? 0x45a0 : 0x4580;         /* jr/c  */
16707
16708           /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
16709           insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
16710           insn |= at << MICROMIPSOP_SH_RT;
16711
16712           if (exp.X_add_number)
16713             {
16714               exp.X_add_symbol = make_expr_symbol (&exp);
16715               exp.X_add_number = 0;
16716             }
16717
16718           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16719                               BFD_RELOC_MICROMIPS_GOT16);
16720           fixp->fx_file = fragp->fr_file;
16721           fixp->fx_line = fragp->fr_line;
16722
16723           buf = write_compressed_insn (buf, insn, 4);
16724
16725           /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
16726           insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
16727           insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
16728
16729           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
16730                               BFD_RELOC_MICROMIPS_LO16);
16731           fixp->fx_file = fragp->fr_file;
16732           fixp->fx_line = fragp->fr_line;
16733
16734           buf = write_compressed_insn (buf, insn, 4);
16735
16736           /* jr/jrc/jalr/jalrs $at  */
16737           insn = al ? jalr : jr;
16738           insn |= at << MICROMIPSOP_SH_MJ;
16739
16740           buf = write_compressed_insn (buf, insn, 2);
16741         }
16742
16743       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
16744       return;
16745     }
16746
16747   if (RELAX_MIPS16_P (fragp->fr_subtype))
16748     {
16749       int type;
16750       const struct mips16_immed_operand *op;
16751       offsetT val;
16752       char *buf;
16753       unsigned int user_length, length;
16754       unsigned long insn;
16755       bfd_boolean ext;
16756
16757       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
16758       op = mips16_immed_operands;
16759       while (op->type != type)
16760         ++op;
16761
16762       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
16763       val = resolve_symbol_value (fragp->fr_symbol);
16764       if (op->pcrel)
16765         {
16766           addressT addr;
16767
16768           addr = fragp->fr_address + fragp->fr_fix;
16769
16770           /* The rules for the base address of a PC relative reloc are
16771              complicated; see mips16_extended_frag.  */
16772           if (type == 'p' || type == 'q')
16773             {
16774               addr += 2;
16775               if (ext)
16776                 addr += 2;
16777               /* Ignore the low bit in the target, since it will be
16778                  set for a text label.  */
16779               if ((val & 1) != 0)
16780                 --val;
16781             }
16782           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16783             addr -= 4;
16784           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16785             addr -= 2;
16786
16787           addr &= ~ (addressT) ((1 << op->shift) - 1);
16788           val -= addr;
16789
16790           /* Make sure the section winds up with the alignment we have
16791              assumed.  */
16792           if (op->shift > 0)
16793             record_alignment (asec, op->shift);
16794         }
16795
16796       if (ext
16797           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
16798               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
16799         as_warn_where (fragp->fr_file, fragp->fr_line,
16800                        _("extended instruction in delay slot"));
16801
16802       buf = fragp->fr_literal + fragp->fr_fix;
16803
16804       insn = read_compressed_insn (buf, 2);
16805       if (ext)
16806         insn |= MIPS16_EXTEND;
16807
16808       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
16809         user_length = 4;
16810       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
16811         user_length = 2;
16812       else
16813         user_length = 0;
16814
16815       mips16_immed (fragp->fr_file, fragp->fr_line, type,
16816                     BFD_RELOC_UNUSED, val, user_length, &insn);
16817
16818       length = (ext ? 4 : 2);
16819       gas_assert (mips16_opcode_length (insn) == length);
16820       write_compressed_insn (buf, insn, length);
16821       fragp->fr_fix += length;
16822     }
16823   else
16824     {
16825       relax_substateT subtype = fragp->fr_subtype;
16826       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
16827       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
16828       int first, second;
16829       fixS *fixp;
16830
16831       first = RELAX_FIRST (subtype);
16832       second = RELAX_SECOND (subtype);
16833       fixp = (fixS *) fragp->fr_opcode;
16834
16835       /* If the delay slot chosen does not match the size of the instruction,
16836          then emit a warning.  */
16837       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
16838            || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
16839         {
16840           relax_substateT s;
16841           const char *msg;
16842
16843           s = subtype & (RELAX_DELAY_SLOT_16BIT
16844                          | RELAX_DELAY_SLOT_SIZE_FIRST
16845                          | RELAX_DELAY_SLOT_SIZE_SECOND);
16846           msg = macro_warning (s);
16847           if (msg != NULL)
16848             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
16849           subtype &= ~s;
16850         }
16851
16852       /* Possibly emit a warning if we've chosen the longer option.  */
16853       if (use_second == second_longer)
16854         {
16855           relax_substateT s;
16856           const char *msg;
16857
16858           s = (subtype
16859                & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
16860           msg = macro_warning (s);
16861           if (msg != NULL)
16862             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
16863           subtype &= ~s;
16864         }
16865
16866       /* Go through all the fixups for the first sequence.  Disable them
16867          (by marking them as done) if we're going to use the second
16868          sequence instead.  */
16869       while (fixp
16870              && fixp->fx_frag == fragp
16871              && fixp->fx_where < fragp->fr_fix - second)
16872         {
16873           if (subtype & RELAX_USE_SECOND)
16874             fixp->fx_done = 1;
16875           fixp = fixp->fx_next;
16876         }
16877
16878       /* Go through the fixups for the second sequence.  Disable them if
16879          we're going to use the first sequence, otherwise adjust their
16880          addresses to account for the relaxation.  */
16881       while (fixp && fixp->fx_frag == fragp)
16882         {
16883           if (subtype & RELAX_USE_SECOND)
16884             fixp->fx_where -= first;
16885           else
16886             fixp->fx_done = 1;
16887           fixp = fixp->fx_next;
16888         }
16889
16890       /* Now modify the frag contents.  */
16891       if (subtype & RELAX_USE_SECOND)
16892         {
16893           char *start;
16894
16895           start = fragp->fr_literal + fragp->fr_fix - first - second;
16896           memmove (start, start + first, second);
16897           fragp->fr_fix -= first;
16898         }
16899       else
16900         fragp->fr_fix -= second;
16901     }
16902 }
16903
16904 /* This function is called after the relocs have been generated.
16905    We've been storing mips16 text labels as odd.  Here we convert them
16906    back to even for the convenience of the debugger.  */
16907
16908 void
16909 mips_frob_file_after_relocs (void)
16910 {
16911   asymbol **syms;
16912   unsigned int count, i;
16913
16914   syms = bfd_get_outsymbols (stdoutput);
16915   count = bfd_get_symcount (stdoutput);
16916   for (i = 0; i < count; i++, syms++)
16917     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
16918         && ((*syms)->value & 1) != 0)
16919       {
16920         (*syms)->value &= ~1;
16921         /* If the symbol has an odd size, it was probably computed
16922            incorrectly, so adjust that as well.  */
16923         if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
16924           ++elf_symbol (*syms)->internal_elf_sym.st_size;
16925       }
16926 }
16927
16928 /* This function is called whenever a label is defined, including fake
16929    labels instantiated off the dot special symbol.  It is used when
16930    handling branch delays; if a branch has a label, we assume we cannot
16931    move it.  This also bumps the value of the symbol by 1 in compressed
16932    code.  */
16933
16934 static void
16935 mips_record_label (symbolS *sym)
16936 {
16937   segment_info_type *si = seg_info (now_seg);
16938   struct insn_label_list *l;
16939
16940   if (free_insn_labels == NULL)
16941     l = (struct insn_label_list *) xmalloc (sizeof *l);
16942   else
16943     {
16944       l = free_insn_labels;
16945       free_insn_labels = l->next;
16946     }
16947
16948   l->label = sym;
16949   l->next = si->label_list;
16950   si->label_list = l;
16951 }
16952
16953 /* This function is called as tc_frob_label() whenever a label is defined
16954    and adds a DWARF-2 record we only want for true labels.  */
16955
16956 void
16957 mips_define_label (symbolS *sym)
16958 {
16959   mips_record_label (sym);
16960   dwarf2_emit_label (sym);
16961 }
16962
16963 /* This function is called by tc_new_dot_label whenever a new dot symbol
16964    is defined.  */
16965
16966 void
16967 mips_add_dot_label (symbolS *sym)
16968 {
16969   mips_record_label (sym);
16970   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
16971     mips_compressed_mark_label (sym);
16972 }
16973 \f
16974 /* Some special processing for a MIPS ELF file.  */
16975
16976 void
16977 mips_elf_final_processing (void)
16978 {
16979   /* Write out the register information.  */
16980   if (mips_abi != N64_ABI)
16981     {
16982       Elf32_RegInfo s;
16983
16984       s.ri_gprmask = mips_gprmask;
16985       s.ri_cprmask[0] = mips_cprmask[0];
16986       s.ri_cprmask[1] = mips_cprmask[1];
16987       s.ri_cprmask[2] = mips_cprmask[2];
16988       s.ri_cprmask[3] = mips_cprmask[3];
16989       /* The gp_value field is set by the MIPS ELF backend.  */
16990
16991       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
16992                                        ((Elf32_External_RegInfo *)
16993                                         mips_regmask_frag));
16994     }
16995   else
16996     {
16997       Elf64_Internal_RegInfo s;
16998
16999       s.ri_gprmask = mips_gprmask;
17000       s.ri_pad = 0;
17001       s.ri_cprmask[0] = mips_cprmask[0];
17002       s.ri_cprmask[1] = mips_cprmask[1];
17003       s.ri_cprmask[2] = mips_cprmask[2];
17004       s.ri_cprmask[3] = mips_cprmask[3];
17005       /* The gp_value field is set by the MIPS ELF backend.  */
17006
17007       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
17008                                        ((Elf64_External_RegInfo *)
17009                                         mips_regmask_frag));
17010     }
17011
17012   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
17013      sort of BFD interface for this.  */
17014   if (mips_any_noreorder)
17015     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
17016   if (mips_pic != NO_PIC)
17017     {
17018       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
17019       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17020     }
17021   if (mips_abicalls)
17022     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
17023
17024   /* Set MIPS ELF flags for ASEs.  Note that not all ASEs have flags
17025      defined at present; this might need to change in future.  */
17026   if (file_ase_mips16)
17027     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
17028   if (file_ase_micromips)
17029     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
17030   if (file_ase & ASE_MDMX)
17031     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
17032
17033   /* Set the MIPS ELF ABI flags.  */
17034   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
17035     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
17036   else if (mips_abi == O64_ABI)
17037     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
17038   else if (mips_abi == EABI_ABI)
17039     {
17040       if (!file_mips_gp32)
17041         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
17042       else
17043         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
17044     }
17045   else if (mips_abi == N32_ABI)
17046     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
17047
17048   /* Nothing to do for N64_ABI.  */
17049
17050   if (mips_32bitmode)
17051     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
17052
17053   if (mips_flag_nan2008)
17054     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
17055
17056 #if 0 /* XXX FIXME */
17057   /* 32 bit code with 64 bit FP registers.  */
17058   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
17059     elf_elfheader (stdoutput)->e_flags |= ???;
17060 #endif
17061 }
17062 \f
17063 typedef struct proc {
17064   symbolS *func_sym;
17065   symbolS *func_end_sym;
17066   unsigned long reg_mask;
17067   unsigned long reg_offset;
17068   unsigned long fpreg_mask;
17069   unsigned long fpreg_offset;
17070   unsigned long frame_offset;
17071   unsigned long frame_reg;
17072   unsigned long pc_reg;
17073 } procS;
17074
17075 static procS cur_proc;
17076 static procS *cur_proc_ptr;
17077 static int numprocs;
17078
17079 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
17080    as "2", and a normal nop as "0".  */
17081
17082 #define NOP_OPCODE_MIPS         0
17083 #define NOP_OPCODE_MIPS16       1
17084 #define NOP_OPCODE_MICROMIPS    2
17085
17086 char
17087 mips_nop_opcode (void)
17088 {
17089   if (seg_info (now_seg)->tc_segment_info_data.micromips)
17090     return NOP_OPCODE_MICROMIPS;
17091   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
17092     return NOP_OPCODE_MIPS16;
17093   else
17094     return NOP_OPCODE_MIPS;
17095 }
17096
17097 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
17098    32-bit microMIPS NOPs here (if applicable).  */
17099
17100 void
17101 mips_handle_align (fragS *fragp)
17102 {
17103   char nop_opcode;
17104   char *p;
17105   int bytes, size, excess;
17106   valueT opcode;
17107
17108   if (fragp->fr_type != rs_align_code)
17109     return;
17110
17111   p = fragp->fr_literal + fragp->fr_fix;
17112   nop_opcode = *p;
17113   switch (nop_opcode)
17114     {
17115     case NOP_OPCODE_MICROMIPS:
17116       opcode = micromips_nop32_insn.insn_opcode;
17117       size = 4;
17118       break;
17119     case NOP_OPCODE_MIPS16:
17120       opcode = mips16_nop_insn.insn_opcode;
17121       size = 2;
17122       break;
17123     case NOP_OPCODE_MIPS:
17124     default:
17125       opcode = nop_insn.insn_opcode;
17126       size = 4;
17127       break;
17128     }
17129
17130   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
17131   excess = bytes % size;
17132
17133   /* Handle the leading part if we're not inserting a whole number of
17134      instructions, and make it the end of the fixed part of the frag.
17135      Try to fit in a short microMIPS NOP if applicable and possible,
17136      and use zeroes otherwise.  */
17137   gas_assert (excess < 4);
17138   fragp->fr_fix += excess;
17139   switch (excess)
17140     {
17141     case 3:
17142       *p++ = '\0';
17143       /* Fall through.  */
17144     case 2:
17145       if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
17146         {
17147           p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
17148           break;
17149         }
17150       *p++ = '\0';
17151       /* Fall through.  */
17152     case 1:
17153       *p++ = '\0';
17154       /* Fall through.  */
17155     case 0:
17156       break;
17157     }
17158
17159   md_number_to_chars (p, opcode, size);
17160   fragp->fr_var = size;
17161 }
17162
17163 static void
17164 md_obj_begin (void)
17165 {
17166 }
17167
17168 static void
17169 md_obj_end (void)
17170 {
17171   /* Check for premature end, nesting errors, etc.  */
17172   if (cur_proc_ptr)
17173     as_warn (_("missing .end at end of assembly"));
17174 }
17175
17176 static long
17177 get_number (void)
17178 {
17179   int negative = 0;
17180   long val = 0;
17181
17182   if (*input_line_pointer == '-')
17183     {
17184       ++input_line_pointer;
17185       negative = 1;
17186     }
17187   if (!ISDIGIT (*input_line_pointer))
17188     as_bad (_("expected simple number"));
17189   if (input_line_pointer[0] == '0')
17190     {
17191       if (input_line_pointer[1] == 'x')
17192         {
17193           input_line_pointer += 2;
17194           while (ISXDIGIT (*input_line_pointer))
17195             {
17196               val <<= 4;
17197               val |= hex_value (*input_line_pointer++);
17198             }
17199           return negative ? -val : val;
17200         }
17201       else
17202         {
17203           ++input_line_pointer;
17204           while (ISDIGIT (*input_line_pointer))
17205             {
17206               val <<= 3;
17207               val |= *input_line_pointer++ - '0';
17208             }
17209           return negative ? -val : val;
17210         }
17211     }
17212   if (!ISDIGIT (*input_line_pointer))
17213     {
17214       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
17215               *input_line_pointer, *input_line_pointer);
17216       as_warn (_("invalid number"));
17217       return -1;
17218     }
17219   while (ISDIGIT (*input_line_pointer))
17220     {
17221       val *= 10;
17222       val += *input_line_pointer++ - '0';
17223     }
17224   return negative ? -val : val;
17225 }
17226
17227 /* The .file directive; just like the usual .file directive, but there
17228    is an initial number which is the ECOFF file index.  In the non-ECOFF
17229    case .file implies DWARF-2.  */
17230
17231 static void
17232 s_mips_file (int x ATTRIBUTE_UNUSED)
17233 {
17234   static int first_file_directive = 0;
17235
17236   if (ECOFF_DEBUGGING)
17237     {
17238       get_number ();
17239       s_app_file (0);
17240     }
17241   else
17242     {
17243       char *filename;
17244
17245       filename = dwarf2_directive_file (0);
17246
17247       /* Versions of GCC up to 3.1 start files with a ".file"
17248          directive even for stabs output.  Make sure that this
17249          ".file" is handled.  Note that you need a version of GCC
17250          after 3.1 in order to support DWARF-2 on MIPS.  */
17251       if (filename != NULL && ! first_file_directive)
17252         {
17253           (void) new_logical_line (filename, -1);
17254           s_app_file_string (filename, 0);
17255         }
17256       first_file_directive = 1;
17257     }
17258 }
17259
17260 /* The .loc directive, implying DWARF-2.  */
17261
17262 static void
17263 s_mips_loc (int x ATTRIBUTE_UNUSED)
17264 {
17265   if (!ECOFF_DEBUGGING)
17266     dwarf2_directive_loc (0);
17267 }
17268
17269 /* The .end directive.  */
17270
17271 static void
17272 s_mips_end (int x ATTRIBUTE_UNUSED)
17273 {
17274   symbolS *p;
17275
17276   /* Following functions need their own .frame and .cprestore directives.  */
17277   mips_frame_reg_valid = 0;
17278   mips_cprestore_valid = 0;
17279
17280   if (!is_end_of_line[(unsigned char) *input_line_pointer])
17281     {
17282       p = get_symbol ();
17283       demand_empty_rest_of_line ();
17284     }
17285   else
17286     p = NULL;
17287
17288   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
17289     as_warn (_(".end not in text section"));
17290
17291   if (!cur_proc_ptr)
17292     {
17293       as_warn (_(".end directive without a preceding .ent directive."));
17294       demand_empty_rest_of_line ();
17295       return;
17296     }
17297
17298   if (p != NULL)
17299     {
17300       gas_assert (S_GET_NAME (p));
17301       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
17302         as_warn (_(".end symbol does not match .ent symbol."));
17303
17304       if (debug_type == DEBUG_STABS)
17305         stabs_generate_asm_endfunc (S_GET_NAME (p),
17306                                     S_GET_NAME (p));
17307     }
17308   else
17309     as_warn (_(".end directive missing or unknown symbol"));
17310
17311   /* Create an expression to calculate the size of the function.  */
17312   if (p && cur_proc_ptr)
17313     {
17314       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
17315       expressionS *exp = xmalloc (sizeof (expressionS));
17316
17317       obj->size = exp;
17318       exp->X_op = O_subtract;
17319       exp->X_add_symbol = symbol_temp_new_now ();
17320       exp->X_op_symbol = p;
17321       exp->X_add_number = 0;
17322
17323       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
17324     }
17325
17326   /* Generate a .pdr section.  */
17327   if (!ECOFF_DEBUGGING && mips_flag_pdr)
17328     {
17329       segT saved_seg = now_seg;
17330       subsegT saved_subseg = now_subseg;
17331       expressionS exp;
17332       char *fragp;
17333
17334 #ifdef md_flush_pending_output
17335       md_flush_pending_output ();
17336 #endif
17337
17338       gas_assert (pdr_seg);
17339       subseg_set (pdr_seg, 0);
17340
17341       /* Write the symbol.  */
17342       exp.X_op = O_symbol;
17343       exp.X_add_symbol = p;
17344       exp.X_add_number = 0;
17345       emit_expr (&exp, 4);
17346
17347       fragp = frag_more (7 * 4);
17348
17349       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
17350       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
17351       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
17352       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
17353       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
17354       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
17355       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
17356
17357       subseg_set (saved_seg, saved_subseg);
17358     }
17359
17360   cur_proc_ptr = NULL;
17361 }
17362
17363 /* The .aent and .ent directives.  */
17364
17365 static void
17366 s_mips_ent (int aent)
17367 {
17368   symbolS *symbolP;
17369
17370   symbolP = get_symbol ();
17371   if (*input_line_pointer == ',')
17372     ++input_line_pointer;
17373   SKIP_WHITESPACE ();
17374   if (ISDIGIT (*input_line_pointer)
17375       || *input_line_pointer == '-')
17376     get_number ();
17377
17378   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
17379     as_warn (_(".ent or .aent not in text section."));
17380
17381   if (!aent && cur_proc_ptr)
17382     as_warn (_("missing .end"));
17383
17384   if (!aent)
17385     {
17386       /* This function needs its own .frame and .cprestore directives.  */
17387       mips_frame_reg_valid = 0;
17388       mips_cprestore_valid = 0;
17389
17390       cur_proc_ptr = &cur_proc;
17391       memset (cur_proc_ptr, '\0', sizeof (procS));
17392
17393       cur_proc_ptr->func_sym = symbolP;
17394
17395       ++numprocs;
17396
17397       if (debug_type == DEBUG_STABS)
17398         stabs_generate_asm_func (S_GET_NAME (symbolP),
17399                                  S_GET_NAME (symbolP));
17400     }
17401
17402   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
17403
17404   demand_empty_rest_of_line ();
17405 }
17406
17407 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
17408    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
17409    s_mips_frame is used so that we can set the PDR information correctly.
17410    We can't use the ecoff routines because they make reference to the ecoff
17411    symbol table (in the mdebug section).  */
17412
17413 static void
17414 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
17415 {
17416   if (ECOFF_DEBUGGING)
17417     s_ignore (ignore);
17418   else
17419     {
17420       long val;
17421
17422       if (cur_proc_ptr == (procS *) NULL)
17423         {
17424           as_warn (_(".frame outside of .ent"));
17425           demand_empty_rest_of_line ();
17426           return;
17427         }
17428
17429       cur_proc_ptr->frame_reg = tc_get_register (1);
17430
17431       SKIP_WHITESPACE ();
17432       if (*input_line_pointer++ != ','
17433           || get_absolute_expression_and_terminator (&val) != ',')
17434         {
17435           as_warn (_("Bad .frame directive"));
17436           --input_line_pointer;
17437           demand_empty_rest_of_line ();
17438           return;
17439         }
17440
17441       cur_proc_ptr->frame_offset = val;
17442       cur_proc_ptr->pc_reg = tc_get_register (0);
17443
17444       demand_empty_rest_of_line ();
17445     }
17446 }
17447
17448 /* The .fmask and .mask directives. If the mdebug section is present
17449    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
17450    embedded targets, s_mips_mask is used so that we can set the PDR
17451    information correctly. We can't use the ecoff routines because they
17452    make reference to the ecoff symbol table (in the mdebug section).  */
17453
17454 static void
17455 s_mips_mask (int reg_type)
17456 {
17457   if (ECOFF_DEBUGGING)
17458     s_ignore (reg_type);
17459   else
17460     {
17461       long mask, off;
17462
17463       if (cur_proc_ptr == (procS *) NULL)
17464         {
17465           as_warn (_(".mask/.fmask outside of .ent"));
17466           demand_empty_rest_of_line ();
17467           return;
17468         }
17469
17470       if (get_absolute_expression_and_terminator (&mask) != ',')
17471         {
17472           as_warn (_("Bad .mask/.fmask directive"));
17473           --input_line_pointer;
17474           demand_empty_rest_of_line ();
17475           return;
17476         }
17477
17478       off = get_absolute_expression ();
17479
17480       if (reg_type == 'F')
17481         {
17482           cur_proc_ptr->fpreg_mask = mask;
17483           cur_proc_ptr->fpreg_offset = off;
17484         }
17485       else
17486         {
17487           cur_proc_ptr->reg_mask = mask;
17488           cur_proc_ptr->reg_offset = off;
17489         }
17490
17491       demand_empty_rest_of_line ();
17492     }
17493 }
17494
17495 /* A table describing all the processors gas knows about.  Names are
17496    matched in the order listed.
17497
17498    To ease comparison, please keep this table in the same order as
17499    gcc's mips_cpu_info_table[].  */
17500 static const struct mips_cpu_info mips_cpu_info_table[] =
17501 {
17502   /* Entries for generic ISAs */
17503   { "mips1",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS1,    CPU_R3000 },
17504   { "mips2",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS2,    CPU_R6000 },
17505   { "mips3",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS3,    CPU_R4000 },
17506   { "mips4",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS4,    CPU_R8000 },
17507   { "mips5",          MIPS_CPU_IS_ISA, 0,       ISA_MIPS5,    CPU_MIPS5 },
17508   { "mips32",         MIPS_CPU_IS_ISA, 0,       ISA_MIPS32,   CPU_MIPS32 },
17509   { "mips32r2",       MIPS_CPU_IS_ISA, 0,       ISA_MIPS32R2, CPU_MIPS32R2 },
17510   { "mips64",         MIPS_CPU_IS_ISA, 0,       ISA_MIPS64,   CPU_MIPS64 },
17511   { "mips64r2",       MIPS_CPU_IS_ISA, 0,       ISA_MIPS64R2, CPU_MIPS64R2 },
17512
17513   /* MIPS I */
17514   { "r3000",          0, 0,                     ISA_MIPS1,    CPU_R3000 },
17515   { "r2000",          0, 0,                     ISA_MIPS1,    CPU_R3000 },
17516   { "r3900",          0, 0,                     ISA_MIPS1,    CPU_R3900 },
17517
17518   /* MIPS II */
17519   { "r6000",          0, 0,                     ISA_MIPS2,    CPU_R6000 },
17520
17521   /* MIPS III */
17522   { "r4000",          0, 0,                     ISA_MIPS3,    CPU_R4000 },
17523   { "r4010",          0, 0,                     ISA_MIPS2,    CPU_R4010 },
17524   { "vr4100",         0, 0,                     ISA_MIPS3,    CPU_VR4100 },
17525   { "vr4111",         0, 0,                     ISA_MIPS3,    CPU_R4111 },
17526   { "vr4120",         0, 0,                     ISA_MIPS3,    CPU_VR4120 },
17527   { "vr4130",         0, 0,                     ISA_MIPS3,    CPU_VR4120 },
17528   { "vr4181",         0, 0,                     ISA_MIPS3,    CPU_R4111 },
17529   { "vr4300",         0, 0,                     ISA_MIPS3,    CPU_R4300 },
17530   { "r4400",          0, 0,                     ISA_MIPS3,    CPU_R4400 },
17531   { "r4600",          0, 0,                     ISA_MIPS3,    CPU_R4600 },
17532   { "orion",          0, 0,                     ISA_MIPS3,    CPU_R4600 },
17533   { "r4650",          0, 0,                     ISA_MIPS3,    CPU_R4650 },
17534   { "r5900",          0, 0,                     ISA_MIPS3,    CPU_R5900 },
17535   /* ST Microelectronics Loongson 2E and 2F cores */
17536   { "loongson2e",     0, 0,                     ISA_MIPS3,    CPU_LOONGSON_2E },
17537   { "loongson2f",     0, 0,                     ISA_MIPS3,    CPU_LOONGSON_2F },
17538
17539   /* MIPS IV */
17540   { "r8000",          0, 0,                     ISA_MIPS4,    CPU_R8000 },
17541   { "r10000",         0, 0,                     ISA_MIPS4,    CPU_R10000 },
17542   { "r12000",         0, 0,                     ISA_MIPS4,    CPU_R12000 },
17543   { "r14000",         0, 0,                     ISA_MIPS4,    CPU_R14000 },
17544   { "r16000",         0, 0,                     ISA_MIPS4,    CPU_R16000 },
17545   { "vr5000",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
17546   { "vr5400",         0, 0,                     ISA_MIPS4,    CPU_VR5400 },
17547   { "vr5500",         0, 0,                     ISA_MIPS4,    CPU_VR5500 },
17548   { "rm5200",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
17549   { "rm5230",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
17550   { "rm5231",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
17551   { "rm5261",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
17552   { "rm5721",         0, 0,                     ISA_MIPS4,    CPU_R5000 },
17553   { "rm7000",         0, 0,                     ISA_MIPS4,    CPU_RM7000 },
17554   { "rm9000",         0, 0,                     ISA_MIPS4,    CPU_RM9000 },
17555
17556   /* MIPS 32 */
17557   { "4kc",            0, 0,                     ISA_MIPS32,   CPU_MIPS32 },
17558   { "4km",            0, 0,                     ISA_MIPS32,   CPU_MIPS32 },
17559   { "4kp",            0, 0,                     ISA_MIPS32,   CPU_MIPS32 },
17560   { "4ksc",           0, ASE_SMARTMIPS,         ISA_MIPS32,   CPU_MIPS32 },
17561
17562   /* MIPS 32 Release 2 */
17563   { "4kec",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17564   { "4kem",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17565   { "4kep",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17566   { "4ksd",           0, ASE_SMARTMIPS,         ISA_MIPS32R2, CPU_MIPS32R2 },
17567   { "m4k",            0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17568   { "m4kp",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17569   { "m14k",           0, ASE_MCU,               ISA_MIPS32R2, CPU_MIPS32R2 },
17570   { "m14kc",          0, ASE_MCU,               ISA_MIPS32R2, CPU_MIPS32R2 },
17571   { "m14ke",          0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17572                                                 ISA_MIPS32R2, CPU_MIPS32R2 },
17573   { "m14kec",         0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
17574                                                 ISA_MIPS32R2, CPU_MIPS32R2 },
17575   { "24kc",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17576   { "24kf2_1",        0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17577   { "24kf",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17578   { "24kf1_1",        0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17579   /* Deprecated forms of the above.  */
17580   { "24kfx",          0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17581   { "24kx",           0, 0,                     ISA_MIPS32R2, CPU_MIPS32R2 },
17582   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
17583   { "24kec",          0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
17584   { "24kef2_1",       0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
17585   { "24kef",          0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
17586   { "24kef1_1",       0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
17587   /* Deprecated forms of the above.  */
17588   { "24kefx",         0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
17589   { "24kex",          0, ASE_DSP,               ISA_MIPS32R2, CPU_MIPS32R2 },
17590   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
17591   { "34kc",           0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17592   { "34kf2_1",        0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17593   { "34kf",           0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17594   { "34kf1_1",        0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17595   /* Deprecated forms of the above.  */
17596   { "34kfx",          0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17597   { "34kx",           0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17598   /* 34Kn is a 34kc without DSP.  */
17599   { "34kn",           0, ASE_MT,                ISA_MIPS32R2, CPU_MIPS32R2 },
17600   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
17601   { "74kc",           0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
17602   { "74kf2_1",        0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
17603   { "74kf",           0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
17604   { "74kf1_1",        0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
17605   { "74kf3_2",        0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
17606   /* Deprecated forms of the above.  */
17607   { "74kfx",          0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
17608   { "74kx",           0, ASE_DSP | ASE_DSPR2,   ISA_MIPS32R2, CPU_MIPS32R2 },
17609   /* 1004K cores are multiprocessor versions of the 34K.  */
17610   { "1004kc",         0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17611   { "1004kf2_1",      0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17612   { "1004kf",         0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17613   { "1004kf1_1",      0, ASE_DSP | ASE_MT,      ISA_MIPS32R2, CPU_MIPS32R2 },
17614
17615   /* MIPS 64 */
17616   { "5kc",            0, 0,                     ISA_MIPS64,   CPU_MIPS64 },
17617   { "5kf",            0, 0,                     ISA_MIPS64,   CPU_MIPS64 },
17618   { "20kc",           0, ASE_MIPS3D,            ISA_MIPS64,   CPU_MIPS64 },
17619   { "25kf",           0, ASE_MIPS3D,            ISA_MIPS64,   CPU_MIPS64 },
17620
17621   /* Broadcom SB-1 CPU core */
17622   { "sb1",            0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64,   CPU_SB1 },
17623   /* Broadcom SB-1A CPU core */
17624   { "sb1a",           0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64,   CPU_SB1 },
17625   
17626   { "loongson3a",     0, 0,                     ISA_MIPS64,   CPU_LOONGSON_3A },
17627
17628   /* MIPS 64 Release 2 */
17629
17630   /* Cavium Networks Octeon CPU core */
17631   { "octeon",         0, 0,                     ISA_MIPS64R2, CPU_OCTEON },
17632   { "octeon+",        0, 0,                     ISA_MIPS64R2, CPU_OCTEONP },
17633   { "octeon2",        0, 0,                     ISA_MIPS64R2, CPU_OCTEON2 },
17634
17635   /* RMI Xlr */
17636   { "xlr",            0, 0,                     ISA_MIPS64,   CPU_XLR },
17637
17638   /* Broadcom XLP.
17639      XLP is mostly like XLR, with the prominent exception that it is
17640      MIPS64R2 rather than MIPS64.  */
17641   { "xlp",            0, 0,                     ISA_MIPS64R2, CPU_XLR },
17642
17643   /* End marker */
17644   { NULL, 0, 0, 0, 0 }
17645 };
17646
17647
17648 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
17649    with a final "000" replaced by "k".  Ignore case.
17650
17651    Note: this function is shared between GCC and GAS.  */
17652
17653 static bfd_boolean
17654 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
17655 {
17656   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
17657     given++, canonical++;
17658
17659   return ((*given == 0 && *canonical == 0)
17660           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
17661 }
17662
17663
17664 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
17665    CPU name.  We've traditionally allowed a lot of variation here.
17666
17667    Note: this function is shared between GCC and GAS.  */
17668
17669 static bfd_boolean
17670 mips_matching_cpu_name_p (const char *canonical, const char *given)
17671 {
17672   /* First see if the name matches exactly, or with a final "000"
17673      turned into "k".  */
17674   if (mips_strict_matching_cpu_name_p (canonical, given))
17675     return TRUE;
17676
17677   /* If not, try comparing based on numerical designation alone.
17678      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
17679   if (TOLOWER (*given) == 'r')
17680     given++;
17681   if (!ISDIGIT (*given))
17682     return FALSE;
17683
17684   /* Skip over some well-known prefixes in the canonical name,
17685      hoping to find a number there too.  */
17686   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
17687     canonical += 2;
17688   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
17689     canonical += 2;
17690   else if (TOLOWER (canonical[0]) == 'r')
17691     canonical += 1;
17692
17693   return mips_strict_matching_cpu_name_p (canonical, given);
17694 }
17695
17696
17697 /* Parse an option that takes the name of a processor as its argument.
17698    OPTION is the name of the option and CPU_STRING is the argument.
17699    Return the corresponding processor enumeration if the CPU_STRING is
17700    recognized, otherwise report an error and return null.
17701
17702    A similar function exists in GCC.  */
17703
17704 static const struct mips_cpu_info *
17705 mips_parse_cpu (const char *option, const char *cpu_string)
17706 {
17707   const struct mips_cpu_info *p;
17708
17709   /* 'from-abi' selects the most compatible architecture for the given
17710      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
17711      EABIs, we have to decide whether we're using the 32-bit or 64-bit
17712      version.  Look first at the -mgp options, if given, otherwise base
17713      the choice on MIPS_DEFAULT_64BIT.
17714
17715      Treat NO_ABI like the EABIs.  One reason to do this is that the
17716      plain 'mips' and 'mips64' configs have 'from-abi' as their default
17717      architecture.  This code picks MIPS I for 'mips' and MIPS III for
17718      'mips64', just as we did in the days before 'from-abi'.  */
17719   if (strcasecmp (cpu_string, "from-abi") == 0)
17720     {
17721       if (ABI_NEEDS_32BIT_REGS (mips_abi))
17722         return mips_cpu_info_from_isa (ISA_MIPS1);
17723
17724       if (ABI_NEEDS_64BIT_REGS (mips_abi))
17725         return mips_cpu_info_from_isa (ISA_MIPS3);
17726
17727       if (file_mips_gp32 >= 0)
17728         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
17729
17730       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
17731                                      ? ISA_MIPS3
17732                                      : ISA_MIPS1);
17733     }
17734
17735   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
17736   if (strcasecmp (cpu_string, "default") == 0)
17737     return 0;
17738
17739   for (p = mips_cpu_info_table; p->name != 0; p++)
17740     if (mips_matching_cpu_name_p (p->name, cpu_string))
17741       return p;
17742
17743   as_bad (_("Bad value (%s) for %s"), cpu_string, option);
17744   return 0;
17745 }
17746
17747 /* Return the canonical processor information for ISA (a member of the
17748    ISA_MIPS* enumeration).  */
17749
17750 static const struct mips_cpu_info *
17751 mips_cpu_info_from_isa (int isa)
17752 {
17753   int i;
17754
17755   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
17756     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
17757         && isa == mips_cpu_info_table[i].isa)
17758       return (&mips_cpu_info_table[i]);
17759
17760   return NULL;
17761 }
17762
17763 static const struct mips_cpu_info *
17764 mips_cpu_info_from_arch (int arch)
17765 {
17766   int i;
17767
17768   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
17769     if (arch == mips_cpu_info_table[i].cpu)
17770       return (&mips_cpu_info_table[i]);
17771
17772   return NULL;
17773 }
17774 \f
17775 static void
17776 show (FILE *stream, const char *string, int *col_p, int *first_p)
17777 {
17778   if (*first_p)
17779     {
17780       fprintf (stream, "%24s", "");
17781       *col_p = 24;
17782     }
17783   else
17784     {
17785       fprintf (stream, ", ");
17786       *col_p += 2;
17787     }
17788
17789   if (*col_p + strlen (string) > 72)
17790     {
17791       fprintf (stream, "\n%24s", "");
17792       *col_p = 24;
17793     }
17794
17795   fprintf (stream, "%s", string);
17796   *col_p += strlen (string);
17797
17798   *first_p = 0;
17799 }
17800
17801 void
17802 md_show_usage (FILE *stream)
17803 {
17804   int column, first;
17805   size_t i;
17806
17807   fprintf (stream, _("\
17808 MIPS options:\n\
17809 -EB                     generate big endian output\n\
17810 -EL                     generate little endian output\n\
17811 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
17812 -G NUM                  allow referencing objects up to NUM bytes\n\
17813                         implicitly with the gp register [default 8]\n"));
17814   fprintf (stream, _("\
17815 -mips1                  generate MIPS ISA I instructions\n\
17816 -mips2                  generate MIPS ISA II instructions\n\
17817 -mips3                  generate MIPS ISA III instructions\n\
17818 -mips4                  generate MIPS ISA IV instructions\n\
17819 -mips5                  generate MIPS ISA V instructions\n\
17820 -mips32                 generate MIPS32 ISA instructions\n\
17821 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
17822 -mips64                 generate MIPS64 ISA instructions\n\
17823 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
17824 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
17825
17826   first = 1;
17827
17828   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
17829     show (stream, mips_cpu_info_table[i].name, &column, &first);
17830   show (stream, "from-abi", &column, &first);
17831   fputc ('\n', stream);
17832
17833   fprintf (stream, _("\
17834 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
17835 -no-mCPU                don't generate code specific to CPU.\n\
17836                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
17837
17838   first = 1;
17839
17840   show (stream, "3900", &column, &first);
17841   show (stream, "4010", &column, &first);
17842   show (stream, "4100", &column, &first);
17843   show (stream, "4650", &column, &first);
17844   fputc ('\n', stream);
17845
17846   fprintf (stream, _("\
17847 -mips16                 generate mips16 instructions\n\
17848 -no-mips16              do not generate mips16 instructions\n"));
17849   fprintf (stream, _("\
17850 -mmicromips             generate microMIPS instructions\n\
17851 -mno-micromips          do not generate microMIPS instructions\n"));
17852   fprintf (stream, _("\
17853 -msmartmips             generate smartmips instructions\n\
17854 -mno-smartmips          do not generate smartmips instructions\n"));  
17855   fprintf (stream, _("\
17856 -mdsp                   generate DSP instructions\n\
17857 -mno-dsp                do not generate DSP instructions\n"));
17858   fprintf (stream, _("\
17859 -mdspr2                 generate DSP R2 instructions\n\
17860 -mno-dspr2              do not generate DSP R2 instructions\n"));
17861   fprintf (stream, _("\
17862 -mmt                    generate MT instructions\n\
17863 -mno-mt                 do not generate MT instructions\n"));
17864   fprintf (stream, _("\
17865 -mmcu                   generate MCU instructions\n\
17866 -mno-mcu                do not generate MCU instructions\n"));
17867   fprintf (stream, _("\
17868 -mvirt                  generate Virtualization instructions\n\
17869 -mno-virt               do not generate Virtualization instructions\n"));
17870   fprintf (stream, _("\
17871 -minsn32                only generate 32-bit microMIPS instructions\n\
17872 -mno-insn32             generate all microMIPS instructions\n"));
17873   fprintf (stream, _("\
17874 -mfix-loongson2f-jump   work around Loongson2F JUMP instructions\n\
17875 -mfix-loongson2f-nop    work around Loongson2F NOP errata\n\
17876 -mfix-vr4120            work around certain VR4120 errata\n\
17877 -mfix-vr4130            work around VR4130 mflo/mfhi errata\n\
17878 -mfix-24k               insert a nop after ERET and DERET instructions\n\
17879 -mfix-cn63xxp1          work around CN63XXP1 PREF errata\n\
17880 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
17881 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
17882 -msym32                 assume all symbols have 32-bit values\n\
17883 -O0                     remove unneeded NOPs, do not swap branches\n\
17884 -O                      remove unneeded NOPs and swap branches\n\
17885 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
17886 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
17887   fprintf (stream, _("\
17888 -mhard-float            allow floating-point instructions\n\
17889 -msoft-float            do not allow floating-point instructions\n\
17890 -msingle-float          only allow 32-bit floating-point operations\n\
17891 -mdouble-float          allow 32-bit and 64-bit floating-point operations\n\
17892 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
17893 --[no-]relax-branch     [dis]allow out-of-range branches to be relaxed\n\
17894 -mnan=ENCODING          select an IEEE 754 NaN encoding convention, either of:\n"));
17895
17896   first = 1;
17897
17898   show (stream, "legacy", &column, &first);
17899   show (stream, "2008", &column, &first);
17900
17901   fputc ('\n', stream);
17902
17903   fprintf (stream, _("\
17904 -KPIC, -call_shared     generate SVR4 position independent code\n\
17905 -call_nonpic            generate non-PIC code that can operate with DSOs\n\
17906 -mvxworks-pic           generate VxWorks position independent code\n\
17907 -non_shared             do not generate code that can operate with DSOs\n\
17908 -xgot                   assume a 32 bit GOT\n\
17909 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
17910 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
17911                         position dependent (non shared) code\n\
17912 -mabi=ABI               create ABI conformant object file for:\n"));
17913
17914   first = 1;
17915
17916   show (stream, "32", &column, &first);
17917   show (stream, "o64", &column, &first);
17918   show (stream, "n32", &column, &first);
17919   show (stream, "64", &column, &first);
17920   show (stream, "eabi", &column, &first);
17921
17922   fputc ('\n', stream);
17923
17924   fprintf (stream, _("\
17925 -32                     create o32 ABI object file (default)\n\
17926 -n32                    create n32 ABI object file\n\
17927 -64                     create 64 ABI object file\n"));
17928 }
17929
17930 #ifdef TE_IRIX
17931 enum dwarf2_format
17932 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
17933 {
17934   if (HAVE_64BIT_SYMBOLS)
17935     return dwarf2_format_64bit_irix;
17936   else
17937     return dwarf2_format_32bit;
17938 }
17939 #endif
17940
17941 int
17942 mips_dwarf2_addr_size (void)
17943 {
17944   if (HAVE_64BIT_OBJECTS)
17945     return 8;
17946   else
17947     return 4;
17948 }
17949
17950 /* Standard calling conventions leave the CFA at SP on entry.  */
17951 void
17952 mips_cfi_frame_initial_instructions (void)
17953 {
17954   cfi_add_CFA_def_cfa_register (SP);
17955 }
17956
17957 int
17958 tc_mips_regname_to_dw2regnum (char *regname)
17959 {
17960   unsigned int regnum = -1;
17961   unsigned int reg;
17962
17963   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
17964     regnum = reg;
17965
17966   return regnum;
17967 }