2013-01-09 Steve Ellcey <sellcey@mips.com>
[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 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too.  */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about.  */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections.  Off by default on IRIX: the native
78    linker doesn't know about and discards them, but relocations against them
79    remain, leading to rld crashes.  */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define ATREG 1
94 #define S0  16
95 #define S7  23
96 #define TREG 24
97 #define PIC_CALL_REG 25
98 #define KT0 26
99 #define KT1 27
100 #define GP  28
101 #define SP  29
102 #define FP  30
103 #define RA  31
104
105 #define ILLEGAL_REG (32)
106
107 #define AT  mips_opts.at
108
109 /* Allow override of standard little-endian ECOFF format.  */
110
111 #ifndef ECOFF_LITTLE_FORMAT
112 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
113 #endif
114
115 extern int target_big_endian;
116
117 /* The name of the readonly data section.  */
118 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
119                             ? ".rdata" \
120                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
121                             ? ".rdata" \
122                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
123                             ? ".rodata" \
124                             : (abort (), ""))
125
126 /* Ways in which an instruction can be "appended" to the output.  */
127 enum append_method {
128   /* Just add it normally.  */
129   APPEND_ADD,
130
131   /* Add it normally and then add a nop.  */
132   APPEND_ADD_WITH_NOP,
133
134   /* Turn an instruction with a delay slot into a "compact" version.  */
135   APPEND_ADD_COMPACT,
136
137   /* Insert the instruction before the last one.  */
138   APPEND_SWAP
139 };
140
141 /* Information about an instruction, including its format, operands
142    and fixups.  */
143 struct mips_cl_insn
144 {
145   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
146   const struct mips_opcode *insn_mo;
147
148   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
149      a copy of INSN_MO->match with the operands filled in.  If we have
150      decided to use an extended MIPS16 instruction, this includes the
151      extension.  */
152   unsigned long insn_opcode;
153
154   /* The frag that contains the instruction.  */
155   struct frag *frag;
156
157   /* The offset into FRAG of the first instruction byte.  */
158   long where;
159
160   /* The relocs associated with the instruction, if any.  */
161   fixS *fixp[3];
162
163   /* True if this entry cannot be moved from its current position.  */
164   unsigned int fixed_p : 1;
165
166   /* True if this instruction occurred in a .set noreorder block.  */
167   unsigned int noreorder_p : 1;
168
169   /* True for mips16 instructions that jump to an absolute address.  */
170   unsigned int mips16_absolute_jump_p : 1;
171
172   /* True if this instruction is complete.  */
173   unsigned int complete_p : 1;
174
175   /* True if this instruction is cleared from history by unconditional
176      branch.  */
177   unsigned int cleared_p : 1;
178 };
179
180 /* The ABI to use.  */
181 enum mips_abi_level
182 {
183   NO_ABI = 0,
184   O32_ABI,
185   O64_ABI,
186   N32_ABI,
187   N64_ABI,
188   EABI_ABI
189 };
190
191 /* MIPS ABI we are using for this output file.  */
192 static enum mips_abi_level mips_abi = NO_ABI;
193
194 /* Whether or not we have code that can call pic code.  */
195 int mips_abicalls = FALSE;
196
197 /* Whether or not we have code which can be put into a shared
198    library.  */
199 static bfd_boolean mips_in_shared = TRUE;
200
201 /* This is the set of options which may be modified by the .set
202    pseudo-op.  We use a struct so that .set push and .set pop are more
203    reliable.  */
204
205 struct mips_set_options
206 {
207   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
208      if it has not been initialized.  Changed by `.set mipsN', and the
209      -mipsN command line option, and the default CPU.  */
210   int isa;
211   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
212      if they have not been initialized.  Changed by `.set <asename>', by
213      command line options, and based on the default architecture.  */
214   int ase_mips3d;
215   int ase_mdmx;
216   int ase_smartmips;
217   int ase_dsp;
218   int ase_dspr2;
219   int ase_mt;
220   int ase_mcu;
221   /* Whether we are assembling for the mips16 processor.  0 if we are
222      not, 1 if we are, and -1 if the value has not been initialized.
223      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
224      -nomips16 command line options, and the default CPU.  */
225   int mips16;
226   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
227      1 if we are, and -1 if the value has not been initialized.  Changed
228      by `.set micromips' and `.set nomicromips', and the -mmicromips
229      and -mno-micromips command line options, and the default CPU.  */
230   int micromips;
231   /* Non-zero if we should not reorder instructions.  Changed by `.set
232      reorder' and `.set noreorder'.  */
233   int noreorder;
234   /* Non-zero if we should not permit the register designated "assembler
235      temporary" to be used in instructions.  The value is the register
236      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
237      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
238   unsigned int at;
239   /* Non-zero if we should warn when a macro instruction expands into
240      more than one machine instruction.  Changed by `.set nomacro' and
241      `.set macro'.  */
242   int warn_about_macros;
243   /* Non-zero if we should not move instructions.  Changed by `.set
244      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
245   int nomove;
246   /* Non-zero if we should not optimize branches by moving the target
247      of the branch into the delay slot.  Actually, we don't perform
248      this optimization anyhow.  Changed by `.set bopt' and `.set
249      nobopt'.  */
250   int nobopt;
251   /* Non-zero if we should not autoextend mips16 instructions.
252      Changed by `.set autoextend' and `.set noautoextend'.  */
253   int noautoextend;
254   /* Restrict general purpose registers and floating point registers
255      to 32 bit.  This is initially determined when -mgp32 or -mfp32
256      is passed but can changed if the assembler code uses .set mipsN.  */
257   int gp32;
258   int fp32;
259   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
260      command line option, and the default CPU.  */
261   int arch;
262   /* True if ".set sym32" is in effect.  */
263   bfd_boolean sym32;
264   /* True if floating-point operations are not allowed.  Changed by .set
265      softfloat or .set hardfloat, by command line options -msoft-float or
266      -mhard-float.  The default is false.  */
267   bfd_boolean soft_float;
268
269   /* True if only single-precision floating-point operations are allowed.
270      Changed by .set singlefloat or .set doublefloat, command-line options
271      -msingle-float or -mdouble-float.  The default is false.  */
272   bfd_boolean single_float;
273 };
274
275 /* This is the struct we use to hold the current set of options.  Note
276    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
277    -1 to indicate that they have not been initialized.  */
278
279 /* True if -mgp32 was passed.  */
280 static int file_mips_gp32 = -1;
281
282 /* True if -mfp32 was passed.  */
283 static int file_mips_fp32 = -1;
284
285 /* 1 if -msoft-float, 0 if -mhard-float.  The default is 0.  */
286 static int file_mips_soft_float = 0;
287
288 /* 1 if -msingle-float, 0 if -mdouble-float.  The default is 0.   */
289 static int file_mips_single_float = 0;
290
291 static struct mips_set_options mips_opts =
292 {
293   /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
294   /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
295   /* ase_mcu */ -1, /* mips16 */ -1, /* micromips */ -1, /* noreorder */ 0,
296   /* at */ ATREG, /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
297   /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
298   /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
299 };
300
301 /* These variables are filled in with the masks of registers used.
302    The object format code reads them and puts them in the appropriate
303    place.  */
304 unsigned long mips_gprmask;
305 unsigned long mips_cprmask[4];
306
307 /* MIPS ISA we are using for this output file.  */
308 static int file_mips_isa = ISA_UNKNOWN;
309
310 /* True if any MIPS16 code was produced.  */
311 static int file_ase_mips16;
312
313 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32               \
314                               || mips_opts.isa == ISA_MIPS32R2          \
315                               || mips_opts.isa == ISA_MIPS64            \
316                               || mips_opts.isa == ISA_MIPS64R2)
317
318 /* True if any microMIPS code was produced.  */
319 static int file_ase_micromips;
320
321 /* True if we want to create R_MIPS_JALR for jalr $25.  */
322 #ifdef TE_IRIX
323 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
324 #else
325 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
326    because there's no place for any addend, the only acceptable
327    expression is a bare symbol.  */
328 #define MIPS_JALR_HINT_P(EXPR) \
329   (!HAVE_IN_PLACE_ADDENDS \
330    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
331 #endif
332
333 /* True if -mips3d was passed or implied by arguments passed on the
334    command line (e.g., by -march).  */
335 static int file_ase_mips3d;
336
337 /* True if -mdmx was passed or implied by arguments passed on the
338    command line (e.g., by -march).  */
339 static int file_ase_mdmx;
340
341 /* True if -msmartmips was passed or implied by arguments passed on the
342    command line (e.g., by -march).  */
343 static int file_ase_smartmips;
344
345 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32             \
346                                 || mips_opts.isa == ISA_MIPS32R2)
347
348 /* True if -mdsp was passed or implied by arguments passed on the
349    command line (e.g., by -march).  */
350 static int file_ase_dsp;
351
352 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2             \
353                               || mips_opts.isa == ISA_MIPS64R2          \
354                               || mips_opts.micromips)
355
356 #define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
357
358 /* True if -mdspr2 was passed or implied by arguments passed on the
359    command line (e.g., by -march).  */
360 static int file_ase_dspr2;
361
362 #define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2           \
363                                 || mips_opts.isa == ISA_MIPS64R2        \
364                                 || mips_opts.micromips)
365
366 /* True if -mmt was passed or implied by arguments passed on the
367    command line (e.g., by -march).  */
368 static int file_ase_mt;
369
370 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2              \
371                              || mips_opts.isa == ISA_MIPS64R2)
372
373 #define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2             \
374                               || mips_opts.isa == ISA_MIPS64R2          \
375                               || mips_opts.micromips)
376
377 /* The argument of the -march= flag.  The architecture we are assembling.  */
378 static int file_mips_arch = CPU_UNKNOWN;
379 static const char *mips_arch_string;
380
381 /* The argument of the -mtune= flag.  The architecture for which we
382    are optimizing.  */
383 static int mips_tune = CPU_UNKNOWN;
384 static const char *mips_tune_string;
385
386 /* True when generating 32-bit code for a 64-bit processor.  */
387 static int mips_32bitmode = 0;
388
389 /* True if the given ABI requires 32-bit registers.  */
390 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
391
392 /* Likewise 64-bit registers.  */
393 #define ABI_NEEDS_64BIT_REGS(ABI)       \
394   ((ABI) == N32_ABI                     \
395    || (ABI) == N64_ABI                  \
396    || (ABI) == O64_ABI)
397
398 /*  Return true if ISA supports 64 bit wide gp registers.  */
399 #define ISA_HAS_64BIT_REGS(ISA)         \
400   ((ISA) == ISA_MIPS3                   \
401    || (ISA) == ISA_MIPS4                \
402    || (ISA) == ISA_MIPS5                \
403    || (ISA) == ISA_MIPS64               \
404    || (ISA) == ISA_MIPS64R2)
405
406 /*  Return true if ISA supports 64 bit wide float registers.  */
407 #define ISA_HAS_64BIT_FPRS(ISA)         \
408   ((ISA) == ISA_MIPS3                   \
409    || (ISA) == ISA_MIPS4                \
410    || (ISA) == ISA_MIPS5                \
411    || (ISA) == ISA_MIPS32R2             \
412    || (ISA) == ISA_MIPS64               \
413    || (ISA) == ISA_MIPS64R2)
414
415 /* Return true if ISA supports 64-bit right rotate (dror et al.)
416    instructions.  */
417 #define ISA_HAS_DROR(ISA)               \
418   ((ISA) == ISA_MIPS64R2                \
419    || (mips_opts.micromips              \
420        && ISA_HAS_64BIT_REGS (ISA))     \
421    )
422
423 /* Return true if ISA supports 32-bit right rotate (ror et al.)
424    instructions.  */
425 #define ISA_HAS_ROR(ISA)                \
426   ((ISA) == ISA_MIPS32R2                \
427    || (ISA) == ISA_MIPS64R2             \
428    || mips_opts.ase_smartmips           \
429    || mips_opts.micromips               \
430    )
431
432 /* Return true if ISA supports single-precision floats in odd registers.  */
433 #define ISA_HAS_ODD_SINGLE_FPR(ISA)     \
434   ((ISA) == ISA_MIPS32                  \
435    || (ISA) == ISA_MIPS32R2             \
436    || (ISA) == ISA_MIPS64               \
437    || (ISA) == ISA_MIPS64R2)
438
439 /* Return true if ISA supports move to/from high part of a 64-bit
440    floating-point register. */
441 #define ISA_HAS_MXHC1(ISA)              \
442   ((ISA) == ISA_MIPS32R2                \
443    || (ISA) == ISA_MIPS64R2)
444
445 #define HAVE_32BIT_GPRS                            \
446     (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
447
448 #define HAVE_32BIT_FPRS                            \
449     (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
450
451 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
452 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
453
454 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
455
456 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
457
458 /* True if relocations are stored in-place.  */
459 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
460
461 /* The ABI-derived address size.  */
462 #define HAVE_64BIT_ADDRESSES \
463   (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
464 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
465
466 /* The size of symbolic constants (i.e., expressions of the form
467    "SYMBOL" or "SYMBOL + OFFSET").  */
468 #define HAVE_32BIT_SYMBOLS \
469   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
470 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
471
472 /* Addresses are loaded in different ways, depending on the address size
473    in use.  The n32 ABI Documentation also mandates the use of additions
474    with overflow checking, but existing implementations don't follow it.  */
475 #define ADDRESS_ADD_INSN                                                \
476    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
477
478 #define ADDRESS_ADDI_INSN                                               \
479    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
480
481 #define ADDRESS_LOAD_INSN                                               \
482    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
483
484 #define ADDRESS_STORE_INSN                                              \
485    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
486
487 /* Return true if the given CPU supports the MIPS16 ASE.  */
488 #define CPU_HAS_MIPS16(cpu)                                             \
489    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
490     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
491
492 /* Return true if the given CPU supports the microMIPS ASE.  */
493 #define CPU_HAS_MICROMIPS(cpu)  0
494
495 /* True if CPU has a dror instruction.  */
496 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
497
498 /* True if CPU has a ror instruction.  */
499 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
500
501 /* True if CPU is in the Octeon family */
502 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
503
504 /* True if CPU has seq/sne and seqi/snei instructions.  */
505 #define CPU_HAS_SEQ(CPU)        (CPU_IS_OCTEON (CPU))
506
507 /* True if mflo and mfhi can be immediately followed by instructions
508    which write to the HI and LO registers.
509
510    According to MIPS specifications, MIPS ISAs I, II, and III need
511    (at least) two instructions between the reads of HI/LO and
512    instructions which write them, and later ISAs do not.  Contradicting
513    the MIPS specifications, some MIPS IV processor user manuals (e.g.
514    the UM for the NEC Vr5000) document needing the instructions between
515    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
516    MIPS64 and later ISAs to have the interlocks, plus any specific
517    earlier-ISA CPUs for which CPU documentation declares that the
518    instructions are really interlocked.  */
519 #define hilo_interlocks \
520   (mips_opts.isa == ISA_MIPS32                        \
521    || mips_opts.isa == ISA_MIPS32R2                   \
522    || mips_opts.isa == ISA_MIPS64                     \
523    || mips_opts.isa == ISA_MIPS64R2                   \
524    || mips_opts.arch == CPU_R4010                     \
525    || mips_opts.arch == CPU_R5900                     \
526    || mips_opts.arch == CPU_R10000                    \
527    || mips_opts.arch == CPU_R12000                    \
528    || mips_opts.arch == CPU_R14000                    \
529    || mips_opts.arch == CPU_R16000                    \
530    || mips_opts.arch == CPU_RM7000                    \
531    || mips_opts.arch == CPU_VR5500                    \
532    || mips_opts.micromips                             \
533    )
534
535 /* Whether the processor uses hardware interlocks to protect reads
536    from the GPRs after they are loaded from memory, and thus does not
537    require nops to be inserted.  This applies to instructions marked
538    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
539    level I and microMIPS mode instructions are always interlocked.  */
540 #define gpr_interlocks                                \
541   (mips_opts.isa != ISA_MIPS1                         \
542    || mips_opts.arch == CPU_R3900                     \
543    || mips_opts.arch == CPU_R5900                     \
544    || mips_opts.micromips                             \
545    )
546
547 /* Whether the processor uses hardware interlocks to avoid delays
548    required by coprocessor instructions, and thus does not require
549    nops to be inserted.  This applies to instructions marked
550    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
551    between instructions marked INSN_WRITE_COND_CODE and ones marked
552    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
553    levels I, II, and III and microMIPS mode instructions are always
554    interlocked.  */
555 /* Itbl support may require additional care here.  */
556 #define cop_interlocks                                \
557   ((mips_opts.isa != ISA_MIPS1                        \
558     && mips_opts.isa != ISA_MIPS2                     \
559     && mips_opts.isa != ISA_MIPS3)                    \
560    || mips_opts.arch == CPU_R4300                     \
561    || mips_opts.micromips                             \
562    )
563
564 /* Whether the processor uses hardware interlocks to protect reads
565    from coprocessor registers after they are loaded from memory, and
566    thus does not require nops to be inserted.  This applies to
567    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
568    requires at MIPS ISA level I and microMIPS mode instructions are
569    always interlocked.  */
570 #define cop_mem_interlocks                            \
571   (mips_opts.isa != ISA_MIPS1                         \
572    || mips_opts.micromips                             \
573    )
574
575 /* Is this a mfhi or mflo instruction?  */
576 #define MF_HILO_INSN(PINFO) \
577   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
578
579 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
580    has been selected.  This implies, in particular, that addresses of text
581    labels have their LSB set.  */
582 #define HAVE_CODE_COMPRESSION                                           \
583   ((mips_opts.mips16 | mips_opts.micromips) != 0)
584
585 /* MIPS PIC level.  */
586
587 enum mips_pic_level mips_pic;
588
589 /* 1 if we should generate 32 bit offsets from the $gp register in
590    SVR4_PIC mode.  Currently has no meaning in other modes.  */
591 static int mips_big_got = 0;
592
593 /* 1 if trap instructions should used for overflow rather than break
594    instructions.  */
595 static int mips_trap = 0;
596
597 /* 1 if double width floating point constants should not be constructed
598    by assembling two single width halves into two single width floating
599    point registers which just happen to alias the double width destination
600    register.  On some architectures this aliasing can be disabled by a bit
601    in the status register, and the setting of this bit cannot be determined
602    automatically at assemble time.  */
603 static int mips_disable_float_construction;
604
605 /* Non-zero if any .set noreorder directives were used.  */
606
607 static int mips_any_noreorder;
608
609 /* Non-zero if nops should be inserted when the register referenced in
610    an mfhi/mflo instruction is read in the next two instructions.  */
611 static int mips_7000_hilo_fix;
612
613 /* The size of objects in the small data section.  */
614 static unsigned int g_switch_value = 8;
615 /* Whether the -G option was used.  */
616 static int g_switch_seen = 0;
617
618 #define N_RMASK 0xc4
619 #define N_VFP   0xd4
620
621 /* If we can determine in advance that GP optimization won't be
622    possible, we can skip the relaxation stuff that tries to produce
623    GP-relative references.  This makes delay slot optimization work
624    better.
625
626    This function can only provide a guess, but it seems to work for
627    gcc output.  It needs to guess right for gcc, otherwise gcc
628    will put what it thinks is a GP-relative instruction in a branch
629    delay slot.
630
631    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
632    fixed it for the non-PIC mode.  KR 95/04/07  */
633 static int nopic_need_relax (symbolS *, int);
634
635 /* handle of the OPCODE hash table */
636 static struct hash_control *op_hash = NULL;
637
638 /* The opcode hash table we use for the mips16.  */
639 static struct hash_control *mips16_op_hash = NULL;
640
641 /* The opcode hash table we use for the microMIPS ASE.  */
642 static struct hash_control *micromips_op_hash = NULL;
643
644 /* This array holds the chars that always start a comment.  If the
645     pre-processor is disabled, these aren't very useful */
646 const char comment_chars[] = "#";
647
648 /* This array holds the chars that only start a comment at the beginning of
649    a line.  If the line seems to have the form '# 123 filename'
650    .line and .file directives will appear in the pre-processed output */
651 /* Note that input_file.c hand checks for '#' at the beginning of the
652    first line of the input file.  This is because the compiler outputs
653    #NO_APP at the beginning of its output.  */
654 /* Also note that C style comments are always supported.  */
655 const char line_comment_chars[] = "#";
656
657 /* This array holds machine specific line separator characters.  */
658 const char line_separator_chars[] = ";";
659
660 /* Chars that can be used to separate mant from exp in floating point nums */
661 const char EXP_CHARS[] = "eE";
662
663 /* Chars that mean this number is a floating point constant */
664 /* As in 0f12.456 */
665 /* or    0d1.2345e12 */
666 const char FLT_CHARS[] = "rRsSfFdDxXpP";
667
668 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
669    changed in read.c .  Ideally it shouldn't have to know about it at all,
670    but nothing is ideal around here.
671  */
672
673 static char *insn_error;
674
675 static int auto_align = 1;
676
677 /* When outputting SVR4 PIC code, the assembler needs to know the
678    offset in the stack frame from which to restore the $gp register.
679    This is set by the .cprestore pseudo-op, and saved in this
680    variable.  */
681 static offsetT mips_cprestore_offset = -1;
682
683 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
684    more optimizations, it can use a register value instead of a memory-saved
685    offset and even an other register than $gp as global pointer.  */
686 static offsetT mips_cpreturn_offset = -1;
687 static int mips_cpreturn_register = -1;
688 static int mips_gp_register = GP;
689 static int mips_gprel_offset = 0;
690
691 /* Whether mips_cprestore_offset has been set in the current function
692    (or whether it has already been warned about, if not).  */
693 static int mips_cprestore_valid = 0;
694
695 /* This is the register which holds the stack frame, as set by the
696    .frame pseudo-op.  This is needed to implement .cprestore.  */
697 static int mips_frame_reg = SP;
698
699 /* Whether mips_frame_reg has been set in the current function
700    (or whether it has already been warned about, if not).  */
701 static int mips_frame_reg_valid = 0;
702
703 /* To output NOP instructions correctly, we need to keep information
704    about the previous two instructions.  */
705
706 /* Whether we are optimizing.  The default value of 2 means to remove
707    unneeded NOPs and swap branch instructions when possible.  A value
708    of 1 means to not swap branches.  A value of 0 means to always
709    insert NOPs.  */
710 static int mips_optimize = 2;
711
712 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
713    equivalent to seeing no -g option at all.  */
714 static int mips_debug = 0;
715
716 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
717 #define MAX_VR4130_NOPS 4
718
719 /* The maximum number of NOPs needed to fill delay slots.  */
720 #define MAX_DELAY_NOPS 2
721
722 /* The maximum number of NOPs needed for any purpose.  */
723 #define MAX_NOPS 4
724
725 /* A list of previous instructions, with index 0 being the most recent.
726    We need to look back MAX_NOPS instructions when filling delay slots
727    or working around processor errata.  We need to look back one
728    instruction further if we're thinking about using history[0] to
729    fill a branch delay slot.  */
730 static struct mips_cl_insn history[1 + MAX_NOPS];
731
732 /* Nop instructions used by emit_nop.  */
733 static struct mips_cl_insn nop_insn;
734 static struct mips_cl_insn mips16_nop_insn;
735 static struct mips_cl_insn micromips_nop16_insn;
736 static struct mips_cl_insn micromips_nop32_insn;
737
738 /* The appropriate nop for the current mode.  */
739 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
740                   : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
741
742 /* The size of NOP_INSN in bytes.  */
743 #define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
744
745 /* If this is set, it points to a frag holding nop instructions which
746    were inserted before the start of a noreorder section.  If those
747    nops turn out to be unnecessary, the size of the frag can be
748    decreased.  */
749 static fragS *prev_nop_frag;
750
751 /* The number of nop instructions we created in prev_nop_frag.  */
752 static int prev_nop_frag_holds;
753
754 /* The number of nop instructions that we know we need in
755    prev_nop_frag.  */
756 static int prev_nop_frag_required;
757
758 /* The number of instructions we've seen since prev_nop_frag.  */
759 static int prev_nop_frag_since;
760
761 /* For ECOFF and ELF, relocations against symbols are done in two
762    parts, with a HI relocation and a LO relocation.  Each relocation
763    has only 16 bits of space to store an addend.  This means that in
764    order for the linker to handle carries correctly, it must be able
765    to locate both the HI and the LO relocation.  This means that the
766    relocations must appear in order in the relocation table.
767
768    In order to implement this, we keep track of each unmatched HI
769    relocation.  We then sort them so that they immediately precede the
770    corresponding LO relocation.  */
771
772 struct mips_hi_fixup
773 {
774   /* Next HI fixup.  */
775   struct mips_hi_fixup *next;
776   /* This fixup.  */
777   fixS *fixp;
778   /* The section this fixup is in.  */
779   segT seg;
780 };
781
782 /* The list of unmatched HI relocs.  */
783
784 static struct mips_hi_fixup *mips_hi_fixup_list;
785
786 /* The frag containing the last explicit relocation operator.
787    Null if explicit relocations have not been used.  */
788
789 static fragS *prev_reloc_op_frag;
790
791 /* Map normal MIPS register numbers to mips16 register numbers.  */
792
793 #define X ILLEGAL_REG
794 static const int mips32_to_16_reg_map[] =
795 {
796   X, X, 2, 3, 4, 5, 6, 7,
797   X, X, X, X, X, X, X, X,
798   0, 1, X, X, X, X, X, X,
799   X, X, X, X, X, X, X, X
800 };
801 #undef X
802
803 /* Map mips16 register numbers to normal MIPS register numbers.  */
804
805 static const unsigned int mips16_to_32_reg_map[] =
806 {
807   16, 17, 2, 3, 4, 5, 6, 7
808 };
809
810 /* Map normal MIPS register numbers to microMIPS register numbers.  */
811
812 #define mips32_to_micromips_reg_b_map   mips32_to_16_reg_map
813 #define mips32_to_micromips_reg_c_map   mips32_to_16_reg_map
814 #define mips32_to_micromips_reg_d_map   mips32_to_16_reg_map
815 #define mips32_to_micromips_reg_e_map   mips32_to_16_reg_map
816 #define mips32_to_micromips_reg_f_map   mips32_to_16_reg_map
817 #define mips32_to_micromips_reg_g_map   mips32_to_16_reg_map
818 #define mips32_to_micromips_reg_l_map   mips32_to_16_reg_map
819
820 #define X ILLEGAL_REG
821 /* reg type h: 4, 5, 6.  */
822 static const int mips32_to_micromips_reg_h_map[] =
823 {
824   X, X, X, X, 4, 5, 6, X,
825   X, X, X, X, X, X, X, X,
826   X, X, X, X, X, X, X, X,
827   X, X, X, X, X, X, X, X
828 };
829
830 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20.  */
831 static const int mips32_to_micromips_reg_m_map[] =
832 {
833   0, X, 2, 3, X, X, X, X,
834   X, X, X, X, X, X, X, X,
835   4, 1, 5, 6, 7, X, X, X,
836   X, X, X, X, X, X, X, X
837 };
838
839 /* reg type q: 0, 2-7. 17.  */
840 static const int mips32_to_micromips_reg_q_map[] =
841 {
842   0, X, 2, 3, 4, 5, 6, 7,
843   X, X, X, X, X, X, X, X,
844   X, 1, X, X, X, X, X, X,
845   X, X, X, X, X, X, X, X
846 };
847
848 #define mips32_to_micromips_reg_n_map  mips32_to_micromips_reg_m_map
849 #undef X
850
851 /* Map microMIPS register numbers to normal MIPS register numbers.  */
852
853 #define micromips_to_32_reg_b_map       mips16_to_32_reg_map
854 #define micromips_to_32_reg_c_map       mips16_to_32_reg_map
855 #define micromips_to_32_reg_d_map       mips16_to_32_reg_map
856 #define micromips_to_32_reg_e_map       mips16_to_32_reg_map
857 #define micromips_to_32_reg_f_map       mips16_to_32_reg_map
858 #define micromips_to_32_reg_g_map       mips16_to_32_reg_map
859
860 /* The microMIPS registers with type h.  */
861 static const unsigned int micromips_to_32_reg_h_map[] =
862 {
863   5, 5, 6, 4, 4, 4, 4, 4
864 };
865
866 /* The microMIPS registers with type i.  */
867 static const unsigned int micromips_to_32_reg_i_map[] =
868 {
869   6, 7, 7, 21, 22, 5, 6, 7
870 };
871
872 #define micromips_to_32_reg_l_map       mips16_to_32_reg_map
873
874 /* The microMIPS registers with type m.  */
875 static const unsigned int micromips_to_32_reg_m_map[] =
876 {
877   0, 17, 2, 3, 16, 18, 19, 20
878 };
879
880 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
881
882 /* The microMIPS registers with type q.  */
883 static const unsigned int micromips_to_32_reg_q_map[] =
884 {
885   0, 17, 2, 3, 4, 5, 6, 7
886 };
887
888 /* microMIPS imm type B.  */
889 static const int micromips_imm_b_map[] =
890 {
891   1, 4, 8, 12, 16, 20, 24, -1
892 };
893
894 /* microMIPS imm type C.  */
895 static const int micromips_imm_c_map[] =
896 {
897   128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
898 };
899
900 /* Classifies the kind of instructions we're interested in when
901    implementing -mfix-vr4120.  */
902 enum fix_vr4120_class
903 {
904   FIX_VR4120_MACC,
905   FIX_VR4120_DMACC,
906   FIX_VR4120_MULT,
907   FIX_VR4120_DMULT,
908   FIX_VR4120_DIV,
909   FIX_VR4120_MTHILO,
910   NUM_FIX_VR4120_CLASSES
911 };
912
913 /* ...likewise -mfix-loongson2f-jump.  */
914 static bfd_boolean mips_fix_loongson2f_jump;
915
916 /* ...likewise -mfix-loongson2f-nop.  */
917 static bfd_boolean mips_fix_loongson2f_nop;
918
919 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
920 static bfd_boolean mips_fix_loongson2f;
921
922 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
923    there must be at least one other instruction between an instruction
924    of type X and an instruction of type Y.  */
925 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
926
927 /* True if -mfix-vr4120 is in force.  */
928 static int mips_fix_vr4120;
929
930 /* ...likewise -mfix-vr4130.  */
931 static int mips_fix_vr4130;
932
933 /* ...likewise -mfix-24k.  */
934 static int mips_fix_24k;
935
936 /* ...likewise -mfix-cn63xxp1 */
937 static bfd_boolean mips_fix_cn63xxp1;
938
939 /* We don't relax branches by default, since this causes us to expand
940    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
941    fail to compute the offset before expanding the macro to the most
942    efficient expansion.  */
943
944 static int mips_relax_branch;
945 \f
946 /* The expansion of many macros depends on the type of symbol that
947    they refer to.  For example, when generating position-dependent code,
948    a macro that refers to a symbol may have two different expansions,
949    one which uses GP-relative addresses and one which uses absolute
950    addresses.  When generating SVR4-style PIC, a macro may have
951    different expansions for local and global symbols.
952
953    We handle these situations by generating both sequences and putting
954    them in variant frags.  In position-dependent code, the first sequence
955    will be the GP-relative one and the second sequence will be the
956    absolute one.  In SVR4 PIC, the first sequence will be for global
957    symbols and the second will be for local symbols.
958
959    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
960    SECOND are the lengths of the two sequences in bytes.  These fields
961    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
962    the subtype has the following flags:
963
964    RELAX_USE_SECOND
965         Set if it has been decided that we should use the second
966         sequence instead of the first.
967
968    RELAX_SECOND_LONGER
969         Set in the first variant frag if the macro's second implementation
970         is longer than its first.  This refers to the macro as a whole,
971         not an individual relaxation.
972
973    RELAX_NOMACRO
974         Set in the first variant frag if the macro appeared in a .set nomacro
975         block and if one alternative requires a warning but the other does not.
976
977    RELAX_DELAY_SLOT
978         Like RELAX_NOMACRO, but indicates that the macro appears in a branch
979         delay slot.
980
981    RELAX_DELAY_SLOT_16BIT
982         Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
983         16-bit instruction.
984
985    RELAX_DELAY_SLOT_SIZE_FIRST
986         Like RELAX_DELAY_SLOT, but indicates that the first implementation of
987         the macro is of the wrong size for the branch delay slot.
988
989    RELAX_DELAY_SLOT_SIZE_SECOND
990         Like RELAX_DELAY_SLOT, but indicates that the second implementation of
991         the macro is of the wrong size for the branch delay slot.
992
993    The frag's "opcode" points to the first fixup for relaxable code.
994
995    Relaxable macros are generated using a sequence such as:
996
997       relax_start (SYMBOL);
998       ... generate first expansion ...
999       relax_switch ();
1000       ... generate second expansion ...
1001       relax_end ();
1002
1003    The code and fixups for the unwanted alternative are discarded
1004    by md_convert_frag.  */
1005 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
1006
1007 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1008 #define RELAX_SECOND(X) ((X) & 0xff)
1009 #define RELAX_USE_SECOND 0x10000
1010 #define RELAX_SECOND_LONGER 0x20000
1011 #define RELAX_NOMACRO 0x40000
1012 #define RELAX_DELAY_SLOT 0x80000
1013 #define RELAX_DELAY_SLOT_16BIT 0x100000
1014 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1015 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
1016
1017 /* Branch without likely bit.  If label is out of range, we turn:
1018
1019         beq reg1, reg2, label
1020         delay slot
1021
1022    into
1023
1024         bne reg1, reg2, 0f
1025         nop
1026         j label
1027      0: delay slot
1028
1029    with the following opcode replacements:
1030
1031         beq <-> bne
1032         blez <-> bgtz
1033         bltz <-> bgez
1034         bc1f <-> bc1t
1035
1036         bltzal <-> bgezal  (with jal label instead of j label)
1037
1038    Even though keeping the delay slot instruction in the delay slot of
1039    the branch would be more efficient, it would be very tricky to do
1040    correctly, because we'd have to introduce a variable frag *after*
1041    the delay slot instruction, and expand that instead.  Let's do it
1042    the easy way for now, even if the branch-not-taken case now costs
1043    one additional instruction.  Out-of-range branches are not supposed
1044    to be common, anyway.
1045
1046    Branch likely.  If label is out of range, we turn:
1047
1048         beql reg1, reg2, label
1049         delay slot (annulled if branch not taken)
1050
1051    into
1052
1053         beql reg1, reg2, 1f
1054         nop
1055         beql $0, $0, 2f
1056         nop
1057      1: j[al] label
1058         delay slot (executed only if branch taken)
1059      2:
1060
1061    It would be possible to generate a shorter sequence by losing the
1062    likely bit, generating something like:
1063
1064         bne reg1, reg2, 0f
1065         nop
1066         j[al] label
1067         delay slot (executed only if branch taken)
1068      0:
1069
1070         beql -> bne
1071         bnel -> beq
1072         blezl -> bgtz
1073         bgtzl -> blez
1074         bltzl -> bgez
1075         bgezl -> bltz
1076         bc1fl -> bc1t
1077         bc1tl -> bc1f
1078
1079         bltzall -> bgezal  (with jal label instead of j label)
1080         bgezall -> bltzal  (ditto)
1081
1082
1083    but it's not clear that it would actually improve performance.  */
1084 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar)   \
1085   ((relax_substateT)                                            \
1086    (0xc0000000                                                  \
1087     | ((at) & 0x1f)                                             \
1088     | ((toofar) ? 0x20 : 0)                                     \
1089     | ((link) ? 0x40 : 0)                                       \
1090     | ((likely) ? 0x80 : 0)                                     \
1091     | ((uncond) ? 0x100 : 0)))
1092 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1093 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1094 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1095 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1096 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1097 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1098
1099 /* For mips16 code, we use an entirely different form of relaxation.
1100    mips16 supports two versions of most instructions which take
1101    immediate values: a small one which takes some small value, and a
1102    larger one which takes a 16 bit value.  Since branches also follow
1103    this pattern, relaxing these values is required.
1104
1105    We can assemble both mips16 and normal MIPS code in a single
1106    object.  Therefore, we need to support this type of relaxation at
1107    the same time that we support the relaxation described above.  We
1108    use the high bit of the subtype field to distinguish these cases.
1109
1110    The information we store for this type of relaxation is the
1111    argument code found in the opcode file for this relocation, whether
1112    the user explicitly requested a small or extended form, and whether
1113    the relocation is in a jump or jal delay slot.  That tells us the
1114    size of the value, and how it should be stored.  We also store
1115    whether the fragment is considered to be extended or not.  We also
1116    store whether this is known to be a branch to a different section,
1117    whether we have tried to relax this frag yet, and whether we have
1118    ever extended a PC relative fragment because of a shift count.  */
1119 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1120   (0x80000000                                                   \
1121    | ((type) & 0xff)                                            \
1122    | ((small) ? 0x100 : 0)                                      \
1123    | ((ext) ? 0x200 : 0)                                        \
1124    | ((dslot) ? 0x400 : 0)                                      \
1125    | ((jal_dslot) ? 0x800 : 0))
1126 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1127 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1128 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1129 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1130 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1131 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1132 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1133 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1134 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1135 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1136 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1137 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1138
1139 /* For microMIPS code, we use relaxation similar to one we use for
1140    MIPS16 code.  Some instructions that take immediate values support
1141    two encodings: a small one which takes some small value, and a
1142    larger one which takes a 16 bit value.  As some branches also follow
1143    this pattern, relaxing these values is required.
1144
1145    We can assemble both microMIPS and normal MIPS code in a single
1146    object.  Therefore, we need to support this type of relaxation at
1147    the same time that we support the relaxation described above.  We
1148    use one of the high bits of the subtype field to distinguish these
1149    cases.
1150
1151    The information we store for this type of relaxation is the argument
1152    code found in the opcode file for this relocation, the register
1153    selected as the assembler temporary, whether the branch is
1154    unconditional, whether it is compact, whether it stores the link
1155    address implicitly in $ra, whether relaxation of out-of-range 32-bit
1156    branches to a sequence of instructions is enabled, and whether the
1157    displacement of a branch is too large to fit as an immediate argument
1158    of a 16-bit and a 32-bit branch, respectively.  */
1159 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1160                                relax32, toofar16, toofar32)     \
1161   (0x40000000                                                   \
1162    | ((type) & 0xff)                                            \
1163    | (((at) & 0x1f) << 8)                                       \
1164    | ((uncond) ? 0x2000 : 0)                                    \
1165    | ((compact) ? 0x4000 : 0)                                   \
1166    | ((link) ? 0x8000 : 0)                                      \
1167    | ((relax32) ? 0x10000 : 0)                                  \
1168    | ((toofar16) ? 0x20000 : 0)                                 \
1169    | ((toofar32) ? 0x40000 : 0))
1170 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1171 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1172 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1173 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1174 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1175 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1176 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1177
1178 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1179 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1180 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1181 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1182 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1183 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1184
1185 /* Sign-extend 16-bit value X.  */
1186 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1187
1188 /* Is the given value a sign-extended 32-bit value?  */
1189 #define IS_SEXT_32BIT_NUM(x)                                            \
1190   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
1191    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1192
1193 /* Is the given value a sign-extended 16-bit value?  */
1194 #define IS_SEXT_16BIT_NUM(x)                                            \
1195   (((x) &~ (offsetT) 0x7fff) == 0                                       \
1196    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1197
1198 /* Is the given value a sign-extended 12-bit value?  */
1199 #define IS_SEXT_12BIT_NUM(x)                                            \
1200   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1201
1202 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1203 #define IS_ZEXT_32BIT_NUM(x)                                            \
1204   (((x) &~ (offsetT) 0xffffffff) == 0                                   \
1205    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1206
1207 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1208    VALUE << SHIFT.  VALUE is evaluated exactly once.  */
1209 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1210   (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1211               | (((VALUE) & (MASK)) << (SHIFT)))
1212
1213 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1214    SHIFT places.  */
1215 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1216   (((STRUCT) >> (SHIFT)) & (MASK))
1217
1218 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1219    INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1220
1221    include/opcode/mips.h specifies operand fields using the macros
1222    OP_MASK_<FIELD> and OP_SH_<FIELD>.  The MIPS16 equivalents start
1223    with "MIPS16OP" instead of "OP".  */
1224 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1225   do \
1226     if (!(MICROMIPS)) \
1227       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1228                    OP_MASK_##FIELD, OP_SH_##FIELD); \
1229     else \
1230       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1231                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1232   while (0)
1233 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1234   INSERT_BITS ((INSN).insn_opcode, VALUE, \
1235                 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1236
1237 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1238 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1239   (!(MICROMIPS) \
1240    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1241    : EXTRACT_BITS ((INSN).insn_opcode, \
1242                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1243 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1244   EXTRACT_BITS ((INSN).insn_opcode, \
1245                 MIPS16OP_MASK_##FIELD, \
1246                 MIPS16OP_SH_##FIELD)
1247
1248 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1249 #define MIPS16_EXTEND (0xf000U << 16)
1250 \f
1251 /* Whether or not we are emitting a branch-likely macro.  */
1252 static bfd_boolean emit_branch_likely_macro = FALSE;
1253
1254 /* Global variables used when generating relaxable macros.  See the
1255    comment above RELAX_ENCODE for more details about how relaxation
1256    is used.  */
1257 static struct {
1258   /* 0 if we're not emitting a relaxable macro.
1259      1 if we're emitting the first of the two relaxation alternatives.
1260      2 if we're emitting the second alternative.  */
1261   int sequence;
1262
1263   /* The first relaxable fixup in the current frag.  (In other words,
1264      the first fixup that refers to relaxable code.)  */
1265   fixS *first_fixup;
1266
1267   /* sizes[0] says how many bytes of the first alternative are stored in
1268      the current frag.  Likewise sizes[1] for the second alternative.  */
1269   unsigned int sizes[2];
1270
1271   /* The symbol on which the choice of sequence depends.  */
1272   symbolS *symbol;
1273 } mips_relax;
1274 \f
1275 /* Global variables used to decide whether a macro needs a warning.  */
1276 static struct {
1277   /* True if the macro is in a branch delay slot.  */
1278   bfd_boolean delay_slot_p;
1279
1280   /* Set to the length in bytes required if the macro is in a delay slot
1281      that requires a specific length of instruction, otherwise zero.  */
1282   unsigned int delay_slot_length;
1283
1284   /* For relaxable macros, sizes[0] is the length of the first alternative
1285      in bytes and sizes[1] is the length of the second alternative.
1286      For non-relaxable macros, both elements give the length of the
1287      macro in bytes.  */
1288   unsigned int sizes[2];
1289
1290   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1291      instruction of the first alternative in bytes and first_insn_sizes[1]
1292      is the length of the first instruction of the second alternative.
1293      For non-relaxable macros, both elements give the length of the first
1294      instruction in bytes.
1295
1296      Set to zero if we haven't yet seen the first instruction.  */
1297   unsigned int first_insn_sizes[2];
1298
1299   /* For relaxable macros, insns[0] is the number of instructions for the
1300      first alternative and insns[1] is the number of instructions for the
1301      second alternative.
1302
1303      For non-relaxable macros, both elements give the number of
1304      instructions for the macro.  */
1305   unsigned int insns[2];
1306
1307   /* The first variant frag for this macro.  */
1308   fragS *first_frag;
1309 } mips_macro_warning;
1310 \f
1311 /* Prototypes for static functions.  */
1312
1313 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1314
1315 static void append_insn
1316   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1317    bfd_boolean expansionp);
1318 static void mips_no_prev_insn (void);
1319 static void macro_build (expressionS *, const char *, const char *, ...);
1320 static void mips16_macro_build
1321   (expressionS *, const char *, const char *, va_list *);
1322 static void load_register (int, expressionS *, int);
1323 static void macro_start (void);
1324 static void macro_end (void);
1325 static void macro (struct mips_cl_insn * ip);
1326 static void mips16_macro (struct mips_cl_insn * ip);
1327 static void mips_ip (char *str, struct mips_cl_insn * ip);
1328 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1329 static void mips16_immed
1330   (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1331    unsigned int, unsigned long *);
1332 static size_t my_getSmallExpression
1333   (expressionS *, bfd_reloc_code_real_type *, char *);
1334 static void my_getExpression (expressionS *, char *);
1335 static void s_align (int);
1336 static void s_change_sec (int);
1337 static void s_change_section (int);
1338 static void s_cons (int);
1339 static void s_float_cons (int);
1340 static void s_mips_globl (int);
1341 static void s_option (int);
1342 static void s_mipsset (int);
1343 static void s_abicalls (int);
1344 static void s_cpload (int);
1345 static void s_cpsetup (int);
1346 static void s_cplocal (int);
1347 static void s_cprestore (int);
1348 static void s_cpreturn (int);
1349 static void s_dtprelword (int);
1350 static void s_dtpreldword (int);
1351 static void s_tprelword (int);
1352 static void s_tpreldword (int);
1353 static void s_gpvalue (int);
1354 static void s_gpword (int);
1355 static void s_gpdword (int);
1356 static void s_cpadd (int);
1357 static void s_insn (int);
1358 static void md_obj_begin (void);
1359 static void md_obj_end (void);
1360 static void s_mips_ent (int);
1361 static void s_mips_end (int);
1362 static void s_mips_frame (int);
1363 static void s_mips_mask (int reg_type);
1364 static void s_mips_stab (int);
1365 static void s_mips_weakext (int);
1366 static void s_mips_file (int);
1367 static void s_mips_loc (int);
1368 static bfd_boolean pic_need_relax (symbolS *, asection *);
1369 static int relaxed_branch_length (fragS *, asection *, int);
1370 static int validate_mips_insn (const struct mips_opcode *);
1371 static int validate_micromips_insn (const struct mips_opcode *);
1372 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1373 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1374
1375 /* Table and functions used to map between CPU/ISA names, and
1376    ISA levels, and CPU numbers.  */
1377
1378 struct mips_cpu_info
1379 {
1380   const char *name;           /* CPU or ISA name.  */
1381   int flags;                  /* ASEs available, or ISA flag.  */
1382   int isa;                    /* ISA level.  */
1383   int cpu;                    /* CPU number (default CPU if ISA).  */
1384 };
1385
1386 #define MIPS_CPU_IS_ISA         0x0001  /* Is this an ISA?  (If 0, a CPU.) */
1387 #define MIPS_CPU_ASE_SMARTMIPS  0x0002  /* CPU implements SmartMIPS ASE */
1388 #define MIPS_CPU_ASE_DSP        0x0004  /* CPU implements DSP ASE */
1389 #define MIPS_CPU_ASE_MT         0x0008  /* CPU implements MT ASE */
1390 #define MIPS_CPU_ASE_MIPS3D     0x0010  /* CPU implements MIPS-3D ASE */
1391 #define MIPS_CPU_ASE_MDMX       0x0020  /* CPU implements MDMX ASE */
1392 #define MIPS_CPU_ASE_DSPR2      0x0040  /* CPU implements DSP R2 ASE */
1393 #define MIPS_CPU_ASE_MCU        0x0080  /* CPU implements MCU ASE */
1394
1395 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1396 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1397 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1398 \f
1399 /* Pseudo-op table.
1400
1401    The following pseudo-ops from the Kane and Heinrich MIPS book
1402    should be defined here, but are currently unsupported: .alias,
1403    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1404
1405    The following pseudo-ops from the Kane and Heinrich MIPS book are
1406    specific to the type of debugging information being generated, and
1407    should be defined by the object format: .aent, .begin, .bend,
1408    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1409    .vreg.
1410
1411    The following pseudo-ops from the Kane and Heinrich MIPS book are
1412    not MIPS CPU specific, but are also not specific to the object file
1413    format.  This file is probably the best place to define them, but
1414    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1415
1416 static const pseudo_typeS mips_pseudo_table[] =
1417 {
1418   /* MIPS specific pseudo-ops.  */
1419   {"option", s_option, 0},
1420   {"set", s_mipsset, 0},
1421   {"rdata", s_change_sec, 'r'},
1422   {"sdata", s_change_sec, 's'},
1423   {"livereg", s_ignore, 0},
1424   {"abicalls", s_abicalls, 0},
1425   {"cpload", s_cpload, 0},
1426   {"cpsetup", s_cpsetup, 0},
1427   {"cplocal", s_cplocal, 0},
1428   {"cprestore", s_cprestore, 0},
1429   {"cpreturn", s_cpreturn, 0},
1430   {"dtprelword", s_dtprelword, 0},
1431   {"dtpreldword", s_dtpreldword, 0},
1432   {"tprelword", s_tprelword, 0},
1433   {"tpreldword", s_tpreldword, 0},
1434   {"gpvalue", s_gpvalue, 0},
1435   {"gpword", s_gpword, 0},
1436   {"gpdword", s_gpdword, 0},
1437   {"cpadd", s_cpadd, 0},
1438   {"insn", s_insn, 0},
1439
1440   /* Relatively generic pseudo-ops that happen to be used on MIPS
1441      chips.  */
1442   {"asciiz", stringer, 8 + 1},
1443   {"bss", s_change_sec, 'b'},
1444   {"err", s_err, 0},
1445   {"half", s_cons, 1},
1446   {"dword", s_cons, 3},
1447   {"weakext", s_mips_weakext, 0},
1448   {"origin", s_org, 0},
1449   {"repeat", s_rept, 0},
1450
1451   /* For MIPS this is non-standard, but we define it for consistency.  */
1452   {"sbss", s_change_sec, 'B'},
1453
1454   /* These pseudo-ops are defined in read.c, but must be overridden
1455      here for one reason or another.  */
1456   {"align", s_align, 0},
1457   {"byte", s_cons, 0},
1458   {"data", s_change_sec, 'd'},
1459   {"double", s_float_cons, 'd'},
1460   {"float", s_float_cons, 'f'},
1461   {"globl", s_mips_globl, 0},
1462   {"global", s_mips_globl, 0},
1463   {"hword", s_cons, 1},
1464   {"int", s_cons, 2},
1465   {"long", s_cons, 2},
1466   {"octa", s_cons, 4},
1467   {"quad", s_cons, 3},
1468   {"section", s_change_section, 0},
1469   {"short", s_cons, 1},
1470   {"single", s_float_cons, 'f'},
1471   {"stabn", s_mips_stab, 'n'},
1472   {"text", s_change_sec, 't'},
1473   {"word", s_cons, 2},
1474
1475   { "extern", ecoff_directive_extern, 0},
1476
1477   { NULL, NULL, 0 },
1478 };
1479
1480 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1481 {
1482   /* These pseudo-ops should be defined by the object file format.
1483      However, a.out doesn't support them, so we have versions here.  */
1484   {"aent", s_mips_ent, 1},
1485   {"bgnb", s_ignore, 0},
1486   {"end", s_mips_end, 0},
1487   {"endb", s_ignore, 0},
1488   {"ent", s_mips_ent, 0},
1489   {"file", s_mips_file, 0},
1490   {"fmask", s_mips_mask, 'F'},
1491   {"frame", s_mips_frame, 0},
1492   {"loc", s_mips_loc, 0},
1493   {"mask", s_mips_mask, 'R'},
1494   {"verstamp", s_ignore, 0},
1495   { NULL, NULL, 0 },
1496 };
1497
1498 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1499    purpose of the `.dc.a' internal pseudo-op.  */
1500
1501 int
1502 mips_address_bytes (void)
1503 {
1504   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1505 }
1506
1507 extern void pop_insert (const pseudo_typeS *);
1508
1509 void
1510 mips_pop_insert (void)
1511 {
1512   pop_insert (mips_pseudo_table);
1513   if (! ECOFF_DEBUGGING)
1514     pop_insert (mips_nonecoff_pseudo_table);
1515 }
1516 \f
1517 /* Symbols labelling the current insn.  */
1518
1519 struct insn_label_list
1520 {
1521   struct insn_label_list *next;
1522   symbolS *label;
1523 };
1524
1525 static struct insn_label_list *free_insn_labels;
1526 #define label_list tc_segment_info_data.labels
1527
1528 static void mips_clear_insn_labels (void);
1529 static void mips_mark_labels (void);
1530 static void mips_compressed_mark_labels (void);
1531
1532 static inline void
1533 mips_clear_insn_labels (void)
1534 {
1535   register struct insn_label_list **pl;
1536   segment_info_type *si;
1537
1538   if (now_seg)
1539     {
1540       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1541         ;
1542       
1543       si = seg_info (now_seg);
1544       *pl = si->label_list;
1545       si->label_list = NULL;
1546     }
1547 }
1548
1549 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1550
1551 static inline void
1552 mips_mark_labels (void)
1553 {
1554   if (HAVE_CODE_COMPRESSION)
1555     mips_compressed_mark_labels ();
1556 }
1557 \f
1558 static char *expr_end;
1559
1560 /* Expressions which appear in instructions.  These are set by
1561    mips_ip.  */
1562
1563 static expressionS imm_expr;
1564 static expressionS imm2_expr;
1565 static expressionS offset_expr;
1566
1567 /* Relocs associated with imm_expr and offset_expr.  */
1568
1569 static bfd_reloc_code_real_type imm_reloc[3]
1570   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1571 static bfd_reloc_code_real_type offset_reloc[3]
1572   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1573
1574 /* This is set to the resulting size of the instruction to be produced
1575    by mips16_ip if an explicit extension is used or by mips_ip if an
1576    explicit size is supplied.  */
1577
1578 static unsigned int forced_insn_length;
1579
1580 /* True if we are assembling an instruction.  All dot symbols defined during
1581    this time should be treated as code labels.  */
1582
1583 static bfd_boolean mips_assembling_insn;
1584
1585 #ifdef OBJ_ELF
1586 /* The pdr segment for per procedure frame/regmask info.  Not used for
1587    ECOFF debugging.  */
1588
1589 static segT pdr_seg;
1590 #endif
1591
1592 /* The default target format to use.  */
1593
1594 #if defined (TE_FreeBSD)
1595 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1596 #elif defined (TE_TMIPS)
1597 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1598 #else
1599 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1600 #endif
1601
1602 const char *
1603 mips_target_format (void)
1604 {
1605   switch (OUTPUT_FLAVOR)
1606     {
1607     case bfd_target_ecoff_flavour:
1608       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1609     case bfd_target_coff_flavour:
1610       return "pe-mips";
1611     case bfd_target_elf_flavour:
1612 #ifdef TE_VXWORKS
1613       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1614         return (target_big_endian
1615                 ? "elf32-bigmips-vxworks"
1616                 : "elf32-littlemips-vxworks");
1617 #endif
1618       return (target_big_endian
1619               ? (HAVE_64BIT_OBJECTS
1620                  ? ELF_TARGET ("elf64-", "big")
1621                  : (HAVE_NEWABI
1622                     ? ELF_TARGET ("elf32-n", "big")
1623                     : ELF_TARGET ("elf32-", "big")))
1624               : (HAVE_64BIT_OBJECTS
1625                  ? ELF_TARGET ("elf64-", "little")
1626                  : (HAVE_NEWABI
1627                     ? ELF_TARGET ("elf32-n", "little")
1628                     : ELF_TARGET ("elf32-", "little"))));
1629     default:
1630       abort ();
1631       return NULL;
1632     }
1633 }
1634
1635 /* Return the length of a microMIPS instruction in bytes.  If bits of
1636    the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1637    Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1638    major opcode) will require further modifications to the opcode
1639    table.  */
1640
1641 static inline unsigned int
1642 micromips_insn_length (const struct mips_opcode *mo)
1643 {
1644   return (mo->mask >> 16) == 0 ? 2 : 4;
1645 }
1646
1647 /* Return the length of MIPS16 instruction OPCODE.  */
1648
1649 static inline unsigned int
1650 mips16_opcode_length (unsigned long opcode)
1651 {
1652   return (opcode >> 16) == 0 ? 2 : 4;
1653 }
1654
1655 /* Return the length of instruction INSN.  */
1656
1657 static inline unsigned int
1658 insn_length (const struct mips_cl_insn *insn)
1659 {
1660   if (mips_opts.micromips)
1661     return micromips_insn_length (insn->insn_mo);
1662   else if (mips_opts.mips16)
1663     return mips16_opcode_length (insn->insn_opcode);
1664   else
1665     return 4;
1666 }
1667
1668 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
1669
1670 static void
1671 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1672 {
1673   size_t i;
1674
1675   insn->insn_mo = mo;
1676   insn->insn_opcode = mo->match;
1677   insn->frag = NULL;
1678   insn->where = 0;
1679   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1680     insn->fixp[i] = NULL;
1681   insn->fixed_p = (mips_opts.noreorder > 0);
1682   insn->noreorder_p = (mips_opts.noreorder > 0);
1683   insn->mips16_absolute_jump_p = 0;
1684   insn->complete_p = 0;
1685   insn->cleared_p = 0;
1686 }
1687
1688 /* Record the current MIPS16/microMIPS mode in now_seg.  */
1689
1690 static void
1691 mips_record_compressed_mode (void)
1692 {
1693   segment_info_type *si;
1694
1695   si = seg_info (now_seg);
1696   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1697     si->tc_segment_info_data.mips16 = mips_opts.mips16;
1698   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1699     si->tc_segment_info_data.micromips = mips_opts.micromips;
1700 }
1701
1702 /* Read a standard MIPS instruction from BUF.  */
1703
1704 static unsigned long
1705 read_insn (char *buf)
1706 {
1707   if (target_big_endian)
1708     return bfd_getb32 ((bfd_byte *) buf);
1709   else
1710     return bfd_getl32 ((bfd_byte *) buf);
1711 }
1712
1713 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
1714    the next byte.  */
1715
1716 static char *
1717 write_insn (char *buf, unsigned int insn)
1718 {
1719   md_number_to_chars (buf, insn, 4);
1720   return buf + 4;
1721 }
1722
1723 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
1724    has length LENGTH.  */
1725
1726 static unsigned long
1727 read_compressed_insn (char *buf, unsigned int length)
1728 {
1729   unsigned long insn;
1730   unsigned int i;
1731
1732   insn = 0;
1733   for (i = 0; i < length; i += 2)
1734     {
1735       insn <<= 16;
1736       if (target_big_endian)
1737         insn |= bfd_getb16 ((char *) buf);
1738       else
1739         insn |= bfd_getl16 ((char *) buf);
1740       buf += 2;
1741     }
1742   return insn;
1743 }
1744
1745 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1746    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
1747
1748 static char *
1749 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1750 {
1751   unsigned int i;
1752
1753   for (i = 0; i < length; i += 2)
1754     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1755   return buf + length;
1756 }
1757
1758 /* Install INSN at the location specified by its "frag" and "where" fields.  */
1759
1760 static void
1761 install_insn (const struct mips_cl_insn *insn)
1762 {
1763   char *f = insn->frag->fr_literal + insn->where;
1764   if (HAVE_CODE_COMPRESSION)
1765     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1766   else
1767     write_insn (f, insn->insn_opcode);
1768   mips_record_compressed_mode ();
1769 }
1770
1771 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
1772    and install the opcode in the new location.  */
1773
1774 static void
1775 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1776 {
1777   size_t i;
1778
1779   insn->frag = frag;
1780   insn->where = where;
1781   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1782     if (insn->fixp[i] != NULL)
1783       {
1784         insn->fixp[i]->fx_frag = frag;
1785         insn->fixp[i]->fx_where = where;
1786       }
1787   install_insn (insn);
1788 }
1789
1790 /* Add INSN to the end of the output.  */
1791
1792 static void
1793 add_fixed_insn (struct mips_cl_insn *insn)
1794 {
1795   char *f = frag_more (insn_length (insn));
1796   move_insn (insn, frag_now, f - frag_now->fr_literal);
1797 }
1798
1799 /* Start a variant frag and move INSN to the start of the variant part,
1800    marking it as fixed.  The other arguments are as for frag_var.  */
1801
1802 static void
1803 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1804                   relax_substateT subtype, symbolS *symbol, offsetT offset)
1805 {
1806   frag_grow (max_chars);
1807   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1808   insn->fixed_p = 1;
1809   frag_var (rs_machine_dependent, max_chars, var,
1810             subtype, symbol, offset, NULL);
1811 }
1812
1813 /* Insert N copies of INSN into the history buffer, starting at
1814    position FIRST.  Neither FIRST nor N need to be clipped.  */
1815
1816 static void
1817 insert_into_history (unsigned int first, unsigned int n,
1818                      const struct mips_cl_insn *insn)
1819 {
1820   if (mips_relax.sequence != 2)
1821     {
1822       unsigned int i;
1823
1824       for (i = ARRAY_SIZE (history); i-- > first;)
1825         if (i >= first + n)
1826           history[i] = history[i - n];
1827         else
1828           history[i] = *insn;
1829     }
1830 }
1831
1832 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
1833    the idea is to make it obvious at a glance that each errata is
1834    included.  */
1835
1836 static void
1837 init_vr4120_conflicts (void)
1838 {
1839 #define CONFLICT(FIRST, SECOND) \
1840     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1841
1842   /* Errata 21 - [D]DIV[U] after [D]MACC */
1843   CONFLICT (MACC, DIV);
1844   CONFLICT (DMACC, DIV);
1845
1846   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
1847   CONFLICT (DMULT, DMULT);
1848   CONFLICT (DMULT, DMACC);
1849   CONFLICT (DMACC, DMULT);
1850   CONFLICT (DMACC, DMACC);
1851
1852   /* Errata 24 - MT{LO,HI} after [D]MACC */
1853   CONFLICT (MACC, MTHILO);
1854   CONFLICT (DMACC, MTHILO);
1855
1856   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1857      instruction is executed immediately after a MACC or DMACC
1858      instruction, the result of [either instruction] is incorrect."  */
1859   CONFLICT (MACC, MULT);
1860   CONFLICT (MACC, DMULT);
1861   CONFLICT (DMACC, MULT);
1862   CONFLICT (DMACC, DMULT);
1863
1864   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1865      executed immediately after a DMULT, DMULTU, DIV, DIVU,
1866      DDIV or DDIVU instruction, the result of the MACC or
1867      DMACC instruction is incorrect.".  */
1868   CONFLICT (DMULT, MACC);
1869   CONFLICT (DMULT, DMACC);
1870   CONFLICT (DIV, MACC);
1871   CONFLICT (DIV, DMACC);
1872
1873 #undef CONFLICT
1874 }
1875
1876 struct regname {
1877   const char *name;
1878   unsigned int num;
1879 };
1880
1881 #define RTYPE_MASK      0x1ff00
1882 #define RTYPE_NUM       0x00100
1883 #define RTYPE_FPU       0x00200
1884 #define RTYPE_FCC       0x00400
1885 #define RTYPE_VEC       0x00800
1886 #define RTYPE_GP        0x01000
1887 #define RTYPE_CP0       0x02000
1888 #define RTYPE_PC        0x04000
1889 #define RTYPE_ACC       0x08000
1890 #define RTYPE_CCC       0x10000
1891 #define RNUM_MASK       0x000ff
1892 #define RWARN           0x80000
1893
1894 #define GENERIC_REGISTER_NUMBERS \
1895     {"$0",      RTYPE_NUM | 0},  \
1896     {"$1",      RTYPE_NUM | 1},  \
1897     {"$2",      RTYPE_NUM | 2},  \
1898     {"$3",      RTYPE_NUM | 3},  \
1899     {"$4",      RTYPE_NUM | 4},  \
1900     {"$5",      RTYPE_NUM | 5},  \
1901     {"$6",      RTYPE_NUM | 6},  \
1902     {"$7",      RTYPE_NUM | 7},  \
1903     {"$8",      RTYPE_NUM | 8},  \
1904     {"$9",      RTYPE_NUM | 9},  \
1905     {"$10",     RTYPE_NUM | 10}, \
1906     {"$11",     RTYPE_NUM | 11}, \
1907     {"$12",     RTYPE_NUM | 12}, \
1908     {"$13",     RTYPE_NUM | 13}, \
1909     {"$14",     RTYPE_NUM | 14}, \
1910     {"$15",     RTYPE_NUM | 15}, \
1911     {"$16",     RTYPE_NUM | 16}, \
1912     {"$17",     RTYPE_NUM | 17}, \
1913     {"$18",     RTYPE_NUM | 18}, \
1914     {"$19",     RTYPE_NUM | 19}, \
1915     {"$20",     RTYPE_NUM | 20}, \
1916     {"$21",     RTYPE_NUM | 21}, \
1917     {"$22",     RTYPE_NUM | 22}, \
1918     {"$23",     RTYPE_NUM | 23}, \
1919     {"$24",     RTYPE_NUM | 24}, \
1920     {"$25",     RTYPE_NUM | 25}, \
1921     {"$26",     RTYPE_NUM | 26}, \
1922     {"$27",     RTYPE_NUM | 27}, \
1923     {"$28",     RTYPE_NUM | 28}, \
1924     {"$29",     RTYPE_NUM | 29}, \
1925     {"$30",     RTYPE_NUM | 30}, \
1926     {"$31",     RTYPE_NUM | 31} 
1927
1928 #define FPU_REGISTER_NAMES       \
1929     {"$f0",     RTYPE_FPU | 0},  \
1930     {"$f1",     RTYPE_FPU | 1},  \
1931     {"$f2",     RTYPE_FPU | 2},  \
1932     {"$f3",     RTYPE_FPU | 3},  \
1933     {"$f4",     RTYPE_FPU | 4},  \
1934     {"$f5",     RTYPE_FPU | 5},  \
1935     {"$f6",     RTYPE_FPU | 6},  \
1936     {"$f7",     RTYPE_FPU | 7},  \
1937     {"$f8",     RTYPE_FPU | 8},  \
1938     {"$f9",     RTYPE_FPU | 9},  \
1939     {"$f10",    RTYPE_FPU | 10}, \
1940     {"$f11",    RTYPE_FPU | 11}, \
1941     {"$f12",    RTYPE_FPU | 12}, \
1942     {"$f13",    RTYPE_FPU | 13}, \
1943     {"$f14",    RTYPE_FPU | 14}, \
1944     {"$f15",    RTYPE_FPU | 15}, \
1945     {"$f16",    RTYPE_FPU | 16}, \
1946     {"$f17",    RTYPE_FPU | 17}, \
1947     {"$f18",    RTYPE_FPU | 18}, \
1948     {"$f19",    RTYPE_FPU | 19}, \
1949     {"$f20",    RTYPE_FPU | 20}, \
1950     {"$f21",    RTYPE_FPU | 21}, \
1951     {"$f22",    RTYPE_FPU | 22}, \
1952     {"$f23",    RTYPE_FPU | 23}, \
1953     {"$f24",    RTYPE_FPU | 24}, \
1954     {"$f25",    RTYPE_FPU | 25}, \
1955     {"$f26",    RTYPE_FPU | 26}, \
1956     {"$f27",    RTYPE_FPU | 27}, \
1957     {"$f28",    RTYPE_FPU | 28}, \
1958     {"$f29",    RTYPE_FPU | 29}, \
1959     {"$f30",    RTYPE_FPU | 30}, \
1960     {"$f31",    RTYPE_FPU | 31}
1961
1962 #define FPU_CONDITION_CODE_NAMES \
1963     {"$fcc0",   RTYPE_FCC | 0},  \
1964     {"$fcc1",   RTYPE_FCC | 1},  \
1965     {"$fcc2",   RTYPE_FCC | 2},  \
1966     {"$fcc3",   RTYPE_FCC | 3},  \
1967     {"$fcc4",   RTYPE_FCC | 4},  \
1968     {"$fcc5",   RTYPE_FCC | 5},  \
1969     {"$fcc6",   RTYPE_FCC | 6},  \
1970     {"$fcc7",   RTYPE_FCC | 7}
1971
1972 #define COPROC_CONDITION_CODE_NAMES         \
1973     {"$cc0",    RTYPE_FCC | RTYPE_CCC | 0}, \
1974     {"$cc1",    RTYPE_FCC | RTYPE_CCC | 1}, \
1975     {"$cc2",    RTYPE_FCC | RTYPE_CCC | 2}, \
1976     {"$cc3",    RTYPE_FCC | RTYPE_CCC | 3}, \
1977     {"$cc4",    RTYPE_FCC | RTYPE_CCC | 4}, \
1978     {"$cc5",    RTYPE_FCC | RTYPE_CCC | 5}, \
1979     {"$cc6",    RTYPE_FCC | RTYPE_CCC | 6}, \
1980     {"$cc7",    RTYPE_FCC | RTYPE_CCC | 7}
1981
1982 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1983     {"$a4",     RTYPE_GP | 8},  \
1984     {"$a5",     RTYPE_GP | 9},  \
1985     {"$a6",     RTYPE_GP | 10}, \
1986     {"$a7",     RTYPE_GP | 11}, \
1987     {"$ta0",    RTYPE_GP | 8},  /* alias for $a4 */ \
1988     {"$ta1",    RTYPE_GP | 9},  /* alias for $a5 */ \
1989     {"$ta2",    RTYPE_GP | 10}, /* alias for $a6 */ \
1990     {"$ta3",    RTYPE_GP | 11}, /* alias for $a7 */ \
1991     {"$t0",     RTYPE_GP | 12}, \
1992     {"$t1",     RTYPE_GP | 13}, \
1993     {"$t2",     RTYPE_GP | 14}, \
1994     {"$t3",     RTYPE_GP | 15}
1995
1996 #define O32_SYMBOLIC_REGISTER_NAMES \
1997     {"$t0",     RTYPE_GP | 8},  \
1998     {"$t1",     RTYPE_GP | 9},  \
1999     {"$t2",     RTYPE_GP | 10}, \
2000     {"$t3",     RTYPE_GP | 11}, \
2001     {"$t4",     RTYPE_GP | 12}, \
2002     {"$t5",     RTYPE_GP | 13}, \
2003     {"$t6",     RTYPE_GP | 14}, \
2004     {"$t7",     RTYPE_GP | 15}, \
2005     {"$ta0",    RTYPE_GP | 12}, /* alias for $t4 */ \
2006     {"$ta1",    RTYPE_GP | 13}, /* alias for $t5 */ \
2007     {"$ta2",    RTYPE_GP | 14}, /* alias for $t6 */ \
2008     {"$ta3",    RTYPE_GP | 15}  /* alias for $t7 */ 
2009
2010 /* Remaining symbolic register names */
2011 #define SYMBOLIC_REGISTER_NAMES \
2012     {"$zero",   RTYPE_GP | 0},  \
2013     {"$at",     RTYPE_GP | 1},  \
2014     {"$AT",     RTYPE_GP | 1},  \
2015     {"$v0",     RTYPE_GP | 2},  \
2016     {"$v1",     RTYPE_GP | 3},  \
2017     {"$a0",     RTYPE_GP | 4},  \
2018     {"$a1",     RTYPE_GP | 5},  \
2019     {"$a2",     RTYPE_GP | 6},  \
2020     {"$a3",     RTYPE_GP | 7},  \
2021     {"$s0",     RTYPE_GP | 16}, \
2022     {"$s1",     RTYPE_GP | 17}, \
2023     {"$s2",     RTYPE_GP | 18}, \
2024     {"$s3",     RTYPE_GP | 19}, \
2025     {"$s4",     RTYPE_GP | 20}, \
2026     {"$s5",     RTYPE_GP | 21}, \
2027     {"$s6",     RTYPE_GP | 22}, \
2028     {"$s7",     RTYPE_GP | 23}, \
2029     {"$t8",     RTYPE_GP | 24}, \
2030     {"$t9",     RTYPE_GP | 25}, \
2031     {"$k0",     RTYPE_GP | 26}, \
2032     {"$kt0",    RTYPE_GP | 26}, \
2033     {"$k1",     RTYPE_GP | 27}, \
2034     {"$kt1",    RTYPE_GP | 27}, \
2035     {"$gp",     RTYPE_GP | 28}, \
2036     {"$sp",     RTYPE_GP | 29}, \
2037     {"$s8",     RTYPE_GP | 30}, \
2038     {"$fp",     RTYPE_GP | 30}, \
2039     {"$ra",     RTYPE_GP | 31}
2040
2041 #define MIPS16_SPECIAL_REGISTER_NAMES \
2042     {"$pc",     RTYPE_PC | 0}
2043
2044 #define MDMX_VECTOR_REGISTER_NAMES \
2045     /* {"$v0",  RTYPE_VEC | 0},  clash with REG 2 above */ \
2046     /* {"$v1",  RTYPE_VEC | 1},  clash with REG 3 above */ \
2047     {"$v2",     RTYPE_VEC | 2},  \
2048     {"$v3",     RTYPE_VEC | 3},  \
2049     {"$v4",     RTYPE_VEC | 4},  \
2050     {"$v5",     RTYPE_VEC | 5},  \
2051     {"$v6",     RTYPE_VEC | 6},  \
2052     {"$v7",     RTYPE_VEC | 7},  \
2053     {"$v8",     RTYPE_VEC | 8},  \
2054     {"$v9",     RTYPE_VEC | 9},  \
2055     {"$v10",    RTYPE_VEC | 10}, \
2056     {"$v11",    RTYPE_VEC | 11}, \
2057     {"$v12",    RTYPE_VEC | 12}, \
2058     {"$v13",    RTYPE_VEC | 13}, \
2059     {"$v14",    RTYPE_VEC | 14}, \
2060     {"$v15",    RTYPE_VEC | 15}, \
2061     {"$v16",    RTYPE_VEC | 16}, \
2062     {"$v17",    RTYPE_VEC | 17}, \
2063     {"$v18",    RTYPE_VEC | 18}, \
2064     {"$v19",    RTYPE_VEC | 19}, \
2065     {"$v20",    RTYPE_VEC | 20}, \
2066     {"$v21",    RTYPE_VEC | 21}, \
2067     {"$v22",    RTYPE_VEC | 22}, \
2068     {"$v23",    RTYPE_VEC | 23}, \
2069     {"$v24",    RTYPE_VEC | 24}, \
2070     {"$v25",    RTYPE_VEC | 25}, \
2071     {"$v26",    RTYPE_VEC | 26}, \
2072     {"$v27",    RTYPE_VEC | 27}, \
2073     {"$v28",    RTYPE_VEC | 28}, \
2074     {"$v29",    RTYPE_VEC | 29}, \
2075     {"$v30",    RTYPE_VEC | 30}, \
2076     {"$v31",    RTYPE_VEC | 31}
2077
2078 #define MIPS_DSP_ACCUMULATOR_NAMES \
2079     {"$ac0",    RTYPE_ACC | 0}, \
2080     {"$ac1",    RTYPE_ACC | 1}, \
2081     {"$ac2",    RTYPE_ACC | 2}, \
2082     {"$ac3",    RTYPE_ACC | 3}
2083
2084 static const struct regname reg_names[] = {
2085   GENERIC_REGISTER_NUMBERS,
2086   FPU_REGISTER_NAMES,
2087   FPU_CONDITION_CODE_NAMES,
2088   COPROC_CONDITION_CODE_NAMES,
2089
2090   /* The $txx registers depends on the abi,
2091      these will be added later into the symbol table from
2092      one of the tables below once mips_abi is set after 
2093      parsing of arguments from the command line. */
2094   SYMBOLIC_REGISTER_NAMES,
2095
2096   MIPS16_SPECIAL_REGISTER_NAMES,
2097   MDMX_VECTOR_REGISTER_NAMES,
2098   MIPS_DSP_ACCUMULATOR_NAMES,
2099   {0, 0}
2100 };
2101
2102 static const struct regname reg_names_o32[] = {
2103   O32_SYMBOLIC_REGISTER_NAMES,
2104   {0, 0}
2105 };
2106
2107 static const struct regname reg_names_n32n64[] = {
2108   N32N64_SYMBOLIC_REGISTER_NAMES,
2109   {0, 0}
2110 };
2111
2112 /* Check if S points at a valid register specifier according to TYPES.
2113    If so, then return 1, advance S to consume the specifier and store
2114    the register's number in REGNOP, otherwise return 0.  */
2115
2116 static int
2117 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2118 {
2119   symbolS *symbolP;
2120   char *e;
2121   char save_c;
2122   int reg = -1;
2123
2124   /* Find end of name.  */
2125   e = *s;
2126   if (is_name_beginner (*e))
2127     ++e;
2128   while (is_part_of_name (*e))
2129     ++e;
2130
2131   /* Terminate name.  */
2132   save_c = *e;
2133   *e = '\0';
2134
2135   /* Look for a register symbol.  */
2136   if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2137     {
2138       int r = S_GET_VALUE (symbolP);
2139       if (r & types)
2140         reg = r & RNUM_MASK;
2141       else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2142         /* Convert GP reg $v0/1 to MDMX reg $v0/1!  */
2143         reg = (r & RNUM_MASK) - 2;
2144     }
2145   /* Else see if this is a register defined in an itbl entry.  */
2146   else if ((types & RTYPE_GP) && itbl_have_entries)
2147     {
2148       char *n = *s;
2149       unsigned long r;
2150
2151       if (*n == '$')
2152         ++n;
2153       if (itbl_get_reg_val (n, &r))
2154         reg = r & RNUM_MASK;
2155     }
2156
2157   /* Advance to next token if a register was recognised.  */
2158   if (reg >= 0)
2159     *s = e;
2160   else if (types & RWARN)
2161     as_warn (_("Unrecognized register name `%s'"), *s);
2162
2163   *e = save_c;
2164   if (regnop)
2165     *regnop = reg;
2166   return reg >= 0;
2167 }
2168
2169 /* Check if S points at a valid register list according to TYPES.
2170    If so, then return 1, advance S to consume the list and store
2171    the registers present on the list as a bitmask of ones in REGLISTP,
2172    otherwise return 0.  A valid list comprises a comma-separated
2173    enumeration of valid single registers and/or dash-separated
2174    contiguous register ranges as determined by their numbers.
2175
2176    As a special exception if one of s0-s7 registers is specified as
2177    the range's lower delimiter and s8 (fp) is its upper one, then no
2178    registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2179    are selected; they have to be listed separately if needed.  */
2180
2181 static int
2182 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2183 {
2184   unsigned int reglist = 0;
2185   unsigned int lastregno;
2186   bfd_boolean ok = TRUE;
2187   unsigned int regmask;
2188   char *s_endlist = *s;
2189   char *s_reset = *s;
2190   unsigned int regno;
2191
2192   while (reg_lookup (s, types, &regno))
2193     {
2194       lastregno = regno;
2195       if (**s == '-')
2196         {
2197           (*s)++;
2198           ok = reg_lookup (s, types, &lastregno);
2199           if (ok && lastregno < regno)
2200             ok = FALSE;
2201           if (!ok)
2202             break;
2203         }
2204
2205       if (lastregno == FP && regno >= S0 && regno <= S7)
2206         {
2207           lastregno = S7;
2208           reglist |= 1 << FP;
2209         }
2210       regmask = 1 << lastregno;
2211       regmask = (regmask << 1) - 1;
2212       regmask ^= (1 << regno) - 1;
2213       reglist |= regmask;
2214
2215       s_endlist = *s;
2216       if (**s != ',')
2217         break;
2218       (*s)++;
2219     }
2220
2221   if (ok)
2222     *s = s_endlist;
2223   else
2224     *s = s_reset;
2225   if (reglistp)
2226     *reglistp = reglist;
2227   return ok && reglist != 0;
2228 }
2229
2230 /* Return TRUE if opcode MO is valid on the currently selected ISA and
2231    architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
2232
2233 static bfd_boolean
2234 is_opcode_valid (const struct mips_opcode *mo)
2235 {
2236   int isa = mips_opts.isa;
2237   int fp_s, fp_d;
2238
2239   if (mips_opts.ase_mdmx)
2240     isa |= INSN_MDMX;
2241   if (mips_opts.ase_dsp)
2242     isa |= INSN_DSP;
2243   if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2244     isa |= INSN_DSP64;
2245   if (mips_opts.ase_dspr2)
2246     isa |= INSN_DSPR2;
2247   if (mips_opts.ase_mt)
2248     isa |= INSN_MT;
2249   if (mips_opts.ase_mips3d)
2250     isa |= INSN_MIPS3D;
2251   if (mips_opts.ase_smartmips)
2252     isa |= INSN_SMARTMIPS;
2253   if (mips_opts.ase_mcu)
2254     isa |= INSN_MCU;
2255
2256   if (!opcode_is_member (mo, isa, mips_opts.arch))
2257     return FALSE;
2258
2259   /* Check whether the instruction or macro requires single-precision or
2260      double-precision floating-point support.  Note that this information is
2261      stored differently in the opcode table for insns and macros.  */
2262   if (mo->pinfo == INSN_MACRO)
2263     {
2264       fp_s = mo->pinfo2 & INSN2_M_FP_S;
2265       fp_d = mo->pinfo2 & INSN2_M_FP_D;
2266     }
2267   else
2268     {
2269       fp_s = mo->pinfo & FP_S;
2270       fp_d = mo->pinfo & FP_D;
2271     }
2272
2273   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2274     return FALSE;
2275
2276   if (fp_s && mips_opts.soft_float)
2277     return FALSE;
2278
2279   return TRUE;
2280 }
2281
2282 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2283    selected ISA and architecture.  */
2284
2285 static bfd_boolean
2286 is_opcode_valid_16 (const struct mips_opcode *mo)
2287 {
2288   return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
2289 }
2290
2291 /* Return TRUE if the size of the microMIPS opcode MO matches one
2292    explicitly requested.  Always TRUE in the standard MIPS mode.  */
2293
2294 static bfd_boolean
2295 is_size_valid (const struct mips_opcode *mo)
2296 {
2297   if (!mips_opts.micromips)
2298     return TRUE;
2299
2300   if (!forced_insn_length)
2301     return TRUE;
2302   if (mo->pinfo == INSN_MACRO)
2303     return FALSE;
2304   return forced_insn_length == micromips_insn_length (mo);
2305 }
2306
2307 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2308    of the preceding instruction.  Always TRUE in the standard MIPS mode.
2309
2310    We don't accept macros in 16-bit delay slots to avoid a case where
2311    a macro expansion fails because it relies on a preceding 32-bit real
2312    instruction to have matched and does not handle the operands correctly.
2313    The only macros that may expand to 16-bit instructions are JAL that
2314    cannot be placed in a delay slot anyway, and corner cases of BALIGN
2315    and BGT (that likewise cannot be placed in a delay slot) that decay to
2316    a NOP.  In all these cases the macros precede any corresponding real
2317    instruction definitions in the opcode table, so they will match in the
2318    second pass where the size of the delay slot is ignored and therefore
2319    produce correct code.  */
2320
2321 static bfd_boolean
2322 is_delay_slot_valid (const struct mips_opcode *mo)
2323 {
2324   if (!mips_opts.micromips)
2325     return TRUE;
2326
2327   if (mo->pinfo == INSN_MACRO)
2328     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
2329   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2330       && micromips_insn_length (mo) != 4)
2331     return FALSE;
2332   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2333       && micromips_insn_length (mo) != 2)
2334     return FALSE;
2335
2336   return TRUE;
2337 }
2338
2339 /* This function is called once, at assembler startup time.  It should set up
2340    all the tables, etc. that the MD part of the assembler will need.  */
2341
2342 void
2343 md_begin (void)
2344 {
2345   const char *retval = NULL;
2346   int i = 0;
2347   int broken = 0;
2348
2349   if (mips_pic != NO_PIC)
2350     {
2351       if (g_switch_seen && g_switch_value != 0)
2352         as_bad (_("-G may not be used in position-independent code"));
2353       g_switch_value = 0;
2354     }
2355
2356   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2357     as_warn (_("Could not set architecture and machine"));
2358
2359   op_hash = hash_new ();
2360
2361   for (i = 0; i < NUMOPCODES;)
2362     {
2363       const char *name = mips_opcodes[i].name;
2364
2365       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2366       if (retval != NULL)
2367         {
2368           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2369                    mips_opcodes[i].name, retval);
2370           /* Probably a memory allocation problem?  Give up now.  */
2371           as_fatal (_("Broken assembler.  No assembly attempted."));
2372         }
2373       do
2374         {
2375           if (mips_opcodes[i].pinfo != INSN_MACRO)
2376             {
2377               if (!validate_mips_insn (&mips_opcodes[i]))
2378                 broken = 1;
2379               if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2380                 {
2381                   create_insn (&nop_insn, mips_opcodes + i);
2382                   if (mips_fix_loongson2f_nop)
2383                     nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2384                   nop_insn.fixed_p = 1;
2385                 }
2386             }
2387           ++i;
2388         }
2389       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2390     }
2391
2392   mips16_op_hash = hash_new ();
2393
2394   i = 0;
2395   while (i < bfd_mips16_num_opcodes)
2396     {
2397       const char *name = mips16_opcodes[i].name;
2398
2399       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2400       if (retval != NULL)
2401         as_fatal (_("internal: can't hash `%s': %s"),
2402                   mips16_opcodes[i].name, retval);
2403       do
2404         {
2405           if (mips16_opcodes[i].pinfo != INSN_MACRO
2406               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2407                   != mips16_opcodes[i].match))
2408             {
2409               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2410                        mips16_opcodes[i].name, mips16_opcodes[i].args);
2411               broken = 1;
2412             }
2413           if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2414             {
2415               create_insn (&mips16_nop_insn, mips16_opcodes + i);
2416               mips16_nop_insn.fixed_p = 1;
2417             }
2418           ++i;
2419         }
2420       while (i < bfd_mips16_num_opcodes
2421              && strcmp (mips16_opcodes[i].name, name) == 0);
2422     }
2423
2424   micromips_op_hash = hash_new ();
2425
2426   i = 0;
2427   while (i < bfd_micromips_num_opcodes)
2428     {
2429       const char *name = micromips_opcodes[i].name;
2430
2431       retval = hash_insert (micromips_op_hash, name,
2432                             (void *) &micromips_opcodes[i]);
2433       if (retval != NULL)
2434         as_fatal (_("internal: can't hash `%s': %s"),
2435                   micromips_opcodes[i].name, retval);
2436       do
2437         if (micromips_opcodes[i].pinfo != INSN_MACRO)
2438           {
2439             struct mips_cl_insn *micromips_nop_insn;
2440
2441             if (!validate_micromips_insn (&micromips_opcodes[i]))
2442               broken = 1;
2443
2444             if (micromips_insn_length (micromips_opcodes + i) == 2)
2445               micromips_nop_insn = &micromips_nop16_insn;
2446             else if (micromips_insn_length (micromips_opcodes + i) == 4)
2447               micromips_nop_insn = &micromips_nop32_insn;
2448             else
2449               continue;
2450
2451             if (micromips_nop_insn->insn_mo == NULL
2452                 && strcmp (name, "nop") == 0)
2453               {
2454                 create_insn (micromips_nop_insn, micromips_opcodes + i);
2455                 micromips_nop_insn->fixed_p = 1;
2456               }
2457           }
2458       while (++i < bfd_micromips_num_opcodes
2459              && strcmp (micromips_opcodes[i].name, name) == 0);
2460     }
2461
2462   if (broken)
2463     as_fatal (_("Broken assembler.  No assembly attempted."));
2464
2465   /* We add all the general register names to the symbol table.  This
2466      helps us detect invalid uses of them.  */
2467   for (i = 0; reg_names[i].name; i++) 
2468     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2469                                      reg_names[i].num, /* & RNUM_MASK, */
2470                                      &zero_address_frag));
2471   if (HAVE_NEWABI)
2472     for (i = 0; reg_names_n32n64[i].name; i++) 
2473       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2474                                        reg_names_n32n64[i].num, /* & RNUM_MASK, */
2475                                        &zero_address_frag));
2476   else
2477     for (i = 0; reg_names_o32[i].name; i++) 
2478       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2479                                        reg_names_o32[i].num, /* & RNUM_MASK, */
2480                                        &zero_address_frag));
2481
2482   mips_no_prev_insn ();
2483
2484   mips_gprmask = 0;
2485   mips_cprmask[0] = 0;
2486   mips_cprmask[1] = 0;
2487   mips_cprmask[2] = 0;
2488   mips_cprmask[3] = 0;
2489
2490   /* set the default alignment for the text section (2**2) */
2491   record_alignment (text_section, 2);
2492
2493   bfd_set_gp_size (stdoutput, g_switch_value);
2494
2495 #ifdef OBJ_ELF
2496   if (IS_ELF)
2497     {
2498       /* On a native system other than VxWorks, sections must be aligned
2499          to 16 byte boundaries.  When configured for an embedded ELF
2500          target, we don't bother.  */
2501       if (strncmp (TARGET_OS, "elf", 3) != 0
2502           && strncmp (TARGET_OS, "vxworks", 7) != 0)
2503         {
2504           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2505           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2506           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2507         }
2508
2509       /* Create a .reginfo section for register masks and a .mdebug
2510          section for debugging information.  */
2511       {
2512         segT seg;
2513         subsegT subseg;
2514         flagword flags;
2515         segT sec;
2516
2517         seg = now_seg;
2518         subseg = now_subseg;
2519
2520         /* The ABI says this section should be loaded so that the
2521            running program can access it.  However, we don't load it
2522            if we are configured for an embedded target */
2523         flags = SEC_READONLY | SEC_DATA;
2524         if (strncmp (TARGET_OS, "elf", 3) != 0)
2525           flags |= SEC_ALLOC | SEC_LOAD;
2526
2527         if (mips_abi != N64_ABI)
2528           {
2529             sec = subseg_new (".reginfo", (subsegT) 0);
2530
2531             bfd_set_section_flags (stdoutput, sec, flags);
2532             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2533
2534             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2535           }
2536         else
2537           {
2538             /* The 64-bit ABI uses a .MIPS.options section rather than
2539                .reginfo section.  */
2540             sec = subseg_new (".MIPS.options", (subsegT) 0);
2541             bfd_set_section_flags (stdoutput, sec, flags);
2542             bfd_set_section_alignment (stdoutput, sec, 3);
2543
2544             /* Set up the option header.  */
2545             {
2546               Elf_Internal_Options opthdr;
2547               char *f;
2548
2549               opthdr.kind = ODK_REGINFO;
2550               opthdr.size = (sizeof (Elf_External_Options)
2551                              + sizeof (Elf64_External_RegInfo));
2552               opthdr.section = 0;
2553               opthdr.info = 0;
2554               f = frag_more (sizeof (Elf_External_Options));
2555               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2556                                              (Elf_External_Options *) f);
2557
2558               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2559             }
2560           }
2561
2562         if (ECOFF_DEBUGGING)
2563           {
2564             sec = subseg_new (".mdebug", (subsegT) 0);
2565             (void) bfd_set_section_flags (stdoutput, sec,
2566                                           SEC_HAS_CONTENTS | SEC_READONLY);
2567             (void) bfd_set_section_alignment (stdoutput, sec, 2);
2568           }
2569         else if (mips_flag_pdr)
2570           {
2571             pdr_seg = subseg_new (".pdr", (subsegT) 0);
2572             (void) bfd_set_section_flags (stdoutput, pdr_seg,
2573                                           SEC_READONLY | SEC_RELOC
2574                                           | SEC_DEBUGGING);
2575             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2576           }
2577
2578         subseg_set (seg, subseg);
2579       }
2580     }
2581 #endif /* OBJ_ELF */
2582
2583   if (! ECOFF_DEBUGGING)
2584     md_obj_begin ();
2585
2586   if (mips_fix_vr4120)
2587     init_vr4120_conflicts ();
2588 }
2589
2590 void
2591 md_mips_end (void)
2592 {
2593   mips_emit_delays ();
2594   if (! ECOFF_DEBUGGING)
2595     md_obj_end ();
2596 }
2597
2598 void
2599 md_assemble (char *str)
2600 {
2601   struct mips_cl_insn insn;
2602   bfd_reloc_code_real_type unused_reloc[3]
2603     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2604
2605   imm_expr.X_op = O_absent;
2606   imm2_expr.X_op = O_absent;
2607   offset_expr.X_op = O_absent;
2608   imm_reloc[0] = BFD_RELOC_UNUSED;
2609   imm_reloc[1] = BFD_RELOC_UNUSED;
2610   imm_reloc[2] = BFD_RELOC_UNUSED;
2611   offset_reloc[0] = BFD_RELOC_UNUSED;
2612   offset_reloc[1] = BFD_RELOC_UNUSED;
2613   offset_reloc[2] = BFD_RELOC_UNUSED;
2614
2615   mips_mark_labels ();
2616   mips_assembling_insn = TRUE;
2617
2618   if (mips_opts.mips16)
2619     mips16_ip (str, &insn);
2620   else
2621     {
2622       mips_ip (str, &insn);
2623       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2624             str, insn.insn_opcode));
2625     }
2626
2627   if (insn_error)
2628     as_bad ("%s `%s'", insn_error, str);
2629   else if (insn.insn_mo->pinfo == INSN_MACRO)
2630     {
2631       macro_start ();
2632       if (mips_opts.mips16)
2633         mips16_macro (&insn);
2634       else
2635         macro (&insn);
2636       macro_end ();
2637     }
2638   else
2639     {
2640       if (imm_expr.X_op != O_absent)
2641         append_insn (&insn, &imm_expr, imm_reloc, FALSE);
2642       else if (offset_expr.X_op != O_absent)
2643         append_insn (&insn, &offset_expr, offset_reloc, FALSE);
2644       else
2645         append_insn (&insn, NULL, unused_reloc, FALSE);
2646     }
2647
2648   mips_assembling_insn = FALSE;
2649 }
2650
2651 /* Convenience functions for abstracting away the differences between
2652    MIPS16 and non-MIPS16 relocations.  */
2653
2654 static inline bfd_boolean
2655 mips16_reloc_p (bfd_reloc_code_real_type reloc)
2656 {
2657   switch (reloc)
2658     {
2659     case BFD_RELOC_MIPS16_JMP:
2660     case BFD_RELOC_MIPS16_GPREL:
2661     case BFD_RELOC_MIPS16_GOT16:
2662     case BFD_RELOC_MIPS16_CALL16:
2663     case BFD_RELOC_MIPS16_HI16_S:
2664     case BFD_RELOC_MIPS16_HI16:
2665     case BFD_RELOC_MIPS16_LO16:
2666       return TRUE;
2667
2668     default:
2669       return FALSE;
2670     }
2671 }
2672
2673 static inline bfd_boolean
2674 micromips_reloc_p (bfd_reloc_code_real_type reloc)
2675 {
2676   switch (reloc)
2677     {
2678     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2679     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2680     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2681     case BFD_RELOC_MICROMIPS_GPREL16:
2682     case BFD_RELOC_MICROMIPS_JMP:
2683     case BFD_RELOC_MICROMIPS_HI16:
2684     case BFD_RELOC_MICROMIPS_HI16_S:
2685     case BFD_RELOC_MICROMIPS_LO16:
2686     case BFD_RELOC_MICROMIPS_LITERAL:
2687     case BFD_RELOC_MICROMIPS_GOT16:
2688     case BFD_RELOC_MICROMIPS_CALL16:
2689     case BFD_RELOC_MICROMIPS_GOT_HI16:
2690     case BFD_RELOC_MICROMIPS_GOT_LO16:
2691     case BFD_RELOC_MICROMIPS_CALL_HI16:
2692     case BFD_RELOC_MICROMIPS_CALL_LO16:
2693     case BFD_RELOC_MICROMIPS_SUB:
2694     case BFD_RELOC_MICROMIPS_GOT_PAGE:
2695     case BFD_RELOC_MICROMIPS_GOT_OFST:
2696     case BFD_RELOC_MICROMIPS_GOT_DISP:
2697     case BFD_RELOC_MICROMIPS_HIGHEST:
2698     case BFD_RELOC_MICROMIPS_HIGHER:
2699     case BFD_RELOC_MICROMIPS_SCN_DISP:
2700     case BFD_RELOC_MICROMIPS_JALR:
2701       return TRUE;
2702
2703     default:
2704       return FALSE;
2705     }
2706 }
2707
2708 static inline bfd_boolean
2709 jmp_reloc_p (bfd_reloc_code_real_type reloc)
2710 {
2711   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2712 }
2713
2714 static inline bfd_boolean
2715 got16_reloc_p (bfd_reloc_code_real_type reloc)
2716 {
2717   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
2718           || reloc == BFD_RELOC_MICROMIPS_GOT16);
2719 }
2720
2721 static inline bfd_boolean
2722 hi16_reloc_p (bfd_reloc_code_real_type reloc)
2723 {
2724   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
2725           || reloc == BFD_RELOC_MICROMIPS_HI16_S);
2726 }
2727
2728 static inline bfd_boolean
2729 lo16_reloc_p (bfd_reloc_code_real_type reloc)
2730 {
2731   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
2732           || reloc == BFD_RELOC_MICROMIPS_LO16);
2733 }
2734
2735 static inline bfd_boolean
2736 jalr_reloc_p (bfd_reloc_code_real_type reloc)
2737 {
2738   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
2739 }
2740
2741 /* Return true if the given relocation might need a matching %lo().
2742    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2743    need a matching %lo() when applied to local symbols.  */
2744
2745 static inline bfd_boolean
2746 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2747 {
2748   return (HAVE_IN_PLACE_ADDENDS
2749           && (hi16_reloc_p (reloc)
2750               /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2751                  all GOT16 relocations evaluate to "G".  */
2752               || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2753 }
2754
2755 /* Return the type of %lo() reloc needed by RELOC, given that
2756    reloc_needs_lo_p.  */
2757
2758 static inline bfd_reloc_code_real_type
2759 matching_lo_reloc (bfd_reloc_code_real_type reloc)
2760 {
2761   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2762           : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2763              : BFD_RELOC_LO16));
2764 }
2765
2766 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2767    relocation.  */
2768
2769 static inline bfd_boolean
2770 fixup_has_matching_lo_p (fixS *fixp)
2771 {
2772   return (fixp->fx_next != NULL
2773           && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
2774           && fixp->fx_addsy == fixp->fx_next->fx_addsy
2775           && fixp->fx_offset == fixp->fx_next->fx_offset);
2776 }
2777
2778 /* This function returns true if modifying a register requires a
2779    delay.  */
2780
2781 static int
2782 reg_needs_delay (unsigned int reg)
2783 {
2784   unsigned long prev_pinfo;
2785
2786   prev_pinfo = history[0].insn_mo->pinfo;
2787   if (! mips_opts.noreorder
2788       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2789            && ! gpr_interlocks)
2790           || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2791               && ! cop_interlocks)))
2792     {
2793       /* A load from a coprocessor or from memory.  All load delays
2794          delay the use of general register rt for one instruction.  */
2795       /* Itbl support may require additional care here.  */
2796       know (prev_pinfo & INSN_WRITE_GPR_T);
2797       if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
2798         return 1;
2799     }
2800
2801   return 0;
2802 }
2803
2804 /* Move all labels in LABELS to the current insertion point.  TEXT_P
2805    says whether the labels refer to text or data.  */
2806
2807 static void
2808 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
2809 {
2810   struct insn_label_list *l;
2811   valueT val;
2812
2813   for (l = labels; l != NULL; l = l->next)
2814     {
2815       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
2816       symbol_set_frag (l->label, frag_now);
2817       val = (valueT) frag_now_fix ();
2818       /* MIPS16/microMIPS text labels are stored as odd.  */
2819       if (text_p && HAVE_CODE_COMPRESSION)
2820         ++val;
2821       S_SET_VALUE (l->label, val);
2822     }
2823 }
2824
2825 /* Move all labels in insn_labels to the current insertion point
2826    and treat them as text labels.  */
2827
2828 static void
2829 mips_move_text_labels (void)
2830 {
2831   mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2832 }
2833
2834 static bfd_boolean
2835 s_is_linkonce (symbolS *sym, segT from_seg)
2836 {
2837   bfd_boolean linkonce = FALSE;
2838   segT symseg = S_GET_SEGMENT (sym);
2839
2840   if (symseg != from_seg && !S_IS_LOCAL (sym))
2841     {
2842       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2843         linkonce = TRUE;
2844 #ifdef OBJ_ELF
2845       /* The GNU toolchain uses an extension for ELF: a section
2846          beginning with the magic string .gnu.linkonce is a
2847          linkonce section.  */
2848       if (strncmp (segment_name (symseg), ".gnu.linkonce",
2849                    sizeof ".gnu.linkonce" - 1) == 0)
2850         linkonce = TRUE;
2851 #endif
2852     }
2853   return linkonce;
2854 }
2855
2856 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
2857    linker to handle them specially, such as generating jalx instructions
2858    when needed.  We also make them odd for the duration of the assembly,
2859    in order to generate the right sort of code.  We will make them even
2860    in the adjust_symtab routine, while leaving them marked.  This is
2861    convenient for the debugger and the disassembler.  The linker knows
2862    to make them odd again.  */
2863
2864 static void
2865 mips_compressed_mark_label (symbolS *label)
2866 {
2867   gas_assert (HAVE_CODE_COMPRESSION);
2868
2869 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2870   if (IS_ELF)
2871     {
2872       if (mips_opts.mips16)
2873         S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2874       else
2875         S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
2876     }
2877 #endif
2878   if ((S_GET_VALUE (label) & 1) == 0
2879       /* Don't adjust the address if the label is global or weak, or
2880          in a link-once section, since we'll be emitting symbol reloc
2881          references to it which will be patched up by the linker, and
2882          the final value of the symbol may or may not be MIPS16/microMIPS.  */
2883       && !S_IS_WEAK (label)
2884       && !S_IS_EXTERNAL (label)
2885       && !s_is_linkonce (label, now_seg))
2886     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2887 }
2888
2889 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
2890
2891 static void
2892 mips_compressed_mark_labels (void)
2893 {
2894   struct insn_label_list *l;
2895
2896   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2897     mips_compressed_mark_label (l->label);
2898 }
2899
2900 /* End the current frag.  Make it a variant frag and record the
2901    relaxation info.  */
2902
2903 static void
2904 relax_close_frag (void)
2905 {
2906   mips_macro_warning.first_frag = frag_now;
2907   frag_var (rs_machine_dependent, 0, 0,
2908             RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2909             mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2910
2911   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2912   mips_relax.first_fixup = 0;
2913 }
2914
2915 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2916    See the comment above RELAX_ENCODE for more details.  */
2917
2918 static void
2919 relax_start (symbolS *symbol)
2920 {
2921   gas_assert (mips_relax.sequence == 0);
2922   mips_relax.sequence = 1;
2923   mips_relax.symbol = symbol;
2924 }
2925
2926 /* Start generating the second version of a relaxable sequence.
2927    See the comment above RELAX_ENCODE for more details.  */
2928
2929 static void
2930 relax_switch (void)
2931 {
2932   gas_assert (mips_relax.sequence == 1);
2933   mips_relax.sequence = 2;
2934 }
2935
2936 /* End the current relaxable sequence.  */
2937
2938 static void
2939 relax_end (void)
2940 {
2941   gas_assert (mips_relax.sequence == 2);
2942   relax_close_frag ();
2943   mips_relax.sequence = 0;
2944 }
2945
2946 /* Return true if IP is a delayed branch or jump.  */
2947
2948 static inline bfd_boolean
2949 delayed_branch_p (const struct mips_cl_insn *ip)
2950 {
2951   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2952                                 | INSN_COND_BRANCH_DELAY
2953                                 | INSN_COND_BRANCH_LIKELY)) != 0;
2954 }
2955
2956 /* Return true if IP is a compact branch or jump.  */
2957
2958 static inline bfd_boolean
2959 compact_branch_p (const struct mips_cl_insn *ip)
2960 {
2961   if (mips_opts.mips16)
2962     return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2963                                   | MIPS16_INSN_COND_BRANCH)) != 0;
2964   else
2965     return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2966                                    | INSN2_COND_BRANCH)) != 0;
2967 }
2968
2969 /* Return true if IP is an unconditional branch or jump.  */
2970
2971 static inline bfd_boolean
2972 uncond_branch_p (const struct mips_cl_insn *ip)
2973 {
2974   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2975           || (mips_opts.mips16
2976               ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2977               : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2978 }
2979
2980 /* Return true if IP is a branch-likely instruction.  */
2981
2982 static inline bfd_boolean
2983 branch_likely_p (const struct mips_cl_insn *ip)
2984 {
2985   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2986 }
2987
2988 /* Return the type of nop that should be used to fill the delay slot
2989    of delayed branch IP.  */
2990
2991 static struct mips_cl_insn *
2992 get_delay_slot_nop (const struct mips_cl_insn *ip)
2993 {
2994   if (mips_opts.micromips
2995       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
2996     return &micromips_nop32_insn;
2997   return NOP_INSN;
2998 }
2999
3000 /* Return the mask of core registers that IP reads or writes.  */
3001
3002 static unsigned int
3003 gpr_mod_mask (const struct mips_cl_insn *ip)
3004 {
3005   unsigned long pinfo2;
3006   unsigned int mask;
3007
3008   mask = 0;
3009   pinfo2 = ip->insn_mo->pinfo2;
3010   if (mips_opts.micromips)
3011     {
3012       if (pinfo2 & INSN2_MOD_GPR_MD)
3013         mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
3014       if (pinfo2 & INSN2_MOD_GPR_MF)
3015         mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
3016       if (pinfo2 & INSN2_MOD_SP)
3017         mask |= 1 << SP;
3018     }
3019   return mask;
3020 }
3021
3022 /* Return the mask of core registers that IP reads.  */
3023
3024 static unsigned int
3025 gpr_read_mask (const struct mips_cl_insn *ip)
3026 {
3027   unsigned long pinfo, pinfo2;
3028   unsigned int mask;
3029
3030   mask = gpr_mod_mask (ip);
3031   pinfo = ip->insn_mo->pinfo;
3032   pinfo2 = ip->insn_mo->pinfo2;
3033   if (mips_opts.mips16)
3034     {
3035       if (pinfo & MIPS16_INSN_READ_X)
3036         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3037       if (pinfo & MIPS16_INSN_READ_Y)
3038         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3039       if (pinfo & MIPS16_INSN_READ_T)
3040         mask |= 1 << TREG;
3041       if (pinfo & MIPS16_INSN_READ_SP)
3042         mask |= 1 << SP;
3043       if (pinfo & MIPS16_INSN_READ_31)
3044         mask |= 1 << RA;
3045       if (pinfo & MIPS16_INSN_READ_Z)
3046         mask |= 1 << (mips16_to_32_reg_map
3047                       [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3048       if (pinfo & MIPS16_INSN_READ_GPR_X)
3049         mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3050     }
3051   else
3052     {
3053       if (pinfo2 & INSN2_READ_GPR_D)
3054         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3055       if (pinfo & INSN_READ_GPR_T)
3056         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3057       if (pinfo & INSN_READ_GPR_S)
3058         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3059       if (pinfo2 & INSN2_READ_GP)
3060         mask |= 1 << GP;
3061       if (pinfo2 & INSN2_READ_GPR_31)
3062         mask |= 1 << RA;
3063       if (pinfo2 & INSN2_READ_GPR_Z)
3064         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3065     }
3066   if (mips_opts.micromips)
3067     {
3068       if (pinfo2 & INSN2_READ_GPR_MC)
3069         mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3070       if (pinfo2 & INSN2_READ_GPR_ME)
3071         mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3072       if (pinfo2 & INSN2_READ_GPR_MG)
3073         mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3074       if (pinfo2 & INSN2_READ_GPR_MJ)
3075         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3076       if (pinfo2 & INSN2_READ_GPR_MMN)
3077         {
3078           mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3079           mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3080         }
3081       if (pinfo2 & INSN2_READ_GPR_MP)
3082         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3083       if (pinfo2 & INSN2_READ_GPR_MQ)
3084         mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3085     }
3086   /* Don't include register 0.  */
3087   return mask & ~1;
3088 }
3089
3090 /* Return the mask of core registers that IP writes.  */
3091
3092 static unsigned int
3093 gpr_write_mask (const struct mips_cl_insn *ip)
3094 {
3095   unsigned long pinfo, pinfo2;
3096   unsigned int mask;
3097
3098   mask = gpr_mod_mask (ip);
3099   pinfo = ip->insn_mo->pinfo;
3100   pinfo2 = ip->insn_mo->pinfo2;
3101   if (mips_opts.mips16)
3102     {
3103       if (pinfo & MIPS16_INSN_WRITE_X)
3104         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3105       if (pinfo & MIPS16_INSN_WRITE_Y)
3106         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3107       if (pinfo & MIPS16_INSN_WRITE_Z)
3108         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3109       if (pinfo & MIPS16_INSN_WRITE_T)
3110         mask |= 1 << TREG;
3111       if (pinfo & MIPS16_INSN_WRITE_SP)
3112         mask |= 1 << SP;
3113       if (pinfo & MIPS16_INSN_WRITE_31)
3114         mask |= 1 << RA;
3115       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3116         mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3117     }
3118   else
3119     {
3120       if (pinfo & INSN_WRITE_GPR_D)
3121         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3122       if (pinfo & INSN_WRITE_GPR_T)
3123         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3124       if (pinfo & INSN_WRITE_GPR_S)
3125         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3126       if (pinfo & INSN_WRITE_GPR_31)
3127         mask |= 1 << RA;
3128       if (pinfo2 & INSN2_WRITE_GPR_Z)
3129         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3130     }
3131   if (mips_opts.micromips)
3132     {
3133       if (pinfo2 & INSN2_WRITE_GPR_MB)
3134         mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3135       if (pinfo2 & INSN2_WRITE_GPR_MHI)
3136         {
3137           mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3138           mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3139         }
3140       if (pinfo2 & INSN2_WRITE_GPR_MJ)
3141         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3142       if (pinfo2 & INSN2_WRITE_GPR_MP)
3143         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3144     }
3145   /* Don't include register 0.  */
3146   return mask & ~1;
3147 }
3148
3149 /* Return the mask of floating-point registers that IP reads.  */
3150
3151 static unsigned int
3152 fpr_read_mask (const struct mips_cl_insn *ip)
3153 {
3154   unsigned long pinfo, pinfo2;
3155   unsigned int mask;
3156
3157   mask = 0;
3158   pinfo = ip->insn_mo->pinfo;
3159   pinfo2 = ip->insn_mo->pinfo2;
3160   if (!mips_opts.mips16)
3161     {
3162       if (pinfo2 & INSN2_READ_FPR_D)
3163         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3164       if (pinfo & INSN_READ_FPR_S)
3165         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3166       if (pinfo & INSN_READ_FPR_T)
3167         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3168       if (pinfo & INSN_READ_FPR_R)
3169         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3170       if (pinfo2 & INSN2_READ_FPR_Z)
3171         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3172     }
3173   /* Conservatively treat all operands to an FP_D instruction are doubles.
3174      (This is overly pessimistic for things like cvt.d.s.)  */
3175   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3176     mask |= mask << 1;
3177   return mask;
3178 }
3179
3180 /* Return the mask of floating-point registers that IP writes.  */
3181
3182 static unsigned int
3183 fpr_write_mask (const struct mips_cl_insn *ip)
3184 {
3185   unsigned long pinfo, pinfo2;
3186   unsigned int mask;
3187
3188   mask = 0;
3189   pinfo = ip->insn_mo->pinfo;
3190   pinfo2 = ip->insn_mo->pinfo2;
3191   if (!mips_opts.mips16)
3192     {
3193       if (pinfo & INSN_WRITE_FPR_D)
3194         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3195       if (pinfo & INSN_WRITE_FPR_S)
3196         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3197       if (pinfo & INSN_WRITE_FPR_T)
3198         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3199       if (pinfo2 & INSN2_WRITE_FPR_Z)
3200         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3201     }
3202   /* Conservatively treat all operands to an FP_D instruction are doubles.
3203      (This is overly pessimistic for things like cvt.s.d.)  */
3204   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3205     mask |= mask << 1;
3206   return mask;
3207 }
3208
3209 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3210    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3211    by VR4120 errata.  */
3212
3213 static unsigned int
3214 classify_vr4120_insn (const char *name)
3215 {
3216   if (strncmp (name, "macc", 4) == 0)
3217     return FIX_VR4120_MACC;
3218   if (strncmp (name, "dmacc", 5) == 0)
3219     return FIX_VR4120_DMACC;
3220   if (strncmp (name, "mult", 4) == 0)
3221     return FIX_VR4120_MULT;
3222   if (strncmp (name, "dmult", 5) == 0)
3223     return FIX_VR4120_DMULT;
3224   if (strstr (name, "div"))
3225     return FIX_VR4120_DIV;
3226   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3227     return FIX_VR4120_MTHILO;
3228   return NUM_FIX_VR4120_CLASSES;
3229 }
3230
3231 #define INSN_ERET  0x42000018
3232 #define INSN_DERET 0x4200001f
3233
3234 /* Return the number of instructions that must separate INSN1 and INSN2,
3235    where INSN1 is the earlier instruction.  Return the worst-case value
3236    for any INSN2 if INSN2 is null.  */
3237
3238 static unsigned int
3239 insns_between (const struct mips_cl_insn *insn1,
3240                const struct mips_cl_insn *insn2)
3241 {
3242   unsigned long pinfo1, pinfo2;
3243   unsigned int mask;
3244
3245   /* This function needs to know which pinfo flags are set for INSN2
3246      and which registers INSN2 uses.  The former is stored in PINFO2 and
3247      the latter is tested via INSN2_USES_GPR.  If INSN2 is null, PINFO2
3248      will have every flag set and INSN2_USES_GPR will always return true.  */
3249   pinfo1 = insn1->insn_mo->pinfo;
3250   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3251
3252 #define INSN2_USES_GPR(REG) \
3253   (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3254
3255   /* For most targets, write-after-read dependencies on the HI and LO
3256      registers must be separated by at least two instructions.  */
3257   if (!hilo_interlocks)
3258     {
3259       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3260         return 2;
3261       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3262         return 2;
3263     }
3264
3265   /* If we're working around r7000 errata, there must be two instructions
3266      between an mfhi or mflo and any instruction that uses the result.  */
3267   if (mips_7000_hilo_fix
3268       && !mips_opts.micromips
3269       && MF_HILO_INSN (pinfo1)
3270       && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3271     return 2;
3272
3273   /* If we're working around 24K errata, one instruction is required
3274      if an ERET or DERET is followed by a branch instruction.  */
3275   if (mips_fix_24k && !mips_opts.micromips)
3276     {
3277       if (insn1->insn_opcode == INSN_ERET
3278           || insn1->insn_opcode == INSN_DERET)
3279         {
3280           if (insn2 == NULL
3281               || insn2->insn_opcode == INSN_ERET
3282               || insn2->insn_opcode == INSN_DERET
3283               || delayed_branch_p (insn2))
3284             return 1;
3285         }
3286     }
3287
3288   /* If working around VR4120 errata, check for combinations that need
3289      a single intervening instruction.  */
3290   if (mips_fix_vr4120 && !mips_opts.micromips)
3291     {
3292       unsigned int class1, class2;
3293
3294       class1 = classify_vr4120_insn (insn1->insn_mo->name);
3295       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3296         {
3297           if (insn2 == NULL)
3298             return 1;
3299           class2 = classify_vr4120_insn (insn2->insn_mo->name);
3300           if (vr4120_conflicts[class1] & (1 << class2))
3301             return 1;
3302         }
3303     }
3304
3305   if (!HAVE_CODE_COMPRESSION)
3306     {
3307       /* Check for GPR or coprocessor load delays.  All such delays
3308          are on the RT register.  */
3309       /* Itbl support may require additional care here.  */
3310       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3311           || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3312         {
3313           know (pinfo1 & INSN_WRITE_GPR_T);
3314           if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3315             return 1;
3316         }
3317
3318       /* Check for generic coprocessor hazards.
3319
3320          This case is not handled very well.  There is no special
3321          knowledge of CP0 handling, and the coprocessors other than
3322          the floating point unit are not distinguished at all.  */
3323       /* Itbl support may require additional care here. FIXME!
3324          Need to modify this to include knowledge about
3325          user specified delays!  */
3326       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3327                || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3328         {
3329           /* Handle cases where INSN1 writes to a known general coprocessor
3330              register.  There must be a one instruction delay before INSN2
3331              if INSN2 reads that register, otherwise no delay is needed.  */
3332           mask = fpr_write_mask (insn1);
3333           if (mask != 0)
3334             {
3335               if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3336                 return 1;
3337             }
3338           else
3339             {
3340               /* Read-after-write dependencies on the control registers
3341                  require a two-instruction gap.  */
3342               if ((pinfo1 & INSN_WRITE_COND_CODE)
3343                   && (pinfo2 & INSN_READ_COND_CODE))
3344                 return 2;
3345
3346               /* We don't know exactly what INSN1 does.  If INSN2 is
3347                  also a coprocessor instruction, assume there must be
3348                  a one instruction gap.  */
3349               if (pinfo2 & INSN_COP)
3350                 return 1;
3351             }
3352         }
3353
3354       /* Check for read-after-write dependencies on the coprocessor
3355          control registers in cases where INSN1 does not need a general
3356          coprocessor delay.  This means that INSN1 is a floating point
3357          comparison instruction.  */
3358       /* Itbl support may require additional care here.  */
3359       else if (!cop_interlocks
3360                && (pinfo1 & INSN_WRITE_COND_CODE)
3361                && (pinfo2 & INSN_READ_COND_CODE))
3362         return 1;
3363     }
3364
3365 #undef INSN2_USES_GPR
3366
3367   return 0;
3368 }
3369
3370 /* Return the number of nops that would be needed to work around the
3371    VR4130 mflo/mfhi errata if instruction INSN immediately followed
3372    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
3373    that are contained within the first IGNORE instructions of HIST.  */
3374
3375 static int
3376 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3377                  const struct mips_cl_insn *insn)
3378 {
3379   int i, j;
3380   unsigned int mask;
3381
3382   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
3383      are not affected by the errata.  */
3384   if (insn != 0
3385       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3386           || strcmp (insn->insn_mo->name, "mtlo") == 0
3387           || strcmp (insn->insn_mo->name, "mthi") == 0))
3388     return 0;
3389
3390   /* Search for the first MFLO or MFHI.  */
3391   for (i = 0; i < MAX_VR4130_NOPS; i++)
3392     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3393       {
3394         /* Extract the destination register.  */
3395         mask = gpr_write_mask (&hist[i]);
3396
3397         /* No nops are needed if INSN reads that register.  */
3398         if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3399           return 0;
3400
3401         /* ...or if any of the intervening instructions do.  */
3402         for (j = 0; j < i; j++)
3403           if (gpr_read_mask (&hist[j]) & mask)
3404             return 0;
3405
3406         if (i >= ignore)
3407           return MAX_VR4130_NOPS - i;
3408       }
3409   return 0;
3410 }
3411
3412 #define BASE_REG_EQ(INSN1, INSN2)       \
3413   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3414       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3415
3416 /* Return the minimum alignment for this store instruction.  */
3417
3418 static int
3419 fix_24k_align_to (const struct mips_opcode *mo)
3420 {
3421   if (strcmp (mo->name, "sh") == 0)
3422     return 2;
3423
3424   if (strcmp (mo->name, "swc1") == 0
3425       || strcmp (mo->name, "swc2") == 0
3426       || strcmp (mo->name, "sw") == 0
3427       || strcmp (mo->name, "sc") == 0
3428       || strcmp (mo->name, "s.s") == 0)
3429     return 4;
3430
3431   if (strcmp (mo->name, "sdc1") == 0
3432       || strcmp (mo->name, "sdc2") == 0
3433       || strcmp (mo->name, "s.d") == 0)
3434     return 8;
3435
3436   /* sb, swl, swr */
3437   return 1;
3438 }
3439
3440 struct fix_24k_store_info
3441   {
3442     /* Immediate offset, if any, for this store instruction.  */
3443     short off;
3444     /* Alignment required by this store instruction.  */
3445     int align_to;
3446     /* True for register offsets.  */
3447     int register_offset;
3448   };
3449
3450 /* Comparison function used by qsort.  */
3451
3452 static int
3453 fix_24k_sort (const void *a, const void *b)
3454 {
3455   const struct fix_24k_store_info *pos1 = a;
3456   const struct fix_24k_store_info *pos2 = b;
3457
3458   return (pos1->off - pos2->off);
3459 }
3460
3461 /* INSN is a store instruction.  Try to record the store information
3462    in STINFO.  Return false if the information isn't known.  */
3463
3464 static bfd_boolean
3465 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3466                            const struct mips_cl_insn *insn)
3467 {
3468   /* The instruction must have a known offset.  */
3469   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3470     return FALSE;
3471
3472   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3473   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3474   return TRUE;
3475 }
3476
3477 /* Return the number of nops that would be needed to work around the 24k
3478    "lost data on stores during refill" errata if instruction INSN
3479    immediately followed the 2 instructions described by HIST.
3480    Ignore hazards that are contained within the first IGNORE
3481    instructions of HIST.
3482
3483    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3484    for the data cache refills and store data. The following describes
3485    the scenario where the store data could be lost.
3486
3487    * A data cache miss, due to either a load or a store, causing fill
3488      data to be supplied by the memory subsystem
3489    * The first three doublewords of fill data are returned and written
3490      into the cache
3491    * A sequence of four stores occurs in consecutive cycles around the
3492      final doubleword of the fill:
3493    * Store A
3494    * Store B
3495    * Store C
3496    * Zero, One or more instructions
3497    * Store D
3498
3499    The four stores A-D must be to different doublewords of the line that
3500    is being filled. The fourth instruction in the sequence above permits
3501    the fill of the final doubleword to be transferred from the FSB into
3502    the cache. In the sequence above, the stores may be either integer
3503    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3504    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3505    different doublewords on the line. If the floating point unit is
3506    running in 1:2 mode, it is not possible to create the sequence above
3507    using only floating point store instructions.
3508
3509    In this case, the cache line being filled is incorrectly marked
3510    invalid, thereby losing the data from any store to the line that
3511    occurs between the original miss and the completion of the five
3512    cycle sequence shown above.
3513
3514    The workarounds are:
3515
3516    * Run the data cache in write-through mode.
3517    * Insert a non-store instruction between
3518      Store A and Store B or Store B and Store C.  */
3519   
3520 static int
3521 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3522               const struct mips_cl_insn *insn)
3523 {
3524   struct fix_24k_store_info pos[3];
3525   int align, i, base_offset;
3526
3527   if (ignore >= 2)
3528     return 0;
3529
3530   /* If the previous instruction wasn't a store, there's nothing to
3531      worry about.  */
3532   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3533     return 0;
3534
3535   /* If the instructions after the previous one are unknown, we have
3536      to assume the worst.  */
3537   if (!insn)
3538     return 1;
3539
3540   /* Check whether we are dealing with three consecutive stores.  */
3541   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3542       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3543     return 0;
3544
3545   /* If we don't know the relationship between the store addresses,
3546      assume the worst.  */
3547   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3548       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3549     return 1;
3550
3551   if (!fix_24k_record_store_info (&pos[0], insn)
3552       || !fix_24k_record_store_info (&pos[1], &hist[0])
3553       || !fix_24k_record_store_info (&pos[2], &hist[1]))
3554     return 1;
3555
3556   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3557
3558   /* Pick a value of ALIGN and X such that all offsets are adjusted by
3559      X bytes and such that the base register + X is known to be aligned
3560      to align bytes.  */
3561
3562   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3563     align = 8;
3564   else
3565     {
3566       align = pos[0].align_to;
3567       base_offset = pos[0].off;
3568       for (i = 1; i < 3; i++)
3569         if (align < pos[i].align_to)
3570           {
3571             align = pos[i].align_to;
3572             base_offset = pos[i].off;
3573           }
3574       for (i = 0; i < 3; i++)
3575         pos[i].off -= base_offset;
3576     }
3577
3578   pos[0].off &= ~align + 1;
3579   pos[1].off &= ~align + 1;
3580   pos[2].off &= ~align + 1;
3581
3582   /* If any two stores write to the same chunk, they also write to the
3583      same doubleword.  The offsets are still sorted at this point.  */
3584   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3585     return 0;
3586
3587   /* A range of at least 9 bytes is needed for the stores to be in
3588      non-overlapping doublewords.  */
3589   if (pos[2].off - pos[0].off <= 8)
3590     return 0;
3591
3592   if (pos[2].off - pos[1].off >= 24
3593       || pos[1].off - pos[0].off >= 24
3594       || pos[2].off - pos[0].off >= 32)
3595     return 0;
3596
3597   return 1;
3598 }
3599
3600 /* Return the number of nops that would be needed if instruction INSN
3601    immediately followed the MAX_NOPS instructions given by HIST,
3602    where HIST[0] is the most recent instruction.  Ignore hazards
3603    between INSN and the first IGNORE instructions in HIST.
3604
3605    If INSN is null, return the worse-case number of nops for any
3606    instruction.  */
3607
3608 static int
3609 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3610                const struct mips_cl_insn *insn)
3611 {
3612   int i, nops, tmp_nops;
3613
3614   nops = 0;
3615   for (i = ignore; i < MAX_DELAY_NOPS; i++)
3616     {
3617       tmp_nops = insns_between (hist + i, insn) - i;
3618       if (tmp_nops > nops)
3619         nops = tmp_nops;
3620     }
3621
3622   if (mips_fix_vr4130 && !mips_opts.micromips)
3623     {
3624       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
3625       if (tmp_nops > nops)
3626         nops = tmp_nops;
3627     }
3628
3629   if (mips_fix_24k && !mips_opts.micromips)
3630     {
3631       tmp_nops = nops_for_24k (ignore, hist, insn);
3632       if (tmp_nops > nops)
3633         nops = tmp_nops;
3634     }
3635
3636   return nops;
3637 }
3638
3639 /* The variable arguments provide NUM_INSNS extra instructions that
3640    might be added to HIST.  Return the largest number of nops that
3641    would be needed after the extended sequence, ignoring hazards
3642    in the first IGNORE instructions.  */
3643
3644 static int
3645 nops_for_sequence (int num_insns, int ignore,
3646                    const struct mips_cl_insn *hist, ...)
3647 {
3648   va_list args;
3649   struct mips_cl_insn buffer[MAX_NOPS];
3650   struct mips_cl_insn *cursor;
3651   int nops;
3652
3653   va_start (args, hist);
3654   cursor = buffer + num_insns;
3655   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
3656   while (cursor > buffer)
3657     *--cursor = *va_arg (args, const struct mips_cl_insn *);
3658
3659   nops = nops_for_insn (ignore, buffer, NULL);
3660   va_end (args);
3661   return nops;
3662 }
3663
3664 /* Like nops_for_insn, but if INSN is a branch, take into account the
3665    worst-case delay for the branch target.  */
3666
3667 static int
3668 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
3669                          const struct mips_cl_insn *insn)
3670 {
3671   int nops, tmp_nops;
3672
3673   nops = nops_for_insn (ignore, hist, insn);
3674   if (delayed_branch_p (insn))
3675     {
3676       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
3677                                     hist, insn, get_delay_slot_nop (insn));
3678       if (tmp_nops > nops)
3679         nops = tmp_nops;
3680     }
3681   else if (compact_branch_p (insn))
3682     {
3683       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
3684       if (tmp_nops > nops)
3685         nops = tmp_nops;
3686     }
3687   return nops;
3688 }
3689
3690 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
3691
3692 static void
3693 fix_loongson2f_nop (struct mips_cl_insn * ip)
3694 {
3695   gas_assert (!HAVE_CODE_COMPRESSION);
3696   if (strcmp (ip->insn_mo->name, "nop") == 0)
3697     ip->insn_opcode = LOONGSON2F_NOP_INSN;
3698 }
3699
3700 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3701                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
3702
3703 static void
3704 fix_loongson2f_jump (struct mips_cl_insn * ip)
3705 {
3706   gas_assert (!HAVE_CODE_COMPRESSION);
3707   if (strcmp (ip->insn_mo->name, "j") == 0
3708       || strcmp (ip->insn_mo->name, "jr") == 0
3709       || strcmp (ip->insn_mo->name, "jalr") == 0)
3710     {
3711       int sreg;
3712       expressionS ep;
3713
3714       if (! mips_opts.at)
3715         return;
3716
3717       sreg = EXTRACT_OPERAND (0, RS, *ip);
3718       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3719         return;
3720
3721       ep.X_op = O_constant;
3722       ep.X_add_number = 0xcfff0000;
3723       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3724       ep.X_add_number = 0xffff;
3725       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3726       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3727     }
3728 }
3729
3730 static void
3731 fix_loongson2f (struct mips_cl_insn * ip)
3732 {
3733   if (mips_fix_loongson2f_nop)
3734     fix_loongson2f_nop (ip);
3735
3736   if (mips_fix_loongson2f_jump)
3737     fix_loongson2f_jump (ip);
3738 }
3739
3740 /* IP is a branch that has a delay slot, and we need to fill it
3741    automatically.   Return true if we can do that by swapping IP
3742    with the previous instruction.
3743    ADDRESS_EXPR is an operand of the instruction to be used with
3744    RELOC_TYPE.  */
3745
3746 static bfd_boolean
3747 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3748   bfd_reloc_code_real_type *reloc_type)
3749 {
3750   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
3751   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3752
3753   /* -O2 and above is required for this optimization.  */
3754   if (mips_optimize < 2)
3755     return FALSE;
3756
3757   /* If we have seen .set volatile or .set nomove, don't optimize.  */
3758   if (mips_opts.nomove)
3759     return FALSE;
3760
3761   /* We can't swap if the previous instruction's position is fixed.  */
3762   if (history[0].fixed_p)
3763     return FALSE;
3764
3765   /* If the previous previous insn was in a .set noreorder, we can't
3766      swap.  Actually, the MIPS assembler will swap in this situation.
3767      However, gcc configured -with-gnu-as will generate code like
3768
3769         .set    noreorder
3770         lw      $4,XXX
3771         .set    reorder
3772         INSN
3773         bne     $4,$0,foo
3774
3775      in which we can not swap the bne and INSN.  If gcc is not configured
3776      -with-gnu-as, it does not output the .set pseudo-ops.  */
3777   if (history[1].noreorder_p)
3778     return FALSE;
3779
3780   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3781      This means that the previous instruction was a 4-byte one anyhow.  */
3782   if (mips_opts.mips16 && history[0].fixp[0])
3783     return FALSE;
3784
3785   /* If the branch is itself the target of a branch, we can not swap.
3786      We cheat on this; all we check for is whether there is a label on
3787      this instruction.  If there are any branches to anything other than
3788      a label, users must use .set noreorder.  */
3789   if (seg_info (now_seg)->label_list)
3790     return FALSE;
3791
3792   /* If the previous instruction is in a variant frag other than this
3793      branch's one, we cannot do the swap.  This does not apply to
3794      MIPS16 code, which uses variant frags for different purposes.  */
3795   if (!mips_opts.mips16
3796       && history[0].frag
3797       && history[0].frag->fr_type == rs_machine_dependent)
3798     return FALSE;
3799
3800   /* We do not swap with instructions that cannot architecturally
3801      be placed in a branch delay slot, such as SYNC or ERET.  We
3802      also refrain from swapping with a trap instruction, since it
3803      complicates trap handlers to have the trap instruction be in
3804      a delay slot.  */
3805   prev_pinfo = history[0].insn_mo->pinfo;
3806   if (prev_pinfo & INSN_NO_DELAY_SLOT)
3807     return FALSE;
3808
3809   /* Check for conflicts between the branch and the instructions
3810      before the candidate delay slot.  */
3811   if (nops_for_insn (0, history + 1, ip) > 0)
3812     return FALSE;
3813
3814   /* Check for conflicts between the swapped sequence and the
3815      target of the branch.  */
3816   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3817     return FALSE;
3818
3819   /* If the branch reads a register that the previous
3820      instruction sets, we can not swap.  */
3821   gpr_read = gpr_read_mask (ip);
3822   prev_gpr_write = gpr_write_mask (&history[0]);
3823   if (gpr_read & prev_gpr_write)
3824     return FALSE;
3825
3826   /* If the branch writes a register that the previous
3827      instruction sets, we can not swap.  */
3828   gpr_write = gpr_write_mask (ip);
3829   if (gpr_write & prev_gpr_write)
3830     return FALSE;
3831
3832   /* If the branch writes a register that the previous
3833      instruction reads, we can not swap.  */
3834   prev_gpr_read = gpr_read_mask (&history[0]);
3835   if (gpr_write & prev_gpr_read)
3836     return FALSE;
3837
3838   /* If one instruction sets a condition code and the
3839      other one uses a condition code, we can not swap.  */
3840   pinfo = ip->insn_mo->pinfo;
3841   if ((pinfo & INSN_READ_COND_CODE)
3842       && (prev_pinfo & INSN_WRITE_COND_CODE))
3843     return FALSE;
3844   if ((pinfo & INSN_WRITE_COND_CODE)
3845       && (prev_pinfo & INSN_READ_COND_CODE))
3846     return FALSE;
3847
3848   /* If the previous instruction uses the PC, we can not swap.  */
3849   prev_pinfo2 = history[0].insn_mo->pinfo2;
3850   if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3851     return FALSE;
3852   if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3853     return FALSE;
3854
3855   /* If the previous instruction has an incorrect size for a fixed
3856      branch delay slot in microMIPS mode, we cannot swap.  */
3857   pinfo2 = ip->insn_mo->pinfo2;
3858   if (mips_opts.micromips
3859       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3860       && insn_length (history) != 2)
3861     return FALSE;
3862   if (mips_opts.micromips
3863       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3864       && insn_length (history) != 4)
3865     return FALSE;
3866
3867   /* On R5900 short loops need to be fixed by inserting a nop in
3868      the branch delay slots.
3869      A short loop can be terminated too early.  */
3870   if (mips_opts.arch == CPU_R5900
3871       /* Check if instruction has a parameter, ignore "j $31". */
3872       && (address_expr != NULL)
3873       /* Parameter must be 16 bit. */
3874       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3875       /* Branch to same segment. */
3876       && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3877       /* Branch to same code fragment. */
3878       && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3879       /* Can only calculate branch offset if value is known. */
3880       && symbol_constant_p(address_expr->X_add_symbol)
3881       /* Check if branch is really conditional. */
3882       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
3883         || (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
3884         || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3885     {
3886       int distance;
3887       /* Check if loop is shorter than 6 instructions including
3888          branch and delay slot.  */
3889       distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3890       if (distance <= 20)
3891         {
3892           int i;
3893           int rv;
3894
3895           rv = FALSE;
3896           /* When the loop includes branches or jumps,
3897              it is not a short loop. */
3898           for (i = 0; i < (distance / 4); i++)
3899             {
3900               if ((history[i].cleared_p)
3901                   || delayed_branch_p(&history[i]))
3902                 {
3903                   rv = TRUE;
3904                   break;
3905                 }
3906             }
3907           if (rv == FALSE)
3908             {
3909               /* Insert nop after branch to fix short loop. */
3910               return FALSE;
3911             }
3912         }
3913     }
3914
3915   return TRUE;
3916 }
3917
3918 /* Decide how we should add IP to the instruction stream.
3919    ADDRESS_EXPR is an operand of the instruction to be used with
3920    RELOC_TYPE.  */
3921
3922 static enum append_method
3923 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3924   bfd_reloc_code_real_type *reloc_type)
3925 {
3926   unsigned long pinfo;
3927
3928   /* The relaxed version of a macro sequence must be inherently
3929      hazard-free.  */
3930   if (mips_relax.sequence == 2)
3931     return APPEND_ADD;
3932
3933   /* We must not dabble with instructions in a ".set norerorder" block.  */
3934   if (mips_opts.noreorder)
3935     return APPEND_ADD;
3936
3937   /* Otherwise, it's our responsibility to fill branch delay slots.  */
3938   if (delayed_branch_p (ip))
3939     {
3940       if (!branch_likely_p (ip)
3941           && can_swap_branch_p (ip, address_expr, reloc_type))
3942         return APPEND_SWAP;
3943
3944       pinfo = ip->insn_mo->pinfo;
3945       if (mips_opts.mips16
3946           && ISA_SUPPORTS_MIPS16E
3947           && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3948         return APPEND_ADD_COMPACT;
3949
3950       return APPEND_ADD_WITH_NOP;
3951     }
3952
3953   return APPEND_ADD;
3954 }
3955
3956 /* IP is a MIPS16 instruction whose opcode we have just changed.
3957    Point IP->insn_mo to the new opcode's definition.  */
3958
3959 static void
3960 find_altered_mips16_opcode (struct mips_cl_insn *ip)
3961 {
3962   const struct mips_opcode *mo, *end;
3963
3964   end = &mips16_opcodes[bfd_mips16_num_opcodes];
3965   for (mo = ip->insn_mo; mo < end; mo++)
3966     if ((ip->insn_opcode & mo->mask) == mo->match)
3967       {
3968         ip->insn_mo = mo;
3969         return;
3970       }
3971   abort ();
3972 }
3973
3974 /* For microMIPS macros, we need to generate a local number label
3975    as the target of branches.  */
3976 #define MICROMIPS_LABEL_CHAR            '\037'
3977 static unsigned long micromips_target_label;
3978 static char micromips_target_name[32];
3979
3980 static char *
3981 micromips_label_name (void)
3982 {
3983   char *p = micromips_target_name;
3984   char symbol_name_temporary[24];
3985   unsigned long l;
3986   int i;
3987
3988   if (*p)
3989     return p;
3990
3991   i = 0;
3992   l = micromips_target_label;
3993 #ifdef LOCAL_LABEL_PREFIX
3994   *p++ = LOCAL_LABEL_PREFIX;
3995 #endif
3996   *p++ = 'L';
3997   *p++ = MICROMIPS_LABEL_CHAR;
3998   do
3999     {
4000       symbol_name_temporary[i++] = l % 10 + '0';
4001       l /= 10;
4002     }
4003   while (l != 0);
4004   while (i > 0)
4005     *p++ = symbol_name_temporary[--i];
4006   *p = '\0';
4007
4008   return micromips_target_name;
4009 }
4010
4011 static void
4012 micromips_label_expr (expressionS *label_expr)
4013 {
4014   label_expr->X_op = O_symbol;
4015   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4016   label_expr->X_add_number = 0;
4017 }
4018
4019 static void
4020 micromips_label_inc (void)
4021 {
4022   micromips_target_label++;
4023   *micromips_target_name = '\0';
4024 }
4025
4026 static void
4027 micromips_add_label (void)
4028 {
4029   symbolS *s;
4030
4031   s = colon (micromips_label_name ());
4032   micromips_label_inc ();
4033 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4034   if (IS_ELF)
4035     S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
4036 #else
4037   (void) s;
4038 #endif
4039 }
4040
4041 /* If assembling microMIPS code, then return the microMIPS reloc
4042    corresponding to the requested one if any.  Otherwise return
4043    the reloc unchanged.  */
4044
4045 static bfd_reloc_code_real_type
4046 micromips_map_reloc (bfd_reloc_code_real_type reloc)
4047 {
4048   static const bfd_reloc_code_real_type relocs[][2] =
4049     {
4050       /* Keep sorted incrementally by the left-hand key.  */
4051       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4052       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4053       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4054       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4055       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4056       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4057       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4058       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4059       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4060       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4061       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4062       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4063       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4064       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4065       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4066       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4067       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4068       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4069       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4070       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4071       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4072       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4073       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4074       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4075       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4076       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4077       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4078     };
4079   bfd_reloc_code_real_type r;
4080   size_t i;
4081
4082   if (!mips_opts.micromips)
4083     return reloc;
4084   for (i = 0; i < ARRAY_SIZE (relocs); i++)
4085     {
4086       r = relocs[i][0];
4087       if (r > reloc)
4088         return reloc;
4089       if (r == reloc)
4090         return relocs[i][1];
4091     }
4092   return reloc;
4093 }
4094
4095 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4096    Return true on success, storing the resolved value in RESULT.  */
4097
4098 static bfd_boolean
4099 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4100                  offsetT *result)
4101 {
4102   switch (reloc)
4103     {
4104     case BFD_RELOC_MIPS_HIGHEST:
4105     case BFD_RELOC_MICROMIPS_HIGHEST:
4106       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4107       return TRUE;
4108
4109     case BFD_RELOC_MIPS_HIGHER:
4110     case BFD_RELOC_MICROMIPS_HIGHER:
4111       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4112       return TRUE;
4113
4114     case BFD_RELOC_HI16_S:
4115     case BFD_RELOC_MICROMIPS_HI16_S:
4116     case BFD_RELOC_MIPS16_HI16_S:
4117       *result = ((operand + 0x8000) >> 16) & 0xffff;
4118       return TRUE;
4119
4120     case BFD_RELOC_HI16:
4121     case BFD_RELOC_MICROMIPS_HI16:
4122     case BFD_RELOC_MIPS16_HI16:
4123       *result = (operand >> 16) & 0xffff;
4124       return TRUE;
4125
4126     case BFD_RELOC_LO16:
4127     case BFD_RELOC_MICROMIPS_LO16:
4128     case BFD_RELOC_MIPS16_LO16:
4129       *result = operand & 0xffff;
4130       return TRUE;
4131
4132     case BFD_RELOC_UNUSED:
4133       *result = operand;
4134       return TRUE;
4135
4136     default:
4137       return FALSE;
4138     }
4139 }
4140
4141 /* Output an instruction.  IP is the instruction information.
4142    ADDRESS_EXPR is an operand of the instruction to be used with
4143    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
4144    a macro expansion.  */
4145
4146 static void
4147 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
4148              bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
4149 {
4150   unsigned long prev_pinfo2, pinfo;
4151   bfd_boolean relaxed_branch = FALSE;
4152   enum append_method method;
4153   bfd_boolean relax32;
4154   int branch_disp;
4155
4156   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4157     fix_loongson2f (ip);
4158
4159   file_ase_mips16 |= mips_opts.mips16;
4160   file_ase_micromips |= mips_opts.micromips;
4161
4162   prev_pinfo2 = history[0].insn_mo->pinfo2;
4163   pinfo = ip->insn_mo->pinfo;
4164
4165   if (mips_opts.micromips
4166       && !expansionp
4167       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4168            && micromips_insn_length (ip->insn_mo) != 2)
4169           || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4170               && micromips_insn_length (ip->insn_mo) != 4)))
4171     as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4172              (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4173
4174   if (address_expr == NULL)
4175     ip->complete_p = 1;
4176   else if (reloc_type[0] <= BFD_RELOC_UNUSED
4177            && reloc_type[1] == BFD_RELOC_UNUSED
4178            && reloc_type[2] == BFD_RELOC_UNUSED
4179            && address_expr->X_op == O_constant)
4180     {
4181       switch (*reloc_type)
4182         {
4183         case BFD_RELOC_MIPS_JMP:
4184           {
4185             int shift;
4186
4187             shift = mips_opts.micromips ? 1 : 2;
4188             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4189               as_bad (_("jump to misaligned address (0x%lx)"),
4190                       (unsigned long) address_expr->X_add_number);
4191             ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4192                                 & 0x3ffffff);
4193             ip->complete_p = 1;
4194           }
4195           break;
4196
4197         case BFD_RELOC_MIPS16_JMP:
4198           if ((address_expr->X_add_number & 3) != 0)
4199             as_bad (_("jump to misaligned address (0x%lx)"),
4200                     (unsigned long) address_expr->X_add_number);
4201           ip->insn_opcode |=
4202             (((address_expr->X_add_number & 0x7c0000) << 3)
4203                | ((address_expr->X_add_number & 0xf800000) >> 7)
4204                | ((address_expr->X_add_number & 0x3fffc) >> 2));
4205           ip->complete_p = 1;
4206           break;
4207
4208         case BFD_RELOC_16_PCREL_S2:
4209           {
4210             int shift;
4211
4212             shift = mips_opts.micromips ? 1 : 2;
4213             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4214               as_bad (_("branch to misaligned address (0x%lx)"),
4215                       (unsigned long) address_expr->X_add_number);
4216             if (!mips_relax_branch)
4217               {
4218                 if ((address_expr->X_add_number + (1 << (shift + 15)))
4219                     & ~((1 << (shift + 16)) - 1))
4220                   as_bad (_("branch address range overflow (0x%lx)"),
4221                           (unsigned long) address_expr->X_add_number);
4222                 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4223                                     & 0xffff);
4224               }
4225           }
4226           break;
4227
4228         default:
4229           {
4230             offsetT value;
4231
4232             if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4233                                  &value))
4234               {
4235                 ip->insn_opcode |= value & 0xffff;
4236                 ip->complete_p = 1;
4237               }
4238           }
4239           break;
4240         }
4241     }
4242
4243   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4244     {
4245       /* There are a lot of optimizations we could do that we don't.
4246          In particular, we do not, in general, reorder instructions.
4247          If you use gcc with optimization, it will reorder
4248          instructions and generally do much more optimization then we
4249          do here; repeating all that work in the assembler would only
4250          benefit hand written assembly code, and does not seem worth
4251          it.  */
4252       int nops = (mips_optimize == 0
4253                   ? nops_for_insn (0, history, NULL)
4254                   : nops_for_insn_or_target (0, history, ip));
4255       if (nops > 0)
4256         {
4257           fragS *old_frag;
4258           unsigned long old_frag_offset;
4259           int i;
4260
4261           old_frag = frag_now;
4262           old_frag_offset = frag_now_fix ();
4263
4264           for (i = 0; i < nops; i++)
4265             add_fixed_insn (NOP_INSN);
4266           insert_into_history (0, nops, NOP_INSN);
4267
4268           if (listing)
4269             {
4270               listing_prev_line ();
4271               /* We may be at the start of a variant frag.  In case we
4272                  are, make sure there is enough space for the frag
4273                  after the frags created by listing_prev_line.  The
4274                  argument to frag_grow here must be at least as large
4275                  as the argument to all other calls to frag_grow in
4276                  this file.  We don't have to worry about being in the
4277                  middle of a variant frag, because the variants insert
4278                  all needed nop instructions themselves.  */
4279               frag_grow (40);
4280             }
4281
4282           mips_move_text_labels ();
4283
4284 #ifndef NO_ECOFF_DEBUGGING
4285           if (ECOFF_DEBUGGING)
4286             ecoff_fix_loc (old_frag, old_frag_offset);
4287 #endif
4288         }
4289     }
4290   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4291     {
4292       int nops;
4293
4294       /* Work out how many nops in prev_nop_frag are needed by IP,
4295          ignoring hazards generated by the first prev_nop_frag_since
4296          instructions.  */
4297       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4298       gas_assert (nops <= prev_nop_frag_holds);
4299
4300       /* Enforce NOPS as a minimum.  */
4301       if (nops > prev_nop_frag_required)
4302         prev_nop_frag_required = nops;
4303
4304       if (prev_nop_frag_holds == prev_nop_frag_required)
4305         {
4306           /* Settle for the current number of nops.  Update the history
4307              accordingly (for the benefit of any future .set reorder code).  */
4308           prev_nop_frag = NULL;
4309           insert_into_history (prev_nop_frag_since,
4310                                prev_nop_frag_holds, NOP_INSN);
4311         }
4312       else
4313         {
4314           /* Allow this instruction to replace one of the nops that was
4315              tentatively added to prev_nop_frag.  */
4316           prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4317           prev_nop_frag_holds--;
4318           prev_nop_frag_since++;
4319         }
4320     }
4321
4322   method = get_append_method (ip, address_expr, reloc_type);
4323   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4324
4325 #ifdef OBJ_ELF
4326   /* The value passed to dwarf2_emit_insn is the distance between
4327      the beginning of the current instruction and the address that
4328      should be recorded in the debug tables.  This is normally the
4329      current address.
4330
4331      For MIPS16/microMIPS debug info we want to use ISA-encoded
4332      addresses, so we use -1 for an address higher by one than the
4333      current one.
4334
4335      If the instruction produced is a branch that we will swap with
4336      the preceding instruction, then we add the displacement by which
4337      the branch will be moved backwards.  This is more appropriate
4338      and for MIPS16/microMIPS code also prevents a debugger from
4339      placing a breakpoint in the middle of the branch (and corrupting
4340      code if software breakpoints are used).  */
4341   dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
4342 #endif
4343
4344   relax32 = (mips_relax_branch
4345              /* Don't try branch relaxation within .set nomacro, or within
4346                 .set noat if we use $at for PIC computations.  If it turns
4347                 out that the branch was out-of-range, we'll get an error.  */
4348              && !mips_opts.warn_about_macros
4349              && (mips_opts.at || mips_pic == NO_PIC)
4350              /* Don't relax BPOSGE32/64 as they have no complementing
4351                 branches.  */
4352              && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
4353
4354   if (!HAVE_CODE_COMPRESSION
4355       && address_expr
4356       && relax32
4357       && *reloc_type == BFD_RELOC_16_PCREL_S2
4358       && delayed_branch_p (ip))
4359     {
4360       relaxed_branch = TRUE;
4361       add_relaxed_insn (ip, (relaxed_branch_length
4362                              (NULL, NULL,
4363                               uncond_branch_p (ip) ? -1
4364                               : branch_likely_p (ip) ? 1
4365                               : 0)), 4,
4366                         RELAX_BRANCH_ENCODE
4367                         (AT,
4368                          uncond_branch_p (ip),
4369                          branch_likely_p (ip),
4370                          pinfo & INSN_WRITE_GPR_31,
4371                          0),
4372                         address_expr->X_add_symbol,
4373                         address_expr->X_add_number);
4374       *reloc_type = BFD_RELOC_UNUSED;
4375     }
4376   else if (mips_opts.micromips
4377            && address_expr
4378            && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4379                || *reloc_type > BFD_RELOC_UNUSED)
4380            && (delayed_branch_p (ip) || compact_branch_p (ip))
4381            /* Don't try branch relaxation when users specify
4382               16-bit/32-bit instructions.  */
4383            && !forced_insn_length)
4384     {
4385       bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4386       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4387       int uncond = uncond_branch_p (ip) ? -1 : 0;
4388       int compact = compact_branch_p (ip);
4389       int al = pinfo & INSN_WRITE_GPR_31;
4390       int length32;
4391
4392       gas_assert (address_expr != NULL);
4393       gas_assert (!mips_relax.sequence);
4394
4395       relaxed_branch = TRUE;
4396       length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4397       add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4398                         RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4399                                                 relax32, 0, 0),
4400                         address_expr->X_add_symbol,
4401                         address_expr->X_add_number);
4402       *reloc_type = BFD_RELOC_UNUSED;
4403     }
4404   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4405     {
4406       /* We need to set up a variant frag.  */
4407       gas_assert (address_expr != NULL);
4408       add_relaxed_insn (ip, 4, 0,
4409                         RELAX_MIPS16_ENCODE
4410                         (*reloc_type - BFD_RELOC_UNUSED,
4411                          forced_insn_length == 2, forced_insn_length == 4,
4412                          delayed_branch_p (&history[0]),
4413                          history[0].mips16_absolute_jump_p),
4414                         make_expr_symbol (address_expr), 0);
4415     }
4416   else if (mips_opts.mips16 && insn_length (ip) == 2)
4417     {
4418       if (!delayed_branch_p (ip))
4419         /* Make sure there is enough room to swap this instruction with
4420            a following jump instruction.  */
4421         frag_grow (6);
4422       add_fixed_insn (ip);
4423     }
4424   else
4425     {
4426       if (mips_opts.mips16
4427           && mips_opts.noreorder
4428           && delayed_branch_p (&history[0]))
4429         as_warn (_("extended instruction in delay slot"));
4430
4431       if (mips_relax.sequence)
4432         {
4433           /* If we've reached the end of this frag, turn it into a variant
4434              frag and record the information for the instructions we've
4435              written so far.  */
4436           if (frag_room () < 4)
4437             relax_close_frag ();
4438           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4439         }
4440
4441       if (mips_relax.sequence != 2)
4442         {
4443           if (mips_macro_warning.first_insn_sizes[0] == 0)
4444             mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4445           mips_macro_warning.sizes[0] += insn_length (ip);
4446           mips_macro_warning.insns[0]++;
4447         }
4448       if (mips_relax.sequence != 1)
4449         {
4450           if (mips_macro_warning.first_insn_sizes[1] == 0)
4451             mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4452           mips_macro_warning.sizes[1] += insn_length (ip);
4453           mips_macro_warning.insns[1]++;
4454         }
4455
4456       if (mips_opts.mips16)
4457         {
4458           ip->fixed_p = 1;
4459           ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4460         }
4461       add_fixed_insn (ip);
4462     }
4463
4464   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4465     {
4466       bfd_reloc_code_real_type final_type[3];
4467       reloc_howto_type *howto0;
4468       reloc_howto_type *howto;
4469       int i;
4470
4471       /* Perform any necessary conversion to microMIPS relocations
4472          and find out how many relocations there actually are.  */
4473       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4474         final_type[i] = micromips_map_reloc (reloc_type[i]);
4475
4476       /* In a compound relocation, it is the final (outermost)
4477          operator that determines the relocated field.  */
4478       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4479
4480       if (howto == NULL)
4481         {
4482           /* To reproduce this failure try assembling gas/testsuites/
4483              gas/mips/mips16-intermix.s with a mips-ecoff targeted
4484              assembler.  */
4485           as_bad (_("Unsupported MIPS relocation number %d"),
4486                   final_type[i - 1]);
4487           howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4488         }
4489
4490       if (i > 1)
4491         howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4492       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4493                                  bfd_get_reloc_size (howto),
4494                                  address_expr,
4495                                  howto0 && howto0->pc_relative,
4496                                  final_type[0]);
4497
4498       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
4499       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4500         *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4501
4502       /* These relocations can have an addend that won't fit in
4503          4 octets for 64bit assembly.  */
4504       if (HAVE_64BIT_GPRS
4505           && ! howto->partial_inplace
4506           && (reloc_type[0] == BFD_RELOC_16
4507               || reloc_type[0] == BFD_RELOC_32
4508               || reloc_type[0] == BFD_RELOC_MIPS_JMP
4509               || reloc_type[0] == BFD_RELOC_GPREL16
4510               || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4511               || reloc_type[0] == BFD_RELOC_GPREL32
4512               || reloc_type[0] == BFD_RELOC_64
4513               || reloc_type[0] == BFD_RELOC_CTOR
4514               || reloc_type[0] == BFD_RELOC_MIPS_SUB
4515               || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4516               || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4517               || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4518               || reloc_type[0] == BFD_RELOC_MIPS_REL16
4519               || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4520               || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4521               || hi16_reloc_p (reloc_type[0])
4522               || lo16_reloc_p (reloc_type[0])))
4523         ip->fixp[0]->fx_no_overflow = 1;
4524
4525       /* These relocations can have an addend that won't fit in 2 octets.  */
4526       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4527           || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4528         ip->fixp[0]->fx_no_overflow = 1;
4529
4530       if (mips_relax.sequence)
4531         {
4532           if (mips_relax.first_fixup == 0)
4533             mips_relax.first_fixup = ip->fixp[0];
4534         }
4535       else if (reloc_needs_lo_p (*reloc_type))
4536         {
4537           struct mips_hi_fixup *hi_fixup;
4538
4539           /* Reuse the last entry if it already has a matching %lo.  */
4540           hi_fixup = mips_hi_fixup_list;
4541           if (hi_fixup == 0
4542               || !fixup_has_matching_lo_p (hi_fixup->fixp))
4543             {
4544               hi_fixup = ((struct mips_hi_fixup *)
4545                           xmalloc (sizeof (struct mips_hi_fixup)));
4546               hi_fixup->next = mips_hi_fixup_list;
4547               mips_hi_fixup_list = hi_fixup;
4548             }
4549           hi_fixup->fixp = ip->fixp[0];
4550           hi_fixup->seg = now_seg;
4551         }
4552
4553       /* Add fixups for the second and third relocations, if given.
4554          Note that the ABI allows the second relocation to be
4555          against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
4556          moment we only use RSS_UNDEF, but we could add support
4557          for the others if it ever becomes necessary.  */
4558       for (i = 1; i < 3; i++)
4559         if (reloc_type[i] != BFD_RELOC_UNUSED)
4560           {
4561             ip->fixp[i] = fix_new (ip->frag, ip->where,
4562                                    ip->fixp[0]->fx_size, NULL, 0,
4563                                    FALSE, final_type[i]);
4564
4565             /* Use fx_tcbit to mark compound relocs.  */
4566             ip->fixp[0]->fx_tcbit = 1;
4567             ip->fixp[i]->fx_tcbit = 1;
4568           }
4569     }
4570   install_insn (ip);
4571
4572   /* Update the register mask information.  */
4573   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4574   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4575
4576   switch (method)
4577     {
4578     case APPEND_ADD:
4579       insert_into_history (0, 1, ip);
4580       break;
4581
4582     case APPEND_ADD_WITH_NOP:
4583       {
4584         struct mips_cl_insn *nop;
4585
4586         insert_into_history (0, 1, ip);
4587         nop = get_delay_slot_nop (ip);
4588         add_fixed_insn (nop);
4589         insert_into_history (0, 1, nop);
4590         if (mips_relax.sequence)
4591           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4592       }
4593       break;
4594
4595     case APPEND_ADD_COMPACT:
4596       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
4597       gas_assert (mips_opts.mips16);
4598       ip->insn_opcode |= 0x0080;
4599       find_altered_mips16_opcode (ip);
4600       install_insn (ip);
4601       insert_into_history (0, 1, ip);
4602       break;
4603
4604     case APPEND_SWAP:
4605       {
4606         struct mips_cl_insn delay = history[0];
4607         if (mips_opts.mips16)
4608           {
4609             know (delay.frag == ip->frag);
4610             move_insn (ip, delay.frag, delay.where);
4611             move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4612           }
4613         else if (relaxed_branch || delay.frag != ip->frag)
4614           {
4615             /* Add the delay slot instruction to the end of the
4616                current frag and shrink the fixed part of the
4617                original frag.  If the branch occupies the tail of
4618                the latter, move it backwards to cover the gap.  */
4619             delay.frag->fr_fix -= branch_disp;
4620             if (delay.frag == ip->frag)
4621               move_insn (ip, ip->frag, ip->where - branch_disp);
4622             add_fixed_insn (&delay);
4623           }
4624         else
4625           {
4626             move_insn (&delay, ip->frag,
4627                        ip->where - branch_disp + insn_length (ip));
4628             move_insn (ip, history[0].frag, history[0].where);
4629           }
4630         history[0] = *ip;
4631         delay.fixed_p = 1;
4632         insert_into_history (0, 1, &delay);
4633       }
4634       break;
4635     }
4636
4637   /* If we have just completed an unconditional branch, clear the history.  */
4638   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4639       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
4640     {
4641       unsigned int i;
4642
4643     mips_no_prev_insn ();
4644
4645       for (i = 0; i < ARRAY_SIZE (history); i++)
4646       {
4647         history[i].cleared_p = 1;
4648       }
4649     }
4650
4651   /* We need to emit a label at the end of branch-likely macros.  */
4652   if (emit_branch_likely_macro)
4653     {
4654       emit_branch_likely_macro = FALSE;
4655       micromips_add_label ();
4656     }
4657
4658   /* We just output an insn, so the next one doesn't have a label.  */
4659   mips_clear_insn_labels ();
4660 }
4661
4662 /* Forget that there was any previous instruction or label.
4663    When BRANCH is true, the branch history is also flushed.  */
4664
4665 static void
4666 mips_no_prev_insn (void)
4667 {
4668   prev_nop_frag = NULL;
4669   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
4670   mips_clear_insn_labels ();
4671 }
4672
4673 /* This function must be called before we emit something other than
4674    instructions.  It is like mips_no_prev_insn except that it inserts
4675    any NOPS that might be needed by previous instructions.  */
4676
4677 void
4678 mips_emit_delays (void)
4679 {
4680   if (! mips_opts.noreorder)
4681     {
4682       int nops = nops_for_insn (0, history, NULL);
4683       if (nops > 0)
4684         {
4685           while (nops-- > 0)
4686             add_fixed_insn (NOP_INSN);
4687           mips_move_text_labels ();
4688         }
4689     }
4690   mips_no_prev_insn ();
4691 }
4692
4693 /* Start a (possibly nested) noreorder block.  */
4694
4695 static void
4696 start_noreorder (void)
4697 {
4698   if (mips_opts.noreorder == 0)
4699     {
4700       unsigned int i;
4701       int nops;
4702
4703       /* None of the instructions before the .set noreorder can be moved.  */
4704       for (i = 0; i < ARRAY_SIZE (history); i++)
4705         history[i].fixed_p = 1;
4706
4707       /* Insert any nops that might be needed between the .set noreorder
4708          block and the previous instructions.  We will later remove any
4709          nops that turn out not to be needed.  */
4710       nops = nops_for_insn (0, history, NULL);
4711       if (nops > 0)
4712         {
4713           if (mips_optimize != 0)
4714             {
4715               /* Record the frag which holds the nop instructions, so
4716                  that we can remove them if we don't need them.  */
4717               frag_grow (nops * NOP_INSN_SIZE);
4718               prev_nop_frag = frag_now;
4719               prev_nop_frag_holds = nops;
4720               prev_nop_frag_required = 0;
4721               prev_nop_frag_since = 0;
4722             }
4723
4724           for (; nops > 0; --nops)
4725             add_fixed_insn (NOP_INSN);
4726
4727           /* Move on to a new frag, so that it is safe to simply
4728              decrease the size of prev_nop_frag.  */
4729           frag_wane (frag_now);
4730           frag_new (0);
4731           mips_move_text_labels ();
4732         }
4733       mips_mark_labels ();
4734       mips_clear_insn_labels ();
4735     }
4736   mips_opts.noreorder++;
4737   mips_any_noreorder = 1;
4738 }
4739
4740 /* End a nested noreorder block.  */
4741
4742 static void
4743 end_noreorder (void)
4744 {
4745   mips_opts.noreorder--;
4746   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4747     {
4748       /* Commit to inserting prev_nop_frag_required nops and go back to
4749          handling nop insertion the .set reorder way.  */
4750       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
4751                                 * NOP_INSN_SIZE);
4752       insert_into_history (prev_nop_frag_since,
4753                            prev_nop_frag_required, NOP_INSN);
4754       prev_nop_frag = NULL;
4755     }
4756 }
4757
4758 /* Set up global variables for the start of a new macro.  */
4759
4760 static void
4761 macro_start (void)
4762 {
4763   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
4764   memset (&mips_macro_warning.first_insn_sizes, 0,
4765           sizeof (mips_macro_warning.first_insn_sizes));
4766   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
4767   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
4768                                      && delayed_branch_p (&history[0]));
4769   switch (history[0].insn_mo->pinfo2
4770           & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4771     {
4772     case INSN2_BRANCH_DELAY_32BIT:
4773       mips_macro_warning.delay_slot_length = 4;
4774       break;
4775     case INSN2_BRANCH_DELAY_16BIT:
4776       mips_macro_warning.delay_slot_length = 2;
4777       break;
4778     default:
4779       mips_macro_warning.delay_slot_length = 0;
4780       break;
4781     }
4782   mips_macro_warning.first_frag = NULL;
4783 }
4784
4785 /* Given that a macro is longer than one instruction or of the wrong size,
4786    return the appropriate warning for it.  Return null if no warning is
4787    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4788    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4789    and RELAX_NOMACRO.  */
4790
4791 static const char *
4792 macro_warning (relax_substateT subtype)
4793 {
4794   if (subtype & RELAX_DELAY_SLOT)
4795     return _("Macro instruction expanded into multiple instructions"
4796              " in a branch delay slot");
4797   else if (subtype & RELAX_NOMACRO)
4798     return _("Macro instruction expanded into multiple instructions");
4799   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4800                       | RELAX_DELAY_SLOT_SIZE_SECOND))
4801     return ((subtype & RELAX_DELAY_SLOT_16BIT)
4802             ? _("Macro instruction expanded into a wrong size instruction"
4803                 " in a 16-bit branch delay slot")
4804             : _("Macro instruction expanded into a wrong size instruction"
4805                 " in a 32-bit branch delay slot"));
4806   else
4807     return 0;
4808 }
4809
4810 /* Finish up a macro.  Emit warnings as appropriate.  */
4811
4812 static void
4813 macro_end (void)
4814 {
4815   /* Relaxation warning flags.  */
4816   relax_substateT subtype = 0;
4817
4818   /* Check delay slot size requirements.  */
4819   if (mips_macro_warning.delay_slot_length == 2)
4820     subtype |= RELAX_DELAY_SLOT_16BIT;
4821   if (mips_macro_warning.delay_slot_length != 0)
4822     {
4823       if (mips_macro_warning.delay_slot_length
4824           != mips_macro_warning.first_insn_sizes[0])
4825         subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4826       if (mips_macro_warning.delay_slot_length
4827           != mips_macro_warning.first_insn_sizes[1])
4828         subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4829     }
4830
4831   /* Check instruction count requirements.  */
4832   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4833     {
4834       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
4835         subtype |= RELAX_SECOND_LONGER;
4836       if (mips_opts.warn_about_macros)
4837         subtype |= RELAX_NOMACRO;
4838       if (mips_macro_warning.delay_slot_p)
4839         subtype |= RELAX_DELAY_SLOT;
4840     }
4841
4842   /* If both alternatives fail to fill a delay slot correctly,
4843      emit the warning now.  */
4844   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4845       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4846     {
4847       relax_substateT s;
4848       const char *msg;
4849
4850       s = subtype & (RELAX_DELAY_SLOT_16BIT
4851                      | RELAX_DELAY_SLOT_SIZE_FIRST
4852                      | RELAX_DELAY_SLOT_SIZE_SECOND);
4853       msg = macro_warning (s);
4854       if (msg != NULL)
4855         as_warn ("%s", msg);
4856       subtype &= ~s;
4857     }
4858
4859   /* If both implementations are longer than 1 instruction, then emit the
4860      warning now.  */
4861   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4862     {
4863       relax_substateT s;
4864       const char *msg;
4865
4866       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4867       msg = macro_warning (s);
4868       if (msg != NULL)
4869         as_warn ("%s", msg);
4870       subtype &= ~s;
4871     }
4872
4873   /* If any flags still set, then one implementation might need a warning
4874      and the other either will need one of a different kind or none at all.
4875      Pass any remaining flags over to relaxation.  */
4876   if (mips_macro_warning.first_frag != NULL)
4877     mips_macro_warning.first_frag->fr_subtype |= subtype;
4878 }
4879
4880 /* Instruction operand formats used in macros that vary between
4881    standard MIPS and microMIPS code.  */
4882
4883 static const char * const brk_fmt[2] = { "c", "mF" };
4884 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4885 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4886 static const char * const lui_fmt[2] = { "t,u", "s,u" };
4887 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4888 static const char * const mfhl_fmt[2] = { "d", "mj" };
4889 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4890 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4891
4892 #define BRK_FMT (brk_fmt[mips_opts.micromips])
4893 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
4894 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
4895 #define LUI_FMT (lui_fmt[mips_opts.micromips])
4896 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4897 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4898 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
4899 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
4900
4901 /* Read a macro's relocation codes from *ARGS and store them in *R.
4902    The first argument in *ARGS will be either the code for a single
4903    relocation or -1 followed by the three codes that make up a
4904    composite relocation.  */
4905
4906 static void
4907 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4908 {
4909   int i, next;
4910
4911   next = va_arg (*args, int);
4912   if (next >= 0)
4913     r[0] = (bfd_reloc_code_real_type) next;
4914   else
4915     for (i = 0; i < 3; i++)
4916       r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4917 }
4918
4919 /* Build an instruction created by a macro expansion.  This is passed
4920    a pointer to the count of instructions created so far, an
4921    expression, the name of the instruction to build, an operand format
4922    string, and corresponding arguments.  */
4923
4924 static void
4925 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
4926 {
4927   const struct mips_opcode *mo = NULL;
4928   bfd_reloc_code_real_type r[3];
4929   const struct mips_opcode *amo;
4930   struct hash_control *hash;
4931   struct mips_cl_insn insn;
4932   va_list args;
4933
4934   va_start (args, fmt);
4935
4936   if (mips_opts.mips16)
4937     {
4938       mips16_macro_build (ep, name, fmt, &args);
4939       va_end (args);
4940       return;
4941     }
4942
4943   r[0] = BFD_RELOC_UNUSED;
4944   r[1] = BFD_RELOC_UNUSED;
4945   r[2] = BFD_RELOC_UNUSED;
4946   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4947   amo = (struct mips_opcode *) hash_find (hash, name);
4948   gas_assert (amo);
4949   gas_assert (strcmp (name, amo->name) == 0);
4950
4951   do
4952     {
4953       /* Search until we get a match for NAME.  It is assumed here that
4954          macros will never generate MDMX, MIPS-3D, or MT instructions.
4955          We try to match an instruction that fulfils the branch delay
4956          slot instruction length requirement (if any) of the previous
4957          instruction.  While doing this we record the first instruction
4958          seen that matches all the other conditions and use it anyway
4959          if the requirement cannot be met; we will issue an appropriate
4960          warning later on.  */
4961       if (strcmp (fmt, amo->args) == 0
4962           && amo->pinfo != INSN_MACRO
4963           && is_opcode_valid (amo)
4964           && is_size_valid (amo))
4965         {
4966           if (is_delay_slot_valid (amo))
4967             {
4968               mo = amo;
4969               break;
4970             }
4971           else if (!mo)
4972             mo = amo;
4973         }
4974
4975       ++amo;
4976       gas_assert (amo->name);
4977     }
4978   while (strcmp (name, amo->name) == 0);
4979
4980   gas_assert (mo);
4981   create_insn (&insn, mo);
4982   for (;;)
4983     {
4984       switch (*fmt++)
4985         {
4986         case '\0':
4987           break;
4988
4989         case ',':
4990         case '(':
4991         case ')':
4992           continue;
4993
4994         case '+':
4995           switch (*fmt++)
4996             {
4997             case 'A':
4998             case 'E':
4999               INSERT_OPERAND (mips_opts.micromips,
5000                               EXTLSB, insn, va_arg (args, int));
5001               continue;
5002
5003             case 'B':
5004             case 'F':
5005               /* Note that in the macro case, these arguments are already
5006                  in MSB form.  (When handling the instruction in the
5007                  non-macro case, these arguments are sizes from which
5008                  MSB values must be calculated.)  */
5009               INSERT_OPERAND (mips_opts.micromips,
5010                               INSMSB, insn, va_arg (args, int));
5011               continue;
5012
5013             case 'C':
5014             case 'G':
5015             case 'H':
5016               /* Note that in the macro case, these arguments are already
5017                  in MSBD form.  (When handling the instruction in the
5018                  non-macro case, these arguments are sizes from which
5019                  MSBD values must be calculated.)  */
5020               INSERT_OPERAND (mips_opts.micromips,
5021                               EXTMSBD, insn, va_arg (args, int));
5022               continue;
5023
5024             case 'Q':
5025               gas_assert (!mips_opts.micromips);
5026               INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
5027               continue;
5028
5029             default:
5030               abort ();
5031             }
5032           continue;
5033
5034         case '2':
5035           INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
5036           continue;
5037
5038         case 'n':
5039           gas_assert (mips_opts.micromips);
5040         case 't':
5041         case 'w':
5042         case 'E':
5043           INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
5044           continue;
5045
5046         case 'c':
5047           gas_assert (!mips_opts.micromips);
5048           INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
5049           continue;
5050
5051         case 'W':
5052           gas_assert (!mips_opts.micromips);
5053         case 'T':
5054           INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
5055           continue;
5056
5057         case 'G':
5058           if (mips_opts.micromips)
5059             INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5060           else
5061             INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5062           continue;
5063
5064         case 'K':
5065           gas_assert (!mips_opts.micromips);
5066         case 'd':
5067           INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
5068           continue;
5069
5070         case 'U':
5071           gas_assert (!mips_opts.micromips);
5072           {
5073             int tmp = va_arg (args, int);
5074
5075             INSERT_OPERAND (0, RT, insn, tmp);
5076             INSERT_OPERAND (0, RD, insn, tmp);
5077           }
5078           continue;
5079
5080         case 'V':
5081         case 'S':
5082           gas_assert (!mips_opts.micromips);
5083           INSERT_OPERAND (0, FS, insn, va_arg (args, int));
5084           continue;
5085
5086         case 'z':
5087           continue;
5088
5089         case '<':
5090           INSERT_OPERAND (mips_opts.micromips,
5091                           SHAMT, insn, va_arg (args, int));
5092           continue;
5093
5094         case 'D':
5095           gas_assert (!mips_opts.micromips);
5096           INSERT_OPERAND (0, FD, insn, va_arg (args, int));
5097           continue;
5098
5099         case 'B':
5100           gas_assert (!mips_opts.micromips);
5101           INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
5102           continue;
5103
5104         case 'J':
5105           gas_assert (!mips_opts.micromips);
5106           INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
5107           continue;
5108
5109         case 'q':
5110           gas_assert (!mips_opts.micromips);
5111           INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
5112           continue;
5113
5114         case 'b':
5115         case 's':
5116         case 'r':
5117         case 'v':
5118           INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
5119           continue;
5120
5121         case 'i':
5122         case 'j':
5123           macro_read_relocs (&args, r);
5124           gas_assert (*r == BFD_RELOC_GPREL16
5125                       || *r == BFD_RELOC_MIPS_HIGHER
5126                       || *r == BFD_RELOC_HI16_S
5127                       || *r == BFD_RELOC_LO16
5128                       || *r == BFD_RELOC_MIPS_GOT_OFST);
5129           continue;
5130
5131         case 'o':
5132           macro_read_relocs (&args, r);
5133           continue;
5134
5135         case 'u':
5136           macro_read_relocs (&args, r);
5137           gas_assert (ep != NULL
5138                       && (ep->X_op == O_constant
5139                           || (ep->X_op == O_symbol
5140                               && (*r == BFD_RELOC_MIPS_HIGHEST
5141                                   || *r == BFD_RELOC_HI16_S
5142                                   || *r == BFD_RELOC_HI16
5143                                   || *r == BFD_RELOC_GPREL16
5144                                   || *r == BFD_RELOC_MIPS_GOT_HI16
5145                                   || *r == BFD_RELOC_MIPS_CALL_HI16))));
5146           continue;
5147
5148         case 'p':
5149           gas_assert (ep != NULL);
5150
5151           /*
5152            * This allows macro() to pass an immediate expression for
5153            * creating short branches without creating a symbol.
5154            *
5155            * We don't allow branch relaxation for these branches, as
5156            * they should only appear in ".set nomacro" anyway.
5157            */
5158           if (ep->X_op == O_constant)
5159             {
5160               /* For microMIPS we always use relocations for branches.
5161                  So we should not resolve immediate values.  */
5162               gas_assert (!mips_opts.micromips);
5163
5164               if ((ep->X_add_number & 3) != 0)
5165                 as_bad (_("branch to misaligned address (0x%lx)"),
5166                         (unsigned long) ep->X_add_number);
5167               if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5168                 as_bad (_("branch address range overflow (0x%lx)"),
5169                         (unsigned long) ep->X_add_number);
5170               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5171               ep = NULL;
5172             }
5173           else
5174             *r = BFD_RELOC_16_PCREL_S2;
5175           continue;
5176
5177         case 'a':
5178           gas_assert (ep != NULL);
5179           *r = BFD_RELOC_MIPS_JMP;
5180           continue;
5181
5182         case 'C':
5183           gas_assert (!mips_opts.micromips);
5184           INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5185           continue;
5186
5187         case 'k':
5188           INSERT_OPERAND (mips_opts.micromips,
5189                           CACHE, insn, va_arg (args, unsigned long));
5190           continue;
5191
5192         case '|':
5193           gas_assert (mips_opts.micromips);
5194           INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5195           continue;
5196
5197         case '.':
5198           gas_assert (mips_opts.micromips);
5199           INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5200           continue;
5201
5202         case '\\':
5203           INSERT_OPERAND (mips_opts.micromips,
5204                           3BITPOS, insn, va_arg (args, unsigned int));
5205           continue;
5206
5207         case '~':
5208           INSERT_OPERAND (mips_opts.micromips,
5209                           OFFSET12, insn, va_arg (args, unsigned long));
5210           continue;
5211
5212         case 'N':
5213           gas_assert (mips_opts.micromips);
5214           INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5215           continue;
5216
5217         case 'm':       /* Opcode extension character.  */
5218           gas_assert (mips_opts.micromips);
5219           switch (*fmt++)
5220             {
5221             case 'j':
5222               INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5223               break;
5224
5225             case 'p':
5226               INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5227               break;
5228
5229             case 'F':
5230               INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5231               break;
5232
5233             default:
5234               abort ();
5235             }
5236           continue;
5237
5238         default:
5239           abort ();
5240         }
5241       break;
5242     }
5243   va_end (args);
5244   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5245
5246   append_insn (&insn, ep, r, TRUE);
5247 }
5248
5249 static void
5250 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5251                     va_list *args)
5252 {
5253   struct mips_opcode *mo;
5254   struct mips_cl_insn insn;
5255   bfd_reloc_code_real_type r[3]
5256     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5257
5258   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5259   gas_assert (mo);
5260   gas_assert (strcmp (name, mo->name) == 0);
5261
5262   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5263     {
5264       ++mo;
5265       gas_assert (mo->name);
5266       gas_assert (strcmp (name, mo->name) == 0);
5267     }
5268
5269   create_insn (&insn, mo);
5270   for (;;)
5271     {
5272       int c;
5273
5274       c = *fmt++;
5275       switch (c)
5276         {
5277         case '\0':
5278           break;
5279
5280         case ',':
5281         case '(':
5282         case ')':
5283           continue;
5284
5285         case 'y':
5286         case 'w':
5287           MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5288           continue;
5289
5290         case 'x':
5291         case 'v':
5292           MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5293           continue;
5294
5295         case 'z':
5296           MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5297           continue;
5298
5299         case 'Z':
5300           MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5301           continue;
5302
5303         case '0':
5304         case 'S':
5305         case 'P':
5306         case 'R':
5307           continue;
5308
5309         case 'X':
5310           MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5311           continue;
5312
5313         case 'Y':
5314           {
5315             int regno;
5316
5317             regno = va_arg (*args, int);
5318             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5319             MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5320           }
5321           continue;
5322
5323         case '<':
5324         case '>':
5325         case '4':
5326         case '5':
5327         case 'H':
5328         case 'W':
5329         case 'D':
5330         case 'j':
5331         case '8':
5332         case 'V':
5333         case 'C':
5334         case 'U':
5335         case 'k':
5336         case 'K':
5337         case 'p':
5338         case 'q':
5339           {
5340             offsetT value;
5341
5342             gas_assert (ep != NULL);
5343
5344             if (ep->X_op != O_constant)
5345               *r = (int) BFD_RELOC_UNUSED + c;
5346             else if (calculate_reloc (*r, ep->X_add_number, &value))
5347               {
5348                 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
5349                 ep = NULL;
5350                 *r = BFD_RELOC_UNUSED;
5351               }
5352           }
5353           continue;
5354
5355         case '6':
5356           MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5357           continue;
5358         }
5359
5360       break;
5361     }
5362
5363   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5364
5365   append_insn (&insn, ep, r, TRUE);
5366 }
5367
5368 /*
5369  * Sign-extend 32-bit mode constants that have bit 31 set and all
5370  * higher bits unset.
5371  */
5372 static void
5373 normalize_constant_expr (expressionS *ex)
5374 {
5375   if (ex->X_op == O_constant
5376       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5377     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5378                         - 0x80000000);
5379 }
5380
5381 /*
5382  * Sign-extend 32-bit mode address offsets that have bit 31 set and
5383  * all higher bits unset.
5384  */
5385 static void
5386 normalize_address_expr (expressionS *ex)
5387 {
5388   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5389         || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5390       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5391     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5392                         - 0x80000000);
5393 }
5394
5395 /*
5396  * Generate a "jalr" instruction with a relocation hint to the called
5397  * function.  This occurs in NewABI PIC code.
5398  */
5399 static void
5400 macro_build_jalr (expressionS *ep, int cprestore)
5401 {
5402   static const bfd_reloc_code_real_type jalr_relocs[2]
5403     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5404   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5405   const char *jalr;
5406   char *f = NULL;
5407
5408   if (MIPS_JALR_HINT_P (ep))
5409     {
5410       frag_grow (8);
5411       f = frag_more (0);
5412     }
5413   if (mips_opts.micromips)
5414     {
5415       jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
5416       if (MIPS_JALR_HINT_P (ep)
5417           || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
5418         macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5419       else
5420         macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5421     }
5422   else
5423     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5424   if (MIPS_JALR_HINT_P (ep))
5425     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5426 }
5427
5428 /*
5429  * Generate a "lui" instruction.
5430  */
5431 static void
5432 macro_build_lui (expressionS *ep, int regnum)
5433 {
5434   gas_assert (! mips_opts.mips16);
5435
5436   if (ep->X_op != O_constant)
5437     {
5438       gas_assert (ep->X_op == O_symbol);
5439       /* _gp_disp is a special case, used from s_cpload.
5440          __gnu_local_gp is used if mips_no_shared.  */
5441       gas_assert (mips_pic == NO_PIC
5442               || (! HAVE_NEWABI
5443                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5444               || (! mips_in_shared
5445                   && strcmp (S_GET_NAME (ep->X_add_symbol),
5446                              "__gnu_local_gp") == 0));
5447     }
5448
5449   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5450 }
5451
5452 /* Generate a sequence of instructions to do a load or store from a constant
5453    offset off of a base register (breg) into/from a target register (treg),
5454    using AT if necessary.  */
5455 static void
5456 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5457                               int treg, int breg, int dbl)
5458 {
5459   gas_assert (ep->X_op == O_constant);
5460
5461   /* Sign-extending 32-bit constants makes their handling easier.  */
5462   if (!dbl)
5463     normalize_constant_expr (ep);
5464
5465   /* Right now, this routine can only handle signed 32-bit constants.  */
5466   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5467     as_warn (_("operand overflow"));
5468
5469   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5470     {
5471       /* Signed 16-bit offset will fit in the op.  Easy!  */
5472       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5473     }
5474   else
5475     {
5476       /* 32-bit offset, need multiple instructions and AT, like:
5477            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
5478            addu     $tempreg,$tempreg,$breg
5479            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
5480          to handle the complete offset.  */
5481       macro_build_lui (ep, AT);
5482       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5483       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5484
5485       if (!mips_opts.at)
5486         as_bad (_("Macro used $at after \".set noat\""));
5487     }
5488 }
5489
5490 /*                      set_at()
5491  * Generates code to set the $at register to true (one)
5492  * if reg is less than the immediate expression.
5493  */
5494 static void
5495 set_at (int reg, int unsignedp)
5496 {
5497   if (imm_expr.X_op == O_constant
5498       && imm_expr.X_add_number >= -0x8000
5499       && imm_expr.X_add_number < 0x8000)
5500     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5501                  AT, reg, BFD_RELOC_LO16);
5502   else
5503     {
5504       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5505       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5506     }
5507 }
5508
5509 /* Warn if an expression is not a constant.  */
5510
5511 static void
5512 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5513 {
5514   if (ex->X_op == O_big)
5515     as_bad (_("unsupported large constant"));
5516   else if (ex->X_op != O_constant)
5517     as_bad (_("Instruction %s requires absolute expression"),
5518             ip->insn_mo->name);
5519
5520   if (HAVE_32BIT_GPRS)
5521     normalize_constant_expr (ex);
5522 }
5523
5524 /* Count the leading zeroes by performing a binary chop. This is a
5525    bulky bit of source, but performance is a LOT better for the
5526    majority of values than a simple loop to count the bits:
5527        for (lcnt = 0; (lcnt < 32); lcnt++)
5528          if ((v) & (1 << (31 - lcnt)))
5529            break;
5530   However it is not code size friendly, and the gain will drop a bit
5531   on certain cached systems.
5532 */
5533 #define COUNT_TOP_ZEROES(v)             \
5534   (((v) & ~0xffff) == 0                 \
5535    ? ((v) & ~0xff) == 0                 \
5536      ? ((v) & ~0xf) == 0                \
5537        ? ((v) & ~0x3) == 0              \
5538          ? ((v) & ~0x1) == 0            \
5539            ? !(v)                       \
5540              ? 32                       \
5541              : 31                       \
5542            : 30                         \
5543          : ((v) & ~0x7) == 0            \
5544            ? 29                         \
5545            : 28                         \
5546        : ((v) & ~0x3f) == 0             \
5547          ? ((v) & ~0x1f) == 0           \
5548            ? 27                         \
5549            : 26                         \
5550          : ((v) & ~0x7f) == 0           \
5551            ? 25                         \
5552            : 24                         \
5553      : ((v) & ~0xfff) == 0              \
5554        ? ((v) & ~0x3ff) == 0            \
5555          ? ((v) & ~0x1ff) == 0          \
5556            ? 23                         \
5557            : 22                         \
5558          : ((v) & ~0x7ff) == 0          \
5559            ? 21                         \
5560            : 20                         \
5561        : ((v) & ~0x3fff) == 0           \
5562          ? ((v) & ~0x1fff) == 0         \
5563            ? 19                         \
5564            : 18                         \
5565          : ((v) & ~0x7fff) == 0         \
5566            ? 17                         \
5567            : 16                         \
5568    : ((v) & ~0xffffff) == 0             \
5569      ? ((v) & ~0xfffff) == 0            \
5570        ? ((v) & ~0x3ffff) == 0          \
5571          ? ((v) & ~0x1ffff) == 0        \
5572            ? 15                         \
5573            : 14                         \
5574          : ((v) & ~0x7ffff) == 0        \
5575            ? 13                         \
5576            : 12                         \
5577        : ((v) & ~0x3fffff) == 0         \
5578          ? ((v) & ~0x1fffff) == 0       \
5579            ? 11                         \
5580            : 10                         \
5581          : ((v) & ~0x7fffff) == 0       \
5582            ? 9                          \
5583            : 8                          \
5584      : ((v) & ~0xfffffff) == 0          \
5585        ? ((v) & ~0x3ffffff) == 0        \
5586          ? ((v) & ~0x1ffffff) == 0      \
5587            ? 7                          \
5588            : 6                          \
5589          : ((v) & ~0x7ffffff) == 0      \
5590            ? 5                          \
5591            : 4                          \
5592        : ((v) & ~0x3fffffff) == 0       \
5593          ? ((v) & ~0x1fffffff) == 0     \
5594            ? 3                          \
5595            : 2                          \
5596          : ((v) & ~0x7fffffff) == 0     \
5597            ? 1                          \
5598            : 0)
5599
5600 /*                      load_register()
5601  *  This routine generates the least number of instructions necessary to load
5602  *  an absolute expression value into a register.
5603  */
5604 static void
5605 load_register (int reg, expressionS *ep, int dbl)
5606 {
5607   int freg;
5608   expressionS hi32, lo32;
5609
5610   if (ep->X_op != O_big)
5611     {
5612       gas_assert (ep->X_op == O_constant);
5613
5614       /* Sign-extending 32-bit constants makes their handling easier.  */
5615       if (!dbl)
5616         normalize_constant_expr (ep);
5617
5618       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5619         {
5620           /* We can handle 16 bit signed values with an addiu to
5621              $zero.  No need to ever use daddiu here, since $zero and
5622              the result are always correct in 32 bit mode.  */
5623           macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5624           return;
5625         }
5626       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5627         {
5628           /* We can handle 16 bit unsigned values with an ori to
5629              $zero.  */
5630           macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5631           return;
5632         }
5633       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
5634         {
5635           /* 32 bit values require an lui.  */
5636           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5637           if ((ep->X_add_number & 0xffff) != 0)
5638             macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5639           return;
5640         }
5641     }
5642
5643   /* The value is larger than 32 bits.  */
5644
5645   if (!dbl || HAVE_32BIT_GPRS)
5646     {
5647       char value[32];
5648
5649       sprintf_vma (value, ep->X_add_number);
5650       as_bad (_("Number (0x%s) larger than 32 bits"), value);
5651       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5652       return;
5653     }
5654
5655   if (ep->X_op != O_big)
5656     {
5657       hi32 = *ep;
5658       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5659       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5660       hi32.X_add_number &= 0xffffffff;
5661       lo32 = *ep;
5662       lo32.X_add_number &= 0xffffffff;
5663     }
5664   else
5665     {
5666       gas_assert (ep->X_add_number > 2);
5667       if (ep->X_add_number == 3)
5668         generic_bignum[3] = 0;
5669       else if (ep->X_add_number > 4)
5670         as_bad (_("Number larger than 64 bits"));
5671       lo32.X_op = O_constant;
5672       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5673       hi32.X_op = O_constant;
5674       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5675     }
5676
5677   if (hi32.X_add_number == 0)
5678     freg = 0;
5679   else
5680     {
5681       int shift, bit;
5682       unsigned long hi, lo;
5683
5684       if (hi32.X_add_number == (offsetT) 0xffffffff)
5685         {
5686           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5687             {
5688               macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5689               return;
5690             }
5691           if (lo32.X_add_number & 0x80000000)
5692             {
5693               macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5694               if (lo32.X_add_number & 0xffff)
5695                 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5696               return;
5697             }
5698         }
5699
5700       /* Check for 16bit shifted constant.  We know that hi32 is
5701          non-zero, so start the mask on the first bit of the hi32
5702          value.  */
5703       shift = 17;
5704       do
5705         {
5706           unsigned long himask, lomask;
5707
5708           if (shift < 32)
5709             {
5710               himask = 0xffff >> (32 - shift);
5711               lomask = (0xffff << shift) & 0xffffffff;
5712             }
5713           else
5714             {
5715               himask = 0xffff << (shift - 32);
5716               lomask = 0;
5717             }
5718           if ((hi32.X_add_number & ~(offsetT) himask) == 0
5719               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5720             {
5721               expressionS tmp;
5722
5723               tmp.X_op = O_constant;
5724               if (shift < 32)
5725                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5726                                     | (lo32.X_add_number >> shift));
5727               else
5728                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
5729               macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5730               macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5731                            reg, reg, (shift >= 32) ? shift - 32 : shift);
5732               return;
5733             }
5734           ++shift;
5735         }
5736       while (shift <= (64 - 16));
5737
5738       /* Find the bit number of the lowest one bit, and store the
5739          shifted value in hi/lo.  */
5740       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5741       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5742       if (lo != 0)
5743         {
5744           bit = 0;
5745           while ((lo & 1) == 0)
5746             {
5747               lo >>= 1;
5748               ++bit;
5749             }
5750           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5751           hi >>= bit;
5752         }
5753       else
5754         {
5755           bit = 32;
5756           while ((hi & 1) == 0)
5757             {
5758               hi >>= 1;
5759               ++bit;
5760             }
5761           lo = hi;
5762           hi = 0;
5763         }
5764
5765       /* Optimize if the shifted value is a (power of 2) - 1.  */
5766       if ((hi == 0 && ((lo + 1) & lo) == 0)
5767           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
5768         {
5769           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
5770           if (shift != 0)
5771             {
5772               expressionS tmp;
5773
5774               /* This instruction will set the register to be all
5775                  ones.  */
5776               tmp.X_op = O_constant;
5777               tmp.X_add_number = (offsetT) -1;
5778               macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5779               if (bit != 0)
5780                 {
5781                   bit += shift;
5782                   macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5783                                reg, reg, (bit >= 32) ? bit - 32 : bit);
5784                 }
5785               macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
5786                            reg, reg, (shift >= 32) ? shift - 32 : shift);
5787               return;
5788             }
5789         }
5790
5791       /* Sign extend hi32 before calling load_register, because we can
5792          generally get better code when we load a sign extended value.  */
5793       if ((hi32.X_add_number & 0x80000000) != 0)
5794         hi32.X_add_number |= ~(offsetT) 0xffffffff;
5795       load_register (reg, &hi32, 0);
5796       freg = reg;
5797     }
5798   if ((lo32.X_add_number & 0xffff0000) == 0)
5799     {
5800       if (freg != 0)
5801         {
5802           macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
5803           freg = reg;
5804         }
5805     }
5806   else
5807     {
5808       expressionS mid16;
5809
5810       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
5811         {
5812           macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5813           macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
5814           return;
5815         }
5816
5817       if (freg != 0)
5818         {
5819           macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
5820           freg = reg;
5821         }
5822       mid16 = lo32;
5823       mid16.X_add_number >>= 16;
5824       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5825       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5826       freg = reg;
5827     }
5828   if ((lo32.X_add_number & 0xffff) != 0)
5829     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5830 }
5831
5832 static inline void
5833 load_delay_nop (void)
5834 {
5835   if (!gpr_interlocks)
5836     macro_build (NULL, "nop", "");
5837 }
5838
5839 /* Load an address into a register.  */
5840
5841 static void
5842 load_address (int reg, expressionS *ep, int *used_at)
5843 {
5844   if (ep->X_op != O_constant
5845       && ep->X_op != O_symbol)
5846     {
5847       as_bad (_("expression too complex"));
5848       ep->X_op = O_constant;
5849     }
5850
5851   if (ep->X_op == O_constant)
5852     {
5853       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
5854       return;
5855     }
5856
5857   if (mips_pic == NO_PIC)
5858     {
5859       /* If this is a reference to a GP relative symbol, we want
5860            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
5861          Otherwise we want
5862            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
5863            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
5864          If we have an addend, we always use the latter form.
5865
5866          With 64bit address space and a usable $at we want
5867            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
5868            lui          $at,<sym>               (BFD_RELOC_HI16_S)
5869            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
5870            daddiu       $at,<sym>               (BFD_RELOC_LO16)
5871            dsll32       $reg,0
5872            daddu        $reg,$reg,$at
5873
5874          If $at is already in use, we use a path which is suboptimal
5875          on superscalar processors.
5876            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
5877            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
5878            dsll         $reg,16
5879            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
5880            dsll         $reg,16
5881            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
5882
5883          For GP relative symbols in 64bit address space we can use
5884          the same sequence as in 32bit address space.  */
5885       if (HAVE_64BIT_SYMBOLS)
5886         {
5887           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5888               && !nopic_need_relax (ep->X_add_symbol, 1))
5889             {
5890               relax_start (ep->X_add_symbol);
5891               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5892                            mips_gp_register, BFD_RELOC_GPREL16);
5893               relax_switch ();
5894             }
5895
5896           if (*used_at == 0 && mips_opts.at)
5897             {
5898               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5899               macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
5900               macro_build (ep, "daddiu", "t,r,j", reg, reg,
5901                            BFD_RELOC_MIPS_HIGHER);
5902               macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
5903               macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
5904               macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
5905               *used_at = 1;
5906             }
5907           else
5908             {
5909               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5910               macro_build (ep, "daddiu", "t,r,j", reg, reg,
5911                            BFD_RELOC_MIPS_HIGHER);
5912               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5913               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
5914               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5915               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
5916             }
5917
5918           if (mips_relax.sequence)
5919             relax_end ();
5920         }
5921       else
5922         {
5923           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5924               && !nopic_need_relax (ep->X_add_symbol, 1))
5925             {
5926               relax_start (ep->X_add_symbol);
5927               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5928                            mips_gp_register, BFD_RELOC_GPREL16);
5929               relax_switch ();
5930             }
5931           macro_build_lui (ep, reg);
5932           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5933                        reg, reg, BFD_RELOC_LO16);
5934           if (mips_relax.sequence)
5935             relax_end ();
5936         }
5937     }
5938   else if (!mips_big_got)
5939     {
5940       expressionS ex;
5941
5942       /* If this is a reference to an external symbol, we want
5943            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
5944          Otherwise we want
5945            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
5946            nop
5947            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
5948          If there is a constant, it must be added in after.
5949
5950          If we have NewABI, we want
5951            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5952          unless we're referencing a global symbol with a non-zero
5953          offset, in which case cst must be added separately.  */
5954       if (HAVE_NEWABI)
5955         {
5956           if (ep->X_add_number)
5957             {
5958               ex.X_add_number = ep->X_add_number;
5959               ep->X_add_number = 0;
5960               relax_start (ep->X_add_symbol);
5961               macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5962                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5963               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5964                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5965               ex.X_op = O_constant;
5966               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5967                            reg, reg, BFD_RELOC_LO16);
5968               ep->X_add_number = ex.X_add_number;
5969               relax_switch ();
5970             }
5971           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5972                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5973           if (mips_relax.sequence)
5974             relax_end ();
5975         }
5976       else
5977         {
5978           ex.X_add_number = ep->X_add_number;
5979           ep->X_add_number = 0;
5980           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5981                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
5982           load_delay_nop ();
5983           relax_start (ep->X_add_symbol);
5984           relax_switch ();
5985           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5986                        BFD_RELOC_LO16);
5987           relax_end ();
5988
5989           if (ex.X_add_number != 0)
5990             {
5991               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5992                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5993               ex.X_op = O_constant;
5994               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5995                            reg, reg, BFD_RELOC_LO16);
5996             }
5997         }
5998     }
5999   else if (mips_big_got)
6000     {
6001       expressionS ex;
6002
6003       /* This is the large GOT case.  If this is a reference to an
6004          external symbol, we want
6005            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
6006            addu         $reg,$reg,$gp
6007            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
6008
6009          Otherwise, for a reference to a local symbol in old ABI, we want
6010            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
6011            nop
6012            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
6013          If there is a constant, it must be added in after.
6014
6015          In the NewABI, for local symbols, with or without offsets, we want:
6016            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
6017            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
6018       */
6019       if (HAVE_NEWABI)
6020         {
6021           ex.X_add_number = ep->X_add_number;
6022           ep->X_add_number = 0;
6023           relax_start (ep->X_add_symbol);
6024           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6025           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6026                        reg, reg, mips_gp_register);
6027           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6028                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6029           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6030             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6031           else if (ex.X_add_number)
6032             {
6033               ex.X_op = O_constant;
6034               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6035                            BFD_RELOC_LO16);
6036             }
6037
6038           ep->X_add_number = ex.X_add_number;
6039           relax_switch ();
6040           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6041                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6042           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6043                        BFD_RELOC_MIPS_GOT_OFST);
6044           relax_end ();
6045         }
6046       else
6047         {
6048           ex.X_add_number = ep->X_add_number;
6049           ep->X_add_number = 0;
6050           relax_start (ep->X_add_symbol);
6051           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6052           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6053                        reg, reg, mips_gp_register);
6054           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6055                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6056           relax_switch ();
6057           if (reg_needs_delay (mips_gp_register))
6058             {
6059               /* We need a nop before loading from $gp.  This special
6060                  check is required because the lui which starts the main
6061                  instruction stream does not refer to $gp, and so will not
6062                  insert the nop which may be required.  */
6063               macro_build (NULL, "nop", "");
6064             }
6065           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6066                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
6067           load_delay_nop ();
6068           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6069                        BFD_RELOC_LO16);
6070           relax_end ();
6071
6072           if (ex.X_add_number != 0)
6073             {
6074               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6075                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6076               ex.X_op = O_constant;
6077               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6078                            BFD_RELOC_LO16);
6079             }
6080         }
6081     }
6082   else
6083     abort ();
6084
6085   if (!mips_opts.at && *used_at == 1)
6086     as_bad (_("Macro used $at after \".set noat\""));
6087 }
6088
6089 /* Move the contents of register SOURCE into register DEST.  */
6090
6091 static void
6092 move_register (int dest, int source)
6093 {
6094   /* Prefer to use a 16-bit microMIPS instruction unless the previous
6095      instruction specifically requires a 32-bit one.  */
6096   if (mips_opts.micromips
6097       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
6098     macro_build (NULL, "move", "mp,mj", dest, source);
6099   else
6100     macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6101                  dest, source, 0);
6102 }
6103
6104 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
6105    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6106    The two alternatives are:
6107
6108    Global symbol                Local sybmol
6109    -------------                ------------
6110    lw DEST,%got(SYMBOL)         lw DEST,%got(SYMBOL + OFFSET)
6111    ...                          ...
6112    addiu DEST,DEST,OFFSET       addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6113
6114    load_got_offset emits the first instruction and add_got_offset
6115    emits the second for a 16-bit offset or add_got_offset_hilo emits
6116    a sequence to add a 32-bit offset using a scratch register.  */
6117
6118 static void
6119 load_got_offset (int dest, expressionS *local)
6120 {
6121   expressionS global;
6122
6123   global = *local;
6124   global.X_add_number = 0;
6125
6126   relax_start (local->X_add_symbol);
6127   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6128                BFD_RELOC_MIPS_GOT16, mips_gp_register);
6129   relax_switch ();
6130   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6131                BFD_RELOC_MIPS_GOT16, mips_gp_register);
6132   relax_end ();
6133 }
6134
6135 static void
6136 add_got_offset (int dest, expressionS *local)
6137 {
6138   expressionS global;
6139
6140   global.X_op = O_constant;
6141   global.X_op_symbol = NULL;
6142   global.X_add_symbol = NULL;
6143   global.X_add_number = local->X_add_number;
6144
6145   relax_start (local->X_add_symbol);
6146   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6147                dest, dest, BFD_RELOC_LO16);
6148   relax_switch ();
6149   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6150   relax_end ();
6151 }
6152
6153 static void
6154 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6155 {
6156   expressionS global;
6157   int hold_mips_optimize;
6158
6159   global.X_op = O_constant;
6160   global.X_op_symbol = NULL;
6161   global.X_add_symbol = NULL;
6162   global.X_add_number = local->X_add_number;
6163
6164   relax_start (local->X_add_symbol);
6165   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6166   relax_switch ();
6167   /* Set mips_optimize around the lui instruction to avoid
6168      inserting an unnecessary nop after the lw.  */
6169   hold_mips_optimize = mips_optimize;
6170   mips_optimize = 2;
6171   macro_build_lui (&global, tmp);
6172   mips_optimize = hold_mips_optimize;
6173   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6174   relax_end ();
6175
6176   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6177 }
6178
6179 /* Emit a sequence of instructions to emulate a branch likely operation.
6180    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
6181    is its complementing branch with the original condition negated.
6182    CALL is set if the original branch specified the link operation.
6183    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6184
6185    Code like this is produced in the noreorder mode:
6186
6187         BRNEG   <args>, 1f
6188          nop
6189         b       <sym>
6190          delay slot (executed only if branch taken)
6191     1:
6192
6193    or, if CALL is set:
6194
6195         BRNEG   <args>, 1f
6196          nop
6197         bal     <sym>
6198          delay slot (executed only if branch taken)
6199     1:
6200
6201    In the reorder mode the delay slot would be filled with a nop anyway,
6202    so code produced is simply:
6203
6204         BR      <args>, <sym>
6205          nop
6206
6207    This function is used when producing code for the microMIPS ASE that
6208    does not implement branch likely instructions in hardware.  */
6209
6210 static void
6211 macro_build_branch_likely (const char *br, const char *brneg,
6212                            int call, expressionS *ep, const char *fmt,
6213                            unsigned int sreg, unsigned int treg)
6214 {
6215   int noreorder = mips_opts.noreorder;
6216   expressionS expr1;
6217
6218   gas_assert (mips_opts.micromips);
6219   start_noreorder ();
6220   if (noreorder)
6221     {
6222       micromips_label_expr (&expr1);
6223       macro_build (&expr1, brneg, fmt, sreg, treg);
6224       macro_build (NULL, "nop", "");
6225       macro_build (ep, call ? "bal" : "b", "p");
6226
6227       /* Set to true so that append_insn adds a label.  */
6228       emit_branch_likely_macro = TRUE;
6229     }
6230   else
6231     {
6232       macro_build (ep, br, fmt, sreg, treg);
6233       macro_build (NULL, "nop", "");
6234     }
6235   end_noreorder ();
6236 }
6237
6238 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6239    the condition code tested.  EP specifies the branch target.  */
6240
6241 static void
6242 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6243 {
6244   const int call = 0;
6245   const char *brneg;
6246   const char *br;
6247
6248   switch (type)
6249     {
6250     case M_BC1FL:
6251       br = "bc1f";
6252       brneg = "bc1t";
6253       break;
6254     case M_BC1TL:
6255       br = "bc1t";
6256       brneg = "bc1f";
6257       break;
6258     case M_BC2FL:
6259       br = "bc2f";
6260       brneg = "bc2t";
6261       break;
6262     case M_BC2TL:
6263       br = "bc2t";
6264       brneg = "bc2f";
6265       break;
6266     default:
6267       abort ();
6268     }
6269   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6270 }
6271
6272 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6273    the register tested.  EP specifies the branch target.  */
6274
6275 static void
6276 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6277 {
6278   const char *brneg = NULL;
6279   const char *br;
6280   int call = 0;
6281
6282   switch (type)
6283     {
6284     case M_BGEZ:
6285       br = "bgez";
6286       break;
6287     case M_BGEZL:
6288       br = mips_opts.micromips ? "bgez" : "bgezl";
6289       brneg = "bltz";
6290       break;
6291     case M_BGEZALL:
6292       gas_assert (mips_opts.micromips);
6293       br = "bgezals";
6294       brneg = "bltz";
6295       call = 1;
6296       break;
6297     case M_BGTZ:
6298       br = "bgtz";
6299       break;
6300     case M_BGTZL:
6301       br = mips_opts.micromips ? "bgtz" : "bgtzl";
6302       brneg = "blez";
6303       break;
6304     case M_BLEZ:
6305       br = "blez";
6306       break;
6307     case M_BLEZL:
6308       br = mips_opts.micromips ? "blez" : "blezl";
6309       brneg = "bgtz";
6310       break;
6311     case M_BLTZ:
6312       br = "bltz";
6313       break;
6314     case M_BLTZL:
6315       br = mips_opts.micromips ? "bltz" : "bltzl";
6316       brneg = "bgez";
6317       break;
6318     case M_BLTZALL:
6319       gas_assert (mips_opts.micromips);
6320       br = "bltzals";
6321       brneg = "bgez";
6322       call = 1;
6323       break;
6324     default:
6325       abort ();
6326     }
6327   if (mips_opts.micromips && brneg)
6328     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6329   else
6330     macro_build (ep, br, "s,p", sreg);
6331 }
6332
6333 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6334    TREG as the registers tested.  EP specifies the branch target.  */
6335
6336 static void
6337 macro_build_branch_rsrt (int type, expressionS *ep,
6338                          unsigned int sreg, unsigned int treg)
6339 {
6340   const char *brneg = NULL;
6341   const int call = 0;
6342   const char *br;
6343
6344   switch (type)
6345     {
6346     case M_BEQ:
6347     case M_BEQ_I:
6348       br = "beq";
6349       break;
6350     case M_BEQL:
6351     case M_BEQL_I:
6352       br = mips_opts.micromips ? "beq" : "beql";
6353       brneg = "bne";
6354       break;
6355     case M_BNE:
6356     case M_BNE_I:
6357       br = "bne";
6358       break;
6359     case M_BNEL:
6360     case M_BNEL_I:
6361       br = mips_opts.micromips ? "bne" : "bnel";
6362       brneg = "beq";
6363       break;
6364     default:
6365       abort ();
6366     }
6367   if (mips_opts.micromips && brneg)
6368     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6369   else
6370     macro_build (ep, br, "s,t,p", sreg, treg);
6371 }
6372
6373 /*
6374  *                      Build macros
6375  *   This routine implements the seemingly endless macro or synthesized
6376  * instructions and addressing modes in the mips assembly language. Many
6377  * of these macros are simple and are similar to each other. These could
6378  * probably be handled by some kind of table or grammar approach instead of
6379  * this verbose method. Others are not simple macros but are more like
6380  * optimizing code generation.
6381  *   One interesting optimization is when several store macros appear
6382  * consecutively that would load AT with the upper half of the same address.
6383  * The ensuing load upper instructions are ommited. This implies some kind
6384  * of global optimization. We currently only optimize within a single macro.
6385  *   For many of the load and store macros if the address is specified as a
6386  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6387  * first load register 'at' with zero and use it as the base register. The
6388  * mips assembler simply uses register $zero. Just one tiny optimization
6389  * we're missing.
6390  */
6391 static void
6392 macro (struct mips_cl_insn *ip)
6393 {
6394   unsigned int treg, sreg, dreg, breg;
6395   unsigned int tempreg;
6396   int mask;
6397   int used_at = 0;
6398   expressionS label_expr;
6399   expressionS expr1;
6400   expressionS *ep;
6401   const char *s;
6402   const char *s2;
6403   const char *fmt;
6404   int likely = 0;
6405   int coproc = 0;
6406   int off12 = 0;
6407   int call = 0;
6408   int jals = 0;
6409   int dbl = 0;
6410   int imm = 0;
6411   int ust = 0;
6412   int lp = 0;
6413   int ab = 0;
6414   int off0 = 0;
6415   int off;
6416   offsetT maxnum;
6417   bfd_reloc_code_real_type r;
6418   int hold_mips_optimize;
6419
6420   gas_assert (! mips_opts.mips16);
6421
6422   treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6423   dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6424   sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6425   mask = ip->insn_mo->mask;
6426
6427   label_expr.X_op = O_constant;
6428   label_expr.X_op_symbol = NULL;
6429   label_expr.X_add_symbol = NULL;
6430   label_expr.X_add_number = 0;
6431
6432   expr1.X_op = O_constant;
6433   expr1.X_op_symbol = NULL;
6434   expr1.X_add_symbol = NULL;
6435   expr1.X_add_number = 1;
6436
6437   switch (mask)
6438     {
6439     case M_DABS:
6440       dbl = 1;
6441     case M_ABS:
6442       /*    bgez    $a0,1f
6443             move    v0,$a0
6444             sub     v0,$zero,$a0
6445          1:
6446        */
6447
6448       start_noreorder ();
6449
6450       if (mips_opts.micromips)
6451         micromips_label_expr (&label_expr);
6452       else
6453         label_expr.X_add_number = 8;
6454       macro_build (&label_expr, "bgez", "s,p", sreg);
6455       if (dreg == sreg)
6456         macro_build (NULL, "nop", "");
6457       else
6458         move_register (dreg, sreg);
6459       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6460       if (mips_opts.micromips)
6461         micromips_add_label ();
6462
6463       end_noreorder ();
6464       break;
6465
6466     case M_ADD_I:
6467       s = "addi";
6468       s2 = "add";
6469       goto do_addi;
6470     case M_ADDU_I:
6471       s = "addiu";
6472       s2 = "addu";
6473       goto do_addi;
6474     case M_DADD_I:
6475       dbl = 1;
6476       s = "daddi";
6477       s2 = "dadd";
6478       if (!mips_opts.micromips)
6479         goto do_addi;
6480       if (imm_expr.X_op == O_constant
6481           && imm_expr.X_add_number >= -0x200
6482           && imm_expr.X_add_number < 0x200)
6483         {
6484           macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6485           break;
6486         }
6487       goto do_addi_i;
6488     case M_DADDU_I:
6489       dbl = 1;
6490       s = "daddiu";
6491       s2 = "daddu";
6492     do_addi:
6493       if (imm_expr.X_op == O_constant
6494           && imm_expr.X_add_number >= -0x8000
6495           && imm_expr.X_add_number < 0x8000)
6496         {
6497           macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6498           break;
6499         }
6500     do_addi_i:
6501       used_at = 1;
6502       load_register (AT, &imm_expr, dbl);
6503       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6504       break;
6505
6506     case M_AND_I:
6507       s = "andi";
6508       s2 = "and";
6509       goto do_bit;
6510     case M_OR_I:
6511       s = "ori";
6512       s2 = "or";
6513       goto do_bit;
6514     case M_NOR_I:
6515       s = "";
6516       s2 = "nor";
6517       goto do_bit;
6518     case M_XOR_I:
6519       s = "xori";
6520       s2 = "xor";
6521     do_bit:
6522       if (imm_expr.X_op == O_constant
6523           && imm_expr.X_add_number >= 0
6524           && imm_expr.X_add_number < 0x10000)
6525         {
6526           if (mask != M_NOR_I)
6527             macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6528           else
6529             {
6530               macro_build (&imm_expr, "ori", "t,r,i",
6531                            treg, sreg, BFD_RELOC_LO16);
6532               macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6533             }
6534           break;
6535         }
6536
6537       used_at = 1;
6538       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6539       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6540       break;
6541
6542     case M_BALIGN:
6543       switch (imm_expr.X_add_number)
6544         {
6545         case 0:
6546           macro_build (NULL, "nop", "");
6547           break;
6548         case 2:
6549           macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6550           break;
6551         case 1:
6552         case 3:
6553           macro_build (NULL, "balign", "t,s,2", treg, sreg,
6554                        (int) imm_expr.X_add_number);
6555           break;
6556         default:
6557           as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6558                   (unsigned long) imm_expr.X_add_number);
6559           break;
6560         }
6561       break;
6562
6563     case M_BC1FL:
6564     case M_BC1TL:
6565     case M_BC2FL:
6566     case M_BC2TL:
6567       gas_assert (mips_opts.micromips);
6568       macro_build_branch_ccl (mask, &offset_expr,
6569                               EXTRACT_OPERAND (1, BCC, *ip));
6570       break;
6571
6572     case M_BEQ_I:
6573     case M_BEQL_I:
6574     case M_BNE_I:
6575     case M_BNEL_I:
6576       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6577         treg = 0;
6578       else
6579         {
6580           treg = AT;
6581           used_at = 1;
6582           load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
6583         }
6584       /* Fall through.  */
6585     case M_BEQL:
6586     case M_BNEL:
6587       macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
6588       break;
6589
6590     case M_BGEL:
6591       likely = 1;
6592     case M_BGE:
6593       if (treg == 0)
6594         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6595       else if (sreg == 0)
6596         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6597       else
6598         {
6599           used_at = 1;
6600           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6601           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6602                                    &offset_expr, AT, ZERO);
6603         }
6604       break;
6605
6606     case M_BGEZL:
6607     case M_BGEZALL:
6608     case M_BGTZL:
6609     case M_BLEZL:
6610     case M_BLTZL:
6611     case M_BLTZALL:
6612       macro_build_branch_rs (mask, &offset_expr, sreg);
6613       break;
6614
6615     case M_BGTL_I:
6616       likely = 1;
6617     case M_BGT_I:
6618       /* Check for > max integer.  */
6619       maxnum = 0x7fffffff;
6620       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6621         {
6622           maxnum <<= 16;
6623           maxnum |= 0xffff;
6624           maxnum <<= 16;
6625           maxnum |= 0xffff;
6626         }
6627       if (imm_expr.X_op == O_constant
6628           && imm_expr.X_add_number >= maxnum
6629           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6630         {
6631         do_false:
6632           /* Result is always false.  */
6633           if (! likely)
6634             macro_build (NULL, "nop", "");
6635           else
6636             macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
6637           break;
6638         }
6639       if (imm_expr.X_op != O_constant)
6640         as_bad (_("Unsupported large constant"));
6641       ++imm_expr.X_add_number;
6642       /* FALLTHROUGH */
6643     case M_BGE_I:
6644     case M_BGEL_I:
6645       if (mask == M_BGEL_I)
6646         likely = 1;
6647       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6648         {
6649           macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6650                                  &offset_expr, sreg);
6651           break;
6652         }
6653       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6654         {
6655           macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6656                                  &offset_expr, sreg);
6657           break;
6658         }
6659       maxnum = 0x7fffffff;
6660       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6661         {
6662           maxnum <<= 16;
6663           maxnum |= 0xffff;
6664           maxnum <<= 16;
6665           maxnum |= 0xffff;
6666         }
6667       maxnum = - maxnum - 1;
6668       if (imm_expr.X_op == O_constant
6669           && imm_expr.X_add_number <= maxnum
6670           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6671         {
6672         do_true:
6673           /* result is always true */
6674           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
6675           macro_build (&offset_expr, "b", "p");
6676           break;
6677         }
6678       used_at = 1;
6679       set_at (sreg, 0);
6680       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6681                                &offset_expr, AT, ZERO);
6682       break;
6683
6684     case M_BGEUL:
6685       likely = 1;
6686     case M_BGEU:
6687       if (treg == 0)
6688         goto do_true;
6689       else if (sreg == 0)
6690         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6691                                  &offset_expr, ZERO, treg);
6692       else
6693         {
6694           used_at = 1;
6695           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6696           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6697                                    &offset_expr, AT, ZERO);
6698         }
6699       break;
6700
6701     case M_BGTUL_I:
6702       likely = 1;
6703     case M_BGTU_I:
6704       if (sreg == 0
6705           || (HAVE_32BIT_GPRS
6706               && imm_expr.X_op == O_constant
6707               && imm_expr.X_add_number == -1))
6708         goto do_false;
6709       if (imm_expr.X_op != O_constant)
6710         as_bad (_("Unsupported large constant"));
6711       ++imm_expr.X_add_number;
6712       /* FALLTHROUGH */
6713     case M_BGEU_I:
6714     case M_BGEUL_I:
6715       if (mask == M_BGEUL_I)
6716         likely = 1;
6717       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6718         goto do_true;
6719       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6720         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6721                                  &offset_expr, sreg, ZERO);
6722       else
6723         {
6724           used_at = 1;
6725           set_at (sreg, 1);
6726           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6727                                    &offset_expr, AT, ZERO);
6728         }
6729       break;
6730
6731     case M_BGTL:
6732       likely = 1;
6733     case M_BGT:
6734       if (treg == 0)
6735         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6736       else if (sreg == 0)
6737         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6738       else
6739         {
6740           used_at = 1;
6741           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6742           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6743                                    &offset_expr, AT, ZERO);
6744         }
6745       break;
6746
6747     case M_BGTUL:
6748       likely = 1;
6749     case M_BGTU:
6750       if (treg == 0)
6751         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6752                                  &offset_expr, sreg, ZERO);
6753       else if (sreg == 0)
6754         goto do_false;
6755       else
6756         {
6757           used_at = 1;
6758           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6759           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6760                                    &offset_expr, AT, ZERO);
6761         }
6762       break;
6763
6764     case M_BLEL:
6765       likely = 1;
6766     case M_BLE:
6767       if (treg == 0)
6768         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6769       else if (sreg == 0)
6770         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6771       else
6772         {
6773           used_at = 1;
6774           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6775           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6776                                    &offset_expr, AT, ZERO);
6777         }
6778       break;
6779
6780     case M_BLEL_I:
6781       likely = 1;
6782     case M_BLE_I:
6783       maxnum = 0x7fffffff;
6784       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6785         {
6786           maxnum <<= 16;
6787           maxnum |= 0xffff;
6788           maxnum <<= 16;
6789           maxnum |= 0xffff;
6790         }
6791       if (imm_expr.X_op == O_constant
6792           && imm_expr.X_add_number >= maxnum
6793           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6794         goto do_true;
6795       if (imm_expr.X_op != O_constant)
6796         as_bad (_("Unsupported large constant"));
6797       ++imm_expr.X_add_number;
6798       /* FALLTHROUGH */
6799     case M_BLT_I:
6800     case M_BLTL_I:
6801       if (mask == M_BLTL_I)
6802         likely = 1;
6803       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6804         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6805       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6806         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6807       else
6808         {
6809           used_at = 1;
6810           set_at (sreg, 0);
6811           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6812                                    &offset_expr, AT, ZERO);
6813         }
6814       break;
6815
6816     case M_BLEUL:
6817       likely = 1;
6818     case M_BLEU:
6819       if (treg == 0)
6820         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6821                                  &offset_expr, sreg, ZERO);
6822       else if (sreg == 0)
6823         goto do_true;
6824       else
6825         {
6826           used_at = 1;
6827           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6828           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6829                                    &offset_expr, AT, ZERO);
6830         }
6831       break;
6832
6833     case M_BLEUL_I:
6834       likely = 1;
6835     case M_BLEU_I:
6836       if (sreg == 0
6837           || (HAVE_32BIT_GPRS
6838               && imm_expr.X_op == O_constant
6839               && imm_expr.X_add_number == -1))
6840         goto do_true;
6841       if (imm_expr.X_op != O_constant)
6842         as_bad (_("Unsupported large constant"));
6843       ++imm_expr.X_add_number;
6844       /* FALLTHROUGH */
6845     case M_BLTU_I:
6846     case M_BLTUL_I:
6847       if (mask == M_BLTUL_I)
6848         likely = 1;
6849       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6850         goto do_false;
6851       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6852         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6853                                  &offset_expr, sreg, ZERO);
6854       else
6855         {
6856           used_at = 1;
6857           set_at (sreg, 1);
6858           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6859                                    &offset_expr, AT, ZERO);
6860         }
6861       break;
6862
6863     case M_BLTL:
6864       likely = 1;
6865     case M_BLT:
6866       if (treg == 0)
6867         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6868       else if (sreg == 0)
6869         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6870       else
6871         {
6872           used_at = 1;
6873           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6874           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6875                                    &offset_expr, AT, ZERO);
6876         }
6877       break;
6878
6879     case M_BLTUL:
6880       likely = 1;
6881     case M_BLTU:
6882       if (treg == 0)
6883         goto do_false;
6884       else if (sreg == 0)
6885         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6886                                  &offset_expr, ZERO, treg);
6887       else
6888         {
6889           used_at = 1;
6890           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6891           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6892                                    &offset_expr, AT, ZERO);
6893         }
6894       break;
6895
6896     case M_DEXT:
6897       {
6898         /* Use unsigned arithmetic.  */
6899         addressT pos;
6900         addressT size;
6901
6902         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6903           {
6904             as_bad (_("Unsupported large constant"));
6905             pos = size = 1;
6906           }
6907         else
6908           {
6909             pos = imm_expr.X_add_number;
6910             size = imm2_expr.X_add_number;
6911           }
6912
6913         if (pos > 63)
6914           {
6915             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6916             pos = 1;
6917           }
6918         if (size == 0 || size > 64 || (pos + size - 1) > 63)
6919           {
6920             as_bad (_("Improper extract size (%lu, position %lu)"),
6921                     (unsigned long) size, (unsigned long) pos);
6922             size = 1;
6923           }
6924
6925         if (size <= 32 && pos < 32)
6926           {
6927             s = "dext";
6928             fmt = "t,r,+A,+C";
6929           }
6930         else if (size <= 32)
6931           {
6932             s = "dextu";
6933             fmt = "t,r,+E,+H";
6934           }
6935         else
6936           {
6937             s = "dextm";
6938             fmt = "t,r,+A,+G";
6939           }
6940         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6941                      (int) (size - 1));
6942       }
6943       break;
6944
6945     case M_DINS:
6946       {
6947         /* Use unsigned arithmetic.  */
6948         addressT pos;
6949         addressT size;
6950
6951         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6952           {
6953             as_bad (_("Unsupported large constant"));
6954             pos = size = 1;
6955           }
6956         else
6957           {
6958             pos = imm_expr.X_add_number;
6959             size = imm2_expr.X_add_number;
6960           }
6961
6962         if (pos > 63)
6963           {
6964             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6965             pos = 1;
6966           }
6967         if (size == 0 || size > 64 || (pos + size - 1) > 63)
6968           {
6969             as_bad (_("Improper insert size (%lu, position %lu)"),
6970                     (unsigned long) size, (unsigned long) pos);
6971             size = 1;
6972           }
6973
6974         if (pos < 32 && (pos + size - 1) < 32)
6975           {
6976             s = "dins";
6977             fmt = "t,r,+A,+B";
6978           }
6979         else if (pos >= 32)
6980           {
6981             s = "dinsu";
6982             fmt = "t,r,+E,+F";
6983           }
6984         else
6985           {
6986             s = "dinsm";
6987             fmt = "t,r,+A,+F";
6988           }
6989         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6990                      (int) (pos + size - 1));
6991       }
6992       break;
6993
6994     case M_DDIV_3:
6995       dbl = 1;
6996     case M_DIV_3:
6997       s = "mflo";
6998       goto do_div3;
6999     case M_DREM_3:
7000       dbl = 1;
7001     case M_REM_3:
7002       s = "mfhi";
7003     do_div3:
7004       if (treg == 0)
7005         {
7006           as_warn (_("Divide by zero."));
7007           if (mips_trap)
7008             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7009           else
7010             macro_build (NULL, "break", BRK_FMT, 7);
7011           break;
7012         }
7013
7014       start_noreorder ();
7015       if (mips_trap)
7016         {
7017           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7018           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7019         }
7020       else
7021         {
7022           if (mips_opts.micromips)
7023             micromips_label_expr (&label_expr);
7024           else
7025             label_expr.X_add_number = 8;
7026           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7027           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7028           macro_build (NULL, "break", BRK_FMT, 7);
7029           if (mips_opts.micromips)
7030             micromips_add_label ();
7031         }
7032       expr1.X_add_number = -1;
7033       used_at = 1;
7034       load_register (AT, &expr1, dbl);
7035       if (mips_opts.micromips)
7036         micromips_label_expr (&label_expr);
7037       else
7038         label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7039       macro_build (&label_expr, "bne", "s,t,p", treg, AT);
7040       if (dbl)
7041         {
7042           expr1.X_add_number = 1;
7043           load_register (AT, &expr1, dbl);
7044           macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
7045         }
7046       else
7047         {
7048           expr1.X_add_number = 0x80000000;
7049           macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
7050         }
7051       if (mips_trap)
7052         {
7053           macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
7054           /* We want to close the noreorder block as soon as possible, so
7055              that later insns are available for delay slot filling.  */
7056           end_noreorder ();
7057         }
7058       else
7059         {
7060           if (mips_opts.micromips)
7061             micromips_label_expr (&label_expr);
7062           else
7063             label_expr.X_add_number = 8;
7064           macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
7065           macro_build (NULL, "nop", "");
7066
7067           /* We want to close the noreorder block as soon as possible, so
7068              that later insns are available for delay slot filling.  */
7069           end_noreorder ();
7070
7071           macro_build (NULL, "break", BRK_FMT, 6);
7072         }
7073       if (mips_opts.micromips)
7074         micromips_add_label ();
7075       macro_build (NULL, s, MFHL_FMT, dreg);
7076       break;
7077
7078     case M_DIV_3I:
7079       s = "div";
7080       s2 = "mflo";
7081       goto do_divi;
7082     case M_DIVU_3I:
7083       s = "divu";
7084       s2 = "mflo";
7085       goto do_divi;
7086     case M_REM_3I:
7087       s = "div";
7088       s2 = "mfhi";
7089       goto do_divi;
7090     case M_REMU_3I:
7091       s = "divu";
7092       s2 = "mfhi";
7093       goto do_divi;
7094     case M_DDIV_3I:
7095       dbl = 1;
7096       s = "ddiv";
7097       s2 = "mflo";
7098       goto do_divi;
7099     case M_DDIVU_3I:
7100       dbl = 1;
7101       s = "ddivu";
7102       s2 = "mflo";
7103       goto do_divi;
7104     case M_DREM_3I:
7105       dbl = 1;
7106       s = "ddiv";
7107       s2 = "mfhi";
7108       goto do_divi;
7109     case M_DREMU_3I:
7110       dbl = 1;
7111       s = "ddivu";
7112       s2 = "mfhi";
7113     do_divi:
7114       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7115         {
7116           as_warn (_("Divide by zero."));
7117           if (mips_trap)
7118             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7119           else
7120             macro_build (NULL, "break", BRK_FMT, 7);
7121           break;
7122         }
7123       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7124         {
7125           if (strcmp (s2, "mflo") == 0)
7126             move_register (dreg, sreg);
7127           else
7128             move_register (dreg, ZERO);
7129           break;
7130         }
7131       if (imm_expr.X_op == O_constant
7132           && imm_expr.X_add_number == -1
7133           && s[strlen (s) - 1] != 'u')
7134         {
7135           if (strcmp (s2, "mflo") == 0)
7136             {
7137               macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
7138             }
7139           else
7140             move_register (dreg, ZERO);
7141           break;
7142         }
7143
7144       used_at = 1;
7145       load_register (AT, &imm_expr, dbl);
7146       macro_build (NULL, s, "z,s,t", sreg, AT);
7147       macro_build (NULL, s2, MFHL_FMT, dreg);
7148       break;
7149
7150     case M_DIVU_3:
7151       s = "divu";
7152       s2 = "mflo";
7153       goto do_divu3;
7154     case M_REMU_3:
7155       s = "divu";
7156       s2 = "mfhi";
7157       goto do_divu3;
7158     case M_DDIVU_3:
7159       s = "ddivu";
7160       s2 = "mflo";
7161       goto do_divu3;
7162     case M_DREMU_3:
7163       s = "ddivu";
7164       s2 = "mfhi";
7165     do_divu3:
7166       start_noreorder ();
7167       if (mips_trap)
7168         {
7169           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7170           macro_build (NULL, s, "z,s,t", sreg, treg);
7171           /* We want to close the noreorder block as soon as possible, so
7172              that later insns are available for delay slot filling.  */
7173           end_noreorder ();
7174         }
7175       else
7176         {
7177           if (mips_opts.micromips)
7178             micromips_label_expr (&label_expr);
7179           else
7180             label_expr.X_add_number = 8;
7181           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7182           macro_build (NULL, s, "z,s,t", sreg, treg);
7183
7184           /* We want to close the noreorder block as soon as possible, so
7185              that later insns are available for delay slot filling.  */
7186           end_noreorder ();
7187           macro_build (NULL, "break", BRK_FMT, 7);
7188           if (mips_opts.micromips)
7189             micromips_add_label ();
7190         }
7191       macro_build (NULL, s2, MFHL_FMT, dreg);
7192       break;
7193
7194     case M_DLCA_AB:
7195       dbl = 1;
7196     case M_LCA_AB:
7197       call = 1;
7198       goto do_la;
7199     case M_DLA_AB:
7200       dbl = 1;
7201     case M_LA_AB:
7202     do_la:
7203       /* Load the address of a symbol into a register.  If breg is not
7204          zero, we then add a base register to it.  */
7205
7206       if (dbl && HAVE_32BIT_GPRS)
7207         as_warn (_("dla used to load 32-bit register"));
7208
7209       if (!dbl && HAVE_64BIT_OBJECTS)
7210         as_warn (_("la used to load 64-bit address"));
7211
7212       if (offset_expr.X_op == O_constant
7213           && offset_expr.X_add_number >= -0x8000
7214           && offset_expr.X_add_number < 0x8000)
7215         {
7216           macro_build (&offset_expr, ADDRESS_ADDI_INSN,
7217                        "t,r,j", treg, sreg, BFD_RELOC_LO16);
7218           break;
7219         }
7220
7221       if (mips_opts.at && (treg == breg))
7222         {
7223           tempreg = AT;
7224           used_at = 1;
7225         }
7226       else
7227         {
7228           tempreg = treg;
7229         }
7230
7231       if (offset_expr.X_op != O_symbol
7232           && offset_expr.X_op != O_constant)
7233         {
7234           as_bad (_("Expression too complex"));
7235           offset_expr.X_op = O_constant;
7236         }
7237
7238       if (offset_expr.X_op == O_constant)
7239         load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7240       else if (mips_pic == NO_PIC)
7241         {
7242           /* If this is a reference to a GP relative symbol, we want
7243                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
7244              Otherwise we want
7245                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
7246                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7247              If we have a constant, we need two instructions anyhow,
7248              so we may as well always use the latter form.
7249
7250              With 64bit address space and a usable $at we want
7251                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7252                lui      $at,<sym>               (BFD_RELOC_HI16_S)
7253                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7254                daddiu   $at,<sym>               (BFD_RELOC_LO16)
7255                dsll32   $tempreg,0
7256                daddu    $tempreg,$tempreg,$at
7257
7258              If $at is already in use, we use a path which is suboptimal
7259              on superscalar processors.
7260                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7261                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7262                dsll     $tempreg,16
7263                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
7264                dsll     $tempreg,16
7265                daddiu   $tempreg,<sym>          (BFD_RELOC_LO16)
7266
7267              For GP relative symbols in 64bit address space we can use
7268              the same sequence as in 32bit address space.  */
7269           if (HAVE_64BIT_SYMBOLS)
7270             {
7271               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7272                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7273                 {
7274                   relax_start (offset_expr.X_add_symbol);
7275                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7276                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7277                   relax_switch ();
7278                 }
7279
7280               if (used_at == 0 && mips_opts.at)
7281                 {
7282                   macro_build (&offset_expr, "lui", LUI_FMT,
7283                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7284                   macro_build (&offset_expr, "lui", LUI_FMT,
7285                                AT, BFD_RELOC_HI16_S);
7286                   macro_build (&offset_expr, "daddiu", "t,r,j",
7287                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7288                   macro_build (&offset_expr, "daddiu", "t,r,j",
7289                                AT, AT, BFD_RELOC_LO16);
7290                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7291                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7292                   used_at = 1;
7293                 }
7294               else
7295                 {
7296                   macro_build (&offset_expr, "lui", LUI_FMT,
7297                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7298                   macro_build (&offset_expr, "daddiu", "t,r,j",
7299                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7300                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7301                   macro_build (&offset_expr, "daddiu", "t,r,j",
7302                                tempreg, tempreg, BFD_RELOC_HI16_S);
7303                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7304                   macro_build (&offset_expr, "daddiu", "t,r,j",
7305                                tempreg, tempreg, BFD_RELOC_LO16);
7306                 }
7307
7308               if (mips_relax.sequence)
7309                 relax_end ();
7310             }
7311           else
7312             {
7313               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7314                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7315                 {
7316                   relax_start (offset_expr.X_add_symbol);
7317                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7318                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7319                   relax_switch ();
7320                 }
7321               if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7322                 as_bad (_("Offset too large"));
7323               macro_build_lui (&offset_expr, tempreg);
7324               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7325                            tempreg, tempreg, BFD_RELOC_LO16);
7326               if (mips_relax.sequence)
7327                 relax_end ();
7328             }
7329         }
7330       else if (!mips_big_got && !HAVE_NEWABI)
7331         {
7332           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7333
7334           /* If this is a reference to an external symbol, and there
7335              is no constant, we want
7336                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7337              or for lca or if tempreg is PIC_CALL_REG
7338                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7339              For a local symbol, we want
7340                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7341                nop
7342                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7343
7344              If we have a small constant, and this is a reference to
7345              an external symbol, we want
7346                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7347                nop
7348                addiu    $tempreg,$tempreg,<constant>
7349              For a local symbol, we want the same instruction
7350              sequence, but we output a BFD_RELOC_LO16 reloc on the
7351              addiu instruction.
7352
7353              If we have a large constant, and this is a reference to
7354              an external symbol, we want
7355                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7356                lui      $at,<hiconstant>
7357                addiu    $at,$at,<loconstant>
7358                addu     $tempreg,$tempreg,$at
7359              For a local symbol, we want the same instruction
7360              sequence, but we output a BFD_RELOC_LO16 reloc on the
7361              addiu instruction.
7362            */
7363
7364           if (offset_expr.X_add_number == 0)
7365             {
7366               if (mips_pic == SVR4_PIC
7367                   && breg == 0
7368                   && (call || tempreg == PIC_CALL_REG))
7369                 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7370
7371               relax_start (offset_expr.X_add_symbol);
7372               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7373                            lw_reloc_type, mips_gp_register);
7374               if (breg != 0)
7375                 {
7376                   /* We're going to put in an addu instruction using
7377                      tempreg, so we may as well insert the nop right
7378                      now.  */
7379                   load_delay_nop ();
7380                 }
7381               relax_switch ();
7382               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7383                            tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7384               load_delay_nop ();
7385               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7386                            tempreg, tempreg, BFD_RELOC_LO16);
7387               relax_end ();
7388               /* FIXME: If breg == 0, and the next instruction uses
7389                  $tempreg, then if this variant case is used an extra
7390                  nop will be generated.  */
7391             }
7392           else if (offset_expr.X_add_number >= -0x8000
7393                    && offset_expr.X_add_number < 0x8000)
7394             {
7395               load_got_offset (tempreg, &offset_expr);
7396               load_delay_nop ();
7397               add_got_offset (tempreg, &offset_expr);
7398             }
7399           else
7400             {
7401               expr1.X_add_number = offset_expr.X_add_number;
7402               offset_expr.X_add_number =
7403                 SEXT_16BIT (offset_expr.X_add_number);
7404               load_got_offset (tempreg, &offset_expr);
7405               offset_expr.X_add_number = expr1.X_add_number;
7406               /* If we are going to add in a base register, and the
7407                  target register and the base register are the same,
7408                  then we are using AT as a temporary register.  Since
7409                  we want to load the constant into AT, we add our
7410                  current AT (from the global offset table) and the
7411                  register into the register now, and pretend we were
7412                  not using a base register.  */
7413               if (breg == treg)
7414                 {
7415                   load_delay_nop ();
7416                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7417                                treg, AT, breg);
7418                   breg = 0;
7419                   tempreg = treg;
7420                 }
7421               add_got_offset_hilo (tempreg, &offset_expr, AT);
7422               used_at = 1;
7423             }
7424         }
7425       else if (!mips_big_got && HAVE_NEWABI)
7426         {
7427           int add_breg_early = 0;
7428
7429           /* If this is a reference to an external, and there is no
7430              constant, or local symbol (*), with or without a
7431              constant, we want
7432                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7433              or for lca or if tempreg is PIC_CALL_REG
7434                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7435
7436              If we have a small constant, and this is a reference to
7437              an external symbol, we want
7438                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7439                addiu    $tempreg,$tempreg,<constant>
7440
7441              If we have a large constant, and this is a reference to
7442              an external symbol, we want
7443                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7444                lui      $at,<hiconstant>
7445                addiu    $at,$at,<loconstant>
7446                addu     $tempreg,$tempreg,$at
7447
7448              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7449              local symbols, even though it introduces an additional
7450              instruction.  */
7451
7452           if (offset_expr.X_add_number)
7453             {
7454               expr1.X_add_number = offset_expr.X_add_number;
7455               offset_expr.X_add_number = 0;
7456
7457               relax_start (offset_expr.X_add_symbol);
7458               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7459                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7460
7461               if (expr1.X_add_number >= -0x8000
7462                   && expr1.X_add_number < 0x8000)
7463                 {
7464                   macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7465                                tempreg, tempreg, BFD_RELOC_LO16);
7466                 }
7467               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7468                 {
7469                   /* If we are going to add in a base register, and the
7470                      target register and the base register are the same,
7471                      then we are using AT as a temporary register.  Since
7472                      we want to load the constant into AT, we add our
7473                      current AT (from the global offset table) and the
7474                      register into the register now, and pretend we were
7475                      not using a base register.  */
7476                   if (breg != treg)
7477                     dreg = tempreg;
7478                   else
7479                     {
7480                       gas_assert (tempreg == AT);
7481                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7482                                    treg, AT, breg);
7483                       dreg = treg;
7484                       add_breg_early = 1;
7485                     }
7486
7487                   load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7488                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7489                                dreg, dreg, AT);
7490
7491                   used_at = 1;
7492                 }
7493               else
7494                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7495
7496               relax_switch ();
7497               offset_expr.X_add_number = expr1.X_add_number;
7498
7499               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7500                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7501               if (add_breg_early)
7502                 {
7503                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7504                                treg, tempreg, breg);
7505                   breg = 0;
7506                   tempreg = treg;
7507                 }
7508               relax_end ();
7509             }
7510           else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7511             {
7512               relax_start (offset_expr.X_add_symbol);
7513               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7514                            BFD_RELOC_MIPS_CALL16, mips_gp_register);
7515               relax_switch ();
7516               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7517                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7518               relax_end ();
7519             }
7520           else
7521             {
7522               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7523                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7524             }
7525         }
7526       else if (mips_big_got && !HAVE_NEWABI)
7527         {
7528           int gpdelay;
7529           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7530           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7531           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7532
7533           /* This is the large GOT case.  If this is a reference to an
7534              external symbol, and there is no constant, we want
7535                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7536                addu     $tempreg,$tempreg,$gp
7537                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7538              or for lca or if tempreg is PIC_CALL_REG
7539                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
7540                addu     $tempreg,$tempreg,$gp
7541                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7542              For a local symbol, we want
7543                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7544                nop
7545                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7546
7547              If we have a small constant, and this is a reference to
7548              an external symbol, we want
7549                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7550                addu     $tempreg,$tempreg,$gp
7551                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7552                nop
7553                addiu    $tempreg,$tempreg,<constant>
7554              For a local symbol, we want
7555                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7556                nop
7557                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7558
7559              If we have a large constant, and this is a reference to
7560              an external symbol, we want
7561                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7562                addu     $tempreg,$tempreg,$gp
7563                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7564                lui      $at,<hiconstant>
7565                addiu    $at,$at,<loconstant>
7566                addu     $tempreg,$tempreg,$at
7567              For a local symbol, we want
7568                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7569                lui      $at,<hiconstant>
7570                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
7571                addu     $tempreg,$tempreg,$at
7572           */
7573
7574           expr1.X_add_number = offset_expr.X_add_number;
7575           offset_expr.X_add_number = 0;
7576           relax_start (offset_expr.X_add_symbol);
7577           gpdelay = reg_needs_delay (mips_gp_register);
7578           if (expr1.X_add_number == 0 && breg == 0
7579               && (call || tempreg == PIC_CALL_REG))
7580             {
7581               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7582               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7583             }
7584           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7585           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7586                        tempreg, tempreg, mips_gp_register);
7587           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7588                        tempreg, lw_reloc_type, tempreg);
7589           if (expr1.X_add_number == 0)
7590             {
7591               if (breg != 0)
7592                 {
7593                   /* We're going to put in an addu instruction using
7594                      tempreg, so we may as well insert the nop right
7595                      now.  */
7596                   load_delay_nop ();
7597                 }
7598             }
7599           else if (expr1.X_add_number >= -0x8000
7600                    && expr1.X_add_number < 0x8000)
7601             {
7602               load_delay_nop ();
7603               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7604                            tempreg, tempreg, BFD_RELOC_LO16);
7605             }
7606           else
7607             {
7608               /* If we are going to add in a base register, and the
7609                  target register and the base register are the same,
7610                  then we are using AT as a temporary register.  Since
7611                  we want to load the constant into AT, we add our
7612                  current AT (from the global offset table) and the
7613                  register into the register now, and pretend we were
7614                  not using a base register.  */
7615               if (breg != treg)
7616                 dreg = tempreg;
7617               else
7618                 {
7619                   gas_assert (tempreg == AT);
7620                   load_delay_nop ();
7621                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7622                                treg, AT, breg);
7623                   dreg = treg;
7624                 }
7625
7626               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7627               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7628
7629               used_at = 1;
7630             }
7631           offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
7632           relax_switch ();
7633
7634           if (gpdelay)
7635             {
7636               /* This is needed because this instruction uses $gp, but
7637                  the first instruction on the main stream does not.  */
7638               macro_build (NULL, "nop", "");
7639             }
7640
7641           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7642                        local_reloc_type, mips_gp_register);
7643           if (expr1.X_add_number >= -0x8000
7644               && expr1.X_add_number < 0x8000)
7645             {
7646               load_delay_nop ();
7647               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7648                            tempreg, tempreg, BFD_RELOC_LO16);
7649               /* FIXME: If add_number is 0, and there was no base
7650                  register, the external symbol case ended with a load,
7651                  so if the symbol turns out to not be external, and
7652                  the next instruction uses tempreg, an unnecessary nop
7653                  will be inserted.  */
7654             }
7655           else
7656             {
7657               if (breg == treg)
7658                 {
7659                   /* We must add in the base register now, as in the
7660                      external symbol case.  */
7661                   gas_assert (tempreg == AT);
7662                   load_delay_nop ();
7663                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7664                                treg, AT, breg);
7665                   tempreg = treg;
7666                   /* We set breg to 0 because we have arranged to add
7667                      it in in both cases.  */
7668                   breg = 0;
7669                 }
7670
7671               macro_build_lui (&expr1, AT);
7672               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7673                            AT, AT, BFD_RELOC_LO16);
7674               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7675                            tempreg, tempreg, AT);
7676               used_at = 1;
7677             }
7678           relax_end ();
7679         }
7680       else if (mips_big_got && HAVE_NEWABI)
7681         {
7682           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7683           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7684           int add_breg_early = 0;
7685
7686           /* This is the large GOT case.  If this is a reference to an
7687              external symbol, and there is no constant, we want
7688                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7689                add      $tempreg,$tempreg,$gp
7690                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7691              or for lca or if tempreg is PIC_CALL_REG
7692                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
7693                add      $tempreg,$tempreg,$gp
7694                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7695
7696              If we have a small constant, and this is a reference to
7697              an external symbol, we want
7698                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7699                add      $tempreg,$tempreg,$gp
7700                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7701                addi     $tempreg,$tempreg,<constant>
7702
7703              If we have a large constant, and this is a reference to
7704              an external symbol, we want
7705                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7706                addu     $tempreg,$tempreg,$gp
7707                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7708                lui      $at,<hiconstant>
7709                addi     $at,$at,<loconstant>
7710                add      $tempreg,$tempreg,$at
7711
7712              If we have NewABI, and we know it's a local symbol, we want
7713                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
7714                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
7715              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
7716
7717           relax_start (offset_expr.X_add_symbol);
7718
7719           expr1.X_add_number = offset_expr.X_add_number;
7720           offset_expr.X_add_number = 0;
7721
7722           if (expr1.X_add_number == 0 && breg == 0
7723               && (call || tempreg == PIC_CALL_REG))
7724             {
7725               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7726               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7727             }
7728           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7729           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7730                        tempreg, tempreg, mips_gp_register);
7731           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7732                        tempreg, lw_reloc_type, tempreg);
7733
7734           if (expr1.X_add_number == 0)
7735             ;
7736           else if (expr1.X_add_number >= -0x8000
7737                    && expr1.X_add_number < 0x8000)
7738             {
7739               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7740                            tempreg, tempreg, BFD_RELOC_LO16);
7741             }
7742           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7743             {
7744               /* If we are going to add in a base register, and the
7745                  target register and the base register are the same,
7746                  then we are using AT as a temporary register.  Since
7747                  we want to load the constant into AT, we add our
7748                  current AT (from the global offset table) and the
7749                  register into the register now, and pretend we were
7750                  not using a base register.  */
7751               if (breg != treg)
7752                 dreg = tempreg;
7753               else
7754                 {
7755                   gas_assert (tempreg == AT);
7756                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7757                                treg, AT, breg);
7758                   dreg = treg;
7759                   add_breg_early = 1;
7760                 }
7761
7762               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7763               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7764
7765               used_at = 1;
7766             }
7767           else
7768             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7769
7770           relax_switch ();
7771           offset_expr.X_add_number = expr1.X_add_number;
7772           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7773                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7774           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7775                        tempreg, BFD_RELOC_MIPS_GOT_OFST);
7776           if (add_breg_early)
7777             {
7778               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7779                            treg, tempreg, breg);
7780               breg = 0;
7781               tempreg = treg;
7782             }
7783           relax_end ();
7784         }
7785       else
7786         abort ();
7787
7788       if (breg != 0)
7789         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
7790       break;
7791
7792     case M_MSGSND:
7793       gas_assert (!mips_opts.micromips);
7794       {
7795         unsigned long temp = (treg << 16) | (0x01);
7796         macro_build (NULL, "c2", "C", temp);
7797       }
7798       break;
7799
7800     case M_MSGLD:
7801       gas_assert (!mips_opts.micromips);
7802       {
7803         unsigned long temp = (0x02);
7804         macro_build (NULL, "c2", "C", temp);
7805       }
7806       break;
7807
7808     case M_MSGLD_T:
7809       gas_assert (!mips_opts.micromips);
7810       {
7811         unsigned long temp = (treg << 16) | (0x02);
7812         macro_build (NULL, "c2", "C", temp);
7813       }
7814       break;
7815
7816     case M_MSGWAIT:
7817       gas_assert (!mips_opts.micromips);
7818       macro_build (NULL, "c2", "C", 3);
7819       break;
7820
7821     case M_MSGWAIT_T:
7822       gas_assert (!mips_opts.micromips);
7823       {
7824         unsigned long temp = (treg << 16) | 0x03;
7825         macro_build (NULL, "c2", "C", temp);
7826       }
7827       break;
7828
7829     case M_J_A:
7830       /* The j instruction may not be used in PIC code, since it
7831          requires an absolute address.  We convert it to a b
7832          instruction.  */
7833       if (mips_pic == NO_PIC)
7834         macro_build (&offset_expr, "j", "a");
7835       else
7836         macro_build (&offset_expr, "b", "p");
7837       break;
7838
7839       /* The jal instructions must be handled as macros because when
7840          generating PIC code they expand to multi-instruction
7841          sequences.  Normally they are simple instructions.  */
7842     case M_JALS_1:
7843       dreg = RA;
7844       /* Fall through.  */
7845     case M_JALS_2:
7846       gas_assert (mips_opts.micromips);
7847       jals = 1;
7848       goto jal;
7849     case M_JAL_1:
7850       dreg = RA;
7851       /* Fall through.  */
7852     case M_JAL_2:
7853     jal:
7854       if (mips_pic == NO_PIC)
7855         {
7856           s = jals ? "jalrs" : "jalr";
7857           if (mips_opts.micromips
7858               && dreg == RA
7859               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7860             macro_build (NULL, s, "mj", sreg);
7861           else
7862             macro_build (NULL, s, JALR_FMT, dreg, sreg);
7863         }
7864       else
7865         {
7866           int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7867                            && mips_cprestore_offset >= 0);
7868
7869           if (sreg != PIC_CALL_REG)
7870             as_warn (_("MIPS PIC call to register other than $25"));
7871
7872           s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7873                ? "jalrs" : "jalr");
7874           if (mips_opts.micromips
7875               && dreg == RA
7876               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7877             macro_build (NULL, s, "mj", sreg);
7878           else
7879             macro_build (NULL, s, JALR_FMT, dreg, sreg);
7880           if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
7881             {
7882               if (mips_cprestore_offset < 0)
7883                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7884               else
7885                 {
7886                   if (!mips_frame_reg_valid)
7887                     {
7888                       as_warn (_("No .frame pseudo-op used in PIC code"));
7889                       /* Quiet this warning.  */
7890                       mips_frame_reg_valid = 1;
7891                     }
7892                   if (!mips_cprestore_valid)
7893                     {
7894                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
7895                       /* Quiet this warning.  */
7896                       mips_cprestore_valid = 1;
7897                     }
7898                   if (mips_opts.noreorder)
7899                     macro_build (NULL, "nop", "");
7900                   expr1.X_add_number = mips_cprestore_offset;
7901                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7902                                                 mips_gp_register,
7903                                                 mips_frame_reg,
7904                                                 HAVE_64BIT_ADDRESSES);
7905                 }
7906             }
7907         }
7908
7909       break;
7910
7911     case M_JALS_A:
7912       gas_assert (mips_opts.micromips);
7913       jals = 1;
7914       /* Fall through.  */
7915     case M_JAL_A:
7916       if (mips_pic == NO_PIC)
7917         macro_build (&offset_expr, jals ? "jals" : "jal", "a");
7918       else if (mips_pic == SVR4_PIC)
7919         {
7920           /* If this is a reference to an external symbol, and we are
7921              using a small GOT, we want
7922                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
7923                nop
7924                jalr     $ra,$25
7925                nop
7926                lw       $gp,cprestore($sp)
7927              The cprestore value is set using the .cprestore
7928              pseudo-op.  If we are using a big GOT, we want
7929                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
7930                addu     $25,$25,$gp
7931                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
7932                nop
7933                jalr     $ra,$25
7934                nop
7935                lw       $gp,cprestore($sp)
7936              If the symbol is not external, we want
7937                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7938                nop
7939                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
7940                jalr     $ra,$25
7941                nop
7942                lw $gp,cprestore($sp)
7943
7944              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7945              sequences above, minus nops, unless the symbol is local,
7946              which enables us to use GOT_PAGE/GOT_OFST (big got) or
7947              GOT_DISP.  */
7948           if (HAVE_NEWABI)
7949             {
7950               if (!mips_big_got)
7951                 {
7952                   relax_start (offset_expr.X_add_symbol);
7953                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7954                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7955                                mips_gp_register);
7956                   relax_switch ();
7957                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7958                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
7959                                mips_gp_register);
7960                   relax_end ();
7961                 }
7962               else
7963                 {
7964                   relax_start (offset_expr.X_add_symbol);
7965                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
7966                                BFD_RELOC_MIPS_CALL_HI16);
7967                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7968                                PIC_CALL_REG, mips_gp_register);
7969                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7970                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7971                                PIC_CALL_REG);
7972                   relax_switch ();
7973                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7974                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7975                                mips_gp_register);
7976                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7977                                PIC_CALL_REG, PIC_CALL_REG,
7978                                BFD_RELOC_MIPS_GOT_OFST);
7979                   relax_end ();
7980                 }
7981
7982               macro_build_jalr (&offset_expr, 0);
7983             }
7984           else
7985             {
7986               relax_start (offset_expr.X_add_symbol);
7987               if (!mips_big_got)
7988                 {
7989                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7990                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7991                                mips_gp_register);
7992                   load_delay_nop ();
7993                   relax_switch ();
7994                 }
7995               else
7996                 {
7997                   int gpdelay;
7998
7999                   gpdelay = reg_needs_delay (mips_gp_register);
8000                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8001                                BFD_RELOC_MIPS_CALL_HI16);
8002                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8003                                PIC_CALL_REG, mips_gp_register);
8004                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8005                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8006                                PIC_CALL_REG);
8007                   load_delay_nop ();
8008                   relax_switch ();
8009                   if (gpdelay)
8010                     macro_build (NULL, "nop", "");
8011                 }
8012               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8013                            PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
8014                            mips_gp_register);
8015               load_delay_nop ();
8016               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8017                            PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
8018               relax_end ();
8019               macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
8020
8021               if (mips_cprestore_offset < 0)
8022                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8023               else
8024                 {
8025                   if (!mips_frame_reg_valid)
8026                     {
8027                       as_warn (_("No .frame pseudo-op used in PIC code"));
8028                       /* Quiet this warning.  */
8029                       mips_frame_reg_valid = 1;
8030                     }
8031                   if (!mips_cprestore_valid)
8032                     {
8033                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
8034                       /* Quiet this warning.  */
8035                       mips_cprestore_valid = 1;
8036                     }
8037                   if (mips_opts.noreorder)
8038                     macro_build (NULL, "nop", "");
8039                   expr1.X_add_number = mips_cprestore_offset;
8040                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8041                                                 mips_gp_register,
8042                                                 mips_frame_reg,
8043                                                 HAVE_64BIT_ADDRESSES);
8044                 }
8045             }
8046         }
8047       else if (mips_pic == VXWORKS_PIC)
8048         as_bad (_("Non-PIC jump used in PIC library"));
8049       else
8050         abort ();
8051
8052       break;
8053
8054     case M_ACLR_AB:
8055       ab = 1;
8056     case M_ACLR_OB:
8057       s = "aclr";
8058       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8059       fmt = "\\,~(b)";
8060       off12 = 1;
8061       goto ld_st;
8062     case M_ASET_AB:
8063       ab = 1;
8064     case M_ASET_OB:
8065       s = "aset";
8066       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8067       fmt = "\\,~(b)";
8068       off12 = 1;
8069       goto ld_st;
8070     case M_LB_AB:
8071       ab = 1;
8072       s = "lb";
8073       fmt = "t,o(b)";
8074       goto ld;
8075     case M_LBU_AB:
8076       ab = 1;
8077       s = "lbu";
8078       fmt = "t,o(b)";
8079       goto ld;
8080     case M_LH_AB:
8081       ab = 1;
8082       s = "lh";
8083       fmt = "t,o(b)";
8084       goto ld;
8085     case M_LHU_AB:
8086       ab = 1;
8087       s = "lhu";
8088       fmt = "t,o(b)";
8089       goto ld;
8090     case M_LW_AB:
8091       ab = 1;
8092       s = "lw";
8093       fmt = "t,o(b)";
8094       goto ld;
8095     case M_LWC0_AB:
8096       ab = 1;
8097       gas_assert (!mips_opts.micromips);
8098       s = "lwc0";
8099       fmt = "E,o(b)";
8100       /* Itbl support may require additional care here.  */
8101       coproc = 1;
8102       goto ld_st;
8103     case M_LWC1_AB:
8104       ab = 1;
8105       s = "lwc1";
8106       fmt = "T,o(b)";
8107       /* Itbl support may require additional care here.  */
8108       coproc = 1;
8109       goto ld_st;
8110     case M_LWC2_AB:
8111       ab = 1;
8112     case M_LWC2_OB:
8113       s = "lwc2";
8114       fmt = COP12_FMT;
8115       off12 = mips_opts.micromips;
8116       /* Itbl support may require additional care here.  */
8117       coproc = 1;
8118       goto ld_st;
8119     case M_LWC3_AB:
8120       ab = 1;
8121       gas_assert (!mips_opts.micromips);
8122       s = "lwc3";
8123       fmt = "E,o(b)";
8124       /* Itbl support may require additional care here.  */
8125       coproc = 1;
8126       goto ld_st;
8127     case M_LWL_AB:
8128       ab = 1;
8129     case M_LWL_OB:
8130       s = "lwl";
8131       fmt = MEM12_FMT;
8132       off12 = mips_opts.micromips;
8133       goto ld_st;
8134     case M_LWR_AB:
8135       ab = 1;
8136     case M_LWR_OB:
8137       s = "lwr";
8138       fmt = MEM12_FMT;
8139       off12 = mips_opts.micromips;
8140       goto ld_st;
8141     case M_LDC1_AB:
8142       ab = 1;
8143       s = "ldc1";
8144       fmt = "T,o(b)";
8145       /* Itbl support may require additional care here.  */
8146       coproc = 1;
8147       goto ld_st;
8148     case M_LDC2_AB:
8149       ab = 1;
8150     case M_LDC2_OB:
8151       s = "ldc2";
8152       fmt = COP12_FMT;
8153       off12 = mips_opts.micromips;
8154       /* Itbl support may require additional care here.  */
8155       coproc = 1;
8156       goto ld_st;
8157     case M_LDC3_AB:
8158       ab = 1;
8159       s = "ldc3";
8160       fmt = "E,o(b)";
8161       /* Itbl support may require additional care here.  */
8162       coproc = 1;
8163       goto ld_st;
8164     case M_LDL_AB:
8165       ab = 1;
8166     case M_LDL_OB:
8167       s = "ldl";
8168       fmt = MEM12_FMT;
8169       off12 = mips_opts.micromips;
8170       goto ld_st;
8171     case M_LDR_AB:
8172       ab = 1;
8173     case M_LDR_OB:
8174       s = "ldr";
8175       fmt = MEM12_FMT;
8176       off12 = mips_opts.micromips;
8177       goto ld_st;
8178     case M_LL_AB:
8179       ab = 1;
8180     case M_LL_OB:
8181       s = "ll";
8182       fmt = MEM12_FMT;
8183       off12 = mips_opts.micromips;
8184       goto ld;
8185     case M_LLD_AB:
8186       ab = 1;
8187     case M_LLD_OB:
8188       s = "lld";
8189       fmt = MEM12_FMT;
8190       off12 = mips_opts.micromips;
8191       goto ld;
8192     case M_LWU_AB:
8193       ab = 1;
8194     case M_LWU_OB:
8195       s = "lwu";
8196       fmt = MEM12_FMT;
8197       off12 = mips_opts.micromips;
8198       goto ld;
8199     case M_LWP_AB:
8200       ab = 1;
8201     case M_LWP_OB:
8202       gas_assert (mips_opts.micromips);
8203       s = "lwp";
8204       fmt = "t,~(b)";
8205       off12 = 1;
8206       lp = 1;
8207       goto ld;
8208     case M_LDP_AB:
8209       ab = 1;
8210     case M_LDP_OB:
8211       gas_assert (mips_opts.micromips);
8212       s = "ldp";
8213       fmt = "t,~(b)";
8214       off12 = 1;
8215       lp = 1;
8216       goto ld;
8217     case M_LWM_AB:
8218       ab = 1;
8219     case M_LWM_OB:
8220       gas_assert (mips_opts.micromips);
8221       s = "lwm";
8222       fmt = "n,~(b)";
8223       off12 = 1;
8224       goto ld_st;
8225     case M_LDM_AB:
8226       ab = 1;
8227     case M_LDM_OB:
8228       gas_assert (mips_opts.micromips);
8229       s = "ldm";
8230       fmt = "n,~(b)";
8231       off12 = 1;
8232       goto ld_st;
8233
8234     ld:
8235       if (breg == treg + lp)
8236         goto ld_st;
8237       else
8238         tempreg = treg + lp;
8239       goto ld_noat;
8240
8241     case M_SB_AB:
8242       ab = 1;
8243       s = "sb";
8244       fmt = "t,o(b)";
8245       goto ld_st;
8246     case M_SH_AB:
8247       ab = 1;
8248       s = "sh";
8249       fmt = "t,o(b)";
8250       goto ld_st;
8251     case M_SW_AB:
8252       ab = 1;
8253       s = "sw";
8254       fmt = "t,o(b)";
8255       goto ld_st;
8256     case M_SWC0_AB:
8257       ab = 1;
8258       gas_assert (!mips_opts.micromips);
8259       s = "swc0";
8260       fmt = "E,o(b)";
8261       /* Itbl support may require additional care here.  */
8262       coproc = 1;
8263       goto ld_st;
8264     case M_SWC1_AB:
8265       ab = 1;
8266       s = "swc1";
8267       fmt = "T,o(b)";
8268       /* Itbl support may require additional care here.  */
8269       coproc = 1;
8270       goto ld_st;
8271     case M_SWC2_AB:
8272       ab = 1;
8273     case M_SWC2_OB:
8274       s = "swc2";
8275       fmt = COP12_FMT;
8276       off12 = mips_opts.micromips;
8277       /* Itbl support may require additional care here.  */
8278       coproc = 1;
8279       goto ld_st;
8280     case M_SWC3_AB:
8281       ab = 1;
8282       gas_assert (!mips_opts.micromips);
8283       s = "swc3";
8284       fmt = "E,o(b)";
8285       /* Itbl support may require additional care here.  */
8286       coproc = 1;
8287       goto ld_st;
8288     case M_SWL_AB:
8289       ab = 1;
8290     case M_SWL_OB:
8291       s = "swl";
8292       fmt = MEM12_FMT;
8293       off12 = mips_opts.micromips;
8294       goto ld_st;
8295     case M_SWR_AB:
8296       ab = 1;
8297     case M_SWR_OB:
8298       s = "swr";
8299       fmt = MEM12_FMT;
8300       off12 = mips_opts.micromips;
8301       goto ld_st;
8302     case M_SC_AB:
8303       ab = 1;
8304     case M_SC_OB:
8305       s = "sc";
8306       fmt = MEM12_FMT;
8307       off12 = mips_opts.micromips;
8308       goto ld_st;
8309     case M_SCD_AB:
8310       ab = 1;
8311     case M_SCD_OB:
8312       s = "scd";
8313       fmt = MEM12_FMT;
8314       off12 = mips_opts.micromips;
8315       goto ld_st;
8316     case M_CACHE_AB:
8317       ab = 1;
8318     case M_CACHE_OB:
8319       s = "cache";
8320       fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8321       off12 = mips_opts.micromips;
8322       goto ld_st;
8323     case M_PREF_AB:
8324       ab = 1;
8325     case M_PREF_OB:
8326       s = "pref";
8327       fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8328       off12 = mips_opts.micromips;
8329       goto ld_st;
8330     case M_SDC1_AB:
8331       ab = 1;
8332       s = "sdc1";
8333       fmt = "T,o(b)";
8334       coproc = 1;
8335       /* Itbl support may require additional care here.  */
8336       goto ld_st;
8337     case M_SDC2_AB:
8338       ab = 1;
8339     case M_SDC2_OB:
8340       s = "sdc2";
8341       fmt = COP12_FMT;
8342       off12 = mips_opts.micromips;
8343       /* Itbl support may require additional care here.  */
8344       coproc = 1;
8345       goto ld_st;
8346     case M_SDC3_AB:
8347       ab = 1;
8348       gas_assert (!mips_opts.micromips);
8349       s = "sdc3";
8350       fmt = "E,o(b)";
8351       /* Itbl support may require additional care here.  */
8352       coproc = 1;
8353       goto ld_st;
8354     case M_SDL_AB:
8355       ab = 1;
8356     case M_SDL_OB:
8357       s = "sdl";
8358       fmt = MEM12_FMT;
8359       off12 = mips_opts.micromips;
8360       goto ld_st;
8361     case M_SDR_AB:
8362       ab = 1;
8363     case M_SDR_OB:
8364       s = "sdr";
8365       fmt = MEM12_FMT;
8366       off12 = mips_opts.micromips;
8367       goto ld_st;
8368     case M_SWP_AB:
8369       ab = 1;
8370     case M_SWP_OB:
8371       gas_assert (mips_opts.micromips);
8372       s = "swp";
8373       fmt = "t,~(b)";
8374       off12 = 1;
8375       goto ld_st;
8376     case M_SDP_AB:
8377       ab = 1;
8378     case M_SDP_OB:
8379       gas_assert (mips_opts.micromips);
8380       s = "sdp";
8381       fmt = "t,~(b)";
8382       off12 = 1;
8383       goto ld_st;
8384     case M_SWM_AB:
8385       ab = 1;
8386     case M_SWM_OB:
8387       gas_assert (mips_opts.micromips);
8388       s = "swm";
8389       fmt = "n,~(b)";
8390       off12 = 1;
8391       goto ld_st;
8392     case M_SDM_AB:
8393       ab = 1;
8394     case M_SDM_OB:
8395       gas_assert (mips_opts.micromips);
8396       s = "sdm";
8397       fmt = "n,~(b)";
8398       off12 = 1;
8399
8400     ld_st:
8401       tempreg = AT;
8402       used_at = 1;
8403     ld_noat:
8404       if (offset_expr.X_op != O_constant
8405           && offset_expr.X_op != O_symbol)
8406         {
8407           as_bad (_("Expression too complex"));
8408           offset_expr.X_op = O_constant;
8409         }
8410
8411       if (HAVE_32BIT_ADDRESSES
8412           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8413         {
8414           char value [32];
8415
8416           sprintf_vma (value, offset_expr.X_add_number);
8417           as_bad (_("Number (0x%s) larger than 32 bits"), value);
8418         }
8419
8420       /* A constant expression in PIC code can be handled just as it
8421          is in non PIC code.  */
8422       if (offset_expr.X_op == O_constant)
8423         {
8424           int hipart = 0;
8425
8426           expr1.X_add_number = offset_expr.X_add_number;
8427           normalize_address_expr (&expr1);
8428           if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
8429             {
8430               expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8431                                     & ~(bfd_vma) 0xffff);
8432               hipart = 1;
8433             }
8434           else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8435             {
8436               expr1.X_add_number = ((expr1.X_add_number + 0x800)
8437                                     & ~(bfd_vma) 0xfff);
8438               hipart = 1;
8439             }
8440           if (hipart)
8441             {
8442               load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8443               if (breg != 0)
8444                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8445                              tempreg, tempreg, breg);
8446               breg = tempreg;
8447             }
8448           if (off0)
8449             {
8450               if (offset_expr.X_add_number == 0)
8451                 tempreg = breg;
8452               else
8453                 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8454                              "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8455               macro_build (NULL, s, fmt, treg, tempreg);
8456             }
8457           else if (!off12)
8458             macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8459           else
8460             macro_build (NULL, s, fmt,
8461                          treg, (unsigned long) offset_expr.X_add_number, breg);
8462         }
8463       else if (off12 || off0)
8464         {
8465           /* A 12-bit or 0-bit offset field is too narrow to be used
8466              for a low-part relocation, so load the whole address into
8467              the auxillary register.  In the case of "A(b)" addresses,
8468              we first load absolute address "A" into the register and
8469              then add base register "b".  In the case of "o(b)" addresses,
8470              we simply need to add 16-bit offset "o" to base register "b", and
8471              offset_reloc already contains the relocations associated
8472              with "o".  */
8473           if (ab)
8474             {
8475               load_address (tempreg, &offset_expr, &used_at);
8476               if (breg != 0)
8477                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8478                              tempreg, tempreg, breg);
8479             }
8480           else
8481             macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8482                          tempreg, breg, -1,
8483                          offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8484           expr1.X_add_number = 0;
8485           if (off0)
8486             macro_build (NULL, s, fmt, treg, tempreg);
8487           else
8488             macro_build (NULL, s, fmt,
8489                          treg, (unsigned long) expr1.X_add_number, tempreg);
8490         }
8491       else if (mips_pic == NO_PIC)
8492         {
8493           /* If this is a reference to a GP relative symbol, and there
8494              is no base register, we want
8495                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
8496              Otherwise, if there is no base register, we want
8497                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8498                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8499              If we have a constant, we need two instructions anyhow,
8500              so we always use the latter form.
8501
8502              If we have a base register, and this is a reference to a
8503              GP relative symbol, we want
8504                addu     $tempreg,$breg,$gp
8505                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
8506              Otherwise we want
8507                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8508                addu     $tempreg,$tempreg,$breg
8509                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8510              With a constant we always use the latter case.
8511
8512              With 64bit address space and no base register and $at usable,
8513              we want
8514                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8515                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8516                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8517                dsll32   $tempreg,0
8518                daddu    $tempreg,$at
8519                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8520              If we have a base register, we want
8521                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8522                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8523                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8524                daddu    $at,$breg
8525                dsll32   $tempreg,0
8526                daddu    $tempreg,$at
8527                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8528
8529              Without $at we can't generate the optimal path for superscalar
8530              processors here since this would require two temporary registers.
8531                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8532                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8533                dsll     $tempreg,16
8534                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8535                dsll     $tempreg,16
8536                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8537              If we have a base register, we want
8538                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8539                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8540                dsll     $tempreg,16
8541                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8542                dsll     $tempreg,16
8543                daddu    $tempreg,$tempreg,$breg
8544                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8545
8546              For GP relative symbols in 64bit address space we can use
8547              the same sequence as in 32bit address space.  */
8548           if (HAVE_64BIT_SYMBOLS)
8549             {
8550               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8551                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8552                 {
8553                   relax_start (offset_expr.X_add_symbol);
8554                   if (breg == 0)
8555                     {
8556                       macro_build (&offset_expr, s, fmt, treg,
8557                                    BFD_RELOC_GPREL16, mips_gp_register);
8558                     }
8559                   else
8560                     {
8561                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8562                                    tempreg, breg, mips_gp_register);
8563                       macro_build (&offset_expr, s, fmt, treg,
8564                                    BFD_RELOC_GPREL16, tempreg);
8565                     }
8566                   relax_switch ();
8567                 }
8568
8569               if (used_at == 0 && mips_opts.at)
8570                 {
8571                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8572                                BFD_RELOC_MIPS_HIGHEST);
8573                   macro_build (&offset_expr, "lui", LUI_FMT, AT,
8574                                BFD_RELOC_HI16_S);
8575                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8576                                tempreg, BFD_RELOC_MIPS_HIGHER);
8577                   if (breg != 0)
8578                     macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
8579                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8580                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8581                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8582                                tempreg);
8583                   used_at = 1;
8584                 }
8585               else
8586                 {
8587                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8588                                BFD_RELOC_MIPS_HIGHEST);
8589                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8590                                tempreg, BFD_RELOC_MIPS_HIGHER);
8591                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8592                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8593                                tempreg, BFD_RELOC_HI16_S);
8594                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8595                   if (breg != 0)
8596                     macro_build (NULL, "daddu", "d,v,t",
8597                                  tempreg, tempreg, breg);
8598                   macro_build (&offset_expr, s, fmt, treg,
8599                                BFD_RELOC_LO16, tempreg);
8600                 }
8601
8602               if (mips_relax.sequence)
8603                 relax_end ();
8604               break;
8605             }
8606
8607           if (breg == 0)
8608             {
8609               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8610                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8611                 {
8612                   relax_start (offset_expr.X_add_symbol);
8613                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8614                                mips_gp_register);
8615                   relax_switch ();
8616                 }
8617               macro_build_lui (&offset_expr, tempreg);
8618               macro_build (&offset_expr, s, fmt, treg,
8619                            BFD_RELOC_LO16, tempreg);
8620               if (mips_relax.sequence)
8621                 relax_end ();
8622             }
8623           else
8624             {
8625               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8626                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8627                 {
8628                   relax_start (offset_expr.X_add_symbol);
8629                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8630                                tempreg, breg, mips_gp_register);
8631                   macro_build (&offset_expr, s, fmt, treg,
8632                                BFD_RELOC_GPREL16, tempreg);
8633                   relax_switch ();
8634                 }
8635               macro_build_lui (&offset_expr, tempreg);
8636               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8637                            tempreg, tempreg, breg);
8638               macro_build (&offset_expr, s, fmt, treg,
8639                            BFD_RELOC_LO16, tempreg);
8640               if (mips_relax.sequence)
8641                 relax_end ();
8642             }
8643         }
8644       else if (!mips_big_got)
8645         {
8646           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
8647
8648           /* If this is a reference to an external symbol, we want
8649                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8650                nop
8651                <op>     $treg,0($tempreg)
8652              Otherwise we want
8653                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8654                nop
8655                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8656                <op>     $treg,0($tempreg)
8657
8658              For NewABI, we want
8659                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
8660                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
8661
8662              If there is a base register, we add it to $tempreg before
8663              the <op>.  If there is a constant, we stick it in the
8664              <op> instruction.  We don't handle constants larger than
8665              16 bits, because we have no way to load the upper 16 bits
8666              (actually, we could handle them for the subset of cases
8667              in which we are not using $at).  */
8668           gas_assert (offset_expr.X_op == O_symbol);
8669           if (HAVE_NEWABI)
8670             {
8671               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8672                            BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8673               if (breg != 0)
8674                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8675                              tempreg, tempreg, breg);
8676               macro_build (&offset_expr, s, fmt, treg,
8677                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
8678               break;
8679             }
8680           expr1.X_add_number = offset_expr.X_add_number;
8681           offset_expr.X_add_number = 0;
8682           if (expr1.X_add_number < -0x8000
8683               || expr1.X_add_number >= 0x8000)
8684             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8685           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8686                        lw_reloc_type, mips_gp_register);
8687           load_delay_nop ();
8688           relax_start (offset_expr.X_add_symbol);
8689           relax_switch ();
8690           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8691                        tempreg, BFD_RELOC_LO16);
8692           relax_end ();
8693           if (breg != 0)
8694             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8695                          tempreg, tempreg, breg);
8696           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8697         }
8698       else if (mips_big_got && !HAVE_NEWABI)
8699         {
8700           int gpdelay;
8701
8702           /* If this is a reference to an external symbol, we want
8703                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8704                addu     $tempreg,$tempreg,$gp
8705                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8706                <op>     $treg,0($tempreg)
8707              Otherwise we want
8708                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8709                nop
8710                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8711                <op>     $treg,0($tempreg)
8712              If there is a base register, we add it to $tempreg before
8713              the <op>.  If there is a constant, we stick it in the
8714              <op> instruction.  We don't handle constants larger than
8715              16 bits, because we have no way to load the upper 16 bits
8716              (actually, we could handle them for the subset of cases
8717              in which we are not using $at).  */
8718           gas_assert (offset_expr.X_op == O_symbol);
8719           expr1.X_add_number = offset_expr.X_add_number;
8720           offset_expr.X_add_number = 0;
8721           if (expr1.X_add_number < -0x8000
8722               || expr1.X_add_number >= 0x8000)
8723             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8724           gpdelay = reg_needs_delay (mips_gp_register);
8725           relax_start (offset_expr.X_add_symbol);
8726           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8727                        BFD_RELOC_MIPS_GOT_HI16);
8728           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8729                        mips_gp_register);
8730           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8731                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
8732           relax_switch ();
8733           if (gpdelay)
8734             macro_build (NULL, "nop", "");
8735           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8736                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
8737           load_delay_nop ();
8738           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8739                        tempreg, BFD_RELOC_LO16);
8740           relax_end ();
8741
8742           if (breg != 0)
8743             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8744                          tempreg, tempreg, breg);
8745           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8746         }
8747       else if (mips_big_got && HAVE_NEWABI)
8748         {
8749           /* If this is a reference to an external symbol, we want
8750                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8751                add      $tempreg,$tempreg,$gp
8752                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8753                <op>     $treg,<ofst>($tempreg)
8754              Otherwise, for local symbols, we want:
8755                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
8756                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
8757           gas_assert (offset_expr.X_op == O_symbol);
8758           expr1.X_add_number = offset_expr.X_add_number;
8759           offset_expr.X_add_number = 0;
8760           if (expr1.X_add_number < -0x8000
8761               || expr1.X_add_number >= 0x8000)
8762             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8763           relax_start (offset_expr.X_add_symbol);
8764           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8765                        BFD_RELOC_MIPS_GOT_HI16);
8766           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8767                        mips_gp_register);
8768           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8769                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
8770           if (breg != 0)
8771             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8772                          tempreg, tempreg, breg);
8773           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8774
8775           relax_switch ();
8776           offset_expr.X_add_number = expr1.X_add_number;
8777           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8778                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8779           if (breg != 0)
8780             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8781                          tempreg, tempreg, breg);
8782           macro_build (&offset_expr, s, fmt, treg,
8783                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
8784           relax_end ();
8785         }
8786       else
8787         abort ();
8788
8789       break;
8790
8791     case M_LI:
8792     case M_LI_S:
8793       load_register (treg, &imm_expr, 0);
8794       break;
8795
8796     case M_DLI:
8797       load_register (treg, &imm_expr, 1);
8798       break;
8799
8800     case M_LI_SS:
8801       if (imm_expr.X_op == O_constant)
8802         {
8803           used_at = 1;
8804           load_register (AT, &imm_expr, 0);
8805           macro_build (NULL, "mtc1", "t,G", AT, treg);
8806           break;
8807         }
8808       else
8809         {
8810           gas_assert (offset_expr.X_op == O_symbol
8811                       && strcmp (segment_name (S_GET_SEGMENT
8812                                                (offset_expr.X_add_symbol)),
8813                                  ".lit4") == 0
8814                       && offset_expr.X_add_number == 0);
8815           macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
8816                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8817           break;
8818         }
8819
8820     case M_LI_D:
8821       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
8822          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
8823          order 32 bits of the value and the low order 32 bits are either
8824          zero or in OFFSET_EXPR.  */
8825       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8826         {
8827           if (HAVE_64BIT_GPRS)
8828             load_register (treg, &imm_expr, 1);
8829           else
8830             {
8831               int hreg, lreg;
8832
8833               if (target_big_endian)
8834                 {
8835                   hreg = treg;
8836                   lreg = treg + 1;
8837                 }
8838               else
8839                 {
8840                   hreg = treg + 1;
8841                   lreg = treg;
8842                 }
8843
8844               if (hreg <= 31)
8845                 load_register (hreg, &imm_expr, 0);
8846               if (lreg <= 31)
8847                 {
8848                   if (offset_expr.X_op == O_absent)
8849                     move_register (lreg, 0);
8850                   else
8851                     {
8852                       gas_assert (offset_expr.X_op == O_constant);
8853                       load_register (lreg, &offset_expr, 0);
8854                     }
8855                 }
8856             }
8857           break;
8858         }
8859
8860       /* We know that sym is in the .rdata section.  First we get the
8861          upper 16 bits of the address.  */
8862       if (mips_pic == NO_PIC)
8863         {
8864           macro_build_lui (&offset_expr, AT);
8865           used_at = 1;
8866         }
8867       else
8868         {
8869           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8870                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
8871           used_at = 1;
8872         }
8873
8874       /* Now we load the register(s).  */
8875       if (HAVE_64BIT_GPRS)
8876         {
8877           used_at = 1;
8878           macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8879         }
8880       else
8881         {
8882           used_at = 1;
8883           macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8884           if (treg != RA)
8885             {
8886               /* FIXME: How in the world do we deal with the possible
8887                  overflow here?  */
8888               offset_expr.X_add_number += 4;
8889               macro_build (&offset_expr, "lw", "t,o(b)",
8890                            treg + 1, BFD_RELOC_LO16, AT);
8891             }
8892         }
8893       break;
8894
8895     case M_LI_DD:
8896       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
8897          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8898          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
8899          the value and the low order 32 bits are either zero or in
8900          OFFSET_EXPR.  */
8901       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8902         {
8903           used_at = 1;
8904           load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
8905           if (HAVE_64BIT_FPRS)
8906             {
8907               gas_assert (HAVE_64BIT_GPRS);
8908               macro_build (NULL, "dmtc1", "t,S", AT, treg);
8909             }
8910           else
8911             {
8912               macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
8913               if (offset_expr.X_op == O_absent)
8914                 macro_build (NULL, "mtc1", "t,G", 0, treg);
8915               else
8916                 {
8917                   gas_assert (offset_expr.X_op == O_constant);
8918                   load_register (AT, &offset_expr, 0);
8919                   macro_build (NULL, "mtc1", "t,G", AT, treg);
8920                 }
8921             }
8922           break;
8923         }
8924
8925       gas_assert (offset_expr.X_op == O_symbol
8926                   && offset_expr.X_add_number == 0);
8927       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8928       if (strcmp (s, ".lit8") == 0)
8929         {
8930           if ((mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
8931             && (mips_opts.arch != CPU_R5900))
8932             {
8933               macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
8934                            BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8935               break;
8936             }
8937           breg = mips_gp_register;
8938           r = BFD_RELOC_MIPS_LITERAL;
8939           goto dob;
8940         }
8941       else
8942         {
8943           gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8944           used_at = 1;
8945           if (mips_pic != NO_PIC)
8946             macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8947                          BFD_RELOC_MIPS_GOT16, mips_gp_register);
8948           else
8949             {
8950               /* FIXME: This won't work for a 64 bit address.  */
8951               macro_build_lui (&offset_expr, AT);
8952             }
8953
8954           if ((mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
8955             && (mips_opts.arch != CPU_R5900))
8956             {
8957               macro_build (&offset_expr, "ldc1", "T,o(b)",
8958                            treg, BFD_RELOC_LO16, AT);
8959               break;
8960             }
8961           breg = AT;
8962           r = BFD_RELOC_LO16;
8963           goto dob;
8964         }
8965
8966     case M_L_DOB:
8967       /* Even on a big endian machine $fn comes before $fn+1.  We have
8968          to adjust when loading from memory.  */
8969       r = BFD_RELOC_LO16;
8970     dob:
8971       gas_assert (!mips_opts.micromips);
8972       gas_assert ((mips_opts.isa == ISA_MIPS1)
8973         || (mips_opts.arch == CPU_R5900));
8974       macro_build (&offset_expr, "lwc1", "T,o(b)",
8975                    target_big_endian ? treg + 1 : treg, r, breg);
8976       /* FIXME: A possible overflow which I don't know how to deal
8977          with.  */
8978       offset_expr.X_add_number += 4;
8979       macro_build (&offset_expr, "lwc1", "T,o(b)",
8980                    target_big_endian ? treg : treg + 1, r, breg);
8981       break;
8982
8983     case M_S_DOB:
8984       gas_assert (!mips_opts.micromips);
8985       gas_assert (mips_opts.isa == ISA_MIPS1);
8986       /* Even on a big endian machine $fn comes before $fn+1.  We have
8987          to adjust when storing to memory.  */
8988       macro_build (&offset_expr, "swc1", "T,o(b)",
8989                    target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8990       offset_expr.X_add_number += 4;
8991       macro_build (&offset_expr, "swc1", "T,o(b)",
8992                    target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8993       break;
8994
8995     case M_L_DAB:
8996       gas_assert (!mips_opts.micromips);
8997       /*
8998        * The MIPS assembler seems to check for X_add_number not
8999        * being double aligned and generating:
9000        *        lui     at,%hi(foo+1)
9001        *        addu    at,at,v1
9002        *        addiu   at,at,%lo(foo+1)
9003        *        lwc1    f2,0(at)
9004        *        lwc1    f3,4(at)
9005        * But, the resulting address is the same after relocation so why
9006        * generate the extra instruction?
9007        */
9008       /* Itbl support may require additional care here.  */
9009       coproc = 1;
9010       fmt = "T,o(b)";
9011       if ((mips_opts.isa != ISA_MIPS1) && (mips_opts.arch != CPU_R5900))
9012         {
9013           s = "ldc1";
9014           goto ld_st;
9015         }
9016       s = "lwc1";
9017       goto ldd_std;
9018
9019     case M_S_DAB:
9020       gas_assert (!mips_opts.micromips);
9021       /* Itbl support may require additional care here.  */
9022       coproc = 1;
9023       fmt = "T,o(b)";
9024       if ((mips_opts.isa != ISA_MIPS1) && (mips_opts.arch != CPU_R5900))
9025         {
9026           s = "sdc1";
9027           goto ld_st;
9028         }
9029       s = "swc1";
9030       goto ldd_std;
9031
9032     case M_LQ_AB:
9033       fmt = "t,o(b)";
9034       s = "lq";
9035       goto ld;
9036
9037     case M_SQ_AB:
9038       fmt = "t,o(b)";
9039       s = "sq";
9040       goto ld_st;
9041
9042     case M_LD_AB:
9043       fmt = "t,o(b)";
9044       if (HAVE_64BIT_GPRS)
9045         {
9046           s = "ld";
9047           goto ld;
9048         }
9049       s = "lw";
9050       goto ldd_std;
9051
9052     case M_SD_AB:
9053       fmt = "t,o(b)";
9054       if (HAVE_64BIT_GPRS)
9055         {
9056           s = "sd";
9057           goto ld_st;
9058         }
9059       s = "sw";
9060
9061     ldd_std:
9062       if (offset_expr.X_op != O_symbol
9063           && offset_expr.X_op != O_constant)
9064         {
9065           as_bad (_("Expression too complex"));
9066           offset_expr.X_op = O_constant;
9067         }
9068
9069       if (HAVE_32BIT_ADDRESSES
9070           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
9071         {
9072           char value [32];
9073
9074           sprintf_vma (value, offset_expr.X_add_number);
9075           as_bad (_("Number (0x%s) larger than 32 bits"), value);
9076         }
9077
9078       /* Even on a big endian machine $fn comes before $fn+1.  We have
9079          to adjust when loading from memory.  We set coproc if we must
9080          load $fn+1 first.  */
9081       /* Itbl support may require additional care here.  */
9082       if (!target_big_endian)
9083         coproc = 0;
9084
9085       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
9086         {
9087           /* If this is a reference to a GP relative symbol, we want
9088                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
9089                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
9090              If we have a base register, we use this
9091                addu     $at,$breg,$gp
9092                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
9093                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
9094              If this is not a GP relative symbol, we want
9095                lui      $at,<sym>               (BFD_RELOC_HI16_S)
9096                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9097                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9098              If there is a base register, we add it to $at after the
9099              lui instruction.  If there is a constant, we always use
9100              the last case.  */
9101           if (offset_expr.X_op == O_symbol
9102               && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9103               && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9104             {
9105               relax_start (offset_expr.X_add_symbol);
9106               if (breg == 0)
9107                 {
9108                   tempreg = mips_gp_register;
9109                 }
9110               else
9111                 {
9112                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9113                                AT, breg, mips_gp_register);
9114                   tempreg = AT;
9115                   used_at = 1;
9116                 }
9117
9118               /* Itbl support may require additional care here.  */
9119               macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9120                            BFD_RELOC_GPREL16, tempreg);
9121               offset_expr.X_add_number += 4;
9122
9123               /* Set mips_optimize to 2 to avoid inserting an
9124                  undesired nop.  */
9125               hold_mips_optimize = mips_optimize;
9126               mips_optimize = 2;
9127               /* Itbl support may require additional care here.  */
9128               macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9129                            BFD_RELOC_GPREL16, tempreg);
9130               mips_optimize = hold_mips_optimize;
9131
9132               relax_switch ();
9133
9134               offset_expr.X_add_number -= 4;
9135             }
9136           used_at = 1;
9137           macro_build_lui (&offset_expr, AT);
9138           if (breg != 0)
9139             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9140           /* Itbl support may require additional care here.  */
9141           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9142                        BFD_RELOC_LO16, AT);
9143           /* FIXME: How do we handle overflow here?  */
9144           offset_expr.X_add_number += 4;
9145           /* Itbl support may require additional care here.  */
9146           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9147                        BFD_RELOC_LO16, AT);
9148           if (mips_relax.sequence)
9149             relax_end ();
9150         }
9151       else if (!mips_big_got)
9152         {
9153           /* If this is a reference to an external symbol, we want
9154                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9155                nop
9156                <op>     $treg,0($at)
9157                <op>     $treg+1,4($at)
9158              Otherwise we want
9159                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9160                nop
9161                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9162                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9163              If there is a base register we add it to $at before the
9164              lwc1 instructions.  If there is a constant we include it
9165              in the lwc1 instructions.  */
9166           used_at = 1;
9167           expr1.X_add_number = offset_expr.X_add_number;
9168           if (expr1.X_add_number < -0x8000
9169               || expr1.X_add_number >= 0x8000 - 4)
9170             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9171           load_got_offset (AT, &offset_expr);
9172           load_delay_nop ();
9173           if (breg != 0)
9174             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9175
9176           /* Set mips_optimize to 2 to avoid inserting an undesired
9177              nop.  */
9178           hold_mips_optimize = mips_optimize;
9179           mips_optimize = 2;
9180
9181           /* Itbl support may require additional care here.  */
9182           relax_start (offset_expr.X_add_symbol);
9183           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9184                        BFD_RELOC_LO16, AT);
9185           expr1.X_add_number += 4;
9186           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9187                        BFD_RELOC_LO16, AT);
9188           relax_switch ();
9189           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9190                        BFD_RELOC_LO16, AT);
9191           offset_expr.X_add_number += 4;
9192           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9193                        BFD_RELOC_LO16, AT);
9194           relax_end ();
9195
9196           mips_optimize = hold_mips_optimize;
9197         }
9198       else if (mips_big_got)
9199         {
9200           int gpdelay;
9201
9202           /* If this is a reference to an external symbol, we want
9203                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
9204                addu     $at,$at,$gp
9205                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
9206                nop
9207                <op>     $treg,0($at)
9208                <op>     $treg+1,4($at)
9209              Otherwise we want
9210                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9211                nop
9212                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9213                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9214              If there is a base register we add it to $at before the
9215              lwc1 instructions.  If there is a constant we include it
9216              in the lwc1 instructions.  */
9217           used_at = 1;
9218           expr1.X_add_number = offset_expr.X_add_number;
9219           offset_expr.X_add_number = 0;
9220           if (expr1.X_add_number < -0x8000
9221               || expr1.X_add_number >= 0x8000 - 4)
9222             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9223           gpdelay = reg_needs_delay (mips_gp_register);
9224           relax_start (offset_expr.X_add_symbol);
9225           macro_build (&offset_expr, "lui", LUI_FMT,
9226                        AT, BFD_RELOC_MIPS_GOT_HI16);
9227           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9228                        AT, AT, mips_gp_register);
9229           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9230                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9231           load_delay_nop ();
9232           if (breg != 0)
9233             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9234           /* Itbl support may require additional care here.  */
9235           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9236                        BFD_RELOC_LO16, AT);
9237           expr1.X_add_number += 4;
9238
9239           /* Set mips_optimize to 2 to avoid inserting an undesired
9240              nop.  */
9241           hold_mips_optimize = mips_optimize;
9242           mips_optimize = 2;
9243           /* Itbl support may require additional care here.  */
9244           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9245                        BFD_RELOC_LO16, AT);
9246           mips_optimize = hold_mips_optimize;
9247           expr1.X_add_number -= 4;
9248
9249           relax_switch ();
9250           offset_expr.X_add_number = expr1.X_add_number;
9251           if (gpdelay)
9252             macro_build (NULL, "nop", "");
9253           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9254                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
9255           load_delay_nop ();
9256           if (breg != 0)
9257             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9258           /* Itbl support may require additional care here.  */
9259           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9260                        BFD_RELOC_LO16, AT);
9261           offset_expr.X_add_number += 4;
9262
9263           /* Set mips_optimize to 2 to avoid inserting an undesired
9264              nop.  */
9265           hold_mips_optimize = mips_optimize;
9266           mips_optimize = 2;
9267           /* Itbl support may require additional care here.  */
9268           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9269                        BFD_RELOC_LO16, AT);
9270           mips_optimize = hold_mips_optimize;
9271           relax_end ();
9272         }
9273       else
9274         abort ();
9275
9276       break;
9277
9278     case M_LD_OB:
9279       s = HAVE_64BIT_GPRS ? "ld" : "lw";
9280       goto sd_ob;
9281     case M_SD_OB:
9282       s = HAVE_64BIT_GPRS ? "sd" : "sw";
9283     sd_ob:
9284       macro_build (&offset_expr, s, "t,o(b)", treg,
9285                    -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9286                    breg);
9287       if (!HAVE_64BIT_GPRS)
9288         {
9289           offset_expr.X_add_number += 4;
9290           macro_build (&offset_expr, s, "t,o(b)", treg + 1,
9291                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9292                        breg);
9293         }
9294       break;
9295
9296         
9297     case M_SAA_AB:
9298       ab = 1;
9299     case M_SAA_OB:
9300       s = "saa";
9301       off0 = 1;
9302       fmt = "t,(b)";
9303       goto ld_st;
9304     case M_SAAD_AB:
9305       ab = 1;
9306     case M_SAAD_OB:
9307       s = "saad";
9308       off0 = 1;
9309       fmt = "t,(b)";
9310       goto ld_st;
9311
9312    /* New code added to support COPZ instructions.
9313       This code builds table entries out of the macros in mip_opcodes.
9314       R4000 uses interlocks to handle coproc delays.
9315       Other chips (like the R3000) require nops to be inserted for delays.
9316
9317       FIXME: Currently, we require that the user handle delays.
9318       In order to fill delay slots for non-interlocked chips,
9319       we must have a way to specify delays based on the coprocessor.
9320       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9321       What are the side-effects of the cop instruction?
9322       What cache support might we have and what are its effects?
9323       Both coprocessor & memory require delays. how long???
9324       What registers are read/set/modified?
9325
9326       If an itbl is provided to interpret cop instructions,
9327       this knowledge can be encoded in the itbl spec.  */
9328
9329     case M_COP0:
9330       s = "c0";
9331       goto copz;
9332     case M_COP1:
9333       s = "c1";
9334       goto copz;
9335     case M_COP2:
9336       s = "c2";
9337       goto copz;
9338     case M_COP3:
9339       s = "c3";
9340     copz:
9341       gas_assert (!mips_opts.micromips);
9342       /* For now we just do C (same as Cz).  The parameter will be
9343          stored in insn_opcode by mips_ip.  */
9344       macro_build (NULL, s, "C", ip->insn_opcode);
9345       break;
9346
9347     case M_MOVE:
9348       move_register (dreg, sreg);
9349       break;
9350
9351     case M_DMUL:
9352       dbl = 1;
9353     case M_MUL:
9354       if (mips_opts.arch == CPU_R5900)
9355         {
9356           macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9357         }
9358       else
9359         {
9360       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9361       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9362         }
9363       break;
9364
9365     case M_DMUL_I:
9366       dbl = 1;
9367     case M_MUL_I:
9368       /* The MIPS assembler some times generates shifts and adds.  I'm
9369          not trying to be that fancy. GCC should do this for us
9370          anyway.  */
9371       used_at = 1;
9372       load_register (AT, &imm_expr, dbl);
9373       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9374       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9375       break;
9376
9377     case M_DMULO_I:
9378       dbl = 1;
9379     case M_MULO_I:
9380       imm = 1;
9381       goto do_mulo;
9382
9383     case M_DMULO:
9384       dbl = 1;
9385     case M_MULO:
9386     do_mulo:
9387       start_noreorder ();
9388       used_at = 1;
9389       if (imm)
9390         load_register (AT, &imm_expr, dbl);
9391       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9392       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9393       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9394       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9395       if (mips_trap)
9396         macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9397       else
9398         {
9399           if (mips_opts.micromips)
9400             micromips_label_expr (&label_expr);
9401           else
9402             label_expr.X_add_number = 8;
9403           macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9404           macro_build (NULL, "nop", "");
9405           macro_build (NULL, "break", BRK_FMT, 6);
9406           if (mips_opts.micromips)
9407             micromips_add_label ();
9408         }
9409       end_noreorder ();
9410       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9411       break;
9412
9413     case M_DMULOU_I:
9414       dbl = 1;
9415     case M_MULOU_I:
9416       imm = 1;
9417       goto do_mulou;
9418
9419     case M_DMULOU:
9420       dbl = 1;
9421     case M_MULOU:
9422     do_mulou:
9423       start_noreorder ();
9424       used_at = 1;
9425       if (imm)
9426         load_register (AT, &imm_expr, dbl);
9427       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9428                    sreg, imm ? AT : treg);
9429       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9430       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9431       if (mips_trap)
9432         macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9433       else
9434         {
9435           if (mips_opts.micromips)
9436             micromips_label_expr (&label_expr);
9437           else
9438             label_expr.X_add_number = 8;
9439           macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9440           macro_build (NULL, "nop", "");
9441           macro_build (NULL, "break", BRK_FMT, 6);
9442           if (mips_opts.micromips)
9443             micromips_add_label ();
9444         }
9445       end_noreorder ();
9446       break;
9447
9448     case M_DROL:
9449       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9450         {
9451           if (dreg == sreg)
9452             {
9453               tempreg = AT;
9454               used_at = 1;
9455             }
9456           else
9457             {
9458               tempreg = dreg;
9459             }
9460           macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9461           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9462           break;
9463         }
9464       used_at = 1;
9465       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9466       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9467       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9468       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9469       break;
9470
9471     case M_ROL:
9472       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9473         {
9474           if (dreg == sreg)
9475             {
9476               tempreg = AT;
9477               used_at = 1;
9478             }
9479           else
9480             {
9481               tempreg = dreg;
9482             }
9483           macro_build (NULL, "negu", "d,w", tempreg, treg);
9484           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9485           break;
9486         }
9487       used_at = 1;
9488       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9489       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9490       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9491       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9492       break;
9493
9494     case M_DROL_I:
9495       {
9496         unsigned int rot;
9497         char *l;
9498         char *rr;
9499
9500         if (imm_expr.X_op != O_constant)
9501           as_bad (_("Improper rotate count"));
9502         rot = imm_expr.X_add_number & 0x3f;
9503         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9504           {
9505             rot = (64 - rot) & 0x3f;
9506             if (rot >= 32)
9507               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9508             else
9509               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9510             break;
9511           }
9512         if (rot == 0)
9513           {
9514             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9515             break;
9516           }
9517         l = (rot < 0x20) ? "dsll" : "dsll32";
9518         rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9519         rot &= 0x1f;
9520         used_at = 1;
9521         macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9522         macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9523         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9524       }
9525       break;
9526
9527     case M_ROL_I:
9528       {
9529         unsigned int rot;
9530
9531         if (imm_expr.X_op != O_constant)
9532           as_bad (_("Improper rotate count"));
9533         rot = imm_expr.X_add_number & 0x1f;
9534         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9535           {
9536             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9537             break;
9538           }
9539         if (rot == 0)
9540           {
9541             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9542             break;
9543           }
9544         used_at = 1;
9545         macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9546         macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9547         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9548       }
9549       break;
9550
9551     case M_DROR:
9552       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9553         {
9554           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9555           break;
9556         }
9557       used_at = 1;
9558       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9559       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9560       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9561       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9562       break;
9563
9564     case M_ROR:
9565       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9566         {
9567           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9568           break;
9569         }
9570       used_at = 1;
9571       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9572       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9573       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9574       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9575       break;
9576
9577     case M_DROR_I:
9578       {
9579         unsigned int rot;
9580         char *l;
9581         char *rr;
9582
9583         if (imm_expr.X_op != O_constant)
9584           as_bad (_("Improper rotate count"));
9585         rot = imm_expr.X_add_number & 0x3f;
9586         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9587           {
9588             if (rot >= 32)
9589               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9590             else
9591               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9592             break;
9593           }
9594         if (rot == 0)
9595           {
9596             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9597             break;
9598           }
9599         rr = (rot < 0x20) ? "dsrl" : "dsrl32";
9600         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9601         rot &= 0x1f;
9602         used_at = 1;
9603         macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9604         macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9605         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9606       }
9607       break;
9608
9609     case M_ROR_I:
9610       {
9611         unsigned int rot;
9612
9613         if (imm_expr.X_op != O_constant)
9614           as_bad (_("Improper rotate count"));
9615         rot = imm_expr.X_add_number & 0x1f;
9616         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9617           {
9618             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
9619             break;
9620           }
9621         if (rot == 0)
9622           {
9623             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9624             break;
9625           }
9626         used_at = 1;
9627         macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9628         macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9629         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9630       }
9631       break;
9632
9633     case M_SEQ:
9634       if (sreg == 0)
9635         macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
9636       else if (treg == 0)
9637         macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9638       else
9639         {
9640           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9641           macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9642         }
9643       break;
9644
9645     case M_SEQ_I:
9646       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9647         {
9648           macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9649           break;
9650         }
9651       if (sreg == 0)
9652         {
9653           as_warn (_("Instruction %s: result is always false"),
9654                    ip->insn_mo->name);
9655           move_register (dreg, 0);
9656           break;
9657         }
9658       if (CPU_HAS_SEQ (mips_opts.arch)
9659           && -512 <= imm_expr.X_add_number
9660           && imm_expr.X_add_number < 512)
9661         {
9662           macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
9663                        (int) imm_expr.X_add_number);
9664           break;
9665         }
9666       if (imm_expr.X_op == O_constant
9667           && imm_expr.X_add_number >= 0
9668           && imm_expr.X_add_number < 0x10000)
9669         {
9670           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9671         }
9672       else if (imm_expr.X_op == O_constant
9673                && imm_expr.X_add_number > -0x8000
9674                && imm_expr.X_add_number < 0)
9675         {
9676           imm_expr.X_add_number = -imm_expr.X_add_number;
9677           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9678                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9679         }
9680       else if (CPU_HAS_SEQ (mips_opts.arch))
9681         {
9682           used_at = 1;
9683           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9684           macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9685           break;
9686         }
9687       else
9688         {
9689           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9690           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9691           used_at = 1;
9692         }
9693       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9694       break;
9695
9696     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
9697       s = "slt";
9698       goto sge;
9699     case M_SGEU:
9700       s = "sltu";
9701     sge:
9702       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9703       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9704       break;
9705
9706     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
9707     case M_SGEU_I:
9708       if (imm_expr.X_op == O_constant
9709           && imm_expr.X_add_number >= -0x8000
9710           && imm_expr.X_add_number < 0x8000)
9711         {
9712           macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9713                        dreg, sreg, BFD_RELOC_LO16);
9714         }
9715       else
9716         {
9717           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9718           macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9719                        dreg, sreg, AT);
9720           used_at = 1;
9721         }
9722       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9723       break;
9724
9725     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
9726       s = "slt";
9727       goto sgt;
9728     case M_SGTU:
9729       s = "sltu";
9730     sgt:
9731       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9732       break;
9733
9734     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
9735       s = "slt";
9736       goto sgti;
9737     case M_SGTU_I:
9738       s = "sltu";
9739     sgti:
9740       used_at = 1;
9741       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9742       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9743       break;
9744
9745     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
9746       s = "slt";
9747       goto sle;
9748     case M_SLEU:
9749       s = "sltu";
9750     sle:
9751       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9752       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9753       break;
9754
9755     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
9756       s = "slt";
9757       goto slei;
9758     case M_SLEU_I:
9759       s = "sltu";
9760     slei:
9761       used_at = 1;
9762       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9763       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9764       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9765       break;
9766
9767     case M_SLT_I:
9768       if (imm_expr.X_op == O_constant
9769           && imm_expr.X_add_number >= -0x8000
9770           && imm_expr.X_add_number < 0x8000)
9771         {
9772           macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9773           break;
9774         }
9775       used_at = 1;
9776       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9777       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
9778       break;
9779
9780     case M_SLTU_I:
9781       if (imm_expr.X_op == O_constant
9782           && imm_expr.X_add_number >= -0x8000
9783           && imm_expr.X_add_number < 0x8000)
9784         {
9785           macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
9786                        BFD_RELOC_LO16);
9787           break;
9788         }
9789       used_at = 1;
9790       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9791       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
9792       break;
9793
9794     case M_SNE:
9795       if (sreg == 0)
9796         macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
9797       else if (treg == 0)
9798         macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9799       else
9800         {
9801           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9802           macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9803         }
9804       break;
9805
9806     case M_SNE_I:
9807       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9808         {
9809           macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9810           break;
9811         }
9812       if (sreg == 0)
9813         {
9814           as_warn (_("Instruction %s: result is always true"),
9815                    ip->insn_mo->name);
9816           macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9817                        dreg, 0, BFD_RELOC_LO16);
9818           break;
9819         }
9820       if (CPU_HAS_SEQ (mips_opts.arch)
9821           && -512 <= imm_expr.X_add_number
9822           && imm_expr.X_add_number < 512)
9823         {
9824           macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
9825                        (int) imm_expr.X_add_number);
9826           break;
9827         }
9828       if (imm_expr.X_op == O_constant
9829           && imm_expr.X_add_number >= 0
9830           && imm_expr.X_add_number < 0x10000)
9831         {
9832           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9833         }
9834       else if (imm_expr.X_op == O_constant
9835                && imm_expr.X_add_number > -0x8000
9836                && imm_expr.X_add_number < 0)
9837         {
9838           imm_expr.X_add_number = -imm_expr.X_add_number;
9839           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9840                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9841         }
9842       else if (CPU_HAS_SEQ (mips_opts.arch))
9843         {
9844           used_at = 1;
9845           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9846           macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9847           break;
9848         }
9849       else
9850         {
9851           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9852           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9853           used_at = 1;
9854         }
9855       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9856       break;
9857
9858     case M_SUB_I:
9859       s = "addi";
9860       s2 = "sub";
9861       goto do_subi;
9862     case M_SUBU_I:
9863       s = "addiu";
9864       s2 = "subu";
9865       goto do_subi;
9866     case M_DSUB_I:
9867       dbl = 1;
9868       s = "daddi";
9869       s2 = "dsub";
9870       if (!mips_opts.micromips)
9871         goto do_subi;
9872       if (imm_expr.X_op == O_constant
9873           && imm_expr.X_add_number > -0x200
9874           && imm_expr.X_add_number <= 0x200)
9875         {
9876           macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
9877           break;
9878         }
9879       goto do_subi_i;
9880     case M_DSUBU_I:
9881       dbl = 1;
9882       s = "daddiu";
9883       s2 = "dsubu";
9884     do_subi:
9885       if (imm_expr.X_op == O_constant
9886           && imm_expr.X_add_number > -0x8000
9887           && imm_expr.X_add_number <= 0x8000)
9888         {
9889           imm_expr.X_add_number = -imm_expr.X_add_number;
9890           macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9891           break;
9892         }
9893     do_subi_i:
9894       used_at = 1;
9895       load_register (AT, &imm_expr, dbl);
9896       macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
9897       break;
9898
9899     case M_TEQ_I:
9900       s = "teq";
9901       goto trap;
9902     case M_TGE_I:
9903       s = "tge";
9904       goto trap;
9905     case M_TGEU_I:
9906       s = "tgeu";
9907       goto trap;
9908     case M_TLT_I:
9909       s = "tlt";
9910       goto trap;
9911     case M_TLTU_I:
9912       s = "tltu";
9913       goto trap;
9914     case M_TNE_I:
9915       s = "tne";
9916     trap:
9917       used_at = 1;
9918       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9919       macro_build (NULL, s, "s,t", sreg, AT);
9920       break;
9921
9922     case M_TRUNCWS:
9923     case M_TRUNCWD:
9924       gas_assert (!mips_opts.micromips);
9925       gas_assert ((mips_opts.isa == ISA_MIPS1) || (mips_opts.arch == CPU_R5900));
9926       used_at = 1;
9927       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
9928       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
9929
9930       /*
9931        * Is the double cfc1 instruction a bug in the mips assembler;
9932        * or is there a reason for it?
9933        */
9934       start_noreorder ();
9935       macro_build (NULL, "cfc1", "t,G", treg, RA);
9936       macro_build (NULL, "cfc1", "t,G", treg, RA);
9937       macro_build (NULL, "nop", "");
9938       expr1.X_add_number = 3;
9939       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
9940       expr1.X_add_number = 2;
9941       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9942       macro_build (NULL, "ctc1", "t,G", AT, RA);
9943       macro_build (NULL, "nop", "");
9944       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9945                    dreg, sreg);
9946       macro_build (NULL, "ctc1", "t,G", treg, RA);
9947       macro_build (NULL, "nop", "");
9948       end_noreorder ();
9949       break;
9950
9951     case M_ULH_A:
9952       ab = 1;
9953     case M_ULH:
9954       s = "lb";
9955       s2 = "lbu";
9956       off = 1;
9957       goto uld_st;
9958     case M_ULHU_A:
9959       ab = 1;
9960     case M_ULHU:
9961       s = "lbu";
9962       s2 = "lbu";
9963       off = 1;
9964       goto uld_st;
9965     case M_ULW_A:
9966       ab = 1;
9967     case M_ULW:
9968       s = "lwl";
9969       s2 = "lwr";
9970       off12 = mips_opts.micromips;
9971       off = 3;
9972       goto uld_st;
9973     case M_ULD_A:
9974       ab = 1;
9975     case M_ULD:
9976       s = "ldl";
9977       s2 = "ldr";
9978       off12 = mips_opts.micromips;
9979       off = 7;
9980       goto uld_st;
9981     case M_USH_A:
9982       ab = 1;
9983     case M_USH:
9984       s = "sb";
9985       s2 = "sb";
9986       off = 1;
9987       ust = 1;
9988       goto uld_st;
9989     case M_USW_A:
9990       ab = 1;
9991     case M_USW:
9992       s = "swl";
9993       s2 = "swr";
9994       off12 = mips_opts.micromips;
9995       off = 3;
9996       ust = 1;
9997       goto uld_st;
9998     case M_USD_A:
9999       ab = 1;
10000     case M_USD:
10001       s = "sdl";
10002       s2 = "sdr";
10003       off12 = mips_opts.micromips;
10004       off = 7;
10005       ust = 1;
10006
10007     uld_st:
10008       if (!ab && offset_expr.X_add_number >= 0x8000 - off)
10009         as_bad (_("Operand overflow"));
10010
10011       ep = &offset_expr;
10012       expr1.X_add_number = 0;
10013       if (ab)
10014         {
10015           used_at = 1;
10016           tempreg = AT;
10017           load_address (tempreg, ep, &used_at);
10018           if (breg != 0)
10019             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10020                          tempreg, tempreg, breg);
10021           breg = tempreg;
10022           tempreg = treg;
10023           ep = &expr1;
10024         }
10025       else if (off12
10026                && (offset_expr.X_op != O_constant
10027                    || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10028                    || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10029         {
10030           used_at = 1;
10031           tempreg = AT;
10032           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10033                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10034           breg = tempreg;
10035           tempreg = treg;
10036           ep = &expr1;
10037         }
10038       else if (!ust && treg == breg)
10039         {
10040           used_at = 1;
10041           tempreg = AT;
10042         }
10043       else
10044         tempreg = treg;
10045
10046       if (off == 1)
10047         goto ulh_sh;
10048
10049       if (!target_big_endian)
10050         ep->X_add_number += off;
10051       if (!off12)
10052         macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10053       else
10054         macro_build (NULL, s, "t,~(b)",
10055                      tempreg, (unsigned long) ep->X_add_number, breg);
10056
10057       if (!target_big_endian)
10058         ep->X_add_number -= off;
10059       else
10060         ep->X_add_number += off;
10061       if (!off12)
10062         macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10063       else
10064         macro_build (NULL, s2, "t,~(b)",
10065                      tempreg, (unsigned long) ep->X_add_number, breg);
10066
10067       /* If necessary, move the result in tempreg to the final destination.  */
10068       if (!ust && treg != tempreg)
10069         {
10070           /* Protect second load's delay slot.  */
10071           load_delay_nop ();
10072           move_register (treg, tempreg);
10073         }
10074       break;
10075
10076     ulh_sh:
10077       used_at = 1;
10078       if (target_big_endian == ust)
10079         ep->X_add_number += off;
10080       tempreg = ust || ab ? treg : AT;
10081       macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10082
10083       /* For halfword transfers we need a temporary register to shuffle
10084          bytes.  Unfortunately for M_USH_A we have none available before
10085          the next store as AT holds the base address.  We deal with this
10086          case by clobbering TREG and then restoring it as with ULH.  */
10087       tempreg = ust == ab ? treg : AT;
10088       if (ust)
10089         macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10090
10091       if (target_big_endian == ust)
10092         ep->X_add_number -= off;
10093       else
10094         ep->X_add_number += off;
10095       macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10096
10097       /* For M_USH_A re-retrieve the LSB.  */
10098       if (ust && ab)
10099         {
10100           if (target_big_endian)
10101             ep->X_add_number += off;
10102           else
10103             ep->X_add_number -= off;
10104           macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10105         }
10106       /* For ULH and M_USH_A OR the LSB in.  */
10107       if (!ust || ab)
10108         {
10109           tempreg = !ab ? AT : treg;
10110           macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10111           macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10112         }
10113       break;
10114
10115     default:
10116       /* FIXME: Check if this is one of the itbl macros, since they
10117          are added dynamically.  */
10118       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10119       break;
10120     }
10121   if (!mips_opts.at && used_at)
10122     as_bad (_("Macro used $at after \".set noat\""));
10123 }
10124
10125 /* Implement macros in mips16 mode.  */
10126
10127 static void
10128 mips16_macro (struct mips_cl_insn *ip)
10129 {
10130   int mask;
10131   int xreg, yreg, zreg, tmp;
10132   expressionS expr1;
10133   int dbl;
10134   const char *s, *s2, *s3;
10135
10136   mask = ip->insn_mo->mask;
10137
10138   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10139   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10140   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10141
10142   expr1.X_op = O_constant;
10143   expr1.X_op_symbol = NULL;
10144   expr1.X_add_symbol = NULL;
10145   expr1.X_add_number = 1;
10146
10147   dbl = 0;
10148
10149   switch (mask)
10150     {
10151     default:
10152       abort ();
10153
10154     case M_DDIV_3:
10155       dbl = 1;
10156     case M_DIV_3:
10157       s = "mflo";
10158       goto do_div3;
10159     case M_DREM_3:
10160       dbl = 1;
10161     case M_REM_3:
10162       s = "mfhi";
10163     do_div3:
10164       start_noreorder ();
10165       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10166       expr1.X_add_number = 2;
10167       macro_build (&expr1, "bnez", "x,p", yreg);
10168       macro_build (NULL, "break", "6", 7);
10169
10170       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10171          since that causes an overflow.  We should do that as well,
10172          but I don't see how to do the comparisons without a temporary
10173          register.  */
10174       end_noreorder ();
10175       macro_build (NULL, s, "x", zreg);
10176       break;
10177
10178     case M_DIVU_3:
10179       s = "divu";
10180       s2 = "mflo";
10181       goto do_divu3;
10182     case M_REMU_3:
10183       s = "divu";
10184       s2 = "mfhi";
10185       goto do_divu3;
10186     case M_DDIVU_3:
10187       s = "ddivu";
10188       s2 = "mflo";
10189       goto do_divu3;
10190     case M_DREMU_3:
10191       s = "ddivu";
10192       s2 = "mfhi";
10193     do_divu3:
10194       start_noreorder ();
10195       macro_build (NULL, s, "0,x,y", xreg, yreg);
10196       expr1.X_add_number = 2;
10197       macro_build (&expr1, "bnez", "x,p", yreg);
10198       macro_build (NULL, "break", "6", 7);
10199       end_noreorder ();
10200       macro_build (NULL, s2, "x", zreg);
10201       break;
10202
10203     case M_DMUL:
10204       dbl = 1;
10205     case M_MUL:
10206       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10207       macro_build (NULL, "mflo", "x", zreg);
10208       break;
10209
10210     case M_DSUBU_I:
10211       dbl = 1;
10212       goto do_subu;
10213     case M_SUBU_I:
10214     do_subu:
10215       if (imm_expr.X_op != O_constant)
10216         as_bad (_("Unsupported large constant"));
10217       imm_expr.X_add_number = -imm_expr.X_add_number;
10218       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10219       break;
10220
10221     case M_SUBU_I_2:
10222       if (imm_expr.X_op != O_constant)
10223         as_bad (_("Unsupported large constant"));
10224       imm_expr.X_add_number = -imm_expr.X_add_number;
10225       macro_build (&imm_expr, "addiu", "x,k", xreg);
10226       break;
10227
10228     case M_DSUBU_I_2:
10229       if (imm_expr.X_op != O_constant)
10230         as_bad (_("Unsupported large constant"));
10231       imm_expr.X_add_number = -imm_expr.X_add_number;
10232       macro_build (&imm_expr, "daddiu", "y,j", yreg);
10233       break;
10234
10235     case M_BEQ:
10236       s = "cmp";
10237       s2 = "bteqz";
10238       goto do_branch;
10239     case M_BNE:
10240       s = "cmp";
10241       s2 = "btnez";
10242       goto do_branch;
10243     case M_BLT:
10244       s = "slt";
10245       s2 = "btnez";
10246       goto do_branch;
10247     case M_BLTU:
10248       s = "sltu";
10249       s2 = "btnez";
10250       goto do_branch;
10251     case M_BLE:
10252       s = "slt";
10253       s2 = "bteqz";
10254       goto do_reverse_branch;
10255     case M_BLEU:
10256       s = "sltu";
10257       s2 = "bteqz";
10258       goto do_reverse_branch;
10259     case M_BGE:
10260       s = "slt";
10261       s2 = "bteqz";
10262       goto do_branch;
10263     case M_BGEU:
10264       s = "sltu";
10265       s2 = "bteqz";
10266       goto do_branch;
10267     case M_BGT:
10268       s = "slt";
10269       s2 = "btnez";
10270       goto do_reverse_branch;
10271     case M_BGTU:
10272       s = "sltu";
10273       s2 = "btnez";
10274
10275     do_reverse_branch:
10276       tmp = xreg;
10277       xreg = yreg;
10278       yreg = tmp;
10279
10280     do_branch:
10281       macro_build (NULL, s, "x,y", xreg, yreg);
10282       macro_build (&offset_expr, s2, "p");
10283       break;
10284
10285     case M_BEQ_I:
10286       s = "cmpi";
10287       s2 = "bteqz";
10288       s3 = "x,U";
10289       goto do_branch_i;
10290     case M_BNE_I:
10291       s = "cmpi";
10292       s2 = "btnez";
10293       s3 = "x,U";
10294       goto do_branch_i;
10295     case M_BLT_I:
10296       s = "slti";
10297       s2 = "btnez";
10298       s3 = "x,8";
10299       goto do_branch_i;
10300     case M_BLTU_I:
10301       s = "sltiu";
10302       s2 = "btnez";
10303       s3 = "x,8";
10304       goto do_branch_i;
10305     case M_BLE_I:
10306       s = "slti";
10307       s2 = "btnez";
10308       s3 = "x,8";
10309       goto do_addone_branch_i;
10310     case M_BLEU_I:
10311       s = "sltiu";
10312       s2 = "btnez";
10313       s3 = "x,8";
10314       goto do_addone_branch_i;
10315     case M_BGE_I:
10316       s = "slti";
10317       s2 = "bteqz";
10318       s3 = "x,8";
10319       goto do_branch_i;
10320     case M_BGEU_I:
10321       s = "sltiu";
10322       s2 = "bteqz";
10323       s3 = "x,8";
10324       goto do_branch_i;
10325     case M_BGT_I:
10326       s = "slti";
10327       s2 = "bteqz";
10328       s3 = "x,8";
10329       goto do_addone_branch_i;
10330     case M_BGTU_I:
10331       s = "sltiu";
10332       s2 = "bteqz";
10333       s3 = "x,8";
10334
10335     do_addone_branch_i:
10336       if (imm_expr.X_op != O_constant)
10337         as_bad (_("Unsupported large constant"));
10338       ++imm_expr.X_add_number;
10339
10340     do_branch_i:
10341       macro_build (&imm_expr, s, s3, xreg);
10342       macro_build (&offset_expr, s2, "p");
10343       break;
10344
10345     case M_ABS:
10346       expr1.X_add_number = 0;
10347       macro_build (&expr1, "slti", "x,8", yreg);
10348       if (xreg != yreg)
10349         move_register (xreg, yreg);
10350       expr1.X_add_number = 2;
10351       macro_build (&expr1, "bteqz", "p");
10352       macro_build (NULL, "neg", "x,w", xreg, xreg);
10353     }
10354 }
10355
10356 /* For consistency checking, verify that all bits are specified either
10357    by the match/mask part of the instruction definition, or by the
10358    operand list.  */
10359 static int
10360 validate_mips_insn (const struct mips_opcode *opc)
10361 {
10362   const char *p = opc->args;
10363   char c;
10364   unsigned long used_bits = opc->mask;
10365
10366   if ((used_bits & opc->match) != opc->match)
10367     {
10368       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10369               opc->name, opc->args);
10370       return 0;
10371     }
10372 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
10373   while (*p)
10374     switch (c = *p++)
10375       {
10376       case ',': break;
10377       case '(': break;
10378       case ')': break;
10379       case '+':
10380         switch (c = *p++)
10381           {
10382           case '1': USE_BITS (OP_MASK_UDI1,     OP_SH_UDI1);    break;
10383           case '2': USE_BITS (OP_MASK_UDI2,     OP_SH_UDI2);    break;
10384           case '3': USE_BITS (OP_MASK_UDI3,     OP_SH_UDI3);    break;
10385           case '4': USE_BITS (OP_MASK_UDI4,     OP_SH_UDI4);    break;
10386           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10387           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10388           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10389           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
10390                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
10391           case 'E': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10392           case 'F': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10393           case 'G': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10394           case 'H': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10395           case 'I': break;
10396           case 't': USE_BITS (OP_MASK_RT,       OP_SH_RT);      break;
10397           case 'T': USE_BITS (OP_MASK_RT,       OP_SH_RT);
10398                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
10399           case 'x': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10400           case 'X': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10401           case 'p': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10402           case 'P': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10403           case 'Q': USE_BITS (OP_MASK_SEQI,     OP_SH_SEQI);    break;
10404           case 's': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10405           case 'S': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10406           case 'z': USE_BITS (OP_MASK_RZ,       OP_SH_RZ);      break;
10407           case 'Z': USE_BITS (OP_MASK_FZ,       OP_SH_FZ);      break;
10408           case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10409           case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10410           case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
10411
10412           default:
10413             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10414                     c, opc->name, opc->args);
10415             return 0;
10416           }
10417         break;
10418       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10419       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10420       case 'A': break;
10421       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
10422       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
10423       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10424       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10425       case 'F': break;
10426       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10427       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
10428       case 'I': break;
10429       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
10430       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10431       case 'L': break;
10432       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
10433       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
10434       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
10435       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
10436                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10437       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
10438       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10439       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10440       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10441       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10442       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10443       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10444       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10445       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
10446       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10447       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
10448       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10449       case 'f': break;
10450       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
10451       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10452       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10453       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
10454       case 'l': break;
10455       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10456       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10457       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
10458       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10459       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10460       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10461       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10462       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10463       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10464       case 'x': break;
10465       case 'z': break;
10466       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
10467       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
10468                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10469       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
10470       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
10471       case '[': break;
10472       case ']': break;
10473       case '1': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10474       case '2': USE_BITS (OP_MASK_BP,           OP_SH_BP);      break;
10475       case '3': USE_BITS (OP_MASK_SA3,          OP_SH_SA3);     break;
10476       case '4': USE_BITS (OP_MASK_SA4,          OP_SH_SA4);     break;
10477       case '5': USE_BITS (OP_MASK_IMM8,         OP_SH_IMM8);    break;
10478       case '6': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10479       case '7': USE_BITS (OP_MASK_DSPACC,       OP_SH_DSPACC);  break;
10480       case '8': USE_BITS (OP_MASK_WRDSP,        OP_SH_WRDSP);   break;
10481       case '9': USE_BITS (OP_MASK_DSPACC_S,     OP_SH_DSPACC_S);break;
10482       case '0': USE_BITS (OP_MASK_DSPSFT,       OP_SH_DSPSFT);  break;
10483       case '\'': USE_BITS (OP_MASK_RDDSP,       OP_SH_RDDSP);   break;
10484       case ':': USE_BITS (OP_MASK_DSPSFT_7,     OP_SH_DSPSFT_7);break;
10485       case '@': USE_BITS (OP_MASK_IMM10,        OP_SH_IMM10);   break;
10486       case '!': USE_BITS (OP_MASK_MT_U,         OP_SH_MT_U);    break;
10487       case '$': USE_BITS (OP_MASK_MT_H,         OP_SH_MT_H);    break;
10488       case '*': USE_BITS (OP_MASK_MTACC_T,      OP_SH_MTACC_T); break;
10489       case '&': USE_BITS (OP_MASK_MTACC_D,      OP_SH_MTACC_D); break;
10490       case '\\': USE_BITS (OP_MASK_3BITPOS,     OP_SH_3BITPOS); break;
10491       case '~': USE_BITS (OP_MASK_OFFSET12,     OP_SH_OFFSET12); break;
10492       case 'g': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10493       default:
10494         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10495                 c, opc->name, opc->args);
10496         return 0;
10497       }
10498 #undef USE_BITS
10499   if (used_bits != 0xffffffff)
10500     {
10501       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10502               ~used_bits & 0xffffffff, opc->name, opc->args);
10503       return 0;
10504     }
10505   return 1;
10506 }
10507
10508 /* For consistency checking, verify that the length implied matches the
10509    major opcode and that all bits are specified either by the match/mask
10510    part of the instruction definition, or by the operand list.  */
10511
10512 static int
10513 validate_micromips_insn (const struct mips_opcode *opc)
10514 {
10515   unsigned long match = opc->match;
10516   unsigned long mask = opc->mask;
10517   const char *p = opc->args;
10518   unsigned long insn_bits;
10519   unsigned long used_bits;
10520   unsigned long major;
10521   unsigned int length;
10522   char e;
10523   char c;
10524
10525   if ((mask & match) != match)
10526     {
10527       as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10528               opc->name, opc->args);
10529       return 0;
10530     }
10531   length = micromips_insn_length (opc);
10532   if (length != 2 && length != 4)
10533     {
10534       as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10535                 "%s %s"), length, opc->name, opc->args);
10536       return 0;
10537     }
10538   major = match >> (10 + 8 * (length - 2));
10539   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10540       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10541     {
10542       as_bad (_("Internal error: bad microMIPS opcode "
10543                 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10544       return 0;
10545     }
10546
10547   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
10548   insn_bits = 1 << 4 * length;
10549   insn_bits <<= 4 * length;
10550   insn_bits -= 1;
10551   used_bits = mask;
10552 #define USE_BITS(field) \
10553   (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10554   while (*p)
10555     switch (c = *p++)
10556       {
10557       case ',': break;
10558       case '(': break;
10559       case ')': break;
10560       case '+':
10561         e = c;
10562         switch (c = *p++)
10563           {
10564           case 'A': USE_BITS (EXTLSB);  break;
10565           case 'B': USE_BITS (INSMSB);  break;
10566           case 'C': USE_BITS (EXTMSBD); break;
10567           case 'D': USE_BITS (RS);      USE_BITS (SEL); break;
10568           case 'E': USE_BITS (EXTLSB);  break;
10569           case 'F': USE_BITS (INSMSB);  break;
10570           case 'G': USE_BITS (EXTMSBD); break;
10571           case 'H': USE_BITS (EXTMSBD); break;
10572           default:
10573             as_bad (_("Internal error: bad mips opcode "
10574                       "(unknown extension operand type `%c%c'): %s %s"),
10575                     e, c, opc->name, opc->args);
10576             return 0;
10577           }
10578         break;
10579       case 'm':
10580         e = c;
10581         switch (c = *p++)
10582           {
10583           case 'A': USE_BITS (IMMA);    break;
10584           case 'B': USE_BITS (IMMB);    break;
10585           case 'C': USE_BITS (IMMC);    break;
10586           case 'D': USE_BITS (IMMD);    break;
10587           case 'E': USE_BITS (IMME);    break;
10588           case 'F': USE_BITS (IMMF);    break;
10589           case 'G': USE_BITS (IMMG);    break;
10590           case 'H': USE_BITS (IMMH);    break;
10591           case 'I': USE_BITS (IMMI);    break;
10592           case 'J': USE_BITS (IMMJ);    break;
10593           case 'L': USE_BITS (IMML);    break;
10594           case 'M': USE_BITS (IMMM);    break;
10595           case 'N': USE_BITS (IMMN);    break;
10596           case 'O': USE_BITS (IMMO);    break;
10597           case 'P': USE_BITS (IMMP);    break;
10598           case 'Q': USE_BITS (IMMQ);    break;
10599           case 'U': USE_BITS (IMMU);    break;
10600           case 'W': USE_BITS (IMMW);    break;
10601           case 'X': USE_BITS (IMMX);    break;
10602           case 'Y': USE_BITS (IMMY);    break;
10603           case 'Z': break;
10604           case 'a': break;
10605           case 'b': USE_BITS (MB);      break;
10606           case 'c': USE_BITS (MC);      break;
10607           case 'd': USE_BITS (MD);      break;
10608           case 'e': USE_BITS (ME);      break;
10609           case 'f': USE_BITS (MF);      break;
10610           case 'g': USE_BITS (MG);      break;
10611           case 'h': USE_BITS (MH);      break;
10612           case 'i': USE_BITS (MI);      break;
10613           case 'j': USE_BITS (MJ);      break;
10614           case 'l': USE_BITS (ML);      break;
10615           case 'm': USE_BITS (MM);      break;
10616           case 'n': USE_BITS (MN);      break;
10617           case 'p': USE_BITS (MP);      break;
10618           case 'q': USE_BITS (MQ);      break;
10619           case 'r': break;
10620           case 's': break;
10621           case 't': break;
10622           case 'x': break;
10623           case 'y': break;
10624           case 'z': break;
10625           default:
10626             as_bad (_("Internal error: bad mips opcode "
10627                       "(unknown extension operand type `%c%c'): %s %s"),
10628                     e, c, opc->name, opc->args);
10629             return 0;
10630           }
10631         break;
10632       case '.': USE_BITS (OFFSET10);    break;
10633       case '1': USE_BITS (STYPE);       break;
10634       case '2': USE_BITS (BP);          break;
10635       case '3': USE_BITS (SA3);         break;
10636       case '4': USE_BITS (SA4);         break;
10637       case '5': USE_BITS (IMM8);        break;
10638       case '6': USE_BITS (RS);          break;
10639       case '7': USE_BITS (DSPACC);      break;
10640       case '8': USE_BITS (WRDSP);       break;
10641       case '0': USE_BITS (DSPSFT);      break;
10642       case '<': USE_BITS (SHAMT);       break;
10643       case '>': USE_BITS (SHAMT);       break;
10644       case '@': USE_BITS (IMM10);       break;
10645       case 'B': USE_BITS (CODE10);      break;
10646       case 'C': USE_BITS (COPZ);        break;
10647       case 'D': USE_BITS (FD);          break;
10648       case 'E': USE_BITS (RT);          break;
10649       case 'G': USE_BITS (RS);          break;
10650       case 'H': USE_BITS (SEL);         break;
10651       case 'K': USE_BITS (RS);          break;
10652       case 'M': USE_BITS (CCC);         break;
10653       case 'N': USE_BITS (BCC);         break;
10654       case 'R': USE_BITS (FR);          break;
10655       case 'S': USE_BITS (FS);          break;
10656       case 'T': USE_BITS (FT);          break;
10657       case 'V': USE_BITS (FS);          break;
10658       case '\\': USE_BITS (3BITPOS);    break;
10659       case '^': USE_BITS (RD);          break;
10660       case 'a': USE_BITS (TARGET);      break;
10661       case 'b': USE_BITS (RS);          break;
10662       case 'c': USE_BITS (CODE);        break;
10663       case 'd': USE_BITS (RD);          break;
10664       case 'h': USE_BITS (PREFX);       break;
10665       case 'i': USE_BITS (IMMEDIATE);   break;
10666       case 'j': USE_BITS (DELTA);       break;
10667       case 'k': USE_BITS (CACHE);       break;
10668       case 'n': USE_BITS (RT);          break;
10669       case 'o': USE_BITS (DELTA);       break;
10670       case 'p': USE_BITS (DELTA);       break;
10671       case 'q': USE_BITS (CODE2);       break;
10672       case 'r': USE_BITS (RS);          break;
10673       case 's': USE_BITS (RS);          break;
10674       case 't': USE_BITS (RT);          break;
10675       case 'u': USE_BITS (IMMEDIATE);   break;
10676       case 'v': USE_BITS (RS);          break;
10677       case 'w': USE_BITS (RT);          break;
10678       case 'y': USE_BITS (RS3);         break;
10679       case 'z': break;
10680       case '|': USE_BITS (TRAP);        break;
10681       case '~': USE_BITS (OFFSET12);    break;
10682       default:
10683         as_bad (_("Internal error: bad microMIPS opcode "
10684                   "(unknown operand type `%c'): %s %s"),
10685                 c, opc->name, opc->args);
10686         return 0;
10687       }
10688 #undef USE_BITS
10689   if (used_bits != insn_bits)
10690     {
10691       if (~used_bits & insn_bits)
10692         as_bad (_("Internal error: bad microMIPS opcode "
10693                   "(bits 0x%lx undefined): %s %s"),
10694                 ~used_bits & insn_bits, opc->name, opc->args);
10695       if (used_bits & ~insn_bits)
10696         as_bad (_("Internal error: bad microMIPS opcode "
10697                   "(bits 0x%lx defined): %s %s"),
10698                 used_bits & ~insn_bits, opc->name, opc->args);
10699       return 0;
10700     }
10701   return 1;
10702 }
10703
10704 /* UDI immediates.  */
10705 struct mips_immed {
10706   char          type;
10707   unsigned int  shift;
10708   unsigned long mask;
10709   const char *  desc;
10710 };
10711
10712 static const struct mips_immed mips_immed[] = {
10713   { '1',        OP_SH_UDI1,     OP_MASK_UDI1,           0},
10714   { '2',        OP_SH_UDI2,     OP_MASK_UDI2,           0},
10715   { '3',        OP_SH_UDI3,     OP_MASK_UDI3,           0},
10716   { '4',        OP_SH_UDI4,     OP_MASK_UDI4,           0},
10717   { 0,0,0,0 }
10718 };
10719
10720 /* Check whether an odd floating-point register is allowed.  */
10721 static int
10722 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10723 {
10724   const char *s = insn->name;
10725
10726   if (insn->pinfo == INSN_MACRO)
10727     /* Let a macro pass, we'll catch it later when it is expanded.  */
10728     return 1;
10729
10730   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
10731     {
10732       /* Allow odd registers for single-precision ops.  */
10733       switch (insn->pinfo & (FP_S | FP_D))
10734         {
10735         case FP_S:
10736         case 0:
10737           return 1;     /* both single precision - ok */
10738         case FP_D:
10739           return 0;     /* both double precision - fail */
10740         default:
10741           break;
10742         }
10743
10744       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
10745       s = strchr (insn->name, '.');
10746       if (argnum == 2)
10747         s = s != NULL ? strchr (s + 1, '.') : NULL;
10748       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10749     } 
10750
10751   /* Single-precision coprocessor loads and moves are OK too.  */
10752   if ((insn->pinfo & FP_S)
10753       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10754                          | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10755     return 1;
10756
10757   return 0;
10758 }
10759
10760 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10761    taking bits from BIT up.  */
10762 static int
10763 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10764 {
10765   return (ep->X_op == O_constant
10766           && (ep->X_add_number & ((1 << bit) - 1)) == 0
10767           && ep->X_add_number >= min << bit
10768           && ep->X_add_number < max << bit);
10769 }
10770
10771 /* This routine assembles an instruction into its binary format.  As a
10772    side effect, it sets one of the global variables imm_reloc or
10773    offset_reloc to the type of relocation to do if one of the operands
10774    is an address expression.  */
10775
10776 static void
10777 mips_ip (char *str, struct mips_cl_insn *ip)
10778 {
10779   bfd_boolean wrong_delay_slot_insns = FALSE;
10780   bfd_boolean need_delay_slot_ok = TRUE;
10781   struct mips_opcode *firstinsn = NULL;
10782   const struct mips_opcode *past;
10783   struct hash_control *hash;
10784   char *s;
10785   const char *args;
10786   char c = 0;
10787   struct mips_opcode *insn;
10788   char *argsStart;
10789   unsigned int regno;
10790   unsigned int lastregno;
10791   unsigned int destregno = 0;
10792   unsigned int lastpos = 0;
10793   unsigned int limlo, limhi;
10794   char *s_reset;
10795   offsetT min_range, max_range;
10796   long opend;
10797   char *name;
10798   int argnum;
10799   unsigned int rtype;
10800   char *dot;
10801   long end;
10802
10803   insn_error = NULL;
10804
10805   if (mips_opts.micromips)
10806     {
10807       hash = micromips_op_hash;
10808       past = &micromips_opcodes[bfd_micromips_num_opcodes];
10809     }
10810   else
10811     {
10812       hash = op_hash;
10813       past = &mips_opcodes[NUMOPCODES];
10814     }
10815   forced_insn_length = 0;
10816   insn = NULL;
10817
10818   /* We first try to match an instruction up to a space or to the end.  */
10819   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10820     continue;
10821
10822   /* Make a copy of the instruction so that we can fiddle with it.  */
10823   name = alloca (end + 1);
10824   memcpy (name, str, end);
10825   name[end] = '\0';
10826
10827   for (;;)
10828     {
10829       insn = (struct mips_opcode *) hash_find (hash, name);
10830
10831       if (insn != NULL || !mips_opts.micromips)
10832         break;
10833       if (forced_insn_length)
10834         break;
10835
10836       /* See if there's an instruction size override suffix,
10837          either `16' or `32', at the end of the mnemonic proper,
10838          that defines the operation, i.e. before the first `.'
10839          character if any.  Strip it and retry.  */
10840       dot = strchr (name, '.');
10841       opend = dot != NULL ? dot - name : end;
10842       if (opend < 3)
10843         break;
10844       if (name[opend - 2] == '1' && name[opend - 1] == '6')
10845         forced_insn_length = 2;
10846       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10847         forced_insn_length = 4;
10848       else
10849         break;
10850       memcpy (name + opend - 2, name + opend, end - opend + 1);
10851     }
10852   if (insn == NULL)
10853     {
10854       insn_error = _("Unrecognized opcode");
10855       return;
10856     }
10857
10858   /* For microMIPS instructions placed in a fixed-length branch delay slot
10859      we make up to two passes over the relevant fragment of the opcode
10860      table.  First we try instructions that meet the delay slot's length
10861      requirement.  If none matched, then we retry with the remaining ones
10862      and if one matches, then we use it and then issue an appropriate
10863      warning later on.  */
10864   argsStart = s = str + end;
10865   for (;;)
10866     {
10867       bfd_boolean delay_slot_ok;
10868       bfd_boolean size_ok;
10869       bfd_boolean ok;
10870
10871       gas_assert (strcmp (insn->name, name) == 0);
10872
10873       ok = is_opcode_valid (insn);
10874       size_ok = is_size_valid (insn);
10875       delay_slot_ok = is_delay_slot_valid (insn);
10876       if (!delay_slot_ok && !wrong_delay_slot_insns)
10877         {
10878           firstinsn = insn;
10879           wrong_delay_slot_insns = TRUE;
10880         }
10881       if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10882         {
10883           static char buf[256];
10884
10885           if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
10886             {
10887               ++insn;
10888               continue;
10889             }
10890           if (wrong_delay_slot_insns && need_delay_slot_ok)
10891             {
10892               gas_assert (firstinsn);
10893               need_delay_slot_ok = FALSE;
10894               past = insn + 1;
10895               insn = firstinsn;
10896               continue;
10897             }
10898
10899           if (insn_error)
10900             return;
10901
10902           if (!ok)
10903             sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
10904                      mips_cpu_info_from_arch (mips_opts.arch)->name,
10905                      mips_cpu_info_from_isa (mips_opts.isa)->name);
10906           else
10907             sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10908                      8 * forced_insn_length);
10909           insn_error = buf;
10910
10911           return;
10912         }
10913
10914       create_insn (ip, insn);
10915       insn_error = NULL;
10916       argnum = 1;
10917       lastregno = 0xffffffff;
10918       for (args = insn->args;; ++args)
10919         {
10920           int is_mdmx;
10921
10922           s += strspn (s, " \t");
10923           is_mdmx = 0;
10924           switch (*args)
10925             {
10926             case '\0':          /* end of args */
10927               if (*s == '\0')
10928                 return;
10929               break;
10930
10931             case '2':
10932               /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10933                  code) or 14 (for microMIPS code).  */
10934               my_getExpression (&imm_expr, s);
10935               check_absolute_expr (ip, &imm_expr);
10936               if ((unsigned long) imm_expr.X_add_number != 1
10937                   && (unsigned long) imm_expr.X_add_number != 3)
10938                 {
10939                   as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10940                           (unsigned long) imm_expr.X_add_number);
10941                 }
10942               INSERT_OPERAND (mips_opts.micromips,
10943                               BP, *ip, imm_expr.X_add_number);
10944               imm_expr.X_op = O_absent;
10945               s = expr_end;
10946               continue;
10947
10948             case '3':
10949               /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
10950                  code) or 21 (for microMIPS code).  */
10951               {
10952                 unsigned long mask = (mips_opts.micromips
10953                                       ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
10954
10955                 my_getExpression (&imm_expr, s);
10956                 check_absolute_expr (ip, &imm_expr);
10957                 if ((unsigned long) imm_expr.X_add_number > mask)
10958                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10959                           mask, (unsigned long) imm_expr.X_add_number);
10960                 INSERT_OPERAND (mips_opts.micromips,
10961                                 SA3, *ip, imm_expr.X_add_number);
10962                 imm_expr.X_op = O_absent;
10963                 s = expr_end;
10964               }
10965               continue;
10966
10967             case '4':
10968               /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
10969                  code) or 21 (for microMIPS code).  */
10970               {
10971                 unsigned long mask = (mips_opts.micromips
10972                                       ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
10973
10974                 my_getExpression (&imm_expr, s);
10975                 check_absolute_expr (ip, &imm_expr);
10976                 if ((unsigned long) imm_expr.X_add_number > mask)
10977                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10978                           mask, (unsigned long) imm_expr.X_add_number);
10979                 INSERT_OPERAND (mips_opts.micromips,
10980                                 SA4, *ip, imm_expr.X_add_number);
10981                 imm_expr.X_op = O_absent;
10982                 s = expr_end;
10983               }
10984               continue;
10985
10986             case '5':
10987               /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
10988                  code) or 16 (for microMIPS code).  */
10989               {
10990                 unsigned long mask = (mips_opts.micromips
10991                                       ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
10992
10993                 my_getExpression (&imm_expr, s);
10994                 check_absolute_expr (ip, &imm_expr);
10995                 if ((unsigned long) imm_expr.X_add_number > mask)
10996                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10997                           mask, (unsigned long) imm_expr.X_add_number);
10998                 INSERT_OPERAND (mips_opts.micromips,
10999                                 IMM8, *ip, imm_expr.X_add_number);
11000                 imm_expr.X_op = O_absent;
11001                 s = expr_end;
11002               }
11003               continue;
11004
11005             case '6':
11006               /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
11007                  code) or 21 (for microMIPS code).  */
11008               {
11009                 unsigned long mask = (mips_opts.micromips
11010                                       ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11011
11012                 my_getExpression (&imm_expr, s);
11013                 check_absolute_expr (ip, &imm_expr);
11014                 if ((unsigned long) imm_expr.X_add_number > mask)
11015                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11016                           mask, (unsigned long) imm_expr.X_add_number);
11017                 INSERT_OPERAND (mips_opts.micromips,
11018                                 RS, *ip, imm_expr.X_add_number);
11019                 imm_expr.X_op = O_absent;
11020                 s = expr_end;
11021               }
11022               continue;
11023
11024             case '7': /* Four DSP accumulators in bits 11,12.  */
11025               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11026                   && s[3] >= '0' && s[3] <= '3')
11027                 {
11028                   regno = s[3] - '0';
11029                   s += 4;
11030                   INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
11031                   continue;
11032                 }
11033               else
11034                 as_bad (_("Invalid dsp acc register"));
11035               break;
11036
11037             case '8':
11038               /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11039                  code) or 14 (for microMIPS code).  */
11040               {
11041                 unsigned long mask = (mips_opts.micromips
11042                                       ? MICROMIPSOP_MASK_WRDSP
11043                                       : OP_MASK_WRDSP);
11044
11045                 my_getExpression (&imm_expr, s);
11046                 check_absolute_expr (ip, &imm_expr);
11047                 if ((unsigned long) imm_expr.X_add_number > mask)
11048                   as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11049                           mask, (unsigned long) imm_expr.X_add_number);
11050                 INSERT_OPERAND (mips_opts.micromips,
11051                                 WRDSP, *ip, imm_expr.X_add_number);
11052                 imm_expr.X_op = O_absent;
11053                 s = expr_end;
11054               }
11055               continue;
11056
11057             case '9': /* Four DSP accumulators in bits 21,22.  */
11058               gas_assert (!mips_opts.micromips);
11059               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11060                   && s[3] >= '0' && s[3] <= '3')
11061                 {
11062                   regno = s[3] - '0';
11063                   s += 4;
11064                   INSERT_OPERAND (0, DSPACC_S, *ip, regno);
11065                   continue;
11066                 }
11067               else
11068                 as_bad (_("Invalid dsp acc register"));
11069               break;
11070
11071             case '0':
11072               /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11073                  code) or 20 (for microMIPS code).  */
11074               {
11075                 long mask = (mips_opts.micromips
11076                              ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11077
11078                 my_getExpression (&imm_expr, s);
11079                 check_absolute_expr (ip, &imm_expr);
11080                 min_range = -((mask + 1) >> 1);
11081                 max_range = ((mask + 1) >> 1) - 1;
11082                 if (imm_expr.X_add_number < min_range
11083                     || imm_expr.X_add_number > max_range)
11084                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11085                           (long) min_range, (long) max_range,
11086                           (long) imm_expr.X_add_number);
11087                 INSERT_OPERAND (mips_opts.micromips,
11088                                 DSPSFT, *ip, imm_expr.X_add_number);
11089                 imm_expr.X_op = O_absent;
11090                 s = expr_end;
11091               }
11092               continue;
11093
11094             case '\'': /* DSP 6-bit unsigned immediate in bit 16.  */
11095               gas_assert (!mips_opts.micromips);
11096               my_getExpression (&imm_expr, s);
11097               check_absolute_expr (ip, &imm_expr);
11098               if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11099                 {
11100                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11101                           OP_MASK_RDDSP,
11102                           (unsigned long) imm_expr.X_add_number);
11103                 }
11104               INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
11105               imm_expr.X_op = O_absent;
11106               s = expr_end;
11107               continue;
11108
11109             case ':': /* DSP 7-bit signed immediate in bit 19.  */
11110               gas_assert (!mips_opts.micromips);
11111               my_getExpression (&imm_expr, s);
11112               check_absolute_expr (ip, &imm_expr);
11113               min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11114               max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11115               if (imm_expr.X_add_number < min_range ||
11116                   imm_expr.X_add_number > max_range)
11117                 {
11118                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11119                           (long) min_range, (long) max_range,
11120                           (long) imm_expr.X_add_number);
11121                 }
11122               INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
11123               imm_expr.X_op = O_absent;
11124               s = expr_end;
11125               continue;
11126
11127             case '@': /* DSP 10-bit signed immediate in bit 16.  */
11128               {
11129                 long mask = (mips_opts.micromips
11130                              ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11131
11132                 my_getExpression (&imm_expr, s);
11133                 check_absolute_expr (ip, &imm_expr);
11134                 min_range = -((mask + 1) >> 1);
11135                 max_range = ((mask + 1) >> 1) - 1;
11136                 if (imm_expr.X_add_number < min_range
11137                     || imm_expr.X_add_number > max_range)
11138                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11139                           (long) min_range, (long) max_range,
11140                           (long) imm_expr.X_add_number);
11141                 INSERT_OPERAND (mips_opts.micromips,
11142                                 IMM10, *ip, imm_expr.X_add_number);
11143                 imm_expr.X_op = O_absent;
11144                 s = expr_end;
11145               }
11146               continue;
11147
11148             case '^': /* DSP 5-bit unsigned immediate in bit 11.  */
11149               gas_assert (mips_opts.micromips);
11150               my_getExpression (&imm_expr, s);
11151               check_absolute_expr (ip, &imm_expr);
11152               if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11153                 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11154                         MICROMIPSOP_MASK_RD,
11155                         (unsigned long) imm_expr.X_add_number);
11156               INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
11157               imm_expr.X_op = O_absent;
11158               s = expr_end;
11159               continue;
11160
11161             case '!': /* MT usermode flag bit.  */
11162               gas_assert (!mips_opts.micromips);
11163               my_getExpression (&imm_expr, s);
11164               check_absolute_expr (ip, &imm_expr);
11165               if (imm_expr.X_add_number & ~OP_MASK_MT_U)
11166                 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11167                         (unsigned long) imm_expr.X_add_number);
11168               INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
11169               imm_expr.X_op = O_absent;
11170               s = expr_end;
11171               continue;
11172
11173             case '$': /* MT load high flag bit.  */
11174               gas_assert (!mips_opts.micromips);
11175               my_getExpression (&imm_expr, s);
11176               check_absolute_expr (ip, &imm_expr);
11177               if (imm_expr.X_add_number & ~OP_MASK_MT_H)
11178                 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11179                         (unsigned long) imm_expr.X_add_number);
11180               INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
11181               imm_expr.X_op = O_absent;
11182               s = expr_end;
11183               continue;
11184
11185             case '*': /* Four DSP accumulators in bits 18,19.  */
11186               gas_assert (!mips_opts.micromips);
11187               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11188                   s[3] >= '0' && s[3] <= '3')
11189                 {
11190                   regno = s[3] - '0';
11191                   s += 4;
11192                   INSERT_OPERAND (0, MTACC_T, *ip, regno);
11193                   continue;
11194                 }
11195               else
11196                 as_bad (_("Invalid dsp/smartmips acc register"));
11197               break;
11198
11199             case '&': /* Four DSP accumulators in bits 13,14.  */
11200               gas_assert (!mips_opts.micromips);
11201               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11202                   s[3] >= '0' && s[3] <= '3')
11203                 {
11204                   regno = s[3] - '0';
11205                   s += 4;
11206                   INSERT_OPERAND (0, MTACC_D, *ip, regno);
11207                   continue;
11208                 }
11209               else
11210                 as_bad (_("Invalid dsp/smartmips acc register"));
11211               break;
11212
11213             case '\\':          /* 3-bit bit position.  */
11214               {
11215                 unsigned long mask = (mips_opts.micromips
11216                                       ? MICROMIPSOP_MASK_3BITPOS
11217                                       : OP_MASK_3BITPOS);
11218
11219                 my_getExpression (&imm_expr, s);
11220                 check_absolute_expr (ip, &imm_expr);
11221                 if ((unsigned long) imm_expr.X_add_number > mask)
11222                   as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11223                            ip->insn_mo->name,
11224                            mask, (unsigned long) imm_expr.X_add_number);
11225                 INSERT_OPERAND (mips_opts.micromips,
11226                                 3BITPOS, *ip, imm_expr.X_add_number);
11227                 imm_expr.X_op = O_absent;
11228                 s = expr_end;
11229               }
11230               continue;
11231
11232             case ',':
11233               ++argnum;
11234               if (*s++ == *args)
11235                 continue;
11236               s--;
11237               switch (*++args)
11238                 {
11239                 case 'r':
11240                 case 'v':
11241                   INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11242                   continue;
11243
11244                 case 'w':
11245                   INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11246                   continue;
11247
11248                 case 'W':
11249                   gas_assert (!mips_opts.micromips);
11250                   INSERT_OPERAND (0, FT, *ip, lastregno);
11251                   continue;
11252
11253                 case 'V':
11254                   INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11255                   continue;
11256                 }
11257               break;
11258
11259             case '(':
11260               /* Handle optional base register.
11261                  Either the base register is omitted or
11262                  we must have a left paren.  */
11263               /* This is dependent on the next operand specifier
11264                  is a base register specification.  */
11265               gas_assert (args[1] == 'b'
11266                           || (mips_opts.micromips
11267                               && args[1] == 'm'
11268                               && (args[2] == 'l' || args[2] == 'n'
11269                                   || args[2] == 's' || args[2] == 'a')));
11270               if (*s == '\0' && args[1] == 'b')
11271                 return;
11272               /* Fall through.  */
11273
11274             case ')':           /* These must match exactly.  */
11275               if (*s++ == *args)
11276                 continue;
11277               break;
11278
11279             case '[':           /* These must match exactly.  */
11280             case ']':
11281               gas_assert (!mips_opts.micromips);
11282               if (*s++ == *args)
11283                 continue;
11284               break;
11285
11286             case '+':           /* Opcode extension character.  */
11287               switch (*++args)
11288                 {
11289                 case '1':       /* UDI immediates.  */
11290                 case '2':
11291                 case '3':
11292                 case '4':
11293                   gas_assert (!mips_opts.micromips);
11294                   {
11295                     const struct mips_immed *imm = mips_immed;
11296
11297                     while (imm->type && imm->type != *args)
11298                       ++imm;
11299                     if (! imm->type)
11300                       abort ();
11301                     my_getExpression (&imm_expr, s);
11302                     check_absolute_expr (ip, &imm_expr);
11303                     if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11304                       {
11305                         as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11306                                  imm->desc ? imm->desc : ip->insn_mo->name,
11307                                  (unsigned long) imm_expr.X_add_number,
11308                                  (unsigned long) imm_expr.X_add_number);
11309                         imm_expr.X_add_number &= imm->mask;
11310                       }
11311                     ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11312                                         << imm->shift);
11313                     imm_expr.X_op = O_absent;
11314                     s = expr_end;
11315                   }
11316                   continue;
11317
11318                 case 'A':               /* ins/ext position, becomes LSB.  */
11319                   limlo = 0;
11320                   limhi = 31;
11321                   goto do_lsb;
11322                 case 'E':
11323                   limlo = 32;
11324                   limhi = 63;
11325                   goto do_lsb;
11326                 do_lsb:
11327                   my_getExpression (&imm_expr, s);
11328                   check_absolute_expr (ip, &imm_expr);
11329                   if ((unsigned long) imm_expr.X_add_number < limlo
11330                       || (unsigned long) imm_expr.X_add_number > limhi)
11331                     {
11332                       as_bad (_("Improper position (%lu)"),
11333                               (unsigned long) imm_expr.X_add_number);
11334                       imm_expr.X_add_number = limlo;
11335                     }
11336                   lastpos = imm_expr.X_add_number;
11337                   INSERT_OPERAND (mips_opts.micromips,
11338                                   EXTLSB, *ip, imm_expr.X_add_number);
11339                   imm_expr.X_op = O_absent;
11340                   s = expr_end;
11341                   continue;
11342
11343                 case 'B':               /* ins size, becomes MSB.  */
11344                   limlo = 1;
11345                   limhi = 32;
11346                   goto do_msb;
11347                 case 'F':
11348                   limlo = 33;
11349                   limhi = 64;
11350                   goto do_msb;
11351                 do_msb:
11352                   my_getExpression (&imm_expr, s);
11353                   check_absolute_expr (ip, &imm_expr);
11354                   /* Check for negative input so that small negative numbers
11355                      will not succeed incorrectly.  The checks against
11356                      (pos+size) transitively check "size" itself,
11357                      assuming that "pos" is reasonable.  */
11358                   if ((long) imm_expr.X_add_number < 0
11359                       || ((unsigned long) imm_expr.X_add_number
11360                           + lastpos) < limlo
11361                       || ((unsigned long) imm_expr.X_add_number
11362                           + lastpos) > limhi)
11363                     {
11364                       as_bad (_("Improper insert size (%lu, position %lu)"),
11365                               (unsigned long) imm_expr.X_add_number,
11366                               (unsigned long) lastpos);
11367                       imm_expr.X_add_number = limlo - lastpos;
11368                     }
11369                   INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11370                                   lastpos + imm_expr.X_add_number - 1);
11371                   imm_expr.X_op = O_absent;
11372                   s = expr_end;
11373                   continue;
11374
11375                 case 'C':               /* ext size, becomes MSBD.  */
11376                   limlo = 1;
11377                   limhi = 32;
11378                   goto do_msbd;
11379                 case 'G':
11380                   limlo = 33;
11381                   limhi = 64;
11382                   goto do_msbd;
11383                 case 'H':
11384                   limlo = 33;
11385                   limhi = 64;
11386                   goto do_msbd;
11387                 do_msbd:
11388                   my_getExpression (&imm_expr, s);
11389                   check_absolute_expr (ip, &imm_expr);
11390                   /* Check for negative input so that small negative numbers
11391                      will not succeed incorrectly.  The checks against
11392                      (pos+size) transitively check "size" itself,
11393                      assuming that "pos" is reasonable.  */
11394                   if ((long) imm_expr.X_add_number < 0
11395                       || ((unsigned long) imm_expr.X_add_number
11396                           + lastpos) < limlo
11397                       || ((unsigned long) imm_expr.X_add_number
11398                           + lastpos) > limhi)
11399                     {
11400                       as_bad (_("Improper extract size (%lu, position %lu)"),
11401                               (unsigned long) imm_expr.X_add_number,
11402                               (unsigned long) lastpos);
11403                       imm_expr.X_add_number = limlo - lastpos;
11404                     }
11405                   INSERT_OPERAND (mips_opts.micromips,
11406                                   EXTMSBD, *ip, imm_expr.X_add_number - 1);
11407                   imm_expr.X_op = O_absent;
11408                   s = expr_end;
11409                   continue;
11410
11411                 case 'D':
11412                   /* +D is for disassembly only; never match.  */
11413                   break;
11414
11415                 case 'I':
11416                   /* "+I" is like "I", except that imm2_expr is used.  */
11417                   my_getExpression (&imm2_expr, s);
11418                   if (imm2_expr.X_op != O_big
11419                       && imm2_expr.X_op != O_constant)
11420                   insn_error = _("absolute expression required");
11421                   if (HAVE_32BIT_GPRS)
11422                     normalize_constant_expr (&imm2_expr);
11423                   s = expr_end;
11424                   continue;
11425
11426                 case 'T': /* Coprocessor register.  */
11427                   gas_assert (!mips_opts.micromips);
11428                   /* +T is for disassembly only; never match.  */
11429                   break;
11430
11431                 case 't': /* Coprocessor register number.  */
11432                   gas_assert (!mips_opts.micromips);
11433                   if (s[0] == '$' && ISDIGIT (s[1]))
11434                     {
11435                       ++s;
11436                       regno = 0;
11437                       do
11438                         {
11439                           regno *= 10;
11440                           regno += *s - '0';
11441                           ++s;
11442                         }
11443                       while (ISDIGIT (*s));
11444                       if (regno > 31)
11445                         as_bad (_("Invalid register number (%d)"), regno);
11446                       else
11447                         {
11448                           INSERT_OPERAND (0, RT, *ip, regno);
11449                           continue;
11450                         }
11451                     }
11452                   else
11453                     as_bad (_("Invalid coprocessor 0 register number"));
11454                   break;
11455
11456                 case 'x':
11457                   /* bbit[01] and bbit[01]32 bit index.  Give error if index
11458                      is not in the valid range.  */
11459                   gas_assert (!mips_opts.micromips);
11460                   my_getExpression (&imm_expr, s);
11461                   check_absolute_expr (ip, &imm_expr);
11462                   if ((unsigned) imm_expr.X_add_number > 31)
11463                     {
11464                       as_bad (_("Improper bit index (%lu)"),
11465                               (unsigned long) imm_expr.X_add_number);
11466                       imm_expr.X_add_number = 0;
11467                     }
11468                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11469                   imm_expr.X_op = O_absent;
11470                   s = expr_end;
11471                   continue;
11472
11473                 case 'X':
11474                   /* bbit[01] bit index when bbit is used but we generate
11475                      bbit[01]32 because the index is over 32.  Move to the
11476                      next candidate if index is not in the valid range.  */
11477                   gas_assert (!mips_opts.micromips);
11478                   my_getExpression (&imm_expr, s);
11479                   check_absolute_expr (ip, &imm_expr);
11480                   if ((unsigned) imm_expr.X_add_number < 32
11481                       || (unsigned) imm_expr.X_add_number > 63)
11482                     break;
11483                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11484                   imm_expr.X_op = O_absent;
11485                   s = expr_end;
11486                   continue;
11487
11488                 case 'p':
11489                   /* cins, cins32, exts and exts32 position field.  Give error
11490                      if it's not in the valid range.  */
11491                   gas_assert (!mips_opts.micromips);
11492                   my_getExpression (&imm_expr, s);
11493                   check_absolute_expr (ip, &imm_expr);
11494                   if ((unsigned) imm_expr.X_add_number > 31)
11495                     {
11496                       as_bad (_("Improper position (%lu)"),
11497                               (unsigned long) imm_expr.X_add_number);
11498                       imm_expr.X_add_number = 0;
11499                     }
11500                   /* Make the pos explicit to simplify +S.  */
11501                   lastpos = imm_expr.X_add_number + 32;
11502                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11503                   imm_expr.X_op = O_absent;
11504                   s = expr_end;
11505                   continue;
11506
11507                 case 'P':
11508                   /* cins, cins32, exts and exts32 position field.  Move to
11509                      the next candidate if it's not in the valid range.  */
11510                   gas_assert (!mips_opts.micromips);
11511                   my_getExpression (&imm_expr, s);
11512                   check_absolute_expr (ip, &imm_expr);
11513                   if ((unsigned) imm_expr.X_add_number < 32
11514                       || (unsigned) imm_expr.X_add_number > 63)
11515                     break;
11516                   lastpos = imm_expr.X_add_number;
11517                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11518                   imm_expr.X_op = O_absent;
11519                   s = expr_end;
11520                   continue;
11521
11522                 case 's':
11523                   /* cins and exts length-minus-one field.  */
11524                   gas_assert (!mips_opts.micromips);
11525                   my_getExpression (&imm_expr, s);
11526                   check_absolute_expr (ip, &imm_expr);
11527                   if ((unsigned long) imm_expr.X_add_number > 31)
11528                     {
11529                       as_bad (_("Improper size (%lu)"),
11530                               (unsigned long) imm_expr.X_add_number);
11531                       imm_expr.X_add_number = 0;
11532                     }
11533                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11534                   imm_expr.X_op = O_absent;
11535                   s = expr_end;
11536                   continue;
11537
11538                 case 'S':
11539                   /* cins32/exts32 and cins/exts aliasing cint32/exts32
11540                      length-minus-one field.  */
11541                   gas_assert (!mips_opts.micromips);
11542                   my_getExpression (&imm_expr, s);
11543                   check_absolute_expr (ip, &imm_expr);
11544                   if ((long) imm_expr.X_add_number < 0
11545                       || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11546                     {
11547                       as_bad (_("Improper size (%lu)"),
11548                               (unsigned long) imm_expr.X_add_number);
11549                       imm_expr.X_add_number = 0;
11550                     }
11551                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11552                   imm_expr.X_op = O_absent;
11553                   s = expr_end;
11554                   continue;
11555
11556                 case 'Q':
11557                   /* seqi/snei immediate field.  */
11558                   gas_assert (!mips_opts.micromips);
11559                   my_getExpression (&imm_expr, s);
11560                   check_absolute_expr (ip, &imm_expr);
11561                   if ((long) imm_expr.X_add_number < -512
11562                       || (long) imm_expr.X_add_number >= 512)
11563                     {
11564                       as_bad (_("Improper immediate (%ld)"),
11565                                (long) imm_expr.X_add_number);
11566                       imm_expr.X_add_number = 0;
11567                     }
11568                   INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11569                   imm_expr.X_op = O_absent;
11570                   s = expr_end;
11571                   continue;
11572
11573                 case 'a': /* 8-bit signed offset in bit 6 */
11574                   gas_assert (!mips_opts.micromips);
11575                   my_getExpression (&imm_expr, s);
11576                   check_absolute_expr (ip, &imm_expr);
11577                   min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11578                   max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11579                   if (imm_expr.X_add_number < min_range
11580                       || imm_expr.X_add_number > max_range)
11581                     {
11582                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11583                               (long) min_range, (long) max_range,
11584                               (long) imm_expr.X_add_number);
11585                     }
11586                   INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
11587                   imm_expr.X_op = O_absent;
11588                   s = expr_end;
11589                   continue;
11590
11591                 case 'b': /* 8-bit signed offset in bit 3 */
11592                   gas_assert (!mips_opts.micromips);
11593                   my_getExpression (&imm_expr, s);
11594                   check_absolute_expr (ip, &imm_expr);
11595                   min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11596                   max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11597                   if (imm_expr.X_add_number < min_range
11598                       || imm_expr.X_add_number > max_range)
11599                     {
11600                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11601                               (long) min_range, (long) max_range,
11602                               (long) imm_expr.X_add_number);
11603                     }
11604                   INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
11605                   imm_expr.X_op = O_absent;
11606                   s = expr_end;
11607                   continue;
11608
11609                 case 'c': /* 9-bit signed offset in bit 6 */
11610                   gas_assert (!mips_opts.micromips);
11611                   my_getExpression (&imm_expr, s);
11612                   check_absolute_expr (ip, &imm_expr);
11613                   min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11614                   max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
11615                   /* We check the offset range before adjusted.  */
11616                   min_range <<= 4;
11617                   max_range <<= 4;
11618                   if (imm_expr.X_add_number < min_range
11619                       || imm_expr.X_add_number > max_range)
11620                     {
11621                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11622                               (long) min_range, (long) max_range,
11623                               (long) imm_expr.X_add_number);
11624                     }
11625                   if (imm_expr.X_add_number & 0xf)
11626                     {
11627                       as_bad (_("Offset not 16 bytes alignment (%ld)"),
11628                               (long) imm_expr.X_add_number);
11629                     }
11630                   /* Right shift 4 bits to adjust the offset operand.  */
11631                   INSERT_OPERAND (0, OFFSET_C, *ip,
11632                                   imm_expr.X_add_number >> 4);
11633                   imm_expr.X_op = O_absent;
11634                   s = expr_end;
11635                   continue;
11636
11637                 case 'z':
11638                   gas_assert (!mips_opts.micromips);
11639                   if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11640                     break;
11641                   if (regno == AT && mips_opts.at)
11642                     {
11643                       if (mips_opts.at == ATREG)
11644                         as_warn (_("used $at without \".set noat\""));
11645                       else
11646                         as_warn (_("used $%u with \".set at=$%u\""),
11647                                  regno, mips_opts.at);
11648                     }
11649                   INSERT_OPERAND (0, RZ, *ip, regno);
11650                   continue;
11651
11652                 case 'Z':
11653                   gas_assert (!mips_opts.micromips);
11654                   if (!reg_lookup (&s, RTYPE_FPU, &regno))
11655                     break;
11656                   INSERT_OPERAND (0, FZ, *ip, regno);
11657                   continue;
11658
11659                 default:
11660                   as_bad (_("Internal error: bad %s opcode "
11661                             "(unknown extension operand type `+%c'): %s %s"),
11662                           mips_opts.micromips ? "microMIPS" : "MIPS",
11663                           *args, insn->name, insn->args);
11664                   /* Further processing is fruitless.  */
11665                   return;
11666                 }
11667               break;
11668
11669             case '.':           /* 10-bit offset.  */
11670               gas_assert (mips_opts.micromips);
11671             case '~':           /* 12-bit offset.  */
11672               {
11673                 int shift = *args == '.' ? 9 : 11;
11674                 size_t i;
11675
11676                 /* Check whether there is only a single bracketed expression
11677                    left.  If so, it must be the base register and the
11678                    constant must be zero.  */
11679                 if (*s == '(' && strchr (s + 1, '(') == 0)
11680                   continue;
11681
11682                 /* If this value won't fit into the offset, then go find
11683                    a macro that will generate a 16- or 32-bit offset code
11684                    pattern.  */
11685                 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11686                 if ((i == 0 && (imm_expr.X_op != O_constant
11687                                 || imm_expr.X_add_number >= 1 << shift
11688                                 || imm_expr.X_add_number < -1 << shift))
11689                     || i > 0)
11690                   {
11691                     imm_expr.X_op = O_absent;
11692                     break;
11693                   }
11694                 if (shift == 9)
11695                   INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11696                 else
11697                   INSERT_OPERAND (mips_opts.micromips,
11698                                   OFFSET12, *ip, imm_expr.X_add_number);
11699                 imm_expr.X_op = O_absent;
11700                 s = expr_end;
11701               }
11702               continue;
11703
11704             case '<':           /* must be at least one digit */
11705               /*
11706                * According to the manual, if the shift amount is greater
11707                * than 31 or less than 0, then the shift amount should be
11708                * mod 32.  In reality the mips assembler issues an error.
11709                * We issue a warning and mask out all but the low 5 bits.
11710                */
11711               my_getExpression (&imm_expr, s);
11712               check_absolute_expr (ip, &imm_expr);
11713               if ((unsigned long) imm_expr.X_add_number > 31)
11714                 as_warn (_("Improper shift amount (%lu)"),
11715                          (unsigned long) imm_expr.X_add_number);
11716               INSERT_OPERAND (mips_opts.micromips,
11717                               SHAMT, *ip, imm_expr.X_add_number);
11718               imm_expr.X_op = O_absent;
11719               s = expr_end;
11720               continue;
11721
11722             case '>':           /* shift amount minus 32 */
11723               my_getExpression (&imm_expr, s);
11724               check_absolute_expr (ip, &imm_expr);
11725               if ((unsigned long) imm_expr.X_add_number < 32
11726                   || (unsigned long) imm_expr.X_add_number > 63)
11727                 break;
11728               INSERT_OPERAND (mips_opts.micromips,
11729                               SHAMT, *ip, imm_expr.X_add_number - 32);
11730               imm_expr.X_op = O_absent;
11731               s = expr_end;
11732               continue;
11733
11734             case 'k':           /* CACHE code.  */
11735             case 'h':           /* PREFX code.  */
11736             case '1':           /* SYNC type.  */
11737               my_getExpression (&imm_expr, s);
11738               check_absolute_expr (ip, &imm_expr);
11739               if ((unsigned long) imm_expr.X_add_number > 31)
11740                 as_warn (_("Invalid value for `%s' (%lu)"),
11741                          ip->insn_mo->name,
11742                          (unsigned long) imm_expr.X_add_number);
11743               switch (*args)
11744                 {
11745                 case 'k':
11746                   if (mips_fix_cn63xxp1
11747                       && !mips_opts.micromips
11748                       && strcmp ("pref", insn->name) == 0)
11749                     switch (imm_expr.X_add_number)
11750                       {
11751                       case 5:
11752                       case 25:
11753                       case 26:
11754                       case 27:
11755                       case 28:
11756                       case 29:
11757                       case 30:
11758                       case 31:  /* These are ok.  */
11759                         break;
11760
11761                       default:  /* The rest must be changed to 28.  */
11762                         imm_expr.X_add_number = 28;
11763                         break;
11764                       }
11765                   INSERT_OPERAND (mips_opts.micromips,
11766                                   CACHE, *ip, imm_expr.X_add_number);
11767                   break;
11768                 case 'h':
11769                   INSERT_OPERAND (mips_opts.micromips,
11770                                   PREFX, *ip, imm_expr.X_add_number);
11771                   break;
11772                 case '1':
11773                   INSERT_OPERAND (mips_opts.micromips,
11774                                   STYPE, *ip, imm_expr.X_add_number);
11775                   break;
11776                 }
11777               imm_expr.X_op = O_absent;
11778               s = expr_end;
11779               continue;
11780
11781             case 'c':           /* BREAK code.  */
11782               {
11783                 unsigned long mask = (mips_opts.micromips
11784                                       ? MICROMIPSOP_MASK_CODE
11785                                       : OP_MASK_CODE);
11786
11787                 my_getExpression (&imm_expr, s);
11788                 check_absolute_expr (ip, &imm_expr);
11789                 if ((unsigned long) imm_expr.X_add_number > mask)
11790                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11791                            ip->insn_mo->name,
11792                            mask, (unsigned long) imm_expr.X_add_number);
11793                 INSERT_OPERAND (mips_opts.micromips,
11794                                 CODE, *ip, imm_expr.X_add_number);
11795                 imm_expr.X_op = O_absent;
11796                 s = expr_end;
11797               }
11798               continue;
11799
11800             case 'q':           /* Lower BREAK code.  */
11801               {
11802                 unsigned long mask = (mips_opts.micromips
11803                                       ? MICROMIPSOP_MASK_CODE2
11804                                       : OP_MASK_CODE2);
11805
11806                 my_getExpression (&imm_expr, s);
11807                 check_absolute_expr (ip, &imm_expr);
11808                 if ((unsigned long) imm_expr.X_add_number > mask)
11809                   as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11810                            ip->insn_mo->name,
11811                            mask, (unsigned long) imm_expr.X_add_number);
11812                 INSERT_OPERAND (mips_opts.micromips,
11813                                 CODE2, *ip, imm_expr.X_add_number);
11814                 imm_expr.X_op = O_absent;
11815                 s = expr_end;
11816               }
11817               continue;
11818
11819             case 'B':           /* 20- or 10-bit syscall/break/wait code.  */
11820               {
11821                 unsigned long mask = (mips_opts.micromips
11822                                       ? MICROMIPSOP_MASK_CODE10
11823                                       : OP_MASK_CODE20);
11824
11825                 my_getExpression (&imm_expr, s);
11826                 check_absolute_expr (ip, &imm_expr);
11827                 if ((unsigned long) imm_expr.X_add_number > mask)
11828                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11829                            ip->insn_mo->name,
11830                            mask, (unsigned long) imm_expr.X_add_number);
11831                 if (mips_opts.micromips)
11832                   INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11833                 else
11834                   INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11835                 imm_expr.X_op = O_absent;
11836                 s = expr_end;
11837               }
11838               continue;
11839
11840             case 'C':           /* 25- or 23-bit coprocessor code.  */
11841               {
11842                 unsigned long mask = (mips_opts.micromips
11843                                       ? MICROMIPSOP_MASK_COPZ
11844                                       : OP_MASK_COPZ);
11845
11846                 my_getExpression (&imm_expr, s);
11847                 check_absolute_expr (ip, &imm_expr);
11848                 if ((unsigned long) imm_expr.X_add_number > mask)
11849                   as_warn (_("Coproccesor code > %u bits (%lu)"),
11850                            mips_opts.micromips ? 23U : 25U,
11851                            (unsigned long) imm_expr.X_add_number);
11852                 INSERT_OPERAND (mips_opts.micromips,
11853                                 COPZ, *ip, imm_expr.X_add_number);
11854                 imm_expr.X_op = O_absent;
11855                 s = expr_end;
11856               }
11857               continue;
11858
11859             case 'J':           /* 19-bit WAIT code.  */
11860               gas_assert (!mips_opts.micromips);
11861               my_getExpression (&imm_expr, s);
11862               check_absolute_expr (ip, &imm_expr);
11863               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
11864                 {
11865                   as_warn (_("Illegal 19-bit code (%lu)"),
11866                            (unsigned long) imm_expr.X_add_number);
11867                   imm_expr.X_add_number &= OP_MASK_CODE19;
11868                 }
11869               INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
11870               imm_expr.X_op = O_absent;
11871               s = expr_end;
11872               continue;
11873
11874             case 'P':           /* Performance register.  */
11875               gas_assert (!mips_opts.micromips);
11876               my_getExpression (&imm_expr, s);
11877               check_absolute_expr (ip, &imm_expr);
11878               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
11879                 as_warn (_("Invalid performance register (%lu)"),
11880                          (unsigned long) imm_expr.X_add_number);
11881               if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
11882                 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
11883                 as_warn (_("Invalid performance register (%lu)"),
11884                   (unsigned long) imm_expr.X_add_number);
11885               INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
11886               imm_expr.X_op = O_absent;
11887               s = expr_end;
11888               continue;
11889
11890             case 'G':           /* Coprocessor destination register.  */
11891               {
11892                 unsigned long opcode = ip->insn_opcode;
11893                 unsigned long mask;
11894                 unsigned int types;
11895                 int cop0;
11896
11897                 if (mips_opts.micromips)
11898                   {
11899                     mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11900                              | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11901                              | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11902                     opcode &= mask;
11903                     switch (opcode)
11904                       {
11905                       case 0x000000fc:                          /* mfc0  */
11906                       case 0x000002fc:                          /* mtc0  */
11907                       case 0x580000fc:                          /* dmfc0 */
11908                       case 0x580002fc:                          /* dmtc0 */
11909                         cop0 = 1;
11910                         break;
11911                       default:
11912                         cop0 = 0;
11913                         break;
11914                       }
11915                   }
11916                 else
11917                   {
11918                     opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11919                     cop0 = opcode == OP_OP_COP0;
11920                   }
11921                 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11922                 ok = reg_lookup (&s, types, &regno);
11923                 if (mips_opts.micromips)
11924                   INSERT_OPERAND (1, RS, *ip, regno);
11925                 else
11926                   INSERT_OPERAND (0, RD, *ip, regno);
11927                 if (ok)
11928                   {
11929                     lastregno = regno;
11930                     continue;
11931                   }
11932               }
11933               break;
11934
11935             case 'y':           /* ALNV.PS source register.  */
11936               gas_assert (mips_opts.micromips);
11937               goto do_reg;
11938             case 'x':           /* Ignore register name.  */
11939             case 'U':           /* Destination register (CLO/CLZ).  */
11940             case 'g':           /* Coprocessor destination register.  */
11941               gas_assert (!mips_opts.micromips);
11942             case 'b':           /* Base register.  */
11943             case 'd':           /* Destination register.  */
11944             case 's':           /* Source register.  */
11945             case 't':           /* Target register.  */
11946             case 'r':           /* Both target and source.  */
11947             case 'v':           /* Both dest and source.  */
11948             case 'w':           /* Both dest and target.  */
11949             case 'E':           /* Coprocessor target register.  */
11950             case 'K':           /* RDHWR destination register.  */
11951             case 'z':           /* Must be zero register.  */
11952             do_reg:
11953               s_reset = s;
11954               if (*args == 'E' || *args == 'K')
11955                 ok = reg_lookup (&s, RTYPE_NUM, &regno);
11956               else
11957                 {
11958                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
11959                   if (regno == AT && mips_opts.at)
11960                     {
11961                       if (mips_opts.at == ATREG)
11962                         as_warn (_("Used $at without \".set noat\""));
11963                       else
11964                         as_warn (_("Used $%u with \".set at=$%u\""),
11965                                  regno, mips_opts.at);
11966                     }
11967                 }
11968               if (ok)
11969                 {
11970                   c = *args;
11971                   if (*s == ' ')
11972                     ++s;
11973                   if (args[1] != *s)
11974                     {
11975                       if (c == 'r' || c == 'v' || c == 'w')
11976                         {
11977                           regno = lastregno;
11978                           s = s_reset;
11979                           ++args;
11980                         }
11981                     }
11982                   /* 'z' only matches $0.  */
11983                   if (c == 'z' && regno != 0)
11984                     break;
11985
11986                   if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
11987                     {
11988                       if (regno == lastregno)
11989                         {
11990                           insn_error
11991                             = _("Source and destination must be different");
11992                           continue;
11993                         }
11994                       if (regno == 31 && lastregno == 0xffffffff)
11995                         {
11996                           insn_error
11997                             = _("A destination register must be supplied");
11998                           continue;
11999                         }
12000                     }
12001                   /* Now that we have assembled one operand, we use the args
12002                      string to figure out where it goes in the instruction.  */
12003                   switch (c)
12004                     {
12005                     case 'r':
12006                     case 's':
12007                     case 'v':
12008                     case 'b':
12009                       INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
12010                       break;
12011
12012                     case 'K':
12013                       if (mips_opts.micromips)
12014                         INSERT_OPERAND (1, RS, *ip, regno);
12015                       else
12016                         INSERT_OPERAND (0, RD, *ip, regno);
12017                       break;
12018
12019                     case 'd':
12020                     case 'g':
12021                       INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
12022                       break;
12023
12024                     case 'U':
12025                       gas_assert (!mips_opts.micromips);
12026                       INSERT_OPERAND (0, RD, *ip, regno);
12027                       INSERT_OPERAND (0, RT, *ip, regno);
12028                       break;
12029
12030                     case 'w':
12031                     case 't':
12032                     case 'E':
12033                       INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12034                       break;
12035
12036                     case 'y':
12037                       gas_assert (mips_opts.micromips);
12038                       INSERT_OPERAND (1, RS3, *ip, regno);
12039                       break;
12040
12041                     case 'x':
12042                       /* This case exists because on the r3000 trunc
12043                          expands into a macro which requires a gp
12044                          register.  On the r6000 or r4000 it is
12045                          assembled into a single instruction which
12046                          ignores the register.  Thus the insn version
12047                          is MIPS_ISA2 and uses 'x', and the macro
12048                          version is MIPS_ISA1 and uses 't'.  */
12049                       break;
12050
12051                     case 'z':
12052                       /* This case is for the div instruction, which
12053                          acts differently if the destination argument
12054                          is $0.  This only matches $0, and is checked
12055                          outside the switch.  */
12056                       break;
12057                     }
12058                   lastregno = regno;
12059                   continue;
12060                 }
12061               switch (*args++)
12062                 {
12063                 case 'r':
12064                 case 'v':
12065                   INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
12066                   continue;
12067
12068                 case 'w':
12069                   INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
12070                   continue;
12071                 }
12072               break;
12073
12074             case 'O':           /* MDMX alignment immediate constant.  */
12075               gas_assert (!mips_opts.micromips);
12076               my_getExpression (&imm_expr, s);
12077               check_absolute_expr (ip, &imm_expr);
12078               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12079                 as_warn (_("Improper align amount (%ld), using low bits"),
12080                          (long) imm_expr.X_add_number);
12081               INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12082               imm_expr.X_op = O_absent;
12083               s = expr_end;
12084               continue;
12085
12086             case 'Q':           /* MDMX vector, element sel, or const.  */
12087               if (s[0] != '$')
12088                 {
12089                   /* MDMX Immediate.  */
12090                   gas_assert (!mips_opts.micromips);
12091                   my_getExpression (&imm_expr, s);
12092                   check_absolute_expr (ip, &imm_expr);
12093                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12094                     as_warn (_("Invalid MDMX Immediate (%ld)"),
12095                              (long) imm_expr.X_add_number);
12096                   INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12097                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12098                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12099                   else
12100                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12101                   imm_expr.X_op = O_absent;
12102                   s = expr_end;
12103                   continue;
12104                 }
12105               /* Not MDMX Immediate.  Fall through.  */
12106             case 'X':           /* MDMX destination register.  */
12107             case 'Y':           /* MDMX source register.  */
12108             case 'Z':           /* MDMX target register.  */
12109               is_mdmx = 1;
12110             case 'W':
12111               gas_assert (!mips_opts.micromips);
12112             case 'D':           /* Floating point destination register.  */
12113             case 'S':           /* Floating point source register.  */
12114             case 'T':           /* Floating point target register.  */
12115             case 'R':           /* Floating point source register.  */
12116             case 'V':
12117               rtype = RTYPE_FPU;
12118               if (is_mdmx
12119                   || (mips_opts.ase_mdmx
12120                       && (ip->insn_mo->pinfo & FP_D)
12121                       && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12122                                                 | INSN_COPROC_MEMORY_DELAY
12123                                                 | INSN_LOAD_COPROC_DELAY
12124                                                 | INSN_LOAD_MEMORY_DELAY
12125                                                 | INSN_STORE_MEMORY))))
12126                 rtype |= RTYPE_VEC;
12127               s_reset = s;
12128               if (reg_lookup (&s, rtype, &regno))
12129                 {
12130                   if ((regno & 1) != 0
12131                       && HAVE_32BIT_FPRS
12132                       && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12133                     as_warn (_("Float register should be even, was %d"),
12134                              regno);
12135
12136                   c = *args;
12137                   if (*s == ' ')
12138                     ++s;
12139                   if (args[1] != *s)
12140                     {
12141                       if (c == 'V' || c == 'W')
12142                         {
12143                           regno = lastregno;
12144                           s = s_reset;
12145                           ++args;
12146                         }
12147                     }
12148                   switch (c)
12149                     {
12150                     case 'D':
12151                     case 'X':
12152                       INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12153                       break;
12154
12155                     case 'V':
12156                     case 'S':
12157                     case 'Y':
12158                       INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12159                       break;
12160
12161                     case 'Q':
12162                       /* This is like 'Z', but also needs to fix the MDMX
12163                          vector/scalar select bits.  Note that the
12164                          scalar immediate case is handled above.  */
12165                       if (*s == '[')
12166                         {
12167                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12168                           int max_el = (is_qh ? 3 : 7);
12169                           s++;
12170                           my_getExpression(&imm_expr, s);
12171                           check_absolute_expr (ip, &imm_expr);
12172                           s = expr_end;
12173                           if (imm_expr.X_add_number > max_el)
12174                             as_bad (_("Bad element selector %ld"),
12175                                     (long) imm_expr.X_add_number);
12176                           imm_expr.X_add_number &= max_el;
12177                           ip->insn_opcode |= (imm_expr.X_add_number
12178                                               << (OP_SH_VSEL +
12179                                                   (is_qh ? 2 : 1)));
12180                           imm_expr.X_op = O_absent;
12181                           if (*s != ']')
12182                             as_warn (_("Expecting ']' found '%s'"), s);
12183                           else
12184                             s++;
12185                         }
12186                       else
12187                         {
12188                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12189                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12190                                                 << OP_SH_VSEL);
12191                           else
12192                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12193                                                 OP_SH_VSEL);
12194                         }
12195                       /* Fall through.  */
12196                     case 'W':
12197                     case 'T':
12198                     case 'Z':
12199                       INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12200                       break;
12201
12202                     case 'R':
12203                       INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12204                       break;
12205                     }
12206                   lastregno = regno;
12207                   continue;
12208                 }
12209
12210               switch (*args++)
12211                 {
12212                 case 'V':
12213                   INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12214                   continue;
12215
12216                 case 'W':
12217                   INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12218                   continue;
12219                 }
12220               break;
12221
12222             case 'I':
12223               my_getExpression (&imm_expr, s);
12224               if (imm_expr.X_op != O_big
12225                   && imm_expr.X_op != O_constant)
12226                 insn_error = _("absolute expression required");
12227               if (HAVE_32BIT_GPRS)
12228                 normalize_constant_expr (&imm_expr);
12229               s = expr_end;
12230               continue;
12231
12232             case 'A':
12233               my_getExpression (&offset_expr, s);
12234               normalize_address_expr (&offset_expr);
12235               *imm_reloc = BFD_RELOC_32;
12236               s = expr_end;
12237               continue;
12238
12239             case 'F':
12240             case 'L':
12241             case 'f':
12242             case 'l':
12243               {
12244                 int f64;
12245                 int using_gprs;
12246                 char *save_in;
12247                 char *err;
12248                 unsigned char temp[8];
12249                 int len;
12250                 unsigned int length;
12251                 segT seg;
12252                 subsegT subseg;
12253                 char *p;
12254
12255                 /* These only appear as the last operand in an
12256                    instruction, and every instruction that accepts
12257                    them in any variant accepts them in all variants.
12258                    This means we don't have to worry about backing out
12259                    any changes if the instruction does not match.
12260
12261                    The difference between them is the size of the
12262                    floating point constant and where it goes.  For 'F'
12263                    and 'L' the constant is 64 bits; for 'f' and 'l' it
12264                    is 32 bits.  Where the constant is placed is based
12265                    on how the MIPS assembler does things:
12266                     F -- .rdata
12267                     L -- .lit8
12268                     f -- immediate value
12269                     l -- .lit4
12270
12271                     The .lit4 and .lit8 sections are only used if
12272                     permitted by the -G argument.
12273
12274                     The code below needs to know whether the target register
12275                     is 32 or 64 bits wide.  It relies on the fact 'f' and
12276                     'F' are used with GPR-based instructions and 'l' and
12277                     'L' are used with FPR-based instructions.  */
12278
12279                 f64 = *args == 'F' || *args == 'L';
12280                 using_gprs = *args == 'F' || *args == 'f';
12281
12282                 save_in = input_line_pointer;
12283                 input_line_pointer = s;
12284                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12285                 length = len;
12286                 s = input_line_pointer;
12287                 input_line_pointer = save_in;
12288                 if (err != NULL && *err != '\0')
12289                   {
12290                     as_bad (_("Bad floating point constant: %s"), err);
12291                     memset (temp, '\0', sizeof temp);
12292                     length = f64 ? 8 : 4;
12293                   }
12294
12295                 gas_assert (length == (unsigned) (f64 ? 8 : 4));
12296
12297                 if (*args == 'f'
12298                     || (*args == 'l'
12299                         && (g_switch_value < 4
12300                             || (temp[0] == 0 && temp[1] == 0)
12301                             || (temp[2] == 0 && temp[3] == 0))))
12302                   {
12303                     imm_expr.X_op = O_constant;
12304                     if (!target_big_endian)
12305                       imm_expr.X_add_number = bfd_getl32 (temp);
12306                     else
12307                       imm_expr.X_add_number = bfd_getb32 (temp);
12308                   }
12309                 else if (length > 4
12310                          && !mips_disable_float_construction
12311                          /* Constants can only be constructed in GPRs and
12312                             copied to FPRs if the GPRs are at least as wide
12313                             as the FPRs.  Force the constant into memory if
12314                             we are using 64-bit FPRs but the GPRs are only
12315                             32 bits wide.  */
12316                          && (using_gprs
12317                              || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12318                          && ((temp[0] == 0 && temp[1] == 0)
12319                              || (temp[2] == 0 && temp[3] == 0))
12320                          && ((temp[4] == 0 && temp[5] == 0)
12321                              || (temp[6] == 0 && temp[7] == 0)))
12322                   {
12323                     /* The value is simple enough to load with a couple of
12324                        instructions.  If using 32-bit registers, set
12325                        imm_expr to the high order 32 bits and offset_expr to
12326                        the low order 32 bits.  Otherwise, set imm_expr to
12327                        the entire 64 bit constant.  */
12328                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12329                       {
12330                         imm_expr.X_op = O_constant;
12331                         offset_expr.X_op = O_constant;
12332                         if (!target_big_endian)
12333                           {
12334                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
12335                             offset_expr.X_add_number = bfd_getl32 (temp);
12336                           }
12337                         else
12338                           {
12339                             imm_expr.X_add_number = bfd_getb32 (temp);
12340                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
12341                           }
12342                         if (offset_expr.X_add_number == 0)
12343                           offset_expr.X_op = O_absent;
12344                       }
12345                     else if (sizeof (imm_expr.X_add_number) > 4)
12346                       {
12347                         imm_expr.X_op = O_constant;
12348                         if (!target_big_endian)
12349                           imm_expr.X_add_number = bfd_getl64 (temp);
12350                         else
12351                           imm_expr.X_add_number = bfd_getb64 (temp);
12352                       }
12353                     else
12354                       {
12355                         imm_expr.X_op = O_big;
12356                         imm_expr.X_add_number = 4;
12357                         if (!target_big_endian)
12358                           {
12359                             generic_bignum[0] = bfd_getl16 (temp);
12360                             generic_bignum[1] = bfd_getl16 (temp + 2);
12361                             generic_bignum[2] = bfd_getl16 (temp + 4);
12362                             generic_bignum[3] = bfd_getl16 (temp + 6);
12363                           }
12364                         else
12365                           {
12366                             generic_bignum[0] = bfd_getb16 (temp + 6);
12367                             generic_bignum[1] = bfd_getb16 (temp + 4);
12368                             generic_bignum[2] = bfd_getb16 (temp + 2);
12369                             generic_bignum[3] = bfd_getb16 (temp);
12370                           }
12371                       }
12372                   }
12373                 else
12374                   {
12375                     const char *newname;
12376                     segT new_seg;
12377
12378                     /* Switch to the right section.  */
12379                     seg = now_seg;
12380                     subseg = now_subseg;
12381                     switch (*args)
12382                       {
12383                       default: /* unused default case avoids warnings.  */
12384                       case 'L':
12385                         newname = RDATA_SECTION_NAME;
12386                         if (g_switch_value >= 8)
12387                           newname = ".lit8";
12388                         break;
12389                       case 'F':
12390                         newname = RDATA_SECTION_NAME;
12391                         break;
12392                       case 'l':
12393                         gas_assert (g_switch_value >= 4);
12394                         newname = ".lit4";
12395                         break;
12396                       }
12397                     new_seg = subseg_new (newname, (subsegT) 0);
12398                     if (IS_ELF)
12399                       bfd_set_section_flags (stdoutput, new_seg,
12400                                              (SEC_ALLOC
12401                                               | SEC_LOAD
12402                                               | SEC_READONLY
12403                                               | SEC_DATA));
12404                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
12405                     if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
12406                       record_alignment (new_seg, 4);
12407                     else
12408                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
12409                     if (seg == now_seg)
12410                       as_bad (_("Can't use floating point insn in this section"));
12411
12412                     /* Set the argument to the current address in the
12413                        section.  */
12414                     offset_expr.X_op = O_symbol;
12415                     offset_expr.X_add_symbol = symbol_temp_new_now ();
12416                     offset_expr.X_add_number = 0;
12417
12418                     /* Put the floating point number into the section.  */
12419                     p = frag_more ((int) length);
12420                     memcpy (p, temp, length);
12421
12422                     /* Switch back to the original section.  */
12423                     subseg_set (seg, subseg);
12424                   }
12425               }
12426               continue;
12427
12428             case 'i':           /* 16-bit unsigned immediate.  */
12429             case 'j':           /* 16-bit signed immediate.  */
12430               *imm_reloc = BFD_RELOC_LO16;
12431               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12432                 {
12433                   int more;
12434                   offsetT minval, maxval;
12435
12436                   more = (insn + 1 < past
12437                           && strcmp (insn->name, insn[1].name) == 0);
12438
12439                   /* If the expression was written as an unsigned number,
12440                      only treat it as signed if there are no more
12441                      alternatives.  */
12442                   if (more
12443                       && *args == 'j'
12444                       && sizeof (imm_expr.X_add_number) <= 4
12445                       && imm_expr.X_op == O_constant
12446                       && imm_expr.X_add_number < 0
12447                       && imm_expr.X_unsigned
12448                       && HAVE_64BIT_GPRS)
12449                     break;
12450
12451                   /* For compatibility with older assemblers, we accept
12452                      0x8000-0xffff as signed 16-bit numbers when only
12453                      signed numbers are allowed.  */
12454                   if (*args == 'i')
12455                     minval = 0, maxval = 0xffff;
12456                   else if (more)
12457                     minval = -0x8000, maxval = 0x7fff;
12458                   else
12459                     minval = -0x8000, maxval = 0xffff;
12460
12461                   if (imm_expr.X_op != O_constant
12462                       || imm_expr.X_add_number < minval
12463                       || imm_expr.X_add_number > maxval)
12464                     {
12465                       if (more)
12466                         break;
12467                       if (imm_expr.X_op == O_constant
12468                           || imm_expr.X_op == O_big)
12469                         as_bad (_("Expression out of range"));
12470                     }
12471                 }
12472               s = expr_end;
12473               continue;
12474
12475             case 'o':           /* 16-bit offset.  */
12476               offset_reloc[0] = BFD_RELOC_LO16;
12477               offset_reloc[1] = BFD_RELOC_UNUSED;
12478               offset_reloc[2] = BFD_RELOC_UNUSED;
12479
12480               /* Check whether there is only a single bracketed expression
12481                  left.  If so, it must be the base register and the
12482                  constant must be zero.  */
12483               if (*s == '(' && strchr (s + 1, '(') == 0)
12484                 {
12485                   offset_expr.X_op = O_constant;
12486                   offset_expr.X_add_number = 0;
12487                   continue;
12488                 }
12489
12490               /* If this value won't fit into a 16 bit offset, then go
12491                  find a macro that will generate the 32 bit offset
12492                  code pattern.  */
12493               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12494                   && (offset_expr.X_op != O_constant
12495                       || offset_expr.X_add_number >= 0x8000
12496                       || offset_expr.X_add_number < -0x8000))
12497                 break;
12498
12499               s = expr_end;
12500               continue;
12501
12502             case 'p':           /* PC-relative offset.  */
12503               *offset_reloc = BFD_RELOC_16_PCREL_S2;
12504               my_getExpression (&offset_expr, s);
12505               s = expr_end;
12506               continue;
12507
12508             case 'u':           /* Upper 16 bits.  */
12509               *imm_reloc = BFD_RELOC_LO16;
12510               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12511                   && imm_expr.X_op == O_constant
12512                   && (imm_expr.X_add_number < 0
12513                       || imm_expr.X_add_number >= 0x10000))
12514                 as_bad (_("lui expression (%lu) not in range 0..65535"),
12515                         (unsigned long) imm_expr.X_add_number);
12516               s = expr_end;
12517               continue;
12518
12519             case 'a':           /* 26-bit address.  */
12520               *offset_reloc = BFD_RELOC_MIPS_JMP;
12521               my_getExpression (&offset_expr, s);
12522               s = expr_end;
12523               continue;
12524
12525             case 'N':           /* 3-bit branch condition code.  */
12526             case 'M':           /* 3-bit compare condition code.  */
12527               rtype = RTYPE_CCC;
12528               if (ip->insn_mo->pinfo & (FP_D | FP_S))
12529                 rtype |= RTYPE_FCC;
12530               if (!reg_lookup (&s, rtype, &regno))
12531                 break;
12532               if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12533                    || strcmp (str + strlen (str) - 5, "any2f") == 0
12534                    || strcmp (str + strlen (str) - 5, "any2t") == 0)
12535                   && (regno & 1) != 0)
12536                 as_warn (_("Condition code register should be even for %s, "
12537                            "was %d"),
12538                          str, regno);
12539               if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12540                    || strcmp (str + strlen (str) - 5, "any4t") == 0)
12541                   && (regno & 3) != 0)
12542                 as_warn (_("Condition code register should be 0 or 4 for %s, "
12543                            "was %d"),
12544                          str, regno);
12545               if (*args == 'N')
12546                 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12547               else
12548                 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12549               continue;
12550
12551             case 'H':
12552               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12553                 s += 2;
12554               if (ISDIGIT (*s))
12555                 {
12556                   c = 0;
12557                   do
12558                     {
12559                       c *= 10;
12560                       c += *s - '0';
12561                       ++s;
12562                     }
12563                   while (ISDIGIT (*s));
12564                 }
12565               else
12566                 c = 8; /* Invalid sel value.  */
12567
12568               if (c > 7)
12569                 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12570               INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12571               continue;
12572
12573             case 'e':
12574               gas_assert (!mips_opts.micromips);
12575               /* Must be at least one digit.  */
12576               my_getExpression (&imm_expr, s);
12577               check_absolute_expr (ip, &imm_expr);
12578
12579               if ((unsigned long) imm_expr.X_add_number
12580                   > (unsigned long) OP_MASK_VECBYTE)
12581                 {
12582                   as_bad (_("bad byte vector index (%ld)"),
12583                            (long) imm_expr.X_add_number);
12584                   imm_expr.X_add_number = 0;
12585                 }
12586
12587               INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12588               imm_expr.X_op = O_absent;
12589               s = expr_end;
12590               continue;
12591
12592             case '%':
12593               gas_assert (!mips_opts.micromips);
12594               my_getExpression (&imm_expr, s);
12595               check_absolute_expr (ip, &imm_expr);
12596
12597               if ((unsigned long) imm_expr.X_add_number
12598                   > (unsigned long) OP_MASK_VECALIGN)
12599                 {
12600                   as_bad (_("bad byte vector index (%ld)"),
12601                            (long) imm_expr.X_add_number);
12602                   imm_expr.X_add_number = 0;
12603                 }
12604
12605               INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12606               imm_expr.X_op = O_absent;
12607               s = expr_end;
12608               continue;
12609
12610             case 'm':           /* Opcode extension character.  */
12611               gas_assert (mips_opts.micromips);
12612               c = *++args;
12613               switch (c)
12614                 {
12615                 case 'r':
12616                   if (strncmp (s, "$pc", 3) == 0)
12617                     {
12618                       s += 3;
12619                       continue;
12620                     }
12621                   break;
12622
12623                 case 'a':
12624                 case 'b':
12625                 case 'c':
12626                 case 'd':
12627                 case 'e':
12628                 case 'f':
12629                 case 'g':
12630                 case 'h':
12631                 case 'i':
12632                 case 'j':
12633                 case 'l':
12634                 case 'm':
12635                 case 'n':
12636                 case 'p':
12637                 case 'q':
12638                 case 's':
12639                 case 't':
12640                 case 'x':
12641                 case 'y':
12642                 case 'z':
12643                   s_reset = s;
12644                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12645                   if (regno == AT && mips_opts.at)
12646                     {
12647                       if (mips_opts.at == ATREG)
12648                         as_warn (_("Used $at without \".set noat\""));
12649                       else
12650                         as_warn (_("Used $%u with \".set at=$%u\""),
12651                                  regno, mips_opts.at);
12652                     }
12653                   if (!ok)
12654                     {
12655                       if (c == 'c')
12656                         {
12657                           gas_assert (args[1] == ',');
12658                           regno = lastregno;
12659                           ++args;
12660                         }
12661                       else if (c == 't')
12662                         {
12663                           gas_assert (args[1] == ',');
12664                           ++args;
12665                           continue;                     /* Nothing to do.  */
12666                         }
12667                       else
12668                         break;
12669                     }
12670
12671                   if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12672                     {
12673                       if (regno == lastregno)
12674                         {
12675                           insn_error
12676                             = _("Source and destination must be different");
12677                           continue;
12678                         }
12679                       if (regno == 31 && lastregno == 0xffffffff)
12680                         {
12681                           insn_error
12682                             = _("A destination register must be supplied");
12683                           continue;
12684                         }
12685                     }
12686
12687                   if (*s == ' ')
12688                     ++s;
12689                   if (args[1] != *s)
12690                     {
12691                       if (c == 'e')
12692                         {
12693                           gas_assert (args[1] == ',');
12694                           regno = lastregno;
12695                           s = s_reset;
12696                           ++args;
12697                         }
12698                       else if (c == 't')
12699                         {
12700                           gas_assert (args[1] == ',');
12701                           s = s_reset;
12702                           ++args;
12703                           continue;                     /* Nothing to do.  */
12704                         }
12705                     }
12706
12707                   /* Make sure regno is the same as lastregno.  */
12708                   if (c == 't' && regno != lastregno)
12709                     break;
12710
12711                   /* Make sure regno is the same as destregno.  */
12712                   if (c == 'x' && regno != destregno)
12713                     break;
12714
12715                   /* We need to save regno, before regno maps to the
12716                      microMIPS register encoding.  */
12717                   lastregno = regno;
12718
12719                   if (c == 'f')
12720                     destregno = regno;
12721
12722                   switch (c)
12723                     {
12724                       case 'a':
12725                         if (regno != GP)
12726                           regno = ILLEGAL_REG;
12727                         break;
12728
12729                       case 'b':
12730                         regno = mips32_to_micromips_reg_b_map[regno];
12731                         break;
12732
12733                       case 'c':
12734                         regno = mips32_to_micromips_reg_c_map[regno];
12735                         break;
12736
12737                       case 'd':
12738                         regno = mips32_to_micromips_reg_d_map[regno];
12739                         break;
12740
12741                       case 'e':
12742                         regno = mips32_to_micromips_reg_e_map[regno];
12743                         break;
12744
12745                       case 'f':
12746                         regno = mips32_to_micromips_reg_f_map[regno];
12747                         break;
12748
12749                       case 'g':
12750                         regno = mips32_to_micromips_reg_g_map[regno];
12751                         break;
12752
12753                       case 'h':
12754                         regno = mips32_to_micromips_reg_h_map[regno];
12755                         break;
12756
12757                       case 'i':
12758                         switch (EXTRACT_OPERAND (1, MI, *ip))
12759                           {
12760                             case 4:
12761                               if (regno == 21)
12762                                 regno = 3;
12763                               else if (regno == 22)
12764                                 regno = 4;
12765                               else if (regno == 5)
12766                                 regno = 5;
12767                               else if (regno == 6)
12768                                 regno = 6;
12769                               else if (regno == 7)
12770                                 regno = 7;
12771                               else
12772                                 regno = ILLEGAL_REG;
12773                               break;
12774
12775                             case 5:
12776                               if (regno == 6)
12777                                 regno = 0;
12778                               else if (regno == 7)
12779                                 regno = 1;
12780                               else
12781                                 regno = ILLEGAL_REG;
12782                               break;
12783
12784                             case 6:
12785                               if (regno == 7)
12786                                 regno = 2;
12787                               else
12788                                 regno = ILLEGAL_REG;
12789                               break;
12790
12791                             default:
12792                               regno = ILLEGAL_REG;
12793                               break;
12794                           }
12795                         break;
12796
12797                       case 'l':
12798                         regno = mips32_to_micromips_reg_l_map[regno];
12799                         break;
12800
12801                       case 'm':
12802                         regno = mips32_to_micromips_reg_m_map[regno];
12803                         break;
12804
12805                       case 'n':
12806                         regno = mips32_to_micromips_reg_n_map[regno];
12807                         break;
12808
12809                       case 'q':
12810                         regno = mips32_to_micromips_reg_q_map[regno];
12811                         break;
12812
12813                       case 's':
12814                         if (regno != SP)
12815                           regno = ILLEGAL_REG;
12816                         break;
12817
12818                       case 'y':
12819                         if (regno != 31)
12820                           regno = ILLEGAL_REG;
12821                         break;
12822
12823                       case 'z':
12824                         if (regno != ZERO)
12825                           regno = ILLEGAL_REG;
12826                         break;
12827
12828                       case 'j': /* Do nothing.  */
12829                       case 'p':
12830                       case 't':
12831                       case 'x':
12832                         break;
12833
12834                       default:
12835                         abort ();
12836                     }
12837
12838                   if (regno == ILLEGAL_REG)
12839                     break;
12840
12841                   switch (c)
12842                     {
12843                       case 'b':
12844                         INSERT_OPERAND (1, MB, *ip, regno);
12845                         break;
12846
12847                       case 'c':
12848                         INSERT_OPERAND (1, MC, *ip, regno);
12849                         break;
12850
12851                       case 'd':
12852                         INSERT_OPERAND (1, MD, *ip, regno);
12853                         break;
12854
12855                       case 'e':
12856                         INSERT_OPERAND (1, ME, *ip, regno);
12857                         break;
12858
12859                       case 'f':
12860                         INSERT_OPERAND (1, MF, *ip, regno);
12861                         break;
12862
12863                       case 'g':
12864                         INSERT_OPERAND (1, MG, *ip, regno);
12865                         break;
12866
12867                       case 'h':
12868                         INSERT_OPERAND (1, MH, *ip, regno);
12869                         break;
12870
12871                       case 'i':
12872                         INSERT_OPERAND (1, MI, *ip, regno);
12873                         break;
12874
12875                       case 'j':
12876                         INSERT_OPERAND (1, MJ, *ip, regno);
12877                         break;
12878
12879                       case 'l':
12880                         INSERT_OPERAND (1, ML, *ip, regno);
12881                         break;
12882
12883                       case 'm':
12884                         INSERT_OPERAND (1, MM, *ip, regno);
12885                         break;
12886
12887                       case 'n':
12888                         INSERT_OPERAND (1, MN, *ip, regno);
12889                         break;
12890
12891                       case 'p':
12892                         INSERT_OPERAND (1, MP, *ip, regno);
12893                         break;
12894
12895                       case 'q':
12896                         INSERT_OPERAND (1, MQ, *ip, regno);
12897                         break;
12898
12899                       case 'a': /* Do nothing.  */
12900                       case 's': /* Do nothing.  */
12901                       case 't': /* Do nothing.  */
12902                       case 'x': /* Do nothing.  */
12903                       case 'y': /* Do nothing.  */
12904                       case 'z': /* Do nothing.  */
12905                         break;
12906
12907                       default:
12908                         abort ();
12909                     }
12910                   continue;
12911
12912                 case 'A':
12913                   {
12914                     bfd_reloc_code_real_type r[3];
12915                     expressionS ep;
12916                     int imm;
12917
12918                     /* Check whether there is only a single bracketed
12919                        expression left.  If so, it must be the base register
12920                        and the constant must be zero.  */
12921                     if (*s == '(' && strchr (s + 1, '(') == 0)
12922                       {
12923                         INSERT_OPERAND (1, IMMA, *ip, 0);
12924                         continue;
12925                       }
12926
12927                     if (my_getSmallExpression (&ep, r, s) > 0
12928                         || !expr_const_in_range (&ep, -64, 64, 2))
12929                       break;
12930
12931                     imm = ep.X_add_number >> 2;
12932                     INSERT_OPERAND (1, IMMA, *ip, imm);
12933                   }
12934                   s = expr_end;
12935                   continue;
12936
12937                 case 'B':
12938                   {
12939                     bfd_reloc_code_real_type r[3];
12940                     expressionS ep;
12941                     int imm;
12942
12943                     if (my_getSmallExpression (&ep, r, s) > 0
12944                         || ep.X_op != O_constant)
12945                       break;
12946
12947                     for (imm = 0; imm < 8; imm++)
12948                       if (micromips_imm_b_map[imm] == ep.X_add_number)
12949                         break;
12950                     if (imm >= 8)
12951                       break;
12952
12953                     INSERT_OPERAND (1, IMMB, *ip, imm);
12954                   }
12955                   s = expr_end;
12956                   continue;
12957
12958                 case 'C':
12959                   {
12960                     bfd_reloc_code_real_type r[3];
12961                     expressionS ep;
12962                     int imm;
12963
12964                     if (my_getSmallExpression (&ep, r, s) > 0
12965                         || ep.X_op != O_constant)
12966                       break;
12967
12968                     for (imm = 0; imm < 16; imm++)
12969                       if (micromips_imm_c_map[imm] == ep.X_add_number)
12970                         break;
12971                     if (imm >= 16)
12972                       break;
12973
12974                     INSERT_OPERAND (1, IMMC, *ip, imm);
12975                   }
12976                   s = expr_end;
12977                   continue;
12978
12979                 case 'D':       /* pc relative offset */
12980                 case 'E':       /* pc relative offset */
12981                   my_getExpression (&offset_expr, s);
12982                   if (offset_expr.X_op == O_register)
12983                     break;
12984
12985                   if (!forced_insn_length)
12986                     *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12987                   else if (c == 'D')
12988                     *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12989                   else
12990                     *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
12991                   s = expr_end;
12992                   continue;
12993
12994                 case 'F':
12995                   {
12996                     bfd_reloc_code_real_type r[3];
12997                     expressionS ep;
12998                     int imm;
12999
13000                     if (my_getSmallExpression (&ep, r, s) > 0
13001                         || !expr_const_in_range (&ep, 0, 16, 0))
13002                       break;
13003
13004                     imm = ep.X_add_number;
13005                     INSERT_OPERAND (1, IMMF, *ip, imm);
13006                   }
13007                   s = expr_end;
13008                   continue;
13009
13010                 case 'G':
13011                   {
13012                     bfd_reloc_code_real_type r[3];
13013                     expressionS ep;
13014                     int imm;
13015
13016                     /* Check whether there is only a single bracketed
13017                        expression left.  If so, it must be the base register
13018                        and the constant must be zero.  */
13019                     if (*s == '(' && strchr (s + 1, '(') == 0)
13020                       {
13021                         INSERT_OPERAND (1, IMMG, *ip, 0);
13022                         continue;
13023                       }
13024
13025                     if (my_getSmallExpression (&ep, r, s) > 0
13026                         || !expr_const_in_range (&ep, -1, 15, 0))
13027                       break;
13028
13029                     imm = ep.X_add_number & 15;
13030                     INSERT_OPERAND (1, IMMG, *ip, imm);
13031                   }
13032                   s = expr_end;
13033                   continue;
13034
13035                 case 'H':
13036                   {
13037                     bfd_reloc_code_real_type r[3];
13038                     expressionS ep;
13039                     int imm;
13040
13041                     /* Check whether there is only a single bracketed
13042                        expression left.  If so, it must be the base register
13043                        and the constant must be zero.  */
13044                     if (*s == '(' && strchr (s + 1, '(') == 0)
13045                       {
13046                         INSERT_OPERAND (1, IMMH, *ip, 0);
13047                         continue;
13048                       }
13049
13050                     if (my_getSmallExpression (&ep, r, s) > 0
13051                         || !expr_const_in_range (&ep, 0, 16, 1))
13052                       break;
13053
13054                     imm = ep.X_add_number >> 1;
13055                     INSERT_OPERAND (1, IMMH, *ip, imm);
13056                   }
13057                   s = expr_end;
13058                   continue;
13059
13060                 case 'I':
13061                   {
13062                     bfd_reloc_code_real_type r[3];
13063                     expressionS ep;
13064                     int imm;
13065
13066                     if (my_getSmallExpression (&ep, r, s) > 0
13067                         || !expr_const_in_range (&ep, -1, 127, 0))
13068                       break;
13069
13070                     imm = ep.X_add_number & 127;
13071                     INSERT_OPERAND (1, IMMI, *ip, imm);
13072                   }
13073                   s = expr_end;
13074                   continue;
13075
13076                 case 'J':
13077                   {
13078                     bfd_reloc_code_real_type r[3];
13079                     expressionS ep;
13080                     int imm;
13081
13082                     /* Check whether there is only a single bracketed
13083                        expression left.  If so, it must be the base register
13084                        and the constant must be zero.  */
13085                     if (*s == '(' && strchr (s + 1, '(') == 0)
13086                       {
13087                         INSERT_OPERAND (1, IMMJ, *ip, 0);
13088                         continue;
13089                       }
13090
13091                     if (my_getSmallExpression (&ep, r, s) > 0
13092                         || !expr_const_in_range (&ep, 0, 16, 2))
13093                       break;
13094
13095                     imm = ep.X_add_number >> 2;
13096                     INSERT_OPERAND (1, IMMJ, *ip, imm);
13097                   }
13098                   s = expr_end;
13099                   continue;
13100
13101                 case 'L':
13102                   {
13103                     bfd_reloc_code_real_type r[3];
13104                     expressionS ep;
13105                     int imm;
13106
13107                     /* Check whether there is only a single bracketed
13108                        expression left.  If so, it must be the base register
13109                        and the constant must be zero.  */
13110                     if (*s == '(' && strchr (s + 1, '(') == 0)
13111                       {
13112                         INSERT_OPERAND (1, IMML, *ip, 0);
13113                         continue;
13114                       }
13115
13116                     if (my_getSmallExpression (&ep, r, s) > 0
13117                         || !expr_const_in_range (&ep, 0, 16, 0))
13118                       break;
13119
13120                     imm = ep.X_add_number;
13121                     INSERT_OPERAND (1, IMML, *ip, imm);
13122                   }
13123                   s = expr_end;
13124                   continue;
13125
13126                 case 'M':
13127                   {
13128                     bfd_reloc_code_real_type r[3];
13129                     expressionS ep;
13130                     int imm;
13131
13132                     if (my_getSmallExpression (&ep, r, s) > 0
13133                         || !expr_const_in_range (&ep, 1, 9, 0))
13134                       break;
13135
13136                     imm = ep.X_add_number & 7;
13137                     INSERT_OPERAND (1, IMMM, *ip, imm);
13138                   }
13139                   s = expr_end;
13140                   continue;
13141
13142                 case 'N':       /* Register list for lwm and swm.  */
13143                   {
13144                     /* A comma-separated list of registers and/or
13145                        dash-separated contiguous ranges including
13146                        both ra and a set of one or more registers
13147                        starting at s0 up to s3 which have to be
13148                        consecutive, e.g.:
13149
13150                        s0, ra
13151                        s0, s1, ra, s2, s3
13152                        s0-s2, ra
13153
13154                        and any permutations of these.  */
13155                     unsigned int reglist;
13156                     int imm;
13157
13158                     if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13159                       break;
13160
13161                     if ((reglist & 0xfff1ffff) != 0x80010000)
13162                       break;
13163
13164                     reglist = (reglist >> 17) & 7;
13165                     reglist += 1;
13166                     if ((reglist & -reglist) != reglist)
13167                       break;
13168
13169                     imm = ffs (reglist) - 1;
13170                     INSERT_OPERAND (1, IMMN, *ip, imm);
13171                   }
13172                   continue;
13173
13174                 case 'O':       /* sdbbp 4-bit code.  */
13175                   {
13176                     bfd_reloc_code_real_type r[3];
13177                     expressionS ep;
13178                     int imm;
13179
13180                     if (my_getSmallExpression (&ep, r, s) > 0
13181                         || !expr_const_in_range (&ep, 0, 16, 0))
13182                       break;
13183
13184                     imm = ep.X_add_number;
13185                     INSERT_OPERAND (1, IMMO, *ip, imm);
13186                   }
13187                   s = expr_end;
13188                   continue;
13189
13190                 case 'P':
13191                   {
13192                     bfd_reloc_code_real_type r[3];
13193                     expressionS ep;
13194                     int imm;
13195
13196                     if (my_getSmallExpression (&ep, r, s) > 0
13197                         || !expr_const_in_range (&ep, 0, 32, 2))
13198                       break;
13199
13200                     imm = ep.X_add_number >> 2;
13201                     INSERT_OPERAND (1, IMMP, *ip, imm);
13202                   }
13203                   s = expr_end;
13204                   continue;
13205
13206                 case 'Q':
13207                   {
13208                     bfd_reloc_code_real_type r[3];
13209                     expressionS ep;
13210                     int imm;
13211
13212                     if (my_getSmallExpression (&ep, r, s) > 0
13213                         || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13214                       break;
13215
13216                     imm = ep.X_add_number >> 2;
13217                     INSERT_OPERAND (1, IMMQ, *ip, imm);
13218                   }
13219                   s = expr_end;
13220                   continue;
13221
13222                 case 'U':
13223                   {
13224                     bfd_reloc_code_real_type r[3];
13225                     expressionS ep;
13226                     int imm;
13227
13228                     /* Check whether there is only a single bracketed
13229                        expression left.  If so, it must be the base register
13230                        and the constant must be zero.  */
13231                     if (*s == '(' && strchr (s + 1, '(') == 0)
13232                       {
13233                         INSERT_OPERAND (1, IMMU, *ip, 0);
13234                         continue;
13235                       }
13236
13237                     if (my_getSmallExpression (&ep, r, s) > 0
13238                         || !expr_const_in_range (&ep, 0, 32, 2))
13239                       break;
13240
13241                     imm = ep.X_add_number >> 2;
13242                     INSERT_OPERAND (1, IMMU, *ip, imm);
13243                   }
13244                   s = expr_end;
13245                   continue;
13246
13247                 case 'W':
13248                   {
13249                     bfd_reloc_code_real_type r[3];
13250                     expressionS ep;
13251                     int imm;
13252
13253                     if (my_getSmallExpression (&ep, r, s) > 0
13254                         || !expr_const_in_range (&ep, 0, 64, 2))
13255                       break;
13256
13257                     imm = ep.X_add_number >> 2;
13258                     INSERT_OPERAND (1, IMMW, *ip, imm);
13259                   }
13260                   s = expr_end;
13261                   continue;
13262
13263                 case 'X':
13264                   {
13265                     bfd_reloc_code_real_type r[3];
13266                     expressionS ep;
13267                     int imm;
13268
13269                     if (my_getSmallExpression (&ep, r, s) > 0
13270                         || !expr_const_in_range (&ep, -8, 8, 0))
13271                       break;
13272
13273                     imm = ep.X_add_number;
13274                     INSERT_OPERAND (1, IMMX, *ip, imm);
13275                   }
13276                   s = expr_end;
13277                   continue;
13278
13279                 case 'Y':
13280                   {
13281                     bfd_reloc_code_real_type r[3];
13282                     expressionS ep;
13283                     int imm;
13284
13285                     if (my_getSmallExpression (&ep, r, s) > 0
13286                         || expr_const_in_range (&ep, -2, 2, 2)
13287                         || !expr_const_in_range (&ep, -258, 258, 2))
13288                       break;
13289
13290                     imm = ep.X_add_number >> 2;
13291                     imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13292                     INSERT_OPERAND (1, IMMY, *ip, imm);
13293                   }
13294                   s = expr_end;
13295                   continue;
13296
13297                 case 'Z':
13298                   {
13299                     bfd_reloc_code_real_type r[3];
13300                     expressionS ep;
13301
13302                     if (my_getSmallExpression (&ep, r, s) > 0
13303                         || !expr_const_in_range (&ep, 0, 1, 0))
13304                       break;
13305                   }
13306                   s = expr_end;
13307                   continue;
13308
13309                 default:
13310                   as_bad (_("Internal error: bad microMIPS opcode "
13311                             "(unknown extension operand type `m%c'): %s %s"),
13312                           *args, insn->name, insn->args);
13313                   /* Further processing is fruitless.  */
13314                   return;
13315                 }
13316               break;
13317
13318             case 'n':           /* Register list for 32-bit lwm and swm.  */
13319               gas_assert (mips_opts.micromips);
13320               {
13321                 /* A comma-separated list of registers and/or
13322                    dash-separated contiguous ranges including
13323                    at least one of ra and a set of one or more
13324                    registers starting at s0 up to s7 and then
13325                    s8 which have to be consecutive, e.g.:
13326
13327                    ra
13328                    s0
13329                    ra, s0, s1, s2
13330                    s0-s8
13331                    s0-s5, ra
13332
13333                    and any permutations of these.  */
13334                 unsigned int reglist;
13335                 int imm;
13336                 int ra;
13337
13338                 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13339                   break;
13340
13341                 if ((reglist & 0x3f00ffff) != 0)
13342                   break;
13343
13344                 ra = (reglist >> 27) & 0x10;
13345                 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13346                 reglist += 1;
13347                 if ((reglist & -reglist) != reglist)
13348                   break;
13349
13350                 imm = (ffs (reglist) - 1) | ra;
13351                 INSERT_OPERAND (1, RT, *ip, imm);
13352                 imm_expr.X_op = O_absent;
13353               }
13354               continue;
13355
13356             case '|':           /* 4-bit trap code.  */
13357               gas_assert (mips_opts.micromips);
13358               my_getExpression (&imm_expr, s);
13359               check_absolute_expr (ip, &imm_expr);
13360               if ((unsigned long) imm_expr.X_add_number
13361                   > MICROMIPSOP_MASK_TRAP)
13362                 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13363                         (unsigned long) imm_expr.X_add_number,
13364                         ip->insn_mo->name);
13365               INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13366               imm_expr.X_op = O_absent;
13367               s = expr_end;
13368               continue;
13369
13370             default:
13371               as_bad (_("Bad char = '%c'\n"), *args);
13372               abort ();
13373             }
13374           break;
13375         }
13376       /* Args don't match.  */
13377       s = argsStart;
13378       insn_error = _("Illegal operands");
13379       if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13380         {
13381           ++insn;
13382           continue;
13383         }
13384       else if (wrong_delay_slot_insns && need_delay_slot_ok)
13385         {
13386           gas_assert (firstinsn);
13387           need_delay_slot_ok = FALSE;
13388           past = insn + 1;
13389           insn = firstinsn;
13390           continue;
13391         }
13392       return;
13393     }
13394 }
13395
13396 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13397
13398 /* This routine assembles an instruction into its binary format when
13399    assembling for the mips16.  As a side effect, it sets one of the
13400    global variables imm_reloc or offset_reloc to the type of relocation
13401    to do if one of the operands is an address expression.  It also sets
13402    forced_insn_length to the resulting instruction size in bytes if the
13403    user explicitly requested a small or extended instruction.  */
13404
13405 static void
13406 mips16_ip (char *str, struct mips_cl_insn *ip)
13407 {
13408   char *s;
13409   const char *args;
13410   struct mips_opcode *insn;
13411   char *argsstart;
13412   unsigned int regno;
13413   unsigned int lastregno = 0;
13414   char *s_reset;
13415   size_t i;
13416
13417   insn_error = NULL;
13418
13419   forced_insn_length = 0;
13420
13421   for (s = str; ISLOWER (*s); ++s)
13422     ;
13423   switch (*s)
13424     {
13425     case '\0':
13426       break;
13427
13428     case ' ':
13429       *s++ = '\0';
13430       break;
13431
13432     case '.':
13433       if (s[1] == 't' && s[2] == ' ')
13434         {
13435           *s = '\0';
13436           forced_insn_length = 2;
13437           s += 3;
13438           break;
13439         }
13440       else if (s[1] == 'e' && s[2] == ' ')
13441         {
13442           *s = '\0';
13443           forced_insn_length = 4;
13444           s += 3;
13445           break;
13446         }
13447       /* Fall through.  */
13448     default:
13449       insn_error = _("unknown opcode");
13450       return;
13451     }
13452
13453   if (mips_opts.noautoextend && !forced_insn_length)
13454     forced_insn_length = 2;
13455
13456   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13457     {
13458       insn_error = _("unrecognized opcode");
13459       return;
13460     }
13461
13462   argsstart = s;
13463   for (;;)
13464     {
13465       bfd_boolean ok;
13466
13467       gas_assert (strcmp (insn->name, str) == 0);
13468
13469       ok = is_opcode_valid_16 (insn);
13470       if (! ok)
13471         {
13472           if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13473               && strcmp (insn->name, insn[1].name) == 0)
13474             {
13475               ++insn;
13476               continue;
13477             }
13478           else
13479             {
13480               if (!insn_error)
13481                 {
13482                   static char buf[100];
13483                   sprintf (buf,
13484                            _("Opcode not supported on this processor: %s (%s)"),
13485                            mips_cpu_info_from_arch (mips_opts.arch)->name,
13486                            mips_cpu_info_from_isa (mips_opts.isa)->name);
13487                   insn_error = buf;
13488                 }
13489               return;
13490             }
13491         }
13492
13493       create_insn (ip, insn);
13494       imm_expr.X_op = O_absent;
13495       imm_reloc[0] = BFD_RELOC_UNUSED;
13496       imm_reloc[1] = BFD_RELOC_UNUSED;
13497       imm_reloc[2] = BFD_RELOC_UNUSED;
13498       imm2_expr.X_op = O_absent;
13499       offset_expr.X_op = O_absent;
13500       offset_reloc[0] = BFD_RELOC_UNUSED;
13501       offset_reloc[1] = BFD_RELOC_UNUSED;
13502       offset_reloc[2] = BFD_RELOC_UNUSED;
13503       for (args = insn->args; 1; ++args)
13504         {
13505           int c;
13506
13507           if (*s == ' ')
13508             ++s;
13509
13510           /* In this switch statement we call break if we did not find
13511              a match, continue if we did find a match, or return if we
13512              are done.  */
13513
13514           c = *args;
13515           switch (c)
13516             {
13517             case '\0':
13518               if (*s == '\0')
13519                 {
13520                   offsetT value;
13521
13522                   /* Stuff the immediate value in now, if we can.  */
13523                   if (imm_expr.X_op == O_constant
13524                       && *imm_reloc > BFD_RELOC_UNUSED
13525                       && insn->pinfo != INSN_MACRO
13526                       && calculate_reloc (*offset_reloc,
13527                                           imm_expr.X_add_number, &value))
13528                     {
13529                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13530                                     *offset_reloc, value, forced_insn_length,
13531                                     &ip->insn_opcode);
13532                       imm_expr.X_op = O_absent;
13533                       *imm_reloc = BFD_RELOC_UNUSED;
13534                       *offset_reloc = BFD_RELOC_UNUSED;
13535                     }
13536
13537                   return;
13538                 }
13539               break;
13540
13541             case ',':
13542               if (*s++ == c)
13543                 continue;
13544               s--;
13545               switch (*++args)
13546                 {
13547                 case 'v':
13548                   MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13549                   continue;
13550                 case 'w':
13551                   MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13552                   continue;
13553                 }
13554               break;
13555
13556             case '(':
13557             case ')':
13558               if (*s++ == c)
13559                 continue;
13560               break;
13561
13562             case 'v':
13563             case 'w':
13564               if (s[0] != '$')
13565                 {
13566                   if (c == 'v')
13567                     MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13568                   else
13569                     MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13570                   ++args;
13571                   continue;
13572                 }
13573               /* Fall through.  */
13574             case 'x':
13575             case 'y':
13576             case 'z':
13577             case 'Z':
13578             case '0':
13579             case 'S':
13580             case 'R':
13581             case 'X':
13582             case 'Y':
13583               s_reset = s;
13584               if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
13585                 {
13586                   if (c == 'v' || c == 'w')
13587                     {
13588                       if (c == 'v')
13589                         MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13590                       else
13591                         MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13592                       ++args;
13593                       continue;
13594                     }
13595                   break;
13596                 }
13597
13598               if (*s == ' ')
13599                 ++s;
13600               if (args[1] != *s)
13601                 {
13602                   if (c == 'v' || c == 'w')
13603                     {
13604                       regno = mips16_to_32_reg_map[lastregno];
13605                       s = s_reset;
13606                       ++args;
13607                     }
13608                 }
13609
13610               switch (c)
13611                 {
13612                 case 'x':
13613                 case 'y':
13614                 case 'z':
13615                 case 'v':
13616                 case 'w':
13617                 case 'Z':
13618                   regno = mips32_to_16_reg_map[regno];
13619                   break;
13620
13621                 case '0':
13622                   if (regno != 0)
13623                     regno = ILLEGAL_REG;
13624                   break;
13625
13626                 case 'S':
13627                   if (regno != SP)
13628                     regno = ILLEGAL_REG;
13629                   break;
13630
13631                 case 'R':
13632                   if (regno != RA)
13633                     regno = ILLEGAL_REG;
13634                   break;
13635
13636                 case 'X':
13637                 case 'Y':
13638                   if (regno == AT && mips_opts.at)
13639                     {
13640                       if (mips_opts.at == ATREG)
13641                         as_warn (_("used $at without \".set noat\""));
13642                       else
13643                         as_warn (_("used $%u with \".set at=$%u\""),
13644                                  regno, mips_opts.at);
13645                     }
13646                   break;
13647
13648                 default:
13649                   abort ();
13650                 }
13651
13652               if (regno == ILLEGAL_REG)
13653                 break;
13654
13655               switch (c)
13656                 {
13657                 case 'x':
13658                 case 'v':
13659                   MIPS16_INSERT_OPERAND (RX, *ip, regno);
13660                   break;
13661                 case 'y':
13662                 case 'w':
13663                   MIPS16_INSERT_OPERAND (RY, *ip, regno);
13664                   break;
13665                 case 'z':
13666                   MIPS16_INSERT_OPERAND (RZ, *ip, regno);
13667                   break;
13668                 case 'Z':
13669                   MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
13670                 case '0':
13671                 case 'S':
13672                 case 'R':
13673                   break;
13674                 case 'X':
13675                   MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
13676                   break;
13677                 case 'Y':
13678                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
13679                   MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
13680                   break;
13681                 default:
13682                   abort ();
13683                 }
13684
13685               lastregno = regno;
13686               continue;
13687
13688             case 'P':
13689               if (strncmp (s, "$pc", 3) == 0)
13690                 {
13691                   s += 3;
13692                   continue;
13693                 }
13694               break;
13695
13696             case '5':
13697             case 'H':
13698             case 'W':
13699             case 'D':
13700             case 'j':
13701             case 'V':
13702             case 'C':
13703             case 'U':
13704             case 'k':
13705             case 'K':
13706               i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13707               if (i > 0)
13708                 {
13709                   if (imm_expr.X_op != O_constant)
13710                     {
13711                       forced_insn_length = 4;
13712                       ip->insn_opcode |= MIPS16_EXTEND;
13713                     }
13714                   else
13715                     {
13716                       /* We need to relax this instruction.  */
13717                       *offset_reloc = *imm_reloc;
13718                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13719                     }
13720                   s = expr_end;
13721                   continue;
13722                 }
13723               *imm_reloc = BFD_RELOC_UNUSED;
13724               /* Fall through.  */
13725             case '<':
13726             case '>':
13727             case '[':
13728             case ']':
13729             case '4':
13730             case '8':
13731               my_getExpression (&imm_expr, s);
13732               if (imm_expr.X_op == O_register)
13733                 {
13734                   /* What we thought was an expression turned out to
13735                      be a register.  */
13736
13737                   if (s[0] == '(' && args[1] == '(')
13738                     {
13739                       /* It looks like the expression was omitted
13740                          before a register indirection, which means
13741                          that the expression is implicitly zero.  We
13742                          still set up imm_expr, so that we handle
13743                          explicit extensions correctly.  */
13744                       imm_expr.X_op = O_constant;
13745                       imm_expr.X_add_number = 0;
13746                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13747                       continue;
13748                     }
13749
13750                   break;
13751                 }
13752
13753               /* We need to relax this instruction.  */
13754               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13755               s = expr_end;
13756               continue;
13757
13758             case 'p':
13759             case 'q':
13760             case 'A':
13761             case 'B':
13762             case 'E':
13763               /* We use offset_reloc rather than imm_reloc for the PC
13764                  relative operands.  This lets macros with both
13765                  immediate and address operands work correctly.  */
13766               my_getExpression (&offset_expr, s);
13767
13768               if (offset_expr.X_op == O_register)
13769                 break;
13770
13771               /* We need to relax this instruction.  */
13772               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13773               s = expr_end;
13774               continue;
13775
13776             case '6':           /* break code */
13777               my_getExpression (&imm_expr, s);
13778               check_absolute_expr (ip, &imm_expr);
13779               if ((unsigned long) imm_expr.X_add_number > 63)
13780                 as_warn (_("Invalid value for `%s' (%lu)"),
13781                          ip->insn_mo->name,
13782                          (unsigned long) imm_expr.X_add_number);
13783               MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
13784               imm_expr.X_op = O_absent;
13785               s = expr_end;
13786               continue;
13787
13788             case 'a':           /* 26 bit address */
13789               my_getExpression (&offset_expr, s);
13790               s = expr_end;
13791               *offset_reloc = BFD_RELOC_MIPS16_JMP;
13792               ip->insn_opcode <<= 16;
13793               continue;
13794
13795             case 'l':           /* register list for entry macro */
13796             case 'L':           /* register list for exit macro */
13797               {
13798                 int mask;
13799
13800                 if (c == 'l')
13801                   mask = 0;
13802                 else
13803                   mask = 7 << 3;
13804                 while (*s != '\0')
13805                   {
13806                     unsigned int freg, reg1, reg2;
13807
13808                     while (*s == ' ' || *s == ',')
13809                       ++s;
13810                     if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13811                       freg = 0;
13812                     else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13813                       freg = 1;
13814                     else
13815                       {
13816                         as_bad (_("can't parse register list"));
13817                         break;
13818                       }
13819                     if (*s == ' ')
13820                       ++s;
13821                     if (*s != '-')
13822                       reg2 = reg1;
13823                     else
13824                       {
13825                         ++s;
13826                         if (!reg_lookup (&s, freg ? RTYPE_FPU 
13827                                          : (RTYPE_GP | RTYPE_NUM), &reg2))
13828                           {
13829                             as_bad (_("invalid register list"));
13830                             break;
13831                           }
13832                       }
13833                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13834                       {
13835                         mask &= ~ (7 << 3);
13836                         mask |= 5 << 3;
13837                       }
13838                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13839                       {
13840                         mask &= ~ (7 << 3);
13841                         mask |= 6 << 3;
13842                       }
13843                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13844                       mask |= (reg2 - 3) << 3;
13845                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13846                       mask |= (reg2 - 15) << 1;
13847                     else if (reg1 == RA && reg2 == RA)
13848                       mask |= 1;
13849                     else
13850                       {
13851                         as_bad (_("invalid register list"));
13852                         break;
13853                       }
13854                   }
13855                 /* The mask is filled in in the opcode table for the
13856                    benefit of the disassembler.  We remove it before
13857                    applying the actual mask.  */
13858                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13859                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13860               }
13861             continue;
13862
13863             case 'm':           /* Register list for save insn.  */
13864             case 'M':           /* Register list for restore insn.  */
13865               {
13866                 int opcode = ip->insn_opcode;
13867                 int framesz = 0, seen_framesz = 0;
13868                 int nargs = 0, statics = 0, sregs = 0;
13869
13870                 while (*s != '\0')
13871                   {
13872                     unsigned int reg1, reg2;
13873
13874                     SKIP_SPACE_TABS (s);
13875                     while (*s == ',')
13876                       ++s;
13877                     SKIP_SPACE_TABS (s);
13878
13879                     my_getExpression (&imm_expr, s);
13880                     if (imm_expr.X_op == O_constant)
13881                       {
13882                         /* Handle the frame size.  */
13883                         if (seen_framesz)
13884                           {
13885                             as_bad (_("more than one frame size in list"));
13886                             break;
13887                           }
13888                         seen_framesz = 1;
13889                         framesz = imm_expr.X_add_number;
13890                         imm_expr.X_op = O_absent;
13891                         s = expr_end;
13892                         continue;
13893                       }
13894
13895                     if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13896                       {
13897                         as_bad (_("can't parse register list"));
13898                         break;
13899                       }
13900
13901                     while (*s == ' ')
13902                       ++s;
13903
13904                     if (*s != '-')
13905                       reg2 = reg1;
13906                     else
13907                       {
13908                         ++s;
13909                         if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13910                             || reg2 < reg1)
13911                           {
13912                             as_bad (_("can't parse register list"));
13913                             break;
13914                           }
13915                       }
13916
13917                     while (reg1 <= reg2)
13918                       {
13919                         if (reg1 >= 4 && reg1 <= 7)
13920                           {
13921                             if (!seen_framesz)
13922                                 /* args $a0-$a3 */
13923                                 nargs |= 1 << (reg1 - 4);
13924                             else
13925                                 /* statics $a0-$a3 */
13926                                 statics |= 1 << (reg1 - 4);
13927                           }
13928                         else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13929                           {
13930                             /* $s0-$s8 */
13931                             sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13932                           }
13933                         else if (reg1 == 31)
13934                           {
13935                             /* Add $ra to insn.  */
13936                             opcode |= 0x40;
13937                           }
13938                         else
13939                           {
13940                             as_bad (_("unexpected register in list"));
13941                             break;
13942                           }
13943                         if (++reg1 == 24)
13944                           reg1 = 30;
13945                       }
13946                   }
13947
13948                 /* Encode args/statics combination.  */
13949                 if (nargs & statics)
13950                   as_bad (_("arg/static registers overlap"));
13951                 else if (nargs == 0xf)
13952                   /* All $a0-$a3 are args.  */
13953                   opcode |= MIPS16_ALL_ARGS << 16;
13954                 else if (statics == 0xf)
13955                   /* All $a0-$a3 are statics.  */
13956                   opcode |= MIPS16_ALL_STATICS << 16;
13957                 else 
13958                   {
13959                     int narg = 0, nstat = 0;
13960
13961                     /* Count arg registers.  */
13962                     while (nargs & 0x1)
13963                       {
13964                         nargs >>= 1;
13965                         narg++;
13966                       }
13967                     if (nargs != 0)
13968                       as_bad (_("invalid arg register list"));
13969
13970                     /* Count static registers.  */
13971                     while (statics & 0x8)
13972                       {
13973                         statics = (statics << 1) & 0xf;
13974                         nstat++;
13975                       }
13976                     if (statics != 0) 
13977                       as_bad (_("invalid static register list"));
13978
13979                     /* Encode args/statics.  */
13980                     opcode |= ((narg << 2) | nstat) << 16;
13981                   }
13982
13983                 /* Encode $s0/$s1.  */
13984                 if (sregs & (1 << 0))           /* $s0 */
13985                   opcode |= 0x20;
13986                 if (sregs & (1 << 1))           /* $s1 */
13987                   opcode |= 0x10;
13988                 sregs >>= 2;
13989
13990                 if (sregs != 0)
13991                   {
13992                     /* Count regs $s2-$s8.  */
13993                     int nsreg = 0;
13994                     while (sregs & 1)
13995                       {
13996                         sregs >>= 1;
13997                         nsreg++;
13998                       }
13999                     if (sregs != 0)
14000                       as_bad (_("invalid static register list"));
14001                     /* Encode $s2-$s8. */
14002                     opcode |= nsreg << 24;
14003                   }
14004
14005                 /* Encode frame size.  */
14006                 if (!seen_framesz)
14007                   as_bad (_("missing frame size"));
14008                 else if ((framesz & 7) != 0 || framesz < 0
14009                          || framesz > 0xff * 8)
14010                   as_bad (_("invalid frame size"));
14011                 else if (framesz != 128 || (opcode >> 16) != 0)
14012                   {
14013                     framesz /= 8;
14014                     opcode |= (((framesz & 0xf0) << 16)
14015                              | (framesz & 0x0f));
14016                   }
14017
14018                 /* Finally build the instruction.  */
14019                 if ((opcode >> 16) != 0 || framesz == 0)
14020                   opcode |= MIPS16_EXTEND;
14021                 ip->insn_opcode = opcode;
14022               }
14023             continue;
14024
14025             case 'e':           /* extend code */
14026               my_getExpression (&imm_expr, s);
14027               check_absolute_expr (ip, &imm_expr);
14028               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14029                 {
14030                   as_warn (_("Invalid value for `%s' (%lu)"),
14031                            ip->insn_mo->name,
14032                            (unsigned long) imm_expr.X_add_number);
14033                   imm_expr.X_add_number &= 0x7ff;
14034                 }
14035               ip->insn_opcode |= imm_expr.X_add_number;
14036               imm_expr.X_op = O_absent;
14037               s = expr_end;
14038               continue;
14039
14040             default:
14041               abort ();
14042             }
14043           break;
14044         }
14045
14046       /* Args don't match.  */
14047       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14048           strcmp (insn->name, insn[1].name) == 0)
14049         {
14050           ++insn;
14051           s = argsstart;
14052           continue;
14053         }
14054
14055       insn_error = _("illegal operands");
14056
14057       return;
14058     }
14059 }
14060
14061 /* This structure holds information we know about a mips16 immediate
14062    argument type.  */
14063
14064 struct mips16_immed_operand
14065 {
14066   /* The type code used in the argument string in the opcode table.  */
14067   int type;
14068   /* The number of bits in the short form of the opcode.  */
14069   int nbits;
14070   /* The number of bits in the extended form of the opcode.  */
14071   int extbits;
14072   /* The amount by which the short form is shifted when it is used;
14073      for example, the sw instruction has a shift count of 2.  */
14074   int shift;
14075   /* The amount by which the short form is shifted when it is stored
14076      into the instruction code.  */
14077   int op_shift;
14078   /* Non-zero if the short form is unsigned.  */
14079   int unsp;
14080   /* Non-zero if the extended form is unsigned.  */
14081   int extu;
14082   /* Non-zero if the value is PC relative.  */
14083   int pcrel;
14084 };
14085
14086 /* The mips16 immediate operand types.  */
14087
14088 static const struct mips16_immed_operand mips16_immed_operands[] =
14089 {
14090   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14091   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14092   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14093   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14094   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14095   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14096   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14097   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14098   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14099   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14100   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14101   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14102   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14103   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14104   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14105   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14106   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14107   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14108   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14109   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14110   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14111 };
14112
14113 #define MIPS16_NUM_IMMED \
14114   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14115
14116 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14117    NBITS is the number of significant bits in VAL.  */
14118
14119 static unsigned long
14120 mips16_immed_extend (offsetT val, unsigned int nbits)
14121 {
14122   int extval;
14123   if (nbits == 16)
14124     {
14125       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14126       val &= 0x1f;
14127     }
14128   else if (nbits == 15)
14129     {
14130       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14131       val &= 0xf;
14132     }
14133   else
14134     {
14135       extval = ((val & 0x1f) << 6) | (val & 0x20);
14136       val = 0;
14137     }
14138   return (extval << 16) | val;
14139 }
14140
14141 /* Install immediate value VAL into MIPS16 instruction *INSN,
14142    extending it if necessary.  The instruction in *INSN may
14143    already be extended.
14144
14145    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14146    if none.  In the former case, VAL is a 16-bit number with no
14147    defined signedness.
14148
14149    TYPE is the type of the immediate field.  USER_INSN_LENGTH
14150    is the length that the user requested, or 0 if none.  */
14151
14152 static void
14153 mips16_immed (char *file, unsigned int line, int type,
14154               bfd_reloc_code_real_type reloc, offsetT val,
14155               unsigned int user_insn_length, unsigned long *insn)
14156 {
14157   const struct mips16_immed_operand *op;
14158   int mintiny, maxtiny;
14159
14160   op = mips16_immed_operands;
14161   while (op->type != type)
14162     {
14163       ++op;
14164       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14165     }
14166
14167   if (op->unsp)
14168     {
14169       if (type == '<' || type == '>' || type == '[' || type == ']')
14170         {
14171           mintiny = 1;
14172           maxtiny = 1 << op->nbits;
14173         }
14174       else
14175         {
14176           mintiny = 0;
14177           maxtiny = (1 << op->nbits) - 1;
14178         }
14179       if (reloc != BFD_RELOC_UNUSED)
14180         val &= 0xffff;
14181     }
14182   else
14183     {
14184       mintiny = - (1 << (op->nbits - 1));
14185       maxtiny = (1 << (op->nbits - 1)) - 1;
14186       if (reloc != BFD_RELOC_UNUSED)
14187         val = SEXT_16BIT (val);
14188     }
14189
14190   /* Branch offsets have an implicit 0 in the lowest bit.  */
14191   if (type == 'p' || type == 'q')
14192     val /= 2;
14193
14194   if ((val & ((1 << op->shift) - 1)) != 0
14195       || val < (mintiny << op->shift)
14196       || val > (maxtiny << op->shift))
14197     {
14198       /* We need an extended instruction.  */
14199       if (user_insn_length == 2)
14200         as_bad_where (file, line, _("invalid unextended operand value"));
14201       else
14202         *insn |= MIPS16_EXTEND;
14203     }
14204   else if (user_insn_length == 4)
14205     {
14206       /* The operand doesn't force an unextended instruction to be extended.
14207          Warn if the user wanted an extended instruction anyway.  */
14208       *insn |= MIPS16_EXTEND;
14209       as_warn_where (file, line,
14210                      _("extended operand requested but not required"));
14211     }
14212
14213   if (mips16_opcode_length (*insn) == 2)
14214     {
14215       int insnval;
14216
14217       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14218       insnval <<= op->op_shift;
14219       *insn |= insnval;
14220     }
14221   else
14222     {
14223       long minext, maxext;
14224
14225       if (reloc == BFD_RELOC_UNUSED)
14226         {
14227           if (op->extu)
14228             {
14229               minext = 0;
14230               maxext = (1 << op->extbits) - 1;
14231             }
14232           else
14233             {
14234               minext = - (1 << (op->extbits - 1));
14235               maxext = (1 << (op->extbits - 1)) - 1;
14236             }
14237           if (val < minext || val > maxext)
14238             as_bad_where (file, line,
14239                           _("operand value out of range for instruction"));
14240         }
14241
14242       *insn |= mips16_immed_extend (val, op->extbits);
14243     }
14244 }
14245 \f
14246 struct percent_op_match
14247 {
14248   const char *str;
14249   bfd_reloc_code_real_type reloc;
14250 };
14251
14252 static const struct percent_op_match mips_percent_op[] =
14253 {
14254   {"%lo", BFD_RELOC_LO16},
14255 #ifdef OBJ_ELF
14256   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14257   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14258   {"%call16", BFD_RELOC_MIPS_CALL16},
14259   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14260   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14261   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14262   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14263   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14264   {"%got", BFD_RELOC_MIPS_GOT16},
14265   {"%gp_rel", BFD_RELOC_GPREL16},
14266   {"%half", BFD_RELOC_16},
14267   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14268   {"%higher", BFD_RELOC_MIPS_HIGHER},
14269   {"%neg", BFD_RELOC_MIPS_SUB},
14270   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14271   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14272   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14273   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14274   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14275   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14276   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14277 #endif
14278   {"%hi", BFD_RELOC_HI16_S}
14279 };
14280
14281 static const struct percent_op_match mips16_percent_op[] =
14282 {
14283   {"%lo", BFD_RELOC_MIPS16_LO16},
14284   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14285   {"%got", BFD_RELOC_MIPS16_GOT16},
14286   {"%call16", BFD_RELOC_MIPS16_CALL16},
14287   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14288   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14289   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14290   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14291   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14292   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14293   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14294   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14295 };
14296
14297
14298 /* Return true if *STR points to a relocation operator.  When returning true,
14299    move *STR over the operator and store its relocation code in *RELOC.
14300    Leave both *STR and *RELOC alone when returning false.  */
14301
14302 static bfd_boolean
14303 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14304 {
14305   const struct percent_op_match *percent_op;
14306   size_t limit, i;
14307
14308   if (mips_opts.mips16)
14309     {
14310       percent_op = mips16_percent_op;
14311       limit = ARRAY_SIZE (mips16_percent_op);
14312     }
14313   else
14314     {
14315       percent_op = mips_percent_op;
14316       limit = ARRAY_SIZE (mips_percent_op);
14317     }
14318
14319   for (i = 0; i < limit; i++)
14320     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14321       {
14322         int len = strlen (percent_op[i].str);
14323
14324         if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14325           continue;
14326
14327         *str += strlen (percent_op[i].str);
14328         *reloc = percent_op[i].reloc;
14329
14330         /* Check whether the output BFD supports this relocation.
14331            If not, issue an error and fall back on something safe.  */
14332         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14333           {
14334             as_bad (_("relocation %s isn't supported by the current ABI"),
14335                     percent_op[i].str);
14336             *reloc = BFD_RELOC_UNUSED;
14337           }
14338         return TRUE;
14339       }
14340   return FALSE;
14341 }
14342
14343
14344 /* Parse string STR as a 16-bit relocatable operand.  Store the
14345    expression in *EP and the relocations in the array starting
14346    at RELOC.  Return the number of relocation operators used.
14347
14348    On exit, EXPR_END points to the first character after the expression.  */
14349
14350 static size_t
14351 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14352                        char *str)
14353 {
14354   bfd_reloc_code_real_type reversed_reloc[3];
14355   size_t reloc_index, i;
14356   int crux_depth, str_depth;
14357   char *crux;
14358
14359   /* Search for the start of the main expression, recoding relocations
14360      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14361      of the main expression and with CRUX_DEPTH containing the number
14362      of open brackets at that point.  */
14363   reloc_index = -1;
14364   str_depth = 0;
14365   do
14366     {
14367       reloc_index++;
14368       crux = str;
14369       crux_depth = str_depth;
14370
14371       /* Skip over whitespace and brackets, keeping count of the number
14372          of brackets.  */
14373       while (*str == ' ' || *str == '\t' || *str == '(')
14374         if (*str++ == '(')
14375           str_depth++;
14376     }
14377   while (*str == '%'
14378          && reloc_index < (HAVE_NEWABI ? 3 : 1)
14379          && parse_relocation (&str, &reversed_reloc[reloc_index]));
14380
14381   my_getExpression (ep, crux);
14382   str = expr_end;
14383
14384   /* Match every open bracket.  */
14385   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14386     if (*str++ == ')')
14387       crux_depth--;
14388
14389   if (crux_depth > 0)
14390     as_bad (_("unclosed '('"));
14391
14392   expr_end = str;
14393
14394   if (reloc_index != 0)
14395     {
14396       prev_reloc_op_frag = frag_now;
14397       for (i = 0; i < reloc_index; i++)
14398         reloc[i] = reversed_reloc[reloc_index - 1 - i];
14399     }
14400
14401   return reloc_index;
14402 }
14403
14404 static void
14405 my_getExpression (expressionS *ep, char *str)
14406 {
14407   char *save_in;
14408
14409   save_in = input_line_pointer;
14410   input_line_pointer = str;
14411   expression (ep);
14412   expr_end = input_line_pointer;
14413   input_line_pointer = save_in;
14414 }
14415
14416 char *
14417 md_atof (int type, char *litP, int *sizeP)
14418 {
14419   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14420 }
14421
14422 void
14423 md_number_to_chars (char *buf, valueT val, int n)
14424 {
14425   if (target_big_endian)
14426     number_to_chars_bigendian (buf, val, n);
14427   else
14428     number_to_chars_littleendian (buf, val, n);
14429 }
14430 \f
14431 #ifdef OBJ_ELF
14432 static int support_64bit_objects(void)
14433 {
14434   const char **list, **l;
14435   int yes;
14436
14437   list = bfd_target_list ();
14438   for (l = list; *l != NULL; l++)
14439     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14440         || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14441       break;
14442   yes = (*l != NULL);
14443   free (list);
14444   return yes;
14445 }
14446 #endif /* OBJ_ELF */
14447
14448 const char *md_shortopts = "O::g::G:";
14449
14450 enum options
14451   {
14452     OPTION_MARCH = OPTION_MD_BASE,
14453     OPTION_MTUNE,
14454     OPTION_MIPS1,
14455     OPTION_MIPS2,
14456     OPTION_MIPS3,
14457     OPTION_MIPS4,
14458     OPTION_MIPS5,
14459     OPTION_MIPS32,
14460     OPTION_MIPS64,
14461     OPTION_MIPS32R2,
14462     OPTION_MIPS64R2,
14463     OPTION_MIPS16,
14464     OPTION_NO_MIPS16,
14465     OPTION_MIPS3D,
14466     OPTION_NO_MIPS3D,
14467     OPTION_MDMX,
14468     OPTION_NO_MDMX,
14469     OPTION_DSP,
14470     OPTION_NO_DSP,
14471     OPTION_MT,
14472     OPTION_NO_MT,
14473     OPTION_SMARTMIPS,
14474     OPTION_NO_SMARTMIPS,
14475     OPTION_DSPR2,
14476     OPTION_NO_DSPR2,
14477     OPTION_MICROMIPS,
14478     OPTION_NO_MICROMIPS,
14479     OPTION_MCU,
14480     OPTION_NO_MCU,
14481     OPTION_COMPAT_ARCH_BASE,
14482     OPTION_M4650,
14483     OPTION_NO_M4650,
14484     OPTION_M4010,
14485     OPTION_NO_M4010,
14486     OPTION_M4100,
14487     OPTION_NO_M4100,
14488     OPTION_M3900,
14489     OPTION_NO_M3900,
14490     OPTION_M7000_HILO_FIX,
14491     OPTION_MNO_7000_HILO_FIX, 
14492     OPTION_FIX_24K,
14493     OPTION_NO_FIX_24K,
14494     OPTION_FIX_LOONGSON2F_JUMP,
14495     OPTION_NO_FIX_LOONGSON2F_JUMP,
14496     OPTION_FIX_LOONGSON2F_NOP,
14497     OPTION_NO_FIX_LOONGSON2F_NOP,
14498     OPTION_FIX_VR4120,
14499     OPTION_NO_FIX_VR4120,
14500     OPTION_FIX_VR4130,
14501     OPTION_NO_FIX_VR4130,
14502     OPTION_FIX_CN63XXP1,
14503     OPTION_NO_FIX_CN63XXP1,
14504     OPTION_TRAP,
14505     OPTION_BREAK,
14506     OPTION_EB,
14507     OPTION_EL,
14508     OPTION_FP32,
14509     OPTION_GP32,
14510     OPTION_CONSTRUCT_FLOATS,
14511     OPTION_NO_CONSTRUCT_FLOATS,
14512     OPTION_FP64,
14513     OPTION_GP64,
14514     OPTION_RELAX_BRANCH,
14515     OPTION_NO_RELAX_BRANCH,
14516     OPTION_MSHARED,
14517     OPTION_MNO_SHARED,
14518     OPTION_MSYM32,
14519     OPTION_MNO_SYM32,
14520     OPTION_SOFT_FLOAT,
14521     OPTION_HARD_FLOAT,
14522     OPTION_SINGLE_FLOAT,
14523     OPTION_DOUBLE_FLOAT,
14524     OPTION_32,
14525 #ifdef OBJ_ELF
14526     OPTION_CALL_SHARED,
14527     OPTION_CALL_NONPIC,
14528     OPTION_NON_SHARED,
14529     OPTION_XGOT,
14530     OPTION_MABI,
14531     OPTION_N32,
14532     OPTION_64,
14533     OPTION_MDEBUG,
14534     OPTION_NO_MDEBUG,
14535     OPTION_PDR,
14536     OPTION_NO_PDR,
14537     OPTION_MVXWORKS_PIC,
14538 #endif /* OBJ_ELF */
14539     OPTION_END_OF_ENUM    
14540   };
14541   
14542 struct option md_longopts[] =
14543 {
14544   /* Options which specify architecture.  */
14545   {"march", required_argument, NULL, OPTION_MARCH},
14546   {"mtune", required_argument, NULL, OPTION_MTUNE},
14547   {"mips0", no_argument, NULL, OPTION_MIPS1},
14548   {"mips1", no_argument, NULL, OPTION_MIPS1},
14549   {"mips2", no_argument, NULL, OPTION_MIPS2},
14550   {"mips3", no_argument, NULL, OPTION_MIPS3},
14551   {"mips4", no_argument, NULL, OPTION_MIPS4},
14552   {"mips5", no_argument, NULL, OPTION_MIPS5},
14553   {"mips32", no_argument, NULL, OPTION_MIPS32},
14554   {"mips64", no_argument, NULL, OPTION_MIPS64},
14555   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
14556   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
14557
14558   /* Options which specify Application Specific Extensions (ASEs).  */
14559   {"mips16", no_argument, NULL, OPTION_MIPS16},
14560   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
14561   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
14562   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
14563   {"mdmx", no_argument, NULL, OPTION_MDMX},
14564   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
14565   {"mdsp", no_argument, NULL, OPTION_DSP},
14566   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
14567   {"mmt", no_argument, NULL, OPTION_MT},
14568   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
14569   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
14570   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
14571   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
14572   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
14573   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14574   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
14575   {"mmcu", no_argument, NULL, OPTION_MCU},
14576   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
14577
14578   /* Old-style architecture options.  Don't add more of these.  */
14579   {"m4650", no_argument, NULL, OPTION_M4650},
14580   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
14581   {"m4010", no_argument, NULL, OPTION_M4010},
14582   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
14583   {"m4100", no_argument, NULL, OPTION_M4100},
14584   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
14585   {"m3900", no_argument, NULL, OPTION_M3900},
14586   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14587
14588   /* Options which enable bug fixes.  */
14589   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
14590   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14591   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14592   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14593   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14594   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14595   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
14596   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
14597   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
14598   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
14599   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
14600   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
14601   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
14602   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14603   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
14604
14605   /* Miscellaneous options.  */
14606   {"trap", no_argument, NULL, OPTION_TRAP},
14607   {"no-break", no_argument, NULL, OPTION_TRAP},
14608   {"break", no_argument, NULL, OPTION_BREAK},
14609   {"no-trap", no_argument, NULL, OPTION_BREAK},
14610   {"EB", no_argument, NULL, OPTION_EB},
14611   {"EL", no_argument, NULL, OPTION_EL},
14612   {"mfp32", no_argument, NULL, OPTION_FP32},
14613   {"mgp32", no_argument, NULL, OPTION_GP32},
14614   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
14615   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
14616   {"mfp64", no_argument, NULL, OPTION_FP64},
14617   {"mgp64", no_argument, NULL, OPTION_GP64},
14618   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14619   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
14620   {"mshared", no_argument, NULL, OPTION_MSHARED},
14621   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
14622   {"msym32", no_argument, NULL, OPTION_MSYM32},
14623   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
14624   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14625   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
14626   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14627   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
14628
14629   /* Strictly speaking this next option is ELF specific,
14630      but we allow it for other ports as well in order to
14631      make testing easier.  */
14632   {"32",          no_argument, NULL, OPTION_32},
14633   
14634   /* ELF-specific options.  */
14635 #ifdef OBJ_ELF
14636   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
14637   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
14638   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
14639   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
14640   {"xgot",        no_argument, NULL, OPTION_XGOT},
14641   {"mabi", required_argument, NULL, OPTION_MABI},
14642   {"n32",         no_argument, NULL, OPTION_N32},
14643   {"64",          no_argument, NULL, OPTION_64},
14644   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
14645   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
14646   {"mpdr", no_argument, NULL, OPTION_PDR},
14647   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
14648   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
14649 #endif /* OBJ_ELF */
14650
14651   {NULL, no_argument, NULL, 0}
14652 };
14653 size_t md_longopts_size = sizeof (md_longopts);
14654
14655 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14656    NEW_VALUE.  Warn if another value was already specified.  Note:
14657    we have to defer parsing the -march and -mtune arguments in order
14658    to handle 'from-abi' correctly, since the ABI might be specified
14659    in a later argument.  */
14660
14661 static void
14662 mips_set_option_string (const char **string_ptr, const char *new_value)
14663 {
14664   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14665     as_warn (_("A different %s was already specified, is now %s"),
14666              string_ptr == &mips_arch_string ? "-march" : "-mtune",
14667              new_value);
14668
14669   *string_ptr = new_value;
14670 }
14671
14672 int
14673 md_parse_option (int c, char *arg)
14674 {
14675   switch (c)
14676     {
14677     case OPTION_CONSTRUCT_FLOATS:
14678       mips_disable_float_construction = 0;
14679       break;
14680
14681     case OPTION_NO_CONSTRUCT_FLOATS:
14682       mips_disable_float_construction = 1;
14683       break;
14684
14685     case OPTION_TRAP:
14686       mips_trap = 1;
14687       break;
14688
14689     case OPTION_BREAK:
14690       mips_trap = 0;
14691       break;
14692
14693     case OPTION_EB:
14694       target_big_endian = 1;
14695       break;
14696
14697     case OPTION_EL:
14698       target_big_endian = 0;
14699       break;
14700
14701     case 'O':
14702       if (arg == NULL)
14703         mips_optimize = 1;
14704       else if (arg[0] == '0')
14705         mips_optimize = 0;
14706       else if (arg[0] == '1')
14707         mips_optimize = 1;
14708       else
14709         mips_optimize = 2;
14710       break;
14711
14712     case 'g':
14713       if (arg == NULL)
14714         mips_debug = 2;
14715       else
14716         mips_debug = atoi (arg);
14717       break;
14718
14719     case OPTION_MIPS1:
14720       file_mips_isa = ISA_MIPS1;
14721       break;
14722
14723     case OPTION_MIPS2:
14724       file_mips_isa = ISA_MIPS2;
14725       break;
14726
14727     case OPTION_MIPS3:
14728       file_mips_isa = ISA_MIPS3;
14729       break;
14730
14731     case OPTION_MIPS4:
14732       file_mips_isa = ISA_MIPS4;
14733       break;
14734
14735     case OPTION_MIPS5:
14736       file_mips_isa = ISA_MIPS5;
14737       break;
14738
14739     case OPTION_MIPS32:
14740       file_mips_isa = ISA_MIPS32;
14741       break;
14742
14743     case OPTION_MIPS32R2:
14744       file_mips_isa = ISA_MIPS32R2;
14745       break;
14746
14747     case OPTION_MIPS64R2:
14748       file_mips_isa = ISA_MIPS64R2;
14749       break;
14750
14751     case OPTION_MIPS64:
14752       file_mips_isa = ISA_MIPS64;
14753       break;
14754
14755     case OPTION_MTUNE:
14756       mips_set_option_string (&mips_tune_string, arg);
14757       break;
14758
14759     case OPTION_MARCH:
14760       mips_set_option_string (&mips_arch_string, arg);
14761       break;
14762
14763     case OPTION_M4650:
14764       mips_set_option_string (&mips_arch_string, "4650");
14765       mips_set_option_string (&mips_tune_string, "4650");
14766       break;
14767
14768     case OPTION_NO_M4650:
14769       break;
14770
14771     case OPTION_M4010:
14772       mips_set_option_string (&mips_arch_string, "4010");
14773       mips_set_option_string (&mips_tune_string, "4010");
14774       break;
14775
14776     case OPTION_NO_M4010:
14777       break;
14778
14779     case OPTION_M4100:
14780       mips_set_option_string (&mips_arch_string, "4100");
14781       mips_set_option_string (&mips_tune_string, "4100");
14782       break;
14783
14784     case OPTION_NO_M4100:
14785       break;
14786
14787     case OPTION_M3900:
14788       mips_set_option_string (&mips_arch_string, "3900");
14789       mips_set_option_string (&mips_tune_string, "3900");
14790       break;
14791
14792     case OPTION_NO_M3900:
14793       break;
14794
14795     case OPTION_MDMX:
14796       mips_opts.ase_mdmx = 1;
14797       break;
14798
14799     case OPTION_NO_MDMX:
14800       mips_opts.ase_mdmx = 0;
14801       break;
14802
14803     case OPTION_DSP:
14804       mips_opts.ase_dsp = 1;
14805       mips_opts.ase_dspr2 = 0;
14806       break;
14807
14808     case OPTION_NO_DSP:
14809       mips_opts.ase_dsp = 0;
14810       mips_opts.ase_dspr2 = 0;
14811       break;
14812
14813     case OPTION_DSPR2:
14814       mips_opts.ase_dspr2 = 1;
14815       mips_opts.ase_dsp = 1;
14816       break;
14817
14818     case OPTION_NO_DSPR2:
14819       mips_opts.ase_dspr2 = 0;
14820       mips_opts.ase_dsp = 0;
14821       break;
14822
14823     case OPTION_MT:
14824       mips_opts.ase_mt = 1;
14825       break;
14826
14827     case OPTION_NO_MT:
14828       mips_opts.ase_mt = 0;
14829       break;
14830
14831     case OPTION_MCU:
14832       mips_opts.ase_mcu = 1;
14833       break;
14834
14835     case OPTION_NO_MCU:
14836       mips_opts.ase_mcu = 0;
14837       break;
14838
14839     case OPTION_MICROMIPS:
14840       if (mips_opts.mips16 == 1)
14841         {
14842           as_bad (_("-mmicromips cannot be used with -mips16"));
14843           return 0;
14844         }
14845       mips_opts.micromips = 1;
14846       mips_no_prev_insn ();
14847       break;
14848
14849     case OPTION_NO_MICROMIPS:
14850       mips_opts.micromips = 0;
14851       mips_no_prev_insn ();
14852       break;
14853
14854     case OPTION_MIPS16:
14855       if (mips_opts.micromips == 1)
14856         {
14857           as_bad (_("-mips16 cannot be used with -micromips"));
14858           return 0;
14859         }
14860       mips_opts.mips16 = 1;
14861       mips_no_prev_insn ();
14862       break;
14863
14864     case OPTION_NO_MIPS16:
14865       mips_opts.mips16 = 0;
14866       mips_no_prev_insn ();
14867       break;
14868
14869     case OPTION_MIPS3D:
14870       mips_opts.ase_mips3d = 1;
14871       break;
14872
14873     case OPTION_NO_MIPS3D:
14874       mips_opts.ase_mips3d = 0;
14875       break;
14876
14877     case OPTION_SMARTMIPS:
14878       mips_opts.ase_smartmips = 1;
14879       break;
14880
14881     case OPTION_NO_SMARTMIPS:
14882       mips_opts.ase_smartmips = 0;
14883       break;
14884
14885     case OPTION_FIX_24K:
14886       mips_fix_24k = 1;
14887       break;
14888
14889     case OPTION_NO_FIX_24K:
14890       mips_fix_24k = 0;
14891       break;
14892
14893     case OPTION_FIX_LOONGSON2F_JUMP:
14894       mips_fix_loongson2f_jump = TRUE;
14895       break;
14896
14897     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14898       mips_fix_loongson2f_jump = FALSE;
14899       break;
14900
14901     case OPTION_FIX_LOONGSON2F_NOP:
14902       mips_fix_loongson2f_nop = TRUE;
14903       break;
14904
14905     case OPTION_NO_FIX_LOONGSON2F_NOP:
14906       mips_fix_loongson2f_nop = FALSE;
14907       break;
14908
14909     case OPTION_FIX_VR4120:
14910       mips_fix_vr4120 = 1;
14911       break;
14912
14913     case OPTION_NO_FIX_VR4120:
14914       mips_fix_vr4120 = 0;
14915       break;
14916
14917     case OPTION_FIX_VR4130:
14918       mips_fix_vr4130 = 1;
14919       break;
14920
14921     case OPTION_NO_FIX_VR4130:
14922       mips_fix_vr4130 = 0;
14923       break;
14924
14925     case OPTION_FIX_CN63XXP1:
14926       mips_fix_cn63xxp1 = TRUE;
14927       break;
14928
14929     case OPTION_NO_FIX_CN63XXP1:
14930       mips_fix_cn63xxp1 = FALSE;
14931       break;
14932
14933     case OPTION_RELAX_BRANCH:
14934       mips_relax_branch = 1;
14935       break;
14936
14937     case OPTION_NO_RELAX_BRANCH:
14938       mips_relax_branch = 0;
14939       break;
14940
14941     case OPTION_MSHARED:
14942       mips_in_shared = TRUE;
14943       break;
14944
14945     case OPTION_MNO_SHARED:
14946       mips_in_shared = FALSE;
14947       break;
14948
14949     case OPTION_MSYM32:
14950       mips_opts.sym32 = TRUE;
14951       break;
14952
14953     case OPTION_MNO_SYM32:
14954       mips_opts.sym32 = FALSE;
14955       break;
14956
14957 #ifdef OBJ_ELF
14958       /* When generating ELF code, we permit -KPIC and -call_shared to
14959          select SVR4_PIC, and -non_shared to select no PIC.  This is
14960          intended to be compatible with Irix 5.  */
14961     case OPTION_CALL_SHARED:
14962       if (!IS_ELF)
14963         {
14964           as_bad (_("-call_shared is supported only for ELF format"));
14965           return 0;
14966         }
14967       mips_pic = SVR4_PIC;
14968       mips_abicalls = TRUE;
14969       break;
14970
14971     case OPTION_CALL_NONPIC:
14972       if (!IS_ELF)
14973         {
14974           as_bad (_("-call_nonpic is supported only for ELF format"));
14975           return 0;
14976         }
14977       mips_pic = NO_PIC;
14978       mips_abicalls = TRUE;
14979       break;
14980
14981     case OPTION_NON_SHARED:
14982       if (!IS_ELF)
14983         {
14984           as_bad (_("-non_shared is supported only for ELF format"));
14985           return 0;
14986         }
14987       mips_pic = NO_PIC;
14988       mips_abicalls = FALSE;
14989       break;
14990
14991       /* The -xgot option tells the assembler to use 32 bit offsets
14992          when accessing the got in SVR4_PIC mode.  It is for Irix
14993          compatibility.  */
14994     case OPTION_XGOT:
14995       mips_big_got = 1;
14996       break;
14997 #endif /* OBJ_ELF */
14998
14999     case 'G':
15000       g_switch_value = atoi (arg);
15001       g_switch_seen = 1;
15002       break;
15003
15004       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15005          and -mabi=64.  */
15006     case OPTION_32:
15007       if (IS_ELF)
15008         mips_abi = O32_ABI;
15009       /* We silently ignore -32 for non-ELF targets.  This greatly
15010          simplifies the construction of the MIPS GAS test cases.  */
15011       break;
15012
15013 #ifdef OBJ_ELF
15014     case OPTION_N32:
15015       if (!IS_ELF)
15016         {
15017           as_bad (_("-n32 is supported for ELF format only"));
15018           return 0;
15019         }
15020       mips_abi = N32_ABI;
15021       break;
15022
15023     case OPTION_64:
15024       if (!IS_ELF)
15025         {
15026           as_bad (_("-64 is supported for ELF format only"));
15027           return 0;
15028         }
15029       mips_abi = N64_ABI;
15030       if (!support_64bit_objects())
15031         as_fatal (_("No compiled in support for 64 bit object file format"));
15032       break;
15033 #endif /* OBJ_ELF */
15034
15035     case OPTION_GP32:
15036       file_mips_gp32 = 1;
15037       break;
15038
15039     case OPTION_GP64:
15040       file_mips_gp32 = 0;
15041       break;
15042
15043     case OPTION_FP32:
15044       file_mips_fp32 = 1;
15045       break;
15046
15047     case OPTION_FP64:
15048       file_mips_fp32 = 0;
15049       break;
15050
15051     case OPTION_SINGLE_FLOAT:
15052       file_mips_single_float = 1;
15053       break;
15054
15055     case OPTION_DOUBLE_FLOAT:
15056       file_mips_single_float = 0;
15057       break;
15058
15059     case OPTION_SOFT_FLOAT:
15060       file_mips_soft_float = 1;
15061       break;
15062
15063     case OPTION_HARD_FLOAT:
15064       file_mips_soft_float = 0;
15065       break;
15066
15067 #ifdef OBJ_ELF
15068     case OPTION_MABI:
15069       if (!IS_ELF)
15070         {
15071           as_bad (_("-mabi is supported for ELF format only"));
15072           return 0;
15073         }
15074       if (strcmp (arg, "32") == 0)
15075         mips_abi = O32_ABI;
15076       else if (strcmp (arg, "o64") == 0)
15077         mips_abi = O64_ABI;
15078       else if (strcmp (arg, "n32") == 0)
15079         mips_abi = N32_ABI;
15080       else if (strcmp (arg, "64") == 0)
15081         {
15082           mips_abi = N64_ABI;
15083           if (! support_64bit_objects())
15084             as_fatal (_("No compiled in support for 64 bit object file "
15085                         "format"));
15086         }
15087       else if (strcmp (arg, "eabi") == 0)
15088         mips_abi = EABI_ABI;
15089       else
15090         {
15091           as_fatal (_("invalid abi -mabi=%s"), arg);
15092           return 0;
15093         }
15094       break;
15095 #endif /* OBJ_ELF */
15096
15097     case OPTION_M7000_HILO_FIX:
15098       mips_7000_hilo_fix = TRUE;
15099       break;
15100
15101     case OPTION_MNO_7000_HILO_FIX:
15102       mips_7000_hilo_fix = FALSE;
15103       break;
15104
15105 #ifdef OBJ_ELF
15106     case OPTION_MDEBUG:
15107       mips_flag_mdebug = TRUE;
15108       break;
15109
15110     case OPTION_NO_MDEBUG:
15111       mips_flag_mdebug = FALSE;
15112       break;
15113
15114     case OPTION_PDR:
15115       mips_flag_pdr = TRUE;
15116       break;
15117
15118     case OPTION_NO_PDR:
15119       mips_flag_pdr = FALSE;
15120       break;
15121
15122     case OPTION_MVXWORKS_PIC:
15123       mips_pic = VXWORKS_PIC;
15124       break;
15125 #endif /* OBJ_ELF */
15126
15127     default:
15128       return 0;
15129     }
15130
15131     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15132
15133   return 1;
15134 }
15135 \f
15136 /* Set up globals to generate code for the ISA or processor
15137    described by INFO.  */
15138
15139 static void
15140 mips_set_architecture (const struct mips_cpu_info *info)
15141 {
15142   if (info != 0)
15143     {
15144       file_mips_arch = info->cpu;
15145       mips_opts.arch = info->cpu;
15146       mips_opts.isa = info->isa;
15147     }
15148 }
15149
15150
15151 /* Likewise for tuning.  */
15152
15153 static void
15154 mips_set_tune (const struct mips_cpu_info *info)
15155 {
15156   if (info != 0)
15157     mips_tune = info->cpu;
15158 }
15159
15160
15161 void
15162 mips_after_parse_args (void)
15163 {
15164   const struct mips_cpu_info *arch_info = 0;
15165   const struct mips_cpu_info *tune_info = 0;
15166
15167   /* GP relative stuff not working for PE */
15168   if (strncmp (TARGET_OS, "pe", 2) == 0)
15169     {
15170       if (g_switch_seen && g_switch_value != 0)
15171         as_bad (_("-G not supported in this configuration."));
15172       g_switch_value = 0;
15173     }
15174
15175   if (mips_abi == NO_ABI)
15176     mips_abi = MIPS_DEFAULT_ABI;
15177
15178   /* The following code determines the architecture and register size.
15179      Similar code was added to GCC 3.3 (see override_options() in
15180      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15181      as much as possible.  */
15182
15183   if (mips_arch_string != 0)
15184     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15185
15186   if (file_mips_isa != ISA_UNKNOWN)
15187     {
15188       /* Handle -mipsN.  At this point, file_mips_isa contains the
15189          ISA level specified by -mipsN, while arch_info->isa contains
15190          the -march selection (if any).  */
15191       if (arch_info != 0)
15192         {
15193           /* -march takes precedence over -mipsN, since it is more descriptive.
15194              There's no harm in specifying both as long as the ISA levels
15195              are the same.  */
15196           if (file_mips_isa != arch_info->isa)
15197             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15198                     mips_cpu_info_from_isa (file_mips_isa)->name,
15199                     mips_cpu_info_from_isa (arch_info->isa)->name);
15200         }
15201       else
15202         arch_info = mips_cpu_info_from_isa (file_mips_isa);
15203     }
15204
15205   if (arch_info == 0)
15206     {
15207       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15208       gas_assert (arch_info);
15209     }
15210
15211   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15212     as_bad (_("-march=%s is not compatible with the selected ABI"),
15213             arch_info->name);
15214
15215   mips_set_architecture (arch_info);
15216
15217   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
15218   if (mips_tune_string != 0)
15219     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15220
15221   if (tune_info == 0)
15222     mips_set_tune (arch_info);
15223   else
15224     mips_set_tune (tune_info);
15225
15226   if (file_mips_gp32 >= 0)
15227     {
15228       /* The user specified the size of the integer registers.  Make sure
15229          it agrees with the ABI and ISA.  */
15230       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15231         as_bad (_("-mgp64 used with a 32-bit processor"));
15232       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15233         as_bad (_("-mgp32 used with a 64-bit ABI"));
15234       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15235         as_bad (_("-mgp64 used with a 32-bit ABI"));
15236     }
15237   else
15238     {
15239       /* Infer the integer register size from the ABI and processor.
15240          Restrict ourselves to 32-bit registers if that's all the
15241          processor has, or if the ABI cannot handle 64-bit registers.  */
15242       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15243                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
15244     }
15245
15246   switch (file_mips_fp32)
15247     {
15248     default:
15249     case -1:
15250       /* No user specified float register size.
15251          ??? GAS treats single-float processors as though they had 64-bit
15252          float registers (although it complains when double-precision
15253          instructions are used).  As things stand, saying they have 32-bit
15254          registers would lead to spurious "register must be even" messages.
15255          So here we assume float registers are never smaller than the
15256          integer ones.  */
15257       if (file_mips_gp32 == 0)
15258         /* 64-bit integer registers implies 64-bit float registers.  */
15259         file_mips_fp32 = 0;
15260       else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15261                && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15262         /* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
15263         file_mips_fp32 = 0;
15264       else
15265         /* 32-bit float registers.  */
15266         file_mips_fp32 = 1;
15267       break;
15268
15269     /* The user specified the size of the float registers.  Check if it
15270        agrees with the ABI and ISA.  */
15271     case 0:
15272       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15273         as_bad (_("-mfp64 used with a 32-bit fpu"));
15274       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15275                && !ISA_HAS_MXHC1 (mips_opts.isa))
15276         as_warn (_("-mfp64 used with a 32-bit ABI"));
15277       break;
15278     case 1:
15279       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15280         as_warn (_("-mfp32 used with a 64-bit ABI"));
15281       break;
15282     }
15283
15284   /* End of GCC-shared inference code.  */
15285
15286   /* This flag is set when we have a 64-bit capable CPU but use only
15287      32-bit wide registers.  Note that EABI does not use it.  */
15288   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15289       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15290           || mips_abi == O32_ABI))
15291     mips_32bitmode = 1;
15292
15293   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15294     as_bad (_("trap exception not supported at ISA 1"));
15295
15296   /* If the selected architecture includes support for ASEs, enable
15297      generation of code for them.  */
15298   if (mips_opts.mips16 == -1)
15299     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15300   if (mips_opts.micromips == -1)
15301     mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15302   if (mips_opts.ase_mips3d == -1)
15303     mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
15304                             && file_mips_fp32 == 0) ? 1 : 0;
15305   if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15306     as_bad (_("-mfp32 used with -mips3d"));
15307
15308   if (mips_opts.ase_mdmx == -1)
15309     mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
15310                           && file_mips_fp32 == 0) ? 1 : 0;
15311   if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15312     as_bad (_("-mfp32 used with -mdmx"));
15313
15314   if (mips_opts.ase_smartmips == -1)
15315     mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15316   if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
15317     as_warn (_("%s ISA does not support SmartMIPS"), 
15318              mips_cpu_info_from_isa (mips_opts.isa)->name);
15319
15320   if (mips_opts.ase_dsp == -1)
15321     mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15322   if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
15323     as_warn (_("%s ISA does not support DSP ASE"), 
15324              mips_cpu_info_from_isa (mips_opts.isa)->name);
15325
15326   if (mips_opts.ase_dspr2 == -1)
15327     {
15328       mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15329       mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15330     }
15331   if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
15332     as_warn (_("%s ISA does not support DSP R2 ASE"),
15333              mips_cpu_info_from_isa (mips_opts.isa)->name);
15334
15335   if (mips_opts.ase_mt == -1)
15336     mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15337   if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
15338     as_warn (_("%s ISA does not support MT ASE"),
15339              mips_cpu_info_from_isa (mips_opts.isa)->name);
15340
15341   if (mips_opts.ase_mcu == -1)
15342     mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15343   if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15344       as_warn (_("%s ISA does not support MCU ASE"),
15345                mips_cpu_info_from_isa (mips_opts.isa)->name);
15346
15347   file_mips_isa = mips_opts.isa;
15348   file_ase_mips3d = mips_opts.ase_mips3d;
15349   file_ase_mdmx = mips_opts.ase_mdmx;
15350   file_ase_smartmips = mips_opts.ase_smartmips;
15351   file_ase_dsp = mips_opts.ase_dsp;
15352   file_ase_dspr2 = mips_opts.ase_dspr2;
15353   file_ase_mt = mips_opts.ase_mt;
15354   mips_opts.gp32 = file_mips_gp32;
15355   mips_opts.fp32 = file_mips_fp32;
15356   mips_opts.soft_float = file_mips_soft_float;
15357   mips_opts.single_float = file_mips_single_float;
15358
15359   if (mips_flag_mdebug < 0)
15360     {
15361 #ifdef OBJ_MAYBE_ECOFF
15362       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15363         mips_flag_mdebug = 1;
15364       else
15365 #endif /* OBJ_MAYBE_ECOFF */
15366         mips_flag_mdebug = 0;
15367     }
15368 }
15369 \f
15370 void
15371 mips_init_after_args (void)
15372 {
15373   /* initialize opcodes */
15374   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15375   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15376 }
15377
15378 long
15379 md_pcrel_from (fixS *fixP)
15380 {
15381   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15382   switch (fixP->fx_r_type)
15383     {
15384     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15385     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15386       /* Return the address of the delay slot.  */
15387       return addr + 2;
15388
15389     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15390     case BFD_RELOC_MICROMIPS_JMP:
15391     case BFD_RELOC_16_PCREL_S2:
15392     case BFD_RELOC_MIPS_JMP:
15393       /* Return the address of the delay slot.  */
15394       return addr + 4;
15395
15396     default:
15397       /* We have no relocation type for PC relative MIPS16 instructions.  */
15398       if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15399         as_bad_where (fixP->fx_file, fixP->fx_line,
15400                       _("PC relative MIPS16 instruction references a different section"));
15401       return addr;
15402     }
15403 }
15404
15405 /* This is called before the symbol table is processed.  In order to
15406    work with gcc when using mips-tfile, we must keep all local labels.
15407    However, in other cases, we want to discard them.  If we were
15408    called with -g, but we didn't see any debugging information, it may
15409    mean that gcc is smuggling debugging information through to
15410    mips-tfile, in which case we must generate all local labels.  */
15411
15412 void
15413 mips_frob_file_before_adjust (void)
15414 {
15415 #ifndef NO_ECOFF_DEBUGGING
15416   if (ECOFF_DEBUGGING
15417       && mips_debug != 0
15418       && ! ecoff_debugging_seen)
15419     flag_keep_locals = 1;
15420 #endif
15421 }
15422
15423 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15424    the corresponding LO16 reloc.  This is called before md_apply_fix and
15425    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15426    relocation operators.
15427
15428    For our purposes, a %lo() expression matches a %got() or %hi()
15429    expression if:
15430
15431       (a) it refers to the same symbol; and
15432       (b) the offset applied in the %lo() expression is no lower than
15433           the offset applied in the %got() or %hi().
15434
15435    (b) allows us to cope with code like:
15436
15437         lui     $4,%hi(foo)
15438         lh      $4,%lo(foo+2)($4)
15439
15440    ...which is legal on RELA targets, and has a well-defined behaviour
15441    if the user knows that adding 2 to "foo" will not induce a carry to
15442    the high 16 bits.
15443
15444    When several %lo()s match a particular %got() or %hi(), we use the
15445    following rules to distinguish them:
15446
15447      (1) %lo()s with smaller offsets are a better match than %lo()s with
15448          higher offsets.
15449
15450      (2) %lo()s with no matching %got() or %hi() are better than those
15451          that already have a matching %got() or %hi().
15452
15453      (3) later %lo()s are better than earlier %lo()s.
15454
15455    These rules are applied in order.
15456
15457    (1) means, among other things, that %lo()s with identical offsets are
15458    chosen if they exist.
15459
15460    (2) means that we won't associate several high-part relocations with
15461    the same low-part relocation unless there's no alternative.  Having
15462    several high parts for the same low part is a GNU extension; this rule
15463    allows careful users to avoid it.
15464
15465    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15466    with the last high-part relocation being at the front of the list.
15467    It therefore makes sense to choose the last matching low-part
15468    relocation, all other things being equal.  It's also easier
15469    to code that way.  */
15470
15471 void
15472 mips_frob_file (void)
15473 {
15474   struct mips_hi_fixup *l;
15475   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15476
15477   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15478     {
15479       segment_info_type *seginfo;
15480       bfd_boolean matched_lo_p;
15481       fixS **hi_pos, **lo_pos, **pos;
15482
15483       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15484
15485       /* If a GOT16 relocation turns out to be against a global symbol,
15486          there isn't supposed to be a matching LO.  Ignore %gots against
15487          constants; we'll report an error for those later.  */
15488       if (got16_reloc_p (l->fixp->fx_r_type)
15489           && !(l->fixp->fx_addsy
15490                && pic_need_relax (l->fixp->fx_addsy, l->seg)))
15491         continue;
15492
15493       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15494       if (fixup_has_matching_lo_p (l->fixp))
15495         continue;
15496
15497       seginfo = seg_info (l->seg);
15498
15499       /* Set HI_POS to the position of this relocation in the chain.
15500          Set LO_POS to the position of the chosen low-part relocation.
15501          MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15502          relocation that matches an immediately-preceding high-part
15503          relocation.  */
15504       hi_pos = NULL;
15505       lo_pos = NULL;
15506       matched_lo_p = FALSE;
15507       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15508
15509       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15510         {
15511           if (*pos == l->fixp)
15512             hi_pos = pos;
15513
15514           if ((*pos)->fx_r_type == looking_for_rtype
15515               && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15516               && (*pos)->fx_offset >= l->fixp->fx_offset
15517               && (lo_pos == NULL
15518                   || (*pos)->fx_offset < (*lo_pos)->fx_offset
15519                   || (!matched_lo_p
15520                       && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15521             lo_pos = pos;
15522
15523           matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15524                           && fixup_has_matching_lo_p (*pos));
15525         }
15526
15527       /* If we found a match, remove the high-part relocation from its
15528          current position and insert it before the low-part relocation.
15529          Make the offsets match so that fixup_has_matching_lo_p()
15530          will return true.
15531
15532          We don't warn about unmatched high-part relocations since some
15533          versions of gcc have been known to emit dead "lui ...%hi(...)"
15534          instructions.  */
15535       if (lo_pos != NULL)
15536         {
15537           l->fixp->fx_offset = (*lo_pos)->fx_offset;
15538           if (l->fixp->fx_next != *lo_pos)
15539             {
15540               *hi_pos = l->fixp->fx_next;
15541               l->fixp->fx_next = *lo_pos;
15542               *lo_pos = l->fixp;
15543             }
15544         }
15545     }
15546 }
15547
15548 int
15549 mips_force_relocation (fixS *fixp)
15550 {
15551   if (generic_force_reloc (fixp))
15552     return 1;
15553
15554   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15555      so that the linker relaxation can update targets.  */
15556   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15557       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15558       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15559     return 1;
15560
15561   return 0;
15562 }
15563
15564 /* Read the instruction associated with RELOC from BUF.  */
15565
15566 static unsigned int
15567 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15568 {
15569   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15570     return read_compressed_insn (buf, 4);
15571   else
15572     return read_insn (buf);
15573 }
15574
15575 /* Write instruction INSN to BUF, given that it has been relocated
15576    by RELOC.  */
15577
15578 static void
15579 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15580                   unsigned long insn)
15581 {
15582   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15583     write_compressed_insn (buf, insn, 4);
15584   else
15585     write_insn (buf, insn);
15586 }
15587
15588 /* Apply a fixup to the object file.  */
15589
15590 void
15591 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15592 {
15593   char *buf;
15594   unsigned long insn;
15595   reloc_howto_type *howto;
15596
15597   /* We ignore generic BFD relocations we don't know about.  */
15598   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15599   if (! howto)
15600     return;
15601
15602   gas_assert (fixP->fx_size == 2
15603               || fixP->fx_size == 4
15604               || fixP->fx_r_type == BFD_RELOC_16
15605               || fixP->fx_r_type == BFD_RELOC_64
15606               || fixP->fx_r_type == BFD_RELOC_CTOR
15607               || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15608               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15609               || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15610               || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15611               || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15612
15613   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15614
15615   gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15616               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15617               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15618               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
15619
15620   /* Don't treat parts of a composite relocation as done.  There are two
15621      reasons for this:
15622
15623      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15624          should nevertheless be emitted if the first part is.
15625
15626      (2) In normal usage, composite relocations are never assembly-time
15627          constants.  The easiest way of dealing with the pathological
15628          exceptions is to generate a relocation against STN_UNDEF and
15629          leave everything up to the linker.  */
15630   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15631     fixP->fx_done = 1;
15632
15633   switch (fixP->fx_r_type)
15634     {
15635     case BFD_RELOC_MIPS_TLS_GD:
15636     case BFD_RELOC_MIPS_TLS_LDM:
15637     case BFD_RELOC_MIPS_TLS_DTPREL32:
15638     case BFD_RELOC_MIPS_TLS_DTPREL64:
15639     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15640     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15641     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15642     case BFD_RELOC_MIPS_TLS_TPREL32:
15643     case BFD_RELOC_MIPS_TLS_TPREL64:
15644     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15645     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15646     case BFD_RELOC_MICROMIPS_TLS_GD:
15647     case BFD_RELOC_MICROMIPS_TLS_LDM:
15648     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15649     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15650     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15651     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15652     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15653     case BFD_RELOC_MIPS16_TLS_GD:
15654     case BFD_RELOC_MIPS16_TLS_LDM:
15655     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15656     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15657     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15658     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15659     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15660       if (!fixP->fx_addsy)
15661         {
15662           as_bad_where (fixP->fx_file, fixP->fx_line,
15663                         _("TLS relocation against a constant"));
15664           break;
15665         }
15666       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15667       /* fall through */
15668
15669     case BFD_RELOC_MIPS_JMP:
15670     case BFD_RELOC_MIPS_SHIFT5:
15671     case BFD_RELOC_MIPS_SHIFT6:
15672     case BFD_RELOC_MIPS_GOT_DISP:
15673     case BFD_RELOC_MIPS_GOT_PAGE:
15674     case BFD_RELOC_MIPS_GOT_OFST:
15675     case BFD_RELOC_MIPS_SUB:
15676     case BFD_RELOC_MIPS_INSERT_A:
15677     case BFD_RELOC_MIPS_INSERT_B:
15678     case BFD_RELOC_MIPS_DELETE:
15679     case BFD_RELOC_MIPS_HIGHEST:
15680     case BFD_RELOC_MIPS_HIGHER:
15681     case BFD_RELOC_MIPS_SCN_DISP:
15682     case BFD_RELOC_MIPS_REL16:
15683     case BFD_RELOC_MIPS_RELGOT:
15684     case BFD_RELOC_MIPS_JALR:
15685     case BFD_RELOC_HI16:
15686     case BFD_RELOC_HI16_S:
15687     case BFD_RELOC_LO16:
15688     case BFD_RELOC_GPREL16:
15689     case BFD_RELOC_MIPS_LITERAL:
15690     case BFD_RELOC_MIPS_CALL16:
15691     case BFD_RELOC_MIPS_GOT16:
15692     case BFD_RELOC_GPREL32:
15693     case BFD_RELOC_MIPS_GOT_HI16:
15694     case BFD_RELOC_MIPS_GOT_LO16:
15695     case BFD_RELOC_MIPS_CALL_HI16:
15696     case BFD_RELOC_MIPS_CALL_LO16:
15697     case BFD_RELOC_MIPS16_GPREL:
15698     case BFD_RELOC_MIPS16_GOT16:
15699     case BFD_RELOC_MIPS16_CALL16:
15700     case BFD_RELOC_MIPS16_HI16:
15701     case BFD_RELOC_MIPS16_HI16_S:
15702     case BFD_RELOC_MIPS16_LO16:
15703     case BFD_RELOC_MIPS16_JMP:
15704     case BFD_RELOC_MICROMIPS_JMP:
15705     case BFD_RELOC_MICROMIPS_GOT_DISP:
15706     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15707     case BFD_RELOC_MICROMIPS_GOT_OFST:
15708     case BFD_RELOC_MICROMIPS_SUB:
15709     case BFD_RELOC_MICROMIPS_HIGHEST:
15710     case BFD_RELOC_MICROMIPS_HIGHER:
15711     case BFD_RELOC_MICROMIPS_SCN_DISP:
15712     case BFD_RELOC_MICROMIPS_JALR:
15713     case BFD_RELOC_MICROMIPS_HI16:
15714     case BFD_RELOC_MICROMIPS_HI16_S:
15715     case BFD_RELOC_MICROMIPS_LO16:
15716     case BFD_RELOC_MICROMIPS_GPREL16:
15717     case BFD_RELOC_MICROMIPS_LITERAL:
15718     case BFD_RELOC_MICROMIPS_CALL16:
15719     case BFD_RELOC_MICROMIPS_GOT16:
15720     case BFD_RELOC_MICROMIPS_GOT_HI16:
15721     case BFD_RELOC_MICROMIPS_GOT_LO16:
15722     case BFD_RELOC_MICROMIPS_CALL_HI16:
15723     case BFD_RELOC_MICROMIPS_CALL_LO16:
15724       if (fixP->fx_done)
15725         {
15726           offsetT value;
15727
15728           if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15729             {
15730               insn = read_reloc_insn (buf, fixP->fx_r_type);
15731               if (mips16_reloc_p (fixP->fx_r_type))
15732                 insn |= mips16_immed_extend (value, 16);
15733               else
15734                 insn |= (value & 0xffff);
15735               write_reloc_insn (buf, fixP->fx_r_type, insn);
15736             }
15737           else
15738             as_bad_where (fixP->fx_file, fixP->fx_line,
15739                           _("Unsupported constant in relocation"));
15740         }
15741       break;
15742
15743     case BFD_RELOC_64:
15744       /* This is handled like BFD_RELOC_32, but we output a sign
15745          extended value if we are only 32 bits.  */
15746       if (fixP->fx_done)
15747         {
15748           if (8 <= sizeof (valueT))
15749             md_number_to_chars (buf, *valP, 8);
15750           else
15751             {
15752               valueT hiv;
15753
15754               if ((*valP & 0x80000000) != 0)
15755                 hiv = 0xffffffff;
15756               else
15757                 hiv = 0;
15758               md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15759               md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15760             }
15761         }
15762       break;
15763
15764     case BFD_RELOC_RVA:
15765     case BFD_RELOC_32:
15766     case BFD_RELOC_16:
15767       /* If we are deleting this reloc entry, we must fill in the
15768          value now.  This can happen if we have a .word which is not
15769          resolved when it appears but is later defined.  */
15770       if (fixP->fx_done)
15771         md_number_to_chars (buf, *valP, fixP->fx_size);
15772       break;
15773
15774     case BFD_RELOC_16_PCREL_S2:
15775       if ((*valP & 0x3) != 0)
15776         as_bad_where (fixP->fx_file, fixP->fx_line,
15777                       _("Branch to misaligned address (%lx)"), (long) *valP);
15778
15779       /* We need to save the bits in the instruction since fixup_segment()
15780          might be deleting the relocation entry (i.e., a branch within
15781          the current segment).  */
15782       if (! fixP->fx_done)
15783         break;
15784
15785       /* Update old instruction data.  */
15786       insn = read_insn (buf);
15787
15788       if (*valP + 0x20000 <= 0x3ffff)
15789         {
15790           insn |= (*valP >> 2) & 0xffff;
15791           write_insn (buf, insn);
15792         }
15793       else if (mips_pic == NO_PIC
15794                && fixP->fx_done
15795                && fixP->fx_frag->fr_address >= text_section->vma
15796                && (fixP->fx_frag->fr_address
15797                    < text_section->vma + bfd_get_section_size (text_section))
15798                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
15799                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
15800                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15801         {
15802           /* The branch offset is too large.  If this is an
15803              unconditional branch, and we are not generating PIC code,
15804              we can convert it to an absolute jump instruction.  */
15805           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
15806             insn = 0x0c000000;  /* jal */
15807           else
15808             insn = 0x08000000;  /* j */
15809           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15810           fixP->fx_done = 0;
15811           fixP->fx_addsy = section_symbol (text_section);
15812           *valP += md_pcrel_from (fixP);
15813           write_insn (buf, insn);
15814         }
15815       else
15816         {
15817           /* If we got here, we have branch-relaxation disabled,
15818              and there's nothing we can do to fix this instruction
15819              without turning it into a longer sequence.  */
15820           as_bad_where (fixP->fx_file, fixP->fx_line,
15821                         _("Branch out of range"));
15822         }
15823       break;
15824
15825     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15826     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15827     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15828       /* We adjust the offset back to even.  */
15829       if ((*valP & 0x1) != 0)
15830         --(*valP);
15831
15832       if (! fixP->fx_done)
15833         break;
15834
15835       /* Should never visit here, because we keep the relocation.  */
15836       abort ();
15837       break;
15838
15839     case BFD_RELOC_VTABLE_INHERIT:
15840       fixP->fx_done = 0;
15841       if (fixP->fx_addsy
15842           && !S_IS_DEFINED (fixP->fx_addsy)
15843           && !S_IS_WEAK (fixP->fx_addsy))
15844         S_SET_WEAK (fixP->fx_addsy);
15845       break;
15846
15847     case BFD_RELOC_VTABLE_ENTRY:
15848       fixP->fx_done = 0;
15849       break;
15850
15851     default:
15852       abort ();
15853     }
15854
15855   /* Remember value for tc_gen_reloc.  */
15856   fixP->fx_addnumber = *valP;
15857 }
15858
15859 static symbolS *
15860 get_symbol (void)
15861 {
15862   int c;
15863   char *name;
15864   symbolS *p;
15865
15866   name = input_line_pointer;
15867   c = get_symbol_end ();
15868   p = (symbolS *) symbol_find_or_make (name);
15869   *input_line_pointer = c;
15870   return p;
15871 }
15872
15873 /* Align the current frag to a given power of two.  If a particular
15874    fill byte should be used, FILL points to an integer that contains
15875    that byte, otherwise FILL is null.
15876
15877    This function used to have the comment:
15878
15879       The MIPS assembler also automatically adjusts any preceding label.
15880
15881    The implementation therefore applied the adjustment to a maximum of
15882    one label.  However, other label adjustments are applied to batches
15883    of labels, and adjusting just one caused problems when new labels
15884    were added for the sake of debugging or unwind information.
15885    We therefore adjust all preceding labels (given as LABELS) instead.  */
15886
15887 static void
15888 mips_align (int to, int *fill, struct insn_label_list *labels)
15889 {
15890   mips_emit_delays ();
15891   mips_record_compressed_mode ();
15892   if (fill == NULL && subseg_text_p (now_seg))
15893     frag_align_code (to, 0);
15894   else
15895     frag_align (to, fill ? *fill : 0, 0);
15896   record_alignment (now_seg, to);
15897   mips_move_labels (labels, FALSE);
15898 }
15899
15900 /* Align to a given power of two.  .align 0 turns off the automatic
15901    alignment used by the data creating pseudo-ops.  */
15902
15903 static void
15904 s_align (int x ATTRIBUTE_UNUSED)
15905 {
15906   int temp, fill_value, *fill_ptr;
15907   long max_alignment = 28;
15908
15909   /* o Note that the assembler pulls down any immediately preceding label
15910        to the aligned address.
15911      o It's not documented but auto alignment is reinstated by
15912        a .align pseudo instruction.
15913      o Note also that after auto alignment is turned off the mips assembler
15914        issues an error on attempt to assemble an improperly aligned data item.
15915        We don't.  */
15916
15917   temp = get_absolute_expression ();
15918   if (temp > max_alignment)
15919     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15920   else if (temp < 0)
15921     {
15922       as_warn (_("Alignment negative: 0 assumed."));
15923       temp = 0;
15924     }
15925   if (*input_line_pointer == ',')
15926     {
15927       ++input_line_pointer;
15928       fill_value = get_absolute_expression ();
15929       fill_ptr = &fill_value;
15930     }
15931   else
15932     fill_ptr = 0;
15933   if (temp)
15934     {
15935       segment_info_type *si = seg_info (now_seg);
15936       struct insn_label_list *l = si->label_list;
15937       /* Auto alignment should be switched on by next section change.  */
15938       auto_align = 1;
15939       mips_align (temp, fill_ptr, l);
15940     }
15941   else
15942     {
15943       auto_align = 0;
15944     }
15945
15946   demand_empty_rest_of_line ();
15947 }
15948
15949 static void
15950 s_change_sec (int sec)
15951 {
15952   segT seg;
15953
15954 #ifdef OBJ_ELF
15955   /* The ELF backend needs to know that we are changing sections, so
15956      that .previous works correctly.  We could do something like check
15957      for an obj_section_change_hook macro, but that might be confusing
15958      as it would not be appropriate to use it in the section changing
15959      functions in read.c, since obj-elf.c intercepts those.  FIXME:
15960      This should be cleaner, somehow.  */
15961   if (IS_ELF)
15962     obj_elf_section_change_hook ();
15963 #endif
15964
15965   mips_emit_delays ();
15966
15967   switch (sec)
15968     {
15969     case 't':
15970       s_text (0);
15971       break;
15972     case 'd':
15973       s_data (0);
15974       break;
15975     case 'b':
15976       subseg_set (bss_section, (subsegT) get_absolute_expression ());
15977       demand_empty_rest_of_line ();
15978       break;
15979
15980     case 'r':
15981       seg = subseg_new (RDATA_SECTION_NAME,
15982                         (subsegT) get_absolute_expression ());
15983       if (IS_ELF)
15984         {
15985           bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15986                                                   | SEC_READONLY | SEC_RELOC
15987                                                   | SEC_DATA));
15988           if (strncmp (TARGET_OS, "elf", 3) != 0)
15989             record_alignment (seg, 4);
15990         }
15991       demand_empty_rest_of_line ();
15992       break;
15993
15994     case 's':
15995       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15996       if (IS_ELF)
15997         {
15998           bfd_set_section_flags (stdoutput, seg,
15999                                  SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16000           if (strncmp (TARGET_OS, "elf", 3) != 0)
16001             record_alignment (seg, 4);
16002         }
16003       demand_empty_rest_of_line ();
16004       break;
16005
16006     case 'B':
16007       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16008       if (IS_ELF)
16009         {
16010           bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16011           if (strncmp (TARGET_OS, "elf", 3) != 0)
16012             record_alignment (seg, 4);
16013         }
16014       demand_empty_rest_of_line ();
16015       break;
16016     }
16017
16018   auto_align = 1;
16019 }
16020
16021 void
16022 s_change_section (int ignore ATTRIBUTE_UNUSED)
16023 {
16024 #ifdef OBJ_ELF
16025   char *section_name;
16026   char c;
16027   char next_c = 0;
16028   int section_type;
16029   int section_flag;
16030   int section_entry_size;
16031   int section_alignment;
16032
16033   if (!IS_ELF)
16034     return;
16035
16036   section_name = input_line_pointer;
16037   c = get_symbol_end ();
16038   if (c)
16039     next_c = *(input_line_pointer + 1);
16040
16041   /* Do we have .section Name<,"flags">?  */
16042   if (c != ',' || (c == ',' && next_c == '"'))
16043     {
16044       /* just after name is now '\0'.  */
16045       *input_line_pointer = c;
16046       input_line_pointer = section_name;
16047       obj_elf_section (ignore);
16048       return;
16049     }
16050   input_line_pointer++;
16051
16052   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16053   if (c == ',')
16054     section_type = get_absolute_expression ();
16055   else
16056     section_type = 0;
16057   if (*input_line_pointer++ == ',')
16058     section_flag = get_absolute_expression ();
16059   else
16060     section_flag = 0;
16061   if (*input_line_pointer++ == ',')
16062     section_entry_size = get_absolute_expression ();
16063   else
16064     section_entry_size = 0;
16065   if (*input_line_pointer++ == ',')
16066     section_alignment = get_absolute_expression ();
16067   else
16068     section_alignment = 0;
16069   /* FIXME: really ignore?  */
16070   (void) section_alignment;
16071
16072   section_name = xstrdup (section_name);
16073
16074   /* When using the generic form of .section (as implemented by obj-elf.c),
16075      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16076      traditionally had to fall back on the more common @progbits instead.
16077
16078      There's nothing really harmful in this, since bfd will correct
16079      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16080      means that, for backwards compatibility, the special_section entries
16081      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16082
16083      Even so, we shouldn't force users of the MIPS .section syntax to
16084      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16085      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16086      generic type-checking code.  */
16087   if (section_type == SHT_MIPS_DWARF)
16088     section_type = SHT_PROGBITS;
16089
16090   obj_elf_change_section (section_name, section_type, section_flag,
16091                           section_entry_size, 0, 0, 0);
16092
16093   if (now_seg->name != section_name)
16094     free (section_name);
16095 #endif /* OBJ_ELF */
16096 }
16097
16098 void
16099 mips_enable_auto_align (void)
16100 {
16101   auto_align = 1;
16102 }
16103
16104 static void
16105 s_cons (int log_size)
16106 {
16107   segment_info_type *si = seg_info (now_seg);
16108   struct insn_label_list *l = si->label_list;
16109
16110   mips_emit_delays ();
16111   if (log_size > 0 && auto_align)
16112     mips_align (log_size, 0, l);
16113   cons (1 << log_size);
16114   mips_clear_insn_labels ();
16115 }
16116
16117 static void
16118 s_float_cons (int type)
16119 {
16120   segment_info_type *si = seg_info (now_seg);
16121   struct insn_label_list *l = si->label_list;
16122
16123   mips_emit_delays ();
16124
16125   if (auto_align)
16126     {
16127       if (type == 'd')
16128         mips_align (3, 0, l);
16129       else
16130         mips_align (2, 0, l);
16131     }
16132
16133   float_cons (type);
16134   mips_clear_insn_labels ();
16135 }
16136
16137 /* Handle .globl.  We need to override it because on Irix 5 you are
16138    permitted to say
16139        .globl foo .text
16140    where foo is an undefined symbol, to mean that foo should be
16141    considered to be the address of a function.  */
16142
16143 static void
16144 s_mips_globl (int x ATTRIBUTE_UNUSED)
16145 {
16146   char *name;
16147   int c;
16148   symbolS *symbolP;
16149   flagword flag;
16150
16151   do
16152     {
16153       name = input_line_pointer;
16154       c = get_symbol_end ();
16155       symbolP = symbol_find_or_make (name);
16156       S_SET_EXTERNAL (symbolP);
16157
16158       *input_line_pointer = c;
16159       SKIP_WHITESPACE ();
16160
16161       /* On Irix 5, every global symbol that is not explicitly labelled as
16162          being a function is apparently labelled as being an object.  */
16163       flag = BSF_OBJECT;
16164
16165       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16166           && (*input_line_pointer != ','))
16167         {
16168           char *secname;
16169           asection *sec;
16170
16171           secname = input_line_pointer;
16172           c = get_symbol_end ();
16173           sec = bfd_get_section_by_name (stdoutput, secname);
16174           if (sec == NULL)
16175             as_bad (_("%s: no such section"), secname);
16176           *input_line_pointer = c;
16177
16178           if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16179             flag = BSF_FUNCTION;
16180         }
16181
16182       symbol_get_bfdsym (symbolP)->flags |= flag;
16183
16184       c = *input_line_pointer;
16185       if (c == ',')
16186         {
16187           input_line_pointer++;
16188           SKIP_WHITESPACE ();
16189           if (is_end_of_line[(unsigned char) *input_line_pointer])
16190             c = '\n';
16191         }
16192     }
16193   while (c == ',');
16194
16195   demand_empty_rest_of_line ();
16196 }
16197
16198 static void
16199 s_option (int x ATTRIBUTE_UNUSED)
16200 {
16201   char *opt;
16202   char c;
16203
16204   opt = input_line_pointer;
16205   c = get_symbol_end ();
16206
16207   if (*opt == 'O')
16208     {
16209       /* FIXME: What does this mean?  */
16210     }
16211   else if (strncmp (opt, "pic", 3) == 0)
16212     {
16213       int i;
16214
16215       i = atoi (opt + 3);
16216       if (i == 0)
16217         mips_pic = NO_PIC;
16218       else if (i == 2)
16219         {
16220           mips_pic = SVR4_PIC;
16221           mips_abicalls = TRUE;
16222         }
16223       else
16224         as_bad (_(".option pic%d not supported"), i);
16225
16226       if (mips_pic == SVR4_PIC)
16227         {
16228           if (g_switch_seen && g_switch_value != 0)
16229             as_warn (_("-G may not be used with SVR4 PIC code"));
16230           g_switch_value = 0;
16231           bfd_set_gp_size (stdoutput, 0);
16232         }
16233     }
16234   else
16235     as_warn (_("Unrecognized option \"%s\""), opt);
16236
16237   *input_line_pointer = c;
16238   demand_empty_rest_of_line ();
16239 }
16240
16241 /* This structure is used to hold a stack of .set values.  */
16242
16243 struct mips_option_stack
16244 {
16245   struct mips_option_stack *next;
16246   struct mips_set_options options;
16247 };
16248
16249 static struct mips_option_stack *mips_opts_stack;
16250
16251 /* Handle the .set pseudo-op.  */
16252
16253 static void
16254 s_mipsset (int x ATTRIBUTE_UNUSED)
16255 {
16256   char *name = input_line_pointer, ch;
16257
16258   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16259     ++input_line_pointer;
16260   ch = *input_line_pointer;
16261   *input_line_pointer = '\0';
16262
16263   if (strcmp (name, "reorder") == 0)
16264     {
16265       if (mips_opts.noreorder)
16266         end_noreorder ();
16267     }
16268   else if (strcmp (name, "noreorder") == 0)
16269     {
16270       if (!mips_opts.noreorder)
16271         start_noreorder ();
16272     }
16273   else if (strncmp (name, "at=", 3) == 0)
16274     {
16275       char *s = name + 3;
16276
16277       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16278         as_bad (_("Unrecognized register name `%s'"), s);
16279     }
16280   else if (strcmp (name, "at") == 0)
16281     {
16282       mips_opts.at = ATREG;
16283     }
16284   else if (strcmp (name, "noat") == 0)
16285     {
16286       mips_opts.at = ZERO;
16287     }
16288   else if (strcmp (name, "macro") == 0)
16289     {
16290       mips_opts.warn_about_macros = 0;
16291     }
16292   else if (strcmp (name, "nomacro") == 0)
16293     {
16294       if (mips_opts.noreorder == 0)
16295         as_bad (_("`noreorder' must be set before `nomacro'"));
16296       mips_opts.warn_about_macros = 1;
16297     }
16298   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16299     {
16300       mips_opts.nomove = 0;
16301     }
16302   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16303     {
16304       mips_opts.nomove = 1;
16305     }
16306   else if (strcmp (name, "bopt") == 0)
16307     {
16308       mips_opts.nobopt = 0;
16309     }
16310   else if (strcmp (name, "nobopt") == 0)
16311     {
16312       mips_opts.nobopt = 1;
16313     }
16314   else if (strcmp (name, "gp=default") == 0)
16315     mips_opts.gp32 = file_mips_gp32;
16316   else if (strcmp (name, "gp=32") == 0)
16317     mips_opts.gp32 = 1;
16318   else if (strcmp (name, "gp=64") == 0)
16319     {
16320       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16321         as_warn (_("%s isa does not support 64-bit registers"),
16322                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16323       mips_opts.gp32 = 0;
16324     }
16325   else if (strcmp (name, "fp=default") == 0)
16326     mips_opts.fp32 = file_mips_fp32;
16327   else if (strcmp (name, "fp=32") == 0)
16328     mips_opts.fp32 = 1;
16329   else if (strcmp (name, "fp=64") == 0)
16330     {
16331       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16332         as_warn (_("%s isa does not support 64-bit floating point registers"),
16333                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16334       mips_opts.fp32 = 0;
16335     }
16336   else if (strcmp (name, "softfloat") == 0)
16337     mips_opts.soft_float = 1;
16338   else if (strcmp (name, "hardfloat") == 0)
16339     mips_opts.soft_float = 0;
16340   else if (strcmp (name, "singlefloat") == 0)
16341     mips_opts.single_float = 1;
16342   else if (strcmp (name, "doublefloat") == 0)
16343     mips_opts.single_float = 0;
16344   else if (strcmp (name, "mips16") == 0
16345            || strcmp (name, "MIPS-16") == 0)
16346     {
16347       if (mips_opts.micromips == 1)
16348         as_fatal (_("`mips16' cannot be used with `micromips'"));
16349       mips_opts.mips16 = 1;
16350     }
16351   else if (strcmp (name, "nomips16") == 0
16352            || strcmp (name, "noMIPS-16") == 0)
16353     mips_opts.mips16 = 0;
16354   else if (strcmp (name, "micromips") == 0)
16355     {
16356       if (mips_opts.mips16 == 1)
16357         as_fatal (_("`micromips' cannot be used with `mips16'"));
16358       mips_opts.micromips = 1;
16359     }
16360   else if (strcmp (name, "nomicromips") == 0)
16361     mips_opts.micromips = 0;
16362   else if (strcmp (name, "smartmips") == 0)
16363     {
16364       if (!ISA_SUPPORTS_SMARTMIPS)
16365         as_warn (_("%s ISA does not support SmartMIPS ASE"), 
16366                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16367       mips_opts.ase_smartmips = 1;
16368     }
16369   else if (strcmp (name, "nosmartmips") == 0)
16370     mips_opts.ase_smartmips = 0;
16371   else if (strcmp (name, "mips3d") == 0)
16372     mips_opts.ase_mips3d = 1;
16373   else if (strcmp (name, "nomips3d") == 0)
16374     mips_opts.ase_mips3d = 0;
16375   else if (strcmp (name, "mdmx") == 0)
16376     mips_opts.ase_mdmx = 1;
16377   else if (strcmp (name, "nomdmx") == 0)
16378     mips_opts.ase_mdmx = 0;
16379   else if (strcmp (name, "dsp") == 0)
16380     {
16381       if (!ISA_SUPPORTS_DSP_ASE)
16382         as_warn (_("%s ISA does not support DSP ASE"), 
16383                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16384       mips_opts.ase_dsp = 1;
16385       mips_opts.ase_dspr2 = 0;
16386     }
16387   else if (strcmp (name, "nodsp") == 0)
16388     {
16389       mips_opts.ase_dsp = 0;
16390       mips_opts.ase_dspr2 = 0;
16391     }
16392   else if (strcmp (name, "dspr2") == 0)
16393     {
16394       if (!ISA_SUPPORTS_DSPR2_ASE)
16395         as_warn (_("%s ISA does not support DSP R2 ASE"),
16396                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16397       mips_opts.ase_dspr2 = 1;
16398       mips_opts.ase_dsp = 1;
16399     }
16400   else if (strcmp (name, "nodspr2") == 0)
16401     {
16402       mips_opts.ase_dspr2 = 0;
16403       mips_opts.ase_dsp = 0;
16404     }
16405   else if (strcmp (name, "mt") == 0)
16406     {
16407       if (!ISA_SUPPORTS_MT_ASE)
16408         as_warn (_("%s ISA does not support MT ASE"), 
16409                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16410       mips_opts.ase_mt = 1;
16411     }
16412   else if (strcmp (name, "nomt") == 0)
16413     mips_opts.ase_mt = 0;
16414   else if (strcmp (name, "mcu") == 0)
16415     mips_opts.ase_mcu = 1;
16416   else if (strcmp (name, "nomcu") == 0)
16417     mips_opts.ase_mcu = 0;
16418   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16419     {
16420       int reset = 0;
16421
16422       /* Permit the user to change the ISA and architecture on the fly.
16423          Needless to say, misuse can cause serious problems.  */
16424       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16425         {
16426           reset = 1;
16427           mips_opts.isa = file_mips_isa;
16428           mips_opts.arch = file_mips_arch;
16429         }
16430       else if (strncmp (name, "arch=", 5) == 0)
16431         {
16432           const struct mips_cpu_info *p;
16433
16434           p = mips_parse_cpu("internal use", name + 5);
16435           if (!p)
16436             as_bad (_("unknown architecture %s"), name + 5);
16437           else
16438             {
16439               mips_opts.arch = p->cpu;
16440               mips_opts.isa = p->isa;
16441             }
16442         }
16443       else if (strncmp (name, "mips", 4) == 0)
16444         {
16445           const struct mips_cpu_info *p;
16446
16447           p = mips_parse_cpu("internal use", name);
16448           if (!p)
16449             as_bad (_("unknown ISA level %s"), name + 4);
16450           else
16451             {
16452               mips_opts.arch = p->cpu;
16453               mips_opts.isa = p->isa;
16454             }
16455         }
16456       else
16457         as_bad (_("unknown ISA or architecture %s"), name);
16458
16459       switch (mips_opts.isa)
16460         {
16461         case  0:
16462           break;
16463         case ISA_MIPS1:
16464         case ISA_MIPS2:
16465         case ISA_MIPS32:
16466         case ISA_MIPS32R2:
16467           mips_opts.gp32 = 1;
16468           mips_opts.fp32 = 1;
16469           break;
16470         case ISA_MIPS3:
16471         case ISA_MIPS4:
16472         case ISA_MIPS5:
16473         case ISA_MIPS64:
16474         case ISA_MIPS64R2:
16475           mips_opts.gp32 = 0;
16476           if (mips_opts.arch == CPU_R5900)
16477             {
16478                 mips_opts.fp32 = 1;
16479             }
16480           else
16481             {
16482           mips_opts.fp32 = 0;
16483             }
16484           break;
16485         default:
16486           as_bad (_("unknown ISA level %s"), name + 4);
16487           break;
16488         }
16489       if (reset)
16490         {
16491           mips_opts.gp32 = file_mips_gp32;
16492           mips_opts.fp32 = file_mips_fp32;
16493         }
16494     }
16495   else if (strcmp (name, "autoextend") == 0)
16496     mips_opts.noautoextend = 0;
16497   else if (strcmp (name, "noautoextend") == 0)
16498     mips_opts.noautoextend = 1;
16499   else if (strcmp (name, "push") == 0)
16500     {
16501       struct mips_option_stack *s;
16502
16503       s = (struct mips_option_stack *) xmalloc (sizeof *s);
16504       s->next = mips_opts_stack;
16505       s->options = mips_opts;
16506       mips_opts_stack = s;
16507     }
16508   else if (strcmp (name, "pop") == 0)
16509     {
16510       struct mips_option_stack *s;
16511
16512       s = mips_opts_stack;
16513       if (s == NULL)
16514         as_bad (_(".set pop with no .set push"));
16515       else
16516         {
16517           /* If we're changing the reorder mode we need to handle
16518              delay slots correctly.  */
16519           if (s->options.noreorder && ! mips_opts.noreorder)
16520             start_noreorder ();
16521           else if (! s->options.noreorder && mips_opts.noreorder)
16522             end_noreorder ();
16523
16524           mips_opts = s->options;
16525           mips_opts_stack = s->next;
16526           free (s);
16527         }
16528     }
16529   else if (strcmp (name, "sym32") == 0)
16530     mips_opts.sym32 = TRUE;
16531   else if (strcmp (name, "nosym32") == 0)
16532     mips_opts.sym32 = FALSE;
16533   else if (strchr (name, ','))
16534     {
16535       /* Generic ".set" directive; use the generic handler.  */
16536       *input_line_pointer = ch;
16537       input_line_pointer = name;
16538       s_set (0);
16539       return;
16540     }
16541   else
16542     {
16543       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16544     }
16545   *input_line_pointer = ch;
16546   demand_empty_rest_of_line ();
16547 }
16548
16549 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16550    .option pic2.  It means to generate SVR4 PIC calls.  */
16551
16552 static void
16553 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16554 {
16555   mips_pic = SVR4_PIC;
16556   mips_abicalls = TRUE;
16557
16558   if (g_switch_seen && g_switch_value != 0)
16559     as_warn (_("-G may not be used with SVR4 PIC code"));
16560   g_switch_value = 0;
16561
16562   bfd_set_gp_size (stdoutput, 0);
16563   demand_empty_rest_of_line ();
16564 }
16565
16566 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16567    PIC code.  It sets the $gp register for the function based on the
16568    function address, which is in the register named in the argument.
16569    This uses a relocation against _gp_disp, which is handled specially
16570    by the linker.  The result is:
16571         lui     $gp,%hi(_gp_disp)
16572         addiu   $gp,$gp,%lo(_gp_disp)
16573         addu    $gp,$gp,.cpload argument
16574    The .cpload argument is normally $25 == $t9.
16575
16576    The -mno-shared option changes this to:
16577         lui     $gp,%hi(__gnu_local_gp)
16578         addiu   $gp,$gp,%lo(__gnu_local_gp)
16579    and the argument is ignored.  This saves an instruction, but the
16580    resulting code is not position independent; it uses an absolute
16581    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16582    can go into an ordinary executable, but not into a shared library.  */
16583
16584 static void
16585 s_cpload (int ignore ATTRIBUTE_UNUSED)
16586 {
16587   expressionS ex;
16588   int reg;
16589   int in_shared;
16590
16591   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16592      .cpload is ignored.  */
16593   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16594     {
16595       s_ignore (0);
16596       return;
16597     }
16598
16599   if (mips_opts.mips16)
16600     {
16601       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16602       ignore_rest_of_line ();
16603       return;
16604     }
16605
16606   /* .cpload should be in a .set noreorder section.  */
16607   if (mips_opts.noreorder == 0)
16608     as_warn (_(".cpload not in noreorder section"));
16609
16610   reg = tc_get_register (0);
16611
16612   /* If we need to produce a 64-bit address, we are better off using
16613      the default instruction sequence.  */
16614   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16615
16616   ex.X_op = O_symbol;
16617   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16618                                          "__gnu_local_gp");
16619   ex.X_op_symbol = NULL;
16620   ex.X_add_number = 0;
16621
16622   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16623   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16624
16625   macro_start ();
16626   macro_build_lui (&ex, mips_gp_register);
16627   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16628                mips_gp_register, BFD_RELOC_LO16);
16629   if (in_shared)
16630     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16631                  mips_gp_register, reg);
16632   macro_end ();
16633
16634   demand_empty_rest_of_line ();
16635 }
16636
16637 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16638      .cpsetup $reg1, offset|$reg2, label
16639
16640    If offset is given, this results in:
16641      sd         $gp, offset($sp)
16642      lui        $gp, %hi(%neg(%gp_rel(label)))
16643      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16644      daddu      $gp, $gp, $reg1
16645
16646    If $reg2 is given, this results in:
16647      daddu      $reg2, $gp, $0
16648      lui        $gp, %hi(%neg(%gp_rel(label)))
16649      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16650      daddu      $gp, $gp, $reg1
16651    $reg1 is normally $25 == $t9.
16652
16653    The -mno-shared option replaces the last three instructions with
16654         lui     $gp,%hi(_gp)
16655         addiu   $gp,$gp,%lo(_gp)  */
16656
16657 static void
16658 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16659 {
16660   expressionS ex_off;
16661   expressionS ex_sym;
16662   int reg1;
16663
16664   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16665      We also need NewABI support.  */
16666   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16667     {
16668       s_ignore (0);
16669       return;
16670     }
16671
16672   if (mips_opts.mips16)
16673     {
16674       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16675       ignore_rest_of_line ();
16676       return;
16677     }
16678
16679   reg1 = tc_get_register (0);
16680   SKIP_WHITESPACE ();
16681   if (*input_line_pointer != ',')
16682     {
16683       as_bad (_("missing argument separator ',' for .cpsetup"));
16684       return;
16685     }
16686   else
16687     ++input_line_pointer;
16688   SKIP_WHITESPACE ();
16689   if (*input_line_pointer == '$')
16690     {
16691       mips_cpreturn_register = tc_get_register (0);
16692       mips_cpreturn_offset = -1;
16693     }
16694   else
16695     {
16696       mips_cpreturn_offset = get_absolute_expression ();
16697       mips_cpreturn_register = -1;
16698     }
16699   SKIP_WHITESPACE ();
16700   if (*input_line_pointer != ',')
16701     {
16702       as_bad (_("missing argument separator ',' for .cpsetup"));
16703       return;
16704     }
16705   else
16706     ++input_line_pointer;
16707   SKIP_WHITESPACE ();
16708   expression (&ex_sym);
16709
16710   macro_start ();
16711   if (mips_cpreturn_register == -1)
16712     {
16713       ex_off.X_op = O_constant;
16714       ex_off.X_add_symbol = NULL;
16715       ex_off.X_op_symbol = NULL;
16716       ex_off.X_add_number = mips_cpreturn_offset;
16717
16718       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16719                    BFD_RELOC_LO16, SP);
16720     }
16721   else
16722     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16723                  mips_gp_register, 0);
16724
16725   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16726     {
16727       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16728                    -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16729                    BFD_RELOC_HI16_S);
16730
16731       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16732                    mips_gp_register, -1, BFD_RELOC_GPREL16,
16733                    BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16734
16735       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16736                    mips_gp_register, reg1);
16737     }
16738   else
16739     {
16740       expressionS ex;
16741
16742       ex.X_op = O_symbol;
16743       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16744       ex.X_op_symbol = NULL;
16745       ex.X_add_number = 0;
16746
16747       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16748       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16749
16750       macro_build_lui (&ex, mips_gp_register);
16751       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16752                    mips_gp_register, BFD_RELOC_LO16);
16753     }
16754
16755   macro_end ();
16756
16757   demand_empty_rest_of_line ();
16758 }
16759
16760 static void
16761 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16762 {
16763   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16764      .cplocal is ignored.  */
16765   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16766     {
16767       s_ignore (0);
16768       return;
16769     }
16770
16771   if (mips_opts.mips16)
16772     {
16773       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16774       ignore_rest_of_line ();
16775       return;
16776     }
16777
16778   mips_gp_register = tc_get_register (0);
16779   demand_empty_rest_of_line ();
16780 }
16781
16782 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16783    offset from $sp.  The offset is remembered, and after making a PIC
16784    call $gp is restored from that location.  */
16785
16786 static void
16787 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16788 {
16789   expressionS ex;
16790
16791   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16792      .cprestore is ignored.  */
16793   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16794     {
16795       s_ignore (0);
16796       return;
16797     }
16798
16799   if (mips_opts.mips16)
16800     {
16801       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16802       ignore_rest_of_line ();
16803       return;
16804     }
16805
16806   mips_cprestore_offset = get_absolute_expression ();
16807   mips_cprestore_valid = 1;
16808
16809   ex.X_op = O_constant;
16810   ex.X_add_symbol = NULL;
16811   ex.X_op_symbol = NULL;
16812   ex.X_add_number = mips_cprestore_offset;
16813
16814   macro_start ();
16815   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16816                                 SP, HAVE_64BIT_ADDRESSES);
16817   macro_end ();
16818
16819   demand_empty_rest_of_line ();
16820 }
16821
16822 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16823    was given in the preceding .cpsetup, it results in:
16824      ld         $gp, offset($sp)
16825
16826    If a register $reg2 was given there, it results in:
16827      daddu      $gp, $reg2, $0  */
16828
16829 static void
16830 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16831 {
16832   expressionS ex;
16833
16834   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16835      We also need NewABI support.  */
16836   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16837     {
16838       s_ignore (0);
16839       return;
16840     }
16841
16842   if (mips_opts.mips16)
16843     {
16844       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16845       ignore_rest_of_line ();
16846       return;
16847     }
16848
16849   macro_start ();
16850   if (mips_cpreturn_register == -1)
16851     {
16852       ex.X_op = O_constant;
16853       ex.X_add_symbol = NULL;
16854       ex.X_op_symbol = NULL;
16855       ex.X_add_number = mips_cpreturn_offset;
16856
16857       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16858     }
16859   else
16860     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16861                  mips_cpreturn_register, 0);
16862   macro_end ();
16863
16864   demand_empty_rest_of_line ();
16865 }
16866
16867 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16868    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16869    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16870    debug information or MIPS16 TLS.  */
16871
16872 static void
16873 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16874                      bfd_reloc_code_real_type rtype)
16875 {
16876   expressionS ex;
16877   char *p;
16878
16879   expression (&ex);
16880
16881   if (ex.X_op != O_symbol)
16882     {
16883       as_bad (_("Unsupported use of %s"), dirstr);
16884       ignore_rest_of_line ();
16885     }
16886
16887   p = frag_more (bytes);
16888   md_number_to_chars (p, 0, bytes);
16889   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16890   demand_empty_rest_of_line ();
16891   mips_clear_insn_labels ();
16892 }
16893
16894 /* Handle .dtprelword.  */
16895
16896 static void
16897 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16898 {
16899   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16900 }
16901
16902 /* Handle .dtpreldword.  */
16903
16904 static void
16905 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16906 {
16907   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16908 }
16909
16910 /* Handle .tprelword.  */
16911
16912 static void
16913 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16914 {
16915   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16916 }
16917
16918 /* Handle .tpreldword.  */
16919
16920 static void
16921 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16922 {
16923   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16924 }
16925
16926 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
16927    code.  It sets the offset to use in gp_rel relocations.  */
16928
16929 static void
16930 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16931 {
16932   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16933      We also need NewABI support.  */
16934   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16935     {
16936       s_ignore (0);
16937       return;
16938     }
16939
16940   mips_gprel_offset = get_absolute_expression ();
16941
16942   demand_empty_rest_of_line ();
16943 }
16944
16945 /* Handle the .gpword pseudo-op.  This is used when generating PIC
16946    code.  It generates a 32 bit GP relative reloc.  */
16947
16948 static void
16949 s_gpword (int ignore ATTRIBUTE_UNUSED)
16950 {
16951   segment_info_type *si;
16952   struct insn_label_list *l;
16953   expressionS ex;
16954   char *p;
16955
16956   /* When not generating PIC code, this is treated as .word.  */
16957   if (mips_pic != SVR4_PIC)
16958     {
16959       s_cons (2);
16960       return;
16961     }
16962
16963   si = seg_info (now_seg);
16964   l = si->label_list;
16965   mips_emit_delays ();
16966   if (auto_align)
16967     mips_align (2, 0, l);
16968
16969   expression (&ex);
16970   mips_clear_insn_labels ();
16971
16972   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16973     {
16974       as_bad (_("Unsupported use of .gpword"));
16975       ignore_rest_of_line ();
16976     }
16977
16978   p = frag_more (4);
16979   md_number_to_chars (p, 0, 4);
16980   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16981                BFD_RELOC_GPREL32);
16982
16983   demand_empty_rest_of_line ();
16984 }
16985
16986 static void
16987 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16988 {
16989   segment_info_type *si;
16990   struct insn_label_list *l;
16991   expressionS ex;
16992   char *p;
16993
16994   /* When not generating PIC code, this is treated as .dword.  */
16995   if (mips_pic != SVR4_PIC)
16996     {
16997       s_cons (3);
16998       return;
16999     }
17000
17001   si = seg_info (now_seg);
17002   l = si->label_list;
17003   mips_emit_delays ();
17004   if (auto_align)
17005     mips_align (3, 0, l);
17006
17007   expression (&ex);
17008   mips_clear_insn_labels ();
17009
17010   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17011     {
17012       as_bad (_("Unsupported use of .gpdword"));
17013       ignore_rest_of_line ();
17014     }
17015
17016   p = frag_more (8);
17017   md_number_to_chars (p, 0, 8);
17018   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17019                BFD_RELOC_GPREL32)->fx_tcbit = 1;
17020
17021   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
17022   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17023            FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17024
17025   demand_empty_rest_of_line ();
17026 }
17027
17028 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
17029    tables in SVR4 PIC code.  */
17030
17031 static void
17032 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17033 {
17034   int reg;
17035
17036   /* This is ignored when not generating SVR4 PIC code.  */
17037   if (mips_pic != SVR4_PIC)
17038     {
17039       s_ignore (0);
17040       return;
17041     }
17042
17043   /* Add $gp to the register named as an argument.  */
17044   macro_start ();
17045   reg = tc_get_register (0);
17046   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17047   macro_end ();
17048
17049   demand_empty_rest_of_line ();
17050 }
17051
17052 /* Handle the .insn pseudo-op.  This marks instruction labels in
17053    mips16/micromips mode.  This permits the linker to handle them specially,
17054    such as generating jalx instructions when needed.  We also make
17055    them odd for the duration of the assembly, in order to generate the
17056    right sort of code.  We will make them even in the adjust_symtab
17057    routine, while leaving them marked.  This is convenient for the
17058    debugger and the disassembler.  The linker knows to make them odd
17059    again.  */
17060
17061 static void
17062 s_insn (int ignore ATTRIBUTE_UNUSED)
17063 {
17064   mips_mark_labels ();
17065
17066   demand_empty_rest_of_line ();
17067 }
17068
17069 /* Handle a .stabn directive.  We need these in order to mark a label
17070    as being a mips16 text label correctly.  Sometimes the compiler
17071    will emit a label, followed by a .stabn, and then switch sections.
17072    If the label and .stabn are in mips16 mode, then the label is
17073    really a mips16 text label.  */
17074
17075 static void
17076 s_mips_stab (int type)
17077 {
17078   if (type == 'n')
17079     mips_mark_labels ();
17080
17081   s_stab (type);
17082 }
17083
17084 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17085
17086 static void
17087 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17088 {
17089   char *name;
17090   int c;
17091   symbolS *symbolP;
17092   expressionS exp;
17093
17094   name = input_line_pointer;
17095   c = get_symbol_end ();
17096   symbolP = symbol_find_or_make (name);
17097   S_SET_WEAK (symbolP);
17098   *input_line_pointer = c;
17099
17100   SKIP_WHITESPACE ();
17101
17102   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17103     {
17104       if (S_IS_DEFINED (symbolP))
17105         {
17106           as_bad (_("ignoring attempt to redefine symbol %s"),
17107                   S_GET_NAME (symbolP));
17108           ignore_rest_of_line ();
17109           return;
17110         }
17111
17112       if (*input_line_pointer == ',')
17113         {
17114           ++input_line_pointer;
17115           SKIP_WHITESPACE ();
17116         }
17117
17118       expression (&exp);
17119       if (exp.X_op != O_symbol)
17120         {
17121           as_bad (_("bad .weakext directive"));
17122           ignore_rest_of_line ();
17123           return;
17124         }
17125       symbol_set_value_expression (symbolP, &exp);
17126     }
17127
17128   demand_empty_rest_of_line ();
17129 }
17130
17131 /* Parse a register string into a number.  Called from the ECOFF code
17132    to parse .frame.  The argument is non-zero if this is the frame
17133    register, so that we can record it in mips_frame_reg.  */
17134
17135 int
17136 tc_get_register (int frame)
17137 {
17138   unsigned int reg;
17139
17140   SKIP_WHITESPACE ();
17141   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17142     reg = 0;
17143   if (frame)
17144     {
17145       mips_frame_reg = reg != 0 ? reg : SP;
17146       mips_frame_reg_valid = 1;
17147       mips_cprestore_valid = 0;
17148     }
17149   return reg;
17150 }
17151
17152 valueT
17153 md_section_align (asection *seg, valueT addr)
17154 {
17155   int align = bfd_get_section_alignment (stdoutput, seg);
17156
17157   if (IS_ELF)
17158     {
17159       /* We don't need to align ELF sections to the full alignment.
17160          However, Irix 5 may prefer that we align them at least to a 16
17161          byte boundary.  We don't bother to align the sections if we
17162          are targeted for an embedded system.  */
17163       if (strncmp (TARGET_OS, "elf", 3) == 0)
17164         return addr;
17165       if (align > 4)
17166         align = 4;
17167     }
17168
17169   return ((addr + (1 << align) - 1) & (-1 << align));
17170 }
17171
17172 /* Utility routine, called from above as well.  If called while the
17173    input file is still being read, it's only an approximation.  (For
17174    example, a symbol may later become defined which appeared to be
17175    undefined earlier.)  */
17176
17177 static int
17178 nopic_need_relax (symbolS *sym, int before_relaxing)
17179 {
17180   if (sym == 0)
17181     return 0;
17182
17183   if (g_switch_value > 0)
17184     {
17185       const char *symname;
17186       int change;
17187
17188       /* Find out whether this symbol can be referenced off the $gp
17189          register.  It can be if it is smaller than the -G size or if
17190          it is in the .sdata or .sbss section.  Certain symbols can
17191          not be referenced off the $gp, although it appears as though
17192          they can.  */
17193       symname = S_GET_NAME (sym);
17194       if (symname != (const char *) NULL
17195           && (strcmp (symname, "eprol") == 0
17196               || strcmp (symname, "etext") == 0
17197               || strcmp (symname, "_gp") == 0
17198               || strcmp (symname, "edata") == 0
17199               || strcmp (symname, "_fbss") == 0
17200               || strcmp (symname, "_fdata") == 0
17201               || strcmp (symname, "_ftext") == 0
17202               || strcmp (symname, "end") == 0
17203               || strcmp (symname, "_gp_disp") == 0))
17204         change = 1;
17205       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17206                && (0
17207 #ifndef NO_ECOFF_DEBUGGING
17208                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
17209                        && (symbol_get_obj (sym)->ecoff_extern_size
17210                            <= g_switch_value))
17211 #endif
17212                    /* We must defer this decision until after the whole
17213                       file has been read, since there might be a .extern
17214                       after the first use of this symbol.  */
17215                    || (before_relaxing
17216 #ifndef NO_ECOFF_DEBUGGING
17217                        && symbol_get_obj (sym)->ecoff_extern_size == 0
17218 #endif
17219                        && S_GET_VALUE (sym) == 0)
17220                    || (S_GET_VALUE (sym) != 0
17221                        && S_GET_VALUE (sym) <= g_switch_value)))
17222         change = 0;
17223       else
17224         {
17225           const char *segname;
17226
17227           segname = segment_name (S_GET_SEGMENT (sym));
17228           gas_assert (strcmp (segname, ".lit8") != 0
17229                   && strcmp (segname, ".lit4") != 0);
17230           change = (strcmp (segname, ".sdata") != 0
17231                     && strcmp (segname, ".sbss") != 0
17232                     && strncmp (segname, ".sdata.", 7) != 0
17233                     && strncmp (segname, ".sbss.", 6) != 0
17234                     && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17235                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17236         }
17237       return change;
17238     }
17239   else
17240     /* We are not optimizing for the $gp register.  */
17241     return 1;
17242 }
17243
17244
17245 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17246
17247 static bfd_boolean
17248 pic_need_relax (symbolS *sym, asection *segtype)
17249 {
17250   asection *symsec;
17251
17252   /* Handle the case of a symbol equated to another symbol.  */
17253   while (symbol_equated_reloc_p (sym))
17254     {
17255       symbolS *n;
17256
17257       /* It's possible to get a loop here in a badly written program.  */
17258       n = symbol_get_value_expression (sym)->X_add_symbol;
17259       if (n == sym)
17260         break;
17261       sym = n;
17262     }
17263
17264   if (symbol_section_p (sym))
17265     return TRUE;
17266
17267   symsec = S_GET_SEGMENT (sym);
17268
17269   /* This must duplicate the test in adjust_reloc_syms.  */
17270   return (!bfd_is_und_section (symsec)
17271           && !bfd_is_abs_section (symsec)
17272           && !bfd_is_com_section (symsec)
17273           && !s_is_linkonce (sym, segtype)
17274 #ifdef OBJ_ELF
17275           /* A global or weak symbol is treated as external.  */
17276           && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
17277 #endif
17278           );
17279 }
17280
17281
17282 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17283    extended opcode.  SEC is the section the frag is in.  */
17284
17285 static int
17286 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17287 {
17288   int type;
17289   const struct mips16_immed_operand *op;
17290   offsetT val;
17291   int mintiny, maxtiny;
17292   segT symsec;
17293   fragS *sym_frag;
17294
17295   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17296     return 0;
17297   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17298     return 1;
17299
17300   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17301   op = mips16_immed_operands;
17302   while (op->type != type)
17303     {
17304       ++op;
17305       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17306     }
17307
17308   if (op->unsp)
17309     {
17310       if (type == '<' || type == '>' || type == '[' || type == ']')
17311         {
17312           mintiny = 1;
17313           maxtiny = 1 << op->nbits;
17314         }
17315       else
17316         {
17317           mintiny = 0;
17318           maxtiny = (1 << op->nbits) - 1;
17319         }
17320     }
17321   else
17322     {
17323       mintiny = - (1 << (op->nbits - 1));
17324       maxtiny = (1 << (op->nbits - 1)) - 1;
17325     }
17326
17327   sym_frag = symbol_get_frag (fragp->fr_symbol);
17328   val = S_GET_VALUE (fragp->fr_symbol);
17329   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17330
17331   if (op->pcrel)
17332     {
17333       addressT addr;
17334
17335       /* We won't have the section when we are called from
17336          mips_relax_frag.  However, we will always have been called
17337          from md_estimate_size_before_relax first.  If this is a
17338          branch to a different section, we mark it as such.  If SEC is
17339          NULL, and the frag is not marked, then it must be a branch to
17340          the same section.  */
17341       if (sec == NULL)
17342         {
17343           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17344             return 1;
17345         }
17346       else
17347         {
17348           /* Must have been called from md_estimate_size_before_relax.  */
17349           if (symsec != sec)
17350             {
17351               fragp->fr_subtype =
17352                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17353
17354               /* FIXME: We should support this, and let the linker
17355                  catch branches and loads that are out of range.  */
17356               as_bad_where (fragp->fr_file, fragp->fr_line,
17357                             _("unsupported PC relative reference to different section"));
17358
17359               return 1;
17360             }
17361           if (fragp != sym_frag && sym_frag->fr_address == 0)
17362             /* Assume non-extended on the first relaxation pass.
17363                The address we have calculated will be bogus if this is
17364                a forward branch to another frag, as the forward frag
17365                will have fr_address == 0.  */
17366             return 0;
17367         }
17368
17369       /* In this case, we know for sure that the symbol fragment is in
17370          the same section.  If the relax_marker of the symbol fragment
17371          differs from the relax_marker of this fragment, we have not
17372          yet adjusted the symbol fragment fr_address.  We want to add
17373          in STRETCH in order to get a better estimate of the address.
17374          This particularly matters because of the shift bits.  */
17375       if (stretch != 0
17376           && sym_frag->relax_marker != fragp->relax_marker)
17377         {
17378           fragS *f;
17379
17380           /* Adjust stretch for any alignment frag.  Note that if have
17381              been expanding the earlier code, the symbol may be
17382              defined in what appears to be an earlier frag.  FIXME:
17383              This doesn't handle the fr_subtype field, which specifies
17384              a maximum number of bytes to skip when doing an
17385              alignment.  */
17386           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17387             {
17388               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17389                 {
17390                   if (stretch < 0)
17391                     stretch = - ((- stretch)
17392                                  & ~ ((1 << (int) f->fr_offset) - 1));
17393                   else
17394                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17395                   if (stretch == 0)
17396                     break;
17397                 }
17398             }
17399           if (f != NULL)
17400             val += stretch;
17401         }
17402
17403       addr = fragp->fr_address + fragp->fr_fix;
17404
17405       /* The base address rules are complicated.  The base address of
17406          a branch is the following instruction.  The base address of a
17407          PC relative load or add is the instruction itself, but if it
17408          is in a delay slot (in which case it can not be extended) use
17409          the address of the instruction whose delay slot it is in.  */
17410       if (type == 'p' || type == 'q')
17411         {
17412           addr += 2;
17413
17414           /* If we are currently assuming that this frag should be
17415              extended, then, the current address is two bytes
17416              higher.  */
17417           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17418             addr += 2;
17419
17420           /* Ignore the low bit in the target, since it will be set
17421              for a text label.  */
17422           if ((val & 1) != 0)
17423             --val;
17424         }
17425       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17426         addr -= 4;
17427       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17428         addr -= 2;
17429
17430       val -= addr & ~ ((1 << op->shift) - 1);
17431
17432       /* Branch offsets have an implicit 0 in the lowest bit.  */
17433       if (type == 'p' || type == 'q')
17434         val /= 2;
17435
17436       /* If any of the shifted bits are set, we must use an extended
17437          opcode.  If the address depends on the size of this
17438          instruction, this can lead to a loop, so we arrange to always
17439          use an extended opcode.  We only check this when we are in
17440          the main relaxation loop, when SEC is NULL.  */
17441       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17442         {
17443           fragp->fr_subtype =
17444             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17445           return 1;
17446         }
17447
17448       /* If we are about to mark a frag as extended because the value
17449          is precisely maxtiny + 1, then there is a chance of an
17450          infinite loop as in the following code:
17451              la $4,foo
17452              .skip      1020
17453              .align     2
17454            foo:
17455          In this case when the la is extended, foo is 0x3fc bytes
17456          away, so the la can be shrunk, but then foo is 0x400 away, so
17457          the la must be extended.  To avoid this loop, we mark the
17458          frag as extended if it was small, and is about to become
17459          extended with a value of maxtiny + 1.  */
17460       if (val == ((maxtiny + 1) << op->shift)
17461           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17462           && sec == NULL)
17463         {
17464           fragp->fr_subtype =
17465             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17466           return 1;
17467         }
17468     }
17469   else if (symsec != absolute_section && sec != NULL)
17470     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17471
17472   if ((val & ((1 << op->shift) - 1)) != 0
17473       || val < (mintiny << op->shift)
17474       || val > (maxtiny << op->shift))
17475     return 1;
17476   else
17477     return 0;
17478 }
17479
17480 /* Compute the length of a branch sequence, and adjust the
17481    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17482    worst-case length is computed, with UPDATE being used to indicate
17483    whether an unconditional (-1), branch-likely (+1) or regular (0)
17484    branch is to be computed.  */
17485 static int
17486 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17487 {
17488   bfd_boolean toofar;
17489   int length;
17490
17491   if (fragp
17492       && S_IS_DEFINED (fragp->fr_symbol)
17493       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17494     {
17495       addressT addr;
17496       offsetT val;
17497
17498       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17499
17500       addr = fragp->fr_address + fragp->fr_fix + 4;
17501
17502       val -= addr;
17503
17504       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17505     }
17506   else if (fragp)
17507     /* If the symbol is not defined or it's in a different segment,
17508        assume the user knows what's going on and emit a short
17509        branch.  */
17510     toofar = FALSE;
17511   else
17512     toofar = TRUE;
17513
17514   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17515     fragp->fr_subtype
17516       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17517                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17518                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17519                              RELAX_BRANCH_LINK (fragp->fr_subtype),
17520                              toofar);
17521
17522   length = 4;
17523   if (toofar)
17524     {
17525       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17526         length += 8;
17527
17528       if (mips_pic != NO_PIC)
17529         {
17530           /* Additional space for PIC loading of target address.  */
17531           length += 8;
17532           if (mips_opts.isa == ISA_MIPS1)
17533             /* Additional space for $at-stabilizing nop.  */
17534             length += 4;
17535         }
17536
17537       /* If branch is conditional.  */
17538       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17539         length += 8;
17540     }
17541
17542   return length;
17543 }
17544
17545 /* Compute the length of a branch sequence, and adjust the
17546    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
17547    worst-case length is computed, with UPDATE being used to indicate
17548    whether an unconditional (-1), or regular (0) branch is to be
17549    computed.  */
17550
17551 static int
17552 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17553 {
17554   bfd_boolean toofar;
17555   int length;
17556
17557   if (fragp
17558       && S_IS_DEFINED (fragp->fr_symbol)
17559       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17560     {
17561       addressT addr;
17562       offsetT val;
17563
17564       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17565       /* Ignore the low bit in the target, since it will be set
17566          for a text label.  */
17567       if ((val & 1) != 0)
17568         --val;
17569
17570       addr = fragp->fr_address + fragp->fr_fix + 4;
17571
17572       val -= addr;
17573
17574       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17575     }
17576   else if (fragp)
17577     /* If the symbol is not defined or it's in a different segment,
17578        assume the user knows what's going on and emit a short
17579        branch.  */
17580     toofar = FALSE;
17581   else
17582     toofar = TRUE;
17583
17584   if (fragp && update
17585       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17586     fragp->fr_subtype = (toofar
17587                          ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17588                          : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17589
17590   length = 4;
17591   if (toofar)
17592     {
17593       bfd_boolean compact_known = fragp != NULL;
17594       bfd_boolean compact = FALSE;
17595       bfd_boolean uncond;
17596
17597       if (compact_known)
17598         compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17599       if (fragp)
17600         uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17601       else
17602         uncond = update < 0;
17603
17604       /* If label is out of range, we turn branch <br>:
17605
17606                 <br>    label                   # 4 bytes
17607             0:
17608
17609          into:
17610
17611                 j       label                   # 4 bytes
17612                 nop                             # 2 bytes if compact && !PIC
17613             0:
17614        */
17615       if (mips_pic == NO_PIC && (!compact_known || compact))
17616         length += 2;
17617
17618       /* If assembling PIC code, we further turn:
17619
17620                         j       label                   # 4 bytes
17621
17622          into:
17623
17624                         lw/ld   at, %got(label)(gp)     # 4 bytes
17625                         d/addiu at, %lo(label)          # 4 bytes
17626                         jr/c    at                      # 2 bytes
17627        */
17628       if (mips_pic != NO_PIC)
17629         length += 6;
17630
17631       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17632
17633                         <brneg> 0f                      # 4 bytes
17634                         nop                             # 2 bytes if !compact
17635        */
17636       if (!uncond)
17637         length += (compact_known && compact) ? 4 : 6;
17638     }
17639
17640   return length;
17641 }
17642
17643 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17644    bit accordingly.  */
17645
17646 static int
17647 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17648 {
17649   bfd_boolean toofar;
17650
17651   if (fragp
17652       && S_IS_DEFINED (fragp->fr_symbol)
17653       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17654     {
17655       addressT addr;
17656       offsetT val;
17657       int type;
17658
17659       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17660       /* Ignore the low bit in the target, since it will be set
17661          for a text label.  */
17662       if ((val & 1) != 0)
17663         --val;
17664
17665       /* Assume this is a 2-byte branch.  */
17666       addr = fragp->fr_address + fragp->fr_fix + 2;
17667
17668       /* We try to avoid the infinite loop by not adding 2 more bytes for
17669          long branches.  */
17670
17671       val -= addr;
17672
17673       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17674       if (type == 'D')
17675         toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17676       else if (type == 'E')
17677         toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17678       else
17679         abort ();
17680     }
17681   else
17682     /* If the symbol is not defined or it's in a different segment,
17683        we emit a normal 32-bit branch.  */
17684     toofar = TRUE;
17685
17686   if (fragp && update
17687       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17688     fragp->fr_subtype
17689       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17690                : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17691
17692   if (toofar)
17693     return 4;
17694
17695   return 2;
17696 }
17697
17698 /* Estimate the size of a frag before relaxing.  Unless this is the
17699    mips16, we are not really relaxing here, and the final size is
17700    encoded in the subtype information.  For the mips16, we have to
17701    decide whether we are using an extended opcode or not.  */
17702
17703 int
17704 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17705 {
17706   int change;
17707
17708   if (RELAX_BRANCH_P (fragp->fr_subtype))
17709     {
17710
17711       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17712
17713       return fragp->fr_var;
17714     }
17715
17716   if (RELAX_MIPS16_P (fragp->fr_subtype))
17717     /* We don't want to modify the EXTENDED bit here; it might get us
17718        into infinite loops.  We change it only in mips_relax_frag().  */
17719     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17720
17721   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17722     {
17723       int length = 4;
17724
17725       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17726         length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17727       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17728         length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17729       fragp->fr_var = length;
17730
17731       return length;
17732     }
17733
17734   if (mips_pic == NO_PIC)
17735     change = nopic_need_relax (fragp->fr_symbol, 0);
17736   else if (mips_pic == SVR4_PIC)
17737     change = pic_need_relax (fragp->fr_symbol, segtype);
17738   else if (mips_pic == VXWORKS_PIC)
17739     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17740     change = 0;
17741   else
17742     abort ();
17743
17744   if (change)
17745     {
17746       fragp->fr_subtype |= RELAX_USE_SECOND;
17747       return -RELAX_FIRST (fragp->fr_subtype);
17748     }
17749   else
17750     return -RELAX_SECOND (fragp->fr_subtype);
17751 }
17752
17753 /* This is called to see whether a reloc against a defined symbol
17754    should be converted into a reloc against a section.  */
17755
17756 int
17757 mips_fix_adjustable (fixS *fixp)
17758 {
17759   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17760       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17761     return 0;
17762
17763   if (fixp->fx_addsy == NULL)
17764     return 1;
17765
17766   /* If symbol SYM is in a mergeable section, relocations of the form
17767      SYM + 0 can usually be made section-relative.  The mergeable data
17768      is then identified by the section offset rather than by the symbol.
17769
17770      However, if we're generating REL LO16 relocations, the offset is split
17771      between the LO16 and parterning high part relocation.  The linker will
17772      need to recalculate the complete offset in order to correctly identify
17773      the merge data.
17774
17775      The linker has traditionally not looked for the parterning high part
17776      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17777      placed anywhere.  Rather than break backwards compatibility by changing
17778      this, it seems better not to force the issue, and instead keep the
17779      original symbol.  This will work with either linker behavior.  */
17780   if ((lo16_reloc_p (fixp->fx_r_type)
17781        || reloc_needs_lo_p (fixp->fx_r_type))
17782       && HAVE_IN_PLACE_ADDENDS
17783       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17784     return 0;
17785
17786   /* There is no place to store an in-place offset for JALR relocations.
17787      Likewise an in-range offset of PC-relative relocations may overflow
17788      the in-place relocatable field if recalculated against the start
17789      address of the symbol's containing section.  */
17790   if (HAVE_IN_PLACE_ADDENDS
17791       && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
17792     return 0;
17793
17794 #ifdef OBJ_ELF
17795   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17796      to a floating-point stub.  The same is true for non-R_MIPS16_26
17797      relocations against MIPS16 functions; in this case, the stub becomes
17798      the function's canonical address.
17799
17800      Floating-point stubs are stored in unique .mips16.call.* or
17801      .mips16.fn.* sections.  If a stub T for function F is in section S,
17802      the first relocation in section S must be against F; this is how the
17803      linker determines the target function.  All relocations that might
17804      resolve to T must also be against F.  We therefore have the following
17805      restrictions, which are given in an intentionally-redundant way:
17806
17807        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17808           symbols.
17809
17810        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17811           if that stub might be used.
17812
17813        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17814           symbols.
17815
17816        4. We cannot reduce a stub's relocations against MIPS16 symbols if
17817           that stub might be used.
17818
17819      There is a further restriction:
17820
17821        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17822           R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17823           targets with in-place addends; the relocation field cannot
17824           encode the low bit.
17825
17826      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17827      against a MIPS16 symbol.  We deal with (5) by by not reducing any
17828      such relocations on REL targets.
17829
17830      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17831      relocation against some symbol R, no relocation against R may be
17832      reduced.  (Note that this deals with (2) as well as (1) because
17833      relocations against global symbols will never be reduced on ELF
17834      targets.)  This approach is a little simpler than trying to detect
17835      stub sections, and gives the "all or nothing" per-symbol consistency
17836      that we have for MIPS16 symbols.  */
17837   if (IS_ELF
17838       && fixp->fx_subsy == NULL
17839       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17840           || *symbol_get_tc (fixp->fx_addsy)
17841           || (HAVE_IN_PLACE_ADDENDS
17842               && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17843               && jmp_reloc_p (fixp->fx_r_type))))
17844     return 0;
17845 #endif
17846
17847   return 1;
17848 }
17849
17850 /* Translate internal representation of relocation info to BFD target
17851    format.  */
17852
17853 arelent **
17854 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17855 {
17856   static arelent *retval[4];
17857   arelent *reloc;
17858   bfd_reloc_code_real_type code;
17859
17860   memset (retval, 0, sizeof(retval));
17861   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17862   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17863   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17864   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17865
17866   if (fixp->fx_pcrel)
17867     {
17868       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17869                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17870                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17871                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
17872
17873       /* At this point, fx_addnumber is "symbol offset - pcrel address".
17874          Relocations want only the symbol offset.  */
17875       reloc->addend = fixp->fx_addnumber + reloc->address;
17876       if (!IS_ELF)
17877         {
17878           /* A gruesome hack which is a result of the gruesome gas
17879              reloc handling.  What's worse, for COFF (as opposed to
17880              ECOFF), we might need yet another copy of reloc->address.
17881              See bfd_install_relocation.  */
17882           reloc->addend += reloc->address;
17883         }
17884     }
17885   else
17886     reloc->addend = fixp->fx_addnumber;
17887
17888   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17889      entry to be used in the relocation's section offset.  */
17890   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17891     {
17892       reloc->address = reloc->addend;
17893       reloc->addend = 0;
17894     }
17895
17896   code = fixp->fx_r_type;
17897
17898   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17899   if (reloc->howto == NULL)
17900     {
17901       as_bad_where (fixp->fx_file, fixp->fx_line,
17902                     _("Can not represent %s relocation in this object file format"),
17903                     bfd_get_reloc_code_name (code));
17904       retval[0] = NULL;
17905     }
17906
17907   return retval;
17908 }
17909
17910 /* Relax a machine dependent frag.  This returns the amount by which
17911    the current size of the frag should change.  */
17912
17913 int
17914 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17915 {
17916   if (RELAX_BRANCH_P (fragp->fr_subtype))
17917     {
17918       offsetT old_var = fragp->fr_var;
17919
17920       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17921
17922       return fragp->fr_var - old_var;
17923     }
17924
17925   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17926     {
17927       offsetT old_var = fragp->fr_var;
17928       offsetT new_var = 4;
17929
17930       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17931         new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17932       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17933         new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17934       fragp->fr_var = new_var;
17935
17936       return new_var - old_var;
17937     }
17938
17939   if (! RELAX_MIPS16_P (fragp->fr_subtype))
17940     return 0;
17941
17942   if (mips16_extended_frag (fragp, NULL, stretch))
17943     {
17944       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17945         return 0;
17946       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17947       return 2;
17948     }
17949   else
17950     {
17951       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17952         return 0;
17953       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17954       return -2;
17955     }
17956
17957   return 0;
17958 }
17959
17960 /* Convert a machine dependent frag.  */
17961
17962 void
17963 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17964 {
17965   if (RELAX_BRANCH_P (fragp->fr_subtype))
17966     {
17967       char *buf;
17968       unsigned long insn;
17969       expressionS exp;
17970       fixS *fixp;
17971
17972       buf = fragp->fr_literal + fragp->fr_fix;
17973       insn = read_insn (buf);
17974
17975       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17976         {
17977           /* We generate a fixup instead of applying it right now
17978              because, if there are linker relaxations, we're going to
17979              need the relocations.  */
17980           exp.X_op = O_symbol;
17981           exp.X_add_symbol = fragp->fr_symbol;
17982           exp.X_add_number = fragp->fr_offset;
17983
17984           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17985                               BFD_RELOC_16_PCREL_S2);
17986           fixp->fx_file = fragp->fr_file;
17987           fixp->fx_line = fragp->fr_line;
17988
17989           buf = write_insn (buf, insn);
17990         }
17991       else
17992         {
17993           int i;
17994
17995           as_warn_where (fragp->fr_file, fragp->fr_line,
17996                          _("Relaxed out-of-range branch into a jump"));
17997
17998           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17999             goto uncond;
18000
18001           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18002             {
18003               /* Reverse the branch.  */
18004               switch ((insn >> 28) & 0xf)
18005                 {
18006                 case 4:
18007                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
18008                      have the condition reversed by tweaking a single
18009                      bit, and their opcodes all have 0x4???????.  */
18010                   gas_assert ((insn & 0xf1000000) == 0x41000000);
18011                   insn ^= 0x00010000;
18012                   break;
18013
18014                 case 0:
18015                   /* bltz       0x04000000      bgez    0x04010000
18016                      bltzal     0x04100000      bgezal  0x04110000  */
18017                   gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18018                   insn ^= 0x00010000;
18019                   break;
18020
18021                 case 1:
18022                   /* beq        0x10000000      bne     0x14000000
18023                      blez       0x18000000      bgtz    0x1c000000  */
18024                   insn ^= 0x04000000;
18025                   break;
18026
18027                 default:
18028                   abort ();
18029                 }
18030             }
18031
18032           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18033             {
18034               /* Clear the and-link bit.  */
18035               gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18036
18037               /* bltzal         0x04100000      bgezal  0x04110000
18038                  bltzall        0x04120000      bgezall 0x04130000  */
18039               insn &= ~0x00100000;
18040             }
18041
18042           /* Branch over the branch (if the branch was likely) or the
18043              full jump (not likely case).  Compute the offset from the
18044              current instruction to branch to.  */
18045           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18046             i = 16;
18047           else
18048             {
18049               /* How many bytes in instructions we've already emitted?  */
18050               i = buf - fragp->fr_literal - fragp->fr_fix;
18051               /* How many bytes in instructions from here to the end?  */
18052               i = fragp->fr_var - i;
18053             }
18054           /* Convert to instruction count.  */
18055           i >>= 2;
18056           /* Branch counts from the next instruction.  */
18057           i--;
18058           insn |= i;
18059           /* Branch over the jump.  */
18060           buf = write_insn (buf, insn);
18061
18062           /* nop */
18063           buf = write_insn (buf, 0);
18064
18065           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18066             {
18067               /* beql $0, $0, 2f */
18068               insn = 0x50000000;
18069               /* Compute the PC offset from the current instruction to
18070                  the end of the variable frag.  */
18071               /* How many bytes in instructions we've already emitted?  */
18072               i = buf - fragp->fr_literal - fragp->fr_fix;
18073               /* How many bytes in instructions from here to the end?  */
18074               i = fragp->fr_var - i;
18075               /* Convert to instruction count.  */
18076               i >>= 2;
18077               /* Don't decrement i, because we want to branch over the
18078                  delay slot.  */
18079               insn |= i;
18080
18081               buf = write_insn (buf, insn);
18082               buf = write_insn (buf, 0);
18083             }
18084
18085         uncond:
18086           if (mips_pic == NO_PIC)
18087             {
18088               /* j or jal.  */
18089               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18090                       ? 0x0c000000 : 0x08000000);
18091               exp.X_op = O_symbol;
18092               exp.X_add_symbol = fragp->fr_symbol;
18093               exp.X_add_number = fragp->fr_offset;
18094
18095               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18096                                   FALSE, BFD_RELOC_MIPS_JMP);
18097               fixp->fx_file = fragp->fr_file;
18098               fixp->fx_line = fragp->fr_line;
18099
18100               buf = write_insn (buf, insn);
18101             }
18102           else
18103             {
18104               unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18105
18106               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18107               insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18108               insn |= at << OP_SH_RT;
18109               exp.X_op = O_symbol;
18110               exp.X_add_symbol = fragp->fr_symbol;
18111               exp.X_add_number = fragp->fr_offset;
18112
18113               if (fragp->fr_offset)
18114                 {
18115                   exp.X_add_symbol = make_expr_symbol (&exp);
18116                   exp.X_add_number = 0;
18117                 }
18118
18119               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18120                                   FALSE, BFD_RELOC_MIPS_GOT16);
18121               fixp->fx_file = fragp->fr_file;
18122               fixp->fx_line = fragp->fr_line;
18123
18124               buf = write_insn (buf, insn);
18125
18126               if (mips_opts.isa == ISA_MIPS1)
18127                 /* nop */
18128                 buf = write_insn (buf, 0);
18129
18130               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18131               insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18132               insn |= at << OP_SH_RS | at << OP_SH_RT;
18133
18134               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18135                                   FALSE, BFD_RELOC_LO16);
18136               fixp->fx_file = fragp->fr_file;
18137               fixp->fx_line = fragp->fr_line;
18138
18139               buf = write_insn (buf, insn);
18140
18141               /* j(al)r $at.  */
18142               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18143                 insn = 0x0000f809;
18144               else
18145                 insn = 0x00000008;
18146               insn |= at << OP_SH_RS;
18147
18148               buf = write_insn (buf, insn);
18149             }
18150         }
18151
18152       fragp->fr_fix += fragp->fr_var;
18153       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18154       return;
18155     }
18156
18157   /* Relax microMIPS branches.  */
18158   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18159     {
18160       char *buf = fragp->fr_literal + fragp->fr_fix;
18161       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18162       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18163       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18164       bfd_boolean short_ds;
18165       unsigned long insn;
18166       expressionS exp;
18167       fixS *fixp;
18168
18169       exp.X_op = O_symbol;
18170       exp.X_add_symbol = fragp->fr_symbol;
18171       exp.X_add_number = fragp->fr_offset;
18172
18173       fragp->fr_fix += fragp->fr_var;
18174
18175       /* Handle 16-bit branches that fit or are forced to fit.  */
18176       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18177         {
18178           /* We generate a fixup instead of applying it right now,
18179              because if there is linker relaxation, we're going to
18180              need the relocations.  */
18181           if (type == 'D')
18182             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18183                                 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18184           else if (type == 'E')
18185             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18186                                 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18187           else
18188             abort ();
18189
18190           fixp->fx_file = fragp->fr_file;
18191           fixp->fx_line = fragp->fr_line;
18192
18193           /* These relocations can have an addend that won't fit in
18194              2 octets.  */
18195           fixp->fx_no_overflow = 1;
18196
18197           return;
18198         }
18199
18200       /* Handle 32-bit branches that fit or are forced to fit.  */
18201       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18202           || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18203         {
18204           /* We generate a fixup instead of applying it right now,
18205              because if there is linker relaxation, we're going to
18206              need the relocations.  */
18207           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18208                               BFD_RELOC_MICROMIPS_16_PCREL_S1);
18209           fixp->fx_file = fragp->fr_file;
18210           fixp->fx_line = fragp->fr_line;
18211
18212           if (type == 0)
18213             return;
18214         }
18215
18216       /* Relax 16-bit branches to 32-bit branches.  */
18217       if (type != 0)
18218         {
18219           insn = read_compressed_insn (buf, 2);
18220
18221           if ((insn & 0xfc00) == 0xcc00)                /* b16  */
18222             insn = 0x94000000;                          /* beq  */
18223           else if ((insn & 0xdc00) == 0x8c00)           /* beqz16/bnez16  */
18224             {
18225               unsigned long regno;
18226
18227               regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18228               regno = micromips_to_32_reg_d_map [regno];
18229               insn = ((insn & 0x2000) << 16) | 0x94000000;      /* beq/bne  */
18230               insn |= regno << MICROMIPSOP_SH_RS;
18231             }
18232           else
18233             abort ();
18234
18235           /* Nothing else to do, just write it out.  */
18236           if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18237               || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18238             {
18239               buf = write_compressed_insn (buf, insn, 4);
18240               gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18241               return;
18242             }
18243         }
18244       else
18245         insn = read_compressed_insn (buf, 4);
18246
18247       /* Relax 32-bit branches to a sequence of instructions.  */
18248       as_warn_where (fragp->fr_file, fragp->fr_line,
18249                      _("Relaxed out-of-range branch into a jump"));
18250
18251       /* Set the short-delay-slot bit.  */
18252       short_ds = al && (insn & 0x02000000) != 0;
18253
18254       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18255         {
18256           symbolS *l;
18257
18258           /* Reverse the branch.  */
18259           if ((insn & 0xfc000000) == 0x94000000                 /* beq  */
18260               || (insn & 0xfc000000) == 0xb4000000)             /* bne  */
18261             insn ^= 0x20000000;
18262           else if ((insn & 0xffe00000) == 0x40000000            /* bltz  */
18263                    || (insn & 0xffe00000) == 0x40400000         /* bgez  */
18264                    || (insn & 0xffe00000) == 0x40800000         /* blez  */
18265                    || (insn & 0xffe00000) == 0x40c00000         /* bgtz  */
18266                    || (insn & 0xffe00000) == 0x40a00000         /* bnezc  */
18267                    || (insn & 0xffe00000) == 0x40e00000         /* beqzc  */
18268                    || (insn & 0xffe00000) == 0x40200000         /* bltzal  */
18269                    || (insn & 0xffe00000) == 0x40600000         /* bgezal  */
18270                    || (insn & 0xffe00000) == 0x42200000         /* bltzals  */
18271                    || (insn & 0xffe00000) == 0x42600000)        /* bgezals  */
18272             insn ^= 0x00400000;
18273           else if ((insn & 0xffe30000) == 0x43800000            /* bc1f  */
18274                    || (insn & 0xffe30000) == 0x43a00000         /* bc1t  */
18275                    || (insn & 0xffe30000) == 0x42800000         /* bc2f  */
18276                    || (insn & 0xffe30000) == 0x42a00000)        /* bc2t  */
18277             insn ^= 0x00200000;
18278           else
18279             abort ();
18280
18281           if (al)
18282             {
18283               /* Clear the and-link and short-delay-slot bits.  */
18284               gas_assert ((insn & 0xfda00000) == 0x40200000);
18285
18286               /* bltzal  0x40200000     bgezal  0x40600000  */
18287               /* bltzals 0x42200000     bgezals 0x42600000  */
18288               insn &= ~0x02200000;
18289             }
18290
18291           /* Make a label at the end for use with the branch.  */
18292           l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18293           micromips_label_inc ();
18294 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18295           if (IS_ELF)
18296             S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18297 #endif
18298
18299           /* Refer to it.  */
18300           fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18301                           BFD_RELOC_MICROMIPS_16_PCREL_S1);
18302           fixp->fx_file = fragp->fr_file;
18303           fixp->fx_line = fragp->fr_line;
18304
18305           /* Branch over the jump.  */
18306           buf = write_compressed_insn (buf, insn, 4);
18307           if (!compact)
18308             /* nop */
18309             buf = write_compressed_insn (buf, 0x0c00, 2);
18310         }
18311
18312       if (mips_pic == NO_PIC)
18313         {
18314           unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
18315
18316           /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18317           insn = al ? jal : 0xd4000000;
18318
18319           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18320                               BFD_RELOC_MICROMIPS_JMP);
18321           fixp->fx_file = fragp->fr_file;
18322           fixp->fx_line = fragp->fr_line;
18323
18324           buf = write_compressed_insn (buf, insn, 4);
18325           if (compact)
18326             /* nop */
18327             buf = write_compressed_insn (buf, 0x0c00, 2);
18328         }
18329       else
18330         {
18331           unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18332           unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;      /* jalr/s  */
18333           unsigned long jr = compact ? 0x45a0 : 0x4580;         /* jr/c  */
18334
18335           /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18336           insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18337           insn |= at << MICROMIPSOP_SH_RT;
18338
18339           if (exp.X_add_number)
18340             {
18341               exp.X_add_symbol = make_expr_symbol (&exp);
18342               exp.X_add_number = 0;
18343             }
18344
18345           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18346                               BFD_RELOC_MICROMIPS_GOT16);
18347           fixp->fx_file = fragp->fr_file;
18348           fixp->fx_line = fragp->fr_line;
18349
18350           buf = write_compressed_insn (buf, insn, 4);
18351
18352           /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18353           insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18354           insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18355
18356           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18357                               BFD_RELOC_MICROMIPS_LO16);
18358           fixp->fx_file = fragp->fr_file;
18359           fixp->fx_line = fragp->fr_line;
18360
18361           buf = write_compressed_insn (buf, insn, 4);
18362
18363           /* jr/jrc/jalr/jalrs $at  */
18364           insn = al ? jalr : jr;
18365           insn |= at << MICROMIPSOP_SH_MJ;
18366
18367           buf = write_compressed_insn (buf, insn, 2);
18368         }
18369
18370       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18371       return;
18372     }
18373
18374   if (RELAX_MIPS16_P (fragp->fr_subtype))
18375     {
18376       int type;
18377       const struct mips16_immed_operand *op;
18378       offsetT val;
18379       char *buf;
18380       unsigned int user_length, length;
18381       unsigned long insn;
18382       bfd_boolean ext;
18383
18384       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18385       op = mips16_immed_operands;
18386       while (op->type != type)
18387         ++op;
18388
18389       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18390       val = resolve_symbol_value (fragp->fr_symbol);
18391       if (op->pcrel)
18392         {
18393           addressT addr;
18394
18395           addr = fragp->fr_address + fragp->fr_fix;
18396
18397           /* The rules for the base address of a PC relative reloc are
18398              complicated; see mips16_extended_frag.  */
18399           if (type == 'p' || type == 'q')
18400             {
18401               addr += 2;
18402               if (ext)
18403                 addr += 2;
18404               /* Ignore the low bit in the target, since it will be
18405                  set for a text label.  */
18406               if ((val & 1) != 0)
18407                 --val;
18408             }
18409           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18410             addr -= 4;
18411           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18412             addr -= 2;
18413
18414           addr &= ~ (addressT) ((1 << op->shift) - 1);
18415           val -= addr;
18416
18417           /* Make sure the section winds up with the alignment we have
18418              assumed.  */
18419           if (op->shift > 0)
18420             record_alignment (asec, op->shift);
18421         }
18422
18423       if (ext
18424           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18425               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18426         as_warn_where (fragp->fr_file, fragp->fr_line,
18427                        _("extended instruction in delay slot"));
18428
18429       buf = fragp->fr_literal + fragp->fr_fix;
18430
18431       insn = read_compressed_insn (buf, 2);
18432       if (ext)
18433         insn |= MIPS16_EXTEND;
18434
18435       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18436         user_length = 4;
18437       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18438         user_length = 2;
18439       else
18440         user_length = 0;
18441
18442       mips16_immed (fragp->fr_file, fragp->fr_line, type,
18443                     BFD_RELOC_UNUSED, val, user_length, &insn);
18444
18445       length = (ext ? 4 : 2);
18446       gas_assert (mips16_opcode_length (insn) == length);
18447       write_compressed_insn (buf, insn, length);
18448       fragp->fr_fix += length;
18449     }
18450   else
18451     {
18452       relax_substateT subtype = fragp->fr_subtype;
18453       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18454       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18455       int first, second;
18456       fixS *fixp;
18457
18458       first = RELAX_FIRST (subtype);
18459       second = RELAX_SECOND (subtype);
18460       fixp = (fixS *) fragp->fr_opcode;
18461
18462       /* If the delay slot chosen does not match the size of the instruction,
18463          then emit a warning.  */
18464       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18465            || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18466         {
18467           relax_substateT s;
18468           const char *msg;
18469
18470           s = subtype & (RELAX_DELAY_SLOT_16BIT
18471                          | RELAX_DELAY_SLOT_SIZE_FIRST
18472                          | RELAX_DELAY_SLOT_SIZE_SECOND);
18473           msg = macro_warning (s);
18474           if (msg != NULL)
18475             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18476           subtype &= ~s;
18477         }
18478
18479       /* Possibly emit a warning if we've chosen the longer option.  */
18480       if (use_second == second_longer)
18481         {
18482           relax_substateT s;
18483           const char *msg;
18484
18485           s = (subtype
18486                & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18487           msg = macro_warning (s);
18488           if (msg != NULL)
18489             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18490           subtype &= ~s;
18491         }
18492
18493       /* Go through all the fixups for the first sequence.  Disable them
18494          (by marking them as done) if we're going to use the second
18495          sequence instead.  */
18496       while (fixp
18497              && fixp->fx_frag == fragp
18498              && fixp->fx_where < fragp->fr_fix - second)
18499         {
18500           if (subtype & RELAX_USE_SECOND)
18501             fixp->fx_done = 1;
18502           fixp = fixp->fx_next;
18503         }
18504
18505       /* Go through the fixups for the second sequence.  Disable them if
18506          we're going to use the first sequence, otherwise adjust their
18507          addresses to account for the relaxation.  */
18508       while (fixp && fixp->fx_frag == fragp)
18509         {
18510           if (subtype & RELAX_USE_SECOND)
18511             fixp->fx_where -= first;
18512           else
18513             fixp->fx_done = 1;
18514           fixp = fixp->fx_next;
18515         }
18516
18517       /* Now modify the frag contents.  */
18518       if (subtype & RELAX_USE_SECOND)
18519         {
18520           char *start;
18521
18522           start = fragp->fr_literal + fragp->fr_fix - first - second;
18523           memmove (start, start + first, second);
18524           fragp->fr_fix -= first;
18525         }
18526       else
18527         fragp->fr_fix -= second;
18528     }
18529 }
18530
18531 #ifdef OBJ_ELF
18532
18533 /* This function is called after the relocs have been generated.
18534    We've been storing mips16 text labels as odd.  Here we convert them
18535    back to even for the convenience of the debugger.  */
18536
18537 void
18538 mips_frob_file_after_relocs (void)
18539 {
18540   asymbol **syms;
18541   unsigned int count, i;
18542
18543   if (!IS_ELF)
18544     return;
18545
18546   syms = bfd_get_outsymbols (stdoutput);
18547   count = bfd_get_symcount (stdoutput);
18548   for (i = 0; i < count; i++, syms++)
18549     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18550         && ((*syms)->value & 1) != 0)
18551       {
18552         (*syms)->value &= ~1;
18553         /* If the symbol has an odd size, it was probably computed
18554            incorrectly, so adjust that as well.  */
18555         if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18556           ++elf_symbol (*syms)->internal_elf_sym.st_size;
18557       }
18558 }
18559
18560 #endif
18561
18562 /* This function is called whenever a label is defined, including fake
18563    labels instantiated off the dot special symbol.  It is used when
18564    handling branch delays; if a branch has a label, we assume we cannot
18565    move it.  This also bumps the value of the symbol by 1 in compressed
18566    code.  */
18567
18568 static void
18569 mips_record_label (symbolS *sym)
18570 {
18571   segment_info_type *si = seg_info (now_seg);
18572   struct insn_label_list *l;
18573
18574   if (free_insn_labels == NULL)
18575     l = (struct insn_label_list *) xmalloc (sizeof *l);
18576   else
18577     {
18578       l = free_insn_labels;
18579       free_insn_labels = l->next;
18580     }
18581
18582   l->label = sym;
18583   l->next = si->label_list;
18584   si->label_list = l;
18585 }
18586
18587 /* This function is called as tc_frob_label() whenever a label is defined
18588    and adds a DWARF-2 record we only want for true labels.  */
18589
18590 void
18591 mips_define_label (symbolS *sym)
18592 {
18593   mips_record_label (sym);
18594 #ifdef OBJ_ELF
18595   dwarf2_emit_label (sym);
18596 #endif
18597 }
18598
18599 /* This function is called by tc_new_dot_label whenever a new dot symbol
18600    is defined.  */
18601
18602 void
18603 mips_add_dot_label (symbolS *sym)
18604 {
18605   mips_record_label (sym);
18606   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18607     mips_compressed_mark_label (sym);
18608 }
18609 \f
18610 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18611
18612 /* Some special processing for a MIPS ELF file.  */
18613
18614 void
18615 mips_elf_final_processing (void)
18616 {
18617   /* Write out the register information.  */
18618   if (mips_abi != N64_ABI)
18619     {
18620       Elf32_RegInfo s;
18621
18622       s.ri_gprmask = mips_gprmask;
18623       s.ri_cprmask[0] = mips_cprmask[0];
18624       s.ri_cprmask[1] = mips_cprmask[1];
18625       s.ri_cprmask[2] = mips_cprmask[2];
18626       s.ri_cprmask[3] = mips_cprmask[3];
18627       /* The gp_value field is set by the MIPS ELF backend.  */
18628
18629       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18630                                        ((Elf32_External_RegInfo *)
18631                                         mips_regmask_frag));
18632     }
18633   else
18634     {
18635       Elf64_Internal_RegInfo s;
18636
18637       s.ri_gprmask = mips_gprmask;
18638       s.ri_pad = 0;
18639       s.ri_cprmask[0] = mips_cprmask[0];
18640       s.ri_cprmask[1] = mips_cprmask[1];
18641       s.ri_cprmask[2] = mips_cprmask[2];
18642       s.ri_cprmask[3] = mips_cprmask[3];
18643       /* The gp_value field is set by the MIPS ELF backend.  */
18644
18645       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18646                                        ((Elf64_External_RegInfo *)
18647                                         mips_regmask_frag));
18648     }
18649
18650   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
18651      sort of BFD interface for this.  */
18652   if (mips_any_noreorder)
18653     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18654   if (mips_pic != NO_PIC)
18655     {
18656       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18657       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18658     }
18659   if (mips_abicalls)
18660     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18661
18662   /* Set MIPS ELF flags for ASEs.  */
18663   /* We may need to define a new flag for DSP ASE, and set this flag when
18664      file_ase_dsp is true.  */
18665   /* Same for DSP R2.  */
18666   /* We may need to define a new flag for MT ASE, and set this flag when
18667      file_ase_mt is true.  */
18668   if (file_ase_mips16)
18669     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18670   if (file_ase_micromips)
18671     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18672 #if 0 /* XXX FIXME */
18673   if (file_ase_mips3d)
18674     elf_elfheader (stdoutput)->e_flags |= ???;
18675 #endif
18676   if (file_ase_mdmx)
18677     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18678
18679   /* Set the MIPS ELF ABI flags.  */
18680   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18681     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18682   else if (mips_abi == O64_ABI)
18683     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18684   else if (mips_abi == EABI_ABI)
18685     {
18686       if (!file_mips_gp32)
18687         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18688       else
18689         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18690     }
18691   else if (mips_abi == N32_ABI)
18692     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18693
18694   /* Nothing to do for N64_ABI.  */
18695
18696   if (mips_32bitmode)
18697     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18698
18699 #if 0 /* XXX FIXME */
18700   /* 32 bit code with 64 bit FP registers.  */
18701   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18702     elf_elfheader (stdoutput)->e_flags |= ???;
18703 #endif
18704 }
18705
18706 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18707 \f
18708 typedef struct proc {
18709   symbolS *func_sym;
18710   symbolS *func_end_sym;
18711   unsigned long reg_mask;
18712   unsigned long reg_offset;
18713   unsigned long fpreg_mask;
18714   unsigned long fpreg_offset;
18715   unsigned long frame_offset;
18716   unsigned long frame_reg;
18717   unsigned long pc_reg;
18718 } procS;
18719
18720 static procS cur_proc;
18721 static procS *cur_proc_ptr;
18722 static int numprocs;
18723
18724 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
18725    as "2", and a normal nop as "0".  */
18726
18727 #define NOP_OPCODE_MIPS         0
18728 #define NOP_OPCODE_MIPS16       1
18729 #define NOP_OPCODE_MICROMIPS    2
18730
18731 char
18732 mips_nop_opcode (void)
18733 {
18734   if (seg_info (now_seg)->tc_segment_info_data.micromips)
18735     return NOP_OPCODE_MICROMIPS;
18736   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18737     return NOP_OPCODE_MIPS16;
18738   else
18739     return NOP_OPCODE_MIPS;
18740 }
18741
18742 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
18743    32-bit microMIPS NOPs here (if applicable).  */
18744
18745 void
18746 mips_handle_align (fragS *fragp)
18747 {
18748   char nop_opcode;
18749   char *p;
18750   int bytes, size, excess;
18751   valueT opcode;
18752
18753   if (fragp->fr_type != rs_align_code)
18754     return;
18755
18756   p = fragp->fr_literal + fragp->fr_fix;
18757   nop_opcode = *p;
18758   switch (nop_opcode)
18759     {
18760     case NOP_OPCODE_MICROMIPS:
18761       opcode = micromips_nop32_insn.insn_opcode;
18762       size = 4;
18763       break;
18764     case NOP_OPCODE_MIPS16:
18765       opcode = mips16_nop_insn.insn_opcode;
18766       size = 2;
18767       break;
18768     case NOP_OPCODE_MIPS:
18769     default:
18770       opcode = nop_insn.insn_opcode;
18771       size = 4;
18772       break;
18773     }
18774
18775   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18776   excess = bytes % size;
18777
18778   /* Handle the leading part if we're not inserting a whole number of
18779      instructions, and make it the end of the fixed part of the frag.
18780      Try to fit in a short microMIPS NOP if applicable and possible,
18781      and use zeroes otherwise.  */
18782   gas_assert (excess < 4);
18783   fragp->fr_fix += excess;
18784   switch (excess)
18785     {
18786     case 3:
18787       *p++ = '\0';
18788       /* Fall through.  */
18789     case 2:
18790       if (nop_opcode == NOP_OPCODE_MICROMIPS)
18791         {
18792           p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18793           break;
18794         }
18795       *p++ = '\0';
18796       /* Fall through.  */
18797     case 1:
18798       *p++ = '\0';
18799       /* Fall through.  */
18800     case 0:
18801       break;
18802     }
18803
18804   md_number_to_chars (p, opcode, size);
18805   fragp->fr_var = size;
18806 }
18807
18808 static void
18809 md_obj_begin (void)
18810 {
18811 }
18812
18813 static void
18814 md_obj_end (void)
18815 {
18816   /* Check for premature end, nesting errors, etc.  */
18817   if (cur_proc_ptr)
18818     as_warn (_("missing .end at end of assembly"));
18819 }
18820
18821 static long
18822 get_number (void)
18823 {
18824   int negative = 0;
18825   long val = 0;
18826
18827   if (*input_line_pointer == '-')
18828     {
18829       ++input_line_pointer;
18830       negative = 1;
18831     }
18832   if (!ISDIGIT (*input_line_pointer))
18833     as_bad (_("expected simple number"));
18834   if (input_line_pointer[0] == '0')
18835     {
18836       if (input_line_pointer[1] == 'x')
18837         {
18838           input_line_pointer += 2;
18839           while (ISXDIGIT (*input_line_pointer))
18840             {
18841               val <<= 4;
18842               val |= hex_value (*input_line_pointer++);
18843             }
18844           return negative ? -val : val;
18845         }
18846       else
18847         {
18848           ++input_line_pointer;
18849           while (ISDIGIT (*input_line_pointer))
18850             {
18851               val <<= 3;
18852               val |= *input_line_pointer++ - '0';
18853             }
18854           return negative ? -val : val;
18855         }
18856     }
18857   if (!ISDIGIT (*input_line_pointer))
18858     {
18859       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18860               *input_line_pointer, *input_line_pointer);
18861       as_warn (_("invalid number"));
18862       return -1;
18863     }
18864   while (ISDIGIT (*input_line_pointer))
18865     {
18866       val *= 10;
18867       val += *input_line_pointer++ - '0';
18868     }
18869   return negative ? -val : val;
18870 }
18871
18872 /* The .file directive; just like the usual .file directive, but there
18873    is an initial number which is the ECOFF file index.  In the non-ECOFF
18874    case .file implies DWARF-2.  */
18875
18876 static void
18877 s_mips_file (int x ATTRIBUTE_UNUSED)
18878 {
18879   static int first_file_directive = 0;
18880
18881   if (ECOFF_DEBUGGING)
18882     {
18883       get_number ();
18884       s_app_file (0);
18885     }
18886   else
18887     {
18888       char *filename;
18889
18890       filename = dwarf2_directive_file (0);
18891
18892       /* Versions of GCC up to 3.1 start files with a ".file"
18893          directive even for stabs output.  Make sure that this
18894          ".file" is handled.  Note that you need a version of GCC
18895          after 3.1 in order to support DWARF-2 on MIPS.  */
18896       if (filename != NULL && ! first_file_directive)
18897         {
18898           (void) new_logical_line (filename, -1);
18899           s_app_file_string (filename, 0);
18900         }
18901       first_file_directive = 1;
18902     }
18903 }
18904
18905 /* The .loc directive, implying DWARF-2.  */
18906
18907 static void
18908 s_mips_loc (int x ATTRIBUTE_UNUSED)
18909 {
18910   if (!ECOFF_DEBUGGING)
18911     dwarf2_directive_loc (0);
18912 }
18913
18914 /* The .end directive.  */
18915
18916 static void
18917 s_mips_end (int x ATTRIBUTE_UNUSED)
18918 {
18919   symbolS *p;
18920
18921   /* Following functions need their own .frame and .cprestore directives.  */
18922   mips_frame_reg_valid = 0;
18923   mips_cprestore_valid = 0;
18924
18925   if (!is_end_of_line[(unsigned char) *input_line_pointer])
18926     {
18927       p = get_symbol ();
18928       demand_empty_rest_of_line ();
18929     }
18930   else
18931     p = NULL;
18932
18933   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18934     as_warn (_(".end not in text section"));
18935
18936   if (!cur_proc_ptr)
18937     {
18938       as_warn (_(".end directive without a preceding .ent directive."));
18939       demand_empty_rest_of_line ();
18940       return;
18941     }
18942
18943   if (p != NULL)
18944     {
18945       gas_assert (S_GET_NAME (p));
18946       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18947         as_warn (_(".end symbol does not match .ent symbol."));
18948
18949       if (debug_type == DEBUG_STABS)
18950         stabs_generate_asm_endfunc (S_GET_NAME (p),
18951                                     S_GET_NAME (p));
18952     }
18953   else
18954     as_warn (_(".end directive missing or unknown symbol"));
18955
18956 #ifdef OBJ_ELF
18957   /* Create an expression to calculate the size of the function.  */
18958   if (p && cur_proc_ptr)
18959     {
18960       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18961       expressionS *exp = xmalloc (sizeof (expressionS));
18962
18963       obj->size = exp;
18964       exp->X_op = O_subtract;
18965       exp->X_add_symbol = symbol_temp_new_now ();
18966       exp->X_op_symbol = p;
18967       exp->X_add_number = 0;
18968
18969       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18970     }
18971
18972   /* Generate a .pdr section.  */
18973   if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
18974     {
18975       segT saved_seg = now_seg;
18976       subsegT saved_subseg = now_subseg;
18977       expressionS exp;
18978       char *fragp;
18979
18980 #ifdef md_flush_pending_output
18981       md_flush_pending_output ();
18982 #endif
18983
18984       gas_assert (pdr_seg);
18985       subseg_set (pdr_seg, 0);
18986
18987       /* Write the symbol.  */
18988       exp.X_op = O_symbol;
18989       exp.X_add_symbol = p;
18990       exp.X_add_number = 0;
18991       emit_expr (&exp, 4);
18992
18993       fragp = frag_more (7 * 4);
18994
18995       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18996       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18997       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18998       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
18999       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19000       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19001       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19002
19003       subseg_set (saved_seg, saved_subseg);
19004     }
19005 #endif /* OBJ_ELF */
19006
19007   cur_proc_ptr = NULL;
19008 }
19009
19010 /* The .aent and .ent directives.  */
19011
19012 static void
19013 s_mips_ent (int aent)
19014 {
19015   symbolS *symbolP;
19016
19017   symbolP = get_symbol ();
19018   if (*input_line_pointer == ',')
19019     ++input_line_pointer;
19020   SKIP_WHITESPACE ();
19021   if (ISDIGIT (*input_line_pointer)
19022       || *input_line_pointer == '-')
19023     get_number ();
19024
19025   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19026     as_warn (_(".ent or .aent not in text section."));
19027
19028   if (!aent && cur_proc_ptr)
19029     as_warn (_("missing .end"));
19030
19031   if (!aent)
19032     {
19033       /* This function needs its own .frame and .cprestore directives.  */
19034       mips_frame_reg_valid = 0;
19035       mips_cprestore_valid = 0;
19036
19037       cur_proc_ptr = &cur_proc;
19038       memset (cur_proc_ptr, '\0', sizeof (procS));
19039
19040       cur_proc_ptr->func_sym = symbolP;
19041
19042       ++numprocs;
19043
19044       if (debug_type == DEBUG_STABS)
19045         stabs_generate_asm_func (S_GET_NAME (symbolP),
19046                                  S_GET_NAME (symbolP));
19047     }
19048
19049   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19050
19051   demand_empty_rest_of_line ();
19052 }
19053
19054 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19055    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19056    s_mips_frame is used so that we can set the PDR information correctly.
19057    We can't use the ecoff routines because they make reference to the ecoff
19058    symbol table (in the mdebug section).  */
19059
19060 static void
19061 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19062 {
19063 #ifdef OBJ_ELF
19064   if (IS_ELF && !ECOFF_DEBUGGING)
19065     {
19066       long val;
19067
19068       if (cur_proc_ptr == (procS *) NULL)
19069         {
19070           as_warn (_(".frame outside of .ent"));
19071           demand_empty_rest_of_line ();
19072           return;
19073         }
19074
19075       cur_proc_ptr->frame_reg = tc_get_register (1);
19076
19077       SKIP_WHITESPACE ();
19078       if (*input_line_pointer++ != ','
19079           || get_absolute_expression_and_terminator (&val) != ',')
19080         {
19081           as_warn (_("Bad .frame directive"));
19082           --input_line_pointer;
19083           demand_empty_rest_of_line ();
19084           return;
19085         }
19086
19087       cur_proc_ptr->frame_offset = val;
19088       cur_proc_ptr->pc_reg = tc_get_register (0);
19089
19090       demand_empty_rest_of_line ();
19091     }
19092   else
19093 #endif /* OBJ_ELF */
19094     s_ignore (ignore);
19095 }
19096
19097 /* The .fmask and .mask directives. If the mdebug section is present
19098    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19099    embedded targets, s_mips_mask is used so that we can set the PDR
19100    information correctly. We can't use the ecoff routines because they
19101    make reference to the ecoff symbol table (in the mdebug section).  */
19102
19103 static void
19104 s_mips_mask (int reg_type)
19105 {
19106 #ifdef OBJ_ELF
19107   if (IS_ELF && !ECOFF_DEBUGGING)
19108     {
19109       long mask, off;
19110
19111       if (cur_proc_ptr == (procS *) NULL)
19112         {
19113           as_warn (_(".mask/.fmask outside of .ent"));
19114           demand_empty_rest_of_line ();
19115           return;
19116         }
19117
19118       if (get_absolute_expression_and_terminator (&mask) != ',')
19119         {
19120           as_warn (_("Bad .mask/.fmask directive"));
19121           --input_line_pointer;
19122           demand_empty_rest_of_line ();
19123           return;
19124         }
19125
19126       off = get_absolute_expression ();
19127
19128       if (reg_type == 'F')
19129         {
19130           cur_proc_ptr->fpreg_mask = mask;
19131           cur_proc_ptr->fpreg_offset = off;
19132         }
19133       else
19134         {
19135           cur_proc_ptr->reg_mask = mask;
19136           cur_proc_ptr->reg_offset = off;
19137         }
19138
19139       demand_empty_rest_of_line ();
19140     }
19141   else
19142 #endif /* OBJ_ELF */
19143     s_ignore (reg_type);
19144 }
19145
19146 /* A table describing all the processors gas knows about.  Names are
19147    matched in the order listed.
19148
19149    To ease comparison, please keep this table in the same order as
19150    gcc's mips_cpu_info_table[].  */
19151 static const struct mips_cpu_info mips_cpu_info_table[] =
19152 {
19153   /* Entries for generic ISAs */
19154   { "mips1",          MIPS_CPU_IS_ISA,          ISA_MIPS1,      CPU_R3000 },
19155   { "mips2",          MIPS_CPU_IS_ISA,          ISA_MIPS2,      CPU_R6000 },
19156   { "mips3",          MIPS_CPU_IS_ISA,          ISA_MIPS3,      CPU_R4000 },
19157   { "mips4",          MIPS_CPU_IS_ISA,          ISA_MIPS4,      CPU_R8000 },
19158   { "mips5",          MIPS_CPU_IS_ISA,          ISA_MIPS5,      CPU_MIPS5 },
19159   { "mips32",         MIPS_CPU_IS_ISA,          ISA_MIPS32,     CPU_MIPS32 },
19160   { "mips32r2",       MIPS_CPU_IS_ISA,          ISA_MIPS32R2,   CPU_MIPS32R2 },
19161   { "mips64",         MIPS_CPU_IS_ISA,          ISA_MIPS64,     CPU_MIPS64 },
19162   { "mips64r2",       MIPS_CPU_IS_ISA,          ISA_MIPS64R2,   CPU_MIPS64R2 },
19163
19164   /* MIPS I */
19165   { "r3000",          0,                        ISA_MIPS1,      CPU_R3000 },
19166   { "r2000",          0,                        ISA_MIPS1,      CPU_R3000 },
19167   { "r3900",          0,                        ISA_MIPS1,      CPU_R3900 },
19168
19169   /* MIPS II */
19170   { "r6000",          0,                        ISA_MIPS2,      CPU_R6000 },
19171
19172   /* MIPS III */
19173   { "r4000",          0,                        ISA_MIPS3,      CPU_R4000 },
19174   { "r4010",          0,                        ISA_MIPS2,      CPU_R4010 },
19175   { "vr4100",         0,                        ISA_MIPS3,      CPU_VR4100 },
19176   { "vr4111",         0,                        ISA_MIPS3,      CPU_R4111 },
19177   { "vr4120",         0,                        ISA_MIPS3,      CPU_VR4120 },
19178   { "vr4130",         0,                        ISA_MIPS3,      CPU_VR4120 },
19179   { "vr4181",         0,                        ISA_MIPS3,      CPU_R4111 },
19180   { "vr4300",         0,                        ISA_MIPS3,      CPU_R4300 },
19181   { "r4400",          0,                        ISA_MIPS3,      CPU_R4400 },
19182   { "r4600",          0,                        ISA_MIPS3,      CPU_R4600 },
19183   { "orion",          0,                        ISA_MIPS3,      CPU_R4600 },
19184   { "r4650",          0,                        ISA_MIPS3,      CPU_R4650 },
19185   { "r5900",          0,                        ISA_MIPS3,      CPU_R5900 },
19186   /* ST Microelectronics Loongson 2E and 2F cores */
19187   { "loongson2e",     0,                        ISA_MIPS3,   CPU_LOONGSON_2E },
19188   { "loongson2f",     0,                        ISA_MIPS3,   CPU_LOONGSON_2F },
19189
19190   /* MIPS IV */
19191   { "r8000",          0,                        ISA_MIPS4,      CPU_R8000 },
19192   { "r10000",         0,                        ISA_MIPS4,      CPU_R10000 },
19193   { "r12000",         0,                        ISA_MIPS4,      CPU_R12000 },
19194   { "r14000",         0,                        ISA_MIPS4,      CPU_R14000 },
19195   { "r16000",         0,                        ISA_MIPS4,      CPU_R16000 },
19196   { "vr5000",         0,                        ISA_MIPS4,      CPU_R5000 },
19197   { "vr5400",         0,                        ISA_MIPS4,      CPU_VR5400 },
19198   { "vr5500",         0,                        ISA_MIPS4,      CPU_VR5500 },
19199   { "rm5200",         0,                        ISA_MIPS4,      CPU_R5000 },
19200   { "rm5230",         0,                        ISA_MIPS4,      CPU_R5000 },
19201   { "rm5231",         0,                        ISA_MIPS4,      CPU_R5000 },
19202   { "rm5261",         0,                        ISA_MIPS4,      CPU_R5000 },
19203   { "rm5721",         0,                        ISA_MIPS4,      CPU_R5000 },
19204   { "rm7000",         0,                        ISA_MIPS4,      CPU_RM7000 },
19205   { "rm9000",         0,                        ISA_MIPS4,      CPU_RM9000 },
19206
19207   /* MIPS 32 */
19208   { "4kc",            0,                        ISA_MIPS32,     CPU_MIPS32 },
19209   { "4km",            0,                        ISA_MIPS32,     CPU_MIPS32 },
19210   { "4kp",            0,                        ISA_MIPS32,     CPU_MIPS32 },
19211   { "4ksc",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32,     CPU_MIPS32 },
19212
19213   /* MIPS 32 Release 2 */
19214   { "4kec",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19215   { "4kem",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19216   { "4kep",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19217   { "4ksd",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32R2,   CPU_MIPS32R2 },
19218   { "m4k",            0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19219   { "m4kp",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19220   { "m14k",           MIPS_CPU_ASE_MCU,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19221   { "m14kc",          MIPS_CPU_ASE_MCU,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19222   { "m14ke",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19223                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19224   { "m14kec",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19225                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19226   { "24kc",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19227   { "24kf2_1",        0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19228   { "24kf",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19229   { "24kf1_1",        0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19230   /* Deprecated forms of the above.  */
19231   { "24kfx",          0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19232   { "24kx",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19233   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
19234   { "24kec",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19235   { "24kef2_1",       MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19236   { "24kef",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19237   { "24kef1_1",       MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19238   /* Deprecated forms of the above.  */
19239   { "24kefx",         MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19240   { "24kex",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19241   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
19242   { "34kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19243                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19244   { "34kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19245                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19246   { "34kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19247                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19248   { "34kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19249                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19250   /* Deprecated forms of the above.  */
19251   { "34kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19252                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19253   { "34kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19254                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19255   /* 34Kn is a 34kc without DSP.  */
19256   { "34kn",           MIPS_CPU_ASE_MT,          ISA_MIPS32R2,   CPU_MIPS32R2 },
19257   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
19258   { "74kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19259                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19260   { "74kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19261                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19262   { "74kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19263                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19264   { "74kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19265                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19266   { "74kf3_2",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19267                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19268   /* Deprecated forms of the above.  */
19269   { "74kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19270                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19271   { "74kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19272                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19273   /* 1004K cores are multiprocessor versions of the 34K.  */
19274   { "1004kc",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19275                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19276   { "1004kf2_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19277                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19278   { "1004kf",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19279                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19280   { "1004kf1_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19281                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19282
19283   /* MIPS 64 */
19284   { "5kc",            0,                        ISA_MIPS64,     CPU_MIPS64 },
19285   { "5kf",            0,                        ISA_MIPS64,     CPU_MIPS64 },
19286   { "20kc",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64,     CPU_MIPS64 },
19287   { "25kf",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64,     CPU_MIPS64 },
19288
19289   /* Broadcom SB-1 CPU core */
19290   { "sb1",            MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19291                                                 ISA_MIPS64,     CPU_SB1 },
19292   /* Broadcom SB-1A CPU core */
19293   { "sb1a",           MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19294                                                 ISA_MIPS64,     CPU_SB1 },
19295   
19296   { "loongson3a",     0,                        ISA_MIPS64,     CPU_LOONGSON_3A },
19297
19298   /* MIPS 64 Release 2 */
19299
19300   /* Cavium Networks Octeon CPU core */
19301   { "octeon",         0,      ISA_MIPS64R2,   CPU_OCTEON },
19302   { "octeon+",        0,      ISA_MIPS64R2,   CPU_OCTEONP },
19303   { "octeon2",        0,      ISA_MIPS64R2,   CPU_OCTEON2 },
19304
19305   /* RMI Xlr */
19306   { "xlr",            0,      ISA_MIPS64,     CPU_XLR },
19307
19308   /* Broadcom XLP.
19309      XLP is mostly like XLR, with the prominent exception that it is
19310      MIPS64R2 rather than MIPS64.  */
19311   { "xlp",            0,      ISA_MIPS64R2,     CPU_XLR },
19312
19313   /* End marker */
19314   { NULL, 0, 0, 0 }
19315 };
19316
19317
19318 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19319    with a final "000" replaced by "k".  Ignore case.
19320
19321    Note: this function is shared between GCC and GAS.  */
19322
19323 static bfd_boolean
19324 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19325 {
19326   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19327     given++, canonical++;
19328
19329   return ((*given == 0 && *canonical == 0)
19330           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19331 }
19332
19333
19334 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19335    CPU name.  We've traditionally allowed a lot of variation here.
19336
19337    Note: this function is shared between GCC and GAS.  */
19338
19339 static bfd_boolean
19340 mips_matching_cpu_name_p (const char *canonical, const char *given)
19341 {
19342   /* First see if the name matches exactly, or with a final "000"
19343      turned into "k".  */
19344   if (mips_strict_matching_cpu_name_p (canonical, given))
19345     return TRUE;
19346
19347   /* If not, try comparing based on numerical designation alone.
19348      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
19349   if (TOLOWER (*given) == 'r')
19350     given++;
19351   if (!ISDIGIT (*given))
19352     return FALSE;
19353
19354   /* Skip over some well-known prefixes in the canonical name,
19355      hoping to find a number there too.  */
19356   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19357     canonical += 2;
19358   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19359     canonical += 2;
19360   else if (TOLOWER (canonical[0]) == 'r')
19361     canonical += 1;
19362
19363   return mips_strict_matching_cpu_name_p (canonical, given);
19364 }
19365
19366
19367 /* Parse an option that takes the name of a processor as its argument.
19368    OPTION is the name of the option and CPU_STRING is the argument.
19369    Return the corresponding processor enumeration if the CPU_STRING is
19370    recognized, otherwise report an error and return null.
19371
19372    A similar function exists in GCC.  */
19373
19374 static const struct mips_cpu_info *
19375 mips_parse_cpu (const char *option, const char *cpu_string)
19376 {
19377   const struct mips_cpu_info *p;
19378
19379   /* 'from-abi' selects the most compatible architecture for the given
19380      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
19381      EABIs, we have to decide whether we're using the 32-bit or 64-bit
19382      version.  Look first at the -mgp options, if given, otherwise base
19383      the choice on MIPS_DEFAULT_64BIT.
19384
19385      Treat NO_ABI like the EABIs.  One reason to do this is that the
19386      plain 'mips' and 'mips64' configs have 'from-abi' as their default
19387      architecture.  This code picks MIPS I for 'mips' and MIPS III for
19388      'mips64', just as we did in the days before 'from-abi'.  */
19389   if (strcasecmp (cpu_string, "from-abi") == 0)
19390     {
19391       if (ABI_NEEDS_32BIT_REGS (mips_abi))
19392         return mips_cpu_info_from_isa (ISA_MIPS1);
19393
19394       if (ABI_NEEDS_64BIT_REGS (mips_abi))
19395         return mips_cpu_info_from_isa (ISA_MIPS3);
19396
19397       if (file_mips_gp32 >= 0)
19398         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19399
19400       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19401                                      ? ISA_MIPS3
19402                                      : ISA_MIPS1);
19403     }
19404
19405   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
19406   if (strcasecmp (cpu_string, "default") == 0)
19407     return 0;
19408
19409   for (p = mips_cpu_info_table; p->name != 0; p++)
19410     if (mips_matching_cpu_name_p (p->name, cpu_string))
19411       return p;
19412
19413   as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19414   return 0;
19415 }
19416
19417 /* Return the canonical processor information for ISA (a member of the
19418    ISA_MIPS* enumeration).  */
19419
19420 static const struct mips_cpu_info *
19421 mips_cpu_info_from_isa (int isa)
19422 {
19423   int i;
19424
19425   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19426     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19427         && isa == mips_cpu_info_table[i].isa)
19428       return (&mips_cpu_info_table[i]);
19429
19430   return NULL;
19431 }
19432
19433 static const struct mips_cpu_info *
19434 mips_cpu_info_from_arch (int arch)
19435 {
19436   int i;
19437
19438   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19439     if (arch == mips_cpu_info_table[i].cpu)
19440       return (&mips_cpu_info_table[i]);
19441
19442   return NULL;
19443 }
19444 \f
19445 static void
19446 show (FILE *stream, const char *string, int *col_p, int *first_p)
19447 {
19448   if (*first_p)
19449     {
19450       fprintf (stream, "%24s", "");
19451       *col_p = 24;
19452     }
19453   else
19454     {
19455       fprintf (stream, ", ");
19456       *col_p += 2;
19457     }
19458
19459   if (*col_p + strlen (string) > 72)
19460     {
19461       fprintf (stream, "\n%24s", "");
19462       *col_p = 24;
19463     }
19464
19465   fprintf (stream, "%s", string);
19466   *col_p += strlen (string);
19467
19468   *first_p = 0;
19469 }
19470
19471 void
19472 md_show_usage (FILE *stream)
19473 {
19474   int column, first;
19475   size_t i;
19476
19477   fprintf (stream, _("\
19478 MIPS options:\n\
19479 -EB                     generate big endian output\n\
19480 -EL                     generate little endian output\n\
19481 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
19482 -G NUM                  allow referencing objects up to NUM bytes\n\
19483                         implicitly with the gp register [default 8]\n"));
19484   fprintf (stream, _("\
19485 -mips1                  generate MIPS ISA I instructions\n\
19486 -mips2                  generate MIPS ISA II instructions\n\
19487 -mips3                  generate MIPS ISA III instructions\n\
19488 -mips4                  generate MIPS ISA IV instructions\n\
19489 -mips5                  generate MIPS ISA V instructions\n\
19490 -mips32                 generate MIPS32 ISA instructions\n\
19491 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
19492 -mips64                 generate MIPS64 ISA instructions\n\
19493 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
19494 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
19495
19496   first = 1;
19497
19498   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19499     show (stream, mips_cpu_info_table[i].name, &column, &first);
19500   show (stream, "from-abi", &column, &first);
19501   fputc ('\n', stream);
19502
19503   fprintf (stream, _("\
19504 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19505 -no-mCPU                don't generate code specific to CPU.\n\
19506                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
19507
19508   first = 1;
19509
19510   show (stream, "3900", &column, &first);
19511   show (stream, "4010", &column, &first);
19512   show (stream, "4100", &column, &first);
19513   show (stream, "4650", &column, &first);
19514   fputc ('\n', stream);
19515
19516   fprintf (stream, _("\
19517 -mips16                 generate mips16 instructions\n\
19518 -no-mips16              do not generate mips16 instructions\n"));
19519   fprintf (stream, _("\
19520 -mmicromips             generate microMIPS instructions\n\
19521 -mno-micromips          do not generate microMIPS instructions\n"));
19522   fprintf (stream, _("\
19523 -msmartmips             generate smartmips instructions\n\
19524 -mno-smartmips          do not generate smartmips instructions\n"));  
19525   fprintf (stream, _("\
19526 -mdsp                   generate DSP instructions\n\
19527 -mno-dsp                do not generate DSP instructions\n"));
19528   fprintf (stream, _("\
19529 -mdspr2                 generate DSP R2 instructions\n\
19530 -mno-dspr2              do not generate DSP R2 instructions\n"));
19531   fprintf (stream, _("\
19532 -mmt                    generate MT instructions\n\
19533 -mno-mt                 do not generate MT instructions\n"));
19534   fprintf (stream, _("\
19535 -mmcu                   generate MCU instructions\n\
19536 -mno-mcu                do not generate MCU instructions\n"));
19537   fprintf (stream, _("\
19538 -mfix-loongson2f-jump   work around Loongson2F JUMP instructions\n\
19539 -mfix-loongson2f-nop    work around Loongson2F NOP errata\n\
19540 -mfix-vr4120            work around certain VR4120 errata\n\
19541 -mfix-vr4130            work around VR4130 mflo/mfhi errata\n\
19542 -mfix-24k               insert a nop after ERET and DERET instructions\n\
19543 -mfix-cn63xxp1          work around CN63XXP1 PREF errata\n\
19544 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
19545 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
19546 -msym32                 assume all symbols have 32-bit values\n\
19547 -O0                     remove unneeded NOPs, do not swap branches\n\
19548 -O                      remove unneeded NOPs and swap branches\n\
19549 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
19550 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
19551   fprintf (stream, _("\
19552 -mhard-float            allow floating-point instructions\n\
19553 -msoft-float            do not allow floating-point instructions\n\
19554 -msingle-float          only allow 32-bit floating-point operations\n\
19555 -mdouble-float          allow 32-bit and 64-bit floating-point operations\n\
19556 --[no-]construct-floats [dis]allow floating point values to be constructed\n"
19557                      ));
19558 #ifdef OBJ_ELF
19559   fprintf (stream, _("\
19560 -KPIC, -call_shared     generate SVR4 position independent code\n\
19561 -call_nonpic            generate non-PIC code that can operate with DSOs\n\
19562 -mvxworks-pic           generate VxWorks position independent code\n\
19563 -non_shared             do not generate code that can operate with DSOs\n\
19564 -xgot                   assume a 32 bit GOT\n\
19565 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
19566 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
19567                         position dependent (non shared) code\n\
19568 -mabi=ABI               create ABI conformant object file for:\n"));
19569
19570   first = 1;
19571
19572   show (stream, "32", &column, &first);
19573   show (stream, "o64", &column, &first);
19574   show (stream, "n32", &column, &first);
19575   show (stream, "64", &column, &first);
19576   show (stream, "eabi", &column, &first);
19577
19578   fputc ('\n', stream);
19579
19580   fprintf (stream, _("\
19581 -32                     create o32 ABI object file (default)\n\
19582 -n32                    create n32 ABI object file\n\
19583 -64                     create 64 ABI object file\n"));
19584 #endif
19585 }
19586
19587 #ifdef TE_IRIX
19588 enum dwarf2_format
19589 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19590 {
19591   if (HAVE_64BIT_SYMBOLS)
19592     return dwarf2_format_64bit_irix;
19593   else
19594     return dwarf2_format_32bit;
19595 }
19596 #endif
19597
19598 int
19599 mips_dwarf2_addr_size (void)
19600 {
19601   if (HAVE_64BIT_OBJECTS)
19602     return 8;
19603   else
19604     return 4;
19605 }
19606
19607 /* Standard calling conventions leave the CFA at SP on entry.  */
19608 void
19609 mips_cfi_frame_initial_instructions (void)
19610 {
19611   cfi_add_CFA_def_cfa_register (SP);
19612 }
19613
19614 int
19615 tc_mips_regname_to_dw2regnum (char *regname)
19616 {
19617   unsigned int regnum = -1;
19618   unsigned int reg;
19619
19620   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19621     regnum = reg;
19622
19623   return regnum;
19624 }