2013-04-30 Chao-ying Fu <Chao-ying.Fu@imgtec.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 CPU has support for ldc1 and sdc1. */
508 #define CPU_HAS_LDC1_SDC1(CPU)  \
509    ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
510
511 /* True if mflo and mfhi can be immediately followed by instructions
512    which write to the HI and LO registers.
513
514    According to MIPS specifications, MIPS ISAs I, II, and III need
515    (at least) two instructions between the reads of HI/LO and
516    instructions which write them, and later ISAs do not.  Contradicting
517    the MIPS specifications, some MIPS IV processor user manuals (e.g.
518    the UM for the NEC Vr5000) document needing the instructions between
519    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
520    MIPS64 and later ISAs to have the interlocks, plus any specific
521    earlier-ISA CPUs for which CPU documentation declares that the
522    instructions are really interlocked.  */
523 #define hilo_interlocks \
524   (mips_opts.isa == ISA_MIPS32                        \
525    || mips_opts.isa == ISA_MIPS32R2                   \
526    || mips_opts.isa == ISA_MIPS64                     \
527    || mips_opts.isa == ISA_MIPS64R2                   \
528    || mips_opts.arch == CPU_R4010                     \
529    || mips_opts.arch == CPU_R5900                     \
530    || mips_opts.arch == CPU_R10000                    \
531    || mips_opts.arch == CPU_R12000                    \
532    || mips_opts.arch == CPU_R14000                    \
533    || mips_opts.arch == CPU_R16000                    \
534    || mips_opts.arch == CPU_RM7000                    \
535    || mips_opts.arch == CPU_VR5500                    \
536    || mips_opts.micromips                             \
537    )
538
539 /* Whether the processor uses hardware interlocks to protect reads
540    from the GPRs after they are loaded from memory, and thus does not
541    require nops to be inserted.  This applies to instructions marked
542    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
543    level I and microMIPS mode instructions are always interlocked.  */
544 #define gpr_interlocks                                \
545   (mips_opts.isa != ISA_MIPS1                         \
546    || mips_opts.arch == CPU_R3900                     \
547    || mips_opts.arch == CPU_R5900                     \
548    || mips_opts.micromips                             \
549    )
550
551 /* Whether the processor uses hardware interlocks to avoid delays
552    required by coprocessor instructions, and thus does not require
553    nops to be inserted.  This applies to instructions marked
554    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
555    between instructions marked INSN_WRITE_COND_CODE and ones marked
556    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
557    levels I, II, and III and microMIPS mode instructions are always
558    interlocked.  */
559 /* Itbl support may require additional care here.  */
560 #define cop_interlocks                                \
561   ((mips_opts.isa != ISA_MIPS1                        \
562     && mips_opts.isa != ISA_MIPS2                     \
563     && mips_opts.isa != ISA_MIPS3)                    \
564    || mips_opts.arch == CPU_R4300                     \
565    || mips_opts.micromips                             \
566    )
567
568 /* Whether the processor uses hardware interlocks to protect reads
569    from coprocessor registers after they are loaded from memory, and
570    thus does not require nops to be inserted.  This applies to
571    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
572    requires at MIPS ISA level I and microMIPS mode instructions are
573    always interlocked.  */
574 #define cop_mem_interlocks                            \
575   (mips_opts.isa != ISA_MIPS1                         \
576    || mips_opts.micromips                             \
577    )
578
579 /* Is this a mfhi or mflo instruction?  */
580 #define MF_HILO_INSN(PINFO) \
581   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
582
583 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
584    has been selected.  This implies, in particular, that addresses of text
585    labels have their LSB set.  */
586 #define HAVE_CODE_COMPRESSION                                           \
587   ((mips_opts.mips16 | mips_opts.micromips) != 0)
588
589 /* MIPS PIC level.  */
590
591 enum mips_pic_level mips_pic;
592
593 /* 1 if we should generate 32 bit offsets from the $gp register in
594    SVR4_PIC mode.  Currently has no meaning in other modes.  */
595 static int mips_big_got = 0;
596
597 /* 1 if trap instructions should used for overflow rather than break
598    instructions.  */
599 static int mips_trap = 0;
600
601 /* 1 if double width floating point constants should not be constructed
602    by assembling two single width halves into two single width floating
603    point registers which just happen to alias the double width destination
604    register.  On some architectures this aliasing can be disabled by a bit
605    in the status register, and the setting of this bit cannot be determined
606    automatically at assemble time.  */
607 static int mips_disable_float_construction;
608
609 /* Non-zero if any .set noreorder directives were used.  */
610
611 static int mips_any_noreorder;
612
613 /* Non-zero if nops should be inserted when the register referenced in
614    an mfhi/mflo instruction is read in the next two instructions.  */
615 static int mips_7000_hilo_fix;
616
617 /* The size of objects in the small data section.  */
618 static unsigned int g_switch_value = 8;
619 /* Whether the -G option was used.  */
620 static int g_switch_seen = 0;
621
622 #define N_RMASK 0xc4
623 #define N_VFP   0xd4
624
625 /* If we can determine in advance that GP optimization won't be
626    possible, we can skip the relaxation stuff that tries to produce
627    GP-relative references.  This makes delay slot optimization work
628    better.
629
630    This function can only provide a guess, but it seems to work for
631    gcc output.  It needs to guess right for gcc, otherwise gcc
632    will put what it thinks is a GP-relative instruction in a branch
633    delay slot.
634
635    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
636    fixed it for the non-PIC mode.  KR 95/04/07  */
637 static int nopic_need_relax (symbolS *, int);
638
639 /* handle of the OPCODE hash table */
640 static struct hash_control *op_hash = NULL;
641
642 /* The opcode hash table we use for the mips16.  */
643 static struct hash_control *mips16_op_hash = NULL;
644
645 /* The opcode hash table we use for the microMIPS ASE.  */
646 static struct hash_control *micromips_op_hash = NULL;
647
648 /* This array holds the chars that always start a comment.  If the
649     pre-processor is disabled, these aren't very useful */
650 const char comment_chars[] = "#";
651
652 /* This array holds the chars that only start a comment at the beginning of
653    a line.  If the line seems to have the form '# 123 filename'
654    .line and .file directives will appear in the pre-processed output */
655 /* Note that input_file.c hand checks for '#' at the beginning of the
656    first line of the input file.  This is because the compiler outputs
657    #NO_APP at the beginning of its output.  */
658 /* Also note that C style comments are always supported.  */
659 const char line_comment_chars[] = "#";
660
661 /* This array holds machine specific line separator characters.  */
662 const char line_separator_chars[] = ";";
663
664 /* Chars that can be used to separate mant from exp in floating point nums */
665 const char EXP_CHARS[] = "eE";
666
667 /* Chars that mean this number is a floating point constant */
668 /* As in 0f12.456 */
669 /* or    0d1.2345e12 */
670 const char FLT_CHARS[] = "rRsSfFdDxXpP";
671
672 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
673    changed in read.c .  Ideally it shouldn't have to know about it at all,
674    but nothing is ideal around here.
675  */
676
677 static char *insn_error;
678
679 static int auto_align = 1;
680
681 /* When outputting SVR4 PIC code, the assembler needs to know the
682    offset in the stack frame from which to restore the $gp register.
683    This is set by the .cprestore pseudo-op, and saved in this
684    variable.  */
685 static offsetT mips_cprestore_offset = -1;
686
687 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
688    more optimizations, it can use a register value instead of a memory-saved
689    offset and even an other register than $gp as global pointer.  */
690 static offsetT mips_cpreturn_offset = -1;
691 static int mips_cpreturn_register = -1;
692 static int mips_gp_register = GP;
693 static int mips_gprel_offset = 0;
694
695 /* Whether mips_cprestore_offset has been set in the current function
696    (or whether it has already been warned about, if not).  */
697 static int mips_cprestore_valid = 0;
698
699 /* This is the register which holds the stack frame, as set by the
700    .frame pseudo-op.  This is needed to implement .cprestore.  */
701 static int mips_frame_reg = SP;
702
703 /* Whether mips_frame_reg has been set in the current function
704    (or whether it has already been warned about, if not).  */
705 static int mips_frame_reg_valid = 0;
706
707 /* To output NOP instructions correctly, we need to keep information
708    about the previous two instructions.  */
709
710 /* Whether we are optimizing.  The default value of 2 means to remove
711    unneeded NOPs and swap branch instructions when possible.  A value
712    of 1 means to not swap branches.  A value of 0 means to always
713    insert NOPs.  */
714 static int mips_optimize = 2;
715
716 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
717    equivalent to seeing no -g option at all.  */
718 static int mips_debug = 0;
719
720 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
721 #define MAX_VR4130_NOPS 4
722
723 /* The maximum number of NOPs needed to fill delay slots.  */
724 #define MAX_DELAY_NOPS 2
725
726 /* The maximum number of NOPs needed for any purpose.  */
727 #define MAX_NOPS 4
728
729 /* A list of previous instructions, with index 0 being the most recent.
730    We need to look back MAX_NOPS instructions when filling delay slots
731    or working around processor errata.  We need to look back one
732    instruction further if we're thinking about using history[0] to
733    fill a branch delay slot.  */
734 static struct mips_cl_insn history[1 + MAX_NOPS];
735
736 /* Nop instructions used by emit_nop.  */
737 static struct mips_cl_insn nop_insn;
738 static struct mips_cl_insn mips16_nop_insn;
739 static struct mips_cl_insn micromips_nop16_insn;
740 static struct mips_cl_insn micromips_nop32_insn;
741
742 /* The appropriate nop for the current mode.  */
743 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
744                   : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
745
746 /* The size of NOP_INSN in bytes.  */
747 #define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
748
749 /* If this is set, it points to a frag holding nop instructions which
750    were inserted before the start of a noreorder section.  If those
751    nops turn out to be unnecessary, the size of the frag can be
752    decreased.  */
753 static fragS *prev_nop_frag;
754
755 /* The number of nop instructions we created in prev_nop_frag.  */
756 static int prev_nop_frag_holds;
757
758 /* The number of nop instructions that we know we need in
759    prev_nop_frag.  */
760 static int prev_nop_frag_required;
761
762 /* The number of instructions we've seen since prev_nop_frag.  */
763 static int prev_nop_frag_since;
764
765 /* For ECOFF and ELF, relocations against symbols are done in two
766    parts, with a HI relocation and a LO relocation.  Each relocation
767    has only 16 bits of space to store an addend.  This means that in
768    order for the linker to handle carries correctly, it must be able
769    to locate both the HI and the LO relocation.  This means that the
770    relocations must appear in order in the relocation table.
771
772    In order to implement this, we keep track of each unmatched HI
773    relocation.  We then sort them so that they immediately precede the
774    corresponding LO relocation.  */
775
776 struct mips_hi_fixup
777 {
778   /* Next HI fixup.  */
779   struct mips_hi_fixup *next;
780   /* This fixup.  */
781   fixS *fixp;
782   /* The section this fixup is in.  */
783   segT seg;
784 };
785
786 /* The list of unmatched HI relocs.  */
787
788 static struct mips_hi_fixup *mips_hi_fixup_list;
789
790 /* The frag containing the last explicit relocation operator.
791    Null if explicit relocations have not been used.  */
792
793 static fragS *prev_reloc_op_frag;
794
795 /* Map normal MIPS register numbers to mips16 register numbers.  */
796
797 #define X ILLEGAL_REG
798 static const int mips32_to_16_reg_map[] =
799 {
800   X, X, 2, 3, 4, 5, 6, 7,
801   X, X, X, X, X, X, X, X,
802   0, 1, X, X, X, X, X, X,
803   X, X, X, X, X, X, X, X
804 };
805 #undef X
806
807 /* Map mips16 register numbers to normal MIPS register numbers.  */
808
809 static const unsigned int mips16_to_32_reg_map[] =
810 {
811   16, 17, 2, 3, 4, 5, 6, 7
812 };
813
814 /* Map normal MIPS register numbers to microMIPS register numbers.  */
815
816 #define mips32_to_micromips_reg_b_map   mips32_to_16_reg_map
817 #define mips32_to_micromips_reg_c_map   mips32_to_16_reg_map
818 #define mips32_to_micromips_reg_d_map   mips32_to_16_reg_map
819 #define mips32_to_micromips_reg_e_map   mips32_to_16_reg_map
820 #define mips32_to_micromips_reg_f_map   mips32_to_16_reg_map
821 #define mips32_to_micromips_reg_g_map   mips32_to_16_reg_map
822 #define mips32_to_micromips_reg_l_map   mips32_to_16_reg_map
823
824 #define X ILLEGAL_REG
825 /* reg type h: 4, 5, 6.  */
826 static const int mips32_to_micromips_reg_h_map[] =
827 {
828   X, X, X, X, 4, 5, 6, X,
829   X, X, X, X, X, X, X, X,
830   X, X, X, X, X, X, X, X,
831   X, X, X, X, X, X, X, X
832 };
833
834 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20.  */
835 static const int mips32_to_micromips_reg_m_map[] =
836 {
837   0, X, 2, 3, X, X, X, X,
838   X, X, X, X, X, X, X, X,
839   4, 1, 5, 6, 7, X, X, X,
840   X, X, X, X, X, X, X, X
841 };
842
843 /* reg type q: 0, 2-7. 17.  */
844 static const int mips32_to_micromips_reg_q_map[] =
845 {
846   0, X, 2, 3, 4, 5, 6, 7,
847   X, X, X, X, X, X, X, X,
848   X, 1, X, X, X, X, X, X,
849   X, X, X, X, X, X, X, X
850 };
851
852 #define mips32_to_micromips_reg_n_map  mips32_to_micromips_reg_m_map
853 #undef X
854
855 /* Map microMIPS register numbers to normal MIPS register numbers.  */
856
857 #define micromips_to_32_reg_b_map       mips16_to_32_reg_map
858 #define micromips_to_32_reg_c_map       mips16_to_32_reg_map
859 #define micromips_to_32_reg_d_map       mips16_to_32_reg_map
860 #define micromips_to_32_reg_e_map       mips16_to_32_reg_map
861 #define micromips_to_32_reg_f_map       mips16_to_32_reg_map
862 #define micromips_to_32_reg_g_map       mips16_to_32_reg_map
863
864 /* The microMIPS registers with type h.  */
865 static const unsigned int micromips_to_32_reg_h_map[] =
866 {
867   5, 5, 6, 4, 4, 4, 4, 4
868 };
869
870 /* The microMIPS registers with type i.  */
871 static const unsigned int micromips_to_32_reg_i_map[] =
872 {
873   6, 7, 7, 21, 22, 5, 6, 7
874 };
875
876 #define micromips_to_32_reg_l_map       mips16_to_32_reg_map
877
878 /* The microMIPS registers with type m.  */
879 static const unsigned int micromips_to_32_reg_m_map[] =
880 {
881   0, 17, 2, 3, 16, 18, 19, 20
882 };
883
884 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
885
886 /* The microMIPS registers with type q.  */
887 static const unsigned int micromips_to_32_reg_q_map[] =
888 {
889   0, 17, 2, 3, 4, 5, 6, 7
890 };
891
892 /* microMIPS imm type B.  */
893 static const int micromips_imm_b_map[] =
894 {
895   1, 4, 8, 12, 16, 20, 24, -1
896 };
897
898 /* microMIPS imm type C.  */
899 static const int micromips_imm_c_map[] =
900 {
901   128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
902 };
903
904 /* Classifies the kind of instructions we're interested in when
905    implementing -mfix-vr4120.  */
906 enum fix_vr4120_class
907 {
908   FIX_VR4120_MACC,
909   FIX_VR4120_DMACC,
910   FIX_VR4120_MULT,
911   FIX_VR4120_DMULT,
912   FIX_VR4120_DIV,
913   FIX_VR4120_MTHILO,
914   NUM_FIX_VR4120_CLASSES
915 };
916
917 /* ...likewise -mfix-loongson2f-jump.  */
918 static bfd_boolean mips_fix_loongson2f_jump;
919
920 /* ...likewise -mfix-loongson2f-nop.  */
921 static bfd_boolean mips_fix_loongson2f_nop;
922
923 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
924 static bfd_boolean mips_fix_loongson2f;
925
926 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
927    there must be at least one other instruction between an instruction
928    of type X and an instruction of type Y.  */
929 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
930
931 /* True if -mfix-vr4120 is in force.  */
932 static int mips_fix_vr4120;
933
934 /* ...likewise -mfix-vr4130.  */
935 static int mips_fix_vr4130;
936
937 /* ...likewise -mfix-24k.  */
938 static int mips_fix_24k;
939
940 /* ...likewise -mfix-cn63xxp1 */
941 static bfd_boolean mips_fix_cn63xxp1;
942
943 /* We don't relax branches by default, since this causes us to expand
944    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
945    fail to compute the offset before expanding the macro to the most
946    efficient expansion.  */
947
948 static int mips_relax_branch;
949 \f
950 /* The expansion of many macros depends on the type of symbol that
951    they refer to.  For example, when generating position-dependent code,
952    a macro that refers to a symbol may have two different expansions,
953    one which uses GP-relative addresses and one which uses absolute
954    addresses.  When generating SVR4-style PIC, a macro may have
955    different expansions for local and global symbols.
956
957    We handle these situations by generating both sequences and putting
958    them in variant frags.  In position-dependent code, the first sequence
959    will be the GP-relative one and the second sequence will be the
960    absolute one.  In SVR4 PIC, the first sequence will be for global
961    symbols and the second will be for local symbols.
962
963    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
964    SECOND are the lengths of the two sequences in bytes.  These fields
965    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
966    the subtype has the following flags:
967
968    RELAX_USE_SECOND
969         Set if it has been decided that we should use the second
970         sequence instead of the first.
971
972    RELAX_SECOND_LONGER
973         Set in the first variant frag if the macro's second implementation
974         is longer than its first.  This refers to the macro as a whole,
975         not an individual relaxation.
976
977    RELAX_NOMACRO
978         Set in the first variant frag if the macro appeared in a .set nomacro
979         block and if one alternative requires a warning but the other does not.
980
981    RELAX_DELAY_SLOT
982         Like RELAX_NOMACRO, but indicates that the macro appears in a branch
983         delay slot.
984
985    RELAX_DELAY_SLOT_16BIT
986         Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
987         16-bit instruction.
988
989    RELAX_DELAY_SLOT_SIZE_FIRST
990         Like RELAX_DELAY_SLOT, but indicates that the first implementation of
991         the macro is of the wrong size for the branch delay slot.
992
993    RELAX_DELAY_SLOT_SIZE_SECOND
994         Like RELAX_DELAY_SLOT, but indicates that the second implementation of
995         the macro is of the wrong size for the branch delay slot.
996
997    The frag's "opcode" points to the first fixup for relaxable code.
998
999    Relaxable macros are generated using a sequence such as:
1000
1001       relax_start (SYMBOL);
1002       ... generate first expansion ...
1003       relax_switch ();
1004       ... generate second expansion ...
1005       relax_end ();
1006
1007    The code and fixups for the unwanted alternative are discarded
1008    by md_convert_frag.  */
1009 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
1010
1011 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1012 #define RELAX_SECOND(X) ((X) & 0xff)
1013 #define RELAX_USE_SECOND 0x10000
1014 #define RELAX_SECOND_LONGER 0x20000
1015 #define RELAX_NOMACRO 0x40000
1016 #define RELAX_DELAY_SLOT 0x80000
1017 #define RELAX_DELAY_SLOT_16BIT 0x100000
1018 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1019 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
1020
1021 /* Branch without likely bit.  If label is out of range, we turn:
1022
1023         beq reg1, reg2, label
1024         delay slot
1025
1026    into
1027
1028         bne reg1, reg2, 0f
1029         nop
1030         j label
1031      0: delay slot
1032
1033    with the following opcode replacements:
1034
1035         beq <-> bne
1036         blez <-> bgtz
1037         bltz <-> bgez
1038         bc1f <-> bc1t
1039
1040         bltzal <-> bgezal  (with jal label instead of j label)
1041
1042    Even though keeping the delay slot instruction in the delay slot of
1043    the branch would be more efficient, it would be very tricky to do
1044    correctly, because we'd have to introduce a variable frag *after*
1045    the delay slot instruction, and expand that instead.  Let's do it
1046    the easy way for now, even if the branch-not-taken case now costs
1047    one additional instruction.  Out-of-range branches are not supposed
1048    to be common, anyway.
1049
1050    Branch likely.  If label is out of range, we turn:
1051
1052         beql reg1, reg2, label
1053         delay slot (annulled if branch not taken)
1054
1055    into
1056
1057         beql reg1, reg2, 1f
1058         nop
1059         beql $0, $0, 2f
1060         nop
1061      1: j[al] label
1062         delay slot (executed only if branch taken)
1063      2:
1064
1065    It would be possible to generate a shorter sequence by losing the
1066    likely bit, generating something like:
1067
1068         bne reg1, reg2, 0f
1069         nop
1070         j[al] label
1071         delay slot (executed only if branch taken)
1072      0:
1073
1074         beql -> bne
1075         bnel -> beq
1076         blezl -> bgtz
1077         bgtzl -> blez
1078         bltzl -> bgez
1079         bgezl -> bltz
1080         bc1fl -> bc1t
1081         bc1tl -> bc1f
1082
1083         bltzall -> bgezal  (with jal label instead of j label)
1084         bgezall -> bltzal  (ditto)
1085
1086
1087    but it's not clear that it would actually improve performance.  */
1088 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar)   \
1089   ((relax_substateT)                                            \
1090    (0xc0000000                                                  \
1091     | ((at) & 0x1f)                                             \
1092     | ((toofar) ? 0x20 : 0)                                     \
1093     | ((link) ? 0x40 : 0)                                       \
1094     | ((likely) ? 0x80 : 0)                                     \
1095     | ((uncond) ? 0x100 : 0)))
1096 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1097 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1098 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1099 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1100 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1101 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1102
1103 /* For mips16 code, we use an entirely different form of relaxation.
1104    mips16 supports two versions of most instructions which take
1105    immediate values: a small one which takes some small value, and a
1106    larger one which takes a 16 bit value.  Since branches also follow
1107    this pattern, relaxing these values is required.
1108
1109    We can assemble both mips16 and normal MIPS code in a single
1110    object.  Therefore, we need to support this type of relaxation at
1111    the same time that we support the relaxation described above.  We
1112    use the high bit of the subtype field to distinguish these cases.
1113
1114    The information we store for this type of relaxation is the
1115    argument code found in the opcode file for this relocation, whether
1116    the user explicitly requested a small or extended form, and whether
1117    the relocation is in a jump or jal delay slot.  That tells us the
1118    size of the value, and how it should be stored.  We also store
1119    whether the fragment is considered to be extended or not.  We also
1120    store whether this is known to be a branch to a different section,
1121    whether we have tried to relax this frag yet, and whether we have
1122    ever extended a PC relative fragment because of a shift count.  */
1123 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1124   (0x80000000                                                   \
1125    | ((type) & 0xff)                                            \
1126    | ((small) ? 0x100 : 0)                                      \
1127    | ((ext) ? 0x200 : 0)                                        \
1128    | ((dslot) ? 0x400 : 0)                                      \
1129    | ((jal_dslot) ? 0x800 : 0))
1130 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1131 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1132 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1133 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1134 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1135 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1136 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1137 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1138 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1139 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1140 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1141 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1142
1143 /* For microMIPS code, we use relaxation similar to one we use for
1144    MIPS16 code.  Some instructions that take immediate values support
1145    two encodings: a small one which takes some small value, and a
1146    larger one which takes a 16 bit value.  As some branches also follow
1147    this pattern, relaxing these values is required.
1148
1149    We can assemble both microMIPS and normal MIPS code in a single
1150    object.  Therefore, we need to support this type of relaxation at
1151    the same time that we support the relaxation described above.  We
1152    use one of the high bits of the subtype field to distinguish these
1153    cases.
1154
1155    The information we store for this type of relaxation is the argument
1156    code found in the opcode file for this relocation, the register
1157    selected as the assembler temporary, whether the branch is
1158    unconditional, whether it is compact, whether it stores the link
1159    address implicitly in $ra, whether relaxation of out-of-range 32-bit
1160    branches to a sequence of instructions is enabled, and whether the
1161    displacement of a branch is too large to fit as an immediate argument
1162    of a 16-bit and a 32-bit branch, respectively.  */
1163 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1164                                relax32, toofar16, toofar32)     \
1165   (0x40000000                                                   \
1166    | ((type) & 0xff)                                            \
1167    | (((at) & 0x1f) << 8)                                       \
1168    | ((uncond) ? 0x2000 : 0)                                    \
1169    | ((compact) ? 0x4000 : 0)                                   \
1170    | ((link) ? 0x8000 : 0)                                      \
1171    | ((relax32) ? 0x10000 : 0)                                  \
1172    | ((toofar16) ? 0x20000 : 0)                                 \
1173    | ((toofar32) ? 0x40000 : 0))
1174 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1175 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1176 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1177 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1178 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1179 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1180 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1181
1182 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1183 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1184 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1185 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1186 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1187 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1188
1189 /* Sign-extend 16-bit value X.  */
1190 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1191
1192 /* Is the given value a sign-extended 32-bit value?  */
1193 #define IS_SEXT_32BIT_NUM(x)                                            \
1194   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
1195    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1196
1197 /* Is the given value a sign-extended 16-bit value?  */
1198 #define IS_SEXT_16BIT_NUM(x)                                            \
1199   (((x) &~ (offsetT) 0x7fff) == 0                                       \
1200    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1201
1202 /* Is the given value a sign-extended 12-bit value?  */
1203 #define IS_SEXT_12BIT_NUM(x)                                            \
1204   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1205
1206 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1207 #define IS_ZEXT_32BIT_NUM(x)                                            \
1208   (((x) &~ (offsetT) 0xffffffff) == 0                                   \
1209    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1210
1211 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1212    VALUE << SHIFT.  VALUE is evaluated exactly once.  */
1213 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1214   (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1215               | (((VALUE) & (MASK)) << (SHIFT)))
1216
1217 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1218    SHIFT places.  */
1219 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1220   (((STRUCT) >> (SHIFT)) & (MASK))
1221
1222 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1223    INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1224
1225    include/opcode/mips.h specifies operand fields using the macros
1226    OP_MASK_<FIELD> and OP_SH_<FIELD>.  The MIPS16 equivalents start
1227    with "MIPS16OP" instead of "OP".  */
1228 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1229   do \
1230     if (!(MICROMIPS)) \
1231       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1232                    OP_MASK_##FIELD, OP_SH_##FIELD); \
1233     else \
1234       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1235                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1236   while (0)
1237 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1238   INSERT_BITS ((INSN).insn_opcode, VALUE, \
1239                 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1240
1241 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1242 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1243   (!(MICROMIPS) \
1244    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1245    : EXTRACT_BITS ((INSN).insn_opcode, \
1246                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1247 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1248   EXTRACT_BITS ((INSN).insn_opcode, \
1249                 MIPS16OP_MASK_##FIELD, \
1250                 MIPS16OP_SH_##FIELD)
1251
1252 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1253 #define MIPS16_EXTEND (0xf000U << 16)
1254 \f
1255 /* Whether or not we are emitting a branch-likely macro.  */
1256 static bfd_boolean emit_branch_likely_macro = FALSE;
1257
1258 /* Global variables used when generating relaxable macros.  See the
1259    comment above RELAX_ENCODE for more details about how relaxation
1260    is used.  */
1261 static struct {
1262   /* 0 if we're not emitting a relaxable macro.
1263      1 if we're emitting the first of the two relaxation alternatives.
1264      2 if we're emitting the second alternative.  */
1265   int sequence;
1266
1267   /* The first relaxable fixup in the current frag.  (In other words,
1268      the first fixup that refers to relaxable code.)  */
1269   fixS *first_fixup;
1270
1271   /* sizes[0] says how many bytes of the first alternative are stored in
1272      the current frag.  Likewise sizes[1] for the second alternative.  */
1273   unsigned int sizes[2];
1274
1275   /* The symbol on which the choice of sequence depends.  */
1276   symbolS *symbol;
1277 } mips_relax;
1278 \f
1279 /* Global variables used to decide whether a macro needs a warning.  */
1280 static struct {
1281   /* True if the macro is in a branch delay slot.  */
1282   bfd_boolean delay_slot_p;
1283
1284   /* Set to the length in bytes required if the macro is in a delay slot
1285      that requires a specific length of instruction, otherwise zero.  */
1286   unsigned int delay_slot_length;
1287
1288   /* For relaxable macros, sizes[0] is the length of the first alternative
1289      in bytes and sizes[1] is the length of the second alternative.
1290      For non-relaxable macros, both elements give the length of the
1291      macro in bytes.  */
1292   unsigned int sizes[2];
1293
1294   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1295      instruction of the first alternative in bytes and first_insn_sizes[1]
1296      is the length of the first instruction of the second alternative.
1297      For non-relaxable macros, both elements give the length of the first
1298      instruction in bytes.
1299
1300      Set to zero if we haven't yet seen the first instruction.  */
1301   unsigned int first_insn_sizes[2];
1302
1303   /* For relaxable macros, insns[0] is the number of instructions for the
1304      first alternative and insns[1] is the number of instructions for the
1305      second alternative.
1306
1307      For non-relaxable macros, both elements give the number of
1308      instructions for the macro.  */
1309   unsigned int insns[2];
1310
1311   /* The first variant frag for this macro.  */
1312   fragS *first_frag;
1313 } mips_macro_warning;
1314 \f
1315 /* Prototypes for static functions.  */
1316
1317 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1318
1319 static void append_insn
1320   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1321    bfd_boolean expansionp);
1322 static void mips_no_prev_insn (void);
1323 static void macro_build (expressionS *, const char *, const char *, ...);
1324 static void mips16_macro_build
1325   (expressionS *, const char *, const char *, va_list *);
1326 static void load_register (int, expressionS *, int);
1327 static void macro_start (void);
1328 static void macro_end (void);
1329 static void macro (struct mips_cl_insn * ip);
1330 static void mips16_macro (struct mips_cl_insn * ip);
1331 static void mips_ip (char *str, struct mips_cl_insn * ip);
1332 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1333 static void mips16_immed
1334   (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1335    unsigned int, unsigned long *);
1336 static size_t my_getSmallExpression
1337   (expressionS *, bfd_reloc_code_real_type *, char *);
1338 static void my_getExpression (expressionS *, char *);
1339 static void s_align (int);
1340 static void s_change_sec (int);
1341 static void s_change_section (int);
1342 static void s_cons (int);
1343 static void s_float_cons (int);
1344 static void s_mips_globl (int);
1345 static void s_option (int);
1346 static void s_mipsset (int);
1347 static void s_abicalls (int);
1348 static void s_cpload (int);
1349 static void s_cpsetup (int);
1350 static void s_cplocal (int);
1351 static void s_cprestore (int);
1352 static void s_cpreturn (int);
1353 static void s_dtprelword (int);
1354 static void s_dtpreldword (int);
1355 static void s_tprelword (int);
1356 static void s_tpreldword (int);
1357 static void s_gpvalue (int);
1358 static void s_gpword (int);
1359 static void s_gpdword (int);
1360 static void s_cpadd (int);
1361 static void s_insn (int);
1362 static void md_obj_begin (void);
1363 static void md_obj_end (void);
1364 static void s_mips_ent (int);
1365 static void s_mips_end (int);
1366 static void s_mips_frame (int);
1367 static void s_mips_mask (int reg_type);
1368 static void s_mips_stab (int);
1369 static void s_mips_weakext (int);
1370 static void s_mips_file (int);
1371 static void s_mips_loc (int);
1372 static bfd_boolean pic_need_relax (symbolS *, asection *);
1373 static int relaxed_branch_length (fragS *, asection *, int);
1374 static int validate_mips_insn (const struct mips_opcode *);
1375 static int validate_micromips_insn (const struct mips_opcode *);
1376 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1377 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1378
1379 /* Table and functions used to map between CPU/ISA names, and
1380    ISA levels, and CPU numbers.  */
1381
1382 struct mips_cpu_info
1383 {
1384   const char *name;           /* CPU or ISA name.  */
1385   int flags;                  /* ASEs available, or ISA flag.  */
1386   int isa;                    /* ISA level.  */
1387   int cpu;                    /* CPU number (default CPU if ISA).  */
1388 };
1389
1390 #define MIPS_CPU_IS_ISA         0x0001  /* Is this an ISA?  (If 0, a CPU.) */
1391 #define MIPS_CPU_ASE_SMARTMIPS  0x0002  /* CPU implements SmartMIPS ASE */
1392 #define MIPS_CPU_ASE_DSP        0x0004  /* CPU implements DSP ASE */
1393 #define MIPS_CPU_ASE_MT         0x0008  /* CPU implements MT ASE */
1394 #define MIPS_CPU_ASE_MIPS3D     0x0010  /* CPU implements MIPS-3D ASE */
1395 #define MIPS_CPU_ASE_MDMX       0x0020  /* CPU implements MDMX ASE */
1396 #define MIPS_CPU_ASE_DSPR2      0x0040  /* CPU implements DSP R2 ASE */
1397 #define MIPS_CPU_ASE_MCU        0x0080  /* CPU implements MCU ASE */
1398
1399 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1400 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1401 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1402 \f
1403 /* Pseudo-op table.
1404
1405    The following pseudo-ops from the Kane and Heinrich MIPS book
1406    should be defined here, but are currently unsupported: .alias,
1407    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1408
1409    The following pseudo-ops from the Kane and Heinrich MIPS book are
1410    specific to the type of debugging information being generated, and
1411    should be defined by the object format: .aent, .begin, .bend,
1412    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1413    .vreg.
1414
1415    The following pseudo-ops from the Kane and Heinrich MIPS book are
1416    not MIPS CPU specific, but are also not specific to the object file
1417    format.  This file is probably the best place to define them, but
1418    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1419
1420 static const pseudo_typeS mips_pseudo_table[] =
1421 {
1422   /* MIPS specific pseudo-ops.  */
1423   {"option", s_option, 0},
1424   {"set", s_mipsset, 0},
1425   {"rdata", s_change_sec, 'r'},
1426   {"sdata", s_change_sec, 's'},
1427   {"livereg", s_ignore, 0},
1428   {"abicalls", s_abicalls, 0},
1429   {"cpload", s_cpload, 0},
1430   {"cpsetup", s_cpsetup, 0},
1431   {"cplocal", s_cplocal, 0},
1432   {"cprestore", s_cprestore, 0},
1433   {"cpreturn", s_cpreturn, 0},
1434   {"dtprelword", s_dtprelword, 0},
1435   {"dtpreldword", s_dtpreldword, 0},
1436   {"tprelword", s_tprelword, 0},
1437   {"tpreldword", s_tpreldword, 0},
1438   {"gpvalue", s_gpvalue, 0},
1439   {"gpword", s_gpword, 0},
1440   {"gpdword", s_gpdword, 0},
1441   {"cpadd", s_cpadd, 0},
1442   {"insn", s_insn, 0},
1443
1444   /* Relatively generic pseudo-ops that happen to be used on MIPS
1445      chips.  */
1446   {"asciiz", stringer, 8 + 1},
1447   {"bss", s_change_sec, 'b'},
1448   {"err", s_err, 0},
1449   {"half", s_cons, 1},
1450   {"dword", s_cons, 3},
1451   {"weakext", s_mips_weakext, 0},
1452   {"origin", s_org, 0},
1453   {"repeat", s_rept, 0},
1454
1455   /* For MIPS this is non-standard, but we define it for consistency.  */
1456   {"sbss", s_change_sec, 'B'},
1457
1458   /* These pseudo-ops are defined in read.c, but must be overridden
1459      here for one reason or another.  */
1460   {"align", s_align, 0},
1461   {"byte", s_cons, 0},
1462   {"data", s_change_sec, 'd'},
1463   {"double", s_float_cons, 'd'},
1464   {"float", s_float_cons, 'f'},
1465   {"globl", s_mips_globl, 0},
1466   {"global", s_mips_globl, 0},
1467   {"hword", s_cons, 1},
1468   {"int", s_cons, 2},
1469   {"long", s_cons, 2},
1470   {"octa", s_cons, 4},
1471   {"quad", s_cons, 3},
1472   {"section", s_change_section, 0},
1473   {"short", s_cons, 1},
1474   {"single", s_float_cons, 'f'},
1475   {"stabn", s_mips_stab, 'n'},
1476   {"text", s_change_sec, 't'},
1477   {"word", s_cons, 2},
1478
1479   { "extern", ecoff_directive_extern, 0},
1480
1481   { NULL, NULL, 0 },
1482 };
1483
1484 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1485 {
1486   /* These pseudo-ops should be defined by the object file format.
1487      However, a.out doesn't support them, so we have versions here.  */
1488   {"aent", s_mips_ent, 1},
1489   {"bgnb", s_ignore, 0},
1490   {"end", s_mips_end, 0},
1491   {"endb", s_ignore, 0},
1492   {"ent", s_mips_ent, 0},
1493   {"file", s_mips_file, 0},
1494   {"fmask", s_mips_mask, 'F'},
1495   {"frame", s_mips_frame, 0},
1496   {"loc", s_mips_loc, 0},
1497   {"mask", s_mips_mask, 'R'},
1498   {"verstamp", s_ignore, 0},
1499   { NULL, NULL, 0 },
1500 };
1501
1502 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1503    purpose of the `.dc.a' internal pseudo-op.  */
1504
1505 int
1506 mips_address_bytes (void)
1507 {
1508   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1509 }
1510
1511 extern void pop_insert (const pseudo_typeS *);
1512
1513 void
1514 mips_pop_insert (void)
1515 {
1516   pop_insert (mips_pseudo_table);
1517   if (! ECOFF_DEBUGGING)
1518     pop_insert (mips_nonecoff_pseudo_table);
1519 }
1520 \f
1521 /* Symbols labelling the current insn.  */
1522
1523 struct insn_label_list
1524 {
1525   struct insn_label_list *next;
1526   symbolS *label;
1527 };
1528
1529 static struct insn_label_list *free_insn_labels;
1530 #define label_list tc_segment_info_data.labels
1531
1532 static void mips_clear_insn_labels (void);
1533 static void mips_mark_labels (void);
1534 static void mips_compressed_mark_labels (void);
1535
1536 static inline void
1537 mips_clear_insn_labels (void)
1538 {
1539   register struct insn_label_list **pl;
1540   segment_info_type *si;
1541
1542   if (now_seg)
1543     {
1544       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1545         ;
1546       
1547       si = seg_info (now_seg);
1548       *pl = si->label_list;
1549       si->label_list = NULL;
1550     }
1551 }
1552
1553 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1554
1555 static inline void
1556 mips_mark_labels (void)
1557 {
1558   if (HAVE_CODE_COMPRESSION)
1559     mips_compressed_mark_labels ();
1560 }
1561 \f
1562 static char *expr_end;
1563
1564 /* Expressions which appear in instructions.  These are set by
1565    mips_ip.  */
1566
1567 static expressionS imm_expr;
1568 static expressionS imm2_expr;
1569 static expressionS offset_expr;
1570
1571 /* Relocs associated with imm_expr and offset_expr.  */
1572
1573 static bfd_reloc_code_real_type imm_reloc[3]
1574   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1575 static bfd_reloc_code_real_type offset_reloc[3]
1576   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1577
1578 /* This is set to the resulting size of the instruction to be produced
1579    by mips16_ip if an explicit extension is used or by mips_ip if an
1580    explicit size is supplied.  */
1581
1582 static unsigned int forced_insn_length;
1583
1584 /* True if we are assembling an instruction.  All dot symbols defined during
1585    this time should be treated as code labels.  */
1586
1587 static bfd_boolean mips_assembling_insn;
1588
1589 #ifdef OBJ_ELF
1590 /* The pdr segment for per procedure frame/regmask info.  Not used for
1591    ECOFF debugging.  */
1592
1593 static segT pdr_seg;
1594 #endif
1595
1596 /* The default target format to use.  */
1597
1598 #if defined (TE_FreeBSD)
1599 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1600 #elif defined (TE_TMIPS)
1601 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1602 #else
1603 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1604 #endif
1605
1606 const char *
1607 mips_target_format (void)
1608 {
1609   switch (OUTPUT_FLAVOR)
1610     {
1611     case bfd_target_ecoff_flavour:
1612       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1613     case bfd_target_coff_flavour:
1614       return "pe-mips";
1615     case bfd_target_elf_flavour:
1616 #ifdef TE_VXWORKS
1617       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1618         return (target_big_endian
1619                 ? "elf32-bigmips-vxworks"
1620                 : "elf32-littlemips-vxworks");
1621 #endif
1622       return (target_big_endian
1623               ? (HAVE_64BIT_OBJECTS
1624                  ? ELF_TARGET ("elf64-", "big")
1625                  : (HAVE_NEWABI
1626                     ? ELF_TARGET ("elf32-n", "big")
1627                     : ELF_TARGET ("elf32-", "big")))
1628               : (HAVE_64BIT_OBJECTS
1629                  ? ELF_TARGET ("elf64-", "little")
1630                  : (HAVE_NEWABI
1631                     ? ELF_TARGET ("elf32-n", "little")
1632                     : ELF_TARGET ("elf32-", "little"))));
1633     default:
1634       abort ();
1635       return NULL;
1636     }
1637 }
1638
1639 /* Return the length of a microMIPS instruction in bytes.  If bits of
1640    the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1641    Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1642    major opcode) will require further modifications to the opcode
1643    table.  */
1644
1645 static inline unsigned int
1646 micromips_insn_length (const struct mips_opcode *mo)
1647 {
1648   return (mo->mask >> 16) == 0 ? 2 : 4;
1649 }
1650
1651 /* Return the length of MIPS16 instruction OPCODE.  */
1652
1653 static inline unsigned int
1654 mips16_opcode_length (unsigned long opcode)
1655 {
1656   return (opcode >> 16) == 0 ? 2 : 4;
1657 }
1658
1659 /* Return the length of instruction INSN.  */
1660
1661 static inline unsigned int
1662 insn_length (const struct mips_cl_insn *insn)
1663 {
1664   if (mips_opts.micromips)
1665     return micromips_insn_length (insn->insn_mo);
1666   else if (mips_opts.mips16)
1667     return mips16_opcode_length (insn->insn_opcode);
1668   else
1669     return 4;
1670 }
1671
1672 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
1673
1674 static void
1675 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1676 {
1677   size_t i;
1678
1679   insn->insn_mo = mo;
1680   insn->insn_opcode = mo->match;
1681   insn->frag = NULL;
1682   insn->where = 0;
1683   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1684     insn->fixp[i] = NULL;
1685   insn->fixed_p = (mips_opts.noreorder > 0);
1686   insn->noreorder_p = (mips_opts.noreorder > 0);
1687   insn->mips16_absolute_jump_p = 0;
1688   insn->complete_p = 0;
1689   insn->cleared_p = 0;
1690 }
1691
1692 /* Record the current MIPS16/microMIPS mode in now_seg.  */
1693
1694 static void
1695 mips_record_compressed_mode (void)
1696 {
1697   segment_info_type *si;
1698
1699   si = seg_info (now_seg);
1700   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1701     si->tc_segment_info_data.mips16 = mips_opts.mips16;
1702   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1703     si->tc_segment_info_data.micromips = mips_opts.micromips;
1704 }
1705
1706 /* Read a standard MIPS instruction from BUF.  */
1707
1708 static unsigned long
1709 read_insn (char *buf)
1710 {
1711   if (target_big_endian)
1712     return bfd_getb32 ((bfd_byte *) buf);
1713   else
1714     return bfd_getl32 ((bfd_byte *) buf);
1715 }
1716
1717 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
1718    the next byte.  */
1719
1720 static char *
1721 write_insn (char *buf, unsigned int insn)
1722 {
1723   md_number_to_chars (buf, insn, 4);
1724   return buf + 4;
1725 }
1726
1727 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
1728    has length LENGTH.  */
1729
1730 static unsigned long
1731 read_compressed_insn (char *buf, unsigned int length)
1732 {
1733   unsigned long insn;
1734   unsigned int i;
1735
1736   insn = 0;
1737   for (i = 0; i < length; i += 2)
1738     {
1739       insn <<= 16;
1740       if (target_big_endian)
1741         insn |= bfd_getb16 ((char *) buf);
1742       else
1743         insn |= bfd_getl16 ((char *) buf);
1744       buf += 2;
1745     }
1746   return insn;
1747 }
1748
1749 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
1750    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
1751
1752 static char *
1753 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
1754 {
1755   unsigned int i;
1756
1757   for (i = 0; i < length; i += 2)
1758     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
1759   return buf + length;
1760 }
1761
1762 /* Install INSN at the location specified by its "frag" and "where" fields.  */
1763
1764 static void
1765 install_insn (const struct mips_cl_insn *insn)
1766 {
1767   char *f = insn->frag->fr_literal + insn->where;
1768   if (HAVE_CODE_COMPRESSION)
1769     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1770   else
1771     write_insn (f, insn->insn_opcode);
1772   mips_record_compressed_mode ();
1773 }
1774
1775 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
1776    and install the opcode in the new location.  */
1777
1778 static void
1779 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1780 {
1781   size_t i;
1782
1783   insn->frag = frag;
1784   insn->where = where;
1785   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1786     if (insn->fixp[i] != NULL)
1787       {
1788         insn->fixp[i]->fx_frag = frag;
1789         insn->fixp[i]->fx_where = where;
1790       }
1791   install_insn (insn);
1792 }
1793
1794 /* Add INSN to the end of the output.  */
1795
1796 static void
1797 add_fixed_insn (struct mips_cl_insn *insn)
1798 {
1799   char *f = frag_more (insn_length (insn));
1800   move_insn (insn, frag_now, f - frag_now->fr_literal);
1801 }
1802
1803 /* Start a variant frag and move INSN to the start of the variant part,
1804    marking it as fixed.  The other arguments are as for frag_var.  */
1805
1806 static void
1807 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1808                   relax_substateT subtype, symbolS *symbol, offsetT offset)
1809 {
1810   frag_grow (max_chars);
1811   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1812   insn->fixed_p = 1;
1813   frag_var (rs_machine_dependent, max_chars, var,
1814             subtype, symbol, offset, NULL);
1815 }
1816
1817 /* Insert N copies of INSN into the history buffer, starting at
1818    position FIRST.  Neither FIRST nor N need to be clipped.  */
1819
1820 static void
1821 insert_into_history (unsigned int first, unsigned int n,
1822                      const struct mips_cl_insn *insn)
1823 {
1824   if (mips_relax.sequence != 2)
1825     {
1826       unsigned int i;
1827
1828       for (i = ARRAY_SIZE (history); i-- > first;)
1829         if (i >= first + n)
1830           history[i] = history[i - n];
1831         else
1832           history[i] = *insn;
1833     }
1834 }
1835
1836 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
1837    the idea is to make it obvious at a glance that each errata is
1838    included.  */
1839
1840 static void
1841 init_vr4120_conflicts (void)
1842 {
1843 #define CONFLICT(FIRST, SECOND) \
1844     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1845
1846   /* Errata 21 - [D]DIV[U] after [D]MACC */
1847   CONFLICT (MACC, DIV);
1848   CONFLICT (DMACC, DIV);
1849
1850   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
1851   CONFLICT (DMULT, DMULT);
1852   CONFLICT (DMULT, DMACC);
1853   CONFLICT (DMACC, DMULT);
1854   CONFLICT (DMACC, DMACC);
1855
1856   /* Errata 24 - MT{LO,HI} after [D]MACC */
1857   CONFLICT (MACC, MTHILO);
1858   CONFLICT (DMACC, MTHILO);
1859
1860   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1861      instruction is executed immediately after a MACC or DMACC
1862      instruction, the result of [either instruction] is incorrect."  */
1863   CONFLICT (MACC, MULT);
1864   CONFLICT (MACC, DMULT);
1865   CONFLICT (DMACC, MULT);
1866   CONFLICT (DMACC, DMULT);
1867
1868   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1869      executed immediately after a DMULT, DMULTU, DIV, DIVU,
1870      DDIV or DDIVU instruction, the result of the MACC or
1871      DMACC instruction is incorrect.".  */
1872   CONFLICT (DMULT, MACC);
1873   CONFLICT (DMULT, DMACC);
1874   CONFLICT (DIV, MACC);
1875   CONFLICT (DIV, DMACC);
1876
1877 #undef CONFLICT
1878 }
1879
1880 struct regname {
1881   const char *name;
1882   unsigned int num;
1883 };
1884
1885 #define RTYPE_MASK      0x1ff00
1886 #define RTYPE_NUM       0x00100
1887 #define RTYPE_FPU       0x00200
1888 #define RTYPE_FCC       0x00400
1889 #define RTYPE_VEC       0x00800
1890 #define RTYPE_GP        0x01000
1891 #define RTYPE_CP0       0x02000
1892 #define RTYPE_PC        0x04000
1893 #define RTYPE_ACC       0x08000
1894 #define RTYPE_CCC       0x10000
1895 #define RNUM_MASK       0x000ff
1896 #define RWARN           0x80000
1897
1898 #define GENERIC_REGISTER_NUMBERS \
1899     {"$0",      RTYPE_NUM | 0},  \
1900     {"$1",      RTYPE_NUM | 1},  \
1901     {"$2",      RTYPE_NUM | 2},  \
1902     {"$3",      RTYPE_NUM | 3},  \
1903     {"$4",      RTYPE_NUM | 4},  \
1904     {"$5",      RTYPE_NUM | 5},  \
1905     {"$6",      RTYPE_NUM | 6},  \
1906     {"$7",      RTYPE_NUM | 7},  \
1907     {"$8",      RTYPE_NUM | 8},  \
1908     {"$9",      RTYPE_NUM | 9},  \
1909     {"$10",     RTYPE_NUM | 10}, \
1910     {"$11",     RTYPE_NUM | 11}, \
1911     {"$12",     RTYPE_NUM | 12}, \
1912     {"$13",     RTYPE_NUM | 13}, \
1913     {"$14",     RTYPE_NUM | 14}, \
1914     {"$15",     RTYPE_NUM | 15}, \
1915     {"$16",     RTYPE_NUM | 16}, \
1916     {"$17",     RTYPE_NUM | 17}, \
1917     {"$18",     RTYPE_NUM | 18}, \
1918     {"$19",     RTYPE_NUM | 19}, \
1919     {"$20",     RTYPE_NUM | 20}, \
1920     {"$21",     RTYPE_NUM | 21}, \
1921     {"$22",     RTYPE_NUM | 22}, \
1922     {"$23",     RTYPE_NUM | 23}, \
1923     {"$24",     RTYPE_NUM | 24}, \
1924     {"$25",     RTYPE_NUM | 25}, \
1925     {"$26",     RTYPE_NUM | 26}, \
1926     {"$27",     RTYPE_NUM | 27}, \
1927     {"$28",     RTYPE_NUM | 28}, \
1928     {"$29",     RTYPE_NUM | 29}, \
1929     {"$30",     RTYPE_NUM | 30}, \
1930     {"$31",     RTYPE_NUM | 31} 
1931
1932 #define FPU_REGISTER_NAMES       \
1933     {"$f0",     RTYPE_FPU | 0},  \
1934     {"$f1",     RTYPE_FPU | 1},  \
1935     {"$f2",     RTYPE_FPU | 2},  \
1936     {"$f3",     RTYPE_FPU | 3},  \
1937     {"$f4",     RTYPE_FPU | 4},  \
1938     {"$f5",     RTYPE_FPU | 5},  \
1939     {"$f6",     RTYPE_FPU | 6},  \
1940     {"$f7",     RTYPE_FPU | 7},  \
1941     {"$f8",     RTYPE_FPU | 8},  \
1942     {"$f9",     RTYPE_FPU | 9},  \
1943     {"$f10",    RTYPE_FPU | 10}, \
1944     {"$f11",    RTYPE_FPU | 11}, \
1945     {"$f12",    RTYPE_FPU | 12}, \
1946     {"$f13",    RTYPE_FPU | 13}, \
1947     {"$f14",    RTYPE_FPU | 14}, \
1948     {"$f15",    RTYPE_FPU | 15}, \
1949     {"$f16",    RTYPE_FPU | 16}, \
1950     {"$f17",    RTYPE_FPU | 17}, \
1951     {"$f18",    RTYPE_FPU | 18}, \
1952     {"$f19",    RTYPE_FPU | 19}, \
1953     {"$f20",    RTYPE_FPU | 20}, \
1954     {"$f21",    RTYPE_FPU | 21}, \
1955     {"$f22",    RTYPE_FPU | 22}, \
1956     {"$f23",    RTYPE_FPU | 23}, \
1957     {"$f24",    RTYPE_FPU | 24}, \
1958     {"$f25",    RTYPE_FPU | 25}, \
1959     {"$f26",    RTYPE_FPU | 26}, \
1960     {"$f27",    RTYPE_FPU | 27}, \
1961     {"$f28",    RTYPE_FPU | 28}, \
1962     {"$f29",    RTYPE_FPU | 29}, \
1963     {"$f30",    RTYPE_FPU | 30}, \
1964     {"$f31",    RTYPE_FPU | 31}
1965
1966 #define FPU_CONDITION_CODE_NAMES \
1967     {"$fcc0",   RTYPE_FCC | 0},  \
1968     {"$fcc1",   RTYPE_FCC | 1},  \
1969     {"$fcc2",   RTYPE_FCC | 2},  \
1970     {"$fcc3",   RTYPE_FCC | 3},  \
1971     {"$fcc4",   RTYPE_FCC | 4},  \
1972     {"$fcc5",   RTYPE_FCC | 5},  \
1973     {"$fcc6",   RTYPE_FCC | 6},  \
1974     {"$fcc7",   RTYPE_FCC | 7}
1975
1976 #define COPROC_CONDITION_CODE_NAMES         \
1977     {"$cc0",    RTYPE_FCC | RTYPE_CCC | 0}, \
1978     {"$cc1",    RTYPE_FCC | RTYPE_CCC | 1}, \
1979     {"$cc2",    RTYPE_FCC | RTYPE_CCC | 2}, \
1980     {"$cc3",    RTYPE_FCC | RTYPE_CCC | 3}, \
1981     {"$cc4",    RTYPE_FCC | RTYPE_CCC | 4}, \
1982     {"$cc5",    RTYPE_FCC | RTYPE_CCC | 5}, \
1983     {"$cc6",    RTYPE_FCC | RTYPE_CCC | 6}, \
1984     {"$cc7",    RTYPE_FCC | RTYPE_CCC | 7}
1985
1986 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1987     {"$a4",     RTYPE_GP | 8},  \
1988     {"$a5",     RTYPE_GP | 9},  \
1989     {"$a6",     RTYPE_GP | 10}, \
1990     {"$a7",     RTYPE_GP | 11}, \
1991     {"$ta0",    RTYPE_GP | 8},  /* alias for $a4 */ \
1992     {"$ta1",    RTYPE_GP | 9},  /* alias for $a5 */ \
1993     {"$ta2",    RTYPE_GP | 10}, /* alias for $a6 */ \
1994     {"$ta3",    RTYPE_GP | 11}, /* alias for $a7 */ \
1995     {"$t0",     RTYPE_GP | 12}, \
1996     {"$t1",     RTYPE_GP | 13}, \
1997     {"$t2",     RTYPE_GP | 14}, \
1998     {"$t3",     RTYPE_GP | 15}
1999
2000 #define O32_SYMBOLIC_REGISTER_NAMES \
2001     {"$t0",     RTYPE_GP | 8},  \
2002     {"$t1",     RTYPE_GP | 9},  \
2003     {"$t2",     RTYPE_GP | 10}, \
2004     {"$t3",     RTYPE_GP | 11}, \
2005     {"$t4",     RTYPE_GP | 12}, \
2006     {"$t5",     RTYPE_GP | 13}, \
2007     {"$t6",     RTYPE_GP | 14}, \
2008     {"$t7",     RTYPE_GP | 15}, \
2009     {"$ta0",    RTYPE_GP | 12}, /* alias for $t4 */ \
2010     {"$ta1",    RTYPE_GP | 13}, /* alias for $t5 */ \
2011     {"$ta2",    RTYPE_GP | 14}, /* alias for $t6 */ \
2012     {"$ta3",    RTYPE_GP | 15}  /* alias for $t7 */ 
2013
2014 /* Remaining symbolic register names */
2015 #define SYMBOLIC_REGISTER_NAMES \
2016     {"$zero",   RTYPE_GP | 0},  \
2017     {"$at",     RTYPE_GP | 1},  \
2018     {"$AT",     RTYPE_GP | 1},  \
2019     {"$v0",     RTYPE_GP | 2},  \
2020     {"$v1",     RTYPE_GP | 3},  \
2021     {"$a0",     RTYPE_GP | 4},  \
2022     {"$a1",     RTYPE_GP | 5},  \
2023     {"$a2",     RTYPE_GP | 6},  \
2024     {"$a3",     RTYPE_GP | 7},  \
2025     {"$s0",     RTYPE_GP | 16}, \
2026     {"$s1",     RTYPE_GP | 17}, \
2027     {"$s2",     RTYPE_GP | 18}, \
2028     {"$s3",     RTYPE_GP | 19}, \
2029     {"$s4",     RTYPE_GP | 20}, \
2030     {"$s5",     RTYPE_GP | 21}, \
2031     {"$s6",     RTYPE_GP | 22}, \
2032     {"$s7",     RTYPE_GP | 23}, \
2033     {"$t8",     RTYPE_GP | 24}, \
2034     {"$t9",     RTYPE_GP | 25}, \
2035     {"$k0",     RTYPE_GP | 26}, \
2036     {"$kt0",    RTYPE_GP | 26}, \
2037     {"$k1",     RTYPE_GP | 27}, \
2038     {"$kt1",    RTYPE_GP | 27}, \
2039     {"$gp",     RTYPE_GP | 28}, \
2040     {"$sp",     RTYPE_GP | 29}, \
2041     {"$s8",     RTYPE_GP | 30}, \
2042     {"$fp",     RTYPE_GP | 30}, \
2043     {"$ra",     RTYPE_GP | 31}
2044
2045 #define MIPS16_SPECIAL_REGISTER_NAMES \
2046     {"$pc",     RTYPE_PC | 0}
2047
2048 #define MDMX_VECTOR_REGISTER_NAMES \
2049     /* {"$v0",  RTYPE_VEC | 0},  clash with REG 2 above */ \
2050     /* {"$v1",  RTYPE_VEC | 1},  clash with REG 3 above */ \
2051     {"$v2",     RTYPE_VEC | 2},  \
2052     {"$v3",     RTYPE_VEC | 3},  \
2053     {"$v4",     RTYPE_VEC | 4},  \
2054     {"$v5",     RTYPE_VEC | 5},  \
2055     {"$v6",     RTYPE_VEC | 6},  \
2056     {"$v7",     RTYPE_VEC | 7},  \
2057     {"$v8",     RTYPE_VEC | 8},  \
2058     {"$v9",     RTYPE_VEC | 9},  \
2059     {"$v10",    RTYPE_VEC | 10}, \
2060     {"$v11",    RTYPE_VEC | 11}, \
2061     {"$v12",    RTYPE_VEC | 12}, \
2062     {"$v13",    RTYPE_VEC | 13}, \
2063     {"$v14",    RTYPE_VEC | 14}, \
2064     {"$v15",    RTYPE_VEC | 15}, \
2065     {"$v16",    RTYPE_VEC | 16}, \
2066     {"$v17",    RTYPE_VEC | 17}, \
2067     {"$v18",    RTYPE_VEC | 18}, \
2068     {"$v19",    RTYPE_VEC | 19}, \
2069     {"$v20",    RTYPE_VEC | 20}, \
2070     {"$v21",    RTYPE_VEC | 21}, \
2071     {"$v22",    RTYPE_VEC | 22}, \
2072     {"$v23",    RTYPE_VEC | 23}, \
2073     {"$v24",    RTYPE_VEC | 24}, \
2074     {"$v25",    RTYPE_VEC | 25}, \
2075     {"$v26",    RTYPE_VEC | 26}, \
2076     {"$v27",    RTYPE_VEC | 27}, \
2077     {"$v28",    RTYPE_VEC | 28}, \
2078     {"$v29",    RTYPE_VEC | 29}, \
2079     {"$v30",    RTYPE_VEC | 30}, \
2080     {"$v31",    RTYPE_VEC | 31}
2081
2082 #define MIPS_DSP_ACCUMULATOR_NAMES \
2083     {"$ac0",    RTYPE_ACC | 0}, \
2084     {"$ac1",    RTYPE_ACC | 1}, \
2085     {"$ac2",    RTYPE_ACC | 2}, \
2086     {"$ac3",    RTYPE_ACC | 3}
2087
2088 static const struct regname reg_names[] = {
2089   GENERIC_REGISTER_NUMBERS,
2090   FPU_REGISTER_NAMES,
2091   FPU_CONDITION_CODE_NAMES,
2092   COPROC_CONDITION_CODE_NAMES,
2093
2094   /* The $txx registers depends on the abi,
2095      these will be added later into the symbol table from
2096      one of the tables below once mips_abi is set after 
2097      parsing of arguments from the command line. */
2098   SYMBOLIC_REGISTER_NAMES,
2099
2100   MIPS16_SPECIAL_REGISTER_NAMES,
2101   MDMX_VECTOR_REGISTER_NAMES,
2102   MIPS_DSP_ACCUMULATOR_NAMES,
2103   {0, 0}
2104 };
2105
2106 static const struct regname reg_names_o32[] = {
2107   O32_SYMBOLIC_REGISTER_NAMES,
2108   {0, 0}
2109 };
2110
2111 static const struct regname reg_names_n32n64[] = {
2112   N32N64_SYMBOLIC_REGISTER_NAMES,
2113   {0, 0}
2114 };
2115
2116 /* Check if S points at a valid register specifier according to TYPES.
2117    If so, then return 1, advance S to consume the specifier and store
2118    the register's number in REGNOP, otherwise return 0.  */
2119
2120 static int
2121 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2122 {
2123   symbolS *symbolP;
2124   char *e;
2125   char save_c;
2126   int reg = -1;
2127
2128   /* Find end of name.  */
2129   e = *s;
2130   if (is_name_beginner (*e))
2131     ++e;
2132   while (is_part_of_name (*e))
2133     ++e;
2134
2135   /* Terminate name.  */
2136   save_c = *e;
2137   *e = '\0';
2138
2139   /* Look for a register symbol.  */
2140   if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2141     {
2142       int r = S_GET_VALUE (symbolP);
2143       if (r & types)
2144         reg = r & RNUM_MASK;
2145       else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2146         /* Convert GP reg $v0/1 to MDMX reg $v0/1!  */
2147         reg = (r & RNUM_MASK) - 2;
2148     }
2149   /* Else see if this is a register defined in an itbl entry.  */
2150   else if ((types & RTYPE_GP) && itbl_have_entries)
2151     {
2152       char *n = *s;
2153       unsigned long r;
2154
2155       if (*n == '$')
2156         ++n;
2157       if (itbl_get_reg_val (n, &r))
2158         reg = r & RNUM_MASK;
2159     }
2160
2161   /* Advance to next token if a register was recognised.  */
2162   if (reg >= 0)
2163     *s = e;
2164   else if (types & RWARN)
2165     as_warn (_("Unrecognized register name `%s'"), *s);
2166
2167   *e = save_c;
2168   if (regnop)
2169     *regnop = reg;
2170   return reg >= 0;
2171 }
2172
2173 /* Check if S points at a valid register list according to TYPES.
2174    If so, then return 1, advance S to consume the list and store
2175    the registers present on the list as a bitmask of ones in REGLISTP,
2176    otherwise return 0.  A valid list comprises a comma-separated
2177    enumeration of valid single registers and/or dash-separated
2178    contiguous register ranges as determined by their numbers.
2179
2180    As a special exception if one of s0-s7 registers is specified as
2181    the range's lower delimiter and s8 (fp) is its upper one, then no
2182    registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2183    are selected; they have to be listed separately if needed.  */
2184
2185 static int
2186 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2187 {
2188   unsigned int reglist = 0;
2189   unsigned int lastregno;
2190   bfd_boolean ok = TRUE;
2191   unsigned int regmask;
2192   char *s_endlist = *s;
2193   char *s_reset = *s;
2194   unsigned int regno;
2195
2196   while (reg_lookup (s, types, &regno))
2197     {
2198       lastregno = regno;
2199       if (**s == '-')
2200         {
2201           (*s)++;
2202           ok = reg_lookup (s, types, &lastregno);
2203           if (ok && lastregno < regno)
2204             ok = FALSE;
2205           if (!ok)
2206             break;
2207         }
2208
2209       if (lastregno == FP && regno >= S0 && regno <= S7)
2210         {
2211           lastregno = S7;
2212           reglist |= 1 << FP;
2213         }
2214       regmask = 1 << lastregno;
2215       regmask = (regmask << 1) - 1;
2216       regmask ^= (1 << regno) - 1;
2217       reglist |= regmask;
2218
2219       s_endlist = *s;
2220       if (**s != ',')
2221         break;
2222       (*s)++;
2223     }
2224
2225   if (ok)
2226     *s = s_endlist;
2227   else
2228     *s = s_reset;
2229   if (reglistp)
2230     *reglistp = reglist;
2231   return ok && reglist != 0;
2232 }
2233
2234 /* Return TRUE if opcode MO is valid on the currently selected ISA and
2235    architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
2236
2237 static bfd_boolean
2238 is_opcode_valid (const struct mips_opcode *mo)
2239 {
2240   int isa = mips_opts.isa;
2241   int fp_s, fp_d;
2242
2243   if (mips_opts.ase_mdmx)
2244     isa |= INSN_MDMX;
2245   if (mips_opts.ase_dsp)
2246     isa |= INSN_DSP;
2247   if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2248     isa |= INSN_DSP64;
2249   if (mips_opts.ase_dspr2)
2250     isa |= INSN_DSPR2;
2251   if (mips_opts.ase_mt)
2252     isa |= INSN_MT;
2253   if (mips_opts.ase_mips3d)
2254     isa |= INSN_MIPS3D;
2255   if (mips_opts.ase_smartmips)
2256     isa |= INSN_SMARTMIPS;
2257   if (mips_opts.ase_mcu)
2258     isa |= INSN_MCU;
2259
2260   if (!opcode_is_member (mo, isa, mips_opts.arch))
2261     return FALSE;
2262
2263   /* Check whether the instruction or macro requires single-precision or
2264      double-precision floating-point support.  Note that this information is
2265      stored differently in the opcode table for insns and macros.  */
2266   if (mo->pinfo == INSN_MACRO)
2267     {
2268       fp_s = mo->pinfo2 & INSN2_M_FP_S;
2269       fp_d = mo->pinfo2 & INSN2_M_FP_D;
2270     }
2271   else
2272     {
2273       fp_s = mo->pinfo & FP_S;
2274       fp_d = mo->pinfo & FP_D;
2275     }
2276
2277   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2278     return FALSE;
2279
2280   if (fp_s && mips_opts.soft_float)
2281     return FALSE;
2282
2283   return TRUE;
2284 }
2285
2286 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2287    selected ISA and architecture.  */
2288
2289 static bfd_boolean
2290 is_opcode_valid_16 (const struct mips_opcode *mo)
2291 {
2292   return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
2293 }
2294
2295 /* Return TRUE if the size of the microMIPS opcode MO matches one
2296    explicitly requested.  Always TRUE in the standard MIPS mode.  */
2297
2298 static bfd_boolean
2299 is_size_valid (const struct mips_opcode *mo)
2300 {
2301   if (!mips_opts.micromips)
2302     return TRUE;
2303
2304   if (!forced_insn_length)
2305     return TRUE;
2306   if (mo->pinfo == INSN_MACRO)
2307     return FALSE;
2308   return forced_insn_length == micromips_insn_length (mo);
2309 }
2310
2311 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2312    of the preceding instruction.  Always TRUE in the standard MIPS mode.
2313
2314    We don't accept macros in 16-bit delay slots to avoid a case where
2315    a macro expansion fails because it relies on a preceding 32-bit real
2316    instruction to have matched and does not handle the operands correctly.
2317    The only macros that may expand to 16-bit instructions are JAL that
2318    cannot be placed in a delay slot anyway, and corner cases of BALIGN
2319    and BGT (that likewise cannot be placed in a delay slot) that decay to
2320    a NOP.  In all these cases the macros precede any corresponding real
2321    instruction definitions in the opcode table, so they will match in the
2322    second pass where the size of the delay slot is ignored and therefore
2323    produce correct code.  */
2324
2325 static bfd_boolean
2326 is_delay_slot_valid (const struct mips_opcode *mo)
2327 {
2328   if (!mips_opts.micromips)
2329     return TRUE;
2330
2331   if (mo->pinfo == INSN_MACRO)
2332     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
2333   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2334       && micromips_insn_length (mo) != 4)
2335     return FALSE;
2336   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2337       && micromips_insn_length (mo) != 2)
2338     return FALSE;
2339
2340   return TRUE;
2341 }
2342
2343 /* This function is called once, at assembler startup time.  It should set up
2344    all the tables, etc. that the MD part of the assembler will need.  */
2345
2346 void
2347 md_begin (void)
2348 {
2349   const char *retval = NULL;
2350   int i = 0;
2351   int broken = 0;
2352
2353   if (mips_pic != NO_PIC)
2354     {
2355       if (g_switch_seen && g_switch_value != 0)
2356         as_bad (_("-G may not be used in position-independent code"));
2357       g_switch_value = 0;
2358     }
2359
2360   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2361     as_warn (_("Could not set architecture and machine"));
2362
2363   op_hash = hash_new ();
2364
2365   for (i = 0; i < NUMOPCODES;)
2366     {
2367       const char *name = mips_opcodes[i].name;
2368
2369       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2370       if (retval != NULL)
2371         {
2372           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2373                    mips_opcodes[i].name, retval);
2374           /* Probably a memory allocation problem?  Give up now.  */
2375           as_fatal (_("Broken assembler.  No assembly attempted."));
2376         }
2377       do
2378         {
2379           if (mips_opcodes[i].pinfo != INSN_MACRO)
2380             {
2381               if (!validate_mips_insn (&mips_opcodes[i]))
2382                 broken = 1;
2383               if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2384                 {
2385                   create_insn (&nop_insn, mips_opcodes + i);
2386                   if (mips_fix_loongson2f_nop)
2387                     nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2388                   nop_insn.fixed_p = 1;
2389                 }
2390             }
2391           ++i;
2392         }
2393       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2394     }
2395
2396   mips16_op_hash = hash_new ();
2397
2398   i = 0;
2399   while (i < bfd_mips16_num_opcodes)
2400     {
2401       const char *name = mips16_opcodes[i].name;
2402
2403       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2404       if (retval != NULL)
2405         as_fatal (_("internal: can't hash `%s': %s"),
2406                   mips16_opcodes[i].name, retval);
2407       do
2408         {
2409           if (mips16_opcodes[i].pinfo != INSN_MACRO
2410               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2411                   != mips16_opcodes[i].match))
2412             {
2413               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2414                        mips16_opcodes[i].name, mips16_opcodes[i].args);
2415               broken = 1;
2416             }
2417           if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2418             {
2419               create_insn (&mips16_nop_insn, mips16_opcodes + i);
2420               mips16_nop_insn.fixed_p = 1;
2421             }
2422           ++i;
2423         }
2424       while (i < bfd_mips16_num_opcodes
2425              && strcmp (mips16_opcodes[i].name, name) == 0);
2426     }
2427
2428   micromips_op_hash = hash_new ();
2429
2430   i = 0;
2431   while (i < bfd_micromips_num_opcodes)
2432     {
2433       const char *name = micromips_opcodes[i].name;
2434
2435       retval = hash_insert (micromips_op_hash, name,
2436                             (void *) &micromips_opcodes[i]);
2437       if (retval != NULL)
2438         as_fatal (_("internal: can't hash `%s': %s"),
2439                   micromips_opcodes[i].name, retval);
2440       do
2441         if (micromips_opcodes[i].pinfo != INSN_MACRO)
2442           {
2443             struct mips_cl_insn *micromips_nop_insn;
2444
2445             if (!validate_micromips_insn (&micromips_opcodes[i]))
2446               broken = 1;
2447
2448             if (micromips_insn_length (micromips_opcodes + i) == 2)
2449               micromips_nop_insn = &micromips_nop16_insn;
2450             else if (micromips_insn_length (micromips_opcodes + i) == 4)
2451               micromips_nop_insn = &micromips_nop32_insn;
2452             else
2453               continue;
2454
2455             if (micromips_nop_insn->insn_mo == NULL
2456                 && strcmp (name, "nop") == 0)
2457               {
2458                 create_insn (micromips_nop_insn, micromips_opcodes + i);
2459                 micromips_nop_insn->fixed_p = 1;
2460               }
2461           }
2462       while (++i < bfd_micromips_num_opcodes
2463              && strcmp (micromips_opcodes[i].name, name) == 0);
2464     }
2465
2466   if (broken)
2467     as_fatal (_("Broken assembler.  No assembly attempted."));
2468
2469   /* We add all the general register names to the symbol table.  This
2470      helps us detect invalid uses of them.  */
2471   for (i = 0; reg_names[i].name; i++) 
2472     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2473                                      reg_names[i].num, /* & RNUM_MASK, */
2474                                      &zero_address_frag));
2475   if (HAVE_NEWABI)
2476     for (i = 0; reg_names_n32n64[i].name; i++) 
2477       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2478                                        reg_names_n32n64[i].num, /* & RNUM_MASK, */
2479                                        &zero_address_frag));
2480   else
2481     for (i = 0; reg_names_o32[i].name; i++) 
2482       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2483                                        reg_names_o32[i].num, /* & RNUM_MASK, */
2484                                        &zero_address_frag));
2485
2486   mips_no_prev_insn ();
2487
2488   mips_gprmask = 0;
2489   mips_cprmask[0] = 0;
2490   mips_cprmask[1] = 0;
2491   mips_cprmask[2] = 0;
2492   mips_cprmask[3] = 0;
2493
2494   /* set the default alignment for the text section (2**2) */
2495   record_alignment (text_section, 2);
2496
2497   bfd_set_gp_size (stdoutput, g_switch_value);
2498
2499 #ifdef OBJ_ELF
2500   if (IS_ELF)
2501     {
2502       /* On a native system other than VxWorks, sections must be aligned
2503          to 16 byte boundaries.  When configured for an embedded ELF
2504          target, we don't bother.  */
2505       if (strncmp (TARGET_OS, "elf", 3) != 0
2506           && strncmp (TARGET_OS, "vxworks", 7) != 0)
2507         {
2508           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2509           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2510           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2511         }
2512
2513       /* Create a .reginfo section for register masks and a .mdebug
2514          section for debugging information.  */
2515       {
2516         segT seg;
2517         subsegT subseg;
2518         flagword flags;
2519         segT sec;
2520
2521         seg = now_seg;
2522         subseg = now_subseg;
2523
2524         /* The ABI says this section should be loaded so that the
2525            running program can access it.  However, we don't load it
2526            if we are configured for an embedded target */
2527         flags = SEC_READONLY | SEC_DATA;
2528         if (strncmp (TARGET_OS, "elf", 3) != 0)
2529           flags |= SEC_ALLOC | SEC_LOAD;
2530
2531         if (mips_abi != N64_ABI)
2532           {
2533             sec = subseg_new (".reginfo", (subsegT) 0);
2534
2535             bfd_set_section_flags (stdoutput, sec, flags);
2536             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2537
2538             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2539           }
2540         else
2541           {
2542             /* The 64-bit ABI uses a .MIPS.options section rather than
2543                .reginfo section.  */
2544             sec = subseg_new (".MIPS.options", (subsegT) 0);
2545             bfd_set_section_flags (stdoutput, sec, flags);
2546             bfd_set_section_alignment (stdoutput, sec, 3);
2547
2548             /* Set up the option header.  */
2549             {
2550               Elf_Internal_Options opthdr;
2551               char *f;
2552
2553               opthdr.kind = ODK_REGINFO;
2554               opthdr.size = (sizeof (Elf_External_Options)
2555                              + sizeof (Elf64_External_RegInfo));
2556               opthdr.section = 0;
2557               opthdr.info = 0;
2558               f = frag_more (sizeof (Elf_External_Options));
2559               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2560                                              (Elf_External_Options *) f);
2561
2562               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2563             }
2564           }
2565
2566         if (ECOFF_DEBUGGING)
2567           {
2568             sec = subseg_new (".mdebug", (subsegT) 0);
2569             (void) bfd_set_section_flags (stdoutput, sec,
2570                                           SEC_HAS_CONTENTS | SEC_READONLY);
2571             (void) bfd_set_section_alignment (stdoutput, sec, 2);
2572           }
2573         else if (mips_flag_pdr)
2574           {
2575             pdr_seg = subseg_new (".pdr", (subsegT) 0);
2576             (void) bfd_set_section_flags (stdoutput, pdr_seg,
2577                                           SEC_READONLY | SEC_RELOC
2578                                           | SEC_DEBUGGING);
2579             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2580           }
2581
2582         subseg_set (seg, subseg);
2583       }
2584     }
2585 #endif /* OBJ_ELF */
2586
2587   if (! ECOFF_DEBUGGING)
2588     md_obj_begin ();
2589
2590   if (mips_fix_vr4120)
2591     init_vr4120_conflicts ();
2592 }
2593
2594 void
2595 md_mips_end (void)
2596 {
2597   mips_emit_delays ();
2598   if (! ECOFF_DEBUGGING)
2599     md_obj_end ();
2600 }
2601
2602 void
2603 md_assemble (char *str)
2604 {
2605   struct mips_cl_insn insn;
2606   bfd_reloc_code_real_type unused_reloc[3]
2607     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2608
2609   imm_expr.X_op = O_absent;
2610   imm2_expr.X_op = O_absent;
2611   offset_expr.X_op = O_absent;
2612   imm_reloc[0] = BFD_RELOC_UNUSED;
2613   imm_reloc[1] = BFD_RELOC_UNUSED;
2614   imm_reloc[2] = BFD_RELOC_UNUSED;
2615   offset_reloc[0] = BFD_RELOC_UNUSED;
2616   offset_reloc[1] = BFD_RELOC_UNUSED;
2617   offset_reloc[2] = BFD_RELOC_UNUSED;
2618
2619   mips_mark_labels ();
2620   mips_assembling_insn = TRUE;
2621
2622   if (mips_opts.mips16)
2623     mips16_ip (str, &insn);
2624   else
2625     {
2626       mips_ip (str, &insn);
2627       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2628             str, insn.insn_opcode));
2629     }
2630
2631   if (insn_error)
2632     as_bad ("%s `%s'", insn_error, str);
2633   else if (insn.insn_mo->pinfo == INSN_MACRO)
2634     {
2635       macro_start ();
2636       if (mips_opts.mips16)
2637         mips16_macro (&insn);
2638       else
2639         macro (&insn);
2640       macro_end ();
2641     }
2642   else
2643     {
2644       if (imm_expr.X_op != O_absent)
2645         append_insn (&insn, &imm_expr, imm_reloc, FALSE);
2646       else if (offset_expr.X_op != O_absent)
2647         append_insn (&insn, &offset_expr, offset_reloc, FALSE);
2648       else
2649         append_insn (&insn, NULL, unused_reloc, FALSE);
2650     }
2651
2652   mips_assembling_insn = FALSE;
2653 }
2654
2655 /* Convenience functions for abstracting away the differences between
2656    MIPS16 and non-MIPS16 relocations.  */
2657
2658 static inline bfd_boolean
2659 mips16_reloc_p (bfd_reloc_code_real_type reloc)
2660 {
2661   switch (reloc)
2662     {
2663     case BFD_RELOC_MIPS16_JMP:
2664     case BFD_RELOC_MIPS16_GPREL:
2665     case BFD_RELOC_MIPS16_GOT16:
2666     case BFD_RELOC_MIPS16_CALL16:
2667     case BFD_RELOC_MIPS16_HI16_S:
2668     case BFD_RELOC_MIPS16_HI16:
2669     case BFD_RELOC_MIPS16_LO16:
2670       return TRUE;
2671
2672     default:
2673       return FALSE;
2674     }
2675 }
2676
2677 static inline bfd_boolean
2678 micromips_reloc_p (bfd_reloc_code_real_type reloc)
2679 {
2680   switch (reloc)
2681     {
2682     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2683     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2684     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2685     case BFD_RELOC_MICROMIPS_GPREL16:
2686     case BFD_RELOC_MICROMIPS_JMP:
2687     case BFD_RELOC_MICROMIPS_HI16:
2688     case BFD_RELOC_MICROMIPS_HI16_S:
2689     case BFD_RELOC_MICROMIPS_LO16:
2690     case BFD_RELOC_MICROMIPS_LITERAL:
2691     case BFD_RELOC_MICROMIPS_GOT16:
2692     case BFD_RELOC_MICROMIPS_CALL16:
2693     case BFD_RELOC_MICROMIPS_GOT_HI16:
2694     case BFD_RELOC_MICROMIPS_GOT_LO16:
2695     case BFD_RELOC_MICROMIPS_CALL_HI16:
2696     case BFD_RELOC_MICROMIPS_CALL_LO16:
2697     case BFD_RELOC_MICROMIPS_SUB:
2698     case BFD_RELOC_MICROMIPS_GOT_PAGE:
2699     case BFD_RELOC_MICROMIPS_GOT_OFST:
2700     case BFD_RELOC_MICROMIPS_GOT_DISP:
2701     case BFD_RELOC_MICROMIPS_HIGHEST:
2702     case BFD_RELOC_MICROMIPS_HIGHER:
2703     case BFD_RELOC_MICROMIPS_SCN_DISP:
2704     case BFD_RELOC_MICROMIPS_JALR:
2705       return TRUE;
2706
2707     default:
2708       return FALSE;
2709     }
2710 }
2711
2712 static inline bfd_boolean
2713 jmp_reloc_p (bfd_reloc_code_real_type reloc)
2714 {
2715   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2716 }
2717
2718 static inline bfd_boolean
2719 got16_reloc_p (bfd_reloc_code_real_type reloc)
2720 {
2721   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
2722           || reloc == BFD_RELOC_MICROMIPS_GOT16);
2723 }
2724
2725 static inline bfd_boolean
2726 hi16_reloc_p (bfd_reloc_code_real_type reloc)
2727 {
2728   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
2729           || reloc == BFD_RELOC_MICROMIPS_HI16_S);
2730 }
2731
2732 static inline bfd_boolean
2733 lo16_reloc_p (bfd_reloc_code_real_type reloc)
2734 {
2735   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
2736           || reloc == BFD_RELOC_MICROMIPS_LO16);
2737 }
2738
2739 static inline bfd_boolean
2740 jalr_reloc_p (bfd_reloc_code_real_type reloc)
2741 {
2742   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
2743 }
2744
2745 /* Return true if the given relocation might need a matching %lo().
2746    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2747    need a matching %lo() when applied to local symbols.  */
2748
2749 static inline bfd_boolean
2750 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2751 {
2752   return (HAVE_IN_PLACE_ADDENDS
2753           && (hi16_reloc_p (reloc)
2754               /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2755                  all GOT16 relocations evaluate to "G".  */
2756               || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2757 }
2758
2759 /* Return the type of %lo() reloc needed by RELOC, given that
2760    reloc_needs_lo_p.  */
2761
2762 static inline bfd_reloc_code_real_type
2763 matching_lo_reloc (bfd_reloc_code_real_type reloc)
2764 {
2765   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2766           : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2767              : BFD_RELOC_LO16));
2768 }
2769
2770 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2771    relocation.  */
2772
2773 static inline bfd_boolean
2774 fixup_has_matching_lo_p (fixS *fixp)
2775 {
2776   return (fixp->fx_next != NULL
2777           && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
2778           && fixp->fx_addsy == fixp->fx_next->fx_addsy
2779           && fixp->fx_offset == fixp->fx_next->fx_offset);
2780 }
2781
2782 /* This function returns true if modifying a register requires a
2783    delay.  */
2784
2785 static int
2786 reg_needs_delay (unsigned int reg)
2787 {
2788   unsigned long prev_pinfo;
2789
2790   prev_pinfo = history[0].insn_mo->pinfo;
2791   if (! mips_opts.noreorder
2792       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2793            && ! gpr_interlocks)
2794           || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2795               && ! cop_interlocks)))
2796     {
2797       /* A load from a coprocessor or from memory.  All load delays
2798          delay the use of general register rt for one instruction.  */
2799       /* Itbl support may require additional care here.  */
2800       know (prev_pinfo & INSN_WRITE_GPR_T);
2801       if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
2802         return 1;
2803     }
2804
2805   return 0;
2806 }
2807
2808 /* Move all labels in LABELS to the current insertion point.  TEXT_P
2809    says whether the labels refer to text or data.  */
2810
2811 static void
2812 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
2813 {
2814   struct insn_label_list *l;
2815   valueT val;
2816
2817   for (l = labels; l != NULL; l = l->next)
2818     {
2819       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
2820       symbol_set_frag (l->label, frag_now);
2821       val = (valueT) frag_now_fix ();
2822       /* MIPS16/microMIPS text labels are stored as odd.  */
2823       if (text_p && HAVE_CODE_COMPRESSION)
2824         ++val;
2825       S_SET_VALUE (l->label, val);
2826     }
2827 }
2828
2829 /* Move all labels in insn_labels to the current insertion point
2830    and treat them as text labels.  */
2831
2832 static void
2833 mips_move_text_labels (void)
2834 {
2835   mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2836 }
2837
2838 static bfd_boolean
2839 s_is_linkonce (symbolS *sym, segT from_seg)
2840 {
2841   bfd_boolean linkonce = FALSE;
2842   segT symseg = S_GET_SEGMENT (sym);
2843
2844   if (symseg != from_seg && !S_IS_LOCAL (sym))
2845     {
2846       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2847         linkonce = TRUE;
2848 #ifdef OBJ_ELF
2849       /* The GNU toolchain uses an extension for ELF: a section
2850          beginning with the magic string .gnu.linkonce is a
2851          linkonce section.  */
2852       if (strncmp (segment_name (symseg), ".gnu.linkonce",
2853                    sizeof ".gnu.linkonce" - 1) == 0)
2854         linkonce = TRUE;
2855 #endif
2856     }
2857   return linkonce;
2858 }
2859
2860 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
2861    linker to handle them specially, such as generating jalx instructions
2862    when needed.  We also make them odd for the duration of the assembly,
2863    in order to generate the right sort of code.  We will make them even
2864    in the adjust_symtab routine, while leaving them marked.  This is
2865    convenient for the debugger and the disassembler.  The linker knows
2866    to make them odd again.  */
2867
2868 static void
2869 mips_compressed_mark_label (symbolS *label)
2870 {
2871   gas_assert (HAVE_CODE_COMPRESSION);
2872
2873 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2874   if (IS_ELF)
2875     {
2876       if (mips_opts.mips16)
2877         S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2878       else
2879         S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
2880     }
2881 #endif
2882   if ((S_GET_VALUE (label) & 1) == 0
2883       /* Don't adjust the address if the label is global or weak, or
2884          in a link-once section, since we'll be emitting symbol reloc
2885          references to it which will be patched up by the linker, and
2886          the final value of the symbol may or may not be MIPS16/microMIPS.  */
2887       && !S_IS_WEAK (label)
2888       && !S_IS_EXTERNAL (label)
2889       && !s_is_linkonce (label, now_seg))
2890     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2891 }
2892
2893 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
2894
2895 static void
2896 mips_compressed_mark_labels (void)
2897 {
2898   struct insn_label_list *l;
2899
2900   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
2901     mips_compressed_mark_label (l->label);
2902 }
2903
2904 /* End the current frag.  Make it a variant frag and record the
2905    relaxation info.  */
2906
2907 static void
2908 relax_close_frag (void)
2909 {
2910   mips_macro_warning.first_frag = frag_now;
2911   frag_var (rs_machine_dependent, 0, 0,
2912             RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2913             mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2914
2915   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2916   mips_relax.first_fixup = 0;
2917 }
2918
2919 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2920    See the comment above RELAX_ENCODE for more details.  */
2921
2922 static void
2923 relax_start (symbolS *symbol)
2924 {
2925   gas_assert (mips_relax.sequence == 0);
2926   mips_relax.sequence = 1;
2927   mips_relax.symbol = symbol;
2928 }
2929
2930 /* Start generating the second version of a relaxable sequence.
2931    See the comment above RELAX_ENCODE for more details.  */
2932
2933 static void
2934 relax_switch (void)
2935 {
2936   gas_assert (mips_relax.sequence == 1);
2937   mips_relax.sequence = 2;
2938 }
2939
2940 /* End the current relaxable sequence.  */
2941
2942 static void
2943 relax_end (void)
2944 {
2945   gas_assert (mips_relax.sequence == 2);
2946   relax_close_frag ();
2947   mips_relax.sequence = 0;
2948 }
2949
2950 /* Return true if IP is a delayed branch or jump.  */
2951
2952 static inline bfd_boolean
2953 delayed_branch_p (const struct mips_cl_insn *ip)
2954 {
2955   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2956                                 | INSN_COND_BRANCH_DELAY
2957                                 | INSN_COND_BRANCH_LIKELY)) != 0;
2958 }
2959
2960 /* Return true if IP is a compact branch or jump.  */
2961
2962 static inline bfd_boolean
2963 compact_branch_p (const struct mips_cl_insn *ip)
2964 {
2965   if (mips_opts.mips16)
2966     return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2967                                   | MIPS16_INSN_COND_BRANCH)) != 0;
2968   else
2969     return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2970                                    | INSN2_COND_BRANCH)) != 0;
2971 }
2972
2973 /* Return true if IP is an unconditional branch or jump.  */
2974
2975 static inline bfd_boolean
2976 uncond_branch_p (const struct mips_cl_insn *ip)
2977 {
2978   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2979           || (mips_opts.mips16
2980               ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2981               : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2982 }
2983
2984 /* Return true if IP is a branch-likely instruction.  */
2985
2986 static inline bfd_boolean
2987 branch_likely_p (const struct mips_cl_insn *ip)
2988 {
2989   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2990 }
2991
2992 /* Return the type of nop that should be used to fill the delay slot
2993    of delayed branch IP.  */
2994
2995 static struct mips_cl_insn *
2996 get_delay_slot_nop (const struct mips_cl_insn *ip)
2997 {
2998   if (mips_opts.micromips
2999       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3000     return &micromips_nop32_insn;
3001   return NOP_INSN;
3002 }
3003
3004 /* Return the mask of core registers that IP reads or writes.  */
3005
3006 static unsigned int
3007 gpr_mod_mask (const struct mips_cl_insn *ip)
3008 {
3009   unsigned long pinfo2;
3010   unsigned int mask;
3011
3012   mask = 0;
3013   pinfo2 = ip->insn_mo->pinfo2;
3014   if (mips_opts.micromips)
3015     {
3016       if (pinfo2 & INSN2_MOD_GPR_MD)
3017         mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
3018       if (pinfo2 & INSN2_MOD_GPR_MF)
3019         mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
3020       if (pinfo2 & INSN2_MOD_SP)
3021         mask |= 1 << SP;
3022     }
3023   return mask;
3024 }
3025
3026 /* Return the mask of core registers that IP reads.  */
3027
3028 static unsigned int
3029 gpr_read_mask (const struct mips_cl_insn *ip)
3030 {
3031   unsigned long pinfo, pinfo2;
3032   unsigned int mask;
3033
3034   mask = gpr_mod_mask (ip);
3035   pinfo = ip->insn_mo->pinfo;
3036   pinfo2 = ip->insn_mo->pinfo2;
3037   if (mips_opts.mips16)
3038     {
3039       if (pinfo & MIPS16_INSN_READ_X)
3040         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3041       if (pinfo & MIPS16_INSN_READ_Y)
3042         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3043       if (pinfo & MIPS16_INSN_READ_T)
3044         mask |= 1 << TREG;
3045       if (pinfo & MIPS16_INSN_READ_SP)
3046         mask |= 1 << SP;
3047       if (pinfo & MIPS16_INSN_READ_31)
3048         mask |= 1 << RA;
3049       if (pinfo & MIPS16_INSN_READ_Z)
3050         mask |= 1 << (mips16_to_32_reg_map
3051                       [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3052       if (pinfo & MIPS16_INSN_READ_GPR_X)
3053         mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3054     }
3055   else
3056     {
3057       if (pinfo2 & INSN2_READ_GPR_D)
3058         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3059       if (pinfo & INSN_READ_GPR_T)
3060         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3061       if (pinfo & INSN_READ_GPR_S)
3062         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3063       if (pinfo2 & INSN2_READ_GP)
3064         mask |= 1 << GP;
3065       if (pinfo2 & INSN2_READ_GPR_31)
3066         mask |= 1 << RA;
3067       if (pinfo2 & INSN2_READ_GPR_Z)
3068         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3069     }
3070   if (mips_opts.micromips)
3071     {
3072       if (pinfo2 & INSN2_READ_GPR_MC)
3073         mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3074       if (pinfo2 & INSN2_READ_GPR_ME)
3075         mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3076       if (pinfo2 & INSN2_READ_GPR_MG)
3077         mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3078       if (pinfo2 & INSN2_READ_GPR_MJ)
3079         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3080       if (pinfo2 & INSN2_READ_GPR_MMN)
3081         {
3082           mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3083           mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3084         }
3085       if (pinfo2 & INSN2_READ_GPR_MP)
3086         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3087       if (pinfo2 & INSN2_READ_GPR_MQ)
3088         mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3089     }
3090   /* Don't include register 0.  */
3091   return mask & ~1;
3092 }
3093
3094 /* Return the mask of core registers that IP writes.  */
3095
3096 static unsigned int
3097 gpr_write_mask (const struct mips_cl_insn *ip)
3098 {
3099   unsigned long pinfo, pinfo2;
3100   unsigned int mask;
3101
3102   mask = gpr_mod_mask (ip);
3103   pinfo = ip->insn_mo->pinfo;
3104   pinfo2 = ip->insn_mo->pinfo2;
3105   if (mips_opts.mips16)
3106     {
3107       if (pinfo & MIPS16_INSN_WRITE_X)
3108         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3109       if (pinfo & MIPS16_INSN_WRITE_Y)
3110         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3111       if (pinfo & MIPS16_INSN_WRITE_Z)
3112         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3113       if (pinfo & MIPS16_INSN_WRITE_T)
3114         mask |= 1 << TREG;
3115       if (pinfo & MIPS16_INSN_WRITE_SP)
3116         mask |= 1 << SP;
3117       if (pinfo & MIPS16_INSN_WRITE_31)
3118         mask |= 1 << RA;
3119       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3120         mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3121     }
3122   else
3123     {
3124       if (pinfo & INSN_WRITE_GPR_D)
3125         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3126       if (pinfo & INSN_WRITE_GPR_T)
3127         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3128       if (pinfo & INSN_WRITE_GPR_S)
3129         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3130       if (pinfo & INSN_WRITE_GPR_31)
3131         mask |= 1 << RA;
3132       if (pinfo2 & INSN2_WRITE_GPR_Z)
3133         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3134     }
3135   if (mips_opts.micromips)
3136     {
3137       if (pinfo2 & INSN2_WRITE_GPR_MB)
3138         mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3139       if (pinfo2 & INSN2_WRITE_GPR_MHI)
3140         {
3141           mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3142           mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3143         }
3144       if (pinfo2 & INSN2_WRITE_GPR_MJ)
3145         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3146       if (pinfo2 & INSN2_WRITE_GPR_MP)
3147         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3148     }
3149   /* Don't include register 0.  */
3150   return mask & ~1;
3151 }
3152
3153 /* Return the mask of floating-point registers that IP reads.  */
3154
3155 static unsigned int
3156 fpr_read_mask (const struct mips_cl_insn *ip)
3157 {
3158   unsigned long pinfo, pinfo2;
3159   unsigned int mask;
3160
3161   mask = 0;
3162   pinfo = ip->insn_mo->pinfo;
3163   pinfo2 = ip->insn_mo->pinfo2;
3164   if (!mips_opts.mips16)
3165     {
3166       if (pinfo2 & INSN2_READ_FPR_D)
3167         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3168       if (pinfo & INSN_READ_FPR_S)
3169         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3170       if (pinfo & INSN_READ_FPR_T)
3171         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3172       if (pinfo & INSN_READ_FPR_R)
3173         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3174       if (pinfo2 & INSN2_READ_FPR_Z)
3175         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3176     }
3177   /* Conservatively treat all operands to an FP_D instruction are doubles.
3178      (This is overly pessimistic for things like cvt.d.s.)  */
3179   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3180     mask |= mask << 1;
3181   return mask;
3182 }
3183
3184 /* Return the mask of floating-point registers that IP writes.  */
3185
3186 static unsigned int
3187 fpr_write_mask (const struct mips_cl_insn *ip)
3188 {
3189   unsigned long pinfo, pinfo2;
3190   unsigned int mask;
3191
3192   mask = 0;
3193   pinfo = ip->insn_mo->pinfo;
3194   pinfo2 = ip->insn_mo->pinfo2;
3195   if (!mips_opts.mips16)
3196     {
3197       if (pinfo & INSN_WRITE_FPR_D)
3198         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3199       if (pinfo & INSN_WRITE_FPR_S)
3200         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3201       if (pinfo & INSN_WRITE_FPR_T)
3202         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3203       if (pinfo2 & INSN2_WRITE_FPR_Z)
3204         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3205     }
3206   /* Conservatively treat all operands to an FP_D instruction are doubles.
3207      (This is overly pessimistic for things like cvt.s.d.)  */
3208   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3209     mask |= mask << 1;
3210   return mask;
3211 }
3212
3213 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3214    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3215    by VR4120 errata.  */
3216
3217 static unsigned int
3218 classify_vr4120_insn (const char *name)
3219 {
3220   if (strncmp (name, "macc", 4) == 0)
3221     return FIX_VR4120_MACC;
3222   if (strncmp (name, "dmacc", 5) == 0)
3223     return FIX_VR4120_DMACC;
3224   if (strncmp (name, "mult", 4) == 0)
3225     return FIX_VR4120_MULT;
3226   if (strncmp (name, "dmult", 5) == 0)
3227     return FIX_VR4120_DMULT;
3228   if (strstr (name, "div"))
3229     return FIX_VR4120_DIV;
3230   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3231     return FIX_VR4120_MTHILO;
3232   return NUM_FIX_VR4120_CLASSES;
3233 }
3234
3235 #define INSN_ERET  0x42000018
3236 #define INSN_DERET 0x4200001f
3237
3238 /* Return the number of instructions that must separate INSN1 and INSN2,
3239    where INSN1 is the earlier instruction.  Return the worst-case value
3240    for any INSN2 if INSN2 is null.  */
3241
3242 static unsigned int
3243 insns_between (const struct mips_cl_insn *insn1,
3244                const struct mips_cl_insn *insn2)
3245 {
3246   unsigned long pinfo1, pinfo2;
3247   unsigned int mask;
3248
3249   /* This function needs to know which pinfo flags are set for INSN2
3250      and which registers INSN2 uses.  The former is stored in PINFO2 and
3251      the latter is tested via INSN2_USES_GPR.  If INSN2 is null, PINFO2
3252      will have every flag set and INSN2_USES_GPR will always return true.  */
3253   pinfo1 = insn1->insn_mo->pinfo;
3254   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3255
3256 #define INSN2_USES_GPR(REG) \
3257   (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3258
3259   /* For most targets, write-after-read dependencies on the HI and LO
3260      registers must be separated by at least two instructions.  */
3261   if (!hilo_interlocks)
3262     {
3263       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3264         return 2;
3265       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3266         return 2;
3267     }
3268
3269   /* If we're working around r7000 errata, there must be two instructions
3270      between an mfhi or mflo and any instruction that uses the result.  */
3271   if (mips_7000_hilo_fix
3272       && !mips_opts.micromips
3273       && MF_HILO_INSN (pinfo1)
3274       && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3275     return 2;
3276
3277   /* If we're working around 24K errata, one instruction is required
3278      if an ERET or DERET is followed by a branch instruction.  */
3279   if (mips_fix_24k && !mips_opts.micromips)
3280     {
3281       if (insn1->insn_opcode == INSN_ERET
3282           || insn1->insn_opcode == INSN_DERET)
3283         {
3284           if (insn2 == NULL
3285               || insn2->insn_opcode == INSN_ERET
3286               || insn2->insn_opcode == INSN_DERET
3287               || delayed_branch_p (insn2))
3288             return 1;
3289         }
3290     }
3291
3292   /* If working around VR4120 errata, check for combinations that need
3293      a single intervening instruction.  */
3294   if (mips_fix_vr4120 && !mips_opts.micromips)
3295     {
3296       unsigned int class1, class2;
3297
3298       class1 = classify_vr4120_insn (insn1->insn_mo->name);
3299       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3300         {
3301           if (insn2 == NULL)
3302             return 1;
3303           class2 = classify_vr4120_insn (insn2->insn_mo->name);
3304           if (vr4120_conflicts[class1] & (1 << class2))
3305             return 1;
3306         }
3307     }
3308
3309   if (!HAVE_CODE_COMPRESSION)
3310     {
3311       /* Check for GPR or coprocessor load delays.  All such delays
3312          are on the RT register.  */
3313       /* Itbl support may require additional care here.  */
3314       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3315           || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3316         {
3317           know (pinfo1 & INSN_WRITE_GPR_T);
3318           if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3319             return 1;
3320         }
3321
3322       /* Check for generic coprocessor hazards.
3323
3324          This case is not handled very well.  There is no special
3325          knowledge of CP0 handling, and the coprocessors other than
3326          the floating point unit are not distinguished at all.  */
3327       /* Itbl support may require additional care here. FIXME!
3328          Need to modify this to include knowledge about
3329          user specified delays!  */
3330       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3331                || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3332         {
3333           /* Handle cases where INSN1 writes to a known general coprocessor
3334              register.  There must be a one instruction delay before INSN2
3335              if INSN2 reads that register, otherwise no delay is needed.  */
3336           mask = fpr_write_mask (insn1);
3337           if (mask != 0)
3338             {
3339               if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3340                 return 1;
3341             }
3342           else
3343             {
3344               /* Read-after-write dependencies on the control registers
3345                  require a two-instruction gap.  */
3346               if ((pinfo1 & INSN_WRITE_COND_CODE)
3347                   && (pinfo2 & INSN_READ_COND_CODE))
3348                 return 2;
3349
3350               /* We don't know exactly what INSN1 does.  If INSN2 is
3351                  also a coprocessor instruction, assume there must be
3352                  a one instruction gap.  */
3353               if (pinfo2 & INSN_COP)
3354                 return 1;
3355             }
3356         }
3357
3358       /* Check for read-after-write dependencies on the coprocessor
3359          control registers in cases where INSN1 does not need a general
3360          coprocessor delay.  This means that INSN1 is a floating point
3361          comparison instruction.  */
3362       /* Itbl support may require additional care here.  */
3363       else if (!cop_interlocks
3364                && (pinfo1 & INSN_WRITE_COND_CODE)
3365                && (pinfo2 & INSN_READ_COND_CODE))
3366         return 1;
3367     }
3368
3369 #undef INSN2_USES_GPR
3370
3371   return 0;
3372 }
3373
3374 /* Return the number of nops that would be needed to work around the
3375    VR4130 mflo/mfhi errata if instruction INSN immediately followed
3376    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
3377    that are contained within the first IGNORE instructions of HIST.  */
3378
3379 static int
3380 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3381                  const struct mips_cl_insn *insn)
3382 {
3383   int i, j;
3384   unsigned int mask;
3385
3386   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
3387      are not affected by the errata.  */
3388   if (insn != 0
3389       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3390           || strcmp (insn->insn_mo->name, "mtlo") == 0
3391           || strcmp (insn->insn_mo->name, "mthi") == 0))
3392     return 0;
3393
3394   /* Search for the first MFLO or MFHI.  */
3395   for (i = 0; i < MAX_VR4130_NOPS; i++)
3396     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3397       {
3398         /* Extract the destination register.  */
3399         mask = gpr_write_mask (&hist[i]);
3400
3401         /* No nops are needed if INSN reads that register.  */
3402         if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3403           return 0;
3404
3405         /* ...or if any of the intervening instructions do.  */
3406         for (j = 0; j < i; j++)
3407           if (gpr_read_mask (&hist[j]) & mask)
3408             return 0;
3409
3410         if (i >= ignore)
3411           return MAX_VR4130_NOPS - i;
3412       }
3413   return 0;
3414 }
3415
3416 #define BASE_REG_EQ(INSN1, INSN2)       \
3417   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3418       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3419
3420 /* Return the minimum alignment for this store instruction.  */
3421
3422 static int
3423 fix_24k_align_to (const struct mips_opcode *mo)
3424 {
3425   if (strcmp (mo->name, "sh") == 0)
3426     return 2;
3427
3428   if (strcmp (mo->name, "swc1") == 0
3429       || strcmp (mo->name, "swc2") == 0
3430       || strcmp (mo->name, "sw") == 0
3431       || strcmp (mo->name, "sc") == 0
3432       || strcmp (mo->name, "s.s") == 0)
3433     return 4;
3434
3435   if (strcmp (mo->name, "sdc1") == 0
3436       || strcmp (mo->name, "sdc2") == 0
3437       || strcmp (mo->name, "s.d") == 0)
3438     return 8;
3439
3440   /* sb, swl, swr */
3441   return 1;
3442 }
3443
3444 struct fix_24k_store_info
3445   {
3446     /* Immediate offset, if any, for this store instruction.  */
3447     short off;
3448     /* Alignment required by this store instruction.  */
3449     int align_to;
3450     /* True for register offsets.  */
3451     int register_offset;
3452   };
3453
3454 /* Comparison function used by qsort.  */
3455
3456 static int
3457 fix_24k_sort (const void *a, const void *b)
3458 {
3459   const struct fix_24k_store_info *pos1 = a;
3460   const struct fix_24k_store_info *pos2 = b;
3461
3462   return (pos1->off - pos2->off);
3463 }
3464
3465 /* INSN is a store instruction.  Try to record the store information
3466    in STINFO.  Return false if the information isn't known.  */
3467
3468 static bfd_boolean
3469 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3470                            const struct mips_cl_insn *insn)
3471 {
3472   /* The instruction must have a known offset.  */
3473   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3474     return FALSE;
3475
3476   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3477   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3478   return TRUE;
3479 }
3480
3481 /* Return the number of nops that would be needed to work around the 24k
3482    "lost data on stores during refill" errata if instruction INSN
3483    immediately followed the 2 instructions described by HIST.
3484    Ignore hazards that are contained within the first IGNORE
3485    instructions of HIST.
3486
3487    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3488    for the data cache refills and store data. The following describes
3489    the scenario where the store data could be lost.
3490
3491    * A data cache miss, due to either a load or a store, causing fill
3492      data to be supplied by the memory subsystem
3493    * The first three doublewords of fill data are returned and written
3494      into the cache
3495    * A sequence of four stores occurs in consecutive cycles around the
3496      final doubleword of the fill:
3497    * Store A
3498    * Store B
3499    * Store C
3500    * Zero, One or more instructions
3501    * Store D
3502
3503    The four stores A-D must be to different doublewords of the line that
3504    is being filled. The fourth instruction in the sequence above permits
3505    the fill of the final doubleword to be transferred from the FSB into
3506    the cache. In the sequence above, the stores may be either integer
3507    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3508    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3509    different doublewords on the line. If the floating point unit is
3510    running in 1:2 mode, it is not possible to create the sequence above
3511    using only floating point store instructions.
3512
3513    In this case, the cache line being filled is incorrectly marked
3514    invalid, thereby losing the data from any store to the line that
3515    occurs between the original miss and the completion of the five
3516    cycle sequence shown above.
3517
3518    The workarounds are:
3519
3520    * Run the data cache in write-through mode.
3521    * Insert a non-store instruction between
3522      Store A and Store B or Store B and Store C.  */
3523   
3524 static int
3525 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3526               const struct mips_cl_insn *insn)
3527 {
3528   struct fix_24k_store_info pos[3];
3529   int align, i, base_offset;
3530
3531   if (ignore >= 2)
3532     return 0;
3533
3534   /* If the previous instruction wasn't a store, there's nothing to
3535      worry about.  */
3536   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3537     return 0;
3538
3539   /* If the instructions after the previous one are unknown, we have
3540      to assume the worst.  */
3541   if (!insn)
3542     return 1;
3543
3544   /* Check whether we are dealing with three consecutive stores.  */
3545   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3546       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3547     return 0;
3548
3549   /* If we don't know the relationship between the store addresses,
3550      assume the worst.  */
3551   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3552       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3553     return 1;
3554
3555   if (!fix_24k_record_store_info (&pos[0], insn)
3556       || !fix_24k_record_store_info (&pos[1], &hist[0])
3557       || !fix_24k_record_store_info (&pos[2], &hist[1]))
3558     return 1;
3559
3560   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3561
3562   /* Pick a value of ALIGN and X such that all offsets are adjusted by
3563      X bytes and such that the base register + X is known to be aligned
3564      to align bytes.  */
3565
3566   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3567     align = 8;
3568   else
3569     {
3570       align = pos[0].align_to;
3571       base_offset = pos[0].off;
3572       for (i = 1; i < 3; i++)
3573         if (align < pos[i].align_to)
3574           {
3575             align = pos[i].align_to;
3576             base_offset = pos[i].off;
3577           }
3578       for (i = 0; i < 3; i++)
3579         pos[i].off -= base_offset;
3580     }
3581
3582   pos[0].off &= ~align + 1;
3583   pos[1].off &= ~align + 1;
3584   pos[2].off &= ~align + 1;
3585
3586   /* If any two stores write to the same chunk, they also write to the
3587      same doubleword.  The offsets are still sorted at this point.  */
3588   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3589     return 0;
3590
3591   /* A range of at least 9 bytes is needed for the stores to be in
3592      non-overlapping doublewords.  */
3593   if (pos[2].off - pos[0].off <= 8)
3594     return 0;
3595
3596   if (pos[2].off - pos[1].off >= 24
3597       || pos[1].off - pos[0].off >= 24
3598       || pos[2].off - pos[0].off >= 32)
3599     return 0;
3600
3601   return 1;
3602 }
3603
3604 /* Return the number of nops that would be needed if instruction INSN
3605    immediately followed the MAX_NOPS instructions given by HIST,
3606    where HIST[0] is the most recent instruction.  Ignore hazards
3607    between INSN and the first IGNORE instructions in HIST.
3608
3609    If INSN is null, return the worse-case number of nops for any
3610    instruction.  */
3611
3612 static int
3613 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3614                const struct mips_cl_insn *insn)
3615 {
3616   int i, nops, tmp_nops;
3617
3618   nops = 0;
3619   for (i = ignore; i < MAX_DELAY_NOPS; i++)
3620     {
3621       tmp_nops = insns_between (hist + i, insn) - i;
3622       if (tmp_nops > nops)
3623         nops = tmp_nops;
3624     }
3625
3626   if (mips_fix_vr4130 && !mips_opts.micromips)
3627     {
3628       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
3629       if (tmp_nops > nops)
3630         nops = tmp_nops;
3631     }
3632
3633   if (mips_fix_24k && !mips_opts.micromips)
3634     {
3635       tmp_nops = nops_for_24k (ignore, hist, insn);
3636       if (tmp_nops > nops)
3637         nops = tmp_nops;
3638     }
3639
3640   return nops;
3641 }
3642
3643 /* The variable arguments provide NUM_INSNS extra instructions that
3644    might be added to HIST.  Return the largest number of nops that
3645    would be needed after the extended sequence, ignoring hazards
3646    in the first IGNORE instructions.  */
3647
3648 static int
3649 nops_for_sequence (int num_insns, int ignore,
3650                    const struct mips_cl_insn *hist, ...)
3651 {
3652   va_list args;
3653   struct mips_cl_insn buffer[MAX_NOPS];
3654   struct mips_cl_insn *cursor;
3655   int nops;
3656
3657   va_start (args, hist);
3658   cursor = buffer + num_insns;
3659   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
3660   while (cursor > buffer)
3661     *--cursor = *va_arg (args, const struct mips_cl_insn *);
3662
3663   nops = nops_for_insn (ignore, buffer, NULL);
3664   va_end (args);
3665   return nops;
3666 }
3667
3668 /* Like nops_for_insn, but if INSN is a branch, take into account the
3669    worst-case delay for the branch target.  */
3670
3671 static int
3672 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
3673                          const struct mips_cl_insn *insn)
3674 {
3675   int nops, tmp_nops;
3676
3677   nops = nops_for_insn (ignore, hist, insn);
3678   if (delayed_branch_p (insn))
3679     {
3680       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
3681                                     hist, insn, get_delay_slot_nop (insn));
3682       if (tmp_nops > nops)
3683         nops = tmp_nops;
3684     }
3685   else if (compact_branch_p (insn))
3686     {
3687       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
3688       if (tmp_nops > nops)
3689         nops = tmp_nops;
3690     }
3691   return nops;
3692 }
3693
3694 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
3695
3696 static void
3697 fix_loongson2f_nop (struct mips_cl_insn * ip)
3698 {
3699   gas_assert (!HAVE_CODE_COMPRESSION);
3700   if (strcmp (ip->insn_mo->name, "nop") == 0)
3701     ip->insn_opcode = LOONGSON2F_NOP_INSN;
3702 }
3703
3704 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3705                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
3706
3707 static void
3708 fix_loongson2f_jump (struct mips_cl_insn * ip)
3709 {
3710   gas_assert (!HAVE_CODE_COMPRESSION);
3711   if (strcmp (ip->insn_mo->name, "j") == 0
3712       || strcmp (ip->insn_mo->name, "jr") == 0
3713       || strcmp (ip->insn_mo->name, "jalr") == 0)
3714     {
3715       int sreg;
3716       expressionS ep;
3717
3718       if (! mips_opts.at)
3719         return;
3720
3721       sreg = EXTRACT_OPERAND (0, RS, *ip);
3722       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3723         return;
3724
3725       ep.X_op = O_constant;
3726       ep.X_add_number = 0xcfff0000;
3727       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3728       ep.X_add_number = 0xffff;
3729       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3730       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3731     }
3732 }
3733
3734 static void
3735 fix_loongson2f (struct mips_cl_insn * ip)
3736 {
3737   if (mips_fix_loongson2f_nop)
3738     fix_loongson2f_nop (ip);
3739
3740   if (mips_fix_loongson2f_jump)
3741     fix_loongson2f_jump (ip);
3742 }
3743
3744 /* IP is a branch that has a delay slot, and we need to fill it
3745    automatically.   Return true if we can do that by swapping IP
3746    with the previous instruction.
3747    ADDRESS_EXPR is an operand of the instruction to be used with
3748    RELOC_TYPE.  */
3749
3750 static bfd_boolean
3751 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
3752   bfd_reloc_code_real_type *reloc_type)
3753 {
3754   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
3755   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3756
3757   /* -O2 and above is required for this optimization.  */
3758   if (mips_optimize < 2)
3759     return FALSE;
3760
3761   /* If we have seen .set volatile or .set nomove, don't optimize.  */
3762   if (mips_opts.nomove)
3763     return FALSE;
3764
3765   /* We can't swap if the previous instruction's position is fixed.  */
3766   if (history[0].fixed_p)
3767     return FALSE;
3768
3769   /* If the previous previous insn was in a .set noreorder, we can't
3770      swap.  Actually, the MIPS assembler will swap in this situation.
3771      However, gcc configured -with-gnu-as will generate code like
3772
3773         .set    noreorder
3774         lw      $4,XXX
3775         .set    reorder
3776         INSN
3777         bne     $4,$0,foo
3778
3779      in which we can not swap the bne and INSN.  If gcc is not configured
3780      -with-gnu-as, it does not output the .set pseudo-ops.  */
3781   if (history[1].noreorder_p)
3782     return FALSE;
3783
3784   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3785      This means that the previous instruction was a 4-byte one anyhow.  */
3786   if (mips_opts.mips16 && history[0].fixp[0])
3787     return FALSE;
3788
3789   /* If the branch is itself the target of a branch, we can not swap.
3790      We cheat on this; all we check for is whether there is a label on
3791      this instruction.  If there are any branches to anything other than
3792      a label, users must use .set noreorder.  */
3793   if (seg_info (now_seg)->label_list)
3794     return FALSE;
3795
3796   /* If the previous instruction is in a variant frag other than this
3797      branch's one, we cannot do the swap.  This does not apply to
3798      MIPS16 code, which uses variant frags for different purposes.  */
3799   if (!mips_opts.mips16
3800       && history[0].frag
3801       && history[0].frag->fr_type == rs_machine_dependent)
3802     return FALSE;
3803
3804   /* We do not swap with instructions that cannot architecturally
3805      be placed in a branch delay slot, such as SYNC or ERET.  We
3806      also refrain from swapping with a trap instruction, since it
3807      complicates trap handlers to have the trap instruction be in
3808      a delay slot.  */
3809   prev_pinfo = history[0].insn_mo->pinfo;
3810   if (prev_pinfo & INSN_NO_DELAY_SLOT)
3811     return FALSE;
3812
3813   /* Check for conflicts between the branch and the instructions
3814      before the candidate delay slot.  */
3815   if (nops_for_insn (0, history + 1, ip) > 0)
3816     return FALSE;
3817
3818   /* Check for conflicts between the swapped sequence and the
3819      target of the branch.  */
3820   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3821     return FALSE;
3822
3823   /* If the branch reads a register that the previous
3824      instruction sets, we can not swap.  */
3825   gpr_read = gpr_read_mask (ip);
3826   prev_gpr_write = gpr_write_mask (&history[0]);
3827   if (gpr_read & prev_gpr_write)
3828     return FALSE;
3829
3830   /* If the branch writes a register that the previous
3831      instruction sets, we can not swap.  */
3832   gpr_write = gpr_write_mask (ip);
3833   if (gpr_write & prev_gpr_write)
3834     return FALSE;
3835
3836   /* If the branch writes a register that the previous
3837      instruction reads, we can not swap.  */
3838   prev_gpr_read = gpr_read_mask (&history[0]);
3839   if (gpr_write & prev_gpr_read)
3840     return FALSE;
3841
3842   /* If one instruction sets a condition code and the
3843      other one uses a condition code, we can not swap.  */
3844   pinfo = ip->insn_mo->pinfo;
3845   if ((pinfo & INSN_READ_COND_CODE)
3846       && (prev_pinfo & INSN_WRITE_COND_CODE))
3847     return FALSE;
3848   if ((pinfo & INSN_WRITE_COND_CODE)
3849       && (prev_pinfo & INSN_READ_COND_CODE))
3850     return FALSE;
3851
3852   /* If the previous instruction uses the PC, we can not swap.  */
3853   prev_pinfo2 = history[0].insn_mo->pinfo2;
3854   if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3855     return FALSE;
3856   if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3857     return FALSE;
3858
3859   /* If the previous instruction has an incorrect size for a fixed
3860      branch delay slot in microMIPS mode, we cannot swap.  */
3861   pinfo2 = ip->insn_mo->pinfo2;
3862   if (mips_opts.micromips
3863       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3864       && insn_length (history) != 2)
3865     return FALSE;
3866   if (mips_opts.micromips
3867       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3868       && insn_length (history) != 4)
3869     return FALSE;
3870
3871   /* On R5900 short loops need to be fixed by inserting a nop in
3872      the branch delay slots.
3873      A short loop can be terminated too early.  */
3874   if (mips_opts.arch == CPU_R5900
3875       /* Check if instruction has a parameter, ignore "j $31". */
3876       && (address_expr != NULL)
3877       /* Parameter must be 16 bit. */
3878       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
3879       /* Branch to same segment. */
3880       && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
3881       /* Branch to same code fragment. */
3882       && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
3883       /* Can only calculate branch offset if value is known. */
3884       && symbol_constant_p(address_expr->X_add_symbol)
3885       /* Check if branch is really conditional. */
3886       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
3887         || (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
3888         || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
3889     {
3890       int distance;
3891       /* Check if loop is shorter than 6 instructions including
3892          branch and delay slot.  */
3893       distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
3894       if (distance <= 20)
3895         {
3896           int i;
3897           int rv;
3898
3899           rv = FALSE;
3900           /* When the loop includes branches or jumps,
3901              it is not a short loop. */
3902           for (i = 0; i < (distance / 4); i++)
3903             {
3904               if ((history[i].cleared_p)
3905                   || delayed_branch_p(&history[i]))
3906                 {
3907                   rv = TRUE;
3908                   break;
3909                 }
3910             }
3911           if (rv == FALSE)
3912             {
3913               /* Insert nop after branch to fix short loop. */
3914               return FALSE;
3915             }
3916         }
3917     }
3918
3919   return TRUE;
3920 }
3921
3922 /* Decide how we should add IP to the instruction stream.
3923    ADDRESS_EXPR is an operand of the instruction to be used with
3924    RELOC_TYPE.  */
3925
3926 static enum append_method
3927 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
3928   bfd_reloc_code_real_type *reloc_type)
3929 {
3930   unsigned long pinfo;
3931
3932   /* The relaxed version of a macro sequence must be inherently
3933      hazard-free.  */
3934   if (mips_relax.sequence == 2)
3935     return APPEND_ADD;
3936
3937   /* We must not dabble with instructions in a ".set norerorder" block.  */
3938   if (mips_opts.noreorder)
3939     return APPEND_ADD;
3940
3941   /* Otherwise, it's our responsibility to fill branch delay slots.  */
3942   if (delayed_branch_p (ip))
3943     {
3944       if (!branch_likely_p (ip)
3945           && can_swap_branch_p (ip, address_expr, reloc_type))
3946         return APPEND_SWAP;
3947
3948       pinfo = ip->insn_mo->pinfo;
3949       if (mips_opts.mips16
3950           && ISA_SUPPORTS_MIPS16E
3951           && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3952         return APPEND_ADD_COMPACT;
3953
3954       return APPEND_ADD_WITH_NOP;
3955     }
3956
3957   return APPEND_ADD;
3958 }
3959
3960 /* IP is a MIPS16 instruction whose opcode we have just changed.
3961    Point IP->insn_mo to the new opcode's definition.  */
3962
3963 static void
3964 find_altered_mips16_opcode (struct mips_cl_insn *ip)
3965 {
3966   const struct mips_opcode *mo, *end;
3967
3968   end = &mips16_opcodes[bfd_mips16_num_opcodes];
3969   for (mo = ip->insn_mo; mo < end; mo++)
3970     if ((ip->insn_opcode & mo->mask) == mo->match)
3971       {
3972         ip->insn_mo = mo;
3973         return;
3974       }
3975   abort ();
3976 }
3977
3978 /* For microMIPS macros, we need to generate a local number label
3979    as the target of branches.  */
3980 #define MICROMIPS_LABEL_CHAR            '\037'
3981 static unsigned long micromips_target_label;
3982 static char micromips_target_name[32];
3983
3984 static char *
3985 micromips_label_name (void)
3986 {
3987   char *p = micromips_target_name;
3988   char symbol_name_temporary[24];
3989   unsigned long l;
3990   int i;
3991
3992   if (*p)
3993     return p;
3994
3995   i = 0;
3996   l = micromips_target_label;
3997 #ifdef LOCAL_LABEL_PREFIX
3998   *p++ = LOCAL_LABEL_PREFIX;
3999 #endif
4000   *p++ = 'L';
4001   *p++ = MICROMIPS_LABEL_CHAR;
4002   do
4003     {
4004       symbol_name_temporary[i++] = l % 10 + '0';
4005       l /= 10;
4006     }
4007   while (l != 0);
4008   while (i > 0)
4009     *p++ = symbol_name_temporary[--i];
4010   *p = '\0';
4011
4012   return micromips_target_name;
4013 }
4014
4015 static void
4016 micromips_label_expr (expressionS *label_expr)
4017 {
4018   label_expr->X_op = O_symbol;
4019   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4020   label_expr->X_add_number = 0;
4021 }
4022
4023 static void
4024 micromips_label_inc (void)
4025 {
4026   micromips_target_label++;
4027   *micromips_target_name = '\0';
4028 }
4029
4030 static void
4031 micromips_add_label (void)
4032 {
4033   symbolS *s;
4034
4035   s = colon (micromips_label_name ());
4036   micromips_label_inc ();
4037 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
4038   if (IS_ELF)
4039     S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
4040 #else
4041   (void) s;
4042 #endif
4043 }
4044
4045 /* If assembling microMIPS code, then return the microMIPS reloc
4046    corresponding to the requested one if any.  Otherwise return
4047    the reloc unchanged.  */
4048
4049 static bfd_reloc_code_real_type
4050 micromips_map_reloc (bfd_reloc_code_real_type reloc)
4051 {
4052   static const bfd_reloc_code_real_type relocs[][2] =
4053     {
4054       /* Keep sorted incrementally by the left-hand key.  */
4055       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4056       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4057       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4058       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4059       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4060       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4061       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4062       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4063       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4064       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4065       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4066       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4067       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4068       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4069       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4070       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4071       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4072       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4073       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4074       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4075       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4076       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4077       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4078       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4079       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4080       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4081       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4082     };
4083   bfd_reloc_code_real_type r;
4084   size_t i;
4085
4086   if (!mips_opts.micromips)
4087     return reloc;
4088   for (i = 0; i < ARRAY_SIZE (relocs); i++)
4089     {
4090       r = relocs[i][0];
4091       if (r > reloc)
4092         return reloc;
4093       if (r == reloc)
4094         return relocs[i][1];
4095     }
4096   return reloc;
4097 }
4098
4099 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4100    Return true on success, storing the resolved value in RESULT.  */
4101
4102 static bfd_boolean
4103 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4104                  offsetT *result)
4105 {
4106   switch (reloc)
4107     {
4108     case BFD_RELOC_MIPS_HIGHEST:
4109     case BFD_RELOC_MICROMIPS_HIGHEST:
4110       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4111       return TRUE;
4112
4113     case BFD_RELOC_MIPS_HIGHER:
4114     case BFD_RELOC_MICROMIPS_HIGHER:
4115       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4116       return TRUE;
4117
4118     case BFD_RELOC_HI16_S:
4119     case BFD_RELOC_MICROMIPS_HI16_S:
4120     case BFD_RELOC_MIPS16_HI16_S:
4121       *result = ((operand + 0x8000) >> 16) & 0xffff;
4122       return TRUE;
4123
4124     case BFD_RELOC_HI16:
4125     case BFD_RELOC_MICROMIPS_HI16:
4126     case BFD_RELOC_MIPS16_HI16:
4127       *result = (operand >> 16) & 0xffff;
4128       return TRUE;
4129
4130     case BFD_RELOC_LO16:
4131     case BFD_RELOC_MICROMIPS_LO16:
4132     case BFD_RELOC_MIPS16_LO16:
4133       *result = operand & 0xffff;
4134       return TRUE;
4135
4136     case BFD_RELOC_UNUSED:
4137       *result = operand;
4138       return TRUE;
4139
4140     default:
4141       return FALSE;
4142     }
4143 }
4144
4145 /* Output an instruction.  IP is the instruction information.
4146    ADDRESS_EXPR is an operand of the instruction to be used with
4147    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
4148    a macro expansion.  */
4149
4150 static void
4151 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
4152              bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
4153 {
4154   unsigned long prev_pinfo2, pinfo;
4155   bfd_boolean relaxed_branch = FALSE;
4156   enum append_method method;
4157   bfd_boolean relax32;
4158   int branch_disp;
4159
4160   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4161     fix_loongson2f (ip);
4162
4163   file_ase_mips16 |= mips_opts.mips16;
4164   file_ase_micromips |= mips_opts.micromips;
4165
4166   prev_pinfo2 = history[0].insn_mo->pinfo2;
4167   pinfo = ip->insn_mo->pinfo;
4168
4169   if (mips_opts.micromips
4170       && !expansionp
4171       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4172            && micromips_insn_length (ip->insn_mo) != 2)
4173           || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4174               && micromips_insn_length (ip->insn_mo) != 4)))
4175     as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4176              (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4177
4178   if (address_expr == NULL)
4179     ip->complete_p = 1;
4180   else if (reloc_type[0] <= BFD_RELOC_UNUSED
4181            && reloc_type[1] == BFD_RELOC_UNUSED
4182            && reloc_type[2] == BFD_RELOC_UNUSED
4183            && address_expr->X_op == O_constant)
4184     {
4185       switch (*reloc_type)
4186         {
4187         case BFD_RELOC_MIPS_JMP:
4188           {
4189             int shift;
4190
4191             shift = mips_opts.micromips ? 1 : 2;
4192             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4193               as_bad (_("jump to misaligned address (0x%lx)"),
4194                       (unsigned long) address_expr->X_add_number);
4195             ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4196                                 & 0x3ffffff);
4197             ip->complete_p = 1;
4198           }
4199           break;
4200
4201         case BFD_RELOC_MIPS16_JMP:
4202           if ((address_expr->X_add_number & 3) != 0)
4203             as_bad (_("jump to misaligned address (0x%lx)"),
4204                     (unsigned long) address_expr->X_add_number);
4205           ip->insn_opcode |=
4206             (((address_expr->X_add_number & 0x7c0000) << 3)
4207                | ((address_expr->X_add_number & 0xf800000) >> 7)
4208                | ((address_expr->X_add_number & 0x3fffc) >> 2));
4209           ip->complete_p = 1;
4210           break;
4211
4212         case BFD_RELOC_16_PCREL_S2:
4213           {
4214             int shift;
4215
4216             shift = mips_opts.micromips ? 1 : 2;
4217             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4218               as_bad (_("branch to misaligned address (0x%lx)"),
4219                       (unsigned long) address_expr->X_add_number);
4220             if (!mips_relax_branch)
4221               {
4222                 if ((address_expr->X_add_number + (1 << (shift + 15)))
4223                     & ~((1 << (shift + 16)) - 1))
4224                   as_bad (_("branch address range overflow (0x%lx)"),
4225                           (unsigned long) address_expr->X_add_number);
4226                 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4227                                     & 0xffff);
4228               }
4229           }
4230           break;
4231
4232         default:
4233           {
4234             offsetT value;
4235
4236             if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4237                                  &value))
4238               {
4239                 ip->insn_opcode |= value & 0xffff;
4240                 ip->complete_p = 1;
4241               }
4242           }
4243           break;
4244         }
4245     }
4246
4247   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4248     {
4249       /* There are a lot of optimizations we could do that we don't.
4250          In particular, we do not, in general, reorder instructions.
4251          If you use gcc with optimization, it will reorder
4252          instructions and generally do much more optimization then we
4253          do here; repeating all that work in the assembler would only
4254          benefit hand written assembly code, and does not seem worth
4255          it.  */
4256       int nops = (mips_optimize == 0
4257                   ? nops_for_insn (0, history, NULL)
4258                   : nops_for_insn_or_target (0, history, ip));
4259       if (nops > 0)
4260         {
4261           fragS *old_frag;
4262           unsigned long old_frag_offset;
4263           int i;
4264
4265           old_frag = frag_now;
4266           old_frag_offset = frag_now_fix ();
4267
4268           for (i = 0; i < nops; i++)
4269             add_fixed_insn (NOP_INSN);
4270           insert_into_history (0, nops, NOP_INSN);
4271
4272           if (listing)
4273             {
4274               listing_prev_line ();
4275               /* We may be at the start of a variant frag.  In case we
4276                  are, make sure there is enough space for the frag
4277                  after the frags created by listing_prev_line.  The
4278                  argument to frag_grow here must be at least as large
4279                  as the argument to all other calls to frag_grow in
4280                  this file.  We don't have to worry about being in the
4281                  middle of a variant frag, because the variants insert
4282                  all needed nop instructions themselves.  */
4283               frag_grow (40);
4284             }
4285
4286           mips_move_text_labels ();
4287
4288 #ifndef NO_ECOFF_DEBUGGING
4289           if (ECOFF_DEBUGGING)
4290             ecoff_fix_loc (old_frag, old_frag_offset);
4291 #endif
4292         }
4293     }
4294   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4295     {
4296       int nops;
4297
4298       /* Work out how many nops in prev_nop_frag are needed by IP,
4299          ignoring hazards generated by the first prev_nop_frag_since
4300          instructions.  */
4301       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4302       gas_assert (nops <= prev_nop_frag_holds);
4303
4304       /* Enforce NOPS as a minimum.  */
4305       if (nops > prev_nop_frag_required)
4306         prev_nop_frag_required = nops;
4307
4308       if (prev_nop_frag_holds == prev_nop_frag_required)
4309         {
4310           /* Settle for the current number of nops.  Update the history
4311              accordingly (for the benefit of any future .set reorder code).  */
4312           prev_nop_frag = NULL;
4313           insert_into_history (prev_nop_frag_since,
4314                                prev_nop_frag_holds, NOP_INSN);
4315         }
4316       else
4317         {
4318           /* Allow this instruction to replace one of the nops that was
4319              tentatively added to prev_nop_frag.  */
4320           prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4321           prev_nop_frag_holds--;
4322           prev_nop_frag_since++;
4323         }
4324     }
4325
4326   method = get_append_method (ip, address_expr, reloc_type);
4327   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4328
4329 #ifdef OBJ_ELF
4330   /* The value passed to dwarf2_emit_insn is the distance between
4331      the beginning of the current instruction and the address that
4332      should be recorded in the debug tables.  This is normally the
4333      current address.
4334
4335      For MIPS16/microMIPS debug info we want to use ISA-encoded
4336      addresses, so we use -1 for an address higher by one than the
4337      current one.
4338
4339      If the instruction produced is a branch that we will swap with
4340      the preceding instruction, then we add the displacement by which
4341      the branch will be moved backwards.  This is more appropriate
4342      and for MIPS16/microMIPS code also prevents a debugger from
4343      placing a breakpoint in the middle of the branch (and corrupting
4344      code if software breakpoints are used).  */
4345   dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
4346 #endif
4347
4348   relax32 = (mips_relax_branch
4349              /* Don't try branch relaxation within .set nomacro, or within
4350                 .set noat if we use $at for PIC computations.  If it turns
4351                 out that the branch was out-of-range, we'll get an error.  */
4352              && !mips_opts.warn_about_macros
4353              && (mips_opts.at || mips_pic == NO_PIC)
4354              /* Don't relax BPOSGE32/64 as they have no complementing
4355                 branches.  */
4356              && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
4357
4358   if (!HAVE_CODE_COMPRESSION
4359       && address_expr
4360       && relax32
4361       && *reloc_type == BFD_RELOC_16_PCREL_S2
4362       && delayed_branch_p (ip))
4363     {
4364       relaxed_branch = TRUE;
4365       add_relaxed_insn (ip, (relaxed_branch_length
4366                              (NULL, NULL,
4367                               uncond_branch_p (ip) ? -1
4368                               : branch_likely_p (ip) ? 1
4369                               : 0)), 4,
4370                         RELAX_BRANCH_ENCODE
4371                         (AT,
4372                          uncond_branch_p (ip),
4373                          branch_likely_p (ip),
4374                          pinfo & INSN_WRITE_GPR_31,
4375                          0),
4376                         address_expr->X_add_symbol,
4377                         address_expr->X_add_number);
4378       *reloc_type = BFD_RELOC_UNUSED;
4379     }
4380   else if (mips_opts.micromips
4381            && address_expr
4382            && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4383                || *reloc_type > BFD_RELOC_UNUSED)
4384            && (delayed_branch_p (ip) || compact_branch_p (ip))
4385            /* Don't try branch relaxation when users specify
4386               16-bit/32-bit instructions.  */
4387            && !forced_insn_length)
4388     {
4389       bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4390       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4391       int uncond = uncond_branch_p (ip) ? -1 : 0;
4392       int compact = compact_branch_p (ip);
4393       int al = pinfo & INSN_WRITE_GPR_31;
4394       int length32;
4395
4396       gas_assert (address_expr != NULL);
4397       gas_assert (!mips_relax.sequence);
4398
4399       relaxed_branch = TRUE;
4400       length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4401       add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4402                         RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4403                                                 relax32, 0, 0),
4404                         address_expr->X_add_symbol,
4405                         address_expr->X_add_number);
4406       *reloc_type = BFD_RELOC_UNUSED;
4407     }
4408   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4409     {
4410       /* We need to set up a variant frag.  */
4411       gas_assert (address_expr != NULL);
4412       add_relaxed_insn (ip, 4, 0,
4413                         RELAX_MIPS16_ENCODE
4414                         (*reloc_type - BFD_RELOC_UNUSED,
4415                          forced_insn_length == 2, forced_insn_length == 4,
4416                          delayed_branch_p (&history[0]),
4417                          history[0].mips16_absolute_jump_p),
4418                         make_expr_symbol (address_expr), 0);
4419     }
4420   else if (mips_opts.mips16 && insn_length (ip) == 2)
4421     {
4422       if (!delayed_branch_p (ip))
4423         /* Make sure there is enough room to swap this instruction with
4424            a following jump instruction.  */
4425         frag_grow (6);
4426       add_fixed_insn (ip);
4427     }
4428   else
4429     {
4430       if (mips_opts.mips16
4431           && mips_opts.noreorder
4432           && delayed_branch_p (&history[0]))
4433         as_warn (_("extended instruction in delay slot"));
4434
4435       if (mips_relax.sequence)
4436         {
4437           /* If we've reached the end of this frag, turn it into a variant
4438              frag and record the information for the instructions we've
4439              written so far.  */
4440           if (frag_room () < 4)
4441             relax_close_frag ();
4442           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4443         }
4444
4445       if (mips_relax.sequence != 2)
4446         {
4447           if (mips_macro_warning.first_insn_sizes[0] == 0)
4448             mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4449           mips_macro_warning.sizes[0] += insn_length (ip);
4450           mips_macro_warning.insns[0]++;
4451         }
4452       if (mips_relax.sequence != 1)
4453         {
4454           if (mips_macro_warning.first_insn_sizes[1] == 0)
4455             mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4456           mips_macro_warning.sizes[1] += insn_length (ip);
4457           mips_macro_warning.insns[1]++;
4458         }
4459
4460       if (mips_opts.mips16)
4461         {
4462           ip->fixed_p = 1;
4463           ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4464         }
4465       add_fixed_insn (ip);
4466     }
4467
4468   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4469     {
4470       bfd_reloc_code_real_type final_type[3];
4471       reloc_howto_type *howto0;
4472       reloc_howto_type *howto;
4473       int i;
4474
4475       /* Perform any necessary conversion to microMIPS relocations
4476          and find out how many relocations there actually are.  */
4477       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4478         final_type[i] = micromips_map_reloc (reloc_type[i]);
4479
4480       /* In a compound relocation, it is the final (outermost)
4481          operator that determines the relocated field.  */
4482       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4483
4484       if (howto == NULL)
4485         {
4486           /* To reproduce this failure try assembling gas/testsuites/
4487              gas/mips/mips16-intermix.s with a mips-ecoff targeted
4488              assembler.  */
4489           as_bad (_("Unsupported MIPS relocation number %d"),
4490                   final_type[i - 1]);
4491           howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4492         }
4493
4494       if (i > 1)
4495         howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4496       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4497                                  bfd_get_reloc_size (howto),
4498                                  address_expr,
4499                                  howto0 && howto0->pc_relative,
4500                                  final_type[0]);
4501
4502       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
4503       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4504         *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4505
4506       /* These relocations can have an addend that won't fit in
4507          4 octets for 64bit assembly.  */
4508       if (HAVE_64BIT_GPRS
4509           && ! howto->partial_inplace
4510           && (reloc_type[0] == BFD_RELOC_16
4511               || reloc_type[0] == BFD_RELOC_32
4512               || reloc_type[0] == BFD_RELOC_MIPS_JMP
4513               || reloc_type[0] == BFD_RELOC_GPREL16
4514               || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4515               || reloc_type[0] == BFD_RELOC_GPREL32
4516               || reloc_type[0] == BFD_RELOC_64
4517               || reloc_type[0] == BFD_RELOC_CTOR
4518               || reloc_type[0] == BFD_RELOC_MIPS_SUB
4519               || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4520               || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4521               || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4522               || reloc_type[0] == BFD_RELOC_MIPS_REL16
4523               || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4524               || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4525               || hi16_reloc_p (reloc_type[0])
4526               || lo16_reloc_p (reloc_type[0])))
4527         ip->fixp[0]->fx_no_overflow = 1;
4528
4529       /* These relocations can have an addend that won't fit in 2 octets.  */
4530       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4531           || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4532         ip->fixp[0]->fx_no_overflow = 1;
4533
4534       if (mips_relax.sequence)
4535         {
4536           if (mips_relax.first_fixup == 0)
4537             mips_relax.first_fixup = ip->fixp[0];
4538         }
4539       else if (reloc_needs_lo_p (*reloc_type))
4540         {
4541           struct mips_hi_fixup *hi_fixup;
4542
4543           /* Reuse the last entry if it already has a matching %lo.  */
4544           hi_fixup = mips_hi_fixup_list;
4545           if (hi_fixup == 0
4546               || !fixup_has_matching_lo_p (hi_fixup->fixp))
4547             {
4548               hi_fixup = ((struct mips_hi_fixup *)
4549                           xmalloc (sizeof (struct mips_hi_fixup)));
4550               hi_fixup->next = mips_hi_fixup_list;
4551               mips_hi_fixup_list = hi_fixup;
4552             }
4553           hi_fixup->fixp = ip->fixp[0];
4554           hi_fixup->seg = now_seg;
4555         }
4556
4557       /* Add fixups for the second and third relocations, if given.
4558          Note that the ABI allows the second relocation to be
4559          against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
4560          moment we only use RSS_UNDEF, but we could add support
4561          for the others if it ever becomes necessary.  */
4562       for (i = 1; i < 3; i++)
4563         if (reloc_type[i] != BFD_RELOC_UNUSED)
4564           {
4565             ip->fixp[i] = fix_new (ip->frag, ip->where,
4566                                    ip->fixp[0]->fx_size, NULL, 0,
4567                                    FALSE, final_type[i]);
4568
4569             /* Use fx_tcbit to mark compound relocs.  */
4570             ip->fixp[0]->fx_tcbit = 1;
4571             ip->fixp[i]->fx_tcbit = 1;
4572           }
4573     }
4574   install_insn (ip);
4575
4576   /* Update the register mask information.  */
4577   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4578   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4579
4580   switch (method)
4581     {
4582     case APPEND_ADD:
4583       insert_into_history (0, 1, ip);
4584       break;
4585
4586     case APPEND_ADD_WITH_NOP:
4587       {
4588         struct mips_cl_insn *nop;
4589
4590         insert_into_history (0, 1, ip);
4591         nop = get_delay_slot_nop (ip);
4592         add_fixed_insn (nop);
4593         insert_into_history (0, 1, nop);
4594         if (mips_relax.sequence)
4595           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4596       }
4597       break;
4598
4599     case APPEND_ADD_COMPACT:
4600       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
4601       gas_assert (mips_opts.mips16);
4602       ip->insn_opcode |= 0x0080;
4603       find_altered_mips16_opcode (ip);
4604       install_insn (ip);
4605       insert_into_history (0, 1, ip);
4606       break;
4607
4608     case APPEND_SWAP:
4609       {
4610         struct mips_cl_insn delay = history[0];
4611         if (mips_opts.mips16)
4612           {
4613             know (delay.frag == ip->frag);
4614             move_insn (ip, delay.frag, delay.where);
4615             move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4616           }
4617         else if (relaxed_branch || delay.frag != ip->frag)
4618           {
4619             /* Add the delay slot instruction to the end of the
4620                current frag and shrink the fixed part of the
4621                original frag.  If the branch occupies the tail of
4622                the latter, move it backwards to cover the gap.  */
4623             delay.frag->fr_fix -= branch_disp;
4624             if (delay.frag == ip->frag)
4625               move_insn (ip, ip->frag, ip->where - branch_disp);
4626             add_fixed_insn (&delay);
4627           }
4628         else
4629           {
4630             move_insn (&delay, ip->frag,
4631                        ip->where - branch_disp + insn_length (ip));
4632             move_insn (ip, history[0].frag, history[0].where);
4633           }
4634         history[0] = *ip;
4635         delay.fixed_p = 1;
4636         insert_into_history (0, 1, &delay);
4637       }
4638       break;
4639     }
4640
4641   /* If we have just completed an unconditional branch, clear the history.  */
4642   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4643       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
4644     {
4645       unsigned int i;
4646
4647       mips_no_prev_insn ();
4648
4649       for (i = 0; i < ARRAY_SIZE (history); i++)
4650         history[i].cleared_p = 1;
4651     }
4652
4653   /* We need to emit a label at the end of branch-likely macros.  */
4654   if (emit_branch_likely_macro)
4655     {
4656       emit_branch_likely_macro = FALSE;
4657       micromips_add_label ();
4658     }
4659
4660   /* We just output an insn, so the next one doesn't have a label.  */
4661   mips_clear_insn_labels ();
4662 }
4663
4664 /* Forget that there was any previous instruction or label.
4665    When BRANCH is true, the branch history is also flushed.  */
4666
4667 static void
4668 mips_no_prev_insn (void)
4669 {
4670   prev_nop_frag = NULL;
4671   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
4672   mips_clear_insn_labels ();
4673 }
4674
4675 /* This function must be called before we emit something other than
4676    instructions.  It is like mips_no_prev_insn except that it inserts
4677    any NOPS that might be needed by previous instructions.  */
4678
4679 void
4680 mips_emit_delays (void)
4681 {
4682   if (! mips_opts.noreorder)
4683     {
4684       int nops = nops_for_insn (0, history, NULL);
4685       if (nops > 0)
4686         {
4687           while (nops-- > 0)
4688             add_fixed_insn (NOP_INSN);
4689           mips_move_text_labels ();
4690         }
4691     }
4692   mips_no_prev_insn ();
4693 }
4694
4695 /* Start a (possibly nested) noreorder block.  */
4696
4697 static void
4698 start_noreorder (void)
4699 {
4700   if (mips_opts.noreorder == 0)
4701     {
4702       unsigned int i;
4703       int nops;
4704
4705       /* None of the instructions before the .set noreorder can be moved.  */
4706       for (i = 0; i < ARRAY_SIZE (history); i++)
4707         history[i].fixed_p = 1;
4708
4709       /* Insert any nops that might be needed between the .set noreorder
4710          block and the previous instructions.  We will later remove any
4711          nops that turn out not to be needed.  */
4712       nops = nops_for_insn (0, history, NULL);
4713       if (nops > 0)
4714         {
4715           if (mips_optimize != 0)
4716             {
4717               /* Record the frag which holds the nop instructions, so
4718                  that we can remove them if we don't need them.  */
4719               frag_grow (nops * NOP_INSN_SIZE);
4720               prev_nop_frag = frag_now;
4721               prev_nop_frag_holds = nops;
4722               prev_nop_frag_required = 0;
4723               prev_nop_frag_since = 0;
4724             }
4725
4726           for (; nops > 0; --nops)
4727             add_fixed_insn (NOP_INSN);
4728
4729           /* Move on to a new frag, so that it is safe to simply
4730              decrease the size of prev_nop_frag.  */
4731           frag_wane (frag_now);
4732           frag_new (0);
4733           mips_move_text_labels ();
4734         }
4735       mips_mark_labels ();
4736       mips_clear_insn_labels ();
4737     }
4738   mips_opts.noreorder++;
4739   mips_any_noreorder = 1;
4740 }
4741
4742 /* End a nested noreorder block.  */
4743
4744 static void
4745 end_noreorder (void)
4746 {
4747   mips_opts.noreorder--;
4748   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4749     {
4750       /* Commit to inserting prev_nop_frag_required nops and go back to
4751          handling nop insertion the .set reorder way.  */
4752       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
4753                                 * NOP_INSN_SIZE);
4754       insert_into_history (prev_nop_frag_since,
4755                            prev_nop_frag_required, NOP_INSN);
4756       prev_nop_frag = NULL;
4757     }
4758 }
4759
4760 /* Set up global variables for the start of a new macro.  */
4761
4762 static void
4763 macro_start (void)
4764 {
4765   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
4766   memset (&mips_macro_warning.first_insn_sizes, 0,
4767           sizeof (mips_macro_warning.first_insn_sizes));
4768   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
4769   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
4770                                      && delayed_branch_p (&history[0]));
4771   switch (history[0].insn_mo->pinfo2
4772           & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4773     {
4774     case INSN2_BRANCH_DELAY_32BIT:
4775       mips_macro_warning.delay_slot_length = 4;
4776       break;
4777     case INSN2_BRANCH_DELAY_16BIT:
4778       mips_macro_warning.delay_slot_length = 2;
4779       break;
4780     default:
4781       mips_macro_warning.delay_slot_length = 0;
4782       break;
4783     }
4784   mips_macro_warning.first_frag = NULL;
4785 }
4786
4787 /* Given that a macro is longer than one instruction or of the wrong size,
4788    return the appropriate warning for it.  Return null if no warning is
4789    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4790    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4791    and RELAX_NOMACRO.  */
4792
4793 static const char *
4794 macro_warning (relax_substateT subtype)
4795 {
4796   if (subtype & RELAX_DELAY_SLOT)
4797     return _("Macro instruction expanded into multiple instructions"
4798              " in a branch delay slot");
4799   else if (subtype & RELAX_NOMACRO)
4800     return _("Macro instruction expanded into multiple instructions");
4801   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4802                       | RELAX_DELAY_SLOT_SIZE_SECOND))
4803     return ((subtype & RELAX_DELAY_SLOT_16BIT)
4804             ? _("Macro instruction expanded into a wrong size instruction"
4805                 " in a 16-bit branch delay slot")
4806             : _("Macro instruction expanded into a wrong size instruction"
4807                 " in a 32-bit branch delay slot"));
4808   else
4809     return 0;
4810 }
4811
4812 /* Finish up a macro.  Emit warnings as appropriate.  */
4813
4814 static void
4815 macro_end (void)
4816 {
4817   /* Relaxation warning flags.  */
4818   relax_substateT subtype = 0;
4819
4820   /* Check delay slot size requirements.  */
4821   if (mips_macro_warning.delay_slot_length == 2)
4822     subtype |= RELAX_DELAY_SLOT_16BIT;
4823   if (mips_macro_warning.delay_slot_length != 0)
4824     {
4825       if (mips_macro_warning.delay_slot_length
4826           != mips_macro_warning.first_insn_sizes[0])
4827         subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4828       if (mips_macro_warning.delay_slot_length
4829           != mips_macro_warning.first_insn_sizes[1])
4830         subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4831     }
4832
4833   /* Check instruction count requirements.  */
4834   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4835     {
4836       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
4837         subtype |= RELAX_SECOND_LONGER;
4838       if (mips_opts.warn_about_macros)
4839         subtype |= RELAX_NOMACRO;
4840       if (mips_macro_warning.delay_slot_p)
4841         subtype |= RELAX_DELAY_SLOT;
4842     }
4843
4844   /* If both alternatives fail to fill a delay slot correctly,
4845      emit the warning now.  */
4846   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4847       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4848     {
4849       relax_substateT s;
4850       const char *msg;
4851
4852       s = subtype & (RELAX_DELAY_SLOT_16BIT
4853                      | RELAX_DELAY_SLOT_SIZE_FIRST
4854                      | RELAX_DELAY_SLOT_SIZE_SECOND);
4855       msg = macro_warning (s);
4856       if (msg != NULL)
4857         as_warn ("%s", msg);
4858       subtype &= ~s;
4859     }
4860
4861   /* If both implementations are longer than 1 instruction, then emit the
4862      warning now.  */
4863   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4864     {
4865       relax_substateT s;
4866       const char *msg;
4867
4868       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4869       msg = macro_warning (s);
4870       if (msg != NULL)
4871         as_warn ("%s", msg);
4872       subtype &= ~s;
4873     }
4874
4875   /* If any flags still set, then one implementation might need a warning
4876      and the other either will need one of a different kind or none at all.
4877      Pass any remaining flags over to relaxation.  */
4878   if (mips_macro_warning.first_frag != NULL)
4879     mips_macro_warning.first_frag->fr_subtype |= subtype;
4880 }
4881
4882 /* Instruction operand formats used in macros that vary between
4883    standard MIPS and microMIPS code.  */
4884
4885 static const char * const brk_fmt[2] = { "c", "mF" };
4886 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4887 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4888 static const char * const lui_fmt[2] = { "t,u", "s,u" };
4889 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4890 static const char * const mfhl_fmt[2] = { "d", "mj" };
4891 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4892 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4893
4894 #define BRK_FMT (brk_fmt[mips_opts.micromips])
4895 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
4896 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
4897 #define LUI_FMT (lui_fmt[mips_opts.micromips])
4898 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4899 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4900 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
4901 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
4902
4903 /* Read a macro's relocation codes from *ARGS and store them in *R.
4904    The first argument in *ARGS will be either the code for a single
4905    relocation or -1 followed by the three codes that make up a
4906    composite relocation.  */
4907
4908 static void
4909 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4910 {
4911   int i, next;
4912
4913   next = va_arg (*args, int);
4914   if (next >= 0)
4915     r[0] = (bfd_reloc_code_real_type) next;
4916   else
4917     for (i = 0; i < 3; i++)
4918       r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4919 }
4920
4921 /* Build an instruction created by a macro expansion.  This is passed
4922    a pointer to the count of instructions created so far, an
4923    expression, the name of the instruction to build, an operand format
4924    string, and corresponding arguments.  */
4925
4926 static void
4927 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
4928 {
4929   const struct mips_opcode *mo = NULL;
4930   bfd_reloc_code_real_type r[3];
4931   const struct mips_opcode *amo;
4932   struct hash_control *hash;
4933   struct mips_cl_insn insn;
4934   va_list args;
4935
4936   va_start (args, fmt);
4937
4938   if (mips_opts.mips16)
4939     {
4940       mips16_macro_build (ep, name, fmt, &args);
4941       va_end (args);
4942       return;
4943     }
4944
4945   r[0] = BFD_RELOC_UNUSED;
4946   r[1] = BFD_RELOC_UNUSED;
4947   r[2] = BFD_RELOC_UNUSED;
4948   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4949   amo = (struct mips_opcode *) hash_find (hash, name);
4950   gas_assert (amo);
4951   gas_assert (strcmp (name, amo->name) == 0);
4952
4953   do
4954     {
4955       /* Search until we get a match for NAME.  It is assumed here that
4956          macros will never generate MDMX, MIPS-3D, or MT instructions.
4957          We try to match an instruction that fulfils the branch delay
4958          slot instruction length requirement (if any) of the previous
4959          instruction.  While doing this we record the first instruction
4960          seen that matches all the other conditions and use it anyway
4961          if the requirement cannot be met; we will issue an appropriate
4962          warning later on.  */
4963       if (strcmp (fmt, amo->args) == 0
4964           && amo->pinfo != INSN_MACRO
4965           && is_opcode_valid (amo)
4966           && is_size_valid (amo))
4967         {
4968           if (is_delay_slot_valid (amo))
4969             {
4970               mo = amo;
4971               break;
4972             }
4973           else if (!mo)
4974             mo = amo;
4975         }
4976
4977       ++amo;
4978       gas_assert (amo->name);
4979     }
4980   while (strcmp (name, amo->name) == 0);
4981
4982   gas_assert (mo);
4983   create_insn (&insn, mo);
4984   for (;;)
4985     {
4986       switch (*fmt++)
4987         {
4988         case '\0':
4989           break;
4990
4991         case ',':
4992         case '(':
4993         case ')':
4994           continue;
4995
4996         case '+':
4997           switch (*fmt++)
4998             {
4999             case 'A':
5000             case 'E':
5001               INSERT_OPERAND (mips_opts.micromips,
5002                               EXTLSB, insn, va_arg (args, int));
5003               continue;
5004
5005             case 'B':
5006             case 'F':
5007               /* Note that in the macro case, these arguments are already
5008                  in MSB form.  (When handling the instruction in the
5009                  non-macro case, these arguments are sizes from which
5010                  MSB values must be calculated.)  */
5011               INSERT_OPERAND (mips_opts.micromips,
5012                               INSMSB, insn, va_arg (args, int));
5013               continue;
5014
5015             case 'C':
5016             case 'G':
5017             case 'H':
5018               /* Note that in the macro case, these arguments are already
5019                  in MSBD form.  (When handling the instruction in the
5020                  non-macro case, these arguments are sizes from which
5021                  MSBD values must be calculated.)  */
5022               INSERT_OPERAND (mips_opts.micromips,
5023                               EXTMSBD, insn, va_arg (args, int));
5024               continue;
5025
5026             case 'Q':
5027               gas_assert (!mips_opts.micromips);
5028               INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
5029               continue;
5030
5031             default:
5032               abort ();
5033             }
5034           continue;
5035
5036         case '2':
5037           INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
5038           continue;
5039
5040         case 'n':
5041           gas_assert (mips_opts.micromips);
5042         case 't':
5043         case 'w':
5044         case 'E':
5045           INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
5046           continue;
5047
5048         case 'c':
5049           gas_assert (!mips_opts.micromips);
5050           INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
5051           continue;
5052
5053         case 'W':
5054           gas_assert (!mips_opts.micromips);
5055         case 'T':
5056           INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
5057           continue;
5058
5059         case 'G':
5060           if (mips_opts.micromips)
5061             INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5062           else
5063             INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5064           continue;
5065
5066         case 'K':
5067           gas_assert (!mips_opts.micromips);
5068         case 'd':
5069           INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
5070           continue;
5071
5072         case 'U':
5073           gas_assert (!mips_opts.micromips);
5074           {
5075             int tmp = va_arg (args, int);
5076
5077             INSERT_OPERAND (0, RT, insn, tmp);
5078             INSERT_OPERAND (0, RD, insn, tmp);
5079           }
5080           continue;
5081
5082         case 'V':
5083         case 'S':
5084           gas_assert (!mips_opts.micromips);
5085           INSERT_OPERAND (0, FS, insn, va_arg (args, int));
5086           continue;
5087
5088         case 'z':
5089           continue;
5090
5091         case '<':
5092           INSERT_OPERAND (mips_opts.micromips,
5093                           SHAMT, insn, va_arg (args, int));
5094           continue;
5095
5096         case 'D':
5097           gas_assert (!mips_opts.micromips);
5098           INSERT_OPERAND (0, FD, insn, va_arg (args, int));
5099           continue;
5100
5101         case 'B':
5102           gas_assert (!mips_opts.micromips);
5103           INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
5104           continue;
5105
5106         case 'J':
5107           gas_assert (!mips_opts.micromips);
5108           INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
5109           continue;
5110
5111         case 'q':
5112           gas_assert (!mips_opts.micromips);
5113           INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
5114           continue;
5115
5116         case 'b':
5117         case 's':
5118         case 'r':
5119         case 'v':
5120           INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
5121           continue;
5122
5123         case 'i':
5124         case 'j':
5125           macro_read_relocs (&args, r);
5126           gas_assert (*r == BFD_RELOC_GPREL16
5127                       || *r == BFD_RELOC_MIPS_HIGHER
5128                       || *r == BFD_RELOC_HI16_S
5129                       || *r == BFD_RELOC_LO16
5130                       || *r == BFD_RELOC_MIPS_GOT_OFST);
5131           continue;
5132
5133         case 'o':
5134           macro_read_relocs (&args, r);
5135           continue;
5136
5137         case 'u':
5138           macro_read_relocs (&args, r);
5139           gas_assert (ep != NULL
5140                       && (ep->X_op == O_constant
5141                           || (ep->X_op == O_symbol
5142                               && (*r == BFD_RELOC_MIPS_HIGHEST
5143                                   || *r == BFD_RELOC_HI16_S
5144                                   || *r == BFD_RELOC_HI16
5145                                   || *r == BFD_RELOC_GPREL16
5146                                   || *r == BFD_RELOC_MIPS_GOT_HI16
5147                                   || *r == BFD_RELOC_MIPS_CALL_HI16))));
5148           continue;
5149
5150         case 'p':
5151           gas_assert (ep != NULL);
5152
5153           /*
5154            * This allows macro() to pass an immediate expression for
5155            * creating short branches without creating a symbol.
5156            *
5157            * We don't allow branch relaxation for these branches, as
5158            * they should only appear in ".set nomacro" anyway.
5159            */
5160           if (ep->X_op == O_constant)
5161             {
5162               /* For microMIPS we always use relocations for branches.
5163                  So we should not resolve immediate values.  */
5164               gas_assert (!mips_opts.micromips);
5165
5166               if ((ep->X_add_number & 3) != 0)
5167                 as_bad (_("branch to misaligned address (0x%lx)"),
5168                         (unsigned long) ep->X_add_number);
5169               if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5170                 as_bad (_("branch address range overflow (0x%lx)"),
5171                         (unsigned long) ep->X_add_number);
5172               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5173               ep = NULL;
5174             }
5175           else
5176             *r = BFD_RELOC_16_PCREL_S2;
5177           continue;
5178
5179         case 'a':
5180           gas_assert (ep != NULL);
5181           *r = BFD_RELOC_MIPS_JMP;
5182           continue;
5183
5184         case 'C':
5185           gas_assert (!mips_opts.micromips);
5186           INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5187           continue;
5188
5189         case 'k':
5190           INSERT_OPERAND (mips_opts.micromips,
5191                           CACHE, insn, va_arg (args, unsigned long));
5192           continue;
5193
5194         case '|':
5195           gas_assert (mips_opts.micromips);
5196           INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5197           continue;
5198
5199         case '.':
5200           gas_assert (mips_opts.micromips);
5201           INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5202           continue;
5203
5204         case '\\':
5205           INSERT_OPERAND (mips_opts.micromips,
5206                           3BITPOS, insn, va_arg (args, unsigned int));
5207           continue;
5208
5209         case '~':
5210           INSERT_OPERAND (mips_opts.micromips,
5211                           OFFSET12, insn, va_arg (args, unsigned long));
5212           continue;
5213
5214         case 'N':
5215           gas_assert (mips_opts.micromips);
5216           INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5217           continue;
5218
5219         case 'm':       /* Opcode extension character.  */
5220           gas_assert (mips_opts.micromips);
5221           switch (*fmt++)
5222             {
5223             case 'j':
5224               INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5225               break;
5226
5227             case 'p':
5228               INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5229               break;
5230
5231             case 'F':
5232               INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5233               break;
5234
5235             default:
5236               abort ();
5237             }
5238           continue;
5239
5240         default:
5241           abort ();
5242         }
5243       break;
5244     }
5245   va_end (args);
5246   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5247
5248   append_insn (&insn, ep, r, TRUE);
5249 }
5250
5251 static void
5252 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5253                     va_list *args)
5254 {
5255   struct mips_opcode *mo;
5256   struct mips_cl_insn insn;
5257   bfd_reloc_code_real_type r[3]
5258     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5259
5260   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5261   gas_assert (mo);
5262   gas_assert (strcmp (name, mo->name) == 0);
5263
5264   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5265     {
5266       ++mo;
5267       gas_assert (mo->name);
5268       gas_assert (strcmp (name, mo->name) == 0);
5269     }
5270
5271   create_insn (&insn, mo);
5272   for (;;)
5273     {
5274       int c;
5275
5276       c = *fmt++;
5277       switch (c)
5278         {
5279         case '\0':
5280           break;
5281
5282         case ',':
5283         case '(':
5284         case ')':
5285           continue;
5286
5287         case 'y':
5288         case 'w':
5289           MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5290           continue;
5291
5292         case 'x':
5293         case 'v':
5294           MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5295           continue;
5296
5297         case 'z':
5298           MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5299           continue;
5300
5301         case 'Z':
5302           MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5303           continue;
5304
5305         case '0':
5306         case 'S':
5307         case 'P':
5308         case 'R':
5309           continue;
5310
5311         case 'X':
5312           MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5313           continue;
5314
5315         case 'Y':
5316           {
5317             int regno;
5318
5319             regno = va_arg (*args, int);
5320             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5321             MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5322           }
5323           continue;
5324
5325         case '<':
5326         case '>':
5327         case '4':
5328         case '5':
5329         case 'H':
5330         case 'W':
5331         case 'D':
5332         case 'j':
5333         case '8':
5334         case 'V':
5335         case 'C':
5336         case 'U':
5337         case 'k':
5338         case 'K':
5339         case 'p':
5340         case 'q':
5341           {
5342             offsetT value;
5343
5344             gas_assert (ep != NULL);
5345
5346             if (ep->X_op != O_constant)
5347               *r = (int) BFD_RELOC_UNUSED + c;
5348             else if (calculate_reloc (*r, ep->X_add_number, &value))
5349               {
5350                 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
5351                 ep = NULL;
5352                 *r = BFD_RELOC_UNUSED;
5353               }
5354           }
5355           continue;
5356
5357         case '6':
5358           MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5359           continue;
5360         }
5361
5362       break;
5363     }
5364
5365   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5366
5367   append_insn (&insn, ep, r, TRUE);
5368 }
5369
5370 /*
5371  * Sign-extend 32-bit mode constants that have bit 31 set and all
5372  * higher bits unset.
5373  */
5374 static void
5375 normalize_constant_expr (expressionS *ex)
5376 {
5377   if (ex->X_op == O_constant
5378       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5379     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5380                         - 0x80000000);
5381 }
5382
5383 /*
5384  * Sign-extend 32-bit mode address offsets that have bit 31 set and
5385  * all higher bits unset.
5386  */
5387 static void
5388 normalize_address_expr (expressionS *ex)
5389 {
5390   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5391         || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5392       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5393     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5394                         - 0x80000000);
5395 }
5396
5397 /*
5398  * Generate a "jalr" instruction with a relocation hint to the called
5399  * function.  This occurs in NewABI PIC code.
5400  */
5401 static void
5402 macro_build_jalr (expressionS *ep, int cprestore)
5403 {
5404   static const bfd_reloc_code_real_type jalr_relocs[2]
5405     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5406   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5407   const char *jalr;
5408   char *f = NULL;
5409
5410   if (MIPS_JALR_HINT_P (ep))
5411     {
5412       frag_grow (8);
5413       f = frag_more (0);
5414     }
5415   if (mips_opts.micromips)
5416     {
5417       jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
5418       if (MIPS_JALR_HINT_P (ep)
5419           || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
5420         macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5421       else
5422         macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5423     }
5424   else
5425     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5426   if (MIPS_JALR_HINT_P (ep))
5427     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5428 }
5429
5430 /*
5431  * Generate a "lui" instruction.
5432  */
5433 static void
5434 macro_build_lui (expressionS *ep, int regnum)
5435 {
5436   gas_assert (! mips_opts.mips16);
5437
5438   if (ep->X_op != O_constant)
5439     {
5440       gas_assert (ep->X_op == O_symbol);
5441       /* _gp_disp is a special case, used from s_cpload.
5442          __gnu_local_gp is used if mips_no_shared.  */
5443       gas_assert (mips_pic == NO_PIC
5444               || (! HAVE_NEWABI
5445                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5446               || (! mips_in_shared
5447                   && strcmp (S_GET_NAME (ep->X_add_symbol),
5448                              "__gnu_local_gp") == 0));
5449     }
5450
5451   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5452 }
5453
5454 /* Generate a sequence of instructions to do a load or store from a constant
5455    offset off of a base register (breg) into/from a target register (treg),
5456    using AT if necessary.  */
5457 static void
5458 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5459                               int treg, int breg, int dbl)
5460 {
5461   gas_assert (ep->X_op == O_constant);
5462
5463   /* Sign-extending 32-bit constants makes their handling easier.  */
5464   if (!dbl)
5465     normalize_constant_expr (ep);
5466
5467   /* Right now, this routine can only handle signed 32-bit constants.  */
5468   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5469     as_warn (_("operand overflow"));
5470
5471   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5472     {
5473       /* Signed 16-bit offset will fit in the op.  Easy!  */
5474       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5475     }
5476   else
5477     {
5478       /* 32-bit offset, need multiple instructions and AT, like:
5479            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
5480            addu     $tempreg,$tempreg,$breg
5481            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
5482          to handle the complete offset.  */
5483       macro_build_lui (ep, AT);
5484       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5485       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5486
5487       if (!mips_opts.at)
5488         as_bad (_("Macro used $at after \".set noat\""));
5489     }
5490 }
5491
5492 /*                      set_at()
5493  * Generates code to set the $at register to true (one)
5494  * if reg is less than the immediate expression.
5495  */
5496 static void
5497 set_at (int reg, int unsignedp)
5498 {
5499   if (imm_expr.X_op == O_constant
5500       && imm_expr.X_add_number >= -0x8000
5501       && imm_expr.X_add_number < 0x8000)
5502     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5503                  AT, reg, BFD_RELOC_LO16);
5504   else
5505     {
5506       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5507       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5508     }
5509 }
5510
5511 /* Warn if an expression is not a constant.  */
5512
5513 static void
5514 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5515 {
5516   if (ex->X_op == O_big)
5517     as_bad (_("unsupported large constant"));
5518   else if (ex->X_op != O_constant)
5519     as_bad (_("Instruction %s requires absolute expression"),
5520             ip->insn_mo->name);
5521
5522   if (HAVE_32BIT_GPRS)
5523     normalize_constant_expr (ex);
5524 }
5525
5526 /* Count the leading zeroes by performing a binary chop. This is a
5527    bulky bit of source, but performance is a LOT better for the
5528    majority of values than a simple loop to count the bits:
5529        for (lcnt = 0; (lcnt < 32); lcnt++)
5530          if ((v) & (1 << (31 - lcnt)))
5531            break;
5532   However it is not code size friendly, and the gain will drop a bit
5533   on certain cached systems.
5534 */
5535 #define COUNT_TOP_ZEROES(v)             \
5536   (((v) & ~0xffff) == 0                 \
5537    ? ((v) & ~0xff) == 0                 \
5538      ? ((v) & ~0xf) == 0                \
5539        ? ((v) & ~0x3) == 0              \
5540          ? ((v) & ~0x1) == 0            \
5541            ? !(v)                       \
5542              ? 32                       \
5543              : 31                       \
5544            : 30                         \
5545          : ((v) & ~0x7) == 0            \
5546            ? 29                         \
5547            : 28                         \
5548        : ((v) & ~0x3f) == 0             \
5549          ? ((v) & ~0x1f) == 0           \
5550            ? 27                         \
5551            : 26                         \
5552          : ((v) & ~0x7f) == 0           \
5553            ? 25                         \
5554            : 24                         \
5555      : ((v) & ~0xfff) == 0              \
5556        ? ((v) & ~0x3ff) == 0            \
5557          ? ((v) & ~0x1ff) == 0          \
5558            ? 23                         \
5559            : 22                         \
5560          : ((v) & ~0x7ff) == 0          \
5561            ? 21                         \
5562            : 20                         \
5563        : ((v) & ~0x3fff) == 0           \
5564          ? ((v) & ~0x1fff) == 0         \
5565            ? 19                         \
5566            : 18                         \
5567          : ((v) & ~0x7fff) == 0         \
5568            ? 17                         \
5569            : 16                         \
5570    : ((v) & ~0xffffff) == 0             \
5571      ? ((v) & ~0xfffff) == 0            \
5572        ? ((v) & ~0x3ffff) == 0          \
5573          ? ((v) & ~0x1ffff) == 0        \
5574            ? 15                         \
5575            : 14                         \
5576          : ((v) & ~0x7ffff) == 0        \
5577            ? 13                         \
5578            : 12                         \
5579        : ((v) & ~0x3fffff) == 0         \
5580          ? ((v) & ~0x1fffff) == 0       \
5581            ? 11                         \
5582            : 10                         \
5583          : ((v) & ~0x7fffff) == 0       \
5584            ? 9                          \
5585            : 8                          \
5586      : ((v) & ~0xfffffff) == 0          \
5587        ? ((v) & ~0x3ffffff) == 0        \
5588          ? ((v) & ~0x1ffffff) == 0      \
5589            ? 7                          \
5590            : 6                          \
5591          : ((v) & ~0x7ffffff) == 0      \
5592            ? 5                          \
5593            : 4                          \
5594        : ((v) & ~0x3fffffff) == 0       \
5595          ? ((v) & ~0x1fffffff) == 0     \
5596            ? 3                          \
5597            : 2                          \
5598          : ((v) & ~0x7fffffff) == 0     \
5599            ? 1                          \
5600            : 0)
5601
5602 /*                      load_register()
5603  *  This routine generates the least number of instructions necessary to load
5604  *  an absolute expression value into a register.
5605  */
5606 static void
5607 load_register (int reg, expressionS *ep, int dbl)
5608 {
5609   int freg;
5610   expressionS hi32, lo32;
5611
5612   if (ep->X_op != O_big)
5613     {
5614       gas_assert (ep->X_op == O_constant);
5615
5616       /* Sign-extending 32-bit constants makes their handling easier.  */
5617       if (!dbl)
5618         normalize_constant_expr (ep);
5619
5620       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5621         {
5622           /* We can handle 16 bit signed values with an addiu to
5623              $zero.  No need to ever use daddiu here, since $zero and
5624              the result are always correct in 32 bit mode.  */
5625           macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5626           return;
5627         }
5628       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5629         {
5630           /* We can handle 16 bit unsigned values with an ori to
5631              $zero.  */
5632           macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5633           return;
5634         }
5635       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
5636         {
5637           /* 32 bit values require an lui.  */
5638           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5639           if ((ep->X_add_number & 0xffff) != 0)
5640             macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5641           return;
5642         }
5643     }
5644
5645   /* The value is larger than 32 bits.  */
5646
5647   if (!dbl || HAVE_32BIT_GPRS)
5648     {
5649       char value[32];
5650
5651       sprintf_vma (value, ep->X_add_number);
5652       as_bad (_("Number (0x%s) larger than 32 bits"), value);
5653       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5654       return;
5655     }
5656
5657   if (ep->X_op != O_big)
5658     {
5659       hi32 = *ep;
5660       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5661       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5662       hi32.X_add_number &= 0xffffffff;
5663       lo32 = *ep;
5664       lo32.X_add_number &= 0xffffffff;
5665     }
5666   else
5667     {
5668       gas_assert (ep->X_add_number > 2);
5669       if (ep->X_add_number == 3)
5670         generic_bignum[3] = 0;
5671       else if (ep->X_add_number > 4)
5672         as_bad (_("Number larger than 64 bits"));
5673       lo32.X_op = O_constant;
5674       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5675       hi32.X_op = O_constant;
5676       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5677     }
5678
5679   if (hi32.X_add_number == 0)
5680     freg = 0;
5681   else
5682     {
5683       int shift, bit;
5684       unsigned long hi, lo;
5685
5686       if (hi32.X_add_number == (offsetT) 0xffffffff)
5687         {
5688           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5689             {
5690               macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5691               return;
5692             }
5693           if (lo32.X_add_number & 0x80000000)
5694             {
5695               macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5696               if (lo32.X_add_number & 0xffff)
5697                 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5698               return;
5699             }
5700         }
5701
5702       /* Check for 16bit shifted constant.  We know that hi32 is
5703          non-zero, so start the mask on the first bit of the hi32
5704          value.  */
5705       shift = 17;
5706       do
5707         {
5708           unsigned long himask, lomask;
5709
5710           if (shift < 32)
5711             {
5712               himask = 0xffff >> (32 - shift);
5713               lomask = (0xffff << shift) & 0xffffffff;
5714             }
5715           else
5716             {
5717               himask = 0xffff << (shift - 32);
5718               lomask = 0;
5719             }
5720           if ((hi32.X_add_number & ~(offsetT) himask) == 0
5721               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5722             {
5723               expressionS tmp;
5724
5725               tmp.X_op = O_constant;
5726               if (shift < 32)
5727                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5728                                     | (lo32.X_add_number >> shift));
5729               else
5730                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
5731               macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5732               macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5733                            reg, reg, (shift >= 32) ? shift - 32 : shift);
5734               return;
5735             }
5736           ++shift;
5737         }
5738       while (shift <= (64 - 16));
5739
5740       /* Find the bit number of the lowest one bit, and store the
5741          shifted value in hi/lo.  */
5742       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5743       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5744       if (lo != 0)
5745         {
5746           bit = 0;
5747           while ((lo & 1) == 0)
5748             {
5749               lo >>= 1;
5750               ++bit;
5751             }
5752           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5753           hi >>= bit;
5754         }
5755       else
5756         {
5757           bit = 32;
5758           while ((hi & 1) == 0)
5759             {
5760               hi >>= 1;
5761               ++bit;
5762             }
5763           lo = hi;
5764           hi = 0;
5765         }
5766
5767       /* Optimize if the shifted value is a (power of 2) - 1.  */
5768       if ((hi == 0 && ((lo + 1) & lo) == 0)
5769           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
5770         {
5771           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
5772           if (shift != 0)
5773             {
5774               expressionS tmp;
5775
5776               /* This instruction will set the register to be all
5777                  ones.  */
5778               tmp.X_op = O_constant;
5779               tmp.X_add_number = (offsetT) -1;
5780               macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5781               if (bit != 0)
5782                 {
5783                   bit += shift;
5784                   macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5785                                reg, reg, (bit >= 32) ? bit - 32 : bit);
5786                 }
5787               macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
5788                            reg, reg, (shift >= 32) ? shift - 32 : shift);
5789               return;
5790             }
5791         }
5792
5793       /* Sign extend hi32 before calling load_register, because we can
5794          generally get better code when we load a sign extended value.  */
5795       if ((hi32.X_add_number & 0x80000000) != 0)
5796         hi32.X_add_number |= ~(offsetT) 0xffffffff;
5797       load_register (reg, &hi32, 0);
5798       freg = reg;
5799     }
5800   if ((lo32.X_add_number & 0xffff0000) == 0)
5801     {
5802       if (freg != 0)
5803         {
5804           macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
5805           freg = reg;
5806         }
5807     }
5808   else
5809     {
5810       expressionS mid16;
5811
5812       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
5813         {
5814           macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5815           macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
5816           return;
5817         }
5818
5819       if (freg != 0)
5820         {
5821           macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
5822           freg = reg;
5823         }
5824       mid16 = lo32;
5825       mid16.X_add_number >>= 16;
5826       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5827       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5828       freg = reg;
5829     }
5830   if ((lo32.X_add_number & 0xffff) != 0)
5831     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5832 }
5833
5834 static inline void
5835 load_delay_nop (void)
5836 {
5837   if (!gpr_interlocks)
5838     macro_build (NULL, "nop", "");
5839 }
5840
5841 /* Load an address into a register.  */
5842
5843 static void
5844 load_address (int reg, expressionS *ep, int *used_at)
5845 {
5846   if (ep->X_op != O_constant
5847       && ep->X_op != O_symbol)
5848     {
5849       as_bad (_("expression too complex"));
5850       ep->X_op = O_constant;
5851     }
5852
5853   if (ep->X_op == O_constant)
5854     {
5855       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
5856       return;
5857     }
5858
5859   if (mips_pic == NO_PIC)
5860     {
5861       /* If this is a reference to a GP relative symbol, we want
5862            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
5863          Otherwise we want
5864            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
5865            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
5866          If we have an addend, we always use the latter form.
5867
5868          With 64bit address space and a usable $at we want
5869            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
5870            lui          $at,<sym>               (BFD_RELOC_HI16_S)
5871            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
5872            daddiu       $at,<sym>               (BFD_RELOC_LO16)
5873            dsll32       $reg,0
5874            daddu        $reg,$reg,$at
5875
5876          If $at is already in use, we use a path which is suboptimal
5877          on superscalar processors.
5878            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
5879            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
5880            dsll         $reg,16
5881            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
5882            dsll         $reg,16
5883            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
5884
5885          For GP relative symbols in 64bit address space we can use
5886          the same sequence as in 32bit address space.  */
5887       if (HAVE_64BIT_SYMBOLS)
5888         {
5889           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5890               && !nopic_need_relax (ep->X_add_symbol, 1))
5891             {
5892               relax_start (ep->X_add_symbol);
5893               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5894                            mips_gp_register, BFD_RELOC_GPREL16);
5895               relax_switch ();
5896             }
5897
5898           if (*used_at == 0 && mips_opts.at)
5899             {
5900               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5901               macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
5902               macro_build (ep, "daddiu", "t,r,j", reg, reg,
5903                            BFD_RELOC_MIPS_HIGHER);
5904               macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
5905               macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
5906               macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
5907               *used_at = 1;
5908             }
5909           else
5910             {
5911               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5912               macro_build (ep, "daddiu", "t,r,j", reg, reg,
5913                            BFD_RELOC_MIPS_HIGHER);
5914               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5915               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
5916               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5917               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
5918             }
5919
5920           if (mips_relax.sequence)
5921             relax_end ();
5922         }
5923       else
5924         {
5925           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5926               && !nopic_need_relax (ep->X_add_symbol, 1))
5927             {
5928               relax_start (ep->X_add_symbol);
5929               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5930                            mips_gp_register, BFD_RELOC_GPREL16);
5931               relax_switch ();
5932             }
5933           macro_build_lui (ep, reg);
5934           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5935                        reg, reg, BFD_RELOC_LO16);
5936           if (mips_relax.sequence)
5937             relax_end ();
5938         }
5939     }
5940   else if (!mips_big_got)
5941     {
5942       expressionS ex;
5943
5944       /* If this is a reference to an external symbol, we want
5945            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
5946          Otherwise we want
5947            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
5948            nop
5949            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
5950          If there is a constant, it must be added in after.
5951
5952          If we have NewABI, we want
5953            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5954          unless we're referencing a global symbol with a non-zero
5955          offset, in which case cst must be added separately.  */
5956       if (HAVE_NEWABI)
5957         {
5958           if (ep->X_add_number)
5959             {
5960               ex.X_add_number = ep->X_add_number;
5961               ep->X_add_number = 0;
5962               relax_start (ep->X_add_symbol);
5963               macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5964                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5965               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5966                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5967               ex.X_op = O_constant;
5968               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5969                            reg, reg, BFD_RELOC_LO16);
5970               ep->X_add_number = ex.X_add_number;
5971               relax_switch ();
5972             }
5973           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5974                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5975           if (mips_relax.sequence)
5976             relax_end ();
5977         }
5978       else
5979         {
5980           ex.X_add_number = ep->X_add_number;
5981           ep->X_add_number = 0;
5982           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5983                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
5984           load_delay_nop ();
5985           relax_start (ep->X_add_symbol);
5986           relax_switch ();
5987           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5988                        BFD_RELOC_LO16);
5989           relax_end ();
5990
5991           if (ex.X_add_number != 0)
5992             {
5993               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5994                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5995               ex.X_op = O_constant;
5996               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5997                            reg, reg, BFD_RELOC_LO16);
5998             }
5999         }
6000     }
6001   else if (mips_big_got)
6002     {
6003       expressionS ex;
6004
6005       /* This is the large GOT case.  If this is a reference to an
6006          external symbol, we want
6007            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
6008            addu         $reg,$reg,$gp
6009            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
6010
6011          Otherwise, for a reference to a local symbol in old ABI, we want
6012            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
6013            nop
6014            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
6015          If there is a constant, it must be added in after.
6016
6017          In the NewABI, for local symbols, with or without offsets, we want:
6018            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
6019            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
6020       */
6021       if (HAVE_NEWABI)
6022         {
6023           ex.X_add_number = ep->X_add_number;
6024           ep->X_add_number = 0;
6025           relax_start (ep->X_add_symbol);
6026           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6027           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6028                        reg, reg, mips_gp_register);
6029           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6030                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6031           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6032             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6033           else if (ex.X_add_number)
6034             {
6035               ex.X_op = O_constant;
6036               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6037                            BFD_RELOC_LO16);
6038             }
6039
6040           ep->X_add_number = ex.X_add_number;
6041           relax_switch ();
6042           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6043                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6044           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6045                        BFD_RELOC_MIPS_GOT_OFST);
6046           relax_end ();
6047         }
6048       else
6049         {
6050           ex.X_add_number = ep->X_add_number;
6051           ep->X_add_number = 0;
6052           relax_start (ep->X_add_symbol);
6053           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
6054           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6055                        reg, reg, mips_gp_register);
6056           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6057                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
6058           relax_switch ();
6059           if (reg_needs_delay (mips_gp_register))
6060             {
6061               /* We need a nop before loading from $gp.  This special
6062                  check is required because the lui which starts the main
6063                  instruction stream does not refer to $gp, and so will not
6064                  insert the nop which may be required.  */
6065               macro_build (NULL, "nop", "");
6066             }
6067           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6068                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
6069           load_delay_nop ();
6070           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6071                        BFD_RELOC_LO16);
6072           relax_end ();
6073
6074           if (ex.X_add_number != 0)
6075             {
6076               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6077                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6078               ex.X_op = O_constant;
6079               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6080                            BFD_RELOC_LO16);
6081             }
6082         }
6083     }
6084   else
6085     abort ();
6086
6087   if (!mips_opts.at && *used_at == 1)
6088     as_bad (_("Macro used $at after \".set noat\""));
6089 }
6090
6091 /* Move the contents of register SOURCE into register DEST.  */
6092
6093 static void
6094 move_register (int dest, int source)
6095 {
6096   /* Prefer to use a 16-bit microMIPS instruction unless the previous
6097      instruction specifically requires a 32-bit one.  */
6098   if (mips_opts.micromips
6099       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
6100     macro_build (NULL, "move", "mp,mj", dest, source);
6101   else
6102     macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6103                  dest, source, 0);
6104 }
6105
6106 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
6107    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6108    The two alternatives are:
6109
6110    Global symbol                Local sybmol
6111    -------------                ------------
6112    lw DEST,%got(SYMBOL)         lw DEST,%got(SYMBOL + OFFSET)
6113    ...                          ...
6114    addiu DEST,DEST,OFFSET       addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6115
6116    load_got_offset emits the first instruction and add_got_offset
6117    emits the second for a 16-bit offset or add_got_offset_hilo emits
6118    a sequence to add a 32-bit offset using a scratch register.  */
6119
6120 static void
6121 load_got_offset (int dest, expressionS *local)
6122 {
6123   expressionS global;
6124
6125   global = *local;
6126   global.X_add_number = 0;
6127
6128   relax_start (local->X_add_symbol);
6129   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6130                BFD_RELOC_MIPS_GOT16, mips_gp_register);
6131   relax_switch ();
6132   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6133                BFD_RELOC_MIPS_GOT16, mips_gp_register);
6134   relax_end ();
6135 }
6136
6137 static void
6138 add_got_offset (int dest, expressionS *local)
6139 {
6140   expressionS global;
6141
6142   global.X_op = O_constant;
6143   global.X_op_symbol = NULL;
6144   global.X_add_symbol = NULL;
6145   global.X_add_number = local->X_add_number;
6146
6147   relax_start (local->X_add_symbol);
6148   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6149                dest, dest, BFD_RELOC_LO16);
6150   relax_switch ();
6151   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6152   relax_end ();
6153 }
6154
6155 static void
6156 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6157 {
6158   expressionS global;
6159   int hold_mips_optimize;
6160
6161   global.X_op = O_constant;
6162   global.X_op_symbol = NULL;
6163   global.X_add_symbol = NULL;
6164   global.X_add_number = local->X_add_number;
6165
6166   relax_start (local->X_add_symbol);
6167   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6168   relax_switch ();
6169   /* Set mips_optimize around the lui instruction to avoid
6170      inserting an unnecessary nop after the lw.  */
6171   hold_mips_optimize = mips_optimize;
6172   mips_optimize = 2;
6173   macro_build_lui (&global, tmp);
6174   mips_optimize = hold_mips_optimize;
6175   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6176   relax_end ();
6177
6178   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6179 }
6180
6181 /* Emit a sequence of instructions to emulate a branch likely operation.
6182    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
6183    is its complementing branch with the original condition negated.
6184    CALL is set if the original branch specified the link operation.
6185    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6186
6187    Code like this is produced in the noreorder mode:
6188
6189         BRNEG   <args>, 1f
6190          nop
6191         b       <sym>
6192          delay slot (executed only if branch taken)
6193     1:
6194
6195    or, if CALL is set:
6196
6197         BRNEG   <args>, 1f
6198          nop
6199         bal     <sym>
6200          delay slot (executed only if branch taken)
6201     1:
6202
6203    In the reorder mode the delay slot would be filled with a nop anyway,
6204    so code produced is simply:
6205
6206         BR      <args>, <sym>
6207          nop
6208
6209    This function is used when producing code for the microMIPS ASE that
6210    does not implement branch likely instructions in hardware.  */
6211
6212 static void
6213 macro_build_branch_likely (const char *br, const char *brneg,
6214                            int call, expressionS *ep, const char *fmt,
6215                            unsigned int sreg, unsigned int treg)
6216 {
6217   int noreorder = mips_opts.noreorder;
6218   expressionS expr1;
6219
6220   gas_assert (mips_opts.micromips);
6221   start_noreorder ();
6222   if (noreorder)
6223     {
6224       micromips_label_expr (&expr1);
6225       macro_build (&expr1, brneg, fmt, sreg, treg);
6226       macro_build (NULL, "nop", "");
6227       macro_build (ep, call ? "bal" : "b", "p");
6228
6229       /* Set to true so that append_insn adds a label.  */
6230       emit_branch_likely_macro = TRUE;
6231     }
6232   else
6233     {
6234       macro_build (ep, br, fmt, sreg, treg);
6235       macro_build (NULL, "nop", "");
6236     }
6237   end_noreorder ();
6238 }
6239
6240 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6241    the condition code tested.  EP specifies the branch target.  */
6242
6243 static void
6244 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6245 {
6246   const int call = 0;
6247   const char *brneg;
6248   const char *br;
6249
6250   switch (type)
6251     {
6252     case M_BC1FL:
6253       br = "bc1f";
6254       brneg = "bc1t";
6255       break;
6256     case M_BC1TL:
6257       br = "bc1t";
6258       brneg = "bc1f";
6259       break;
6260     case M_BC2FL:
6261       br = "bc2f";
6262       brneg = "bc2t";
6263       break;
6264     case M_BC2TL:
6265       br = "bc2t";
6266       brneg = "bc2f";
6267       break;
6268     default:
6269       abort ();
6270     }
6271   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6272 }
6273
6274 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6275    the register tested.  EP specifies the branch target.  */
6276
6277 static void
6278 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6279 {
6280   const char *brneg = NULL;
6281   const char *br;
6282   int call = 0;
6283
6284   switch (type)
6285     {
6286     case M_BGEZ:
6287       br = "bgez";
6288       break;
6289     case M_BGEZL:
6290       br = mips_opts.micromips ? "bgez" : "bgezl";
6291       brneg = "bltz";
6292       break;
6293     case M_BGEZALL:
6294       gas_assert (mips_opts.micromips);
6295       br = "bgezals";
6296       brneg = "bltz";
6297       call = 1;
6298       break;
6299     case M_BGTZ:
6300       br = "bgtz";
6301       break;
6302     case M_BGTZL:
6303       br = mips_opts.micromips ? "bgtz" : "bgtzl";
6304       brneg = "blez";
6305       break;
6306     case M_BLEZ:
6307       br = "blez";
6308       break;
6309     case M_BLEZL:
6310       br = mips_opts.micromips ? "blez" : "blezl";
6311       brneg = "bgtz";
6312       break;
6313     case M_BLTZ:
6314       br = "bltz";
6315       break;
6316     case M_BLTZL:
6317       br = mips_opts.micromips ? "bltz" : "bltzl";
6318       brneg = "bgez";
6319       break;
6320     case M_BLTZALL:
6321       gas_assert (mips_opts.micromips);
6322       br = "bltzals";
6323       brneg = "bgez";
6324       call = 1;
6325       break;
6326     default:
6327       abort ();
6328     }
6329   if (mips_opts.micromips && brneg)
6330     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6331   else
6332     macro_build (ep, br, "s,p", sreg);
6333 }
6334
6335 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6336    TREG as the registers tested.  EP specifies the branch target.  */
6337
6338 static void
6339 macro_build_branch_rsrt (int type, expressionS *ep,
6340                          unsigned int sreg, unsigned int treg)
6341 {
6342   const char *brneg = NULL;
6343   const int call = 0;
6344   const char *br;
6345
6346   switch (type)
6347     {
6348     case M_BEQ:
6349     case M_BEQ_I:
6350       br = "beq";
6351       break;
6352     case M_BEQL:
6353     case M_BEQL_I:
6354       br = mips_opts.micromips ? "beq" : "beql";
6355       brneg = "bne";
6356       break;
6357     case M_BNE:
6358     case M_BNE_I:
6359       br = "bne";
6360       break;
6361     case M_BNEL:
6362     case M_BNEL_I:
6363       br = mips_opts.micromips ? "bne" : "bnel";
6364       brneg = "beq";
6365       break;
6366     default:
6367       abort ();
6368     }
6369   if (mips_opts.micromips && brneg)
6370     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6371   else
6372     macro_build (ep, br, "s,t,p", sreg, treg);
6373 }
6374
6375 /*
6376  *                      Build macros
6377  *   This routine implements the seemingly endless macro or synthesized
6378  * instructions and addressing modes in the mips assembly language. Many
6379  * of these macros are simple and are similar to each other. These could
6380  * probably be handled by some kind of table or grammar approach instead of
6381  * this verbose method. Others are not simple macros but are more like
6382  * optimizing code generation.
6383  *   One interesting optimization is when several store macros appear
6384  * consecutively that would load AT with the upper half of the same address.
6385  * The ensuing load upper instructions are ommited. This implies some kind
6386  * of global optimization. We currently only optimize within a single macro.
6387  *   For many of the load and store macros if the address is specified as a
6388  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6389  * first load register 'at' with zero and use it as the base register. The
6390  * mips assembler simply uses register $zero. Just one tiny optimization
6391  * we're missing.
6392  */
6393 static void
6394 macro (struct mips_cl_insn *ip)
6395 {
6396   unsigned int treg, sreg, dreg, breg;
6397   unsigned int tempreg;
6398   int mask;
6399   int used_at = 0;
6400   expressionS label_expr;
6401   expressionS expr1;
6402   expressionS *ep;
6403   const char *s;
6404   const char *s2;
6405   const char *fmt;
6406   int likely = 0;
6407   int coproc = 0;
6408   int off12 = 0;
6409   int call = 0;
6410   int jals = 0;
6411   int dbl = 0;
6412   int imm = 0;
6413   int ust = 0;
6414   int lp = 0;
6415   int ab = 0;
6416   int off0 = 0;
6417   int off;
6418   offsetT maxnum;
6419   bfd_reloc_code_real_type r;
6420   int hold_mips_optimize;
6421
6422   gas_assert (! mips_opts.mips16);
6423
6424   treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6425   dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6426   sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6427   mask = ip->insn_mo->mask;
6428
6429   label_expr.X_op = O_constant;
6430   label_expr.X_op_symbol = NULL;
6431   label_expr.X_add_symbol = NULL;
6432   label_expr.X_add_number = 0;
6433
6434   expr1.X_op = O_constant;
6435   expr1.X_op_symbol = NULL;
6436   expr1.X_add_symbol = NULL;
6437   expr1.X_add_number = 1;
6438
6439   switch (mask)
6440     {
6441     case M_DABS:
6442       dbl = 1;
6443     case M_ABS:
6444       /*    bgez    $a0,1f
6445             move    v0,$a0
6446             sub     v0,$zero,$a0
6447          1:
6448        */
6449
6450       start_noreorder ();
6451
6452       if (mips_opts.micromips)
6453         micromips_label_expr (&label_expr);
6454       else
6455         label_expr.X_add_number = 8;
6456       macro_build (&label_expr, "bgez", "s,p", sreg);
6457       if (dreg == sreg)
6458         macro_build (NULL, "nop", "");
6459       else
6460         move_register (dreg, sreg);
6461       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6462       if (mips_opts.micromips)
6463         micromips_add_label ();
6464
6465       end_noreorder ();
6466       break;
6467
6468     case M_ADD_I:
6469       s = "addi";
6470       s2 = "add";
6471       goto do_addi;
6472     case M_ADDU_I:
6473       s = "addiu";
6474       s2 = "addu";
6475       goto do_addi;
6476     case M_DADD_I:
6477       dbl = 1;
6478       s = "daddi";
6479       s2 = "dadd";
6480       if (!mips_opts.micromips)
6481         goto do_addi;
6482       if (imm_expr.X_op == O_constant
6483           && imm_expr.X_add_number >= -0x200
6484           && imm_expr.X_add_number < 0x200)
6485         {
6486           macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6487           break;
6488         }
6489       goto do_addi_i;
6490     case M_DADDU_I:
6491       dbl = 1;
6492       s = "daddiu";
6493       s2 = "daddu";
6494     do_addi:
6495       if (imm_expr.X_op == O_constant
6496           && imm_expr.X_add_number >= -0x8000
6497           && imm_expr.X_add_number < 0x8000)
6498         {
6499           macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6500           break;
6501         }
6502     do_addi_i:
6503       used_at = 1;
6504       load_register (AT, &imm_expr, dbl);
6505       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6506       break;
6507
6508     case M_AND_I:
6509       s = "andi";
6510       s2 = "and";
6511       goto do_bit;
6512     case M_OR_I:
6513       s = "ori";
6514       s2 = "or";
6515       goto do_bit;
6516     case M_NOR_I:
6517       s = "";
6518       s2 = "nor";
6519       goto do_bit;
6520     case M_XOR_I:
6521       s = "xori";
6522       s2 = "xor";
6523     do_bit:
6524       if (imm_expr.X_op == O_constant
6525           && imm_expr.X_add_number >= 0
6526           && imm_expr.X_add_number < 0x10000)
6527         {
6528           if (mask != M_NOR_I)
6529             macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6530           else
6531             {
6532               macro_build (&imm_expr, "ori", "t,r,i",
6533                            treg, sreg, BFD_RELOC_LO16);
6534               macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6535             }
6536           break;
6537         }
6538
6539       used_at = 1;
6540       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6541       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6542       break;
6543
6544     case M_BALIGN:
6545       switch (imm_expr.X_add_number)
6546         {
6547         case 0:
6548           macro_build (NULL, "nop", "");
6549           break;
6550         case 2:
6551           macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6552           break;
6553         case 1:
6554         case 3:
6555           macro_build (NULL, "balign", "t,s,2", treg, sreg,
6556                        (int) imm_expr.X_add_number);
6557           break;
6558         default:
6559           as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6560                   (unsigned long) imm_expr.X_add_number);
6561           break;
6562         }
6563       break;
6564
6565     case M_BC1FL:
6566     case M_BC1TL:
6567     case M_BC2FL:
6568     case M_BC2TL:
6569       gas_assert (mips_opts.micromips);
6570       macro_build_branch_ccl (mask, &offset_expr,
6571                               EXTRACT_OPERAND (1, BCC, *ip));
6572       break;
6573
6574     case M_BEQ_I:
6575     case M_BEQL_I:
6576     case M_BNE_I:
6577     case M_BNEL_I:
6578       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6579         treg = 0;
6580       else
6581         {
6582           treg = AT;
6583           used_at = 1;
6584           load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
6585         }
6586       /* Fall through.  */
6587     case M_BEQL:
6588     case M_BNEL:
6589       macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
6590       break;
6591
6592     case M_BGEL:
6593       likely = 1;
6594     case M_BGE:
6595       if (treg == 0)
6596         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6597       else if (sreg == 0)
6598         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6599       else
6600         {
6601           used_at = 1;
6602           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6603           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6604                                    &offset_expr, AT, ZERO);
6605         }
6606       break;
6607
6608     case M_BGEZL:
6609     case M_BGEZALL:
6610     case M_BGTZL:
6611     case M_BLEZL:
6612     case M_BLTZL:
6613     case M_BLTZALL:
6614       macro_build_branch_rs (mask, &offset_expr, sreg);
6615       break;
6616
6617     case M_BGTL_I:
6618       likely = 1;
6619     case M_BGT_I:
6620       /* Check for > max integer.  */
6621       maxnum = 0x7fffffff;
6622       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6623         {
6624           maxnum <<= 16;
6625           maxnum |= 0xffff;
6626           maxnum <<= 16;
6627           maxnum |= 0xffff;
6628         }
6629       if (imm_expr.X_op == O_constant
6630           && imm_expr.X_add_number >= maxnum
6631           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6632         {
6633         do_false:
6634           /* Result is always false.  */
6635           if (! likely)
6636             macro_build (NULL, "nop", "");
6637           else
6638             macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
6639           break;
6640         }
6641       if (imm_expr.X_op != O_constant)
6642         as_bad (_("Unsupported large constant"));
6643       ++imm_expr.X_add_number;
6644       /* FALLTHROUGH */
6645     case M_BGE_I:
6646     case M_BGEL_I:
6647       if (mask == M_BGEL_I)
6648         likely = 1;
6649       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6650         {
6651           macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6652                                  &offset_expr, sreg);
6653           break;
6654         }
6655       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6656         {
6657           macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6658                                  &offset_expr, sreg);
6659           break;
6660         }
6661       maxnum = 0x7fffffff;
6662       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6663         {
6664           maxnum <<= 16;
6665           maxnum |= 0xffff;
6666           maxnum <<= 16;
6667           maxnum |= 0xffff;
6668         }
6669       maxnum = - maxnum - 1;
6670       if (imm_expr.X_op == O_constant
6671           && imm_expr.X_add_number <= maxnum
6672           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6673         {
6674         do_true:
6675           /* result is always true */
6676           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
6677           macro_build (&offset_expr, "b", "p");
6678           break;
6679         }
6680       used_at = 1;
6681       set_at (sreg, 0);
6682       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6683                                &offset_expr, AT, ZERO);
6684       break;
6685
6686     case M_BGEUL:
6687       likely = 1;
6688     case M_BGEU:
6689       if (treg == 0)
6690         goto do_true;
6691       else if (sreg == 0)
6692         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6693                                  &offset_expr, ZERO, treg);
6694       else
6695         {
6696           used_at = 1;
6697           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6698           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6699                                    &offset_expr, AT, ZERO);
6700         }
6701       break;
6702
6703     case M_BGTUL_I:
6704       likely = 1;
6705     case M_BGTU_I:
6706       if (sreg == 0
6707           || (HAVE_32BIT_GPRS
6708               && imm_expr.X_op == O_constant
6709               && imm_expr.X_add_number == -1))
6710         goto do_false;
6711       if (imm_expr.X_op != O_constant)
6712         as_bad (_("Unsupported large constant"));
6713       ++imm_expr.X_add_number;
6714       /* FALLTHROUGH */
6715     case M_BGEU_I:
6716     case M_BGEUL_I:
6717       if (mask == M_BGEUL_I)
6718         likely = 1;
6719       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6720         goto do_true;
6721       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6722         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6723                                  &offset_expr, sreg, ZERO);
6724       else
6725         {
6726           used_at = 1;
6727           set_at (sreg, 1);
6728           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6729                                    &offset_expr, AT, ZERO);
6730         }
6731       break;
6732
6733     case M_BGTL:
6734       likely = 1;
6735     case M_BGT:
6736       if (treg == 0)
6737         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6738       else if (sreg == 0)
6739         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6740       else
6741         {
6742           used_at = 1;
6743           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6744           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6745                                    &offset_expr, AT, ZERO);
6746         }
6747       break;
6748
6749     case M_BGTUL:
6750       likely = 1;
6751     case M_BGTU:
6752       if (treg == 0)
6753         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6754                                  &offset_expr, sreg, ZERO);
6755       else if (sreg == 0)
6756         goto do_false;
6757       else
6758         {
6759           used_at = 1;
6760           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6761           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6762                                    &offset_expr, AT, ZERO);
6763         }
6764       break;
6765
6766     case M_BLEL:
6767       likely = 1;
6768     case M_BLE:
6769       if (treg == 0)
6770         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6771       else if (sreg == 0)
6772         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6773       else
6774         {
6775           used_at = 1;
6776           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6777           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6778                                    &offset_expr, AT, ZERO);
6779         }
6780       break;
6781
6782     case M_BLEL_I:
6783       likely = 1;
6784     case M_BLE_I:
6785       maxnum = 0x7fffffff;
6786       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6787         {
6788           maxnum <<= 16;
6789           maxnum |= 0xffff;
6790           maxnum <<= 16;
6791           maxnum |= 0xffff;
6792         }
6793       if (imm_expr.X_op == O_constant
6794           && imm_expr.X_add_number >= maxnum
6795           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6796         goto do_true;
6797       if (imm_expr.X_op != O_constant)
6798         as_bad (_("Unsupported large constant"));
6799       ++imm_expr.X_add_number;
6800       /* FALLTHROUGH */
6801     case M_BLT_I:
6802     case M_BLTL_I:
6803       if (mask == M_BLTL_I)
6804         likely = 1;
6805       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6806         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6807       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6808         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6809       else
6810         {
6811           used_at = 1;
6812           set_at (sreg, 0);
6813           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6814                                    &offset_expr, AT, ZERO);
6815         }
6816       break;
6817
6818     case M_BLEUL:
6819       likely = 1;
6820     case M_BLEU:
6821       if (treg == 0)
6822         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6823                                  &offset_expr, sreg, ZERO);
6824       else if (sreg == 0)
6825         goto do_true;
6826       else
6827         {
6828           used_at = 1;
6829           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6830           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6831                                    &offset_expr, AT, ZERO);
6832         }
6833       break;
6834
6835     case M_BLEUL_I:
6836       likely = 1;
6837     case M_BLEU_I:
6838       if (sreg == 0
6839           || (HAVE_32BIT_GPRS
6840               && imm_expr.X_op == O_constant
6841               && imm_expr.X_add_number == -1))
6842         goto do_true;
6843       if (imm_expr.X_op != O_constant)
6844         as_bad (_("Unsupported large constant"));
6845       ++imm_expr.X_add_number;
6846       /* FALLTHROUGH */
6847     case M_BLTU_I:
6848     case M_BLTUL_I:
6849       if (mask == M_BLTUL_I)
6850         likely = 1;
6851       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6852         goto do_false;
6853       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6854         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6855                                  &offset_expr, sreg, ZERO);
6856       else
6857         {
6858           used_at = 1;
6859           set_at (sreg, 1);
6860           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6861                                    &offset_expr, AT, ZERO);
6862         }
6863       break;
6864
6865     case M_BLTL:
6866       likely = 1;
6867     case M_BLT:
6868       if (treg == 0)
6869         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6870       else if (sreg == 0)
6871         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6872       else
6873         {
6874           used_at = 1;
6875           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6876           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6877                                    &offset_expr, AT, ZERO);
6878         }
6879       break;
6880
6881     case M_BLTUL:
6882       likely = 1;
6883     case M_BLTU:
6884       if (treg == 0)
6885         goto do_false;
6886       else if (sreg == 0)
6887         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6888                                  &offset_expr, ZERO, treg);
6889       else
6890         {
6891           used_at = 1;
6892           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6893           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6894                                    &offset_expr, AT, ZERO);
6895         }
6896       break;
6897
6898     case M_DEXT:
6899       {
6900         /* Use unsigned arithmetic.  */
6901         addressT pos;
6902         addressT size;
6903
6904         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6905           {
6906             as_bad (_("Unsupported large constant"));
6907             pos = size = 1;
6908           }
6909         else
6910           {
6911             pos = imm_expr.X_add_number;
6912             size = imm2_expr.X_add_number;
6913           }
6914
6915         if (pos > 63)
6916           {
6917             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6918             pos = 1;
6919           }
6920         if (size == 0 || size > 64 || (pos + size - 1) > 63)
6921           {
6922             as_bad (_("Improper extract size (%lu, position %lu)"),
6923                     (unsigned long) size, (unsigned long) pos);
6924             size = 1;
6925           }
6926
6927         if (size <= 32 && pos < 32)
6928           {
6929             s = "dext";
6930             fmt = "t,r,+A,+C";
6931           }
6932         else if (size <= 32)
6933           {
6934             s = "dextu";
6935             fmt = "t,r,+E,+H";
6936           }
6937         else
6938           {
6939             s = "dextm";
6940             fmt = "t,r,+A,+G";
6941           }
6942         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6943                      (int) (size - 1));
6944       }
6945       break;
6946
6947     case M_DINS:
6948       {
6949         /* Use unsigned arithmetic.  */
6950         addressT pos;
6951         addressT size;
6952
6953         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6954           {
6955             as_bad (_("Unsupported large constant"));
6956             pos = size = 1;
6957           }
6958         else
6959           {
6960             pos = imm_expr.X_add_number;
6961             size = imm2_expr.X_add_number;
6962           }
6963
6964         if (pos > 63)
6965           {
6966             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6967             pos = 1;
6968           }
6969         if (size == 0 || size > 64 || (pos + size - 1) > 63)
6970           {
6971             as_bad (_("Improper insert size (%lu, position %lu)"),
6972                     (unsigned long) size, (unsigned long) pos);
6973             size = 1;
6974           }
6975
6976         if (pos < 32 && (pos + size - 1) < 32)
6977           {
6978             s = "dins";
6979             fmt = "t,r,+A,+B";
6980           }
6981         else if (pos >= 32)
6982           {
6983             s = "dinsu";
6984             fmt = "t,r,+E,+F";
6985           }
6986         else
6987           {
6988             s = "dinsm";
6989             fmt = "t,r,+A,+F";
6990           }
6991         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6992                      (int) (pos + size - 1));
6993       }
6994       break;
6995
6996     case M_DDIV_3:
6997       dbl = 1;
6998     case M_DIV_3:
6999       s = "mflo";
7000       goto do_div3;
7001     case M_DREM_3:
7002       dbl = 1;
7003     case M_REM_3:
7004       s = "mfhi";
7005     do_div3:
7006       if (treg == 0)
7007         {
7008           as_warn (_("Divide by zero."));
7009           if (mips_trap)
7010             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7011           else
7012             macro_build (NULL, "break", BRK_FMT, 7);
7013           break;
7014         }
7015
7016       start_noreorder ();
7017       if (mips_trap)
7018         {
7019           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7020           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7021         }
7022       else
7023         {
7024           if (mips_opts.micromips)
7025             micromips_label_expr (&label_expr);
7026           else
7027             label_expr.X_add_number = 8;
7028           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7029           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
7030           macro_build (NULL, "break", BRK_FMT, 7);
7031           if (mips_opts.micromips)
7032             micromips_add_label ();
7033         }
7034       expr1.X_add_number = -1;
7035       used_at = 1;
7036       load_register (AT, &expr1, dbl);
7037       if (mips_opts.micromips)
7038         micromips_label_expr (&label_expr);
7039       else
7040         label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7041       macro_build (&label_expr, "bne", "s,t,p", treg, AT);
7042       if (dbl)
7043         {
7044           expr1.X_add_number = 1;
7045           load_register (AT, &expr1, dbl);
7046           macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
7047         }
7048       else
7049         {
7050           expr1.X_add_number = 0x80000000;
7051           macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
7052         }
7053       if (mips_trap)
7054         {
7055           macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
7056           /* We want to close the noreorder block as soon as possible, so
7057              that later insns are available for delay slot filling.  */
7058           end_noreorder ();
7059         }
7060       else
7061         {
7062           if (mips_opts.micromips)
7063             micromips_label_expr (&label_expr);
7064           else
7065             label_expr.X_add_number = 8;
7066           macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
7067           macro_build (NULL, "nop", "");
7068
7069           /* We want to close the noreorder block as soon as possible, so
7070              that later insns are available for delay slot filling.  */
7071           end_noreorder ();
7072
7073           macro_build (NULL, "break", BRK_FMT, 6);
7074         }
7075       if (mips_opts.micromips)
7076         micromips_add_label ();
7077       macro_build (NULL, s, MFHL_FMT, dreg);
7078       break;
7079
7080     case M_DIV_3I:
7081       s = "div";
7082       s2 = "mflo";
7083       goto do_divi;
7084     case M_DIVU_3I:
7085       s = "divu";
7086       s2 = "mflo";
7087       goto do_divi;
7088     case M_REM_3I:
7089       s = "div";
7090       s2 = "mfhi";
7091       goto do_divi;
7092     case M_REMU_3I:
7093       s = "divu";
7094       s2 = "mfhi";
7095       goto do_divi;
7096     case M_DDIV_3I:
7097       dbl = 1;
7098       s = "ddiv";
7099       s2 = "mflo";
7100       goto do_divi;
7101     case M_DDIVU_3I:
7102       dbl = 1;
7103       s = "ddivu";
7104       s2 = "mflo";
7105       goto do_divi;
7106     case M_DREM_3I:
7107       dbl = 1;
7108       s = "ddiv";
7109       s2 = "mfhi";
7110       goto do_divi;
7111     case M_DREMU_3I:
7112       dbl = 1;
7113       s = "ddivu";
7114       s2 = "mfhi";
7115     do_divi:
7116       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7117         {
7118           as_warn (_("Divide by zero."));
7119           if (mips_trap)
7120             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7121           else
7122             macro_build (NULL, "break", BRK_FMT, 7);
7123           break;
7124         }
7125       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7126         {
7127           if (strcmp (s2, "mflo") == 0)
7128             move_register (dreg, sreg);
7129           else
7130             move_register (dreg, ZERO);
7131           break;
7132         }
7133       if (imm_expr.X_op == O_constant
7134           && imm_expr.X_add_number == -1
7135           && s[strlen (s) - 1] != 'u')
7136         {
7137           if (strcmp (s2, "mflo") == 0)
7138             {
7139               macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
7140             }
7141           else
7142             move_register (dreg, ZERO);
7143           break;
7144         }
7145
7146       used_at = 1;
7147       load_register (AT, &imm_expr, dbl);
7148       macro_build (NULL, s, "z,s,t", sreg, AT);
7149       macro_build (NULL, s2, MFHL_FMT, dreg);
7150       break;
7151
7152     case M_DIVU_3:
7153       s = "divu";
7154       s2 = "mflo";
7155       goto do_divu3;
7156     case M_REMU_3:
7157       s = "divu";
7158       s2 = "mfhi";
7159       goto do_divu3;
7160     case M_DDIVU_3:
7161       s = "ddivu";
7162       s2 = "mflo";
7163       goto do_divu3;
7164     case M_DREMU_3:
7165       s = "ddivu";
7166       s2 = "mfhi";
7167     do_divu3:
7168       start_noreorder ();
7169       if (mips_trap)
7170         {
7171           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7172           macro_build (NULL, s, "z,s,t", sreg, treg);
7173           /* We want to close the noreorder block as soon as possible, so
7174              that later insns are available for delay slot filling.  */
7175           end_noreorder ();
7176         }
7177       else
7178         {
7179           if (mips_opts.micromips)
7180             micromips_label_expr (&label_expr);
7181           else
7182             label_expr.X_add_number = 8;
7183           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7184           macro_build (NULL, s, "z,s,t", sreg, treg);
7185
7186           /* We want to close the noreorder block as soon as possible, so
7187              that later insns are available for delay slot filling.  */
7188           end_noreorder ();
7189           macro_build (NULL, "break", BRK_FMT, 7);
7190           if (mips_opts.micromips)
7191             micromips_add_label ();
7192         }
7193       macro_build (NULL, s2, MFHL_FMT, dreg);
7194       break;
7195
7196     case M_DLCA_AB:
7197       dbl = 1;
7198     case M_LCA_AB:
7199       call = 1;
7200       goto do_la;
7201     case M_DLA_AB:
7202       dbl = 1;
7203     case M_LA_AB:
7204     do_la:
7205       /* Load the address of a symbol into a register.  If breg is not
7206          zero, we then add a base register to it.  */
7207
7208       if (dbl && HAVE_32BIT_GPRS)
7209         as_warn (_("dla used to load 32-bit register"));
7210
7211       if (!dbl && HAVE_64BIT_OBJECTS)
7212         as_warn (_("la used to load 64-bit address"));
7213
7214       if (offset_expr.X_op == O_constant
7215           && offset_expr.X_add_number >= -0x8000
7216           && offset_expr.X_add_number < 0x8000)
7217         {
7218           macro_build (&offset_expr, ADDRESS_ADDI_INSN,
7219                        "t,r,j", treg, sreg, BFD_RELOC_LO16);
7220           break;
7221         }
7222
7223       if (mips_opts.at && (treg == breg))
7224         {
7225           tempreg = AT;
7226           used_at = 1;
7227         }
7228       else
7229         {
7230           tempreg = treg;
7231         }
7232
7233       if (offset_expr.X_op != O_symbol
7234           && offset_expr.X_op != O_constant)
7235         {
7236           as_bad (_("Expression too complex"));
7237           offset_expr.X_op = O_constant;
7238         }
7239
7240       if (offset_expr.X_op == O_constant)
7241         load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7242       else if (mips_pic == NO_PIC)
7243         {
7244           /* If this is a reference to a GP relative symbol, we want
7245                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
7246              Otherwise we want
7247                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
7248                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7249              If we have a constant, we need two instructions anyhow,
7250              so we may as well always use the latter form.
7251
7252              With 64bit address space and a usable $at we want
7253                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7254                lui      $at,<sym>               (BFD_RELOC_HI16_S)
7255                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7256                daddiu   $at,<sym>               (BFD_RELOC_LO16)
7257                dsll32   $tempreg,0
7258                daddu    $tempreg,$tempreg,$at
7259
7260              If $at is already in use, we use a path which is suboptimal
7261              on superscalar processors.
7262                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7263                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7264                dsll     $tempreg,16
7265                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
7266                dsll     $tempreg,16
7267                daddiu   $tempreg,<sym>          (BFD_RELOC_LO16)
7268
7269              For GP relative symbols in 64bit address space we can use
7270              the same sequence as in 32bit address space.  */
7271           if (HAVE_64BIT_SYMBOLS)
7272             {
7273               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7274                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7275                 {
7276                   relax_start (offset_expr.X_add_symbol);
7277                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7278                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7279                   relax_switch ();
7280                 }
7281
7282               if (used_at == 0 && mips_opts.at)
7283                 {
7284                   macro_build (&offset_expr, "lui", LUI_FMT,
7285                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7286                   macro_build (&offset_expr, "lui", LUI_FMT,
7287                                AT, BFD_RELOC_HI16_S);
7288                   macro_build (&offset_expr, "daddiu", "t,r,j",
7289                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7290                   macro_build (&offset_expr, "daddiu", "t,r,j",
7291                                AT, AT, BFD_RELOC_LO16);
7292                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7293                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7294                   used_at = 1;
7295                 }
7296               else
7297                 {
7298                   macro_build (&offset_expr, "lui", LUI_FMT,
7299                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7300                   macro_build (&offset_expr, "daddiu", "t,r,j",
7301                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7302                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7303                   macro_build (&offset_expr, "daddiu", "t,r,j",
7304                                tempreg, tempreg, BFD_RELOC_HI16_S);
7305                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7306                   macro_build (&offset_expr, "daddiu", "t,r,j",
7307                                tempreg, tempreg, BFD_RELOC_LO16);
7308                 }
7309
7310               if (mips_relax.sequence)
7311                 relax_end ();
7312             }
7313           else
7314             {
7315               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7316                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7317                 {
7318                   relax_start (offset_expr.X_add_symbol);
7319                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7320                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7321                   relax_switch ();
7322                 }
7323               if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7324                 as_bad (_("Offset too large"));
7325               macro_build_lui (&offset_expr, tempreg);
7326               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7327                            tempreg, tempreg, BFD_RELOC_LO16);
7328               if (mips_relax.sequence)
7329                 relax_end ();
7330             }
7331         }
7332       else if (!mips_big_got && !HAVE_NEWABI)
7333         {
7334           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7335
7336           /* If this is a reference to an external symbol, and there
7337              is no constant, we want
7338                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7339              or for lca or if tempreg is PIC_CALL_REG
7340                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7341              For a local symbol, we want
7342                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7343                nop
7344                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7345
7346              If we have a small constant, and this is a reference to
7347              an external symbol, we want
7348                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7349                nop
7350                addiu    $tempreg,$tempreg,<constant>
7351              For a local symbol, we want the same instruction
7352              sequence, but we output a BFD_RELOC_LO16 reloc on the
7353              addiu instruction.
7354
7355              If we have a large constant, and this is a reference to
7356              an external symbol, we want
7357                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7358                lui      $at,<hiconstant>
7359                addiu    $at,$at,<loconstant>
7360                addu     $tempreg,$tempreg,$at
7361              For a local symbol, we want the same instruction
7362              sequence, but we output a BFD_RELOC_LO16 reloc on the
7363              addiu instruction.
7364            */
7365
7366           if (offset_expr.X_add_number == 0)
7367             {
7368               if (mips_pic == SVR4_PIC
7369                   && breg == 0
7370                   && (call || tempreg == PIC_CALL_REG))
7371                 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7372
7373               relax_start (offset_expr.X_add_symbol);
7374               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7375                            lw_reloc_type, mips_gp_register);
7376               if (breg != 0)
7377                 {
7378                   /* We're going to put in an addu instruction using
7379                      tempreg, so we may as well insert the nop right
7380                      now.  */
7381                   load_delay_nop ();
7382                 }
7383               relax_switch ();
7384               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7385                            tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7386               load_delay_nop ();
7387               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7388                            tempreg, tempreg, BFD_RELOC_LO16);
7389               relax_end ();
7390               /* FIXME: If breg == 0, and the next instruction uses
7391                  $tempreg, then if this variant case is used an extra
7392                  nop will be generated.  */
7393             }
7394           else if (offset_expr.X_add_number >= -0x8000
7395                    && offset_expr.X_add_number < 0x8000)
7396             {
7397               load_got_offset (tempreg, &offset_expr);
7398               load_delay_nop ();
7399               add_got_offset (tempreg, &offset_expr);
7400             }
7401           else
7402             {
7403               expr1.X_add_number = offset_expr.X_add_number;
7404               offset_expr.X_add_number =
7405                 SEXT_16BIT (offset_expr.X_add_number);
7406               load_got_offset (tempreg, &offset_expr);
7407               offset_expr.X_add_number = expr1.X_add_number;
7408               /* If we are going to add in a base register, and the
7409                  target register and the base register are the same,
7410                  then we are using AT as a temporary register.  Since
7411                  we want to load the constant into AT, we add our
7412                  current AT (from the global offset table) and the
7413                  register into the register now, and pretend we were
7414                  not using a base register.  */
7415               if (breg == treg)
7416                 {
7417                   load_delay_nop ();
7418                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7419                                treg, AT, breg);
7420                   breg = 0;
7421                   tempreg = treg;
7422                 }
7423               add_got_offset_hilo (tempreg, &offset_expr, AT);
7424               used_at = 1;
7425             }
7426         }
7427       else if (!mips_big_got && HAVE_NEWABI)
7428         {
7429           int add_breg_early = 0;
7430
7431           /* If this is a reference to an external, and there is no
7432              constant, or local symbol (*), with or without a
7433              constant, we want
7434                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7435              or for lca or if tempreg is PIC_CALL_REG
7436                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7437
7438              If we have a small constant, and this is a reference to
7439              an external symbol, we want
7440                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7441                addiu    $tempreg,$tempreg,<constant>
7442
7443              If we have a large constant, and this is a reference to
7444              an external symbol, we want
7445                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7446                lui      $at,<hiconstant>
7447                addiu    $at,$at,<loconstant>
7448                addu     $tempreg,$tempreg,$at
7449
7450              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7451              local symbols, even though it introduces an additional
7452              instruction.  */
7453
7454           if (offset_expr.X_add_number)
7455             {
7456               expr1.X_add_number = offset_expr.X_add_number;
7457               offset_expr.X_add_number = 0;
7458
7459               relax_start (offset_expr.X_add_symbol);
7460               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7461                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7462
7463               if (expr1.X_add_number >= -0x8000
7464                   && expr1.X_add_number < 0x8000)
7465                 {
7466                   macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7467                                tempreg, tempreg, BFD_RELOC_LO16);
7468                 }
7469               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7470                 {
7471                   /* If we are going to add in a base register, and the
7472                      target register and the base register are the same,
7473                      then we are using AT as a temporary register.  Since
7474                      we want to load the constant into AT, we add our
7475                      current AT (from the global offset table) and the
7476                      register into the register now, and pretend we were
7477                      not using a base register.  */
7478                   if (breg != treg)
7479                     dreg = tempreg;
7480                   else
7481                     {
7482                       gas_assert (tempreg == AT);
7483                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7484                                    treg, AT, breg);
7485                       dreg = treg;
7486                       add_breg_early = 1;
7487                     }
7488
7489                   load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7490                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7491                                dreg, dreg, AT);
7492
7493                   used_at = 1;
7494                 }
7495               else
7496                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7497
7498               relax_switch ();
7499               offset_expr.X_add_number = expr1.X_add_number;
7500
7501               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7502                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7503               if (add_breg_early)
7504                 {
7505                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7506                                treg, tempreg, breg);
7507                   breg = 0;
7508                   tempreg = treg;
7509                 }
7510               relax_end ();
7511             }
7512           else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7513             {
7514               relax_start (offset_expr.X_add_symbol);
7515               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7516                            BFD_RELOC_MIPS_CALL16, mips_gp_register);
7517               relax_switch ();
7518               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7519                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7520               relax_end ();
7521             }
7522           else
7523             {
7524               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7525                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7526             }
7527         }
7528       else if (mips_big_got && !HAVE_NEWABI)
7529         {
7530           int gpdelay;
7531           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7532           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7533           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7534
7535           /* This is the large GOT case.  If this is a reference to an
7536              external symbol, and there is no constant, we want
7537                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7538                addu     $tempreg,$tempreg,$gp
7539                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7540              or for lca or if tempreg is PIC_CALL_REG
7541                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
7542                addu     $tempreg,$tempreg,$gp
7543                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7544              For a local symbol, we want
7545                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7546                nop
7547                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7548
7549              If we have a small constant, and this is a reference to
7550              an external symbol, we want
7551                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7552                addu     $tempreg,$tempreg,$gp
7553                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7554                nop
7555                addiu    $tempreg,$tempreg,<constant>
7556              For a local symbol, we want
7557                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7558                nop
7559                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7560
7561              If we have a large constant, and this is a reference to
7562              an external symbol, we want
7563                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7564                addu     $tempreg,$tempreg,$gp
7565                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7566                lui      $at,<hiconstant>
7567                addiu    $at,$at,<loconstant>
7568                addu     $tempreg,$tempreg,$at
7569              For a local symbol, we want
7570                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7571                lui      $at,<hiconstant>
7572                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
7573                addu     $tempreg,$tempreg,$at
7574           */
7575
7576           expr1.X_add_number = offset_expr.X_add_number;
7577           offset_expr.X_add_number = 0;
7578           relax_start (offset_expr.X_add_symbol);
7579           gpdelay = reg_needs_delay (mips_gp_register);
7580           if (expr1.X_add_number == 0 && breg == 0
7581               && (call || tempreg == PIC_CALL_REG))
7582             {
7583               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7584               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7585             }
7586           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7587           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7588                        tempreg, tempreg, mips_gp_register);
7589           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7590                        tempreg, lw_reloc_type, tempreg);
7591           if (expr1.X_add_number == 0)
7592             {
7593               if (breg != 0)
7594                 {
7595                   /* We're going to put in an addu instruction using
7596                      tempreg, so we may as well insert the nop right
7597                      now.  */
7598                   load_delay_nop ();
7599                 }
7600             }
7601           else if (expr1.X_add_number >= -0x8000
7602                    && expr1.X_add_number < 0x8000)
7603             {
7604               load_delay_nop ();
7605               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7606                            tempreg, tempreg, BFD_RELOC_LO16);
7607             }
7608           else
7609             {
7610               /* If we are going to add in a base register, and the
7611                  target register and the base register are the same,
7612                  then we are using AT as a temporary register.  Since
7613                  we want to load the constant into AT, we add our
7614                  current AT (from the global offset table) and the
7615                  register into the register now, and pretend we were
7616                  not using a base register.  */
7617               if (breg != treg)
7618                 dreg = tempreg;
7619               else
7620                 {
7621                   gas_assert (tempreg == AT);
7622                   load_delay_nop ();
7623                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7624                                treg, AT, breg);
7625                   dreg = treg;
7626                 }
7627
7628               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7629               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7630
7631               used_at = 1;
7632             }
7633           offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
7634           relax_switch ();
7635
7636           if (gpdelay)
7637             {
7638               /* This is needed because this instruction uses $gp, but
7639                  the first instruction on the main stream does not.  */
7640               macro_build (NULL, "nop", "");
7641             }
7642
7643           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7644                        local_reloc_type, mips_gp_register);
7645           if (expr1.X_add_number >= -0x8000
7646               && expr1.X_add_number < 0x8000)
7647             {
7648               load_delay_nop ();
7649               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7650                            tempreg, tempreg, BFD_RELOC_LO16);
7651               /* FIXME: If add_number is 0, and there was no base
7652                  register, the external symbol case ended with a load,
7653                  so if the symbol turns out to not be external, and
7654                  the next instruction uses tempreg, an unnecessary nop
7655                  will be inserted.  */
7656             }
7657           else
7658             {
7659               if (breg == treg)
7660                 {
7661                   /* We must add in the base register now, as in the
7662                      external symbol case.  */
7663                   gas_assert (tempreg == AT);
7664                   load_delay_nop ();
7665                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7666                                treg, AT, breg);
7667                   tempreg = treg;
7668                   /* We set breg to 0 because we have arranged to add
7669                      it in in both cases.  */
7670                   breg = 0;
7671                 }
7672
7673               macro_build_lui (&expr1, AT);
7674               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7675                            AT, AT, BFD_RELOC_LO16);
7676               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7677                            tempreg, tempreg, AT);
7678               used_at = 1;
7679             }
7680           relax_end ();
7681         }
7682       else if (mips_big_got && HAVE_NEWABI)
7683         {
7684           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7685           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7686           int add_breg_early = 0;
7687
7688           /* This is the large GOT case.  If this is a reference to an
7689              external symbol, and there is no constant, we want
7690                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7691                add      $tempreg,$tempreg,$gp
7692                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7693              or for lca or if tempreg is PIC_CALL_REG
7694                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
7695                add      $tempreg,$tempreg,$gp
7696                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7697
7698              If we have a small constant, and this is a reference to
7699              an external symbol, we want
7700                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7701                add      $tempreg,$tempreg,$gp
7702                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7703                addi     $tempreg,$tempreg,<constant>
7704
7705              If we have a large constant, and this is a reference to
7706              an external symbol, we want
7707                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7708                addu     $tempreg,$tempreg,$gp
7709                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7710                lui      $at,<hiconstant>
7711                addi     $at,$at,<loconstant>
7712                add      $tempreg,$tempreg,$at
7713
7714              If we have NewABI, and we know it's a local symbol, we want
7715                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
7716                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
7717              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
7718
7719           relax_start (offset_expr.X_add_symbol);
7720
7721           expr1.X_add_number = offset_expr.X_add_number;
7722           offset_expr.X_add_number = 0;
7723
7724           if (expr1.X_add_number == 0 && breg == 0
7725               && (call || tempreg == PIC_CALL_REG))
7726             {
7727               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7728               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7729             }
7730           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7731           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7732                        tempreg, tempreg, mips_gp_register);
7733           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7734                        tempreg, lw_reloc_type, tempreg);
7735
7736           if (expr1.X_add_number == 0)
7737             ;
7738           else if (expr1.X_add_number >= -0x8000
7739                    && expr1.X_add_number < 0x8000)
7740             {
7741               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7742                            tempreg, tempreg, BFD_RELOC_LO16);
7743             }
7744           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7745             {
7746               /* If we are going to add in a base register, and the
7747                  target register and the base register are the same,
7748                  then we are using AT as a temporary register.  Since
7749                  we want to load the constant into AT, we add our
7750                  current AT (from the global offset table) and the
7751                  register into the register now, and pretend we were
7752                  not using a base register.  */
7753               if (breg != treg)
7754                 dreg = tempreg;
7755               else
7756                 {
7757                   gas_assert (tempreg == AT);
7758                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7759                                treg, AT, breg);
7760                   dreg = treg;
7761                   add_breg_early = 1;
7762                 }
7763
7764               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7765               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7766
7767               used_at = 1;
7768             }
7769           else
7770             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7771
7772           relax_switch ();
7773           offset_expr.X_add_number = expr1.X_add_number;
7774           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7775                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7776           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7777                        tempreg, BFD_RELOC_MIPS_GOT_OFST);
7778           if (add_breg_early)
7779             {
7780               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7781                            treg, tempreg, breg);
7782               breg = 0;
7783               tempreg = treg;
7784             }
7785           relax_end ();
7786         }
7787       else
7788         abort ();
7789
7790       if (breg != 0)
7791         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
7792       break;
7793
7794     case M_MSGSND:
7795       gas_assert (!mips_opts.micromips);
7796       {
7797         unsigned long temp = (treg << 16) | (0x01);
7798         macro_build (NULL, "c2", "C", temp);
7799       }
7800       break;
7801
7802     case M_MSGLD:
7803       gas_assert (!mips_opts.micromips);
7804       {
7805         unsigned long temp = (0x02);
7806         macro_build (NULL, "c2", "C", temp);
7807       }
7808       break;
7809
7810     case M_MSGLD_T:
7811       gas_assert (!mips_opts.micromips);
7812       {
7813         unsigned long temp = (treg << 16) | (0x02);
7814         macro_build (NULL, "c2", "C", temp);
7815       }
7816       break;
7817
7818     case M_MSGWAIT:
7819       gas_assert (!mips_opts.micromips);
7820       macro_build (NULL, "c2", "C", 3);
7821       break;
7822
7823     case M_MSGWAIT_T:
7824       gas_assert (!mips_opts.micromips);
7825       {
7826         unsigned long temp = (treg << 16) | 0x03;
7827         macro_build (NULL, "c2", "C", temp);
7828       }
7829       break;
7830
7831     case M_J_A:
7832       /* The j instruction may not be used in PIC code, since it
7833          requires an absolute address.  We convert it to a b
7834          instruction.  */
7835       if (mips_pic == NO_PIC)
7836         macro_build (&offset_expr, "j", "a");
7837       else
7838         macro_build (&offset_expr, "b", "p");
7839       break;
7840
7841       /* The jal instructions must be handled as macros because when
7842          generating PIC code they expand to multi-instruction
7843          sequences.  Normally they are simple instructions.  */
7844     case M_JALS_1:
7845       dreg = RA;
7846       /* Fall through.  */
7847     case M_JALS_2:
7848       gas_assert (mips_opts.micromips);
7849       jals = 1;
7850       goto jal;
7851     case M_JAL_1:
7852       dreg = RA;
7853       /* Fall through.  */
7854     case M_JAL_2:
7855     jal:
7856       if (mips_pic == NO_PIC)
7857         {
7858           s = jals ? "jalrs" : "jalr";
7859           if (mips_opts.micromips
7860               && dreg == RA
7861               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7862             macro_build (NULL, s, "mj", sreg);
7863           else
7864             macro_build (NULL, s, JALR_FMT, dreg, sreg);
7865         }
7866       else
7867         {
7868           int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7869                            && mips_cprestore_offset >= 0);
7870
7871           if (sreg != PIC_CALL_REG)
7872             as_warn (_("MIPS PIC call to register other than $25"));
7873
7874           s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7875                ? "jalrs" : "jalr");
7876           if (mips_opts.micromips
7877               && dreg == RA
7878               && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7879             macro_build (NULL, s, "mj", sreg);
7880           else
7881             macro_build (NULL, s, JALR_FMT, dreg, sreg);
7882           if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
7883             {
7884               if (mips_cprestore_offset < 0)
7885                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7886               else
7887                 {
7888                   if (!mips_frame_reg_valid)
7889                     {
7890                       as_warn (_("No .frame pseudo-op used in PIC code"));
7891                       /* Quiet this warning.  */
7892                       mips_frame_reg_valid = 1;
7893                     }
7894                   if (!mips_cprestore_valid)
7895                     {
7896                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
7897                       /* Quiet this warning.  */
7898                       mips_cprestore_valid = 1;
7899                     }
7900                   if (mips_opts.noreorder)
7901                     macro_build (NULL, "nop", "");
7902                   expr1.X_add_number = mips_cprestore_offset;
7903                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7904                                                 mips_gp_register,
7905                                                 mips_frame_reg,
7906                                                 HAVE_64BIT_ADDRESSES);
7907                 }
7908             }
7909         }
7910
7911       break;
7912
7913     case M_JALS_A:
7914       gas_assert (mips_opts.micromips);
7915       jals = 1;
7916       /* Fall through.  */
7917     case M_JAL_A:
7918       if (mips_pic == NO_PIC)
7919         macro_build (&offset_expr, jals ? "jals" : "jal", "a");
7920       else if (mips_pic == SVR4_PIC)
7921         {
7922           /* If this is a reference to an external symbol, and we are
7923              using a small GOT, we want
7924                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
7925                nop
7926                jalr     $ra,$25
7927                nop
7928                lw       $gp,cprestore($sp)
7929              The cprestore value is set using the .cprestore
7930              pseudo-op.  If we are using a big GOT, we want
7931                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
7932                addu     $25,$25,$gp
7933                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
7934                nop
7935                jalr     $ra,$25
7936                nop
7937                lw       $gp,cprestore($sp)
7938              If the symbol is not external, we want
7939                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7940                nop
7941                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
7942                jalr     $ra,$25
7943                nop
7944                lw $gp,cprestore($sp)
7945
7946              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7947              sequences above, minus nops, unless the symbol is local,
7948              which enables us to use GOT_PAGE/GOT_OFST (big got) or
7949              GOT_DISP.  */
7950           if (HAVE_NEWABI)
7951             {
7952               if (!mips_big_got)
7953                 {
7954                   relax_start (offset_expr.X_add_symbol);
7955                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7956                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7957                                mips_gp_register);
7958                   relax_switch ();
7959                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7960                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
7961                                mips_gp_register);
7962                   relax_end ();
7963                 }
7964               else
7965                 {
7966                   relax_start (offset_expr.X_add_symbol);
7967                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
7968                                BFD_RELOC_MIPS_CALL_HI16);
7969                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7970                                PIC_CALL_REG, mips_gp_register);
7971                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7972                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7973                                PIC_CALL_REG);
7974                   relax_switch ();
7975                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7976                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7977                                mips_gp_register);
7978                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7979                                PIC_CALL_REG, PIC_CALL_REG,
7980                                BFD_RELOC_MIPS_GOT_OFST);
7981                   relax_end ();
7982                 }
7983
7984               macro_build_jalr (&offset_expr, 0);
7985             }
7986           else
7987             {
7988               relax_start (offset_expr.X_add_symbol);
7989               if (!mips_big_got)
7990                 {
7991                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7992                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7993                                mips_gp_register);
7994                   load_delay_nop ();
7995                   relax_switch ();
7996                 }
7997               else
7998                 {
7999                   int gpdelay;
8000
8001                   gpdelay = reg_needs_delay (mips_gp_register);
8002                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
8003                                BFD_RELOC_MIPS_CALL_HI16);
8004                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8005                                PIC_CALL_REG, mips_gp_register);
8006                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8007                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8008                                PIC_CALL_REG);
8009                   load_delay_nop ();
8010                   relax_switch ();
8011                   if (gpdelay)
8012                     macro_build (NULL, "nop", "");
8013                 }
8014               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8015                            PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
8016                            mips_gp_register);
8017               load_delay_nop ();
8018               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8019                            PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
8020               relax_end ();
8021               macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
8022
8023               if (mips_cprestore_offset < 0)
8024                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8025               else
8026                 {
8027                   if (!mips_frame_reg_valid)
8028                     {
8029                       as_warn (_("No .frame pseudo-op used in PIC code"));
8030                       /* Quiet this warning.  */
8031                       mips_frame_reg_valid = 1;
8032                     }
8033                   if (!mips_cprestore_valid)
8034                     {
8035                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
8036                       /* Quiet this warning.  */
8037                       mips_cprestore_valid = 1;
8038                     }
8039                   if (mips_opts.noreorder)
8040                     macro_build (NULL, "nop", "");
8041                   expr1.X_add_number = mips_cprestore_offset;
8042                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
8043                                                 mips_gp_register,
8044                                                 mips_frame_reg,
8045                                                 HAVE_64BIT_ADDRESSES);
8046                 }
8047             }
8048         }
8049       else if (mips_pic == VXWORKS_PIC)
8050         as_bad (_("Non-PIC jump used in PIC library"));
8051       else
8052         abort ();
8053
8054       break;
8055
8056     case M_ACLR_AB:
8057       ab = 1;
8058     case M_ACLR_OB:
8059       s = "aclr";
8060       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8061       fmt = "\\,~(b)";
8062       off12 = 1;
8063       goto ld_st;
8064     case M_ASET_AB:
8065       ab = 1;
8066     case M_ASET_OB:
8067       s = "aset";
8068       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8069       fmt = "\\,~(b)";
8070       off12 = 1;
8071       goto ld_st;
8072     case M_LB_AB:
8073       ab = 1;
8074       s = "lb";
8075       fmt = "t,o(b)";
8076       goto ld;
8077     case M_LBU_AB:
8078       ab = 1;
8079       s = "lbu";
8080       fmt = "t,o(b)";
8081       goto ld;
8082     case M_LH_AB:
8083       ab = 1;
8084       s = "lh";
8085       fmt = "t,o(b)";
8086       goto ld;
8087     case M_LHU_AB:
8088       ab = 1;
8089       s = "lhu";
8090       fmt = "t,o(b)";
8091       goto ld;
8092     case M_LW_AB:
8093       ab = 1;
8094       s = "lw";
8095       fmt = "t,o(b)";
8096       goto ld;
8097     case M_LWC0_AB:
8098       ab = 1;
8099       gas_assert (!mips_opts.micromips);
8100       s = "lwc0";
8101       fmt = "E,o(b)";
8102       /* Itbl support may require additional care here.  */
8103       coproc = 1;
8104       goto ld_st;
8105     case M_LWC1_AB:
8106       ab = 1;
8107       s = "lwc1";
8108       fmt = "T,o(b)";
8109       /* Itbl support may require additional care here.  */
8110       coproc = 1;
8111       goto ld_st;
8112     case M_LWC2_AB:
8113       ab = 1;
8114     case M_LWC2_OB:
8115       s = "lwc2";
8116       fmt = COP12_FMT;
8117       off12 = mips_opts.micromips;
8118       /* Itbl support may require additional care here.  */
8119       coproc = 1;
8120       goto ld_st;
8121     case M_LWC3_AB:
8122       ab = 1;
8123       gas_assert (!mips_opts.micromips);
8124       s = "lwc3";
8125       fmt = "E,o(b)";
8126       /* Itbl support may require additional care here.  */
8127       coproc = 1;
8128       goto ld_st;
8129     case M_LWL_AB:
8130       ab = 1;
8131     case M_LWL_OB:
8132       s = "lwl";
8133       fmt = MEM12_FMT;
8134       off12 = mips_opts.micromips;
8135       goto ld_st;
8136     case M_LWR_AB:
8137       ab = 1;
8138     case M_LWR_OB:
8139       s = "lwr";
8140       fmt = MEM12_FMT;
8141       off12 = mips_opts.micromips;
8142       goto ld_st;
8143     case M_LDC1_AB:
8144       ab = 1;
8145       s = "ldc1";
8146       fmt = "T,o(b)";
8147       /* Itbl support may require additional care here.  */
8148       coproc = 1;
8149       goto ld_st;
8150     case M_LDC2_AB:
8151       ab = 1;
8152     case M_LDC2_OB:
8153       s = "ldc2";
8154       fmt = COP12_FMT;
8155       off12 = mips_opts.micromips;
8156       /* Itbl support may require additional care here.  */
8157       coproc = 1;
8158       goto ld_st;
8159     case M_LDC3_AB:
8160       ab = 1;
8161       s = "ldc3";
8162       fmt = "E,o(b)";
8163       /* Itbl support may require additional care here.  */
8164       coproc = 1;
8165       goto ld_st;
8166     case M_LDL_AB:
8167       ab = 1;
8168     case M_LDL_OB:
8169       s = "ldl";
8170       fmt = MEM12_FMT;
8171       off12 = mips_opts.micromips;
8172       goto ld_st;
8173     case M_LDR_AB:
8174       ab = 1;
8175     case M_LDR_OB:
8176       s = "ldr";
8177       fmt = MEM12_FMT;
8178       off12 = mips_opts.micromips;
8179       goto ld_st;
8180     case M_LL_AB:
8181       ab = 1;
8182     case M_LL_OB:
8183       s = "ll";
8184       fmt = MEM12_FMT;
8185       off12 = mips_opts.micromips;
8186       goto ld;
8187     case M_LLD_AB:
8188       ab = 1;
8189     case M_LLD_OB:
8190       s = "lld";
8191       fmt = MEM12_FMT;
8192       off12 = mips_opts.micromips;
8193       goto ld;
8194     case M_LWU_AB:
8195       ab = 1;
8196     case M_LWU_OB:
8197       s = "lwu";
8198       fmt = MEM12_FMT;
8199       off12 = mips_opts.micromips;
8200       goto ld;
8201     case M_LWP_AB:
8202       ab = 1;
8203     case M_LWP_OB:
8204       gas_assert (mips_opts.micromips);
8205       s = "lwp";
8206       fmt = "t,~(b)";
8207       off12 = 1;
8208       lp = 1;
8209       goto ld;
8210     case M_LDP_AB:
8211       ab = 1;
8212     case M_LDP_OB:
8213       gas_assert (mips_opts.micromips);
8214       s = "ldp";
8215       fmt = "t,~(b)";
8216       off12 = 1;
8217       lp = 1;
8218       goto ld;
8219     case M_LWM_AB:
8220       ab = 1;
8221     case M_LWM_OB:
8222       gas_assert (mips_opts.micromips);
8223       s = "lwm";
8224       fmt = "n,~(b)";
8225       off12 = 1;
8226       goto ld_st;
8227     case M_LDM_AB:
8228       ab = 1;
8229     case M_LDM_OB:
8230       gas_assert (mips_opts.micromips);
8231       s = "ldm";
8232       fmt = "n,~(b)";
8233       off12 = 1;
8234       goto ld_st;
8235
8236     ld:
8237       if (breg == treg + lp)
8238         goto ld_st;
8239       else
8240         tempreg = treg + lp;
8241       goto ld_noat;
8242
8243     case M_SB_AB:
8244       ab = 1;
8245       s = "sb";
8246       fmt = "t,o(b)";
8247       goto ld_st;
8248     case M_SH_AB:
8249       ab = 1;
8250       s = "sh";
8251       fmt = "t,o(b)";
8252       goto ld_st;
8253     case M_SW_AB:
8254       ab = 1;
8255       s = "sw";
8256       fmt = "t,o(b)";
8257       goto ld_st;
8258     case M_SWC0_AB:
8259       ab = 1;
8260       gas_assert (!mips_opts.micromips);
8261       s = "swc0";
8262       fmt = "E,o(b)";
8263       /* Itbl support may require additional care here.  */
8264       coproc = 1;
8265       goto ld_st;
8266     case M_SWC1_AB:
8267       ab = 1;
8268       s = "swc1";
8269       fmt = "T,o(b)";
8270       /* Itbl support may require additional care here.  */
8271       coproc = 1;
8272       goto ld_st;
8273     case M_SWC2_AB:
8274       ab = 1;
8275     case M_SWC2_OB:
8276       s = "swc2";
8277       fmt = COP12_FMT;
8278       off12 = mips_opts.micromips;
8279       /* Itbl support may require additional care here.  */
8280       coproc = 1;
8281       goto ld_st;
8282     case M_SWC3_AB:
8283       ab = 1;
8284       gas_assert (!mips_opts.micromips);
8285       s = "swc3";
8286       fmt = "E,o(b)";
8287       /* Itbl support may require additional care here.  */
8288       coproc = 1;
8289       goto ld_st;
8290     case M_SWL_AB:
8291       ab = 1;
8292     case M_SWL_OB:
8293       s = "swl";
8294       fmt = MEM12_FMT;
8295       off12 = mips_opts.micromips;
8296       goto ld_st;
8297     case M_SWR_AB:
8298       ab = 1;
8299     case M_SWR_OB:
8300       s = "swr";
8301       fmt = MEM12_FMT;
8302       off12 = mips_opts.micromips;
8303       goto ld_st;
8304     case M_SC_AB:
8305       ab = 1;
8306     case M_SC_OB:
8307       s = "sc";
8308       fmt = MEM12_FMT;
8309       off12 = mips_opts.micromips;
8310       goto ld_st;
8311     case M_SCD_AB:
8312       ab = 1;
8313     case M_SCD_OB:
8314       s = "scd";
8315       fmt = MEM12_FMT;
8316       off12 = mips_opts.micromips;
8317       goto ld_st;
8318     case M_CACHE_AB:
8319       ab = 1;
8320     case M_CACHE_OB:
8321       s = "cache";
8322       fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8323       off12 = mips_opts.micromips;
8324       goto ld_st;
8325     case M_PREF_AB:
8326       ab = 1;
8327     case M_PREF_OB:
8328       s = "pref";
8329       fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8330       off12 = mips_opts.micromips;
8331       goto ld_st;
8332     case M_SDC1_AB:
8333       ab = 1;
8334       s = "sdc1";
8335       fmt = "T,o(b)";
8336       coproc = 1;
8337       /* Itbl support may require additional care here.  */
8338       goto ld_st;
8339     case M_SDC2_AB:
8340       ab = 1;
8341     case M_SDC2_OB:
8342       s = "sdc2";
8343       fmt = COP12_FMT;
8344       off12 = mips_opts.micromips;
8345       /* Itbl support may require additional care here.  */
8346       coproc = 1;
8347       goto ld_st;
8348     case M_SDC3_AB:
8349       ab = 1;
8350       gas_assert (!mips_opts.micromips);
8351       s = "sdc3";
8352       fmt = "E,o(b)";
8353       /* Itbl support may require additional care here.  */
8354       coproc = 1;
8355       goto ld_st;
8356     case M_SDL_AB:
8357       ab = 1;
8358     case M_SDL_OB:
8359       s = "sdl";
8360       fmt = MEM12_FMT;
8361       off12 = mips_opts.micromips;
8362       goto ld_st;
8363     case M_SDR_AB:
8364       ab = 1;
8365     case M_SDR_OB:
8366       s = "sdr";
8367       fmt = MEM12_FMT;
8368       off12 = mips_opts.micromips;
8369       goto ld_st;
8370     case M_SWP_AB:
8371       ab = 1;
8372     case M_SWP_OB:
8373       gas_assert (mips_opts.micromips);
8374       s = "swp";
8375       fmt = "t,~(b)";
8376       off12 = 1;
8377       goto ld_st;
8378     case M_SDP_AB:
8379       ab = 1;
8380     case M_SDP_OB:
8381       gas_assert (mips_opts.micromips);
8382       s = "sdp";
8383       fmt = "t,~(b)";
8384       off12 = 1;
8385       goto ld_st;
8386     case M_SWM_AB:
8387       ab = 1;
8388     case M_SWM_OB:
8389       gas_assert (mips_opts.micromips);
8390       s = "swm";
8391       fmt = "n,~(b)";
8392       off12 = 1;
8393       goto ld_st;
8394     case M_SDM_AB:
8395       ab = 1;
8396     case M_SDM_OB:
8397       gas_assert (mips_opts.micromips);
8398       s = "sdm";
8399       fmt = "n,~(b)";
8400       off12 = 1;
8401
8402     ld_st:
8403       tempreg = AT;
8404       used_at = 1;
8405     ld_noat:
8406       if (offset_expr.X_op != O_constant
8407           && offset_expr.X_op != O_symbol)
8408         {
8409           as_bad (_("Expression too complex"));
8410           offset_expr.X_op = O_constant;
8411         }
8412
8413       if (HAVE_32BIT_ADDRESSES
8414           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8415         {
8416           char value [32];
8417
8418           sprintf_vma (value, offset_expr.X_add_number);
8419           as_bad (_("Number (0x%s) larger than 32 bits"), value);
8420         }
8421
8422       /* A constant expression in PIC code can be handled just as it
8423          is in non PIC code.  */
8424       if (offset_expr.X_op == O_constant)
8425         {
8426           int hipart = 0;
8427
8428           expr1.X_add_number = offset_expr.X_add_number;
8429           normalize_address_expr (&expr1);
8430           if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
8431             {
8432               expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8433                                     & ~(bfd_vma) 0xffff);
8434               hipart = 1;
8435             }
8436           else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8437             {
8438               expr1.X_add_number = ((expr1.X_add_number + 0x800)
8439                                     & ~(bfd_vma) 0xfff);
8440               hipart = 1;
8441             }
8442           if (hipart)
8443             {
8444               load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8445               if (breg != 0)
8446                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8447                              tempreg, tempreg, breg);
8448               breg = tempreg;
8449             }
8450           if (off0)
8451             {
8452               if (offset_expr.X_add_number == 0)
8453                 tempreg = breg;
8454               else
8455                 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8456                              "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8457               macro_build (NULL, s, fmt, treg, tempreg);
8458             }
8459           else if (!off12)
8460             macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8461           else
8462             macro_build (NULL, s, fmt,
8463                          treg, (unsigned long) offset_expr.X_add_number, breg);
8464         }
8465       else if (off12 || off0)
8466         {
8467           /* A 12-bit or 0-bit offset field is too narrow to be used
8468              for a low-part relocation, so load the whole address into
8469              the auxillary register.  In the case of "A(b)" addresses,
8470              we first load absolute address "A" into the register and
8471              then add base register "b".  In the case of "o(b)" addresses,
8472              we simply need to add 16-bit offset "o" to base register "b", and
8473              offset_reloc already contains the relocations associated
8474              with "o".  */
8475           if (ab)
8476             {
8477               load_address (tempreg, &offset_expr, &used_at);
8478               if (breg != 0)
8479                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8480                              tempreg, tempreg, breg);
8481             }
8482           else
8483             macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8484                          tempreg, breg, -1,
8485                          offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8486           expr1.X_add_number = 0;
8487           if (off0)
8488             macro_build (NULL, s, fmt, treg, tempreg);
8489           else
8490             macro_build (NULL, s, fmt,
8491                          treg, (unsigned long) expr1.X_add_number, tempreg);
8492         }
8493       else if (mips_pic == NO_PIC)
8494         {
8495           /* If this is a reference to a GP relative symbol, and there
8496              is no base register, we want
8497                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
8498              Otherwise, if there is no base register, we want
8499                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8500                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8501              If we have a constant, we need two instructions anyhow,
8502              so we always use the latter form.
8503
8504              If we have a base register, and this is a reference to a
8505              GP relative symbol, we want
8506                addu     $tempreg,$breg,$gp
8507                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
8508              Otherwise we want
8509                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8510                addu     $tempreg,$tempreg,$breg
8511                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8512              With a constant we always use the latter case.
8513
8514              With 64bit address space and no base register and $at usable,
8515              we want
8516                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8517                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8518                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8519                dsll32   $tempreg,0
8520                daddu    $tempreg,$at
8521                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8522              If we have a base register, we want
8523                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8524                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8525                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8526                daddu    $at,$breg
8527                dsll32   $tempreg,0
8528                daddu    $tempreg,$at
8529                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8530
8531              Without $at we can't generate the optimal path for superscalar
8532              processors here since this would require two temporary registers.
8533                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8534                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8535                dsll     $tempreg,16
8536                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8537                dsll     $tempreg,16
8538                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8539              If we have a base register, we want
8540                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8541                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8542                dsll     $tempreg,16
8543                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8544                dsll     $tempreg,16
8545                daddu    $tempreg,$tempreg,$breg
8546                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8547
8548              For GP relative symbols in 64bit address space we can use
8549              the same sequence as in 32bit address space.  */
8550           if (HAVE_64BIT_SYMBOLS)
8551             {
8552               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8553                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8554                 {
8555                   relax_start (offset_expr.X_add_symbol);
8556                   if (breg == 0)
8557                     {
8558                       macro_build (&offset_expr, s, fmt, treg,
8559                                    BFD_RELOC_GPREL16, mips_gp_register);
8560                     }
8561                   else
8562                     {
8563                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8564                                    tempreg, breg, mips_gp_register);
8565                       macro_build (&offset_expr, s, fmt, treg,
8566                                    BFD_RELOC_GPREL16, tempreg);
8567                     }
8568                   relax_switch ();
8569                 }
8570
8571               if (used_at == 0 && mips_opts.at)
8572                 {
8573                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8574                                BFD_RELOC_MIPS_HIGHEST);
8575                   macro_build (&offset_expr, "lui", LUI_FMT, AT,
8576                                BFD_RELOC_HI16_S);
8577                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8578                                tempreg, BFD_RELOC_MIPS_HIGHER);
8579                   if (breg != 0)
8580                     macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
8581                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8582                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8583                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8584                                tempreg);
8585                   used_at = 1;
8586                 }
8587               else
8588                 {
8589                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8590                                BFD_RELOC_MIPS_HIGHEST);
8591                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8592                                tempreg, BFD_RELOC_MIPS_HIGHER);
8593                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8594                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8595                                tempreg, BFD_RELOC_HI16_S);
8596                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8597                   if (breg != 0)
8598                     macro_build (NULL, "daddu", "d,v,t",
8599                                  tempreg, tempreg, breg);
8600                   macro_build (&offset_expr, s, fmt, treg,
8601                                BFD_RELOC_LO16, tempreg);
8602                 }
8603
8604               if (mips_relax.sequence)
8605                 relax_end ();
8606               break;
8607             }
8608
8609           if (breg == 0)
8610             {
8611               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8612                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8613                 {
8614                   relax_start (offset_expr.X_add_symbol);
8615                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8616                                mips_gp_register);
8617                   relax_switch ();
8618                 }
8619               macro_build_lui (&offset_expr, tempreg);
8620               macro_build (&offset_expr, s, fmt, treg,
8621                            BFD_RELOC_LO16, tempreg);
8622               if (mips_relax.sequence)
8623                 relax_end ();
8624             }
8625           else
8626             {
8627               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8628                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8629                 {
8630                   relax_start (offset_expr.X_add_symbol);
8631                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8632                                tempreg, breg, mips_gp_register);
8633                   macro_build (&offset_expr, s, fmt, treg,
8634                                BFD_RELOC_GPREL16, tempreg);
8635                   relax_switch ();
8636                 }
8637               macro_build_lui (&offset_expr, tempreg);
8638               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8639                            tempreg, tempreg, breg);
8640               macro_build (&offset_expr, s, fmt, treg,
8641                            BFD_RELOC_LO16, tempreg);
8642               if (mips_relax.sequence)
8643                 relax_end ();
8644             }
8645         }
8646       else if (!mips_big_got)
8647         {
8648           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
8649
8650           /* If this is a reference to an external symbol, we want
8651                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8652                nop
8653                <op>     $treg,0($tempreg)
8654              Otherwise we want
8655                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8656                nop
8657                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8658                <op>     $treg,0($tempreg)
8659
8660              For NewABI, we want
8661                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
8662                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
8663
8664              If there is a base register, we add it to $tempreg before
8665              the <op>.  If there is a constant, we stick it in the
8666              <op> instruction.  We don't handle constants larger than
8667              16 bits, because we have no way to load the upper 16 bits
8668              (actually, we could handle them for the subset of cases
8669              in which we are not using $at).  */
8670           gas_assert (offset_expr.X_op == O_symbol);
8671           if (HAVE_NEWABI)
8672             {
8673               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8674                            BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8675               if (breg != 0)
8676                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8677                              tempreg, tempreg, breg);
8678               macro_build (&offset_expr, s, fmt, treg,
8679                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
8680               break;
8681             }
8682           expr1.X_add_number = offset_expr.X_add_number;
8683           offset_expr.X_add_number = 0;
8684           if (expr1.X_add_number < -0x8000
8685               || expr1.X_add_number >= 0x8000)
8686             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8687           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8688                        lw_reloc_type, mips_gp_register);
8689           load_delay_nop ();
8690           relax_start (offset_expr.X_add_symbol);
8691           relax_switch ();
8692           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8693                        tempreg, BFD_RELOC_LO16);
8694           relax_end ();
8695           if (breg != 0)
8696             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8697                          tempreg, tempreg, breg);
8698           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8699         }
8700       else if (mips_big_got && !HAVE_NEWABI)
8701         {
8702           int gpdelay;
8703
8704           /* If this is a reference to an external symbol, we want
8705                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8706                addu     $tempreg,$tempreg,$gp
8707                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8708                <op>     $treg,0($tempreg)
8709              Otherwise we want
8710                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8711                nop
8712                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8713                <op>     $treg,0($tempreg)
8714              If there is a base register, we add it to $tempreg before
8715              the <op>.  If there is a constant, we stick it in the
8716              <op> instruction.  We don't handle constants larger than
8717              16 bits, because we have no way to load the upper 16 bits
8718              (actually, we could handle them for the subset of cases
8719              in which we are not using $at).  */
8720           gas_assert (offset_expr.X_op == O_symbol);
8721           expr1.X_add_number = offset_expr.X_add_number;
8722           offset_expr.X_add_number = 0;
8723           if (expr1.X_add_number < -0x8000
8724               || expr1.X_add_number >= 0x8000)
8725             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8726           gpdelay = reg_needs_delay (mips_gp_register);
8727           relax_start (offset_expr.X_add_symbol);
8728           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8729                        BFD_RELOC_MIPS_GOT_HI16);
8730           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8731                        mips_gp_register);
8732           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8733                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
8734           relax_switch ();
8735           if (gpdelay)
8736             macro_build (NULL, "nop", "");
8737           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8738                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
8739           load_delay_nop ();
8740           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8741                        tempreg, BFD_RELOC_LO16);
8742           relax_end ();
8743
8744           if (breg != 0)
8745             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8746                          tempreg, tempreg, breg);
8747           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8748         }
8749       else if (mips_big_got && HAVE_NEWABI)
8750         {
8751           /* If this is a reference to an external symbol, we want
8752                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8753                add      $tempreg,$tempreg,$gp
8754                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8755                <op>     $treg,<ofst>($tempreg)
8756              Otherwise, for local symbols, we want:
8757                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
8758                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
8759           gas_assert (offset_expr.X_op == O_symbol);
8760           expr1.X_add_number = offset_expr.X_add_number;
8761           offset_expr.X_add_number = 0;
8762           if (expr1.X_add_number < -0x8000
8763               || expr1.X_add_number >= 0x8000)
8764             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8765           relax_start (offset_expr.X_add_symbol);
8766           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8767                        BFD_RELOC_MIPS_GOT_HI16);
8768           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8769                        mips_gp_register);
8770           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8771                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
8772           if (breg != 0)
8773             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8774                          tempreg, tempreg, breg);
8775           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8776
8777           relax_switch ();
8778           offset_expr.X_add_number = expr1.X_add_number;
8779           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8780                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8781           if (breg != 0)
8782             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8783                          tempreg, tempreg, breg);
8784           macro_build (&offset_expr, s, fmt, treg,
8785                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
8786           relax_end ();
8787         }
8788       else
8789         abort ();
8790
8791       break;
8792
8793     case M_LI:
8794     case M_LI_S:
8795       load_register (treg, &imm_expr, 0);
8796       break;
8797
8798     case M_DLI:
8799       load_register (treg, &imm_expr, 1);
8800       break;
8801
8802     case M_LI_SS:
8803       if (imm_expr.X_op == O_constant)
8804         {
8805           used_at = 1;
8806           load_register (AT, &imm_expr, 0);
8807           macro_build (NULL, "mtc1", "t,G", AT, treg);
8808           break;
8809         }
8810       else
8811         {
8812           gas_assert (offset_expr.X_op == O_symbol
8813                       && strcmp (segment_name (S_GET_SEGMENT
8814                                                (offset_expr.X_add_symbol)),
8815                                  ".lit4") == 0
8816                       && offset_expr.X_add_number == 0);
8817           macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
8818                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8819           break;
8820         }
8821
8822     case M_LI_D:
8823       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
8824          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
8825          order 32 bits of the value and the low order 32 bits are either
8826          zero or in OFFSET_EXPR.  */
8827       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8828         {
8829           if (HAVE_64BIT_GPRS)
8830             load_register (treg, &imm_expr, 1);
8831           else
8832             {
8833               int hreg, lreg;
8834
8835               if (target_big_endian)
8836                 {
8837                   hreg = treg;
8838                   lreg = treg + 1;
8839                 }
8840               else
8841                 {
8842                   hreg = treg + 1;
8843                   lreg = treg;
8844                 }
8845
8846               if (hreg <= 31)
8847                 load_register (hreg, &imm_expr, 0);
8848               if (lreg <= 31)
8849                 {
8850                   if (offset_expr.X_op == O_absent)
8851                     move_register (lreg, 0);
8852                   else
8853                     {
8854                       gas_assert (offset_expr.X_op == O_constant);
8855                       load_register (lreg, &offset_expr, 0);
8856                     }
8857                 }
8858             }
8859           break;
8860         }
8861
8862       /* We know that sym is in the .rdata section.  First we get the
8863          upper 16 bits of the address.  */
8864       if (mips_pic == NO_PIC)
8865         {
8866           macro_build_lui (&offset_expr, AT);
8867           used_at = 1;
8868         }
8869       else
8870         {
8871           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8872                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
8873           used_at = 1;
8874         }
8875
8876       /* Now we load the register(s).  */
8877       if (HAVE_64BIT_GPRS)
8878         {
8879           used_at = 1;
8880           macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8881         }
8882       else
8883         {
8884           used_at = 1;
8885           macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8886           if (treg != RA)
8887             {
8888               /* FIXME: How in the world do we deal with the possible
8889                  overflow here?  */
8890               offset_expr.X_add_number += 4;
8891               macro_build (&offset_expr, "lw", "t,o(b)",
8892                            treg + 1, BFD_RELOC_LO16, AT);
8893             }
8894         }
8895       break;
8896
8897     case M_LI_DD:
8898       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
8899          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8900          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
8901          the value and the low order 32 bits are either zero or in
8902          OFFSET_EXPR.  */
8903       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8904         {
8905           used_at = 1;
8906           load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
8907           if (HAVE_64BIT_FPRS)
8908             {
8909               gas_assert (HAVE_64BIT_GPRS);
8910               macro_build (NULL, "dmtc1", "t,S", AT, treg);
8911             }
8912           else
8913             {
8914               macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
8915               if (offset_expr.X_op == O_absent)
8916                 macro_build (NULL, "mtc1", "t,G", 0, treg);
8917               else
8918                 {
8919                   gas_assert (offset_expr.X_op == O_constant);
8920                   load_register (AT, &offset_expr, 0);
8921                   macro_build (NULL, "mtc1", "t,G", AT, treg);
8922                 }
8923             }
8924           break;
8925         }
8926
8927       gas_assert (offset_expr.X_op == O_symbol
8928                   && offset_expr.X_add_number == 0);
8929       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8930       if (strcmp (s, ".lit8") == 0)
8931         {
8932           if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
8933             {
8934               macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
8935                            BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8936               break;
8937             }
8938           breg = mips_gp_register;
8939           r = BFD_RELOC_MIPS_LITERAL;
8940           goto dob;
8941         }
8942       else
8943         {
8944           gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8945           used_at = 1;
8946           if (mips_pic != NO_PIC)
8947             macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8948                          BFD_RELOC_MIPS_GOT16, mips_gp_register);
8949           else
8950             {
8951               /* FIXME: This won't work for a 64 bit address.  */
8952               macro_build_lui (&offset_expr, AT);
8953             }
8954
8955           if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
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 (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
8973       macro_build (&offset_expr, "lwc1", "T,o(b)",
8974                    target_big_endian ? treg + 1 : treg, r, breg);
8975       /* FIXME: A possible overflow which I don't know how to deal
8976          with.  */
8977       offset_expr.X_add_number += 4;
8978       macro_build (&offset_expr, "lwc1", "T,o(b)",
8979                    target_big_endian ? treg : treg + 1, r, breg);
8980       break;
8981
8982     case M_S_DOB:
8983       gas_assert (!mips_opts.micromips);
8984       gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
8985       /* Even on a big endian machine $fn comes before $fn+1.  We have
8986          to adjust when storing to memory.  */
8987       macro_build (&offset_expr, "swc1", "T,o(b)",
8988                    target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8989       offset_expr.X_add_number += 4;
8990       macro_build (&offset_expr, "swc1", "T,o(b)",
8991                    target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8992       break;
8993
8994     case M_L_DAB:
8995       gas_assert (!mips_opts.micromips);
8996       /*
8997        * The MIPS assembler seems to check for X_add_number not
8998        * being double aligned and generating:
8999        *        lui     at,%hi(foo+1)
9000        *        addu    at,at,v1
9001        *        addiu   at,at,%lo(foo+1)
9002        *        lwc1    f2,0(at)
9003        *        lwc1    f3,4(at)
9004        * But, the resulting address is the same after relocation so why
9005        * generate the extra instruction?
9006        */
9007       /* Itbl support may require additional care here.  */
9008       coproc = 1;
9009       fmt = "T,o(b)";
9010       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9011         {
9012           s = "ldc1";
9013           goto ld_st;
9014         }
9015       s = "lwc1";
9016       goto ldd_std;
9017
9018     case M_S_DAB:
9019       gas_assert (!mips_opts.micromips);
9020       /* Itbl support may require additional care here.  */
9021       coproc = 1;
9022       fmt = "T,o(b)";
9023       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
9024         {
9025           s = "sdc1";
9026           goto ld_st;
9027         }
9028       s = "swc1";
9029       goto ldd_std;
9030
9031     case M_LQ_AB:
9032       fmt = "t,o(b)";
9033       s = "lq";
9034       goto ld;
9035
9036     case M_SQ_AB:
9037       fmt = "t,o(b)";
9038       s = "sq";
9039       goto ld_st;
9040
9041     case M_LD_AB:
9042       fmt = "t,o(b)";
9043       if (HAVE_64BIT_GPRS)
9044         {
9045           s = "ld";
9046           goto ld;
9047         }
9048       s = "lw";
9049       goto ldd_std;
9050
9051     case M_SD_AB:
9052       fmt = "t,o(b)";
9053       if (HAVE_64BIT_GPRS)
9054         {
9055           s = "sd";
9056           goto ld_st;
9057         }
9058       s = "sw";
9059
9060     ldd_std:
9061       if (offset_expr.X_op != O_symbol
9062           && offset_expr.X_op != O_constant)
9063         {
9064           as_bad (_("Expression too complex"));
9065           offset_expr.X_op = O_constant;
9066         }
9067
9068       if (HAVE_32BIT_ADDRESSES
9069           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
9070         {
9071           char value [32];
9072
9073           sprintf_vma (value, offset_expr.X_add_number);
9074           as_bad (_("Number (0x%s) larger than 32 bits"), value);
9075         }
9076
9077       /* Even on a big endian machine $fn comes before $fn+1.  We have
9078          to adjust when loading from memory.  We set coproc if we must
9079          load $fn+1 first.  */
9080       /* Itbl support may require additional care here.  */
9081       if (!target_big_endian)
9082         coproc = 0;
9083
9084       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
9085         {
9086           /* If this is a reference to a GP relative symbol, we want
9087                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
9088                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
9089              If we have a base register, we use this
9090                addu     $at,$breg,$gp
9091                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
9092                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
9093              If this is not a GP relative symbol, we want
9094                lui      $at,<sym>               (BFD_RELOC_HI16_S)
9095                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9096                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9097              If there is a base register, we add it to $at after the
9098              lui instruction.  If there is a constant, we always use
9099              the last case.  */
9100           if (offset_expr.X_op == O_symbol
9101               && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9102               && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9103             {
9104               relax_start (offset_expr.X_add_symbol);
9105               if (breg == 0)
9106                 {
9107                   tempreg = mips_gp_register;
9108                 }
9109               else
9110                 {
9111                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9112                                AT, breg, mips_gp_register);
9113                   tempreg = AT;
9114                   used_at = 1;
9115                 }
9116
9117               /* Itbl support may require additional care here.  */
9118               macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9119                            BFD_RELOC_GPREL16, tempreg);
9120               offset_expr.X_add_number += 4;
9121
9122               /* Set mips_optimize to 2 to avoid inserting an
9123                  undesired nop.  */
9124               hold_mips_optimize = mips_optimize;
9125               mips_optimize = 2;
9126               /* Itbl support may require additional care here.  */
9127               macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9128                            BFD_RELOC_GPREL16, tempreg);
9129               mips_optimize = hold_mips_optimize;
9130
9131               relax_switch ();
9132
9133               offset_expr.X_add_number -= 4;
9134             }
9135           used_at = 1;
9136           macro_build_lui (&offset_expr, AT);
9137           if (breg != 0)
9138             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9139           /* Itbl support may require additional care here.  */
9140           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9141                        BFD_RELOC_LO16, AT);
9142           /* FIXME: How do we handle overflow here?  */
9143           offset_expr.X_add_number += 4;
9144           /* Itbl support may require additional care here.  */
9145           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9146                        BFD_RELOC_LO16, AT);
9147           if (mips_relax.sequence)
9148             relax_end ();
9149         }
9150       else if (!mips_big_got)
9151         {
9152           /* If this is a reference to an external symbol, we want
9153                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9154                nop
9155                <op>     $treg,0($at)
9156                <op>     $treg+1,4($at)
9157              Otherwise we want
9158                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9159                nop
9160                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9161                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9162              If there is a base register we add it to $at before the
9163              lwc1 instructions.  If there is a constant we include it
9164              in the lwc1 instructions.  */
9165           used_at = 1;
9166           expr1.X_add_number = offset_expr.X_add_number;
9167           if (expr1.X_add_number < -0x8000
9168               || expr1.X_add_number >= 0x8000 - 4)
9169             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9170           load_got_offset (AT, &offset_expr);
9171           load_delay_nop ();
9172           if (breg != 0)
9173             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9174
9175           /* Set mips_optimize to 2 to avoid inserting an undesired
9176              nop.  */
9177           hold_mips_optimize = mips_optimize;
9178           mips_optimize = 2;
9179
9180           /* Itbl support may require additional care here.  */
9181           relax_start (offset_expr.X_add_symbol);
9182           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9183                        BFD_RELOC_LO16, AT);
9184           expr1.X_add_number += 4;
9185           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9186                        BFD_RELOC_LO16, AT);
9187           relax_switch ();
9188           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9189                        BFD_RELOC_LO16, AT);
9190           offset_expr.X_add_number += 4;
9191           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9192                        BFD_RELOC_LO16, AT);
9193           relax_end ();
9194
9195           mips_optimize = hold_mips_optimize;
9196         }
9197       else if (mips_big_got)
9198         {
9199           int gpdelay;
9200
9201           /* If this is a reference to an external symbol, we want
9202                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
9203                addu     $at,$at,$gp
9204                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
9205                nop
9206                <op>     $treg,0($at)
9207                <op>     $treg+1,4($at)
9208              Otherwise we want
9209                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9210                nop
9211                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9212                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9213              If there is a base register we add it to $at before the
9214              lwc1 instructions.  If there is a constant we include it
9215              in the lwc1 instructions.  */
9216           used_at = 1;
9217           expr1.X_add_number = offset_expr.X_add_number;
9218           offset_expr.X_add_number = 0;
9219           if (expr1.X_add_number < -0x8000
9220               || expr1.X_add_number >= 0x8000 - 4)
9221             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9222           gpdelay = reg_needs_delay (mips_gp_register);
9223           relax_start (offset_expr.X_add_symbol);
9224           macro_build (&offset_expr, "lui", LUI_FMT,
9225                        AT, BFD_RELOC_MIPS_GOT_HI16);
9226           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9227                        AT, AT, mips_gp_register);
9228           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9229                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9230           load_delay_nop ();
9231           if (breg != 0)
9232             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9233           /* Itbl support may require additional care here.  */
9234           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9235                        BFD_RELOC_LO16, AT);
9236           expr1.X_add_number += 4;
9237
9238           /* Set mips_optimize to 2 to avoid inserting an undesired
9239              nop.  */
9240           hold_mips_optimize = mips_optimize;
9241           mips_optimize = 2;
9242           /* Itbl support may require additional care here.  */
9243           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9244                        BFD_RELOC_LO16, AT);
9245           mips_optimize = hold_mips_optimize;
9246           expr1.X_add_number -= 4;
9247
9248           relax_switch ();
9249           offset_expr.X_add_number = expr1.X_add_number;
9250           if (gpdelay)
9251             macro_build (NULL, "nop", "");
9252           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9253                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
9254           load_delay_nop ();
9255           if (breg != 0)
9256             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9257           /* Itbl support may require additional care here.  */
9258           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9259                        BFD_RELOC_LO16, AT);
9260           offset_expr.X_add_number += 4;
9261
9262           /* Set mips_optimize to 2 to avoid inserting an undesired
9263              nop.  */
9264           hold_mips_optimize = mips_optimize;
9265           mips_optimize = 2;
9266           /* Itbl support may require additional care here.  */
9267           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9268                        BFD_RELOC_LO16, AT);
9269           mips_optimize = hold_mips_optimize;
9270           relax_end ();
9271         }
9272       else
9273         abort ();
9274
9275       break;
9276
9277     case M_LD_OB:
9278       s = HAVE_64BIT_GPRS ? "ld" : "lw";
9279       goto sd_ob;
9280     case M_SD_OB:
9281       s = HAVE_64BIT_GPRS ? "sd" : "sw";
9282     sd_ob:
9283       macro_build (&offset_expr, s, "t,o(b)", treg,
9284                    -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9285                    breg);
9286       if (!HAVE_64BIT_GPRS)
9287         {
9288           offset_expr.X_add_number += 4;
9289           macro_build (&offset_expr, s, "t,o(b)", treg + 1,
9290                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9291                        breg);
9292         }
9293       break;
9294
9295         
9296     case M_SAA_AB:
9297       ab = 1;
9298     case M_SAA_OB:
9299       s = "saa";
9300       off0 = 1;
9301       fmt = "t,(b)";
9302       goto ld_st;
9303     case M_SAAD_AB:
9304       ab = 1;
9305     case M_SAAD_OB:
9306       s = "saad";
9307       off0 = 1;
9308       fmt = "t,(b)";
9309       goto ld_st;
9310
9311    /* New code added to support COPZ instructions.
9312       This code builds table entries out of the macros in mip_opcodes.
9313       R4000 uses interlocks to handle coproc delays.
9314       Other chips (like the R3000) require nops to be inserted for delays.
9315
9316       FIXME: Currently, we require that the user handle delays.
9317       In order to fill delay slots for non-interlocked chips,
9318       we must have a way to specify delays based on the coprocessor.
9319       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9320       What are the side-effects of the cop instruction?
9321       What cache support might we have and what are its effects?
9322       Both coprocessor & memory require delays. how long???
9323       What registers are read/set/modified?
9324
9325       If an itbl is provided to interpret cop instructions,
9326       this knowledge can be encoded in the itbl spec.  */
9327
9328     case M_COP0:
9329       s = "c0";
9330       goto copz;
9331     case M_COP1:
9332       s = "c1";
9333       goto copz;
9334     case M_COP2:
9335       s = "c2";
9336       goto copz;
9337     case M_COP3:
9338       s = "c3";
9339     copz:
9340       gas_assert (!mips_opts.micromips);
9341       /* For now we just do C (same as Cz).  The parameter will be
9342          stored in insn_opcode by mips_ip.  */
9343       macro_build (NULL, s, "C", ip->insn_opcode);
9344       break;
9345
9346     case M_MOVE:
9347       move_register (dreg, sreg);
9348       break;
9349
9350     case M_DMUL:
9351       dbl = 1;
9352     case M_MUL:
9353       if (mips_opts.arch == CPU_R5900)
9354         {
9355           macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9356         }
9357       else
9358         {
9359       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9360       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9361         }
9362       break;
9363
9364     case M_DMUL_I:
9365       dbl = 1;
9366     case M_MUL_I:
9367       /* The MIPS assembler some times generates shifts and adds.  I'm
9368          not trying to be that fancy. GCC should do this for us
9369          anyway.  */
9370       used_at = 1;
9371       load_register (AT, &imm_expr, dbl);
9372       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9373       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9374       break;
9375
9376     case M_DMULO_I:
9377       dbl = 1;
9378     case M_MULO_I:
9379       imm = 1;
9380       goto do_mulo;
9381
9382     case M_DMULO:
9383       dbl = 1;
9384     case M_MULO:
9385     do_mulo:
9386       start_noreorder ();
9387       used_at = 1;
9388       if (imm)
9389         load_register (AT, &imm_expr, dbl);
9390       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9391       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9392       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9393       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9394       if (mips_trap)
9395         macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9396       else
9397         {
9398           if (mips_opts.micromips)
9399             micromips_label_expr (&label_expr);
9400           else
9401             label_expr.X_add_number = 8;
9402           macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9403           macro_build (NULL, "nop", "");
9404           macro_build (NULL, "break", BRK_FMT, 6);
9405           if (mips_opts.micromips)
9406             micromips_add_label ();
9407         }
9408       end_noreorder ();
9409       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9410       break;
9411
9412     case M_DMULOU_I:
9413       dbl = 1;
9414     case M_MULOU_I:
9415       imm = 1;
9416       goto do_mulou;
9417
9418     case M_DMULOU:
9419       dbl = 1;
9420     case M_MULOU:
9421     do_mulou:
9422       start_noreorder ();
9423       used_at = 1;
9424       if (imm)
9425         load_register (AT, &imm_expr, dbl);
9426       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9427                    sreg, imm ? AT : treg);
9428       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9429       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9430       if (mips_trap)
9431         macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9432       else
9433         {
9434           if (mips_opts.micromips)
9435             micromips_label_expr (&label_expr);
9436           else
9437             label_expr.X_add_number = 8;
9438           macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9439           macro_build (NULL, "nop", "");
9440           macro_build (NULL, "break", BRK_FMT, 6);
9441           if (mips_opts.micromips)
9442             micromips_add_label ();
9443         }
9444       end_noreorder ();
9445       break;
9446
9447     case M_DROL:
9448       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9449         {
9450           if (dreg == sreg)
9451             {
9452               tempreg = AT;
9453               used_at = 1;
9454             }
9455           else
9456             {
9457               tempreg = dreg;
9458             }
9459           macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9460           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9461           break;
9462         }
9463       used_at = 1;
9464       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9465       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9466       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9467       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9468       break;
9469
9470     case M_ROL:
9471       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9472         {
9473           if (dreg == sreg)
9474             {
9475               tempreg = AT;
9476               used_at = 1;
9477             }
9478           else
9479             {
9480               tempreg = dreg;
9481             }
9482           macro_build (NULL, "negu", "d,w", tempreg, treg);
9483           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9484           break;
9485         }
9486       used_at = 1;
9487       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9488       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9489       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9490       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9491       break;
9492
9493     case M_DROL_I:
9494       {
9495         unsigned int rot;
9496         char *l;
9497         char *rr;
9498
9499         if (imm_expr.X_op != O_constant)
9500           as_bad (_("Improper rotate count"));
9501         rot = imm_expr.X_add_number & 0x3f;
9502         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9503           {
9504             rot = (64 - rot) & 0x3f;
9505             if (rot >= 32)
9506               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9507             else
9508               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9509             break;
9510           }
9511         if (rot == 0)
9512           {
9513             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9514             break;
9515           }
9516         l = (rot < 0x20) ? "dsll" : "dsll32";
9517         rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9518         rot &= 0x1f;
9519         used_at = 1;
9520         macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9521         macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9522         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9523       }
9524       break;
9525
9526     case M_ROL_I:
9527       {
9528         unsigned int rot;
9529
9530         if (imm_expr.X_op != O_constant)
9531           as_bad (_("Improper rotate count"));
9532         rot = imm_expr.X_add_number & 0x1f;
9533         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9534           {
9535             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9536             break;
9537           }
9538         if (rot == 0)
9539           {
9540             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9541             break;
9542           }
9543         used_at = 1;
9544         macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9545         macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9546         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9547       }
9548       break;
9549
9550     case M_DROR:
9551       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9552         {
9553           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9554           break;
9555         }
9556       used_at = 1;
9557       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9558       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9559       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9560       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9561       break;
9562
9563     case M_ROR:
9564       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9565         {
9566           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9567           break;
9568         }
9569       used_at = 1;
9570       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9571       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9572       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9573       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9574       break;
9575
9576     case M_DROR_I:
9577       {
9578         unsigned int rot;
9579         char *l;
9580         char *rr;
9581
9582         if (imm_expr.X_op != O_constant)
9583           as_bad (_("Improper rotate count"));
9584         rot = imm_expr.X_add_number & 0x3f;
9585         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9586           {
9587             if (rot >= 32)
9588               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9589             else
9590               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9591             break;
9592           }
9593         if (rot == 0)
9594           {
9595             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9596             break;
9597           }
9598         rr = (rot < 0x20) ? "dsrl" : "dsrl32";
9599         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9600         rot &= 0x1f;
9601         used_at = 1;
9602         macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9603         macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9604         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9605       }
9606       break;
9607
9608     case M_ROR_I:
9609       {
9610         unsigned int rot;
9611
9612         if (imm_expr.X_op != O_constant)
9613           as_bad (_("Improper rotate count"));
9614         rot = imm_expr.X_add_number & 0x1f;
9615         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9616           {
9617             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
9618             break;
9619           }
9620         if (rot == 0)
9621           {
9622             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9623             break;
9624           }
9625         used_at = 1;
9626         macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9627         macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9628         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9629       }
9630       break;
9631
9632     case M_SEQ:
9633       if (sreg == 0)
9634         macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
9635       else if (treg == 0)
9636         macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9637       else
9638         {
9639           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9640           macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9641         }
9642       break;
9643
9644     case M_SEQ_I:
9645       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9646         {
9647           macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9648           break;
9649         }
9650       if (sreg == 0)
9651         {
9652           as_warn (_("Instruction %s: result is always false"),
9653                    ip->insn_mo->name);
9654           move_register (dreg, 0);
9655           break;
9656         }
9657       if (CPU_HAS_SEQ (mips_opts.arch)
9658           && -512 <= imm_expr.X_add_number
9659           && imm_expr.X_add_number < 512)
9660         {
9661           macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
9662                        (int) imm_expr.X_add_number);
9663           break;
9664         }
9665       if (imm_expr.X_op == O_constant
9666           && imm_expr.X_add_number >= 0
9667           && imm_expr.X_add_number < 0x10000)
9668         {
9669           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9670         }
9671       else if (imm_expr.X_op == O_constant
9672                && imm_expr.X_add_number > -0x8000
9673                && imm_expr.X_add_number < 0)
9674         {
9675           imm_expr.X_add_number = -imm_expr.X_add_number;
9676           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9677                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9678         }
9679       else if (CPU_HAS_SEQ (mips_opts.arch))
9680         {
9681           used_at = 1;
9682           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9683           macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9684           break;
9685         }
9686       else
9687         {
9688           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9689           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9690           used_at = 1;
9691         }
9692       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9693       break;
9694
9695     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
9696       s = "slt";
9697       goto sge;
9698     case M_SGEU:
9699       s = "sltu";
9700     sge:
9701       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9702       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9703       break;
9704
9705     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
9706     case M_SGEU_I:
9707       if (imm_expr.X_op == O_constant
9708           && imm_expr.X_add_number >= -0x8000
9709           && imm_expr.X_add_number < 0x8000)
9710         {
9711           macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9712                        dreg, sreg, BFD_RELOC_LO16);
9713         }
9714       else
9715         {
9716           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9717           macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9718                        dreg, sreg, AT);
9719           used_at = 1;
9720         }
9721       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9722       break;
9723
9724     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
9725       s = "slt";
9726       goto sgt;
9727     case M_SGTU:
9728       s = "sltu";
9729     sgt:
9730       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9731       break;
9732
9733     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
9734       s = "slt";
9735       goto sgti;
9736     case M_SGTU_I:
9737       s = "sltu";
9738     sgti:
9739       used_at = 1;
9740       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9741       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9742       break;
9743
9744     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
9745       s = "slt";
9746       goto sle;
9747     case M_SLEU:
9748       s = "sltu";
9749     sle:
9750       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9751       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9752       break;
9753
9754     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
9755       s = "slt";
9756       goto slei;
9757     case M_SLEU_I:
9758       s = "sltu";
9759     slei:
9760       used_at = 1;
9761       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9762       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9763       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9764       break;
9765
9766     case M_SLT_I:
9767       if (imm_expr.X_op == O_constant
9768           && imm_expr.X_add_number >= -0x8000
9769           && imm_expr.X_add_number < 0x8000)
9770         {
9771           macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9772           break;
9773         }
9774       used_at = 1;
9775       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9776       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
9777       break;
9778
9779     case M_SLTU_I:
9780       if (imm_expr.X_op == O_constant
9781           && imm_expr.X_add_number >= -0x8000
9782           && imm_expr.X_add_number < 0x8000)
9783         {
9784           macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
9785                        BFD_RELOC_LO16);
9786           break;
9787         }
9788       used_at = 1;
9789       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9790       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
9791       break;
9792
9793     case M_SNE:
9794       if (sreg == 0)
9795         macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
9796       else if (treg == 0)
9797         macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9798       else
9799         {
9800           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9801           macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9802         }
9803       break;
9804
9805     case M_SNE_I:
9806       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9807         {
9808           macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9809           break;
9810         }
9811       if (sreg == 0)
9812         {
9813           as_warn (_("Instruction %s: result is always true"),
9814                    ip->insn_mo->name);
9815           macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9816                        dreg, 0, BFD_RELOC_LO16);
9817           break;
9818         }
9819       if (CPU_HAS_SEQ (mips_opts.arch)
9820           && -512 <= imm_expr.X_add_number
9821           && imm_expr.X_add_number < 512)
9822         {
9823           macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
9824                        (int) imm_expr.X_add_number);
9825           break;
9826         }
9827       if (imm_expr.X_op == O_constant
9828           && imm_expr.X_add_number >= 0
9829           && imm_expr.X_add_number < 0x10000)
9830         {
9831           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9832         }
9833       else if (imm_expr.X_op == O_constant
9834                && imm_expr.X_add_number > -0x8000
9835                && imm_expr.X_add_number < 0)
9836         {
9837           imm_expr.X_add_number = -imm_expr.X_add_number;
9838           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9839                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9840         }
9841       else if (CPU_HAS_SEQ (mips_opts.arch))
9842         {
9843           used_at = 1;
9844           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9845           macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9846           break;
9847         }
9848       else
9849         {
9850           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9851           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9852           used_at = 1;
9853         }
9854       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9855       break;
9856
9857     case M_SUB_I:
9858       s = "addi";
9859       s2 = "sub";
9860       goto do_subi;
9861     case M_SUBU_I:
9862       s = "addiu";
9863       s2 = "subu";
9864       goto do_subi;
9865     case M_DSUB_I:
9866       dbl = 1;
9867       s = "daddi";
9868       s2 = "dsub";
9869       if (!mips_opts.micromips)
9870         goto do_subi;
9871       if (imm_expr.X_op == O_constant
9872           && imm_expr.X_add_number > -0x200
9873           && imm_expr.X_add_number <= 0x200)
9874         {
9875           macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
9876           break;
9877         }
9878       goto do_subi_i;
9879     case M_DSUBU_I:
9880       dbl = 1;
9881       s = "daddiu";
9882       s2 = "dsubu";
9883     do_subi:
9884       if (imm_expr.X_op == O_constant
9885           && imm_expr.X_add_number > -0x8000
9886           && imm_expr.X_add_number <= 0x8000)
9887         {
9888           imm_expr.X_add_number = -imm_expr.X_add_number;
9889           macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9890           break;
9891         }
9892     do_subi_i:
9893       used_at = 1;
9894       load_register (AT, &imm_expr, dbl);
9895       macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
9896       break;
9897
9898     case M_TEQ_I:
9899       s = "teq";
9900       goto trap;
9901     case M_TGE_I:
9902       s = "tge";
9903       goto trap;
9904     case M_TGEU_I:
9905       s = "tgeu";
9906       goto trap;
9907     case M_TLT_I:
9908       s = "tlt";
9909       goto trap;
9910     case M_TLTU_I:
9911       s = "tltu";
9912       goto trap;
9913     case M_TNE_I:
9914       s = "tne";
9915     trap:
9916       used_at = 1;
9917       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9918       macro_build (NULL, s, "s,t", sreg, AT);
9919       break;
9920
9921     case M_TRUNCWS:
9922     case M_TRUNCWD:
9923       gas_assert (!mips_opts.micromips);
9924       gas_assert (mips_opts.isa == ISA_MIPS1);
9925       used_at = 1;
9926       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
9927       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
9928
9929       /*
9930        * Is the double cfc1 instruction a bug in the mips assembler;
9931        * or is there a reason for it?
9932        */
9933       start_noreorder ();
9934       macro_build (NULL, "cfc1", "t,G", treg, RA);
9935       macro_build (NULL, "cfc1", "t,G", treg, RA);
9936       macro_build (NULL, "nop", "");
9937       expr1.X_add_number = 3;
9938       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
9939       expr1.X_add_number = 2;
9940       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9941       macro_build (NULL, "ctc1", "t,G", AT, RA);
9942       macro_build (NULL, "nop", "");
9943       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9944                    dreg, sreg);
9945       macro_build (NULL, "ctc1", "t,G", treg, RA);
9946       macro_build (NULL, "nop", "");
9947       end_noreorder ();
9948       break;
9949
9950     case M_ULH_A:
9951       ab = 1;
9952     case M_ULH:
9953       s = "lb";
9954       s2 = "lbu";
9955       off = 1;
9956       goto uld_st;
9957     case M_ULHU_A:
9958       ab = 1;
9959     case M_ULHU:
9960       s = "lbu";
9961       s2 = "lbu";
9962       off = 1;
9963       goto uld_st;
9964     case M_ULW_A:
9965       ab = 1;
9966     case M_ULW:
9967       s = "lwl";
9968       s2 = "lwr";
9969       off12 = mips_opts.micromips;
9970       off = 3;
9971       goto uld_st;
9972     case M_ULD_A:
9973       ab = 1;
9974     case M_ULD:
9975       s = "ldl";
9976       s2 = "ldr";
9977       off12 = mips_opts.micromips;
9978       off = 7;
9979       goto uld_st;
9980     case M_USH_A:
9981       ab = 1;
9982     case M_USH:
9983       s = "sb";
9984       s2 = "sb";
9985       off = 1;
9986       ust = 1;
9987       goto uld_st;
9988     case M_USW_A:
9989       ab = 1;
9990     case M_USW:
9991       s = "swl";
9992       s2 = "swr";
9993       off12 = mips_opts.micromips;
9994       off = 3;
9995       ust = 1;
9996       goto uld_st;
9997     case M_USD_A:
9998       ab = 1;
9999     case M_USD:
10000       s = "sdl";
10001       s2 = "sdr";
10002       off12 = mips_opts.micromips;
10003       off = 7;
10004       ust = 1;
10005
10006     uld_st:
10007       if (!ab && offset_expr.X_add_number >= 0x8000 - off)
10008         as_bad (_("Operand overflow"));
10009
10010       ep = &offset_expr;
10011       expr1.X_add_number = 0;
10012       if (ab)
10013         {
10014           used_at = 1;
10015           tempreg = AT;
10016           load_address (tempreg, ep, &used_at);
10017           if (breg != 0)
10018             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10019                          tempreg, tempreg, breg);
10020           breg = tempreg;
10021           tempreg = treg;
10022           ep = &expr1;
10023         }
10024       else if (off12
10025                && (offset_expr.X_op != O_constant
10026                    || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10027                    || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10028         {
10029           used_at = 1;
10030           tempreg = AT;
10031           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10032                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10033           breg = tempreg;
10034           tempreg = treg;
10035           ep = &expr1;
10036         }
10037       else if (!ust && treg == breg)
10038         {
10039           used_at = 1;
10040           tempreg = AT;
10041         }
10042       else
10043         tempreg = treg;
10044
10045       if (off == 1)
10046         goto ulh_sh;
10047
10048       if (!target_big_endian)
10049         ep->X_add_number += off;
10050       if (!off12)
10051         macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10052       else
10053         macro_build (NULL, s, "t,~(b)",
10054                      tempreg, (unsigned long) ep->X_add_number, breg);
10055
10056       if (!target_big_endian)
10057         ep->X_add_number -= off;
10058       else
10059         ep->X_add_number += off;
10060       if (!off12)
10061         macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10062       else
10063         macro_build (NULL, s2, "t,~(b)",
10064                      tempreg, (unsigned long) ep->X_add_number, breg);
10065
10066       /* If necessary, move the result in tempreg to the final destination.  */
10067       if (!ust && treg != tempreg)
10068         {
10069           /* Protect second load's delay slot.  */
10070           load_delay_nop ();
10071           move_register (treg, tempreg);
10072         }
10073       break;
10074
10075     ulh_sh:
10076       used_at = 1;
10077       if (target_big_endian == ust)
10078         ep->X_add_number += off;
10079       tempreg = ust || ab ? treg : AT;
10080       macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10081
10082       /* For halfword transfers we need a temporary register to shuffle
10083          bytes.  Unfortunately for M_USH_A we have none available before
10084          the next store as AT holds the base address.  We deal with this
10085          case by clobbering TREG and then restoring it as with ULH.  */
10086       tempreg = ust == ab ? treg : AT;
10087       if (ust)
10088         macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10089
10090       if (target_big_endian == ust)
10091         ep->X_add_number -= off;
10092       else
10093         ep->X_add_number += off;
10094       macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10095
10096       /* For M_USH_A re-retrieve the LSB.  */
10097       if (ust && ab)
10098         {
10099           if (target_big_endian)
10100             ep->X_add_number += off;
10101           else
10102             ep->X_add_number -= off;
10103           macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10104         }
10105       /* For ULH and M_USH_A OR the LSB in.  */
10106       if (!ust || ab)
10107         {
10108           tempreg = !ab ? AT : treg;
10109           macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10110           macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10111         }
10112       break;
10113
10114     default:
10115       /* FIXME: Check if this is one of the itbl macros, since they
10116          are added dynamically.  */
10117       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10118       break;
10119     }
10120   if (!mips_opts.at && used_at)
10121     as_bad (_("Macro used $at after \".set noat\""));
10122 }
10123
10124 /* Implement macros in mips16 mode.  */
10125
10126 static void
10127 mips16_macro (struct mips_cl_insn *ip)
10128 {
10129   int mask;
10130   int xreg, yreg, zreg, tmp;
10131   expressionS expr1;
10132   int dbl;
10133   const char *s, *s2, *s3;
10134
10135   mask = ip->insn_mo->mask;
10136
10137   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10138   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10139   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10140
10141   expr1.X_op = O_constant;
10142   expr1.X_op_symbol = NULL;
10143   expr1.X_add_symbol = NULL;
10144   expr1.X_add_number = 1;
10145
10146   dbl = 0;
10147
10148   switch (mask)
10149     {
10150     default:
10151       abort ();
10152
10153     case M_DDIV_3:
10154       dbl = 1;
10155     case M_DIV_3:
10156       s = "mflo";
10157       goto do_div3;
10158     case M_DREM_3:
10159       dbl = 1;
10160     case M_REM_3:
10161       s = "mfhi";
10162     do_div3:
10163       start_noreorder ();
10164       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10165       expr1.X_add_number = 2;
10166       macro_build (&expr1, "bnez", "x,p", yreg);
10167       macro_build (NULL, "break", "6", 7);
10168
10169       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10170          since that causes an overflow.  We should do that as well,
10171          but I don't see how to do the comparisons without a temporary
10172          register.  */
10173       end_noreorder ();
10174       macro_build (NULL, s, "x", zreg);
10175       break;
10176
10177     case M_DIVU_3:
10178       s = "divu";
10179       s2 = "mflo";
10180       goto do_divu3;
10181     case M_REMU_3:
10182       s = "divu";
10183       s2 = "mfhi";
10184       goto do_divu3;
10185     case M_DDIVU_3:
10186       s = "ddivu";
10187       s2 = "mflo";
10188       goto do_divu3;
10189     case M_DREMU_3:
10190       s = "ddivu";
10191       s2 = "mfhi";
10192     do_divu3:
10193       start_noreorder ();
10194       macro_build (NULL, s, "0,x,y", xreg, yreg);
10195       expr1.X_add_number = 2;
10196       macro_build (&expr1, "bnez", "x,p", yreg);
10197       macro_build (NULL, "break", "6", 7);
10198       end_noreorder ();
10199       macro_build (NULL, s2, "x", zreg);
10200       break;
10201
10202     case M_DMUL:
10203       dbl = 1;
10204     case M_MUL:
10205       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10206       macro_build (NULL, "mflo", "x", zreg);
10207       break;
10208
10209     case M_DSUBU_I:
10210       dbl = 1;
10211       goto do_subu;
10212     case M_SUBU_I:
10213     do_subu:
10214       if (imm_expr.X_op != O_constant)
10215         as_bad (_("Unsupported large constant"));
10216       imm_expr.X_add_number = -imm_expr.X_add_number;
10217       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10218       break;
10219
10220     case M_SUBU_I_2:
10221       if (imm_expr.X_op != O_constant)
10222         as_bad (_("Unsupported large constant"));
10223       imm_expr.X_add_number = -imm_expr.X_add_number;
10224       macro_build (&imm_expr, "addiu", "x,k", xreg);
10225       break;
10226
10227     case M_DSUBU_I_2:
10228       if (imm_expr.X_op != O_constant)
10229         as_bad (_("Unsupported large constant"));
10230       imm_expr.X_add_number = -imm_expr.X_add_number;
10231       macro_build (&imm_expr, "daddiu", "y,j", yreg);
10232       break;
10233
10234     case M_BEQ:
10235       s = "cmp";
10236       s2 = "bteqz";
10237       goto do_branch;
10238     case M_BNE:
10239       s = "cmp";
10240       s2 = "btnez";
10241       goto do_branch;
10242     case M_BLT:
10243       s = "slt";
10244       s2 = "btnez";
10245       goto do_branch;
10246     case M_BLTU:
10247       s = "sltu";
10248       s2 = "btnez";
10249       goto do_branch;
10250     case M_BLE:
10251       s = "slt";
10252       s2 = "bteqz";
10253       goto do_reverse_branch;
10254     case M_BLEU:
10255       s = "sltu";
10256       s2 = "bteqz";
10257       goto do_reverse_branch;
10258     case M_BGE:
10259       s = "slt";
10260       s2 = "bteqz";
10261       goto do_branch;
10262     case M_BGEU:
10263       s = "sltu";
10264       s2 = "bteqz";
10265       goto do_branch;
10266     case M_BGT:
10267       s = "slt";
10268       s2 = "btnez";
10269       goto do_reverse_branch;
10270     case M_BGTU:
10271       s = "sltu";
10272       s2 = "btnez";
10273
10274     do_reverse_branch:
10275       tmp = xreg;
10276       xreg = yreg;
10277       yreg = tmp;
10278
10279     do_branch:
10280       macro_build (NULL, s, "x,y", xreg, yreg);
10281       macro_build (&offset_expr, s2, "p");
10282       break;
10283
10284     case M_BEQ_I:
10285       s = "cmpi";
10286       s2 = "bteqz";
10287       s3 = "x,U";
10288       goto do_branch_i;
10289     case M_BNE_I:
10290       s = "cmpi";
10291       s2 = "btnez";
10292       s3 = "x,U";
10293       goto do_branch_i;
10294     case M_BLT_I:
10295       s = "slti";
10296       s2 = "btnez";
10297       s3 = "x,8";
10298       goto do_branch_i;
10299     case M_BLTU_I:
10300       s = "sltiu";
10301       s2 = "btnez";
10302       s3 = "x,8";
10303       goto do_branch_i;
10304     case M_BLE_I:
10305       s = "slti";
10306       s2 = "btnez";
10307       s3 = "x,8";
10308       goto do_addone_branch_i;
10309     case M_BLEU_I:
10310       s = "sltiu";
10311       s2 = "btnez";
10312       s3 = "x,8";
10313       goto do_addone_branch_i;
10314     case M_BGE_I:
10315       s = "slti";
10316       s2 = "bteqz";
10317       s3 = "x,8";
10318       goto do_branch_i;
10319     case M_BGEU_I:
10320       s = "sltiu";
10321       s2 = "bteqz";
10322       s3 = "x,8";
10323       goto do_branch_i;
10324     case M_BGT_I:
10325       s = "slti";
10326       s2 = "bteqz";
10327       s3 = "x,8";
10328       goto do_addone_branch_i;
10329     case M_BGTU_I:
10330       s = "sltiu";
10331       s2 = "bteqz";
10332       s3 = "x,8";
10333
10334     do_addone_branch_i:
10335       if (imm_expr.X_op != O_constant)
10336         as_bad (_("Unsupported large constant"));
10337       ++imm_expr.X_add_number;
10338
10339     do_branch_i:
10340       macro_build (&imm_expr, s, s3, xreg);
10341       macro_build (&offset_expr, s2, "p");
10342       break;
10343
10344     case M_ABS:
10345       expr1.X_add_number = 0;
10346       macro_build (&expr1, "slti", "x,8", yreg);
10347       if (xreg != yreg)
10348         move_register (xreg, yreg);
10349       expr1.X_add_number = 2;
10350       macro_build (&expr1, "bteqz", "p");
10351       macro_build (NULL, "neg", "x,w", xreg, xreg);
10352     }
10353 }
10354
10355 /* For consistency checking, verify that all bits are specified either
10356    by the match/mask part of the instruction definition, or by the
10357    operand list.  */
10358 static int
10359 validate_mips_insn (const struct mips_opcode *opc)
10360 {
10361   const char *p = opc->args;
10362   char c;
10363   unsigned long used_bits = opc->mask;
10364
10365   if ((used_bits & opc->match) != opc->match)
10366     {
10367       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10368               opc->name, opc->args);
10369       return 0;
10370     }
10371 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
10372   while (*p)
10373     switch (c = *p++)
10374       {
10375       case ',': break;
10376       case '(': break;
10377       case ')': break;
10378       case '+':
10379         switch (c = *p++)
10380           {
10381           case '1': USE_BITS (OP_MASK_UDI1,     OP_SH_UDI1);    break;
10382           case '2': USE_BITS (OP_MASK_UDI2,     OP_SH_UDI2);    break;
10383           case '3': USE_BITS (OP_MASK_UDI3,     OP_SH_UDI3);    break;
10384           case '4': USE_BITS (OP_MASK_UDI4,     OP_SH_UDI4);    break;
10385           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10386           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10387           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10388           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
10389                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
10390           case 'E': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10391           case 'F': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10392           case 'G': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10393           case 'H': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10394           case 'I': break;
10395           case 't': USE_BITS (OP_MASK_RT,       OP_SH_RT);      break;
10396           case 'T': USE_BITS (OP_MASK_RT,       OP_SH_RT);
10397                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
10398           case 'x': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10399           case 'X': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10400           case 'p': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10401           case 'P': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10402           case 'Q': USE_BITS (OP_MASK_SEQI,     OP_SH_SEQI);    break;
10403           case 's': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10404           case 'S': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10405           case 'z': USE_BITS (OP_MASK_RZ,       OP_SH_RZ);      break;
10406           case 'Z': USE_BITS (OP_MASK_FZ,       OP_SH_FZ);      break;
10407           case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10408           case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10409           case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
10410
10411           default:
10412             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10413                     c, opc->name, opc->args);
10414             return 0;
10415           }
10416         break;
10417       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10418       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10419       case 'A': break;
10420       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
10421       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
10422       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10423       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10424       case 'F': break;
10425       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10426       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
10427       case 'I': break;
10428       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
10429       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10430       case 'L': break;
10431       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
10432       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
10433       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
10434       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
10435                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10436       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
10437       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10438       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10439       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10440       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10441       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10442       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10443       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10444       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
10445       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10446       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
10447       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10448       case 'f': break;
10449       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
10450       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10451       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10452       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
10453       case 'l': break;
10454       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10455       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10456       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
10457       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10458       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10459       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10460       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10461       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10462       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10463       case 'x': break;
10464       case 'z': break;
10465       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
10466       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
10467                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10468       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
10469       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
10470       case '[': break;
10471       case ']': break;
10472       case '1': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10473       case '2': USE_BITS (OP_MASK_BP,           OP_SH_BP);      break;
10474       case '3': USE_BITS (OP_MASK_SA3,          OP_SH_SA3);     break;
10475       case '4': USE_BITS (OP_MASK_SA4,          OP_SH_SA4);     break;
10476       case '5': USE_BITS (OP_MASK_IMM8,         OP_SH_IMM8);    break;
10477       case '6': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10478       case '7': USE_BITS (OP_MASK_DSPACC,       OP_SH_DSPACC);  break;
10479       case '8': USE_BITS (OP_MASK_WRDSP,        OP_SH_WRDSP);   break;
10480       case '9': USE_BITS (OP_MASK_DSPACC_S,     OP_SH_DSPACC_S);break;
10481       case '0': USE_BITS (OP_MASK_DSPSFT,       OP_SH_DSPSFT);  break;
10482       case '\'': USE_BITS (OP_MASK_RDDSP,       OP_SH_RDDSP);   break;
10483       case ':': USE_BITS (OP_MASK_DSPSFT_7,     OP_SH_DSPSFT_7);break;
10484       case '@': USE_BITS (OP_MASK_IMM10,        OP_SH_IMM10);   break;
10485       case '!': USE_BITS (OP_MASK_MT_U,         OP_SH_MT_U);    break;
10486       case '$': USE_BITS (OP_MASK_MT_H,         OP_SH_MT_H);    break;
10487       case '*': USE_BITS (OP_MASK_MTACC_T,      OP_SH_MTACC_T); break;
10488       case '&': USE_BITS (OP_MASK_MTACC_D,      OP_SH_MTACC_D); break;
10489       case '\\': USE_BITS (OP_MASK_3BITPOS,     OP_SH_3BITPOS); break;
10490       case '~': USE_BITS (OP_MASK_OFFSET12,     OP_SH_OFFSET12); break;
10491       case 'g': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10492       default:
10493         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10494                 c, opc->name, opc->args);
10495         return 0;
10496       }
10497 #undef USE_BITS
10498   if (used_bits != 0xffffffff)
10499     {
10500       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10501               ~used_bits & 0xffffffff, opc->name, opc->args);
10502       return 0;
10503     }
10504   return 1;
10505 }
10506
10507 /* For consistency checking, verify that the length implied matches the
10508    major opcode and that all bits are specified either by the match/mask
10509    part of the instruction definition, or by the operand list.  */
10510
10511 static int
10512 validate_micromips_insn (const struct mips_opcode *opc)
10513 {
10514   unsigned long match = opc->match;
10515   unsigned long mask = opc->mask;
10516   const char *p = opc->args;
10517   unsigned long insn_bits;
10518   unsigned long used_bits;
10519   unsigned long major;
10520   unsigned int length;
10521   char e;
10522   char c;
10523
10524   if ((mask & match) != match)
10525     {
10526       as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10527               opc->name, opc->args);
10528       return 0;
10529     }
10530   length = micromips_insn_length (opc);
10531   if (length != 2 && length != 4)
10532     {
10533       as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10534                 "%s %s"), length, opc->name, opc->args);
10535       return 0;
10536     }
10537   major = match >> (10 + 8 * (length - 2));
10538   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10539       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10540     {
10541       as_bad (_("Internal error: bad microMIPS opcode "
10542                 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10543       return 0;
10544     }
10545
10546   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
10547   insn_bits = 1 << 4 * length;
10548   insn_bits <<= 4 * length;
10549   insn_bits -= 1;
10550   used_bits = mask;
10551 #define USE_BITS(field) \
10552   (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10553   while (*p)
10554     switch (c = *p++)
10555       {
10556       case ',': break;
10557       case '(': break;
10558       case ')': break;
10559       case '+':
10560         e = c;
10561         switch (c = *p++)
10562           {
10563           case 'A': USE_BITS (EXTLSB);  break;
10564           case 'B': USE_BITS (INSMSB);  break;
10565           case 'C': USE_BITS (EXTMSBD); break;
10566           case 'D': USE_BITS (RS);      USE_BITS (SEL); break;
10567           case 'E': USE_BITS (EXTLSB);  break;
10568           case 'F': USE_BITS (INSMSB);  break;
10569           case 'G': USE_BITS (EXTMSBD); break;
10570           case 'H': USE_BITS (EXTMSBD); break;
10571           default:
10572             as_bad (_("Internal error: bad mips opcode "
10573                       "(unknown extension operand type `%c%c'): %s %s"),
10574                     e, c, opc->name, opc->args);
10575             return 0;
10576           }
10577         break;
10578       case 'm':
10579         e = c;
10580         switch (c = *p++)
10581           {
10582           case 'A': USE_BITS (IMMA);    break;
10583           case 'B': USE_BITS (IMMB);    break;
10584           case 'C': USE_BITS (IMMC);    break;
10585           case 'D': USE_BITS (IMMD);    break;
10586           case 'E': USE_BITS (IMME);    break;
10587           case 'F': USE_BITS (IMMF);    break;
10588           case 'G': USE_BITS (IMMG);    break;
10589           case 'H': USE_BITS (IMMH);    break;
10590           case 'I': USE_BITS (IMMI);    break;
10591           case 'J': USE_BITS (IMMJ);    break;
10592           case 'L': USE_BITS (IMML);    break;
10593           case 'M': USE_BITS (IMMM);    break;
10594           case 'N': USE_BITS (IMMN);    break;
10595           case 'O': USE_BITS (IMMO);    break;
10596           case 'P': USE_BITS (IMMP);    break;
10597           case 'Q': USE_BITS (IMMQ);    break;
10598           case 'U': USE_BITS (IMMU);    break;
10599           case 'W': USE_BITS (IMMW);    break;
10600           case 'X': USE_BITS (IMMX);    break;
10601           case 'Y': USE_BITS (IMMY);    break;
10602           case 'Z': break;
10603           case 'a': break;
10604           case 'b': USE_BITS (MB);      break;
10605           case 'c': USE_BITS (MC);      break;
10606           case 'd': USE_BITS (MD);      break;
10607           case 'e': USE_BITS (ME);      break;
10608           case 'f': USE_BITS (MF);      break;
10609           case 'g': USE_BITS (MG);      break;
10610           case 'h': USE_BITS (MH);      break;
10611           case 'i': USE_BITS (MI);      break;
10612           case 'j': USE_BITS (MJ);      break;
10613           case 'l': USE_BITS (ML);      break;
10614           case 'm': USE_BITS (MM);      break;
10615           case 'n': USE_BITS (MN);      break;
10616           case 'p': USE_BITS (MP);      break;
10617           case 'q': USE_BITS (MQ);      break;
10618           case 'r': break;
10619           case 's': break;
10620           case 't': break;
10621           case 'x': break;
10622           case 'y': break;
10623           case 'z': break;
10624           default:
10625             as_bad (_("Internal error: bad mips opcode "
10626                       "(unknown extension operand type `%c%c'): %s %s"),
10627                     e, c, opc->name, opc->args);
10628             return 0;
10629           }
10630         break;
10631       case '.': USE_BITS (OFFSET10);    break;
10632       case '1': USE_BITS (STYPE);       break;
10633       case '2': USE_BITS (BP);          break;
10634       case '3': USE_BITS (SA3);         break;
10635       case '4': USE_BITS (SA4);         break;
10636       case '5': USE_BITS (IMM8);        break;
10637       case '6': USE_BITS (RS);          break;
10638       case '7': USE_BITS (DSPACC);      break;
10639       case '8': USE_BITS (WRDSP);       break;
10640       case '0': USE_BITS (DSPSFT);      break;
10641       case '<': USE_BITS (SHAMT);       break;
10642       case '>': USE_BITS (SHAMT);       break;
10643       case '@': USE_BITS (IMM10);       break;
10644       case 'B': USE_BITS (CODE10);      break;
10645       case 'C': USE_BITS (COPZ);        break;
10646       case 'D': USE_BITS (FD);          break;
10647       case 'E': USE_BITS (RT);          break;
10648       case 'G': USE_BITS (RS);          break;
10649       case 'H': USE_BITS (SEL);         break;
10650       case 'K': USE_BITS (RS);          break;
10651       case 'M': USE_BITS (CCC);         break;
10652       case 'N': USE_BITS (BCC);         break;
10653       case 'R': USE_BITS (FR);          break;
10654       case 'S': USE_BITS (FS);          break;
10655       case 'T': USE_BITS (FT);          break;
10656       case 'V': USE_BITS (FS);          break;
10657       case '\\': USE_BITS (3BITPOS);    break;
10658       case '^': USE_BITS (RD);          break;
10659       case 'a': USE_BITS (TARGET);      break;
10660       case 'b': USE_BITS (RS);          break;
10661       case 'c': USE_BITS (CODE);        break;
10662       case 'd': USE_BITS (RD);          break;
10663       case 'h': USE_BITS (PREFX);       break;
10664       case 'i': USE_BITS (IMMEDIATE);   break;
10665       case 'j': USE_BITS (DELTA);       break;
10666       case 'k': USE_BITS (CACHE);       break;
10667       case 'n': USE_BITS (RT);          break;
10668       case 'o': USE_BITS (DELTA);       break;
10669       case 'p': USE_BITS (DELTA);       break;
10670       case 'q': USE_BITS (CODE2);       break;
10671       case 'r': USE_BITS (RS);          break;
10672       case 's': USE_BITS (RS);          break;
10673       case 't': USE_BITS (RT);          break;
10674       case 'u': USE_BITS (IMMEDIATE);   break;
10675       case 'v': USE_BITS (RS);          break;
10676       case 'w': USE_BITS (RT);          break;
10677       case 'y': USE_BITS (RS3);         break;
10678       case 'z': break;
10679       case '|': USE_BITS (TRAP);        break;
10680       case '~': USE_BITS (OFFSET12);    break;
10681       default:
10682         as_bad (_("Internal error: bad microMIPS opcode "
10683                   "(unknown operand type `%c'): %s %s"),
10684                 c, opc->name, opc->args);
10685         return 0;
10686       }
10687 #undef USE_BITS
10688   if (used_bits != insn_bits)
10689     {
10690       if (~used_bits & insn_bits)
10691         as_bad (_("Internal error: bad microMIPS opcode "
10692                   "(bits 0x%lx undefined): %s %s"),
10693                 ~used_bits & insn_bits, opc->name, opc->args);
10694       if (used_bits & ~insn_bits)
10695         as_bad (_("Internal error: bad microMIPS opcode "
10696                   "(bits 0x%lx defined): %s %s"),
10697                 used_bits & ~insn_bits, opc->name, opc->args);
10698       return 0;
10699     }
10700   return 1;
10701 }
10702
10703 /* UDI immediates.  */
10704 struct mips_immed {
10705   char          type;
10706   unsigned int  shift;
10707   unsigned long mask;
10708   const char *  desc;
10709 };
10710
10711 static const struct mips_immed mips_immed[] = {
10712   { '1',        OP_SH_UDI1,     OP_MASK_UDI1,           0},
10713   { '2',        OP_SH_UDI2,     OP_MASK_UDI2,           0},
10714   { '3',        OP_SH_UDI3,     OP_MASK_UDI3,           0},
10715   { '4',        OP_SH_UDI4,     OP_MASK_UDI4,           0},
10716   { 0,0,0,0 }
10717 };
10718
10719 /* Check whether an odd floating-point register is allowed.  */
10720 static int
10721 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10722 {
10723   const char *s = insn->name;
10724
10725   if (insn->pinfo == INSN_MACRO)
10726     /* Let a macro pass, we'll catch it later when it is expanded.  */
10727     return 1;
10728
10729   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
10730     {
10731       /* Allow odd registers for single-precision ops.  */
10732       switch (insn->pinfo & (FP_S | FP_D))
10733         {
10734         case FP_S:
10735         case 0:
10736           return 1;     /* both single precision - ok */
10737         case FP_D:
10738           return 0;     /* both double precision - fail */
10739         default:
10740           break;
10741         }
10742
10743       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
10744       s = strchr (insn->name, '.');
10745       if (argnum == 2)
10746         s = s != NULL ? strchr (s + 1, '.') : NULL;
10747       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10748     } 
10749
10750   /* Single-precision coprocessor loads and moves are OK too.  */
10751   if ((insn->pinfo & FP_S)
10752       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10753                          | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10754     return 1;
10755
10756   return 0;
10757 }
10758
10759 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10760    taking bits from BIT up.  */
10761 static int
10762 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10763 {
10764   return (ep->X_op == O_constant
10765           && (ep->X_add_number & ((1 << bit) - 1)) == 0
10766           && ep->X_add_number >= min << bit
10767           && ep->X_add_number < max << bit);
10768 }
10769
10770 /* This routine assembles an instruction into its binary format.  As a
10771    side effect, it sets one of the global variables imm_reloc or
10772    offset_reloc to the type of relocation to do if one of the operands
10773    is an address expression.  */
10774
10775 static void
10776 mips_ip (char *str, struct mips_cl_insn *ip)
10777 {
10778   bfd_boolean wrong_delay_slot_insns = FALSE;
10779   bfd_boolean need_delay_slot_ok = TRUE;
10780   struct mips_opcode *firstinsn = NULL;
10781   const struct mips_opcode *past;
10782   struct hash_control *hash;
10783   char *s;
10784   const char *args;
10785   char c = 0;
10786   struct mips_opcode *insn;
10787   char *argsStart;
10788   unsigned int regno;
10789   unsigned int lastregno;
10790   unsigned int destregno = 0;
10791   unsigned int lastpos = 0;
10792   unsigned int limlo, limhi;
10793   int sizelo;
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                   sizelo = 1;
11379                   goto do_msbd;
11380                 case 'G':
11381                   limlo = 33;
11382                   limhi = 64;
11383                   sizelo = 33;
11384                   goto do_msbd;
11385                 case 'H':
11386                   limlo = 33;
11387                   limhi = 64;
11388                   sizelo = 1;
11389                   goto do_msbd;
11390                 do_msbd:
11391                   my_getExpression (&imm_expr, s);
11392                   check_absolute_expr (ip, &imm_expr);
11393                   /* The checks against (pos+size) don't transitively check
11394                      "size" itself, assuming that "pos" is reasonable.
11395                      We also need to check the lower bound of "size".  */
11396                   if ((long) imm_expr.X_add_number < sizelo
11397                       || ((unsigned long) imm_expr.X_add_number
11398                           + lastpos) < limlo
11399                       || ((unsigned long) imm_expr.X_add_number
11400                           + lastpos) > limhi)
11401                     {
11402                       as_bad (_("Improper extract size (%lu, position %lu)"),
11403                               (unsigned long) imm_expr.X_add_number,
11404                               (unsigned long) lastpos);
11405                       imm_expr.X_add_number = limlo - lastpos;
11406                     }
11407                   INSERT_OPERAND (mips_opts.micromips,
11408                                   EXTMSBD, *ip, imm_expr.X_add_number - 1);
11409                   imm_expr.X_op = O_absent;
11410                   s = expr_end;
11411                   continue;
11412
11413                 case 'D':
11414                   /* +D is for disassembly only; never match.  */
11415                   break;
11416
11417                 case 'I':
11418                   /* "+I" is like "I", except that imm2_expr is used.  */
11419                   my_getExpression (&imm2_expr, s);
11420                   if (imm2_expr.X_op != O_big
11421                       && imm2_expr.X_op != O_constant)
11422                   insn_error = _("absolute expression required");
11423                   if (HAVE_32BIT_GPRS)
11424                     normalize_constant_expr (&imm2_expr);
11425                   s = expr_end;
11426                   continue;
11427
11428                 case 'T': /* Coprocessor register.  */
11429                   gas_assert (!mips_opts.micromips);
11430                   /* +T is for disassembly only; never match.  */
11431                   break;
11432
11433                 case 't': /* Coprocessor register number.  */
11434                   gas_assert (!mips_opts.micromips);
11435                   if (s[0] == '$' && ISDIGIT (s[1]))
11436                     {
11437                       ++s;
11438                       regno = 0;
11439                       do
11440                         {
11441                           regno *= 10;
11442                           regno += *s - '0';
11443                           ++s;
11444                         }
11445                       while (ISDIGIT (*s));
11446                       if (regno > 31)
11447                         as_bad (_("Invalid register number (%d)"), regno);
11448                       else
11449                         {
11450                           INSERT_OPERAND (0, RT, *ip, regno);
11451                           continue;
11452                         }
11453                     }
11454                   else
11455                     as_bad (_("Invalid coprocessor 0 register number"));
11456                   break;
11457
11458                 case 'x':
11459                   /* bbit[01] and bbit[01]32 bit index.  Give error if index
11460                      is not in the valid range.  */
11461                   gas_assert (!mips_opts.micromips);
11462                   my_getExpression (&imm_expr, s);
11463                   check_absolute_expr (ip, &imm_expr);
11464                   if ((unsigned) imm_expr.X_add_number > 31)
11465                     {
11466                       as_bad (_("Improper bit index (%lu)"),
11467                               (unsigned long) imm_expr.X_add_number);
11468                       imm_expr.X_add_number = 0;
11469                     }
11470                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11471                   imm_expr.X_op = O_absent;
11472                   s = expr_end;
11473                   continue;
11474
11475                 case 'X':
11476                   /* bbit[01] bit index when bbit is used but we generate
11477                      bbit[01]32 because the index is over 32.  Move to the
11478                      next candidate if index is not in the valid range.  */
11479                   gas_assert (!mips_opts.micromips);
11480                   my_getExpression (&imm_expr, s);
11481                   check_absolute_expr (ip, &imm_expr);
11482                   if ((unsigned) imm_expr.X_add_number < 32
11483                       || (unsigned) imm_expr.X_add_number > 63)
11484                     break;
11485                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11486                   imm_expr.X_op = O_absent;
11487                   s = expr_end;
11488                   continue;
11489
11490                 case 'p':
11491                   /* cins, cins32, exts and exts32 position field.  Give error
11492                      if it's not in the valid range.  */
11493                   gas_assert (!mips_opts.micromips);
11494                   my_getExpression (&imm_expr, s);
11495                   check_absolute_expr (ip, &imm_expr);
11496                   if ((unsigned) imm_expr.X_add_number > 31)
11497                     {
11498                       as_bad (_("Improper position (%lu)"),
11499                               (unsigned long) imm_expr.X_add_number);
11500                       imm_expr.X_add_number = 0;
11501                     }
11502                   /* Make the pos explicit to simplify +S.  */
11503                   lastpos = imm_expr.X_add_number + 32;
11504                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11505                   imm_expr.X_op = O_absent;
11506                   s = expr_end;
11507                   continue;
11508
11509                 case 'P':
11510                   /* cins, cins32, exts and exts32 position field.  Move to
11511                      the next candidate if it's not in the valid range.  */
11512                   gas_assert (!mips_opts.micromips);
11513                   my_getExpression (&imm_expr, s);
11514                   check_absolute_expr (ip, &imm_expr);
11515                   if ((unsigned) imm_expr.X_add_number < 32
11516                       || (unsigned) imm_expr.X_add_number > 63)
11517                     break;
11518                   lastpos = imm_expr.X_add_number;
11519                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11520                   imm_expr.X_op = O_absent;
11521                   s = expr_end;
11522                   continue;
11523
11524                 case 's':
11525                   /* cins and exts length-minus-one field.  */
11526                   gas_assert (!mips_opts.micromips);
11527                   my_getExpression (&imm_expr, s);
11528                   check_absolute_expr (ip, &imm_expr);
11529                   if ((unsigned long) imm_expr.X_add_number > 31)
11530                     {
11531                       as_bad (_("Improper size (%lu)"),
11532                               (unsigned long) imm_expr.X_add_number);
11533                       imm_expr.X_add_number = 0;
11534                     }
11535                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11536                   imm_expr.X_op = O_absent;
11537                   s = expr_end;
11538                   continue;
11539
11540                 case 'S':
11541                   /* cins32/exts32 and cins/exts aliasing cint32/exts32
11542                      length-minus-one field.  */
11543                   gas_assert (!mips_opts.micromips);
11544                   my_getExpression (&imm_expr, s);
11545                   check_absolute_expr (ip, &imm_expr);
11546                   if ((long) imm_expr.X_add_number < 0
11547                       || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11548                     {
11549                       as_bad (_("Improper size (%lu)"),
11550                               (unsigned long) imm_expr.X_add_number);
11551                       imm_expr.X_add_number = 0;
11552                     }
11553                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11554                   imm_expr.X_op = O_absent;
11555                   s = expr_end;
11556                   continue;
11557
11558                 case 'Q':
11559                   /* seqi/snei immediate field.  */
11560                   gas_assert (!mips_opts.micromips);
11561                   my_getExpression (&imm_expr, s);
11562                   check_absolute_expr (ip, &imm_expr);
11563                   if ((long) imm_expr.X_add_number < -512
11564                       || (long) imm_expr.X_add_number >= 512)
11565                     {
11566                       as_bad (_("Improper immediate (%ld)"),
11567                                (long) imm_expr.X_add_number);
11568                       imm_expr.X_add_number = 0;
11569                     }
11570                   INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11571                   imm_expr.X_op = O_absent;
11572                   s = expr_end;
11573                   continue;
11574
11575                 case 'a': /* 8-bit signed offset in bit 6 */
11576                   gas_assert (!mips_opts.micromips);
11577                   my_getExpression (&imm_expr, s);
11578                   check_absolute_expr (ip, &imm_expr);
11579                   min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11580                   max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11581                   if (imm_expr.X_add_number < min_range
11582                       || imm_expr.X_add_number > max_range)
11583                     {
11584                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11585                               (long) min_range, (long) max_range,
11586                               (long) imm_expr.X_add_number);
11587                     }
11588                   INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
11589                   imm_expr.X_op = O_absent;
11590                   s = expr_end;
11591                   continue;
11592
11593                 case 'b': /* 8-bit signed offset in bit 3 */
11594                   gas_assert (!mips_opts.micromips);
11595                   my_getExpression (&imm_expr, s);
11596                   check_absolute_expr (ip, &imm_expr);
11597                   min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11598                   max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11599                   if (imm_expr.X_add_number < min_range
11600                       || imm_expr.X_add_number > max_range)
11601                     {
11602                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11603                               (long) min_range, (long) max_range,
11604                               (long) imm_expr.X_add_number);
11605                     }
11606                   INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
11607                   imm_expr.X_op = O_absent;
11608                   s = expr_end;
11609                   continue;
11610
11611                 case 'c': /* 9-bit signed offset in bit 6 */
11612                   gas_assert (!mips_opts.micromips);
11613                   my_getExpression (&imm_expr, s);
11614                   check_absolute_expr (ip, &imm_expr);
11615                   min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11616                   max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
11617                   /* We check the offset range before adjusted.  */
11618                   min_range <<= 4;
11619                   max_range <<= 4;
11620                   if (imm_expr.X_add_number < min_range
11621                       || imm_expr.X_add_number > max_range)
11622                     {
11623                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11624                               (long) min_range, (long) max_range,
11625                               (long) imm_expr.X_add_number);
11626                     }
11627                   if (imm_expr.X_add_number & 0xf)
11628                     {
11629                       as_bad (_("Offset not 16 bytes alignment (%ld)"),
11630                               (long) imm_expr.X_add_number);
11631                     }
11632                   /* Right shift 4 bits to adjust the offset operand.  */
11633                   INSERT_OPERAND (0, OFFSET_C, *ip,
11634                                   imm_expr.X_add_number >> 4);
11635                   imm_expr.X_op = O_absent;
11636                   s = expr_end;
11637                   continue;
11638
11639                 case 'z':
11640                   gas_assert (!mips_opts.micromips);
11641                   if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11642                     break;
11643                   if (regno == AT && mips_opts.at)
11644                     {
11645                       if (mips_opts.at == ATREG)
11646                         as_warn (_("used $at without \".set noat\""));
11647                       else
11648                         as_warn (_("used $%u with \".set at=$%u\""),
11649                                  regno, mips_opts.at);
11650                     }
11651                   INSERT_OPERAND (0, RZ, *ip, regno);
11652                   continue;
11653
11654                 case 'Z':
11655                   gas_assert (!mips_opts.micromips);
11656                   if (!reg_lookup (&s, RTYPE_FPU, &regno))
11657                     break;
11658                   INSERT_OPERAND (0, FZ, *ip, regno);
11659                   continue;
11660
11661                 default:
11662                   as_bad (_("Internal error: bad %s opcode "
11663                             "(unknown extension operand type `+%c'): %s %s"),
11664                           mips_opts.micromips ? "microMIPS" : "MIPS",
11665                           *args, insn->name, insn->args);
11666                   /* Further processing is fruitless.  */
11667                   return;
11668                 }
11669               break;
11670
11671             case '.':           /* 10-bit offset.  */
11672               gas_assert (mips_opts.micromips);
11673             case '~':           /* 12-bit offset.  */
11674               {
11675                 int shift = *args == '.' ? 9 : 11;
11676                 size_t i;
11677
11678                 /* Check whether there is only a single bracketed expression
11679                    left.  If so, it must be the base register and the
11680                    constant must be zero.  */
11681                 if (*s == '(' && strchr (s + 1, '(') == 0)
11682                   continue;
11683
11684                 /* If this value won't fit into the offset, then go find
11685                    a macro that will generate a 16- or 32-bit offset code
11686                    pattern.  */
11687                 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11688                 if ((i == 0 && (imm_expr.X_op != O_constant
11689                                 || imm_expr.X_add_number >= 1 << shift
11690                                 || imm_expr.X_add_number < -1 << shift))
11691                     || i > 0)
11692                   {
11693                     imm_expr.X_op = O_absent;
11694                     break;
11695                   }
11696                 if (shift == 9)
11697                   INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11698                 else
11699                   INSERT_OPERAND (mips_opts.micromips,
11700                                   OFFSET12, *ip, imm_expr.X_add_number);
11701                 imm_expr.X_op = O_absent;
11702                 s = expr_end;
11703               }
11704               continue;
11705
11706             case '<':           /* must be at least one digit */
11707               /*
11708                * According to the manual, if the shift amount is greater
11709                * than 31 or less than 0, then the shift amount should be
11710                * mod 32.  In reality the mips assembler issues an error.
11711                * We issue a warning and mask out all but the low 5 bits.
11712                */
11713               my_getExpression (&imm_expr, s);
11714               check_absolute_expr (ip, &imm_expr);
11715               if ((unsigned long) imm_expr.X_add_number > 31)
11716                 as_warn (_("Improper shift amount (%lu)"),
11717                          (unsigned long) imm_expr.X_add_number);
11718               INSERT_OPERAND (mips_opts.micromips,
11719                               SHAMT, *ip, imm_expr.X_add_number);
11720               imm_expr.X_op = O_absent;
11721               s = expr_end;
11722               continue;
11723
11724             case '>':           /* shift amount minus 32 */
11725               my_getExpression (&imm_expr, s);
11726               check_absolute_expr (ip, &imm_expr);
11727               if ((unsigned long) imm_expr.X_add_number < 32
11728                   || (unsigned long) imm_expr.X_add_number > 63)
11729                 break;
11730               INSERT_OPERAND (mips_opts.micromips,
11731                               SHAMT, *ip, imm_expr.X_add_number - 32);
11732               imm_expr.X_op = O_absent;
11733               s = expr_end;
11734               continue;
11735
11736             case 'k':           /* CACHE code.  */
11737             case 'h':           /* PREFX code.  */
11738             case '1':           /* SYNC type.  */
11739               my_getExpression (&imm_expr, s);
11740               check_absolute_expr (ip, &imm_expr);
11741               if ((unsigned long) imm_expr.X_add_number > 31)
11742                 as_warn (_("Invalid value for `%s' (%lu)"),
11743                          ip->insn_mo->name,
11744                          (unsigned long) imm_expr.X_add_number);
11745               switch (*args)
11746                 {
11747                 case 'k':
11748                   if (mips_fix_cn63xxp1
11749                       && !mips_opts.micromips
11750                       && strcmp ("pref", insn->name) == 0)
11751                     switch (imm_expr.X_add_number)
11752                       {
11753                       case 5:
11754                       case 25:
11755                       case 26:
11756                       case 27:
11757                       case 28:
11758                       case 29:
11759                       case 30:
11760                       case 31:  /* These are ok.  */
11761                         break;
11762
11763                       default:  /* The rest must be changed to 28.  */
11764                         imm_expr.X_add_number = 28;
11765                         break;
11766                       }
11767                   INSERT_OPERAND (mips_opts.micromips,
11768                                   CACHE, *ip, imm_expr.X_add_number);
11769                   break;
11770                 case 'h':
11771                   INSERT_OPERAND (mips_opts.micromips,
11772                                   PREFX, *ip, imm_expr.X_add_number);
11773                   break;
11774                 case '1':
11775                   INSERT_OPERAND (mips_opts.micromips,
11776                                   STYPE, *ip, imm_expr.X_add_number);
11777                   break;
11778                 }
11779               imm_expr.X_op = O_absent;
11780               s = expr_end;
11781               continue;
11782
11783             case 'c':           /* BREAK code.  */
11784               {
11785                 unsigned long mask = (mips_opts.micromips
11786                                       ? MICROMIPSOP_MASK_CODE
11787                                       : OP_MASK_CODE);
11788
11789                 my_getExpression (&imm_expr, s);
11790                 check_absolute_expr (ip, &imm_expr);
11791                 if ((unsigned long) imm_expr.X_add_number > mask)
11792                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11793                            ip->insn_mo->name,
11794                            mask, (unsigned long) imm_expr.X_add_number);
11795                 INSERT_OPERAND (mips_opts.micromips,
11796                                 CODE, *ip, imm_expr.X_add_number);
11797                 imm_expr.X_op = O_absent;
11798                 s = expr_end;
11799               }
11800               continue;
11801
11802             case 'q':           /* Lower BREAK code.  */
11803               {
11804                 unsigned long mask = (mips_opts.micromips
11805                                       ? MICROMIPSOP_MASK_CODE2
11806                                       : OP_MASK_CODE2);
11807
11808                 my_getExpression (&imm_expr, s);
11809                 check_absolute_expr (ip, &imm_expr);
11810                 if ((unsigned long) imm_expr.X_add_number > mask)
11811                   as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11812                            ip->insn_mo->name,
11813                            mask, (unsigned long) imm_expr.X_add_number);
11814                 INSERT_OPERAND (mips_opts.micromips,
11815                                 CODE2, *ip, imm_expr.X_add_number);
11816                 imm_expr.X_op = O_absent;
11817                 s = expr_end;
11818               }
11819               continue;
11820
11821             case 'B':           /* 20- or 10-bit syscall/break/wait code.  */
11822               {
11823                 unsigned long mask = (mips_opts.micromips
11824                                       ? MICROMIPSOP_MASK_CODE10
11825                                       : OP_MASK_CODE20);
11826
11827                 my_getExpression (&imm_expr, s);
11828                 check_absolute_expr (ip, &imm_expr);
11829                 if ((unsigned long) imm_expr.X_add_number > mask)
11830                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11831                            ip->insn_mo->name,
11832                            mask, (unsigned long) imm_expr.X_add_number);
11833                 if (mips_opts.micromips)
11834                   INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11835                 else
11836                   INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11837                 imm_expr.X_op = O_absent;
11838                 s = expr_end;
11839               }
11840               continue;
11841
11842             case 'C':           /* 25- or 23-bit coprocessor code.  */
11843               {
11844                 unsigned long mask = (mips_opts.micromips
11845                                       ? MICROMIPSOP_MASK_COPZ
11846                                       : OP_MASK_COPZ);
11847
11848                 my_getExpression (&imm_expr, s);
11849                 check_absolute_expr (ip, &imm_expr);
11850                 if ((unsigned long) imm_expr.X_add_number > mask)
11851                   as_warn (_("Coproccesor code > %u bits (%lu)"),
11852                            mips_opts.micromips ? 23U : 25U,
11853                            (unsigned long) imm_expr.X_add_number);
11854                 INSERT_OPERAND (mips_opts.micromips,
11855                                 COPZ, *ip, imm_expr.X_add_number);
11856                 imm_expr.X_op = O_absent;
11857                 s = expr_end;
11858               }
11859               continue;
11860
11861             case 'J':           /* 19-bit WAIT code.  */
11862               gas_assert (!mips_opts.micromips);
11863               my_getExpression (&imm_expr, s);
11864               check_absolute_expr (ip, &imm_expr);
11865               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
11866                 {
11867                   as_warn (_("Illegal 19-bit code (%lu)"),
11868                            (unsigned long) imm_expr.X_add_number);
11869                   imm_expr.X_add_number &= OP_MASK_CODE19;
11870                 }
11871               INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
11872               imm_expr.X_op = O_absent;
11873               s = expr_end;
11874               continue;
11875
11876             case 'P':           /* Performance register.  */
11877               gas_assert (!mips_opts.micromips);
11878               my_getExpression (&imm_expr, s);
11879               check_absolute_expr (ip, &imm_expr);
11880               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
11881                 as_warn (_("Invalid performance register (%lu)"),
11882                          (unsigned long) imm_expr.X_add_number);
11883               if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
11884                 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
11885                 as_warn (_("Invalid performance register (%lu)"),
11886                   (unsigned long) imm_expr.X_add_number);
11887               INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
11888               imm_expr.X_op = O_absent;
11889               s = expr_end;
11890               continue;
11891
11892             case 'G':           /* Coprocessor destination register.  */
11893               {
11894                 unsigned long opcode = ip->insn_opcode;
11895                 unsigned long mask;
11896                 unsigned int types;
11897                 int cop0;
11898
11899                 if (mips_opts.micromips)
11900                   {
11901                     mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11902                              | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11903                              | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11904                     opcode &= mask;
11905                     switch (opcode)
11906                       {
11907                       case 0x000000fc:                          /* mfc0  */
11908                       case 0x000002fc:                          /* mtc0  */
11909                       case 0x580000fc:                          /* dmfc0 */
11910                       case 0x580002fc:                          /* dmtc0 */
11911                         cop0 = 1;
11912                         break;
11913                       default:
11914                         cop0 = 0;
11915                         break;
11916                       }
11917                   }
11918                 else
11919                   {
11920                     opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11921                     cop0 = opcode == OP_OP_COP0;
11922                   }
11923                 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11924                 ok = reg_lookup (&s, types, &regno);
11925                 if (mips_opts.micromips)
11926                   INSERT_OPERAND (1, RS, *ip, regno);
11927                 else
11928                   INSERT_OPERAND (0, RD, *ip, regno);
11929                 if (ok)
11930                   {
11931                     lastregno = regno;
11932                     continue;
11933                   }
11934               }
11935               break;
11936
11937             case 'y':           /* ALNV.PS source register.  */
11938               gas_assert (mips_opts.micromips);
11939               goto do_reg;
11940             case 'x':           /* Ignore register name.  */
11941             case 'U':           /* Destination register (CLO/CLZ).  */
11942             case 'g':           /* Coprocessor destination register.  */
11943               gas_assert (!mips_opts.micromips);
11944             case 'b':           /* Base register.  */
11945             case 'd':           /* Destination register.  */
11946             case 's':           /* Source register.  */
11947             case 't':           /* Target register.  */
11948             case 'r':           /* Both target and source.  */
11949             case 'v':           /* Both dest and source.  */
11950             case 'w':           /* Both dest and target.  */
11951             case 'E':           /* Coprocessor target register.  */
11952             case 'K':           /* RDHWR destination register.  */
11953             case 'z':           /* Must be zero register.  */
11954             do_reg:
11955               s_reset = s;
11956               if (*args == 'E' || *args == 'K')
11957                 ok = reg_lookup (&s, RTYPE_NUM, &regno);
11958               else
11959                 {
11960                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
11961                   if (regno == AT && mips_opts.at)
11962                     {
11963                       if (mips_opts.at == ATREG)
11964                         as_warn (_("Used $at without \".set noat\""));
11965                       else
11966                         as_warn (_("Used $%u with \".set at=$%u\""),
11967                                  regno, mips_opts.at);
11968                     }
11969                 }
11970               if (ok)
11971                 {
11972                   c = *args;
11973                   if (*s == ' ')
11974                     ++s;
11975                   if (args[1] != *s)
11976                     {
11977                       if (c == 'r' || c == 'v' || c == 'w')
11978                         {
11979                           regno = lastregno;
11980                           s = s_reset;
11981                           ++args;
11982                         }
11983                     }
11984                   /* 'z' only matches $0.  */
11985                   if (c == 'z' && regno != 0)
11986                     break;
11987
11988                   if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
11989                     {
11990                       if (regno == lastregno)
11991                         {
11992                           insn_error
11993                             = _("Source and destination must be different");
11994                           continue;
11995                         }
11996                       if (regno == 31 && lastregno == 0xffffffff)
11997                         {
11998                           insn_error
11999                             = _("A destination register must be supplied");
12000                           continue;
12001                         }
12002                     }
12003                   /* Now that we have assembled one operand, we use the args
12004                      string to figure out where it goes in the instruction.  */
12005                   switch (c)
12006                     {
12007                     case 'r':
12008                     case 's':
12009                     case 'v':
12010                     case 'b':
12011                       INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
12012                       break;
12013
12014                     case 'K':
12015                       if (mips_opts.micromips)
12016                         INSERT_OPERAND (1, RS, *ip, regno);
12017                       else
12018                         INSERT_OPERAND (0, RD, *ip, regno);
12019                       break;
12020
12021                     case 'd':
12022                     case 'g':
12023                       INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
12024                       break;
12025
12026                     case 'U':
12027                       gas_assert (!mips_opts.micromips);
12028                       INSERT_OPERAND (0, RD, *ip, regno);
12029                       INSERT_OPERAND (0, RT, *ip, regno);
12030                       break;
12031
12032                     case 'w':
12033                     case 't':
12034                     case 'E':
12035                       INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12036                       break;
12037
12038                     case 'y':
12039                       gas_assert (mips_opts.micromips);
12040                       INSERT_OPERAND (1, RS3, *ip, regno);
12041                       break;
12042
12043                     case 'x':
12044                       /* This case exists because on the r3000 trunc
12045                          expands into a macro which requires a gp
12046                          register.  On the r6000 or r4000 it is
12047                          assembled into a single instruction which
12048                          ignores the register.  Thus the insn version
12049                          is MIPS_ISA2 and uses 'x', and the macro
12050                          version is MIPS_ISA1 and uses 't'.  */
12051                       break;
12052
12053                     case 'z':
12054                       /* This case is for the div instruction, which
12055                          acts differently if the destination argument
12056                          is $0.  This only matches $0, and is checked
12057                          outside the switch.  */
12058                       break;
12059                     }
12060                   lastregno = regno;
12061                   continue;
12062                 }
12063               switch (*args++)
12064                 {
12065                 case 'r':
12066                 case 'v':
12067                   INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
12068                   continue;
12069
12070                 case 'w':
12071                   INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
12072                   continue;
12073                 }
12074               break;
12075
12076             case 'O':           /* MDMX alignment immediate constant.  */
12077               gas_assert (!mips_opts.micromips);
12078               my_getExpression (&imm_expr, s);
12079               check_absolute_expr (ip, &imm_expr);
12080               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12081                 as_warn (_("Improper align amount (%ld), using low bits"),
12082                          (long) imm_expr.X_add_number);
12083               INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12084               imm_expr.X_op = O_absent;
12085               s = expr_end;
12086               continue;
12087
12088             case 'Q':           /* MDMX vector, element sel, or const.  */
12089               if (s[0] != '$')
12090                 {
12091                   /* MDMX Immediate.  */
12092                   gas_assert (!mips_opts.micromips);
12093                   my_getExpression (&imm_expr, s);
12094                   check_absolute_expr (ip, &imm_expr);
12095                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12096                     as_warn (_("Invalid MDMX Immediate (%ld)"),
12097                              (long) imm_expr.X_add_number);
12098                   INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12099                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12100                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12101                   else
12102                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12103                   imm_expr.X_op = O_absent;
12104                   s = expr_end;
12105                   continue;
12106                 }
12107               /* Not MDMX Immediate.  Fall through.  */
12108             case 'X':           /* MDMX destination register.  */
12109             case 'Y':           /* MDMX source register.  */
12110             case 'Z':           /* MDMX target register.  */
12111               is_mdmx = 1;
12112             case 'W':
12113               gas_assert (!mips_opts.micromips);
12114             case 'D':           /* Floating point destination register.  */
12115             case 'S':           /* Floating point source register.  */
12116             case 'T':           /* Floating point target register.  */
12117             case 'R':           /* Floating point source register.  */
12118             case 'V':
12119               rtype = RTYPE_FPU;
12120               if (is_mdmx
12121                   || (mips_opts.ase_mdmx
12122                       && (ip->insn_mo->pinfo & FP_D)
12123                       && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12124                                                 | INSN_COPROC_MEMORY_DELAY
12125                                                 | INSN_LOAD_COPROC_DELAY
12126                                                 | INSN_LOAD_MEMORY_DELAY
12127                                                 | INSN_STORE_MEMORY))))
12128                 rtype |= RTYPE_VEC;
12129               s_reset = s;
12130               if (reg_lookup (&s, rtype, &regno))
12131                 {
12132                   if ((regno & 1) != 0
12133                       && HAVE_32BIT_FPRS
12134                       && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12135                     as_warn (_("Float register should be even, was %d"),
12136                              regno);
12137
12138                   c = *args;
12139                   if (*s == ' ')
12140                     ++s;
12141                   if (args[1] != *s)
12142                     {
12143                       if (c == 'V' || c == 'W')
12144                         {
12145                           regno = lastregno;
12146                           s = s_reset;
12147                           ++args;
12148                         }
12149                     }
12150                   switch (c)
12151                     {
12152                     case 'D':
12153                     case 'X':
12154                       INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12155                       break;
12156
12157                     case 'V':
12158                     case 'S':
12159                     case 'Y':
12160                       INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12161                       break;
12162
12163                     case 'Q':
12164                       /* This is like 'Z', but also needs to fix the MDMX
12165                          vector/scalar select bits.  Note that the
12166                          scalar immediate case is handled above.  */
12167                       if (*s == '[')
12168                         {
12169                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12170                           int max_el = (is_qh ? 3 : 7);
12171                           s++;
12172                           my_getExpression(&imm_expr, s);
12173                           check_absolute_expr (ip, &imm_expr);
12174                           s = expr_end;
12175                           if (imm_expr.X_add_number > max_el)
12176                             as_bad (_("Bad element selector %ld"),
12177                                     (long) imm_expr.X_add_number);
12178                           imm_expr.X_add_number &= max_el;
12179                           ip->insn_opcode |= (imm_expr.X_add_number
12180                                               << (OP_SH_VSEL +
12181                                                   (is_qh ? 2 : 1)));
12182                           imm_expr.X_op = O_absent;
12183                           if (*s != ']')
12184                             as_warn (_("Expecting ']' found '%s'"), s);
12185                           else
12186                             s++;
12187                         }
12188                       else
12189                         {
12190                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12191                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12192                                                 << OP_SH_VSEL);
12193                           else
12194                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12195                                                 OP_SH_VSEL);
12196                         }
12197                       /* Fall through.  */
12198                     case 'W':
12199                     case 'T':
12200                     case 'Z':
12201                       INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12202                       break;
12203
12204                     case 'R':
12205                       INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12206                       break;
12207                     }
12208                   lastregno = regno;
12209                   continue;
12210                 }
12211
12212               switch (*args++)
12213                 {
12214                 case 'V':
12215                   INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12216                   continue;
12217
12218                 case 'W':
12219                   INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12220                   continue;
12221                 }
12222               break;
12223
12224             case 'I':
12225               my_getExpression (&imm_expr, s);
12226               if (imm_expr.X_op != O_big
12227                   && imm_expr.X_op != O_constant)
12228                 insn_error = _("absolute expression required");
12229               if (HAVE_32BIT_GPRS)
12230                 normalize_constant_expr (&imm_expr);
12231               s = expr_end;
12232               continue;
12233
12234             case 'A':
12235               my_getExpression (&offset_expr, s);
12236               normalize_address_expr (&offset_expr);
12237               *imm_reloc = BFD_RELOC_32;
12238               s = expr_end;
12239               continue;
12240
12241             case 'F':
12242             case 'L':
12243             case 'f':
12244             case 'l':
12245               {
12246                 int f64;
12247                 int using_gprs;
12248                 char *save_in;
12249                 char *err;
12250                 unsigned char temp[8];
12251                 int len;
12252                 unsigned int length;
12253                 segT seg;
12254                 subsegT subseg;
12255                 char *p;
12256
12257                 /* These only appear as the last operand in an
12258                    instruction, and every instruction that accepts
12259                    them in any variant accepts them in all variants.
12260                    This means we don't have to worry about backing out
12261                    any changes if the instruction does not match.
12262
12263                    The difference between them is the size of the
12264                    floating point constant and where it goes.  For 'F'
12265                    and 'L' the constant is 64 bits; for 'f' and 'l' it
12266                    is 32 bits.  Where the constant is placed is based
12267                    on how the MIPS assembler does things:
12268                     F -- .rdata
12269                     L -- .lit8
12270                     f -- immediate value
12271                     l -- .lit4
12272
12273                     The .lit4 and .lit8 sections are only used if
12274                     permitted by the -G argument.
12275
12276                     The code below needs to know whether the target register
12277                     is 32 or 64 bits wide.  It relies on the fact 'f' and
12278                     'F' are used with GPR-based instructions and 'l' and
12279                     'L' are used with FPR-based instructions.  */
12280
12281                 f64 = *args == 'F' || *args == 'L';
12282                 using_gprs = *args == 'F' || *args == 'f';
12283
12284                 save_in = input_line_pointer;
12285                 input_line_pointer = s;
12286                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12287                 length = len;
12288                 s = input_line_pointer;
12289                 input_line_pointer = save_in;
12290                 if (err != NULL && *err != '\0')
12291                   {
12292                     as_bad (_("Bad floating point constant: %s"), err);
12293                     memset (temp, '\0', sizeof temp);
12294                     length = f64 ? 8 : 4;
12295                   }
12296
12297                 gas_assert (length == (unsigned) (f64 ? 8 : 4));
12298
12299                 if (*args == 'f'
12300                     || (*args == 'l'
12301                         && (g_switch_value < 4
12302                             || (temp[0] == 0 && temp[1] == 0)
12303                             || (temp[2] == 0 && temp[3] == 0))))
12304                   {
12305                     imm_expr.X_op = O_constant;
12306                     if (!target_big_endian)
12307                       imm_expr.X_add_number = bfd_getl32 (temp);
12308                     else
12309                       imm_expr.X_add_number = bfd_getb32 (temp);
12310                   }
12311                 else if (length > 4
12312                          && !mips_disable_float_construction
12313                          /* Constants can only be constructed in GPRs and
12314                             copied to FPRs if the GPRs are at least as wide
12315                             as the FPRs.  Force the constant into memory if
12316                             we are using 64-bit FPRs but the GPRs are only
12317                             32 bits wide.  */
12318                          && (using_gprs
12319                              || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12320                          && ((temp[0] == 0 && temp[1] == 0)
12321                              || (temp[2] == 0 && temp[3] == 0))
12322                          && ((temp[4] == 0 && temp[5] == 0)
12323                              || (temp[6] == 0 && temp[7] == 0)))
12324                   {
12325                     /* The value is simple enough to load with a couple of
12326                        instructions.  If using 32-bit registers, set
12327                        imm_expr to the high order 32 bits and offset_expr to
12328                        the low order 32 bits.  Otherwise, set imm_expr to
12329                        the entire 64 bit constant.  */
12330                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12331                       {
12332                         imm_expr.X_op = O_constant;
12333                         offset_expr.X_op = O_constant;
12334                         if (!target_big_endian)
12335                           {
12336                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
12337                             offset_expr.X_add_number = bfd_getl32 (temp);
12338                           }
12339                         else
12340                           {
12341                             imm_expr.X_add_number = bfd_getb32 (temp);
12342                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
12343                           }
12344                         if (offset_expr.X_add_number == 0)
12345                           offset_expr.X_op = O_absent;
12346                       }
12347                     else if (sizeof (imm_expr.X_add_number) > 4)
12348                       {
12349                         imm_expr.X_op = O_constant;
12350                         if (!target_big_endian)
12351                           imm_expr.X_add_number = bfd_getl64 (temp);
12352                         else
12353                           imm_expr.X_add_number = bfd_getb64 (temp);
12354                       }
12355                     else
12356                       {
12357                         imm_expr.X_op = O_big;
12358                         imm_expr.X_add_number = 4;
12359                         if (!target_big_endian)
12360                           {
12361                             generic_bignum[0] = bfd_getl16 (temp);
12362                             generic_bignum[1] = bfd_getl16 (temp + 2);
12363                             generic_bignum[2] = bfd_getl16 (temp + 4);
12364                             generic_bignum[3] = bfd_getl16 (temp + 6);
12365                           }
12366                         else
12367                           {
12368                             generic_bignum[0] = bfd_getb16 (temp + 6);
12369                             generic_bignum[1] = bfd_getb16 (temp + 4);
12370                             generic_bignum[2] = bfd_getb16 (temp + 2);
12371                             generic_bignum[3] = bfd_getb16 (temp);
12372                           }
12373                       }
12374                   }
12375                 else
12376                   {
12377                     const char *newname;
12378                     segT new_seg;
12379
12380                     /* Switch to the right section.  */
12381                     seg = now_seg;
12382                     subseg = now_subseg;
12383                     switch (*args)
12384                       {
12385                       default: /* unused default case avoids warnings.  */
12386                       case 'L':
12387                         newname = RDATA_SECTION_NAME;
12388                         if (g_switch_value >= 8)
12389                           newname = ".lit8";
12390                         break;
12391                       case 'F':
12392                         newname = RDATA_SECTION_NAME;
12393                         break;
12394                       case 'l':
12395                         gas_assert (g_switch_value >= 4);
12396                         newname = ".lit4";
12397                         break;
12398                       }
12399                     new_seg = subseg_new (newname, (subsegT) 0);
12400                     if (IS_ELF)
12401                       bfd_set_section_flags (stdoutput, new_seg,
12402                                              (SEC_ALLOC
12403                                               | SEC_LOAD
12404                                               | SEC_READONLY
12405                                               | SEC_DATA));
12406                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
12407                     if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
12408                       record_alignment (new_seg, 4);
12409                     else
12410                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
12411                     if (seg == now_seg)
12412                       as_bad (_("Can't use floating point insn in this section"));
12413
12414                     /* Set the argument to the current address in the
12415                        section.  */
12416                     offset_expr.X_op = O_symbol;
12417                     offset_expr.X_add_symbol = symbol_temp_new_now ();
12418                     offset_expr.X_add_number = 0;
12419
12420                     /* Put the floating point number into the section.  */
12421                     p = frag_more ((int) length);
12422                     memcpy (p, temp, length);
12423
12424                     /* Switch back to the original section.  */
12425                     subseg_set (seg, subseg);
12426                   }
12427               }
12428               continue;
12429
12430             case 'i':           /* 16-bit unsigned immediate.  */
12431             case 'j':           /* 16-bit signed immediate.  */
12432               *imm_reloc = BFD_RELOC_LO16;
12433               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12434                 {
12435                   int more;
12436                   offsetT minval, maxval;
12437
12438                   more = (insn + 1 < past
12439                           && strcmp (insn->name, insn[1].name) == 0);
12440
12441                   /* If the expression was written as an unsigned number,
12442                      only treat it as signed if there are no more
12443                      alternatives.  */
12444                   if (more
12445                       && *args == 'j'
12446                       && sizeof (imm_expr.X_add_number) <= 4
12447                       && imm_expr.X_op == O_constant
12448                       && imm_expr.X_add_number < 0
12449                       && imm_expr.X_unsigned
12450                       && HAVE_64BIT_GPRS)
12451                     break;
12452
12453                   /* For compatibility with older assemblers, we accept
12454                      0x8000-0xffff as signed 16-bit numbers when only
12455                      signed numbers are allowed.  */
12456                   if (*args == 'i')
12457                     minval = 0, maxval = 0xffff;
12458                   else if (more)
12459                     minval = -0x8000, maxval = 0x7fff;
12460                   else
12461                     minval = -0x8000, maxval = 0xffff;
12462
12463                   if (imm_expr.X_op != O_constant
12464                       || imm_expr.X_add_number < minval
12465                       || imm_expr.X_add_number > maxval)
12466                     {
12467                       if (more)
12468                         break;
12469                       if (imm_expr.X_op == O_constant
12470                           || imm_expr.X_op == O_big)
12471                         as_bad (_("Expression out of range"));
12472                     }
12473                 }
12474               s = expr_end;
12475               continue;
12476
12477             case 'o':           /* 16-bit offset.  */
12478               offset_reloc[0] = BFD_RELOC_LO16;
12479               offset_reloc[1] = BFD_RELOC_UNUSED;
12480               offset_reloc[2] = BFD_RELOC_UNUSED;
12481
12482               /* Check whether there is only a single bracketed expression
12483                  left.  If so, it must be the base register and the
12484                  constant must be zero.  */
12485               if (*s == '(' && strchr (s + 1, '(') == 0)
12486                 {
12487                   offset_expr.X_op = O_constant;
12488                   offset_expr.X_add_number = 0;
12489                   continue;
12490                 }
12491
12492               /* If this value won't fit into a 16 bit offset, then go
12493                  find a macro that will generate the 32 bit offset
12494                  code pattern.  */
12495               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12496                   && (offset_expr.X_op != O_constant
12497                       || offset_expr.X_add_number >= 0x8000
12498                       || offset_expr.X_add_number < -0x8000))
12499                 break;
12500
12501               s = expr_end;
12502               continue;
12503
12504             case 'p':           /* PC-relative offset.  */
12505               *offset_reloc = BFD_RELOC_16_PCREL_S2;
12506               my_getExpression (&offset_expr, s);
12507               s = expr_end;
12508               continue;
12509
12510             case 'u':           /* Upper 16 bits.  */
12511               *imm_reloc = BFD_RELOC_LO16;
12512               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12513                   && imm_expr.X_op == O_constant
12514                   && (imm_expr.X_add_number < 0
12515                       || imm_expr.X_add_number >= 0x10000))
12516                 as_bad (_("lui expression (%lu) not in range 0..65535"),
12517                         (unsigned long) imm_expr.X_add_number);
12518               s = expr_end;
12519               continue;
12520
12521             case 'a':           /* 26-bit address.  */
12522               *offset_reloc = BFD_RELOC_MIPS_JMP;
12523               my_getExpression (&offset_expr, s);
12524               s = expr_end;
12525               continue;
12526
12527             case 'N':           /* 3-bit branch condition code.  */
12528             case 'M':           /* 3-bit compare condition code.  */
12529               rtype = RTYPE_CCC;
12530               if (ip->insn_mo->pinfo & (FP_D | FP_S))
12531                 rtype |= RTYPE_FCC;
12532               if (!reg_lookup (&s, rtype, &regno))
12533                 break;
12534               if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12535                    || strcmp (str + strlen (str) - 5, "any2f") == 0
12536                    || strcmp (str + strlen (str) - 5, "any2t") == 0)
12537                   && (regno & 1) != 0)
12538                 as_warn (_("Condition code register should be even for %s, "
12539                            "was %d"),
12540                          str, regno);
12541               if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12542                    || strcmp (str + strlen (str) - 5, "any4t") == 0)
12543                   && (regno & 3) != 0)
12544                 as_warn (_("Condition code register should be 0 or 4 for %s, "
12545                            "was %d"),
12546                          str, regno);
12547               if (*args == 'N')
12548                 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12549               else
12550                 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12551               continue;
12552
12553             case 'H':
12554               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12555                 s += 2;
12556               if (ISDIGIT (*s))
12557                 {
12558                   c = 0;
12559                   do
12560                     {
12561                       c *= 10;
12562                       c += *s - '0';
12563                       ++s;
12564                     }
12565                   while (ISDIGIT (*s));
12566                 }
12567               else
12568                 c = 8; /* Invalid sel value.  */
12569
12570               if (c > 7)
12571                 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12572               INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12573               continue;
12574
12575             case 'e':
12576               gas_assert (!mips_opts.micromips);
12577               /* Must be at least one digit.  */
12578               my_getExpression (&imm_expr, s);
12579               check_absolute_expr (ip, &imm_expr);
12580
12581               if ((unsigned long) imm_expr.X_add_number
12582                   > (unsigned long) OP_MASK_VECBYTE)
12583                 {
12584                   as_bad (_("bad byte vector index (%ld)"),
12585                            (long) imm_expr.X_add_number);
12586                   imm_expr.X_add_number = 0;
12587                 }
12588
12589               INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12590               imm_expr.X_op = O_absent;
12591               s = expr_end;
12592               continue;
12593
12594             case '%':
12595               gas_assert (!mips_opts.micromips);
12596               my_getExpression (&imm_expr, s);
12597               check_absolute_expr (ip, &imm_expr);
12598
12599               if ((unsigned long) imm_expr.X_add_number
12600                   > (unsigned long) OP_MASK_VECALIGN)
12601                 {
12602                   as_bad (_("bad byte vector index (%ld)"),
12603                            (long) imm_expr.X_add_number);
12604                   imm_expr.X_add_number = 0;
12605                 }
12606
12607               INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12608               imm_expr.X_op = O_absent;
12609               s = expr_end;
12610               continue;
12611
12612             case 'm':           /* Opcode extension character.  */
12613               gas_assert (mips_opts.micromips);
12614               c = *++args;
12615               switch (c)
12616                 {
12617                 case 'r':
12618                   if (strncmp (s, "$pc", 3) == 0)
12619                     {
12620                       s += 3;
12621                       continue;
12622                     }
12623                   break;
12624
12625                 case 'a':
12626                 case 'b':
12627                 case 'c':
12628                 case 'd':
12629                 case 'e':
12630                 case 'f':
12631                 case 'g':
12632                 case 'h':
12633                 case 'i':
12634                 case 'j':
12635                 case 'l':
12636                 case 'm':
12637                 case 'n':
12638                 case 'p':
12639                 case 'q':
12640                 case 's':
12641                 case 't':
12642                 case 'x':
12643                 case 'y':
12644                 case 'z':
12645                   s_reset = s;
12646                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12647                   if (regno == AT && mips_opts.at)
12648                     {
12649                       if (mips_opts.at == ATREG)
12650                         as_warn (_("Used $at without \".set noat\""));
12651                       else
12652                         as_warn (_("Used $%u with \".set at=$%u\""),
12653                                  regno, mips_opts.at);
12654                     }
12655                   if (!ok)
12656                     {
12657                       if (c == 'c')
12658                         {
12659                           gas_assert (args[1] == ',');
12660                           regno = lastregno;
12661                           ++args;
12662                         }
12663                       else if (c == 't')
12664                         {
12665                           gas_assert (args[1] == ',');
12666                           ++args;
12667                           continue;                     /* Nothing to do.  */
12668                         }
12669                       else
12670                         break;
12671                     }
12672
12673                   if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12674                     {
12675                       if (regno == lastregno)
12676                         {
12677                           insn_error
12678                             = _("Source and destination must be different");
12679                           continue;
12680                         }
12681                       if (regno == 31 && lastregno == 0xffffffff)
12682                         {
12683                           insn_error
12684                             = _("A destination register must be supplied");
12685                           continue;
12686                         }
12687                     }
12688
12689                   if (*s == ' ')
12690                     ++s;
12691                   if (args[1] != *s)
12692                     {
12693                       if (c == 'e')
12694                         {
12695                           gas_assert (args[1] == ',');
12696                           regno = lastregno;
12697                           s = s_reset;
12698                           ++args;
12699                         }
12700                       else if (c == 't')
12701                         {
12702                           gas_assert (args[1] == ',');
12703                           s = s_reset;
12704                           ++args;
12705                           continue;                     /* Nothing to do.  */
12706                         }
12707                     }
12708
12709                   /* Make sure regno is the same as lastregno.  */
12710                   if (c == 't' && regno != lastregno)
12711                     break;
12712
12713                   /* Make sure regno is the same as destregno.  */
12714                   if (c == 'x' && regno != destregno)
12715                     break;
12716
12717                   /* We need to save regno, before regno maps to the
12718                      microMIPS register encoding.  */
12719                   lastregno = regno;
12720
12721                   if (c == 'f')
12722                     destregno = regno;
12723
12724                   switch (c)
12725                     {
12726                       case 'a':
12727                         if (regno != GP)
12728                           regno = ILLEGAL_REG;
12729                         break;
12730
12731                       case 'b':
12732                         regno = mips32_to_micromips_reg_b_map[regno];
12733                         break;
12734
12735                       case 'c':
12736                         regno = mips32_to_micromips_reg_c_map[regno];
12737                         break;
12738
12739                       case 'd':
12740                         regno = mips32_to_micromips_reg_d_map[regno];
12741                         break;
12742
12743                       case 'e':
12744                         regno = mips32_to_micromips_reg_e_map[regno];
12745                         break;
12746
12747                       case 'f':
12748                         regno = mips32_to_micromips_reg_f_map[regno];
12749                         break;
12750
12751                       case 'g':
12752                         regno = mips32_to_micromips_reg_g_map[regno];
12753                         break;
12754
12755                       case 'h':
12756                         regno = mips32_to_micromips_reg_h_map[regno];
12757                         break;
12758
12759                       case 'i':
12760                         switch (EXTRACT_OPERAND (1, MI, *ip))
12761                           {
12762                             case 4:
12763                               if (regno == 21)
12764                                 regno = 3;
12765                               else if (regno == 22)
12766                                 regno = 4;
12767                               else if (regno == 5)
12768                                 regno = 5;
12769                               else if (regno == 6)
12770                                 regno = 6;
12771                               else if (regno == 7)
12772                                 regno = 7;
12773                               else
12774                                 regno = ILLEGAL_REG;
12775                               break;
12776
12777                             case 5:
12778                               if (regno == 6)
12779                                 regno = 0;
12780                               else if (regno == 7)
12781                                 regno = 1;
12782                               else
12783                                 regno = ILLEGAL_REG;
12784                               break;
12785
12786                             case 6:
12787                               if (regno == 7)
12788                                 regno = 2;
12789                               else
12790                                 regno = ILLEGAL_REG;
12791                               break;
12792
12793                             default:
12794                               regno = ILLEGAL_REG;
12795                               break;
12796                           }
12797                         break;
12798
12799                       case 'l':
12800                         regno = mips32_to_micromips_reg_l_map[regno];
12801                         break;
12802
12803                       case 'm':
12804                         regno = mips32_to_micromips_reg_m_map[regno];
12805                         break;
12806
12807                       case 'n':
12808                         regno = mips32_to_micromips_reg_n_map[regno];
12809                         break;
12810
12811                       case 'q':
12812                         regno = mips32_to_micromips_reg_q_map[regno];
12813                         break;
12814
12815                       case 's':
12816                         if (regno != SP)
12817                           regno = ILLEGAL_REG;
12818                         break;
12819
12820                       case 'y':
12821                         if (regno != 31)
12822                           regno = ILLEGAL_REG;
12823                         break;
12824
12825                       case 'z':
12826                         if (regno != ZERO)
12827                           regno = ILLEGAL_REG;
12828                         break;
12829
12830                       case 'j': /* Do nothing.  */
12831                       case 'p':
12832                       case 't':
12833                       case 'x':
12834                         break;
12835
12836                       default:
12837                         abort ();
12838                     }
12839
12840                   if (regno == ILLEGAL_REG)
12841                     break;
12842
12843                   switch (c)
12844                     {
12845                       case 'b':
12846                         INSERT_OPERAND (1, MB, *ip, regno);
12847                         break;
12848
12849                       case 'c':
12850                         INSERT_OPERAND (1, MC, *ip, regno);
12851                         break;
12852
12853                       case 'd':
12854                         INSERT_OPERAND (1, MD, *ip, regno);
12855                         break;
12856
12857                       case 'e':
12858                         INSERT_OPERAND (1, ME, *ip, regno);
12859                         break;
12860
12861                       case 'f':
12862                         INSERT_OPERAND (1, MF, *ip, regno);
12863                         break;
12864
12865                       case 'g':
12866                         INSERT_OPERAND (1, MG, *ip, regno);
12867                         break;
12868
12869                       case 'h':
12870                         INSERT_OPERAND (1, MH, *ip, regno);
12871                         break;
12872
12873                       case 'i':
12874                         INSERT_OPERAND (1, MI, *ip, regno);
12875                         break;
12876
12877                       case 'j':
12878                         INSERT_OPERAND (1, MJ, *ip, regno);
12879                         break;
12880
12881                       case 'l':
12882                         INSERT_OPERAND (1, ML, *ip, regno);
12883                         break;
12884
12885                       case 'm':
12886                         INSERT_OPERAND (1, MM, *ip, regno);
12887                         break;
12888
12889                       case 'n':
12890                         INSERT_OPERAND (1, MN, *ip, regno);
12891                         break;
12892
12893                       case 'p':
12894                         INSERT_OPERAND (1, MP, *ip, regno);
12895                         break;
12896
12897                       case 'q':
12898                         INSERT_OPERAND (1, MQ, *ip, regno);
12899                         break;
12900
12901                       case 'a': /* Do nothing.  */
12902                       case 's': /* Do nothing.  */
12903                       case 't': /* Do nothing.  */
12904                       case 'x': /* Do nothing.  */
12905                       case 'y': /* Do nothing.  */
12906                       case 'z': /* Do nothing.  */
12907                         break;
12908
12909                       default:
12910                         abort ();
12911                     }
12912                   continue;
12913
12914                 case 'A':
12915                   {
12916                     bfd_reloc_code_real_type r[3];
12917                     expressionS ep;
12918                     int imm;
12919
12920                     /* Check whether there is only a single bracketed
12921                        expression left.  If so, it must be the base register
12922                        and the constant must be zero.  */
12923                     if (*s == '(' && strchr (s + 1, '(') == 0)
12924                       {
12925                         INSERT_OPERAND (1, IMMA, *ip, 0);
12926                         continue;
12927                       }
12928
12929                     if (my_getSmallExpression (&ep, r, s) > 0
12930                         || !expr_const_in_range (&ep, -64, 64, 2))
12931                       break;
12932
12933                     imm = ep.X_add_number >> 2;
12934                     INSERT_OPERAND (1, IMMA, *ip, imm);
12935                   }
12936                   s = expr_end;
12937                   continue;
12938
12939                 case 'B':
12940                   {
12941                     bfd_reloc_code_real_type r[3];
12942                     expressionS ep;
12943                     int imm;
12944
12945                     if (my_getSmallExpression (&ep, r, s) > 0
12946                         || ep.X_op != O_constant)
12947                       break;
12948
12949                     for (imm = 0; imm < 8; imm++)
12950                       if (micromips_imm_b_map[imm] == ep.X_add_number)
12951                         break;
12952                     if (imm >= 8)
12953                       break;
12954
12955                     INSERT_OPERAND (1, IMMB, *ip, imm);
12956                   }
12957                   s = expr_end;
12958                   continue;
12959
12960                 case 'C':
12961                   {
12962                     bfd_reloc_code_real_type r[3];
12963                     expressionS ep;
12964                     int imm;
12965
12966                     if (my_getSmallExpression (&ep, r, s) > 0
12967                         || ep.X_op != O_constant)
12968                       break;
12969
12970                     for (imm = 0; imm < 16; imm++)
12971                       if (micromips_imm_c_map[imm] == ep.X_add_number)
12972                         break;
12973                     if (imm >= 16)
12974                       break;
12975
12976                     INSERT_OPERAND (1, IMMC, *ip, imm);
12977                   }
12978                   s = expr_end;
12979                   continue;
12980
12981                 case 'D':       /* pc relative offset */
12982                 case 'E':       /* pc relative offset */
12983                   my_getExpression (&offset_expr, s);
12984                   if (offset_expr.X_op == O_register)
12985                     break;
12986
12987                   if (!forced_insn_length)
12988                     *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12989                   else if (c == 'D')
12990                     *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12991                   else
12992                     *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
12993                   s = expr_end;
12994                   continue;
12995
12996                 case 'F':
12997                   {
12998                     bfd_reloc_code_real_type r[3];
12999                     expressionS ep;
13000                     int imm;
13001
13002                     if (my_getSmallExpression (&ep, r, s) > 0
13003                         || !expr_const_in_range (&ep, 0, 16, 0))
13004                       break;
13005
13006                     imm = ep.X_add_number;
13007                     INSERT_OPERAND (1, IMMF, *ip, imm);
13008                   }
13009                   s = expr_end;
13010                   continue;
13011
13012                 case 'G':
13013                   {
13014                     bfd_reloc_code_real_type r[3];
13015                     expressionS ep;
13016                     int imm;
13017
13018                     /* Check whether there is only a single bracketed
13019                        expression left.  If so, it must be the base register
13020                        and the constant must be zero.  */
13021                     if (*s == '(' && strchr (s + 1, '(') == 0)
13022                       {
13023                         INSERT_OPERAND (1, IMMG, *ip, 0);
13024                         continue;
13025                       }
13026
13027                     if (my_getSmallExpression (&ep, r, s) > 0
13028                         || !expr_const_in_range (&ep, -1, 15, 0))
13029                       break;
13030
13031                     imm = ep.X_add_number & 15;
13032                     INSERT_OPERAND (1, IMMG, *ip, imm);
13033                   }
13034                   s = expr_end;
13035                   continue;
13036
13037                 case 'H':
13038                   {
13039                     bfd_reloc_code_real_type r[3];
13040                     expressionS ep;
13041                     int imm;
13042
13043                     /* Check whether there is only a single bracketed
13044                        expression left.  If so, it must be the base register
13045                        and the constant must be zero.  */
13046                     if (*s == '(' && strchr (s + 1, '(') == 0)
13047                       {
13048                         INSERT_OPERAND (1, IMMH, *ip, 0);
13049                         continue;
13050                       }
13051
13052                     if (my_getSmallExpression (&ep, r, s) > 0
13053                         || !expr_const_in_range (&ep, 0, 16, 1))
13054                       break;
13055
13056                     imm = ep.X_add_number >> 1;
13057                     INSERT_OPERAND (1, IMMH, *ip, imm);
13058                   }
13059                   s = expr_end;
13060                   continue;
13061
13062                 case 'I':
13063                   {
13064                     bfd_reloc_code_real_type r[3];
13065                     expressionS ep;
13066                     int imm;
13067
13068                     if (my_getSmallExpression (&ep, r, s) > 0
13069                         || !expr_const_in_range (&ep, -1, 127, 0))
13070                       break;
13071
13072                     imm = ep.X_add_number & 127;
13073                     INSERT_OPERAND (1, IMMI, *ip, imm);
13074                   }
13075                   s = expr_end;
13076                   continue;
13077
13078                 case 'J':
13079                   {
13080                     bfd_reloc_code_real_type r[3];
13081                     expressionS ep;
13082                     int imm;
13083
13084                     /* Check whether there is only a single bracketed
13085                        expression left.  If so, it must be the base register
13086                        and the constant must be zero.  */
13087                     if (*s == '(' && strchr (s + 1, '(') == 0)
13088                       {
13089                         INSERT_OPERAND (1, IMMJ, *ip, 0);
13090                         continue;
13091                       }
13092
13093                     if (my_getSmallExpression (&ep, r, s) > 0
13094                         || !expr_const_in_range (&ep, 0, 16, 2))
13095                       break;
13096
13097                     imm = ep.X_add_number >> 2;
13098                     INSERT_OPERAND (1, IMMJ, *ip, imm);
13099                   }
13100                   s = expr_end;
13101                   continue;
13102
13103                 case 'L':
13104                   {
13105                     bfd_reloc_code_real_type r[3];
13106                     expressionS ep;
13107                     int imm;
13108
13109                     /* Check whether there is only a single bracketed
13110                        expression left.  If so, it must be the base register
13111                        and the constant must be zero.  */
13112                     if (*s == '(' && strchr (s + 1, '(') == 0)
13113                       {
13114                         INSERT_OPERAND (1, IMML, *ip, 0);
13115                         continue;
13116                       }
13117
13118                     if (my_getSmallExpression (&ep, r, s) > 0
13119                         || !expr_const_in_range (&ep, 0, 16, 0))
13120                       break;
13121
13122                     imm = ep.X_add_number;
13123                     INSERT_OPERAND (1, IMML, *ip, imm);
13124                   }
13125                   s = expr_end;
13126                   continue;
13127
13128                 case 'M':
13129                   {
13130                     bfd_reloc_code_real_type r[3];
13131                     expressionS ep;
13132                     int imm;
13133
13134                     if (my_getSmallExpression (&ep, r, s) > 0
13135                         || !expr_const_in_range (&ep, 1, 9, 0))
13136                       break;
13137
13138                     imm = ep.X_add_number & 7;
13139                     INSERT_OPERAND (1, IMMM, *ip, imm);
13140                   }
13141                   s = expr_end;
13142                   continue;
13143
13144                 case 'N':       /* Register list for lwm and swm.  */
13145                   {
13146                     /* A comma-separated list of registers and/or
13147                        dash-separated contiguous ranges including
13148                        both ra and a set of one or more registers
13149                        starting at s0 up to s3 which have to be
13150                        consecutive, e.g.:
13151
13152                        s0, ra
13153                        s0, s1, ra, s2, s3
13154                        s0-s2, ra
13155
13156                        and any permutations of these.  */
13157                     unsigned int reglist;
13158                     int imm;
13159
13160                     if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13161                       break;
13162
13163                     if ((reglist & 0xfff1ffff) != 0x80010000)
13164                       break;
13165
13166                     reglist = (reglist >> 17) & 7;
13167                     reglist += 1;
13168                     if ((reglist & -reglist) != reglist)
13169                       break;
13170
13171                     imm = ffs (reglist) - 1;
13172                     INSERT_OPERAND (1, IMMN, *ip, imm);
13173                   }
13174                   continue;
13175
13176                 case 'O':       /* sdbbp 4-bit code.  */
13177                   {
13178                     bfd_reloc_code_real_type r[3];
13179                     expressionS ep;
13180                     int imm;
13181
13182                     if (my_getSmallExpression (&ep, r, s) > 0
13183                         || !expr_const_in_range (&ep, 0, 16, 0))
13184                       break;
13185
13186                     imm = ep.X_add_number;
13187                     INSERT_OPERAND (1, IMMO, *ip, imm);
13188                   }
13189                   s = expr_end;
13190                   continue;
13191
13192                 case 'P':
13193                   {
13194                     bfd_reloc_code_real_type r[3];
13195                     expressionS ep;
13196                     int imm;
13197
13198                     if (my_getSmallExpression (&ep, r, s) > 0
13199                         || !expr_const_in_range (&ep, 0, 32, 2))
13200                       break;
13201
13202                     imm = ep.X_add_number >> 2;
13203                     INSERT_OPERAND (1, IMMP, *ip, imm);
13204                   }
13205                   s = expr_end;
13206                   continue;
13207
13208                 case 'Q':
13209                   {
13210                     bfd_reloc_code_real_type r[3];
13211                     expressionS ep;
13212                     int imm;
13213
13214                     if (my_getSmallExpression (&ep, r, s) > 0
13215                         || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13216                       break;
13217
13218                     imm = ep.X_add_number >> 2;
13219                     INSERT_OPERAND (1, IMMQ, *ip, imm);
13220                   }
13221                   s = expr_end;
13222                   continue;
13223
13224                 case 'U':
13225                   {
13226                     bfd_reloc_code_real_type r[3];
13227                     expressionS ep;
13228                     int imm;
13229
13230                     /* Check whether there is only a single bracketed
13231                        expression left.  If so, it must be the base register
13232                        and the constant must be zero.  */
13233                     if (*s == '(' && strchr (s + 1, '(') == 0)
13234                       {
13235                         INSERT_OPERAND (1, IMMU, *ip, 0);
13236                         continue;
13237                       }
13238
13239                     if (my_getSmallExpression (&ep, r, s) > 0
13240                         || !expr_const_in_range (&ep, 0, 32, 2))
13241                       break;
13242
13243                     imm = ep.X_add_number >> 2;
13244                     INSERT_OPERAND (1, IMMU, *ip, imm);
13245                   }
13246                   s = expr_end;
13247                   continue;
13248
13249                 case 'W':
13250                   {
13251                     bfd_reloc_code_real_type r[3];
13252                     expressionS ep;
13253                     int imm;
13254
13255                     if (my_getSmallExpression (&ep, r, s) > 0
13256                         || !expr_const_in_range (&ep, 0, 64, 2))
13257                       break;
13258
13259                     imm = ep.X_add_number >> 2;
13260                     INSERT_OPERAND (1, IMMW, *ip, imm);
13261                   }
13262                   s = expr_end;
13263                   continue;
13264
13265                 case 'X':
13266                   {
13267                     bfd_reloc_code_real_type r[3];
13268                     expressionS ep;
13269                     int imm;
13270
13271                     if (my_getSmallExpression (&ep, r, s) > 0
13272                         || !expr_const_in_range (&ep, -8, 8, 0))
13273                       break;
13274
13275                     imm = ep.X_add_number;
13276                     INSERT_OPERAND (1, IMMX, *ip, imm);
13277                   }
13278                   s = expr_end;
13279                   continue;
13280
13281                 case 'Y':
13282                   {
13283                     bfd_reloc_code_real_type r[3];
13284                     expressionS ep;
13285                     int imm;
13286
13287                     if (my_getSmallExpression (&ep, r, s) > 0
13288                         || expr_const_in_range (&ep, -2, 2, 2)
13289                         || !expr_const_in_range (&ep, -258, 258, 2))
13290                       break;
13291
13292                     imm = ep.X_add_number >> 2;
13293                     imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13294                     INSERT_OPERAND (1, IMMY, *ip, imm);
13295                   }
13296                   s = expr_end;
13297                   continue;
13298
13299                 case 'Z':
13300                   {
13301                     bfd_reloc_code_real_type r[3];
13302                     expressionS ep;
13303
13304                     if (my_getSmallExpression (&ep, r, s) > 0
13305                         || !expr_const_in_range (&ep, 0, 1, 0))
13306                       break;
13307                   }
13308                   s = expr_end;
13309                   continue;
13310
13311                 default:
13312                   as_bad (_("Internal error: bad microMIPS opcode "
13313                             "(unknown extension operand type `m%c'): %s %s"),
13314                           *args, insn->name, insn->args);
13315                   /* Further processing is fruitless.  */
13316                   return;
13317                 }
13318               break;
13319
13320             case 'n':           /* Register list for 32-bit lwm and swm.  */
13321               gas_assert (mips_opts.micromips);
13322               {
13323                 /* A comma-separated list of registers and/or
13324                    dash-separated contiguous ranges including
13325                    at least one of ra and a set of one or more
13326                    registers starting at s0 up to s7 and then
13327                    s8 which have to be consecutive, e.g.:
13328
13329                    ra
13330                    s0
13331                    ra, s0, s1, s2
13332                    s0-s8
13333                    s0-s5, ra
13334
13335                    and any permutations of these.  */
13336                 unsigned int reglist;
13337                 int imm;
13338                 int ra;
13339
13340                 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13341                   break;
13342
13343                 if ((reglist & 0x3f00ffff) != 0)
13344                   break;
13345
13346                 ra = (reglist >> 27) & 0x10;
13347                 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13348                 reglist += 1;
13349                 if ((reglist & -reglist) != reglist)
13350                   break;
13351
13352                 imm = (ffs (reglist) - 1) | ra;
13353                 INSERT_OPERAND (1, RT, *ip, imm);
13354                 imm_expr.X_op = O_absent;
13355               }
13356               continue;
13357
13358             case '|':           /* 4-bit trap code.  */
13359               gas_assert (mips_opts.micromips);
13360               my_getExpression (&imm_expr, s);
13361               check_absolute_expr (ip, &imm_expr);
13362               if ((unsigned long) imm_expr.X_add_number
13363                   > MICROMIPSOP_MASK_TRAP)
13364                 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13365                         (unsigned long) imm_expr.X_add_number,
13366                         ip->insn_mo->name);
13367               INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13368               imm_expr.X_op = O_absent;
13369               s = expr_end;
13370               continue;
13371
13372             default:
13373               as_bad (_("Bad char = '%c'\n"), *args);
13374               abort ();
13375             }
13376           break;
13377         }
13378       /* Args don't match.  */
13379       s = argsStart;
13380       insn_error = _("Illegal operands");
13381       if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13382         {
13383           ++insn;
13384           continue;
13385         }
13386       else if (wrong_delay_slot_insns && need_delay_slot_ok)
13387         {
13388           gas_assert (firstinsn);
13389           need_delay_slot_ok = FALSE;
13390           past = insn + 1;
13391           insn = firstinsn;
13392           continue;
13393         }
13394       return;
13395     }
13396 }
13397
13398 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13399
13400 /* This routine assembles an instruction into its binary format when
13401    assembling for the mips16.  As a side effect, it sets one of the
13402    global variables imm_reloc or offset_reloc to the type of relocation
13403    to do if one of the operands is an address expression.  It also sets
13404    forced_insn_length to the resulting instruction size in bytes if the
13405    user explicitly requested a small or extended instruction.  */
13406
13407 static void
13408 mips16_ip (char *str, struct mips_cl_insn *ip)
13409 {
13410   char *s;
13411   const char *args;
13412   struct mips_opcode *insn;
13413   char *argsstart;
13414   unsigned int regno;
13415   unsigned int lastregno = 0;
13416   char *s_reset;
13417   size_t i;
13418
13419   insn_error = NULL;
13420
13421   forced_insn_length = 0;
13422
13423   for (s = str; ISLOWER (*s); ++s)
13424     ;
13425   switch (*s)
13426     {
13427     case '\0':
13428       break;
13429
13430     case ' ':
13431       *s++ = '\0';
13432       break;
13433
13434     case '.':
13435       if (s[1] == 't' && s[2] == ' ')
13436         {
13437           *s = '\0';
13438           forced_insn_length = 2;
13439           s += 3;
13440           break;
13441         }
13442       else if (s[1] == 'e' && s[2] == ' ')
13443         {
13444           *s = '\0';
13445           forced_insn_length = 4;
13446           s += 3;
13447           break;
13448         }
13449       /* Fall through.  */
13450     default:
13451       insn_error = _("unknown opcode");
13452       return;
13453     }
13454
13455   if (mips_opts.noautoextend && !forced_insn_length)
13456     forced_insn_length = 2;
13457
13458   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13459     {
13460       insn_error = _("unrecognized opcode");
13461       return;
13462     }
13463
13464   argsstart = s;
13465   for (;;)
13466     {
13467       bfd_boolean ok;
13468
13469       gas_assert (strcmp (insn->name, str) == 0);
13470
13471       ok = is_opcode_valid_16 (insn);
13472       if (! ok)
13473         {
13474           if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13475               && strcmp (insn->name, insn[1].name) == 0)
13476             {
13477               ++insn;
13478               continue;
13479             }
13480           else
13481             {
13482               if (!insn_error)
13483                 {
13484                   static char buf[100];
13485                   sprintf (buf,
13486                            _("Opcode not supported on this processor: %s (%s)"),
13487                            mips_cpu_info_from_arch (mips_opts.arch)->name,
13488                            mips_cpu_info_from_isa (mips_opts.isa)->name);
13489                   insn_error = buf;
13490                 }
13491               return;
13492             }
13493         }
13494
13495       create_insn (ip, insn);
13496       imm_expr.X_op = O_absent;
13497       imm_reloc[0] = BFD_RELOC_UNUSED;
13498       imm_reloc[1] = BFD_RELOC_UNUSED;
13499       imm_reloc[2] = BFD_RELOC_UNUSED;
13500       imm2_expr.X_op = O_absent;
13501       offset_expr.X_op = O_absent;
13502       offset_reloc[0] = BFD_RELOC_UNUSED;
13503       offset_reloc[1] = BFD_RELOC_UNUSED;
13504       offset_reloc[2] = BFD_RELOC_UNUSED;
13505       for (args = insn->args; 1; ++args)
13506         {
13507           int c;
13508
13509           if (*s == ' ')
13510             ++s;
13511
13512           /* In this switch statement we call break if we did not find
13513              a match, continue if we did find a match, or return if we
13514              are done.  */
13515
13516           c = *args;
13517           switch (c)
13518             {
13519             case '\0':
13520               if (*s == '\0')
13521                 {
13522                   offsetT value;
13523
13524                   /* Stuff the immediate value in now, if we can.  */
13525                   if (imm_expr.X_op == O_constant
13526                       && *imm_reloc > BFD_RELOC_UNUSED
13527                       && insn->pinfo != INSN_MACRO
13528                       && calculate_reloc (*offset_reloc,
13529                                           imm_expr.X_add_number, &value))
13530                     {
13531                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13532                                     *offset_reloc, value, forced_insn_length,
13533                                     &ip->insn_opcode);
13534                       imm_expr.X_op = O_absent;
13535                       *imm_reloc = BFD_RELOC_UNUSED;
13536                       *offset_reloc = BFD_RELOC_UNUSED;
13537                     }
13538
13539                   return;
13540                 }
13541               break;
13542
13543             case ',':
13544               if (*s++ == c)
13545                 continue;
13546               s--;
13547               switch (*++args)
13548                 {
13549                 case 'v':
13550                   MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13551                   continue;
13552                 case 'w':
13553                   MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13554                   continue;
13555                 }
13556               break;
13557
13558             case '(':
13559             case ')':
13560               if (*s++ == c)
13561                 continue;
13562               break;
13563
13564             case 'v':
13565             case 'w':
13566               if (s[0] != '$')
13567                 {
13568                   if (c == 'v')
13569                     MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13570                   else
13571                     MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13572                   ++args;
13573                   continue;
13574                 }
13575               /* Fall through.  */
13576             case 'x':
13577             case 'y':
13578             case 'z':
13579             case 'Z':
13580             case '0':
13581             case 'S':
13582             case 'R':
13583             case 'X':
13584             case 'Y':
13585               s_reset = s;
13586               if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
13587                 {
13588                   if (c == 'v' || c == 'w')
13589                     {
13590                       if (c == 'v')
13591                         MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13592                       else
13593                         MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13594                       ++args;
13595                       continue;
13596                     }
13597                   break;
13598                 }
13599
13600               if (*s == ' ')
13601                 ++s;
13602               if (args[1] != *s)
13603                 {
13604                   if (c == 'v' || c == 'w')
13605                     {
13606                       regno = mips16_to_32_reg_map[lastregno];
13607                       s = s_reset;
13608                       ++args;
13609                     }
13610                 }
13611
13612               switch (c)
13613                 {
13614                 case 'x':
13615                 case 'y':
13616                 case 'z':
13617                 case 'v':
13618                 case 'w':
13619                 case 'Z':
13620                   regno = mips32_to_16_reg_map[regno];
13621                   break;
13622
13623                 case '0':
13624                   if (regno != 0)
13625                     regno = ILLEGAL_REG;
13626                   break;
13627
13628                 case 'S':
13629                   if (regno != SP)
13630                     regno = ILLEGAL_REG;
13631                   break;
13632
13633                 case 'R':
13634                   if (regno != RA)
13635                     regno = ILLEGAL_REG;
13636                   break;
13637
13638                 case 'X':
13639                 case 'Y':
13640                   if (regno == AT && mips_opts.at)
13641                     {
13642                       if (mips_opts.at == ATREG)
13643                         as_warn (_("used $at without \".set noat\""));
13644                       else
13645                         as_warn (_("used $%u with \".set at=$%u\""),
13646                                  regno, mips_opts.at);
13647                     }
13648                   break;
13649
13650                 default:
13651                   abort ();
13652                 }
13653
13654               if (regno == ILLEGAL_REG)
13655                 break;
13656
13657               switch (c)
13658                 {
13659                 case 'x':
13660                 case 'v':
13661                   MIPS16_INSERT_OPERAND (RX, *ip, regno);
13662                   break;
13663                 case 'y':
13664                 case 'w':
13665                   MIPS16_INSERT_OPERAND (RY, *ip, regno);
13666                   break;
13667                 case 'z':
13668                   MIPS16_INSERT_OPERAND (RZ, *ip, regno);
13669                   break;
13670                 case 'Z':
13671                   MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
13672                 case '0':
13673                 case 'S':
13674                 case 'R':
13675                   break;
13676                 case 'X':
13677                   MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
13678                   break;
13679                 case 'Y':
13680                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
13681                   MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
13682                   break;
13683                 default:
13684                   abort ();
13685                 }
13686
13687               lastregno = regno;
13688               continue;
13689
13690             case 'P':
13691               if (strncmp (s, "$pc", 3) == 0)
13692                 {
13693                   s += 3;
13694                   continue;
13695                 }
13696               break;
13697
13698             case '5':
13699             case 'H':
13700             case 'W':
13701             case 'D':
13702             case 'j':
13703             case 'V':
13704             case 'C':
13705             case 'U':
13706             case 'k':
13707             case 'K':
13708               i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13709               if (i > 0)
13710                 {
13711                   if (imm_expr.X_op != O_constant)
13712                     {
13713                       forced_insn_length = 4;
13714                       ip->insn_opcode |= MIPS16_EXTEND;
13715                     }
13716                   else
13717                     {
13718                       /* We need to relax this instruction.  */
13719                       *offset_reloc = *imm_reloc;
13720                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13721                     }
13722                   s = expr_end;
13723                   continue;
13724                 }
13725               *imm_reloc = BFD_RELOC_UNUSED;
13726               /* Fall through.  */
13727             case '<':
13728             case '>':
13729             case '[':
13730             case ']':
13731             case '4':
13732             case '8':
13733               my_getExpression (&imm_expr, s);
13734               if (imm_expr.X_op == O_register)
13735                 {
13736                   /* What we thought was an expression turned out to
13737                      be a register.  */
13738
13739                   if (s[0] == '(' && args[1] == '(')
13740                     {
13741                       /* It looks like the expression was omitted
13742                          before a register indirection, which means
13743                          that the expression is implicitly zero.  We
13744                          still set up imm_expr, so that we handle
13745                          explicit extensions correctly.  */
13746                       imm_expr.X_op = O_constant;
13747                       imm_expr.X_add_number = 0;
13748                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13749                       continue;
13750                     }
13751
13752                   break;
13753                 }
13754
13755               /* We need to relax this instruction.  */
13756               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13757               s = expr_end;
13758               continue;
13759
13760             case 'p':
13761             case 'q':
13762             case 'A':
13763             case 'B':
13764             case 'E':
13765               /* We use offset_reloc rather than imm_reloc for the PC
13766                  relative operands.  This lets macros with both
13767                  immediate and address operands work correctly.  */
13768               my_getExpression (&offset_expr, s);
13769
13770               if (offset_expr.X_op == O_register)
13771                 break;
13772
13773               /* We need to relax this instruction.  */
13774               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13775               s = expr_end;
13776               continue;
13777
13778             case '6':           /* break code */
13779               my_getExpression (&imm_expr, s);
13780               check_absolute_expr (ip, &imm_expr);
13781               if ((unsigned long) imm_expr.X_add_number > 63)
13782                 as_warn (_("Invalid value for `%s' (%lu)"),
13783                          ip->insn_mo->name,
13784                          (unsigned long) imm_expr.X_add_number);
13785               MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
13786               imm_expr.X_op = O_absent;
13787               s = expr_end;
13788               continue;
13789
13790             case 'a':           /* 26 bit address */
13791               my_getExpression (&offset_expr, s);
13792               s = expr_end;
13793               *offset_reloc = BFD_RELOC_MIPS16_JMP;
13794               ip->insn_opcode <<= 16;
13795               continue;
13796
13797             case 'l':           /* register list for entry macro */
13798             case 'L':           /* register list for exit macro */
13799               {
13800                 int mask;
13801
13802                 if (c == 'l')
13803                   mask = 0;
13804                 else
13805                   mask = 7 << 3;
13806                 while (*s != '\0')
13807                   {
13808                     unsigned int freg, reg1, reg2;
13809
13810                     while (*s == ' ' || *s == ',')
13811                       ++s;
13812                     if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13813                       freg = 0;
13814                     else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13815                       freg = 1;
13816                     else
13817                       {
13818                         as_bad (_("can't parse register list"));
13819                         break;
13820                       }
13821                     if (*s == ' ')
13822                       ++s;
13823                     if (*s != '-')
13824                       reg2 = reg1;
13825                     else
13826                       {
13827                         ++s;
13828                         if (!reg_lookup (&s, freg ? RTYPE_FPU 
13829                                          : (RTYPE_GP | RTYPE_NUM), &reg2))
13830                           {
13831                             as_bad (_("invalid register list"));
13832                             break;
13833                           }
13834                       }
13835                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13836                       {
13837                         mask &= ~ (7 << 3);
13838                         mask |= 5 << 3;
13839                       }
13840                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13841                       {
13842                         mask &= ~ (7 << 3);
13843                         mask |= 6 << 3;
13844                       }
13845                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13846                       mask |= (reg2 - 3) << 3;
13847                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13848                       mask |= (reg2 - 15) << 1;
13849                     else if (reg1 == RA && reg2 == RA)
13850                       mask |= 1;
13851                     else
13852                       {
13853                         as_bad (_("invalid register list"));
13854                         break;
13855                       }
13856                   }
13857                 /* The mask is filled in in the opcode table for the
13858                    benefit of the disassembler.  We remove it before
13859                    applying the actual mask.  */
13860                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13861                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13862               }
13863             continue;
13864
13865             case 'm':           /* Register list for save insn.  */
13866             case 'M':           /* Register list for restore insn.  */
13867               {
13868                 int opcode = ip->insn_opcode;
13869                 int framesz = 0, seen_framesz = 0;
13870                 int nargs = 0, statics = 0, sregs = 0;
13871
13872                 while (*s != '\0')
13873                   {
13874                     unsigned int reg1, reg2;
13875
13876                     SKIP_SPACE_TABS (s);
13877                     while (*s == ',')
13878                       ++s;
13879                     SKIP_SPACE_TABS (s);
13880
13881                     my_getExpression (&imm_expr, s);
13882                     if (imm_expr.X_op == O_constant)
13883                       {
13884                         /* Handle the frame size.  */
13885                         if (seen_framesz)
13886                           {
13887                             as_bad (_("more than one frame size in list"));
13888                             break;
13889                           }
13890                         seen_framesz = 1;
13891                         framesz = imm_expr.X_add_number;
13892                         imm_expr.X_op = O_absent;
13893                         s = expr_end;
13894                         continue;
13895                       }
13896
13897                     if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13898                       {
13899                         as_bad (_("can't parse register list"));
13900                         break;
13901                       }
13902
13903                     while (*s == ' ')
13904                       ++s;
13905
13906                     if (*s != '-')
13907                       reg2 = reg1;
13908                     else
13909                       {
13910                         ++s;
13911                         if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13912                             || reg2 < reg1)
13913                           {
13914                             as_bad (_("can't parse register list"));
13915                             break;
13916                           }
13917                       }
13918
13919                     while (reg1 <= reg2)
13920                       {
13921                         if (reg1 >= 4 && reg1 <= 7)
13922                           {
13923                             if (!seen_framesz)
13924                                 /* args $a0-$a3 */
13925                                 nargs |= 1 << (reg1 - 4);
13926                             else
13927                                 /* statics $a0-$a3 */
13928                                 statics |= 1 << (reg1 - 4);
13929                           }
13930                         else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13931                           {
13932                             /* $s0-$s8 */
13933                             sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13934                           }
13935                         else if (reg1 == 31)
13936                           {
13937                             /* Add $ra to insn.  */
13938                             opcode |= 0x40;
13939                           }
13940                         else
13941                           {
13942                             as_bad (_("unexpected register in list"));
13943                             break;
13944                           }
13945                         if (++reg1 == 24)
13946                           reg1 = 30;
13947                       }
13948                   }
13949
13950                 /* Encode args/statics combination.  */
13951                 if (nargs & statics)
13952                   as_bad (_("arg/static registers overlap"));
13953                 else if (nargs == 0xf)
13954                   /* All $a0-$a3 are args.  */
13955                   opcode |= MIPS16_ALL_ARGS << 16;
13956                 else if (statics == 0xf)
13957                   /* All $a0-$a3 are statics.  */
13958                   opcode |= MIPS16_ALL_STATICS << 16;
13959                 else 
13960                   {
13961                     int narg = 0, nstat = 0;
13962
13963                     /* Count arg registers.  */
13964                     while (nargs & 0x1)
13965                       {
13966                         nargs >>= 1;
13967                         narg++;
13968                       }
13969                     if (nargs != 0)
13970                       as_bad (_("invalid arg register list"));
13971
13972                     /* Count static registers.  */
13973                     while (statics & 0x8)
13974                       {
13975                         statics = (statics << 1) & 0xf;
13976                         nstat++;
13977                       }
13978                     if (statics != 0) 
13979                       as_bad (_("invalid static register list"));
13980
13981                     /* Encode args/statics.  */
13982                     opcode |= ((narg << 2) | nstat) << 16;
13983                   }
13984
13985                 /* Encode $s0/$s1.  */
13986                 if (sregs & (1 << 0))           /* $s0 */
13987                   opcode |= 0x20;
13988                 if (sregs & (1 << 1))           /* $s1 */
13989                   opcode |= 0x10;
13990                 sregs >>= 2;
13991
13992                 if (sregs != 0)
13993                   {
13994                     /* Count regs $s2-$s8.  */
13995                     int nsreg = 0;
13996                     while (sregs & 1)
13997                       {
13998                         sregs >>= 1;
13999                         nsreg++;
14000                       }
14001                     if (sregs != 0)
14002                       as_bad (_("invalid static register list"));
14003                     /* Encode $s2-$s8. */
14004                     opcode |= nsreg << 24;
14005                   }
14006
14007                 /* Encode frame size.  */
14008                 if (!seen_framesz)
14009                   as_bad (_("missing frame size"));
14010                 else if ((framesz & 7) != 0 || framesz < 0
14011                          || framesz > 0xff * 8)
14012                   as_bad (_("invalid frame size"));
14013                 else if (framesz != 128 || (opcode >> 16) != 0)
14014                   {
14015                     framesz /= 8;
14016                     opcode |= (((framesz & 0xf0) << 16)
14017                              | (framesz & 0x0f));
14018                   }
14019
14020                 /* Finally build the instruction.  */
14021                 if ((opcode >> 16) != 0 || framesz == 0)
14022                   opcode |= MIPS16_EXTEND;
14023                 ip->insn_opcode = opcode;
14024               }
14025             continue;
14026
14027             case 'e':           /* extend code */
14028               my_getExpression (&imm_expr, s);
14029               check_absolute_expr (ip, &imm_expr);
14030               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14031                 {
14032                   as_warn (_("Invalid value for `%s' (%lu)"),
14033                            ip->insn_mo->name,
14034                            (unsigned long) imm_expr.X_add_number);
14035                   imm_expr.X_add_number &= 0x7ff;
14036                 }
14037               ip->insn_opcode |= imm_expr.X_add_number;
14038               imm_expr.X_op = O_absent;
14039               s = expr_end;
14040               continue;
14041
14042             default:
14043               abort ();
14044             }
14045           break;
14046         }
14047
14048       /* Args don't match.  */
14049       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14050           strcmp (insn->name, insn[1].name) == 0)
14051         {
14052           ++insn;
14053           s = argsstart;
14054           continue;
14055         }
14056
14057       insn_error = _("illegal operands");
14058
14059       return;
14060     }
14061 }
14062
14063 /* This structure holds information we know about a mips16 immediate
14064    argument type.  */
14065
14066 struct mips16_immed_operand
14067 {
14068   /* The type code used in the argument string in the opcode table.  */
14069   int type;
14070   /* The number of bits in the short form of the opcode.  */
14071   int nbits;
14072   /* The number of bits in the extended form of the opcode.  */
14073   int extbits;
14074   /* The amount by which the short form is shifted when it is used;
14075      for example, the sw instruction has a shift count of 2.  */
14076   int shift;
14077   /* The amount by which the short form is shifted when it is stored
14078      into the instruction code.  */
14079   int op_shift;
14080   /* Non-zero if the short form is unsigned.  */
14081   int unsp;
14082   /* Non-zero if the extended form is unsigned.  */
14083   int extu;
14084   /* Non-zero if the value is PC relative.  */
14085   int pcrel;
14086 };
14087
14088 /* The mips16 immediate operand types.  */
14089
14090 static const struct mips16_immed_operand mips16_immed_operands[] =
14091 {
14092   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14093   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14094   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14095   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14096   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14097   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14098   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14099   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14100   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14101   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14102   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14103   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14104   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14105   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14106   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14107   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14108   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14109   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14110   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14111   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14112   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14113 };
14114
14115 #define MIPS16_NUM_IMMED \
14116   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14117
14118 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14119    NBITS is the number of significant bits in VAL.  */
14120
14121 static unsigned long
14122 mips16_immed_extend (offsetT val, unsigned int nbits)
14123 {
14124   int extval;
14125   if (nbits == 16)
14126     {
14127       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14128       val &= 0x1f;
14129     }
14130   else if (nbits == 15)
14131     {
14132       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14133       val &= 0xf;
14134     }
14135   else
14136     {
14137       extval = ((val & 0x1f) << 6) | (val & 0x20);
14138       val = 0;
14139     }
14140   return (extval << 16) | val;
14141 }
14142
14143 /* Install immediate value VAL into MIPS16 instruction *INSN,
14144    extending it if necessary.  The instruction in *INSN may
14145    already be extended.
14146
14147    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14148    if none.  In the former case, VAL is a 16-bit number with no
14149    defined signedness.
14150
14151    TYPE is the type of the immediate field.  USER_INSN_LENGTH
14152    is the length that the user requested, or 0 if none.  */
14153
14154 static void
14155 mips16_immed (char *file, unsigned int line, int type,
14156               bfd_reloc_code_real_type reloc, offsetT val,
14157               unsigned int user_insn_length, unsigned long *insn)
14158 {
14159   const struct mips16_immed_operand *op;
14160   int mintiny, maxtiny;
14161
14162   op = mips16_immed_operands;
14163   while (op->type != type)
14164     {
14165       ++op;
14166       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14167     }
14168
14169   if (op->unsp)
14170     {
14171       if (type == '<' || type == '>' || type == '[' || type == ']')
14172         {
14173           mintiny = 1;
14174           maxtiny = 1 << op->nbits;
14175         }
14176       else
14177         {
14178           mintiny = 0;
14179           maxtiny = (1 << op->nbits) - 1;
14180         }
14181       if (reloc != BFD_RELOC_UNUSED)
14182         val &= 0xffff;
14183     }
14184   else
14185     {
14186       mintiny = - (1 << (op->nbits - 1));
14187       maxtiny = (1 << (op->nbits - 1)) - 1;
14188       if (reloc != BFD_RELOC_UNUSED)
14189         val = SEXT_16BIT (val);
14190     }
14191
14192   /* Branch offsets have an implicit 0 in the lowest bit.  */
14193   if (type == 'p' || type == 'q')
14194     val /= 2;
14195
14196   if ((val & ((1 << op->shift) - 1)) != 0
14197       || val < (mintiny << op->shift)
14198       || val > (maxtiny << op->shift))
14199     {
14200       /* We need an extended instruction.  */
14201       if (user_insn_length == 2)
14202         as_bad_where (file, line, _("invalid unextended operand value"));
14203       else
14204         *insn |= MIPS16_EXTEND;
14205     }
14206   else if (user_insn_length == 4)
14207     {
14208       /* The operand doesn't force an unextended instruction to be extended.
14209          Warn if the user wanted an extended instruction anyway.  */
14210       *insn |= MIPS16_EXTEND;
14211       as_warn_where (file, line,
14212                      _("extended operand requested but not required"));
14213     }
14214
14215   if (mips16_opcode_length (*insn) == 2)
14216     {
14217       int insnval;
14218
14219       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14220       insnval <<= op->op_shift;
14221       *insn |= insnval;
14222     }
14223   else
14224     {
14225       long minext, maxext;
14226
14227       if (reloc == BFD_RELOC_UNUSED)
14228         {
14229           if (op->extu)
14230             {
14231               minext = 0;
14232               maxext = (1 << op->extbits) - 1;
14233             }
14234           else
14235             {
14236               minext = - (1 << (op->extbits - 1));
14237               maxext = (1 << (op->extbits - 1)) - 1;
14238             }
14239           if (val < minext || val > maxext)
14240             as_bad_where (file, line,
14241                           _("operand value out of range for instruction"));
14242         }
14243
14244       *insn |= mips16_immed_extend (val, op->extbits);
14245     }
14246 }
14247 \f
14248 struct percent_op_match
14249 {
14250   const char *str;
14251   bfd_reloc_code_real_type reloc;
14252 };
14253
14254 static const struct percent_op_match mips_percent_op[] =
14255 {
14256   {"%lo", BFD_RELOC_LO16},
14257 #ifdef OBJ_ELF
14258   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14259   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14260   {"%call16", BFD_RELOC_MIPS_CALL16},
14261   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14262   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14263   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14264   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14265   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14266   {"%got", BFD_RELOC_MIPS_GOT16},
14267   {"%gp_rel", BFD_RELOC_GPREL16},
14268   {"%half", BFD_RELOC_16},
14269   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14270   {"%higher", BFD_RELOC_MIPS_HIGHER},
14271   {"%neg", BFD_RELOC_MIPS_SUB},
14272   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14273   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14274   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14275   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14276   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14277   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14278   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14279 #endif
14280   {"%hi", BFD_RELOC_HI16_S}
14281 };
14282
14283 static const struct percent_op_match mips16_percent_op[] =
14284 {
14285   {"%lo", BFD_RELOC_MIPS16_LO16},
14286   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14287   {"%got", BFD_RELOC_MIPS16_GOT16},
14288   {"%call16", BFD_RELOC_MIPS16_CALL16},
14289   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14290   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14291   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14292   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14293   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14294   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14295   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14296   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14297 };
14298
14299
14300 /* Return true if *STR points to a relocation operator.  When returning true,
14301    move *STR over the operator and store its relocation code in *RELOC.
14302    Leave both *STR and *RELOC alone when returning false.  */
14303
14304 static bfd_boolean
14305 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14306 {
14307   const struct percent_op_match *percent_op;
14308   size_t limit, i;
14309
14310   if (mips_opts.mips16)
14311     {
14312       percent_op = mips16_percent_op;
14313       limit = ARRAY_SIZE (mips16_percent_op);
14314     }
14315   else
14316     {
14317       percent_op = mips_percent_op;
14318       limit = ARRAY_SIZE (mips_percent_op);
14319     }
14320
14321   for (i = 0; i < limit; i++)
14322     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14323       {
14324         int len = strlen (percent_op[i].str);
14325
14326         if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14327           continue;
14328
14329         *str += strlen (percent_op[i].str);
14330         *reloc = percent_op[i].reloc;
14331
14332         /* Check whether the output BFD supports this relocation.
14333            If not, issue an error and fall back on something safe.  */
14334         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14335           {
14336             as_bad (_("relocation %s isn't supported by the current ABI"),
14337                     percent_op[i].str);
14338             *reloc = BFD_RELOC_UNUSED;
14339           }
14340         return TRUE;
14341       }
14342   return FALSE;
14343 }
14344
14345
14346 /* Parse string STR as a 16-bit relocatable operand.  Store the
14347    expression in *EP and the relocations in the array starting
14348    at RELOC.  Return the number of relocation operators used.
14349
14350    On exit, EXPR_END points to the first character after the expression.  */
14351
14352 static size_t
14353 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14354                        char *str)
14355 {
14356   bfd_reloc_code_real_type reversed_reloc[3];
14357   size_t reloc_index, i;
14358   int crux_depth, str_depth;
14359   char *crux;
14360
14361   /* Search for the start of the main expression, recoding relocations
14362      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14363      of the main expression and with CRUX_DEPTH containing the number
14364      of open brackets at that point.  */
14365   reloc_index = -1;
14366   str_depth = 0;
14367   do
14368     {
14369       reloc_index++;
14370       crux = str;
14371       crux_depth = str_depth;
14372
14373       /* Skip over whitespace and brackets, keeping count of the number
14374          of brackets.  */
14375       while (*str == ' ' || *str == '\t' || *str == '(')
14376         if (*str++ == '(')
14377           str_depth++;
14378     }
14379   while (*str == '%'
14380          && reloc_index < (HAVE_NEWABI ? 3 : 1)
14381          && parse_relocation (&str, &reversed_reloc[reloc_index]));
14382
14383   my_getExpression (ep, crux);
14384   str = expr_end;
14385
14386   /* Match every open bracket.  */
14387   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14388     if (*str++ == ')')
14389       crux_depth--;
14390
14391   if (crux_depth > 0)
14392     as_bad (_("unclosed '('"));
14393
14394   expr_end = str;
14395
14396   if (reloc_index != 0)
14397     {
14398       prev_reloc_op_frag = frag_now;
14399       for (i = 0; i < reloc_index; i++)
14400         reloc[i] = reversed_reloc[reloc_index - 1 - i];
14401     }
14402
14403   return reloc_index;
14404 }
14405
14406 static void
14407 my_getExpression (expressionS *ep, char *str)
14408 {
14409   char *save_in;
14410
14411   save_in = input_line_pointer;
14412   input_line_pointer = str;
14413   expression (ep);
14414   expr_end = input_line_pointer;
14415   input_line_pointer = save_in;
14416 }
14417
14418 char *
14419 md_atof (int type, char *litP, int *sizeP)
14420 {
14421   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14422 }
14423
14424 void
14425 md_number_to_chars (char *buf, valueT val, int n)
14426 {
14427   if (target_big_endian)
14428     number_to_chars_bigendian (buf, val, n);
14429   else
14430     number_to_chars_littleendian (buf, val, n);
14431 }
14432 \f
14433 #ifdef OBJ_ELF
14434 static int support_64bit_objects(void)
14435 {
14436   const char **list, **l;
14437   int yes;
14438
14439   list = bfd_target_list ();
14440   for (l = list; *l != NULL; l++)
14441     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14442         || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14443       break;
14444   yes = (*l != NULL);
14445   free (list);
14446   return yes;
14447 }
14448 #endif /* OBJ_ELF */
14449
14450 const char *md_shortopts = "O::g::G:";
14451
14452 enum options
14453   {
14454     OPTION_MARCH = OPTION_MD_BASE,
14455     OPTION_MTUNE,
14456     OPTION_MIPS1,
14457     OPTION_MIPS2,
14458     OPTION_MIPS3,
14459     OPTION_MIPS4,
14460     OPTION_MIPS5,
14461     OPTION_MIPS32,
14462     OPTION_MIPS64,
14463     OPTION_MIPS32R2,
14464     OPTION_MIPS64R2,
14465     OPTION_MIPS16,
14466     OPTION_NO_MIPS16,
14467     OPTION_MIPS3D,
14468     OPTION_NO_MIPS3D,
14469     OPTION_MDMX,
14470     OPTION_NO_MDMX,
14471     OPTION_DSP,
14472     OPTION_NO_DSP,
14473     OPTION_MT,
14474     OPTION_NO_MT,
14475     OPTION_SMARTMIPS,
14476     OPTION_NO_SMARTMIPS,
14477     OPTION_DSPR2,
14478     OPTION_NO_DSPR2,
14479     OPTION_MICROMIPS,
14480     OPTION_NO_MICROMIPS,
14481     OPTION_MCU,
14482     OPTION_NO_MCU,
14483     OPTION_COMPAT_ARCH_BASE,
14484     OPTION_M4650,
14485     OPTION_NO_M4650,
14486     OPTION_M4010,
14487     OPTION_NO_M4010,
14488     OPTION_M4100,
14489     OPTION_NO_M4100,
14490     OPTION_M3900,
14491     OPTION_NO_M3900,
14492     OPTION_M7000_HILO_FIX,
14493     OPTION_MNO_7000_HILO_FIX, 
14494     OPTION_FIX_24K,
14495     OPTION_NO_FIX_24K,
14496     OPTION_FIX_LOONGSON2F_JUMP,
14497     OPTION_NO_FIX_LOONGSON2F_JUMP,
14498     OPTION_FIX_LOONGSON2F_NOP,
14499     OPTION_NO_FIX_LOONGSON2F_NOP,
14500     OPTION_FIX_VR4120,
14501     OPTION_NO_FIX_VR4120,
14502     OPTION_FIX_VR4130,
14503     OPTION_NO_FIX_VR4130,
14504     OPTION_FIX_CN63XXP1,
14505     OPTION_NO_FIX_CN63XXP1,
14506     OPTION_TRAP,
14507     OPTION_BREAK,
14508     OPTION_EB,
14509     OPTION_EL,
14510     OPTION_FP32,
14511     OPTION_GP32,
14512     OPTION_CONSTRUCT_FLOATS,
14513     OPTION_NO_CONSTRUCT_FLOATS,
14514     OPTION_FP64,
14515     OPTION_GP64,
14516     OPTION_RELAX_BRANCH,
14517     OPTION_NO_RELAX_BRANCH,
14518     OPTION_MSHARED,
14519     OPTION_MNO_SHARED,
14520     OPTION_MSYM32,
14521     OPTION_MNO_SYM32,
14522     OPTION_SOFT_FLOAT,
14523     OPTION_HARD_FLOAT,
14524     OPTION_SINGLE_FLOAT,
14525     OPTION_DOUBLE_FLOAT,
14526     OPTION_32,
14527 #ifdef OBJ_ELF
14528     OPTION_CALL_SHARED,
14529     OPTION_CALL_NONPIC,
14530     OPTION_NON_SHARED,
14531     OPTION_XGOT,
14532     OPTION_MABI,
14533     OPTION_N32,
14534     OPTION_64,
14535     OPTION_MDEBUG,
14536     OPTION_NO_MDEBUG,
14537     OPTION_PDR,
14538     OPTION_NO_PDR,
14539     OPTION_MVXWORKS_PIC,
14540 #endif /* OBJ_ELF */
14541     OPTION_END_OF_ENUM    
14542   };
14543   
14544 struct option md_longopts[] =
14545 {
14546   /* Options which specify architecture.  */
14547   {"march", required_argument, NULL, OPTION_MARCH},
14548   {"mtune", required_argument, NULL, OPTION_MTUNE},
14549   {"mips0", no_argument, NULL, OPTION_MIPS1},
14550   {"mips1", no_argument, NULL, OPTION_MIPS1},
14551   {"mips2", no_argument, NULL, OPTION_MIPS2},
14552   {"mips3", no_argument, NULL, OPTION_MIPS3},
14553   {"mips4", no_argument, NULL, OPTION_MIPS4},
14554   {"mips5", no_argument, NULL, OPTION_MIPS5},
14555   {"mips32", no_argument, NULL, OPTION_MIPS32},
14556   {"mips64", no_argument, NULL, OPTION_MIPS64},
14557   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
14558   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
14559
14560   /* Options which specify Application Specific Extensions (ASEs).  */
14561   {"mips16", no_argument, NULL, OPTION_MIPS16},
14562   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
14563   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
14564   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
14565   {"mdmx", no_argument, NULL, OPTION_MDMX},
14566   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
14567   {"mdsp", no_argument, NULL, OPTION_DSP},
14568   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
14569   {"mmt", no_argument, NULL, OPTION_MT},
14570   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
14571   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
14572   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
14573   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
14574   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
14575   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14576   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
14577   {"mmcu", no_argument, NULL, OPTION_MCU},
14578   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
14579
14580   /* Old-style architecture options.  Don't add more of these.  */
14581   {"m4650", no_argument, NULL, OPTION_M4650},
14582   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
14583   {"m4010", no_argument, NULL, OPTION_M4010},
14584   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
14585   {"m4100", no_argument, NULL, OPTION_M4100},
14586   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
14587   {"m3900", no_argument, NULL, OPTION_M3900},
14588   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14589
14590   /* Options which enable bug fixes.  */
14591   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
14592   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14593   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14594   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14595   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14596   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14597   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
14598   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
14599   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
14600   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
14601   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
14602   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
14603   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
14604   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14605   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
14606
14607   /* Miscellaneous options.  */
14608   {"trap", no_argument, NULL, OPTION_TRAP},
14609   {"no-break", no_argument, NULL, OPTION_TRAP},
14610   {"break", no_argument, NULL, OPTION_BREAK},
14611   {"no-trap", no_argument, NULL, OPTION_BREAK},
14612   {"EB", no_argument, NULL, OPTION_EB},
14613   {"EL", no_argument, NULL, OPTION_EL},
14614   {"mfp32", no_argument, NULL, OPTION_FP32},
14615   {"mgp32", no_argument, NULL, OPTION_GP32},
14616   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
14617   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
14618   {"mfp64", no_argument, NULL, OPTION_FP64},
14619   {"mgp64", no_argument, NULL, OPTION_GP64},
14620   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14621   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
14622   {"mshared", no_argument, NULL, OPTION_MSHARED},
14623   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
14624   {"msym32", no_argument, NULL, OPTION_MSYM32},
14625   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
14626   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14627   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
14628   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14629   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
14630
14631   /* Strictly speaking this next option is ELF specific,
14632      but we allow it for other ports as well in order to
14633      make testing easier.  */
14634   {"32",          no_argument, NULL, OPTION_32},
14635   
14636   /* ELF-specific options.  */
14637 #ifdef OBJ_ELF
14638   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
14639   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
14640   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
14641   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
14642   {"xgot",        no_argument, NULL, OPTION_XGOT},
14643   {"mabi", required_argument, NULL, OPTION_MABI},
14644   {"n32",         no_argument, NULL, OPTION_N32},
14645   {"64",          no_argument, NULL, OPTION_64},
14646   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
14647   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
14648   {"mpdr", no_argument, NULL, OPTION_PDR},
14649   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
14650   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
14651 #endif /* OBJ_ELF */
14652
14653   {NULL, no_argument, NULL, 0}
14654 };
14655 size_t md_longopts_size = sizeof (md_longopts);
14656
14657 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14658    NEW_VALUE.  Warn if another value was already specified.  Note:
14659    we have to defer parsing the -march and -mtune arguments in order
14660    to handle 'from-abi' correctly, since the ABI might be specified
14661    in a later argument.  */
14662
14663 static void
14664 mips_set_option_string (const char **string_ptr, const char *new_value)
14665 {
14666   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14667     as_warn (_("A different %s was already specified, is now %s"),
14668              string_ptr == &mips_arch_string ? "-march" : "-mtune",
14669              new_value);
14670
14671   *string_ptr = new_value;
14672 }
14673
14674 int
14675 md_parse_option (int c, char *arg)
14676 {
14677   switch (c)
14678     {
14679     case OPTION_CONSTRUCT_FLOATS:
14680       mips_disable_float_construction = 0;
14681       break;
14682
14683     case OPTION_NO_CONSTRUCT_FLOATS:
14684       mips_disable_float_construction = 1;
14685       break;
14686
14687     case OPTION_TRAP:
14688       mips_trap = 1;
14689       break;
14690
14691     case OPTION_BREAK:
14692       mips_trap = 0;
14693       break;
14694
14695     case OPTION_EB:
14696       target_big_endian = 1;
14697       break;
14698
14699     case OPTION_EL:
14700       target_big_endian = 0;
14701       break;
14702
14703     case 'O':
14704       if (arg == NULL)
14705         mips_optimize = 1;
14706       else if (arg[0] == '0')
14707         mips_optimize = 0;
14708       else if (arg[0] == '1')
14709         mips_optimize = 1;
14710       else
14711         mips_optimize = 2;
14712       break;
14713
14714     case 'g':
14715       if (arg == NULL)
14716         mips_debug = 2;
14717       else
14718         mips_debug = atoi (arg);
14719       break;
14720
14721     case OPTION_MIPS1:
14722       file_mips_isa = ISA_MIPS1;
14723       break;
14724
14725     case OPTION_MIPS2:
14726       file_mips_isa = ISA_MIPS2;
14727       break;
14728
14729     case OPTION_MIPS3:
14730       file_mips_isa = ISA_MIPS3;
14731       break;
14732
14733     case OPTION_MIPS4:
14734       file_mips_isa = ISA_MIPS4;
14735       break;
14736
14737     case OPTION_MIPS5:
14738       file_mips_isa = ISA_MIPS5;
14739       break;
14740
14741     case OPTION_MIPS32:
14742       file_mips_isa = ISA_MIPS32;
14743       break;
14744
14745     case OPTION_MIPS32R2:
14746       file_mips_isa = ISA_MIPS32R2;
14747       break;
14748
14749     case OPTION_MIPS64R2:
14750       file_mips_isa = ISA_MIPS64R2;
14751       break;
14752
14753     case OPTION_MIPS64:
14754       file_mips_isa = ISA_MIPS64;
14755       break;
14756
14757     case OPTION_MTUNE:
14758       mips_set_option_string (&mips_tune_string, arg);
14759       break;
14760
14761     case OPTION_MARCH:
14762       mips_set_option_string (&mips_arch_string, arg);
14763       break;
14764
14765     case OPTION_M4650:
14766       mips_set_option_string (&mips_arch_string, "4650");
14767       mips_set_option_string (&mips_tune_string, "4650");
14768       break;
14769
14770     case OPTION_NO_M4650:
14771       break;
14772
14773     case OPTION_M4010:
14774       mips_set_option_string (&mips_arch_string, "4010");
14775       mips_set_option_string (&mips_tune_string, "4010");
14776       break;
14777
14778     case OPTION_NO_M4010:
14779       break;
14780
14781     case OPTION_M4100:
14782       mips_set_option_string (&mips_arch_string, "4100");
14783       mips_set_option_string (&mips_tune_string, "4100");
14784       break;
14785
14786     case OPTION_NO_M4100:
14787       break;
14788
14789     case OPTION_M3900:
14790       mips_set_option_string (&mips_arch_string, "3900");
14791       mips_set_option_string (&mips_tune_string, "3900");
14792       break;
14793
14794     case OPTION_NO_M3900:
14795       break;
14796
14797     case OPTION_MDMX:
14798       mips_opts.ase_mdmx = 1;
14799       break;
14800
14801     case OPTION_NO_MDMX:
14802       mips_opts.ase_mdmx = 0;
14803       break;
14804
14805     case OPTION_DSP:
14806       mips_opts.ase_dsp = 1;
14807       mips_opts.ase_dspr2 = 0;
14808       break;
14809
14810     case OPTION_NO_DSP:
14811       mips_opts.ase_dsp = 0;
14812       mips_opts.ase_dspr2 = 0;
14813       break;
14814
14815     case OPTION_DSPR2:
14816       mips_opts.ase_dspr2 = 1;
14817       mips_opts.ase_dsp = 1;
14818       break;
14819
14820     case OPTION_NO_DSPR2:
14821       mips_opts.ase_dspr2 = 0;
14822       mips_opts.ase_dsp = 0;
14823       break;
14824
14825     case OPTION_MT:
14826       mips_opts.ase_mt = 1;
14827       break;
14828
14829     case OPTION_NO_MT:
14830       mips_opts.ase_mt = 0;
14831       break;
14832
14833     case OPTION_MCU:
14834       mips_opts.ase_mcu = 1;
14835       break;
14836
14837     case OPTION_NO_MCU:
14838       mips_opts.ase_mcu = 0;
14839       break;
14840
14841     case OPTION_MICROMIPS:
14842       if (mips_opts.mips16 == 1)
14843         {
14844           as_bad (_("-mmicromips cannot be used with -mips16"));
14845           return 0;
14846         }
14847       mips_opts.micromips = 1;
14848       mips_no_prev_insn ();
14849       break;
14850
14851     case OPTION_NO_MICROMIPS:
14852       mips_opts.micromips = 0;
14853       mips_no_prev_insn ();
14854       break;
14855
14856     case OPTION_MIPS16:
14857       if (mips_opts.micromips == 1)
14858         {
14859           as_bad (_("-mips16 cannot be used with -micromips"));
14860           return 0;
14861         }
14862       mips_opts.mips16 = 1;
14863       mips_no_prev_insn ();
14864       break;
14865
14866     case OPTION_NO_MIPS16:
14867       mips_opts.mips16 = 0;
14868       mips_no_prev_insn ();
14869       break;
14870
14871     case OPTION_MIPS3D:
14872       mips_opts.ase_mips3d = 1;
14873       break;
14874
14875     case OPTION_NO_MIPS3D:
14876       mips_opts.ase_mips3d = 0;
14877       break;
14878
14879     case OPTION_SMARTMIPS:
14880       mips_opts.ase_smartmips = 1;
14881       break;
14882
14883     case OPTION_NO_SMARTMIPS:
14884       mips_opts.ase_smartmips = 0;
14885       break;
14886
14887     case OPTION_FIX_24K:
14888       mips_fix_24k = 1;
14889       break;
14890
14891     case OPTION_NO_FIX_24K:
14892       mips_fix_24k = 0;
14893       break;
14894
14895     case OPTION_FIX_LOONGSON2F_JUMP:
14896       mips_fix_loongson2f_jump = TRUE;
14897       break;
14898
14899     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14900       mips_fix_loongson2f_jump = FALSE;
14901       break;
14902
14903     case OPTION_FIX_LOONGSON2F_NOP:
14904       mips_fix_loongson2f_nop = TRUE;
14905       break;
14906
14907     case OPTION_NO_FIX_LOONGSON2F_NOP:
14908       mips_fix_loongson2f_nop = FALSE;
14909       break;
14910
14911     case OPTION_FIX_VR4120:
14912       mips_fix_vr4120 = 1;
14913       break;
14914
14915     case OPTION_NO_FIX_VR4120:
14916       mips_fix_vr4120 = 0;
14917       break;
14918
14919     case OPTION_FIX_VR4130:
14920       mips_fix_vr4130 = 1;
14921       break;
14922
14923     case OPTION_NO_FIX_VR4130:
14924       mips_fix_vr4130 = 0;
14925       break;
14926
14927     case OPTION_FIX_CN63XXP1:
14928       mips_fix_cn63xxp1 = TRUE;
14929       break;
14930
14931     case OPTION_NO_FIX_CN63XXP1:
14932       mips_fix_cn63xxp1 = FALSE;
14933       break;
14934
14935     case OPTION_RELAX_BRANCH:
14936       mips_relax_branch = 1;
14937       break;
14938
14939     case OPTION_NO_RELAX_BRANCH:
14940       mips_relax_branch = 0;
14941       break;
14942
14943     case OPTION_MSHARED:
14944       mips_in_shared = TRUE;
14945       break;
14946
14947     case OPTION_MNO_SHARED:
14948       mips_in_shared = FALSE;
14949       break;
14950
14951     case OPTION_MSYM32:
14952       mips_opts.sym32 = TRUE;
14953       break;
14954
14955     case OPTION_MNO_SYM32:
14956       mips_opts.sym32 = FALSE;
14957       break;
14958
14959 #ifdef OBJ_ELF
14960       /* When generating ELF code, we permit -KPIC and -call_shared to
14961          select SVR4_PIC, and -non_shared to select no PIC.  This is
14962          intended to be compatible with Irix 5.  */
14963     case OPTION_CALL_SHARED:
14964       if (!IS_ELF)
14965         {
14966           as_bad (_("-call_shared is supported only for ELF format"));
14967           return 0;
14968         }
14969       mips_pic = SVR4_PIC;
14970       mips_abicalls = TRUE;
14971       break;
14972
14973     case OPTION_CALL_NONPIC:
14974       if (!IS_ELF)
14975         {
14976           as_bad (_("-call_nonpic is supported only for ELF format"));
14977           return 0;
14978         }
14979       mips_pic = NO_PIC;
14980       mips_abicalls = TRUE;
14981       break;
14982
14983     case OPTION_NON_SHARED:
14984       if (!IS_ELF)
14985         {
14986           as_bad (_("-non_shared is supported only for ELF format"));
14987           return 0;
14988         }
14989       mips_pic = NO_PIC;
14990       mips_abicalls = FALSE;
14991       break;
14992
14993       /* The -xgot option tells the assembler to use 32 bit offsets
14994          when accessing the got in SVR4_PIC mode.  It is for Irix
14995          compatibility.  */
14996     case OPTION_XGOT:
14997       mips_big_got = 1;
14998       break;
14999 #endif /* OBJ_ELF */
15000
15001     case 'G':
15002       g_switch_value = atoi (arg);
15003       g_switch_seen = 1;
15004       break;
15005
15006       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15007          and -mabi=64.  */
15008     case OPTION_32:
15009       if (IS_ELF)
15010         mips_abi = O32_ABI;
15011       /* We silently ignore -32 for non-ELF targets.  This greatly
15012          simplifies the construction of the MIPS GAS test cases.  */
15013       break;
15014
15015 #ifdef OBJ_ELF
15016     case OPTION_N32:
15017       if (!IS_ELF)
15018         {
15019           as_bad (_("-n32 is supported for ELF format only"));
15020           return 0;
15021         }
15022       mips_abi = N32_ABI;
15023       break;
15024
15025     case OPTION_64:
15026       if (!IS_ELF)
15027         {
15028           as_bad (_("-64 is supported for ELF format only"));
15029           return 0;
15030         }
15031       mips_abi = N64_ABI;
15032       if (!support_64bit_objects())
15033         as_fatal (_("No compiled in support for 64 bit object file format"));
15034       break;
15035 #endif /* OBJ_ELF */
15036
15037     case OPTION_GP32:
15038       file_mips_gp32 = 1;
15039       break;
15040
15041     case OPTION_GP64:
15042       file_mips_gp32 = 0;
15043       break;
15044
15045     case OPTION_FP32:
15046       file_mips_fp32 = 1;
15047       break;
15048
15049     case OPTION_FP64:
15050       file_mips_fp32 = 0;
15051       break;
15052
15053     case OPTION_SINGLE_FLOAT:
15054       file_mips_single_float = 1;
15055       break;
15056
15057     case OPTION_DOUBLE_FLOAT:
15058       file_mips_single_float = 0;
15059       break;
15060
15061     case OPTION_SOFT_FLOAT:
15062       file_mips_soft_float = 1;
15063       break;
15064
15065     case OPTION_HARD_FLOAT:
15066       file_mips_soft_float = 0;
15067       break;
15068
15069 #ifdef OBJ_ELF
15070     case OPTION_MABI:
15071       if (!IS_ELF)
15072         {
15073           as_bad (_("-mabi is supported for ELF format only"));
15074           return 0;
15075         }
15076       if (strcmp (arg, "32") == 0)
15077         mips_abi = O32_ABI;
15078       else if (strcmp (arg, "o64") == 0)
15079         mips_abi = O64_ABI;
15080       else if (strcmp (arg, "n32") == 0)
15081         mips_abi = N32_ABI;
15082       else if (strcmp (arg, "64") == 0)
15083         {
15084           mips_abi = N64_ABI;
15085           if (! support_64bit_objects())
15086             as_fatal (_("No compiled in support for 64 bit object file "
15087                         "format"));
15088         }
15089       else if (strcmp (arg, "eabi") == 0)
15090         mips_abi = EABI_ABI;
15091       else
15092         {
15093           as_fatal (_("invalid abi -mabi=%s"), arg);
15094           return 0;
15095         }
15096       break;
15097 #endif /* OBJ_ELF */
15098
15099     case OPTION_M7000_HILO_FIX:
15100       mips_7000_hilo_fix = TRUE;
15101       break;
15102
15103     case OPTION_MNO_7000_HILO_FIX:
15104       mips_7000_hilo_fix = FALSE;
15105       break;
15106
15107 #ifdef OBJ_ELF
15108     case OPTION_MDEBUG:
15109       mips_flag_mdebug = TRUE;
15110       break;
15111
15112     case OPTION_NO_MDEBUG:
15113       mips_flag_mdebug = FALSE;
15114       break;
15115
15116     case OPTION_PDR:
15117       mips_flag_pdr = TRUE;
15118       break;
15119
15120     case OPTION_NO_PDR:
15121       mips_flag_pdr = FALSE;
15122       break;
15123
15124     case OPTION_MVXWORKS_PIC:
15125       mips_pic = VXWORKS_PIC;
15126       break;
15127 #endif /* OBJ_ELF */
15128
15129     default:
15130       return 0;
15131     }
15132
15133     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15134
15135   return 1;
15136 }
15137 \f
15138 /* Set up globals to generate code for the ISA or processor
15139    described by INFO.  */
15140
15141 static void
15142 mips_set_architecture (const struct mips_cpu_info *info)
15143 {
15144   if (info != 0)
15145     {
15146       file_mips_arch = info->cpu;
15147       mips_opts.arch = info->cpu;
15148       mips_opts.isa = info->isa;
15149     }
15150 }
15151
15152
15153 /* Likewise for tuning.  */
15154
15155 static void
15156 mips_set_tune (const struct mips_cpu_info *info)
15157 {
15158   if (info != 0)
15159     mips_tune = info->cpu;
15160 }
15161
15162
15163 void
15164 mips_after_parse_args (void)
15165 {
15166   const struct mips_cpu_info *arch_info = 0;
15167   const struct mips_cpu_info *tune_info = 0;
15168
15169   /* GP relative stuff not working for PE */
15170   if (strncmp (TARGET_OS, "pe", 2) == 0)
15171     {
15172       if (g_switch_seen && g_switch_value != 0)
15173         as_bad (_("-G not supported in this configuration."));
15174       g_switch_value = 0;
15175     }
15176
15177   if (mips_abi == NO_ABI)
15178     mips_abi = MIPS_DEFAULT_ABI;
15179
15180   /* The following code determines the architecture and register size.
15181      Similar code was added to GCC 3.3 (see override_options() in
15182      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15183      as much as possible.  */
15184
15185   if (mips_arch_string != 0)
15186     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15187
15188   if (file_mips_isa != ISA_UNKNOWN)
15189     {
15190       /* Handle -mipsN.  At this point, file_mips_isa contains the
15191          ISA level specified by -mipsN, while arch_info->isa contains
15192          the -march selection (if any).  */
15193       if (arch_info != 0)
15194         {
15195           /* -march takes precedence over -mipsN, since it is more descriptive.
15196              There's no harm in specifying both as long as the ISA levels
15197              are the same.  */
15198           if (file_mips_isa != arch_info->isa)
15199             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15200                     mips_cpu_info_from_isa (file_mips_isa)->name,
15201                     mips_cpu_info_from_isa (arch_info->isa)->name);
15202         }
15203       else
15204         arch_info = mips_cpu_info_from_isa (file_mips_isa);
15205     }
15206
15207   if (arch_info == 0)
15208     {
15209       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15210       gas_assert (arch_info);
15211     }
15212
15213   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15214     as_bad (_("-march=%s is not compatible with the selected ABI"),
15215             arch_info->name);
15216
15217   mips_set_architecture (arch_info);
15218
15219   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
15220   if (mips_tune_string != 0)
15221     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15222
15223   if (tune_info == 0)
15224     mips_set_tune (arch_info);
15225   else
15226     mips_set_tune (tune_info);
15227
15228   if (file_mips_gp32 >= 0)
15229     {
15230       /* The user specified the size of the integer registers.  Make sure
15231          it agrees with the ABI and ISA.  */
15232       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15233         as_bad (_("-mgp64 used with a 32-bit processor"));
15234       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15235         as_bad (_("-mgp32 used with a 64-bit ABI"));
15236       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15237         as_bad (_("-mgp64 used with a 32-bit ABI"));
15238     }
15239   else
15240     {
15241       /* Infer the integer register size from the ABI and processor.
15242          Restrict ourselves to 32-bit registers if that's all the
15243          processor has, or if the ABI cannot handle 64-bit registers.  */
15244       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15245                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
15246     }
15247
15248   switch (file_mips_fp32)
15249     {
15250     default:
15251     case -1:
15252       /* No user specified float register size.
15253          ??? GAS treats single-float processors as though they had 64-bit
15254          float registers (although it complains when double-precision
15255          instructions are used).  As things stand, saying they have 32-bit
15256          registers would lead to spurious "register must be even" messages.
15257          So here we assume float registers are never smaller than the
15258          integer ones.  */
15259       if (file_mips_gp32 == 0)
15260         /* 64-bit integer registers implies 64-bit float registers.  */
15261         file_mips_fp32 = 0;
15262       else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15263                && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15264         /* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
15265         file_mips_fp32 = 0;
15266       else
15267         /* 32-bit float registers.  */
15268         file_mips_fp32 = 1;
15269       break;
15270
15271     /* The user specified the size of the float registers.  Check if it
15272        agrees with the ABI and ISA.  */
15273     case 0:
15274       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15275         as_bad (_("-mfp64 used with a 32-bit fpu"));
15276       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15277                && !ISA_HAS_MXHC1 (mips_opts.isa))
15278         as_warn (_("-mfp64 used with a 32-bit ABI"));
15279       break;
15280     case 1:
15281       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15282         as_warn (_("-mfp32 used with a 64-bit ABI"));
15283       break;
15284     }
15285
15286   /* End of GCC-shared inference code.  */
15287
15288   /* This flag is set when we have a 64-bit capable CPU but use only
15289      32-bit wide registers.  Note that EABI does not use it.  */
15290   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15291       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15292           || mips_abi == O32_ABI))
15293     mips_32bitmode = 1;
15294
15295   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15296     as_bad (_("trap exception not supported at ISA 1"));
15297
15298   /* If the selected architecture includes support for ASEs, enable
15299      generation of code for them.  */
15300   if (mips_opts.mips16 == -1)
15301     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15302   if (mips_opts.micromips == -1)
15303     mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15304   if (mips_opts.ase_mips3d == -1)
15305     mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
15306                             && file_mips_fp32 == 0) ? 1 : 0;
15307   if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15308     as_bad (_("-mfp32 used with -mips3d"));
15309
15310   if (mips_opts.ase_mdmx == -1)
15311     mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
15312                           && file_mips_fp32 == 0) ? 1 : 0;
15313   if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15314     as_bad (_("-mfp32 used with -mdmx"));
15315
15316   if (mips_opts.ase_smartmips == -1)
15317     mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15318   if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
15319     as_warn (_("%s ISA does not support SmartMIPS"), 
15320              mips_cpu_info_from_isa (mips_opts.isa)->name);
15321
15322   if (mips_opts.ase_dsp == -1)
15323     mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15324   if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
15325     as_warn (_("%s ISA does not support DSP ASE"), 
15326              mips_cpu_info_from_isa (mips_opts.isa)->name);
15327
15328   if (mips_opts.ase_dspr2 == -1)
15329     {
15330       mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15331       mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15332     }
15333   if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
15334     as_warn (_("%s ISA does not support DSP R2 ASE"),
15335              mips_cpu_info_from_isa (mips_opts.isa)->name);
15336
15337   if (mips_opts.ase_mt == -1)
15338     mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15339   if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
15340     as_warn (_("%s ISA does not support MT ASE"),
15341              mips_cpu_info_from_isa (mips_opts.isa)->name);
15342
15343   if (mips_opts.ase_mcu == -1)
15344     mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15345   if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15346       as_warn (_("%s ISA does not support MCU ASE"),
15347                mips_cpu_info_from_isa (mips_opts.isa)->name);
15348
15349   file_mips_isa = mips_opts.isa;
15350   file_ase_mips3d = mips_opts.ase_mips3d;
15351   file_ase_mdmx = mips_opts.ase_mdmx;
15352   file_ase_smartmips = mips_opts.ase_smartmips;
15353   file_ase_dsp = mips_opts.ase_dsp;
15354   file_ase_dspr2 = mips_opts.ase_dspr2;
15355   file_ase_mt = mips_opts.ase_mt;
15356   mips_opts.gp32 = file_mips_gp32;
15357   mips_opts.fp32 = file_mips_fp32;
15358   mips_opts.soft_float = file_mips_soft_float;
15359   mips_opts.single_float = file_mips_single_float;
15360
15361   if (mips_flag_mdebug < 0)
15362     {
15363 #ifdef OBJ_MAYBE_ECOFF
15364       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15365         mips_flag_mdebug = 1;
15366       else
15367 #endif /* OBJ_MAYBE_ECOFF */
15368         mips_flag_mdebug = 0;
15369     }
15370 }
15371 \f
15372 void
15373 mips_init_after_args (void)
15374 {
15375   /* initialize opcodes */
15376   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15377   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15378 }
15379
15380 long
15381 md_pcrel_from (fixS *fixP)
15382 {
15383   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15384   switch (fixP->fx_r_type)
15385     {
15386     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15387     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15388       /* Return the address of the delay slot.  */
15389       return addr + 2;
15390
15391     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15392     case BFD_RELOC_MICROMIPS_JMP:
15393     case BFD_RELOC_16_PCREL_S2:
15394     case BFD_RELOC_MIPS_JMP:
15395       /* Return the address of the delay slot.  */
15396       return addr + 4;
15397
15398     default:
15399       /* We have no relocation type for PC relative MIPS16 instructions.  */
15400       if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15401         as_bad_where (fixP->fx_file, fixP->fx_line,
15402                       _("PC relative MIPS16 instruction references a different section"));
15403       return addr;
15404     }
15405 }
15406
15407 /* This is called before the symbol table is processed.  In order to
15408    work with gcc when using mips-tfile, we must keep all local labels.
15409    However, in other cases, we want to discard them.  If we were
15410    called with -g, but we didn't see any debugging information, it may
15411    mean that gcc is smuggling debugging information through to
15412    mips-tfile, in which case we must generate all local labels.  */
15413
15414 void
15415 mips_frob_file_before_adjust (void)
15416 {
15417 #ifndef NO_ECOFF_DEBUGGING
15418   if (ECOFF_DEBUGGING
15419       && mips_debug != 0
15420       && ! ecoff_debugging_seen)
15421     flag_keep_locals = 1;
15422 #endif
15423 }
15424
15425 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15426    the corresponding LO16 reloc.  This is called before md_apply_fix and
15427    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15428    relocation operators.
15429
15430    For our purposes, a %lo() expression matches a %got() or %hi()
15431    expression if:
15432
15433       (a) it refers to the same symbol; and
15434       (b) the offset applied in the %lo() expression is no lower than
15435           the offset applied in the %got() or %hi().
15436
15437    (b) allows us to cope with code like:
15438
15439         lui     $4,%hi(foo)
15440         lh      $4,%lo(foo+2)($4)
15441
15442    ...which is legal on RELA targets, and has a well-defined behaviour
15443    if the user knows that adding 2 to "foo" will not induce a carry to
15444    the high 16 bits.
15445
15446    When several %lo()s match a particular %got() or %hi(), we use the
15447    following rules to distinguish them:
15448
15449      (1) %lo()s with smaller offsets are a better match than %lo()s with
15450          higher offsets.
15451
15452      (2) %lo()s with no matching %got() or %hi() are better than those
15453          that already have a matching %got() or %hi().
15454
15455      (3) later %lo()s are better than earlier %lo()s.
15456
15457    These rules are applied in order.
15458
15459    (1) means, among other things, that %lo()s with identical offsets are
15460    chosen if they exist.
15461
15462    (2) means that we won't associate several high-part relocations with
15463    the same low-part relocation unless there's no alternative.  Having
15464    several high parts for the same low part is a GNU extension; this rule
15465    allows careful users to avoid it.
15466
15467    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15468    with the last high-part relocation being at the front of the list.
15469    It therefore makes sense to choose the last matching low-part
15470    relocation, all other things being equal.  It's also easier
15471    to code that way.  */
15472
15473 void
15474 mips_frob_file (void)
15475 {
15476   struct mips_hi_fixup *l;
15477   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15478
15479   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15480     {
15481       segment_info_type *seginfo;
15482       bfd_boolean matched_lo_p;
15483       fixS **hi_pos, **lo_pos, **pos;
15484
15485       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15486
15487       /* If a GOT16 relocation turns out to be against a global symbol,
15488          there isn't supposed to be a matching LO.  Ignore %gots against
15489          constants; we'll report an error for those later.  */
15490       if (got16_reloc_p (l->fixp->fx_r_type)
15491           && !(l->fixp->fx_addsy
15492                && pic_need_relax (l->fixp->fx_addsy, l->seg)))
15493         continue;
15494
15495       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15496       if (fixup_has_matching_lo_p (l->fixp))
15497         continue;
15498
15499       seginfo = seg_info (l->seg);
15500
15501       /* Set HI_POS to the position of this relocation in the chain.
15502          Set LO_POS to the position of the chosen low-part relocation.
15503          MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15504          relocation that matches an immediately-preceding high-part
15505          relocation.  */
15506       hi_pos = NULL;
15507       lo_pos = NULL;
15508       matched_lo_p = FALSE;
15509       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15510
15511       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15512         {
15513           if (*pos == l->fixp)
15514             hi_pos = pos;
15515
15516           if ((*pos)->fx_r_type == looking_for_rtype
15517               && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15518               && (*pos)->fx_offset >= l->fixp->fx_offset
15519               && (lo_pos == NULL
15520                   || (*pos)->fx_offset < (*lo_pos)->fx_offset
15521                   || (!matched_lo_p
15522                       && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15523             lo_pos = pos;
15524
15525           matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15526                           && fixup_has_matching_lo_p (*pos));
15527         }
15528
15529       /* If we found a match, remove the high-part relocation from its
15530          current position and insert it before the low-part relocation.
15531          Make the offsets match so that fixup_has_matching_lo_p()
15532          will return true.
15533
15534          We don't warn about unmatched high-part relocations since some
15535          versions of gcc have been known to emit dead "lui ...%hi(...)"
15536          instructions.  */
15537       if (lo_pos != NULL)
15538         {
15539           l->fixp->fx_offset = (*lo_pos)->fx_offset;
15540           if (l->fixp->fx_next != *lo_pos)
15541             {
15542               *hi_pos = l->fixp->fx_next;
15543               l->fixp->fx_next = *lo_pos;
15544               *lo_pos = l->fixp;
15545             }
15546         }
15547     }
15548 }
15549
15550 int
15551 mips_force_relocation (fixS *fixp)
15552 {
15553   if (generic_force_reloc (fixp))
15554     return 1;
15555
15556   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15557      so that the linker relaxation can update targets.  */
15558   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15559       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15560       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15561     return 1;
15562
15563   return 0;
15564 }
15565
15566 /* Read the instruction associated with RELOC from BUF.  */
15567
15568 static unsigned int
15569 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15570 {
15571   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15572     return read_compressed_insn (buf, 4);
15573   else
15574     return read_insn (buf);
15575 }
15576
15577 /* Write instruction INSN to BUF, given that it has been relocated
15578    by RELOC.  */
15579
15580 static void
15581 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15582                   unsigned long insn)
15583 {
15584   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15585     write_compressed_insn (buf, insn, 4);
15586   else
15587     write_insn (buf, insn);
15588 }
15589
15590 /* Apply a fixup to the object file.  */
15591
15592 void
15593 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15594 {
15595   char *buf;
15596   unsigned long insn;
15597   reloc_howto_type *howto;
15598
15599   /* We ignore generic BFD relocations we don't know about.  */
15600   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15601   if (! howto)
15602     return;
15603
15604   gas_assert (fixP->fx_size == 2
15605               || fixP->fx_size == 4
15606               || fixP->fx_r_type == BFD_RELOC_16
15607               || fixP->fx_r_type == BFD_RELOC_64
15608               || fixP->fx_r_type == BFD_RELOC_CTOR
15609               || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15610               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15611               || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15612               || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15613               || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15614
15615   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15616
15617   gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15618               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15619               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15620               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
15621
15622   /* Don't treat parts of a composite relocation as done.  There are two
15623      reasons for this:
15624
15625      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15626          should nevertheless be emitted if the first part is.
15627
15628      (2) In normal usage, composite relocations are never assembly-time
15629          constants.  The easiest way of dealing with the pathological
15630          exceptions is to generate a relocation against STN_UNDEF and
15631          leave everything up to the linker.  */
15632   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15633     fixP->fx_done = 1;
15634
15635   switch (fixP->fx_r_type)
15636     {
15637     case BFD_RELOC_MIPS_TLS_GD:
15638     case BFD_RELOC_MIPS_TLS_LDM:
15639     case BFD_RELOC_MIPS_TLS_DTPREL32:
15640     case BFD_RELOC_MIPS_TLS_DTPREL64:
15641     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15642     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15643     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15644     case BFD_RELOC_MIPS_TLS_TPREL32:
15645     case BFD_RELOC_MIPS_TLS_TPREL64:
15646     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15647     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15648     case BFD_RELOC_MICROMIPS_TLS_GD:
15649     case BFD_RELOC_MICROMIPS_TLS_LDM:
15650     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15651     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15652     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15653     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15654     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15655     case BFD_RELOC_MIPS16_TLS_GD:
15656     case BFD_RELOC_MIPS16_TLS_LDM:
15657     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15658     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15659     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15660     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15661     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15662       if (!fixP->fx_addsy)
15663         {
15664           as_bad_where (fixP->fx_file, fixP->fx_line,
15665                         _("TLS relocation against a constant"));
15666           break;
15667         }
15668       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15669       /* fall through */
15670
15671     case BFD_RELOC_MIPS_JMP:
15672     case BFD_RELOC_MIPS_SHIFT5:
15673     case BFD_RELOC_MIPS_SHIFT6:
15674     case BFD_RELOC_MIPS_GOT_DISP:
15675     case BFD_RELOC_MIPS_GOT_PAGE:
15676     case BFD_RELOC_MIPS_GOT_OFST:
15677     case BFD_RELOC_MIPS_SUB:
15678     case BFD_RELOC_MIPS_INSERT_A:
15679     case BFD_RELOC_MIPS_INSERT_B:
15680     case BFD_RELOC_MIPS_DELETE:
15681     case BFD_RELOC_MIPS_HIGHEST:
15682     case BFD_RELOC_MIPS_HIGHER:
15683     case BFD_RELOC_MIPS_SCN_DISP:
15684     case BFD_RELOC_MIPS_REL16:
15685     case BFD_RELOC_MIPS_RELGOT:
15686     case BFD_RELOC_MIPS_JALR:
15687     case BFD_RELOC_HI16:
15688     case BFD_RELOC_HI16_S:
15689     case BFD_RELOC_LO16:
15690     case BFD_RELOC_GPREL16:
15691     case BFD_RELOC_MIPS_LITERAL:
15692     case BFD_RELOC_MIPS_CALL16:
15693     case BFD_RELOC_MIPS_GOT16:
15694     case BFD_RELOC_GPREL32:
15695     case BFD_RELOC_MIPS_GOT_HI16:
15696     case BFD_RELOC_MIPS_GOT_LO16:
15697     case BFD_RELOC_MIPS_CALL_HI16:
15698     case BFD_RELOC_MIPS_CALL_LO16:
15699     case BFD_RELOC_MIPS16_GPREL:
15700     case BFD_RELOC_MIPS16_GOT16:
15701     case BFD_RELOC_MIPS16_CALL16:
15702     case BFD_RELOC_MIPS16_HI16:
15703     case BFD_RELOC_MIPS16_HI16_S:
15704     case BFD_RELOC_MIPS16_LO16:
15705     case BFD_RELOC_MIPS16_JMP:
15706     case BFD_RELOC_MICROMIPS_JMP:
15707     case BFD_RELOC_MICROMIPS_GOT_DISP:
15708     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15709     case BFD_RELOC_MICROMIPS_GOT_OFST:
15710     case BFD_RELOC_MICROMIPS_SUB:
15711     case BFD_RELOC_MICROMIPS_HIGHEST:
15712     case BFD_RELOC_MICROMIPS_HIGHER:
15713     case BFD_RELOC_MICROMIPS_SCN_DISP:
15714     case BFD_RELOC_MICROMIPS_JALR:
15715     case BFD_RELOC_MICROMIPS_HI16:
15716     case BFD_RELOC_MICROMIPS_HI16_S:
15717     case BFD_RELOC_MICROMIPS_LO16:
15718     case BFD_RELOC_MICROMIPS_GPREL16:
15719     case BFD_RELOC_MICROMIPS_LITERAL:
15720     case BFD_RELOC_MICROMIPS_CALL16:
15721     case BFD_RELOC_MICROMIPS_GOT16:
15722     case BFD_RELOC_MICROMIPS_GOT_HI16:
15723     case BFD_RELOC_MICROMIPS_GOT_LO16:
15724     case BFD_RELOC_MICROMIPS_CALL_HI16:
15725     case BFD_RELOC_MICROMIPS_CALL_LO16:
15726       if (fixP->fx_done)
15727         {
15728           offsetT value;
15729
15730           if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15731             {
15732               insn = read_reloc_insn (buf, fixP->fx_r_type);
15733               if (mips16_reloc_p (fixP->fx_r_type))
15734                 insn |= mips16_immed_extend (value, 16);
15735               else
15736                 insn |= (value & 0xffff);
15737               write_reloc_insn (buf, fixP->fx_r_type, insn);
15738             }
15739           else
15740             as_bad_where (fixP->fx_file, fixP->fx_line,
15741                           _("Unsupported constant in relocation"));
15742         }
15743       break;
15744
15745     case BFD_RELOC_64:
15746       /* This is handled like BFD_RELOC_32, but we output a sign
15747          extended value if we are only 32 bits.  */
15748       if (fixP->fx_done)
15749         {
15750           if (8 <= sizeof (valueT))
15751             md_number_to_chars (buf, *valP, 8);
15752           else
15753             {
15754               valueT hiv;
15755
15756               if ((*valP & 0x80000000) != 0)
15757                 hiv = 0xffffffff;
15758               else
15759                 hiv = 0;
15760               md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15761               md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15762             }
15763         }
15764       break;
15765
15766     case BFD_RELOC_RVA:
15767     case BFD_RELOC_32:
15768     case BFD_RELOC_16:
15769       /* If we are deleting this reloc entry, we must fill in the
15770          value now.  This can happen if we have a .word which is not
15771          resolved when it appears but is later defined.  */
15772       if (fixP->fx_done)
15773         md_number_to_chars (buf, *valP, fixP->fx_size);
15774       break;
15775
15776     case BFD_RELOC_16_PCREL_S2:
15777       if ((*valP & 0x3) != 0)
15778         as_bad_where (fixP->fx_file, fixP->fx_line,
15779                       _("Branch to misaligned address (%lx)"), (long) *valP);
15780
15781       /* We need to save the bits in the instruction since fixup_segment()
15782          might be deleting the relocation entry (i.e., a branch within
15783          the current segment).  */
15784       if (! fixP->fx_done)
15785         break;
15786
15787       /* Update old instruction data.  */
15788       insn = read_insn (buf);
15789
15790       if (*valP + 0x20000 <= 0x3ffff)
15791         {
15792           insn |= (*valP >> 2) & 0xffff;
15793           write_insn (buf, insn);
15794         }
15795       else if (mips_pic == NO_PIC
15796                && fixP->fx_done
15797                && fixP->fx_frag->fr_address >= text_section->vma
15798                && (fixP->fx_frag->fr_address
15799                    < text_section->vma + bfd_get_section_size (text_section))
15800                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
15801                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
15802                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15803         {
15804           /* The branch offset is too large.  If this is an
15805              unconditional branch, and we are not generating PIC code,
15806              we can convert it to an absolute jump instruction.  */
15807           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
15808             insn = 0x0c000000;  /* jal */
15809           else
15810             insn = 0x08000000;  /* j */
15811           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15812           fixP->fx_done = 0;
15813           fixP->fx_addsy = section_symbol (text_section);
15814           *valP += md_pcrel_from (fixP);
15815           write_insn (buf, insn);
15816         }
15817       else
15818         {
15819           /* If we got here, we have branch-relaxation disabled,
15820              and there's nothing we can do to fix this instruction
15821              without turning it into a longer sequence.  */
15822           as_bad_where (fixP->fx_file, fixP->fx_line,
15823                         _("Branch out of range"));
15824         }
15825       break;
15826
15827     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15828     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15829     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15830       /* We adjust the offset back to even.  */
15831       if ((*valP & 0x1) != 0)
15832         --(*valP);
15833
15834       if (! fixP->fx_done)
15835         break;
15836
15837       /* Should never visit here, because we keep the relocation.  */
15838       abort ();
15839       break;
15840
15841     case BFD_RELOC_VTABLE_INHERIT:
15842       fixP->fx_done = 0;
15843       if (fixP->fx_addsy
15844           && !S_IS_DEFINED (fixP->fx_addsy)
15845           && !S_IS_WEAK (fixP->fx_addsy))
15846         S_SET_WEAK (fixP->fx_addsy);
15847       break;
15848
15849     case BFD_RELOC_VTABLE_ENTRY:
15850       fixP->fx_done = 0;
15851       break;
15852
15853     default:
15854       abort ();
15855     }
15856
15857   /* Remember value for tc_gen_reloc.  */
15858   fixP->fx_addnumber = *valP;
15859 }
15860
15861 static symbolS *
15862 get_symbol (void)
15863 {
15864   int c;
15865   char *name;
15866   symbolS *p;
15867
15868   name = input_line_pointer;
15869   c = get_symbol_end ();
15870   p = (symbolS *) symbol_find_or_make (name);
15871   *input_line_pointer = c;
15872   return p;
15873 }
15874
15875 /* Align the current frag to a given power of two.  If a particular
15876    fill byte should be used, FILL points to an integer that contains
15877    that byte, otherwise FILL is null.
15878
15879    This function used to have the comment:
15880
15881       The MIPS assembler also automatically adjusts any preceding label.
15882
15883    The implementation therefore applied the adjustment to a maximum of
15884    one label.  However, other label adjustments are applied to batches
15885    of labels, and adjusting just one caused problems when new labels
15886    were added for the sake of debugging or unwind information.
15887    We therefore adjust all preceding labels (given as LABELS) instead.  */
15888
15889 static void
15890 mips_align (int to, int *fill, struct insn_label_list *labels)
15891 {
15892   mips_emit_delays ();
15893   mips_record_compressed_mode ();
15894   if (fill == NULL && subseg_text_p (now_seg))
15895     frag_align_code (to, 0);
15896   else
15897     frag_align (to, fill ? *fill : 0, 0);
15898   record_alignment (now_seg, to);
15899   mips_move_labels (labels, FALSE);
15900 }
15901
15902 /* Align to a given power of two.  .align 0 turns off the automatic
15903    alignment used by the data creating pseudo-ops.  */
15904
15905 static void
15906 s_align (int x ATTRIBUTE_UNUSED)
15907 {
15908   int temp, fill_value, *fill_ptr;
15909   long max_alignment = 28;
15910
15911   /* o Note that the assembler pulls down any immediately preceding label
15912        to the aligned address.
15913      o It's not documented but auto alignment is reinstated by
15914        a .align pseudo instruction.
15915      o Note also that after auto alignment is turned off the mips assembler
15916        issues an error on attempt to assemble an improperly aligned data item.
15917        We don't.  */
15918
15919   temp = get_absolute_expression ();
15920   if (temp > max_alignment)
15921     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15922   else if (temp < 0)
15923     {
15924       as_warn (_("Alignment negative: 0 assumed."));
15925       temp = 0;
15926     }
15927   if (*input_line_pointer == ',')
15928     {
15929       ++input_line_pointer;
15930       fill_value = get_absolute_expression ();
15931       fill_ptr = &fill_value;
15932     }
15933   else
15934     fill_ptr = 0;
15935   if (temp)
15936     {
15937       segment_info_type *si = seg_info (now_seg);
15938       struct insn_label_list *l = si->label_list;
15939       /* Auto alignment should be switched on by next section change.  */
15940       auto_align = 1;
15941       mips_align (temp, fill_ptr, l);
15942     }
15943   else
15944     {
15945       auto_align = 0;
15946     }
15947
15948   demand_empty_rest_of_line ();
15949 }
15950
15951 static void
15952 s_change_sec (int sec)
15953 {
15954   segT seg;
15955
15956 #ifdef OBJ_ELF
15957   /* The ELF backend needs to know that we are changing sections, so
15958      that .previous works correctly.  We could do something like check
15959      for an obj_section_change_hook macro, but that might be confusing
15960      as it would not be appropriate to use it in the section changing
15961      functions in read.c, since obj-elf.c intercepts those.  FIXME:
15962      This should be cleaner, somehow.  */
15963   if (IS_ELF)
15964     obj_elf_section_change_hook ();
15965 #endif
15966
15967   mips_emit_delays ();
15968
15969   switch (sec)
15970     {
15971     case 't':
15972       s_text (0);
15973       break;
15974     case 'd':
15975       s_data (0);
15976       break;
15977     case 'b':
15978       subseg_set (bss_section, (subsegT) get_absolute_expression ());
15979       demand_empty_rest_of_line ();
15980       break;
15981
15982     case 'r':
15983       seg = subseg_new (RDATA_SECTION_NAME,
15984                         (subsegT) get_absolute_expression ());
15985       if (IS_ELF)
15986         {
15987           bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15988                                                   | SEC_READONLY | SEC_RELOC
15989                                                   | SEC_DATA));
15990           if (strncmp (TARGET_OS, "elf", 3) != 0)
15991             record_alignment (seg, 4);
15992         }
15993       demand_empty_rest_of_line ();
15994       break;
15995
15996     case 's':
15997       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15998       if (IS_ELF)
15999         {
16000           bfd_set_section_flags (stdoutput, seg,
16001                                  SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16002           if (strncmp (TARGET_OS, "elf", 3) != 0)
16003             record_alignment (seg, 4);
16004         }
16005       demand_empty_rest_of_line ();
16006       break;
16007
16008     case 'B':
16009       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16010       if (IS_ELF)
16011         {
16012           bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16013           if (strncmp (TARGET_OS, "elf", 3) != 0)
16014             record_alignment (seg, 4);
16015         }
16016       demand_empty_rest_of_line ();
16017       break;
16018     }
16019
16020   auto_align = 1;
16021 }
16022
16023 void
16024 s_change_section (int ignore ATTRIBUTE_UNUSED)
16025 {
16026 #ifdef OBJ_ELF
16027   char *section_name;
16028   char c;
16029   char next_c = 0;
16030   int section_type;
16031   int section_flag;
16032   int section_entry_size;
16033   int section_alignment;
16034
16035   if (!IS_ELF)
16036     return;
16037
16038   section_name = input_line_pointer;
16039   c = get_symbol_end ();
16040   if (c)
16041     next_c = *(input_line_pointer + 1);
16042
16043   /* Do we have .section Name<,"flags">?  */
16044   if (c != ',' || (c == ',' && next_c == '"'))
16045     {
16046       /* just after name is now '\0'.  */
16047       *input_line_pointer = c;
16048       input_line_pointer = section_name;
16049       obj_elf_section (ignore);
16050       return;
16051     }
16052   input_line_pointer++;
16053
16054   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16055   if (c == ',')
16056     section_type = get_absolute_expression ();
16057   else
16058     section_type = 0;
16059   if (*input_line_pointer++ == ',')
16060     section_flag = get_absolute_expression ();
16061   else
16062     section_flag = 0;
16063   if (*input_line_pointer++ == ',')
16064     section_entry_size = get_absolute_expression ();
16065   else
16066     section_entry_size = 0;
16067   if (*input_line_pointer++ == ',')
16068     section_alignment = get_absolute_expression ();
16069   else
16070     section_alignment = 0;
16071   /* FIXME: really ignore?  */
16072   (void) section_alignment;
16073
16074   section_name = xstrdup (section_name);
16075
16076   /* When using the generic form of .section (as implemented by obj-elf.c),
16077      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16078      traditionally had to fall back on the more common @progbits instead.
16079
16080      There's nothing really harmful in this, since bfd will correct
16081      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16082      means that, for backwards compatibility, the special_section entries
16083      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16084
16085      Even so, we shouldn't force users of the MIPS .section syntax to
16086      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16087      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16088      generic type-checking code.  */
16089   if (section_type == SHT_MIPS_DWARF)
16090     section_type = SHT_PROGBITS;
16091
16092   obj_elf_change_section (section_name, section_type, section_flag,
16093                           section_entry_size, 0, 0, 0);
16094
16095   if (now_seg->name != section_name)
16096     free (section_name);
16097 #endif /* OBJ_ELF */
16098 }
16099
16100 void
16101 mips_enable_auto_align (void)
16102 {
16103   auto_align = 1;
16104 }
16105
16106 static void
16107 s_cons (int log_size)
16108 {
16109   segment_info_type *si = seg_info (now_seg);
16110   struct insn_label_list *l = si->label_list;
16111
16112   mips_emit_delays ();
16113   if (log_size > 0 && auto_align)
16114     mips_align (log_size, 0, l);
16115   cons (1 << log_size);
16116   mips_clear_insn_labels ();
16117 }
16118
16119 static void
16120 s_float_cons (int type)
16121 {
16122   segment_info_type *si = seg_info (now_seg);
16123   struct insn_label_list *l = si->label_list;
16124
16125   mips_emit_delays ();
16126
16127   if (auto_align)
16128     {
16129       if (type == 'd')
16130         mips_align (3, 0, l);
16131       else
16132         mips_align (2, 0, l);
16133     }
16134
16135   float_cons (type);
16136   mips_clear_insn_labels ();
16137 }
16138
16139 /* Handle .globl.  We need to override it because on Irix 5 you are
16140    permitted to say
16141        .globl foo .text
16142    where foo is an undefined symbol, to mean that foo should be
16143    considered to be the address of a function.  */
16144
16145 static void
16146 s_mips_globl (int x ATTRIBUTE_UNUSED)
16147 {
16148   char *name;
16149   int c;
16150   symbolS *symbolP;
16151   flagword flag;
16152
16153   do
16154     {
16155       name = input_line_pointer;
16156       c = get_symbol_end ();
16157       symbolP = symbol_find_or_make (name);
16158       S_SET_EXTERNAL (symbolP);
16159
16160       *input_line_pointer = c;
16161       SKIP_WHITESPACE ();
16162
16163       /* On Irix 5, every global symbol that is not explicitly labelled as
16164          being a function is apparently labelled as being an object.  */
16165       flag = BSF_OBJECT;
16166
16167       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16168           && (*input_line_pointer != ','))
16169         {
16170           char *secname;
16171           asection *sec;
16172
16173           secname = input_line_pointer;
16174           c = get_symbol_end ();
16175           sec = bfd_get_section_by_name (stdoutput, secname);
16176           if (sec == NULL)
16177             as_bad (_("%s: no such section"), secname);
16178           *input_line_pointer = c;
16179
16180           if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16181             flag = BSF_FUNCTION;
16182         }
16183
16184       symbol_get_bfdsym (symbolP)->flags |= flag;
16185
16186       c = *input_line_pointer;
16187       if (c == ',')
16188         {
16189           input_line_pointer++;
16190           SKIP_WHITESPACE ();
16191           if (is_end_of_line[(unsigned char) *input_line_pointer])
16192             c = '\n';
16193         }
16194     }
16195   while (c == ',');
16196
16197   demand_empty_rest_of_line ();
16198 }
16199
16200 static void
16201 s_option (int x ATTRIBUTE_UNUSED)
16202 {
16203   char *opt;
16204   char c;
16205
16206   opt = input_line_pointer;
16207   c = get_symbol_end ();
16208
16209   if (*opt == 'O')
16210     {
16211       /* FIXME: What does this mean?  */
16212     }
16213   else if (strncmp (opt, "pic", 3) == 0)
16214     {
16215       int i;
16216
16217       i = atoi (opt + 3);
16218       if (i == 0)
16219         mips_pic = NO_PIC;
16220       else if (i == 2)
16221         {
16222           mips_pic = SVR4_PIC;
16223           mips_abicalls = TRUE;
16224         }
16225       else
16226         as_bad (_(".option pic%d not supported"), i);
16227
16228       if (mips_pic == SVR4_PIC)
16229         {
16230           if (g_switch_seen && g_switch_value != 0)
16231             as_warn (_("-G may not be used with SVR4 PIC code"));
16232           g_switch_value = 0;
16233           bfd_set_gp_size (stdoutput, 0);
16234         }
16235     }
16236   else
16237     as_warn (_("Unrecognized option \"%s\""), opt);
16238
16239   *input_line_pointer = c;
16240   demand_empty_rest_of_line ();
16241 }
16242
16243 /* This structure is used to hold a stack of .set values.  */
16244
16245 struct mips_option_stack
16246 {
16247   struct mips_option_stack *next;
16248   struct mips_set_options options;
16249 };
16250
16251 static struct mips_option_stack *mips_opts_stack;
16252
16253 /* Handle the .set pseudo-op.  */
16254
16255 static void
16256 s_mipsset (int x ATTRIBUTE_UNUSED)
16257 {
16258   char *name = input_line_pointer, ch;
16259
16260   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16261     ++input_line_pointer;
16262   ch = *input_line_pointer;
16263   *input_line_pointer = '\0';
16264
16265   if (strcmp (name, "reorder") == 0)
16266     {
16267       if (mips_opts.noreorder)
16268         end_noreorder ();
16269     }
16270   else if (strcmp (name, "noreorder") == 0)
16271     {
16272       if (!mips_opts.noreorder)
16273         start_noreorder ();
16274     }
16275   else if (strncmp (name, "at=", 3) == 0)
16276     {
16277       char *s = name + 3;
16278
16279       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16280         as_bad (_("Unrecognized register name `%s'"), s);
16281     }
16282   else if (strcmp (name, "at") == 0)
16283     {
16284       mips_opts.at = ATREG;
16285     }
16286   else if (strcmp (name, "noat") == 0)
16287     {
16288       mips_opts.at = ZERO;
16289     }
16290   else if (strcmp (name, "macro") == 0)
16291     {
16292       mips_opts.warn_about_macros = 0;
16293     }
16294   else if (strcmp (name, "nomacro") == 0)
16295     {
16296       if (mips_opts.noreorder == 0)
16297         as_bad (_("`noreorder' must be set before `nomacro'"));
16298       mips_opts.warn_about_macros = 1;
16299     }
16300   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16301     {
16302       mips_opts.nomove = 0;
16303     }
16304   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16305     {
16306       mips_opts.nomove = 1;
16307     }
16308   else if (strcmp (name, "bopt") == 0)
16309     {
16310       mips_opts.nobopt = 0;
16311     }
16312   else if (strcmp (name, "nobopt") == 0)
16313     {
16314       mips_opts.nobopt = 1;
16315     }
16316   else if (strcmp (name, "gp=default") == 0)
16317     mips_opts.gp32 = file_mips_gp32;
16318   else if (strcmp (name, "gp=32") == 0)
16319     mips_opts.gp32 = 1;
16320   else if (strcmp (name, "gp=64") == 0)
16321     {
16322       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16323         as_warn (_("%s isa does not support 64-bit registers"),
16324                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16325       mips_opts.gp32 = 0;
16326     }
16327   else if (strcmp (name, "fp=default") == 0)
16328     mips_opts.fp32 = file_mips_fp32;
16329   else if (strcmp (name, "fp=32") == 0)
16330     mips_opts.fp32 = 1;
16331   else if (strcmp (name, "fp=64") == 0)
16332     {
16333       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16334         as_warn (_("%s isa does not support 64-bit floating point registers"),
16335                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16336       mips_opts.fp32 = 0;
16337     }
16338   else if (strcmp (name, "softfloat") == 0)
16339     mips_opts.soft_float = 1;
16340   else if (strcmp (name, "hardfloat") == 0)
16341     mips_opts.soft_float = 0;
16342   else if (strcmp (name, "singlefloat") == 0)
16343     mips_opts.single_float = 1;
16344   else if (strcmp (name, "doublefloat") == 0)
16345     mips_opts.single_float = 0;
16346   else if (strcmp (name, "mips16") == 0
16347            || strcmp (name, "MIPS-16") == 0)
16348     {
16349       if (mips_opts.micromips == 1)
16350         as_fatal (_("`mips16' cannot be used with `micromips'"));
16351       mips_opts.mips16 = 1;
16352     }
16353   else if (strcmp (name, "nomips16") == 0
16354            || strcmp (name, "noMIPS-16") == 0)
16355     mips_opts.mips16 = 0;
16356   else if (strcmp (name, "micromips") == 0)
16357     {
16358       if (mips_opts.mips16 == 1)
16359         as_fatal (_("`micromips' cannot be used with `mips16'"));
16360       mips_opts.micromips = 1;
16361     }
16362   else if (strcmp (name, "nomicromips") == 0)
16363     mips_opts.micromips = 0;
16364   else if (strcmp (name, "smartmips") == 0)
16365     {
16366       if (!ISA_SUPPORTS_SMARTMIPS)
16367         as_warn (_("%s ISA does not support SmartMIPS ASE"), 
16368                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16369       mips_opts.ase_smartmips = 1;
16370     }
16371   else if (strcmp (name, "nosmartmips") == 0)
16372     mips_opts.ase_smartmips = 0;
16373   else if (strcmp (name, "mips3d") == 0)
16374     mips_opts.ase_mips3d = 1;
16375   else if (strcmp (name, "nomips3d") == 0)
16376     mips_opts.ase_mips3d = 0;
16377   else if (strcmp (name, "mdmx") == 0)
16378     mips_opts.ase_mdmx = 1;
16379   else if (strcmp (name, "nomdmx") == 0)
16380     mips_opts.ase_mdmx = 0;
16381   else if (strcmp (name, "dsp") == 0)
16382     {
16383       if (!ISA_SUPPORTS_DSP_ASE)
16384         as_warn (_("%s ISA does not support DSP ASE"), 
16385                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16386       mips_opts.ase_dsp = 1;
16387       mips_opts.ase_dspr2 = 0;
16388     }
16389   else if (strcmp (name, "nodsp") == 0)
16390     {
16391       mips_opts.ase_dsp = 0;
16392       mips_opts.ase_dspr2 = 0;
16393     }
16394   else if (strcmp (name, "dspr2") == 0)
16395     {
16396       if (!ISA_SUPPORTS_DSPR2_ASE)
16397         as_warn (_("%s ISA does not support DSP R2 ASE"),
16398                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16399       mips_opts.ase_dspr2 = 1;
16400       mips_opts.ase_dsp = 1;
16401     }
16402   else if (strcmp (name, "nodspr2") == 0)
16403     {
16404       mips_opts.ase_dspr2 = 0;
16405       mips_opts.ase_dsp = 0;
16406     }
16407   else if (strcmp (name, "mt") == 0)
16408     {
16409       if (!ISA_SUPPORTS_MT_ASE)
16410         as_warn (_("%s ISA does not support MT ASE"), 
16411                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16412       mips_opts.ase_mt = 1;
16413     }
16414   else if (strcmp (name, "nomt") == 0)
16415     mips_opts.ase_mt = 0;
16416   else if (strcmp (name, "mcu") == 0)
16417     mips_opts.ase_mcu = 1;
16418   else if (strcmp (name, "nomcu") == 0)
16419     mips_opts.ase_mcu = 0;
16420   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16421     {
16422       int reset = 0;
16423
16424       /* Permit the user to change the ISA and architecture on the fly.
16425          Needless to say, misuse can cause serious problems.  */
16426       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16427         {
16428           reset = 1;
16429           mips_opts.isa = file_mips_isa;
16430           mips_opts.arch = file_mips_arch;
16431         }
16432       else if (strncmp (name, "arch=", 5) == 0)
16433         {
16434           const struct mips_cpu_info *p;
16435
16436           p = mips_parse_cpu("internal use", name + 5);
16437           if (!p)
16438             as_bad (_("unknown architecture %s"), name + 5);
16439           else
16440             {
16441               mips_opts.arch = p->cpu;
16442               mips_opts.isa = p->isa;
16443             }
16444         }
16445       else if (strncmp (name, "mips", 4) == 0)
16446         {
16447           const struct mips_cpu_info *p;
16448
16449           p = mips_parse_cpu("internal use", name);
16450           if (!p)
16451             as_bad (_("unknown ISA level %s"), name + 4);
16452           else
16453             {
16454               mips_opts.arch = p->cpu;
16455               mips_opts.isa = p->isa;
16456             }
16457         }
16458       else
16459         as_bad (_("unknown ISA or architecture %s"), name);
16460
16461       switch (mips_opts.isa)
16462         {
16463         case  0:
16464           break;
16465         case ISA_MIPS1:
16466         case ISA_MIPS2:
16467         case ISA_MIPS32:
16468         case ISA_MIPS32R2:
16469           mips_opts.gp32 = 1;
16470           mips_opts.fp32 = 1;
16471           break;
16472         case ISA_MIPS3:
16473         case ISA_MIPS4:
16474         case ISA_MIPS5:
16475         case ISA_MIPS64:
16476         case ISA_MIPS64R2:
16477           mips_opts.gp32 = 0;
16478           if (mips_opts.arch == CPU_R5900)
16479             {
16480                 mips_opts.fp32 = 1;
16481             }
16482           else
16483             {
16484           mips_opts.fp32 = 0;
16485             }
16486           break;
16487         default:
16488           as_bad (_("unknown ISA level %s"), name + 4);
16489           break;
16490         }
16491       if (reset)
16492         {
16493           mips_opts.gp32 = file_mips_gp32;
16494           mips_opts.fp32 = file_mips_fp32;
16495         }
16496     }
16497   else if (strcmp (name, "autoextend") == 0)
16498     mips_opts.noautoextend = 0;
16499   else if (strcmp (name, "noautoextend") == 0)
16500     mips_opts.noautoextend = 1;
16501   else if (strcmp (name, "push") == 0)
16502     {
16503       struct mips_option_stack *s;
16504
16505       s = (struct mips_option_stack *) xmalloc (sizeof *s);
16506       s->next = mips_opts_stack;
16507       s->options = mips_opts;
16508       mips_opts_stack = s;
16509     }
16510   else if (strcmp (name, "pop") == 0)
16511     {
16512       struct mips_option_stack *s;
16513
16514       s = mips_opts_stack;
16515       if (s == NULL)
16516         as_bad (_(".set pop with no .set push"));
16517       else
16518         {
16519           /* If we're changing the reorder mode we need to handle
16520              delay slots correctly.  */
16521           if (s->options.noreorder && ! mips_opts.noreorder)
16522             start_noreorder ();
16523           else if (! s->options.noreorder && mips_opts.noreorder)
16524             end_noreorder ();
16525
16526           mips_opts = s->options;
16527           mips_opts_stack = s->next;
16528           free (s);
16529         }
16530     }
16531   else if (strcmp (name, "sym32") == 0)
16532     mips_opts.sym32 = TRUE;
16533   else if (strcmp (name, "nosym32") == 0)
16534     mips_opts.sym32 = FALSE;
16535   else if (strchr (name, ','))
16536     {
16537       /* Generic ".set" directive; use the generic handler.  */
16538       *input_line_pointer = ch;
16539       input_line_pointer = name;
16540       s_set (0);
16541       return;
16542     }
16543   else
16544     {
16545       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16546     }
16547   *input_line_pointer = ch;
16548   demand_empty_rest_of_line ();
16549 }
16550
16551 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16552    .option pic2.  It means to generate SVR4 PIC calls.  */
16553
16554 static void
16555 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16556 {
16557   mips_pic = SVR4_PIC;
16558   mips_abicalls = TRUE;
16559
16560   if (g_switch_seen && g_switch_value != 0)
16561     as_warn (_("-G may not be used with SVR4 PIC code"));
16562   g_switch_value = 0;
16563
16564   bfd_set_gp_size (stdoutput, 0);
16565   demand_empty_rest_of_line ();
16566 }
16567
16568 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16569    PIC code.  It sets the $gp register for the function based on the
16570    function address, which is in the register named in the argument.
16571    This uses a relocation against _gp_disp, which is handled specially
16572    by the linker.  The result is:
16573         lui     $gp,%hi(_gp_disp)
16574         addiu   $gp,$gp,%lo(_gp_disp)
16575         addu    $gp,$gp,.cpload argument
16576    The .cpload argument is normally $25 == $t9.
16577
16578    The -mno-shared option changes this to:
16579         lui     $gp,%hi(__gnu_local_gp)
16580         addiu   $gp,$gp,%lo(__gnu_local_gp)
16581    and the argument is ignored.  This saves an instruction, but the
16582    resulting code is not position independent; it uses an absolute
16583    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16584    can go into an ordinary executable, but not into a shared library.  */
16585
16586 static void
16587 s_cpload (int ignore ATTRIBUTE_UNUSED)
16588 {
16589   expressionS ex;
16590   int reg;
16591   int in_shared;
16592
16593   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16594      .cpload is ignored.  */
16595   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16596     {
16597       s_ignore (0);
16598       return;
16599     }
16600
16601   if (mips_opts.mips16)
16602     {
16603       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16604       ignore_rest_of_line ();
16605       return;
16606     }
16607
16608   /* .cpload should be in a .set noreorder section.  */
16609   if (mips_opts.noreorder == 0)
16610     as_warn (_(".cpload not in noreorder section"));
16611
16612   reg = tc_get_register (0);
16613
16614   /* If we need to produce a 64-bit address, we are better off using
16615      the default instruction sequence.  */
16616   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16617
16618   ex.X_op = O_symbol;
16619   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16620                                          "__gnu_local_gp");
16621   ex.X_op_symbol = NULL;
16622   ex.X_add_number = 0;
16623
16624   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16625   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16626
16627   mips_mark_labels ();
16628   mips_assembling_insn = TRUE;
16629
16630   macro_start ();
16631   macro_build_lui (&ex, mips_gp_register);
16632   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16633                mips_gp_register, BFD_RELOC_LO16);
16634   if (in_shared)
16635     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16636                  mips_gp_register, reg);
16637   macro_end ();
16638
16639   mips_assembling_insn = FALSE;
16640   demand_empty_rest_of_line ();
16641 }
16642
16643 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16644      .cpsetup $reg1, offset|$reg2, label
16645
16646    If offset is given, this results in:
16647      sd         $gp, offset($sp)
16648      lui        $gp, %hi(%neg(%gp_rel(label)))
16649      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16650      daddu      $gp, $gp, $reg1
16651
16652    If $reg2 is given, this results in:
16653      daddu      $reg2, $gp, $0
16654      lui        $gp, %hi(%neg(%gp_rel(label)))
16655      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16656      daddu      $gp, $gp, $reg1
16657    $reg1 is normally $25 == $t9.
16658
16659    The -mno-shared option replaces the last three instructions with
16660         lui     $gp,%hi(_gp)
16661         addiu   $gp,$gp,%lo(_gp)  */
16662
16663 static void
16664 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16665 {
16666   expressionS ex_off;
16667   expressionS ex_sym;
16668   int reg1;
16669
16670   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16671      We also need NewABI support.  */
16672   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16673     {
16674       s_ignore (0);
16675       return;
16676     }
16677
16678   if (mips_opts.mips16)
16679     {
16680       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16681       ignore_rest_of_line ();
16682       return;
16683     }
16684
16685   reg1 = tc_get_register (0);
16686   SKIP_WHITESPACE ();
16687   if (*input_line_pointer != ',')
16688     {
16689       as_bad (_("missing argument separator ',' for .cpsetup"));
16690       return;
16691     }
16692   else
16693     ++input_line_pointer;
16694   SKIP_WHITESPACE ();
16695   if (*input_line_pointer == '$')
16696     {
16697       mips_cpreturn_register = tc_get_register (0);
16698       mips_cpreturn_offset = -1;
16699     }
16700   else
16701     {
16702       mips_cpreturn_offset = get_absolute_expression ();
16703       mips_cpreturn_register = -1;
16704     }
16705   SKIP_WHITESPACE ();
16706   if (*input_line_pointer != ',')
16707     {
16708       as_bad (_("missing argument separator ',' for .cpsetup"));
16709       return;
16710     }
16711   else
16712     ++input_line_pointer;
16713   SKIP_WHITESPACE ();
16714   expression (&ex_sym);
16715
16716   mips_mark_labels ();
16717   mips_assembling_insn = TRUE;
16718
16719   macro_start ();
16720   if (mips_cpreturn_register == -1)
16721     {
16722       ex_off.X_op = O_constant;
16723       ex_off.X_add_symbol = NULL;
16724       ex_off.X_op_symbol = NULL;
16725       ex_off.X_add_number = mips_cpreturn_offset;
16726
16727       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16728                    BFD_RELOC_LO16, SP);
16729     }
16730   else
16731     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16732                  mips_gp_register, 0);
16733
16734   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16735     {
16736       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16737                    -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16738                    BFD_RELOC_HI16_S);
16739
16740       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16741                    mips_gp_register, -1, BFD_RELOC_GPREL16,
16742                    BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16743
16744       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16745                    mips_gp_register, reg1);
16746     }
16747   else
16748     {
16749       expressionS ex;
16750
16751       ex.X_op = O_symbol;
16752       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16753       ex.X_op_symbol = NULL;
16754       ex.X_add_number = 0;
16755
16756       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16757       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16758
16759       macro_build_lui (&ex, mips_gp_register);
16760       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16761                    mips_gp_register, BFD_RELOC_LO16);
16762     }
16763
16764   macro_end ();
16765
16766   mips_assembling_insn = FALSE;
16767   demand_empty_rest_of_line ();
16768 }
16769
16770 static void
16771 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16772 {
16773   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16774      .cplocal is ignored.  */
16775   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16776     {
16777       s_ignore (0);
16778       return;
16779     }
16780
16781   if (mips_opts.mips16)
16782     {
16783       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16784       ignore_rest_of_line ();
16785       return;
16786     }
16787
16788   mips_gp_register = tc_get_register (0);
16789   demand_empty_rest_of_line ();
16790 }
16791
16792 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16793    offset from $sp.  The offset is remembered, and after making a PIC
16794    call $gp is restored from that location.  */
16795
16796 static void
16797 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16798 {
16799   expressionS ex;
16800
16801   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16802      .cprestore is ignored.  */
16803   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16804     {
16805       s_ignore (0);
16806       return;
16807     }
16808
16809   if (mips_opts.mips16)
16810     {
16811       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16812       ignore_rest_of_line ();
16813       return;
16814     }
16815
16816   mips_cprestore_offset = get_absolute_expression ();
16817   mips_cprestore_valid = 1;
16818
16819   ex.X_op = O_constant;
16820   ex.X_add_symbol = NULL;
16821   ex.X_op_symbol = NULL;
16822   ex.X_add_number = mips_cprestore_offset;
16823
16824   mips_mark_labels ();
16825   mips_assembling_insn = TRUE;
16826
16827   macro_start ();
16828   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16829                                 SP, HAVE_64BIT_ADDRESSES);
16830   macro_end ();
16831
16832   mips_assembling_insn = FALSE;
16833   demand_empty_rest_of_line ();
16834 }
16835
16836 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16837    was given in the preceding .cpsetup, it results in:
16838      ld         $gp, offset($sp)
16839
16840    If a register $reg2 was given there, it results in:
16841      daddu      $gp, $reg2, $0  */
16842
16843 static void
16844 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16845 {
16846   expressionS ex;
16847
16848   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16849      We also need NewABI support.  */
16850   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16851     {
16852       s_ignore (0);
16853       return;
16854     }
16855
16856   if (mips_opts.mips16)
16857     {
16858       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16859       ignore_rest_of_line ();
16860       return;
16861     }
16862
16863   mips_mark_labels ();
16864   mips_assembling_insn = TRUE;
16865
16866   macro_start ();
16867   if (mips_cpreturn_register == -1)
16868     {
16869       ex.X_op = O_constant;
16870       ex.X_add_symbol = NULL;
16871       ex.X_op_symbol = NULL;
16872       ex.X_add_number = mips_cpreturn_offset;
16873
16874       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16875     }
16876   else
16877     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16878                  mips_cpreturn_register, 0);
16879   macro_end ();
16880
16881   mips_assembling_insn = FALSE;
16882   demand_empty_rest_of_line ();
16883 }
16884
16885 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16886    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16887    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16888    debug information or MIPS16 TLS.  */
16889
16890 static void
16891 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16892                      bfd_reloc_code_real_type rtype)
16893 {
16894   expressionS ex;
16895   char *p;
16896
16897   expression (&ex);
16898
16899   if (ex.X_op != O_symbol)
16900     {
16901       as_bad (_("Unsupported use of %s"), dirstr);
16902       ignore_rest_of_line ();
16903     }
16904
16905   p = frag_more (bytes);
16906   md_number_to_chars (p, 0, bytes);
16907   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16908   demand_empty_rest_of_line ();
16909   mips_clear_insn_labels ();
16910 }
16911
16912 /* Handle .dtprelword.  */
16913
16914 static void
16915 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16916 {
16917   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16918 }
16919
16920 /* Handle .dtpreldword.  */
16921
16922 static void
16923 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16924 {
16925   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16926 }
16927
16928 /* Handle .tprelword.  */
16929
16930 static void
16931 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16932 {
16933   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16934 }
16935
16936 /* Handle .tpreldword.  */
16937
16938 static void
16939 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16940 {
16941   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16942 }
16943
16944 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
16945    code.  It sets the offset to use in gp_rel relocations.  */
16946
16947 static void
16948 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16949 {
16950   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16951      We also need NewABI support.  */
16952   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16953     {
16954       s_ignore (0);
16955       return;
16956     }
16957
16958   mips_gprel_offset = get_absolute_expression ();
16959
16960   demand_empty_rest_of_line ();
16961 }
16962
16963 /* Handle the .gpword pseudo-op.  This is used when generating PIC
16964    code.  It generates a 32 bit GP relative reloc.  */
16965
16966 static void
16967 s_gpword (int ignore ATTRIBUTE_UNUSED)
16968 {
16969   segment_info_type *si;
16970   struct insn_label_list *l;
16971   expressionS ex;
16972   char *p;
16973
16974   /* When not generating PIC code, this is treated as .word.  */
16975   if (mips_pic != SVR4_PIC)
16976     {
16977       s_cons (2);
16978       return;
16979     }
16980
16981   si = seg_info (now_seg);
16982   l = si->label_list;
16983   mips_emit_delays ();
16984   if (auto_align)
16985     mips_align (2, 0, l);
16986
16987   expression (&ex);
16988   mips_clear_insn_labels ();
16989
16990   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16991     {
16992       as_bad (_("Unsupported use of .gpword"));
16993       ignore_rest_of_line ();
16994     }
16995
16996   p = frag_more (4);
16997   md_number_to_chars (p, 0, 4);
16998   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16999                BFD_RELOC_GPREL32);
17000
17001   demand_empty_rest_of_line ();
17002 }
17003
17004 static void
17005 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17006 {
17007   segment_info_type *si;
17008   struct insn_label_list *l;
17009   expressionS ex;
17010   char *p;
17011
17012   /* When not generating PIC code, this is treated as .dword.  */
17013   if (mips_pic != SVR4_PIC)
17014     {
17015       s_cons (3);
17016       return;
17017     }
17018
17019   si = seg_info (now_seg);
17020   l = si->label_list;
17021   mips_emit_delays ();
17022   if (auto_align)
17023     mips_align (3, 0, l);
17024
17025   expression (&ex);
17026   mips_clear_insn_labels ();
17027
17028   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17029     {
17030       as_bad (_("Unsupported use of .gpdword"));
17031       ignore_rest_of_line ();
17032     }
17033
17034   p = frag_more (8);
17035   md_number_to_chars (p, 0, 8);
17036   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17037                BFD_RELOC_GPREL32)->fx_tcbit = 1;
17038
17039   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
17040   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17041            FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17042
17043   demand_empty_rest_of_line ();
17044 }
17045
17046 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
17047    tables in SVR4 PIC code.  */
17048
17049 static void
17050 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17051 {
17052   int reg;
17053
17054   /* This is ignored when not generating SVR4 PIC code.  */
17055   if (mips_pic != SVR4_PIC)
17056     {
17057       s_ignore (0);
17058       return;
17059     }
17060
17061   mips_mark_labels ();
17062   mips_assembling_insn = TRUE;
17063
17064   /* Add $gp to the register named as an argument.  */
17065   macro_start ();
17066   reg = tc_get_register (0);
17067   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17068   macro_end ();
17069
17070   mips_assembling_insn = FALSE;
17071   demand_empty_rest_of_line ();
17072 }
17073
17074 /* Handle the .insn pseudo-op.  This marks instruction labels in
17075    mips16/micromips mode.  This permits the linker to handle them specially,
17076    such as generating jalx instructions when needed.  We also make
17077    them odd for the duration of the assembly, in order to generate the
17078    right sort of code.  We will make them even in the adjust_symtab
17079    routine, while leaving them marked.  This is convenient for the
17080    debugger and the disassembler.  The linker knows to make them odd
17081    again.  */
17082
17083 static void
17084 s_insn (int ignore ATTRIBUTE_UNUSED)
17085 {
17086   mips_mark_labels ();
17087
17088   demand_empty_rest_of_line ();
17089 }
17090
17091 /* Handle a .stabn directive.  We need these in order to mark a label
17092    as being a mips16 text label correctly.  Sometimes the compiler
17093    will emit a label, followed by a .stabn, and then switch sections.
17094    If the label and .stabn are in mips16 mode, then the label is
17095    really a mips16 text label.  */
17096
17097 static void
17098 s_mips_stab (int type)
17099 {
17100   if (type == 'n')
17101     mips_mark_labels ();
17102
17103   s_stab (type);
17104 }
17105
17106 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17107
17108 static void
17109 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17110 {
17111   char *name;
17112   int c;
17113   symbolS *symbolP;
17114   expressionS exp;
17115
17116   name = input_line_pointer;
17117   c = get_symbol_end ();
17118   symbolP = symbol_find_or_make (name);
17119   S_SET_WEAK (symbolP);
17120   *input_line_pointer = c;
17121
17122   SKIP_WHITESPACE ();
17123
17124   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17125     {
17126       if (S_IS_DEFINED (symbolP))
17127         {
17128           as_bad (_("ignoring attempt to redefine symbol %s"),
17129                   S_GET_NAME (symbolP));
17130           ignore_rest_of_line ();
17131           return;
17132         }
17133
17134       if (*input_line_pointer == ',')
17135         {
17136           ++input_line_pointer;
17137           SKIP_WHITESPACE ();
17138         }
17139
17140       expression (&exp);
17141       if (exp.X_op != O_symbol)
17142         {
17143           as_bad (_("bad .weakext directive"));
17144           ignore_rest_of_line ();
17145           return;
17146         }
17147       symbol_set_value_expression (symbolP, &exp);
17148     }
17149
17150   demand_empty_rest_of_line ();
17151 }
17152
17153 /* Parse a register string into a number.  Called from the ECOFF code
17154    to parse .frame.  The argument is non-zero if this is the frame
17155    register, so that we can record it in mips_frame_reg.  */
17156
17157 int
17158 tc_get_register (int frame)
17159 {
17160   unsigned int reg;
17161
17162   SKIP_WHITESPACE ();
17163   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17164     reg = 0;
17165   if (frame)
17166     {
17167       mips_frame_reg = reg != 0 ? reg : SP;
17168       mips_frame_reg_valid = 1;
17169       mips_cprestore_valid = 0;
17170     }
17171   return reg;
17172 }
17173
17174 valueT
17175 md_section_align (asection *seg, valueT addr)
17176 {
17177   int align = bfd_get_section_alignment (stdoutput, seg);
17178
17179   if (IS_ELF)
17180     {
17181       /* We don't need to align ELF sections to the full alignment.
17182          However, Irix 5 may prefer that we align them at least to a 16
17183          byte boundary.  We don't bother to align the sections if we
17184          are targeted for an embedded system.  */
17185       if (strncmp (TARGET_OS, "elf", 3) == 0)
17186         return addr;
17187       if (align > 4)
17188         align = 4;
17189     }
17190
17191   return ((addr + (1 << align) - 1) & (-1 << align));
17192 }
17193
17194 /* Utility routine, called from above as well.  If called while the
17195    input file is still being read, it's only an approximation.  (For
17196    example, a symbol may later become defined which appeared to be
17197    undefined earlier.)  */
17198
17199 static int
17200 nopic_need_relax (symbolS *sym, int before_relaxing)
17201 {
17202   if (sym == 0)
17203     return 0;
17204
17205   if (g_switch_value > 0)
17206     {
17207       const char *symname;
17208       int change;
17209
17210       /* Find out whether this symbol can be referenced off the $gp
17211          register.  It can be if it is smaller than the -G size or if
17212          it is in the .sdata or .sbss section.  Certain symbols can
17213          not be referenced off the $gp, although it appears as though
17214          they can.  */
17215       symname = S_GET_NAME (sym);
17216       if (symname != (const char *) NULL
17217           && (strcmp (symname, "eprol") == 0
17218               || strcmp (symname, "etext") == 0
17219               || strcmp (symname, "_gp") == 0
17220               || strcmp (symname, "edata") == 0
17221               || strcmp (symname, "_fbss") == 0
17222               || strcmp (symname, "_fdata") == 0
17223               || strcmp (symname, "_ftext") == 0
17224               || strcmp (symname, "end") == 0
17225               || strcmp (symname, "_gp_disp") == 0))
17226         change = 1;
17227       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17228                && (0
17229 #ifndef NO_ECOFF_DEBUGGING
17230                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
17231                        && (symbol_get_obj (sym)->ecoff_extern_size
17232                            <= g_switch_value))
17233 #endif
17234                    /* We must defer this decision until after the whole
17235                       file has been read, since there might be a .extern
17236                       after the first use of this symbol.  */
17237                    || (before_relaxing
17238 #ifndef NO_ECOFF_DEBUGGING
17239                        && symbol_get_obj (sym)->ecoff_extern_size == 0
17240 #endif
17241                        && S_GET_VALUE (sym) == 0)
17242                    || (S_GET_VALUE (sym) != 0
17243                        && S_GET_VALUE (sym) <= g_switch_value)))
17244         change = 0;
17245       else
17246         {
17247           const char *segname;
17248
17249           segname = segment_name (S_GET_SEGMENT (sym));
17250           gas_assert (strcmp (segname, ".lit8") != 0
17251                   && strcmp (segname, ".lit4") != 0);
17252           change = (strcmp (segname, ".sdata") != 0
17253                     && strcmp (segname, ".sbss") != 0
17254                     && strncmp (segname, ".sdata.", 7) != 0
17255                     && strncmp (segname, ".sbss.", 6) != 0
17256                     && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17257                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17258         }
17259       return change;
17260     }
17261   else
17262     /* We are not optimizing for the $gp register.  */
17263     return 1;
17264 }
17265
17266
17267 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17268
17269 static bfd_boolean
17270 pic_need_relax (symbolS *sym, asection *segtype)
17271 {
17272   asection *symsec;
17273
17274   /* Handle the case of a symbol equated to another symbol.  */
17275   while (symbol_equated_reloc_p (sym))
17276     {
17277       symbolS *n;
17278
17279       /* It's possible to get a loop here in a badly written program.  */
17280       n = symbol_get_value_expression (sym)->X_add_symbol;
17281       if (n == sym)
17282         break;
17283       sym = n;
17284     }
17285
17286   if (symbol_section_p (sym))
17287     return TRUE;
17288
17289   symsec = S_GET_SEGMENT (sym);
17290
17291   /* This must duplicate the test in adjust_reloc_syms.  */
17292   return (!bfd_is_und_section (symsec)
17293           && !bfd_is_abs_section (symsec)
17294           && !bfd_is_com_section (symsec)
17295           && !s_is_linkonce (sym, segtype)
17296 #ifdef OBJ_ELF
17297           /* A global or weak symbol is treated as external.  */
17298           && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
17299 #endif
17300           );
17301 }
17302
17303
17304 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17305    extended opcode.  SEC is the section the frag is in.  */
17306
17307 static int
17308 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17309 {
17310   int type;
17311   const struct mips16_immed_operand *op;
17312   offsetT val;
17313   int mintiny, maxtiny;
17314   segT symsec;
17315   fragS *sym_frag;
17316
17317   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17318     return 0;
17319   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17320     return 1;
17321
17322   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17323   op = mips16_immed_operands;
17324   while (op->type != type)
17325     {
17326       ++op;
17327       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17328     }
17329
17330   if (op->unsp)
17331     {
17332       if (type == '<' || type == '>' || type == '[' || type == ']')
17333         {
17334           mintiny = 1;
17335           maxtiny = 1 << op->nbits;
17336         }
17337       else
17338         {
17339           mintiny = 0;
17340           maxtiny = (1 << op->nbits) - 1;
17341         }
17342     }
17343   else
17344     {
17345       mintiny = - (1 << (op->nbits - 1));
17346       maxtiny = (1 << (op->nbits - 1)) - 1;
17347     }
17348
17349   sym_frag = symbol_get_frag (fragp->fr_symbol);
17350   val = S_GET_VALUE (fragp->fr_symbol);
17351   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17352
17353   if (op->pcrel)
17354     {
17355       addressT addr;
17356
17357       /* We won't have the section when we are called from
17358          mips_relax_frag.  However, we will always have been called
17359          from md_estimate_size_before_relax first.  If this is a
17360          branch to a different section, we mark it as such.  If SEC is
17361          NULL, and the frag is not marked, then it must be a branch to
17362          the same section.  */
17363       if (sec == NULL)
17364         {
17365           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17366             return 1;
17367         }
17368       else
17369         {
17370           /* Must have been called from md_estimate_size_before_relax.  */
17371           if (symsec != sec)
17372             {
17373               fragp->fr_subtype =
17374                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17375
17376               /* FIXME: We should support this, and let the linker
17377                  catch branches and loads that are out of range.  */
17378               as_bad_where (fragp->fr_file, fragp->fr_line,
17379                             _("unsupported PC relative reference to different section"));
17380
17381               return 1;
17382             }
17383           if (fragp != sym_frag && sym_frag->fr_address == 0)
17384             /* Assume non-extended on the first relaxation pass.
17385                The address we have calculated will be bogus if this is
17386                a forward branch to another frag, as the forward frag
17387                will have fr_address == 0.  */
17388             return 0;
17389         }
17390
17391       /* In this case, we know for sure that the symbol fragment is in
17392          the same section.  If the relax_marker of the symbol fragment
17393          differs from the relax_marker of this fragment, we have not
17394          yet adjusted the symbol fragment fr_address.  We want to add
17395          in STRETCH in order to get a better estimate of the address.
17396          This particularly matters because of the shift bits.  */
17397       if (stretch != 0
17398           && sym_frag->relax_marker != fragp->relax_marker)
17399         {
17400           fragS *f;
17401
17402           /* Adjust stretch for any alignment frag.  Note that if have
17403              been expanding the earlier code, the symbol may be
17404              defined in what appears to be an earlier frag.  FIXME:
17405              This doesn't handle the fr_subtype field, which specifies
17406              a maximum number of bytes to skip when doing an
17407              alignment.  */
17408           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17409             {
17410               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17411                 {
17412                   if (stretch < 0)
17413                     stretch = - ((- stretch)
17414                                  & ~ ((1 << (int) f->fr_offset) - 1));
17415                   else
17416                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17417                   if (stretch == 0)
17418                     break;
17419                 }
17420             }
17421           if (f != NULL)
17422             val += stretch;
17423         }
17424
17425       addr = fragp->fr_address + fragp->fr_fix;
17426
17427       /* The base address rules are complicated.  The base address of
17428          a branch is the following instruction.  The base address of a
17429          PC relative load or add is the instruction itself, but if it
17430          is in a delay slot (in which case it can not be extended) use
17431          the address of the instruction whose delay slot it is in.  */
17432       if (type == 'p' || type == 'q')
17433         {
17434           addr += 2;
17435
17436           /* If we are currently assuming that this frag should be
17437              extended, then, the current address is two bytes
17438              higher.  */
17439           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17440             addr += 2;
17441
17442           /* Ignore the low bit in the target, since it will be set
17443              for a text label.  */
17444           if ((val & 1) != 0)
17445             --val;
17446         }
17447       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17448         addr -= 4;
17449       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17450         addr -= 2;
17451
17452       val -= addr & ~ ((1 << op->shift) - 1);
17453
17454       /* Branch offsets have an implicit 0 in the lowest bit.  */
17455       if (type == 'p' || type == 'q')
17456         val /= 2;
17457
17458       /* If any of the shifted bits are set, we must use an extended
17459          opcode.  If the address depends on the size of this
17460          instruction, this can lead to a loop, so we arrange to always
17461          use an extended opcode.  We only check this when we are in
17462          the main relaxation loop, when SEC is NULL.  */
17463       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17464         {
17465           fragp->fr_subtype =
17466             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17467           return 1;
17468         }
17469
17470       /* If we are about to mark a frag as extended because the value
17471          is precisely maxtiny + 1, then there is a chance of an
17472          infinite loop as in the following code:
17473              la $4,foo
17474              .skip      1020
17475              .align     2
17476            foo:
17477          In this case when the la is extended, foo is 0x3fc bytes
17478          away, so the la can be shrunk, but then foo is 0x400 away, so
17479          the la must be extended.  To avoid this loop, we mark the
17480          frag as extended if it was small, and is about to become
17481          extended with a value of maxtiny + 1.  */
17482       if (val == ((maxtiny + 1) << op->shift)
17483           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17484           && sec == NULL)
17485         {
17486           fragp->fr_subtype =
17487             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17488           return 1;
17489         }
17490     }
17491   else if (symsec != absolute_section && sec != NULL)
17492     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17493
17494   if ((val & ((1 << op->shift) - 1)) != 0
17495       || val < (mintiny << op->shift)
17496       || val > (maxtiny << op->shift))
17497     return 1;
17498   else
17499     return 0;
17500 }
17501
17502 /* Compute the length of a branch sequence, and adjust the
17503    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17504    worst-case length is computed, with UPDATE being used to indicate
17505    whether an unconditional (-1), branch-likely (+1) or regular (0)
17506    branch is to be computed.  */
17507 static int
17508 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17509 {
17510   bfd_boolean toofar;
17511   int length;
17512
17513   if (fragp
17514       && S_IS_DEFINED (fragp->fr_symbol)
17515       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17516     {
17517       addressT addr;
17518       offsetT val;
17519
17520       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17521
17522       addr = fragp->fr_address + fragp->fr_fix + 4;
17523
17524       val -= addr;
17525
17526       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17527     }
17528   else if (fragp)
17529     /* If the symbol is not defined or it's in a different segment,
17530        assume the user knows what's going on and emit a short
17531        branch.  */
17532     toofar = FALSE;
17533   else
17534     toofar = TRUE;
17535
17536   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17537     fragp->fr_subtype
17538       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17539                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17540                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17541                              RELAX_BRANCH_LINK (fragp->fr_subtype),
17542                              toofar);
17543
17544   length = 4;
17545   if (toofar)
17546     {
17547       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17548         length += 8;
17549
17550       if (mips_pic != NO_PIC)
17551         {
17552           /* Additional space for PIC loading of target address.  */
17553           length += 8;
17554           if (mips_opts.isa == ISA_MIPS1)
17555             /* Additional space for $at-stabilizing nop.  */
17556             length += 4;
17557         }
17558
17559       /* If branch is conditional.  */
17560       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17561         length += 8;
17562     }
17563
17564   return length;
17565 }
17566
17567 /* Compute the length of a branch sequence, and adjust the
17568    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
17569    worst-case length is computed, with UPDATE being used to indicate
17570    whether an unconditional (-1), or regular (0) branch is to be
17571    computed.  */
17572
17573 static int
17574 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17575 {
17576   bfd_boolean toofar;
17577   int length;
17578
17579   if (fragp
17580       && S_IS_DEFINED (fragp->fr_symbol)
17581       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17582     {
17583       addressT addr;
17584       offsetT val;
17585
17586       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17587       /* Ignore the low bit in the target, since it will be set
17588          for a text label.  */
17589       if ((val & 1) != 0)
17590         --val;
17591
17592       addr = fragp->fr_address + fragp->fr_fix + 4;
17593
17594       val -= addr;
17595
17596       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17597     }
17598   else if (fragp)
17599     /* If the symbol is not defined or it's in a different segment,
17600        assume the user knows what's going on and emit a short
17601        branch.  */
17602     toofar = FALSE;
17603   else
17604     toofar = TRUE;
17605
17606   if (fragp && update
17607       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17608     fragp->fr_subtype = (toofar
17609                          ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17610                          : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17611
17612   length = 4;
17613   if (toofar)
17614     {
17615       bfd_boolean compact_known = fragp != NULL;
17616       bfd_boolean compact = FALSE;
17617       bfd_boolean uncond;
17618
17619       if (compact_known)
17620         compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17621       if (fragp)
17622         uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17623       else
17624         uncond = update < 0;
17625
17626       /* If label is out of range, we turn branch <br>:
17627
17628                 <br>    label                   # 4 bytes
17629             0:
17630
17631          into:
17632
17633                 j       label                   # 4 bytes
17634                 nop                             # 2 bytes if compact && !PIC
17635             0:
17636        */
17637       if (mips_pic == NO_PIC && (!compact_known || compact))
17638         length += 2;
17639
17640       /* If assembling PIC code, we further turn:
17641
17642                         j       label                   # 4 bytes
17643
17644          into:
17645
17646                         lw/ld   at, %got(label)(gp)     # 4 bytes
17647                         d/addiu at, %lo(label)          # 4 bytes
17648                         jr/c    at                      # 2 bytes
17649        */
17650       if (mips_pic != NO_PIC)
17651         length += 6;
17652
17653       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17654
17655                         <brneg> 0f                      # 4 bytes
17656                         nop                             # 2 bytes if !compact
17657        */
17658       if (!uncond)
17659         length += (compact_known && compact) ? 4 : 6;
17660     }
17661
17662   return length;
17663 }
17664
17665 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17666    bit accordingly.  */
17667
17668 static int
17669 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17670 {
17671   bfd_boolean toofar;
17672
17673   if (fragp
17674       && S_IS_DEFINED (fragp->fr_symbol)
17675       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17676     {
17677       addressT addr;
17678       offsetT val;
17679       int type;
17680
17681       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17682       /* Ignore the low bit in the target, since it will be set
17683          for a text label.  */
17684       if ((val & 1) != 0)
17685         --val;
17686
17687       /* Assume this is a 2-byte branch.  */
17688       addr = fragp->fr_address + fragp->fr_fix + 2;
17689
17690       /* We try to avoid the infinite loop by not adding 2 more bytes for
17691          long branches.  */
17692
17693       val -= addr;
17694
17695       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17696       if (type == 'D')
17697         toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17698       else if (type == 'E')
17699         toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17700       else
17701         abort ();
17702     }
17703   else
17704     /* If the symbol is not defined or it's in a different segment,
17705        we emit a normal 32-bit branch.  */
17706     toofar = TRUE;
17707
17708   if (fragp && update
17709       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17710     fragp->fr_subtype
17711       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17712                : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17713
17714   if (toofar)
17715     return 4;
17716
17717   return 2;
17718 }
17719
17720 /* Estimate the size of a frag before relaxing.  Unless this is the
17721    mips16, we are not really relaxing here, and the final size is
17722    encoded in the subtype information.  For the mips16, we have to
17723    decide whether we are using an extended opcode or not.  */
17724
17725 int
17726 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17727 {
17728   int change;
17729
17730   if (RELAX_BRANCH_P (fragp->fr_subtype))
17731     {
17732
17733       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17734
17735       return fragp->fr_var;
17736     }
17737
17738   if (RELAX_MIPS16_P (fragp->fr_subtype))
17739     /* We don't want to modify the EXTENDED bit here; it might get us
17740        into infinite loops.  We change it only in mips_relax_frag().  */
17741     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17742
17743   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17744     {
17745       int length = 4;
17746
17747       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17748         length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17749       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17750         length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17751       fragp->fr_var = length;
17752
17753       return length;
17754     }
17755
17756   if (mips_pic == NO_PIC)
17757     change = nopic_need_relax (fragp->fr_symbol, 0);
17758   else if (mips_pic == SVR4_PIC)
17759     change = pic_need_relax (fragp->fr_symbol, segtype);
17760   else if (mips_pic == VXWORKS_PIC)
17761     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17762     change = 0;
17763   else
17764     abort ();
17765
17766   if (change)
17767     {
17768       fragp->fr_subtype |= RELAX_USE_SECOND;
17769       return -RELAX_FIRST (fragp->fr_subtype);
17770     }
17771   else
17772     return -RELAX_SECOND (fragp->fr_subtype);
17773 }
17774
17775 /* This is called to see whether a reloc against a defined symbol
17776    should be converted into a reloc against a section.  */
17777
17778 int
17779 mips_fix_adjustable (fixS *fixp)
17780 {
17781   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17782       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17783     return 0;
17784
17785   if (fixp->fx_addsy == NULL)
17786     return 1;
17787
17788   /* If symbol SYM is in a mergeable section, relocations of the form
17789      SYM + 0 can usually be made section-relative.  The mergeable data
17790      is then identified by the section offset rather than by the symbol.
17791
17792      However, if we're generating REL LO16 relocations, the offset is split
17793      between the LO16 and parterning high part relocation.  The linker will
17794      need to recalculate the complete offset in order to correctly identify
17795      the merge data.
17796
17797      The linker has traditionally not looked for the parterning high part
17798      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17799      placed anywhere.  Rather than break backwards compatibility by changing
17800      this, it seems better not to force the issue, and instead keep the
17801      original symbol.  This will work with either linker behavior.  */
17802   if ((lo16_reloc_p (fixp->fx_r_type)
17803        || reloc_needs_lo_p (fixp->fx_r_type))
17804       && HAVE_IN_PLACE_ADDENDS
17805       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17806     return 0;
17807
17808   /* There is no place to store an in-place offset for JALR relocations.
17809      Likewise an in-range offset of PC-relative relocations may overflow
17810      the in-place relocatable field if recalculated against the start
17811      address of the symbol's containing section.  */
17812   if (HAVE_IN_PLACE_ADDENDS
17813       && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
17814     return 0;
17815
17816 #ifdef OBJ_ELF
17817   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17818      to a floating-point stub.  The same is true for non-R_MIPS16_26
17819      relocations against MIPS16 functions; in this case, the stub becomes
17820      the function's canonical address.
17821
17822      Floating-point stubs are stored in unique .mips16.call.* or
17823      .mips16.fn.* sections.  If a stub T for function F is in section S,
17824      the first relocation in section S must be against F; this is how the
17825      linker determines the target function.  All relocations that might
17826      resolve to T must also be against F.  We therefore have the following
17827      restrictions, which are given in an intentionally-redundant way:
17828
17829        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17830           symbols.
17831
17832        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17833           if that stub might be used.
17834
17835        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17836           symbols.
17837
17838        4. We cannot reduce a stub's relocations against MIPS16 symbols if
17839           that stub might be used.
17840
17841      There is a further restriction:
17842
17843        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17844           R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17845           targets with in-place addends; the relocation field cannot
17846           encode the low bit.
17847
17848      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17849      against a MIPS16 symbol.  We deal with (5) by by not reducing any
17850      such relocations on REL targets.
17851
17852      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17853      relocation against some symbol R, no relocation against R may be
17854      reduced.  (Note that this deals with (2) as well as (1) because
17855      relocations against global symbols will never be reduced on ELF
17856      targets.)  This approach is a little simpler than trying to detect
17857      stub sections, and gives the "all or nothing" per-symbol consistency
17858      that we have for MIPS16 symbols.  */
17859   if (IS_ELF
17860       && fixp->fx_subsy == NULL
17861       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17862           || *symbol_get_tc (fixp->fx_addsy)
17863           || (HAVE_IN_PLACE_ADDENDS
17864               && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17865               && jmp_reloc_p (fixp->fx_r_type))))
17866     return 0;
17867 #endif
17868
17869   return 1;
17870 }
17871
17872 /* Translate internal representation of relocation info to BFD target
17873    format.  */
17874
17875 arelent **
17876 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17877 {
17878   static arelent *retval[4];
17879   arelent *reloc;
17880   bfd_reloc_code_real_type code;
17881
17882   memset (retval, 0, sizeof(retval));
17883   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17884   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17885   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17886   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17887
17888   if (fixp->fx_pcrel)
17889     {
17890       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17891                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17892                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17893                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
17894
17895       /* At this point, fx_addnumber is "symbol offset - pcrel address".
17896          Relocations want only the symbol offset.  */
17897       reloc->addend = fixp->fx_addnumber + reloc->address;
17898       if (!IS_ELF)
17899         {
17900           /* A gruesome hack which is a result of the gruesome gas
17901              reloc handling.  What's worse, for COFF (as opposed to
17902              ECOFF), we might need yet another copy of reloc->address.
17903              See bfd_install_relocation.  */
17904           reloc->addend += reloc->address;
17905         }
17906     }
17907   else
17908     reloc->addend = fixp->fx_addnumber;
17909
17910   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17911      entry to be used in the relocation's section offset.  */
17912   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17913     {
17914       reloc->address = reloc->addend;
17915       reloc->addend = 0;
17916     }
17917
17918   code = fixp->fx_r_type;
17919
17920   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17921   if (reloc->howto == NULL)
17922     {
17923       as_bad_where (fixp->fx_file, fixp->fx_line,
17924                     _("Can not represent %s relocation in this object file format"),
17925                     bfd_get_reloc_code_name (code));
17926       retval[0] = NULL;
17927     }
17928
17929   return retval;
17930 }
17931
17932 /* Relax a machine dependent frag.  This returns the amount by which
17933    the current size of the frag should change.  */
17934
17935 int
17936 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17937 {
17938   if (RELAX_BRANCH_P (fragp->fr_subtype))
17939     {
17940       offsetT old_var = fragp->fr_var;
17941
17942       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17943
17944       return fragp->fr_var - old_var;
17945     }
17946
17947   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17948     {
17949       offsetT old_var = fragp->fr_var;
17950       offsetT new_var = 4;
17951
17952       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17953         new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17954       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17955         new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17956       fragp->fr_var = new_var;
17957
17958       return new_var - old_var;
17959     }
17960
17961   if (! RELAX_MIPS16_P (fragp->fr_subtype))
17962     return 0;
17963
17964   if (mips16_extended_frag (fragp, NULL, stretch))
17965     {
17966       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17967         return 0;
17968       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17969       return 2;
17970     }
17971   else
17972     {
17973       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17974         return 0;
17975       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17976       return -2;
17977     }
17978
17979   return 0;
17980 }
17981
17982 /* Convert a machine dependent frag.  */
17983
17984 void
17985 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17986 {
17987   if (RELAX_BRANCH_P (fragp->fr_subtype))
17988     {
17989       char *buf;
17990       unsigned long insn;
17991       expressionS exp;
17992       fixS *fixp;
17993
17994       buf = fragp->fr_literal + fragp->fr_fix;
17995       insn = read_insn (buf);
17996
17997       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17998         {
17999           /* We generate a fixup instead of applying it right now
18000              because, if there are linker relaxations, we're going to
18001              need the relocations.  */
18002           exp.X_op = O_symbol;
18003           exp.X_add_symbol = fragp->fr_symbol;
18004           exp.X_add_number = fragp->fr_offset;
18005
18006           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18007                               BFD_RELOC_16_PCREL_S2);
18008           fixp->fx_file = fragp->fr_file;
18009           fixp->fx_line = fragp->fr_line;
18010
18011           buf = write_insn (buf, insn);
18012         }
18013       else
18014         {
18015           int i;
18016
18017           as_warn_where (fragp->fr_file, fragp->fr_line,
18018                          _("Relaxed out-of-range branch into a jump"));
18019
18020           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18021             goto uncond;
18022
18023           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18024             {
18025               /* Reverse the branch.  */
18026               switch ((insn >> 28) & 0xf)
18027                 {
18028                 case 4:
18029                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
18030                      have the condition reversed by tweaking a single
18031                      bit, and their opcodes all have 0x4???????.  */
18032                   gas_assert ((insn & 0xf1000000) == 0x41000000);
18033                   insn ^= 0x00010000;
18034                   break;
18035
18036                 case 0:
18037                   /* bltz       0x04000000      bgez    0x04010000
18038                      bltzal     0x04100000      bgezal  0x04110000  */
18039                   gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18040                   insn ^= 0x00010000;
18041                   break;
18042
18043                 case 1:
18044                   /* beq        0x10000000      bne     0x14000000
18045                      blez       0x18000000      bgtz    0x1c000000  */
18046                   insn ^= 0x04000000;
18047                   break;
18048
18049                 default:
18050                   abort ();
18051                 }
18052             }
18053
18054           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18055             {
18056               /* Clear the and-link bit.  */
18057               gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18058
18059               /* bltzal         0x04100000      bgezal  0x04110000
18060                  bltzall        0x04120000      bgezall 0x04130000  */
18061               insn &= ~0x00100000;
18062             }
18063
18064           /* Branch over the branch (if the branch was likely) or the
18065              full jump (not likely case).  Compute the offset from the
18066              current instruction to branch to.  */
18067           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18068             i = 16;
18069           else
18070             {
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             }
18076           /* Convert to instruction count.  */
18077           i >>= 2;
18078           /* Branch counts from the next instruction.  */
18079           i--;
18080           insn |= i;
18081           /* Branch over the jump.  */
18082           buf = write_insn (buf, insn);
18083
18084           /* nop */
18085           buf = write_insn (buf, 0);
18086
18087           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18088             {
18089               /* beql $0, $0, 2f */
18090               insn = 0x50000000;
18091               /* Compute the PC offset from the current instruction to
18092                  the end of the variable frag.  */
18093               /* How many bytes in instructions we've already emitted?  */
18094               i = buf - fragp->fr_literal - fragp->fr_fix;
18095               /* How many bytes in instructions from here to the end?  */
18096               i = fragp->fr_var - i;
18097               /* Convert to instruction count.  */
18098               i >>= 2;
18099               /* Don't decrement i, because we want to branch over the
18100                  delay slot.  */
18101               insn |= i;
18102
18103               buf = write_insn (buf, insn);
18104               buf = write_insn (buf, 0);
18105             }
18106
18107         uncond:
18108           if (mips_pic == NO_PIC)
18109             {
18110               /* j or jal.  */
18111               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18112                       ? 0x0c000000 : 0x08000000);
18113               exp.X_op = O_symbol;
18114               exp.X_add_symbol = fragp->fr_symbol;
18115               exp.X_add_number = fragp->fr_offset;
18116
18117               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18118                                   FALSE, BFD_RELOC_MIPS_JMP);
18119               fixp->fx_file = fragp->fr_file;
18120               fixp->fx_line = fragp->fr_line;
18121
18122               buf = write_insn (buf, insn);
18123             }
18124           else
18125             {
18126               unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18127
18128               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18129               insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18130               insn |= at << OP_SH_RT;
18131               exp.X_op = O_symbol;
18132               exp.X_add_symbol = fragp->fr_symbol;
18133               exp.X_add_number = fragp->fr_offset;
18134
18135               if (fragp->fr_offset)
18136                 {
18137                   exp.X_add_symbol = make_expr_symbol (&exp);
18138                   exp.X_add_number = 0;
18139                 }
18140
18141               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18142                                   FALSE, BFD_RELOC_MIPS_GOT16);
18143               fixp->fx_file = fragp->fr_file;
18144               fixp->fx_line = fragp->fr_line;
18145
18146               buf = write_insn (buf, insn);
18147
18148               if (mips_opts.isa == ISA_MIPS1)
18149                 /* nop */
18150                 buf = write_insn (buf, 0);
18151
18152               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18153               insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18154               insn |= at << OP_SH_RS | at << OP_SH_RT;
18155
18156               fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18157                                   FALSE, BFD_RELOC_LO16);
18158               fixp->fx_file = fragp->fr_file;
18159               fixp->fx_line = fragp->fr_line;
18160
18161               buf = write_insn (buf, insn);
18162
18163               /* j(al)r $at.  */
18164               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18165                 insn = 0x0000f809;
18166               else
18167                 insn = 0x00000008;
18168               insn |= at << OP_SH_RS;
18169
18170               buf = write_insn (buf, insn);
18171             }
18172         }
18173
18174       fragp->fr_fix += fragp->fr_var;
18175       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18176       return;
18177     }
18178
18179   /* Relax microMIPS branches.  */
18180   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18181     {
18182       char *buf = fragp->fr_literal + fragp->fr_fix;
18183       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18184       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18185       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18186       bfd_boolean short_ds;
18187       unsigned long insn;
18188       expressionS exp;
18189       fixS *fixp;
18190
18191       exp.X_op = O_symbol;
18192       exp.X_add_symbol = fragp->fr_symbol;
18193       exp.X_add_number = fragp->fr_offset;
18194
18195       fragp->fr_fix += fragp->fr_var;
18196
18197       /* Handle 16-bit branches that fit or are forced to fit.  */
18198       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18199         {
18200           /* We generate a fixup instead of applying it right now,
18201              because if there is linker relaxation, we're going to
18202              need the relocations.  */
18203           if (type == 'D')
18204             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18205                                 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18206           else if (type == 'E')
18207             fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
18208                                 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18209           else
18210             abort ();
18211
18212           fixp->fx_file = fragp->fr_file;
18213           fixp->fx_line = fragp->fr_line;
18214
18215           /* These relocations can have an addend that won't fit in
18216              2 octets.  */
18217           fixp->fx_no_overflow = 1;
18218
18219           return;
18220         }
18221
18222       /* Handle 32-bit branches that fit or are forced to fit.  */
18223       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18224           || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18225         {
18226           /* We generate a fixup instead of applying it right now,
18227              because if there is linker relaxation, we're going to
18228              need the relocations.  */
18229           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18230                               BFD_RELOC_MICROMIPS_16_PCREL_S1);
18231           fixp->fx_file = fragp->fr_file;
18232           fixp->fx_line = fragp->fr_line;
18233
18234           if (type == 0)
18235             return;
18236         }
18237
18238       /* Relax 16-bit branches to 32-bit branches.  */
18239       if (type != 0)
18240         {
18241           insn = read_compressed_insn (buf, 2);
18242
18243           if ((insn & 0xfc00) == 0xcc00)                /* b16  */
18244             insn = 0x94000000;                          /* beq  */
18245           else if ((insn & 0xdc00) == 0x8c00)           /* beqz16/bnez16  */
18246             {
18247               unsigned long regno;
18248
18249               regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18250               regno = micromips_to_32_reg_d_map [regno];
18251               insn = ((insn & 0x2000) << 16) | 0x94000000;      /* beq/bne  */
18252               insn |= regno << MICROMIPSOP_SH_RS;
18253             }
18254           else
18255             abort ();
18256
18257           /* Nothing else to do, just write it out.  */
18258           if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18259               || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18260             {
18261               buf = write_compressed_insn (buf, insn, 4);
18262               gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18263               return;
18264             }
18265         }
18266       else
18267         insn = read_compressed_insn (buf, 4);
18268
18269       /* Relax 32-bit branches to a sequence of instructions.  */
18270       as_warn_where (fragp->fr_file, fragp->fr_line,
18271                      _("Relaxed out-of-range branch into a jump"));
18272
18273       /* Set the short-delay-slot bit.  */
18274       short_ds = al && (insn & 0x02000000) != 0;
18275
18276       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18277         {
18278           symbolS *l;
18279
18280           /* Reverse the branch.  */
18281           if ((insn & 0xfc000000) == 0x94000000                 /* beq  */
18282               || (insn & 0xfc000000) == 0xb4000000)             /* bne  */
18283             insn ^= 0x20000000;
18284           else if ((insn & 0xffe00000) == 0x40000000            /* bltz  */
18285                    || (insn & 0xffe00000) == 0x40400000         /* bgez  */
18286                    || (insn & 0xffe00000) == 0x40800000         /* blez  */
18287                    || (insn & 0xffe00000) == 0x40c00000         /* bgtz  */
18288                    || (insn & 0xffe00000) == 0x40a00000         /* bnezc  */
18289                    || (insn & 0xffe00000) == 0x40e00000         /* beqzc  */
18290                    || (insn & 0xffe00000) == 0x40200000         /* bltzal  */
18291                    || (insn & 0xffe00000) == 0x40600000         /* bgezal  */
18292                    || (insn & 0xffe00000) == 0x42200000         /* bltzals  */
18293                    || (insn & 0xffe00000) == 0x42600000)        /* bgezals  */
18294             insn ^= 0x00400000;
18295           else if ((insn & 0xffe30000) == 0x43800000            /* bc1f  */
18296                    || (insn & 0xffe30000) == 0x43a00000         /* bc1t  */
18297                    || (insn & 0xffe30000) == 0x42800000         /* bc2f  */
18298                    || (insn & 0xffe30000) == 0x42a00000)        /* bc2t  */
18299             insn ^= 0x00200000;
18300           else
18301             abort ();
18302
18303           if (al)
18304             {
18305               /* Clear the and-link and short-delay-slot bits.  */
18306               gas_assert ((insn & 0xfda00000) == 0x40200000);
18307
18308               /* bltzal  0x40200000     bgezal  0x40600000  */
18309               /* bltzals 0x42200000     bgezals 0x42600000  */
18310               insn &= ~0x02200000;
18311             }
18312
18313           /* Make a label at the end for use with the branch.  */
18314           l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18315           micromips_label_inc ();
18316 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18317           if (IS_ELF)
18318             S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18319 #endif
18320
18321           /* Refer to it.  */
18322           fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18323                           BFD_RELOC_MICROMIPS_16_PCREL_S1);
18324           fixp->fx_file = fragp->fr_file;
18325           fixp->fx_line = fragp->fr_line;
18326
18327           /* Branch over the jump.  */
18328           buf = write_compressed_insn (buf, insn, 4);
18329           if (!compact)
18330             /* nop */
18331             buf = write_compressed_insn (buf, 0x0c00, 2);
18332         }
18333
18334       if (mips_pic == NO_PIC)
18335         {
18336           unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
18337
18338           /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18339           insn = al ? jal : 0xd4000000;
18340
18341           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18342                               BFD_RELOC_MICROMIPS_JMP);
18343           fixp->fx_file = fragp->fr_file;
18344           fixp->fx_line = fragp->fr_line;
18345
18346           buf = write_compressed_insn (buf, insn, 4);
18347           if (compact)
18348             /* nop */
18349             buf = write_compressed_insn (buf, 0x0c00, 2);
18350         }
18351       else
18352         {
18353           unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18354           unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;      /* jalr/s  */
18355           unsigned long jr = compact ? 0x45a0 : 0x4580;         /* jr/c  */
18356
18357           /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18358           insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18359           insn |= at << MICROMIPSOP_SH_RT;
18360
18361           if (exp.X_add_number)
18362             {
18363               exp.X_add_symbol = make_expr_symbol (&exp);
18364               exp.X_add_number = 0;
18365             }
18366
18367           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18368                               BFD_RELOC_MICROMIPS_GOT16);
18369           fixp->fx_file = fragp->fr_file;
18370           fixp->fx_line = fragp->fr_line;
18371
18372           buf = write_compressed_insn (buf, insn, 4);
18373
18374           /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18375           insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18376           insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18377
18378           fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18379                               BFD_RELOC_MICROMIPS_LO16);
18380           fixp->fx_file = fragp->fr_file;
18381           fixp->fx_line = fragp->fr_line;
18382
18383           buf = write_compressed_insn (buf, insn, 4);
18384
18385           /* jr/jrc/jalr/jalrs $at  */
18386           insn = al ? jalr : jr;
18387           insn |= at << MICROMIPSOP_SH_MJ;
18388
18389           buf = write_compressed_insn (buf, insn, 2);
18390         }
18391
18392       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18393       return;
18394     }
18395
18396   if (RELAX_MIPS16_P (fragp->fr_subtype))
18397     {
18398       int type;
18399       const struct mips16_immed_operand *op;
18400       offsetT val;
18401       char *buf;
18402       unsigned int user_length, length;
18403       unsigned long insn;
18404       bfd_boolean ext;
18405
18406       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18407       op = mips16_immed_operands;
18408       while (op->type != type)
18409         ++op;
18410
18411       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18412       val = resolve_symbol_value (fragp->fr_symbol);
18413       if (op->pcrel)
18414         {
18415           addressT addr;
18416
18417           addr = fragp->fr_address + fragp->fr_fix;
18418
18419           /* The rules for the base address of a PC relative reloc are
18420              complicated; see mips16_extended_frag.  */
18421           if (type == 'p' || type == 'q')
18422             {
18423               addr += 2;
18424               if (ext)
18425                 addr += 2;
18426               /* Ignore the low bit in the target, since it will be
18427                  set for a text label.  */
18428               if ((val & 1) != 0)
18429                 --val;
18430             }
18431           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18432             addr -= 4;
18433           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18434             addr -= 2;
18435
18436           addr &= ~ (addressT) ((1 << op->shift) - 1);
18437           val -= addr;
18438
18439           /* Make sure the section winds up with the alignment we have
18440              assumed.  */
18441           if (op->shift > 0)
18442             record_alignment (asec, op->shift);
18443         }
18444
18445       if (ext
18446           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18447               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18448         as_warn_where (fragp->fr_file, fragp->fr_line,
18449                        _("extended instruction in delay slot"));
18450
18451       buf = fragp->fr_literal + fragp->fr_fix;
18452
18453       insn = read_compressed_insn (buf, 2);
18454       if (ext)
18455         insn |= MIPS16_EXTEND;
18456
18457       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18458         user_length = 4;
18459       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18460         user_length = 2;
18461       else
18462         user_length = 0;
18463
18464       mips16_immed (fragp->fr_file, fragp->fr_line, type,
18465                     BFD_RELOC_UNUSED, val, user_length, &insn);
18466
18467       length = (ext ? 4 : 2);
18468       gas_assert (mips16_opcode_length (insn) == length);
18469       write_compressed_insn (buf, insn, length);
18470       fragp->fr_fix += length;
18471     }
18472   else
18473     {
18474       relax_substateT subtype = fragp->fr_subtype;
18475       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18476       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18477       int first, second;
18478       fixS *fixp;
18479
18480       first = RELAX_FIRST (subtype);
18481       second = RELAX_SECOND (subtype);
18482       fixp = (fixS *) fragp->fr_opcode;
18483
18484       /* If the delay slot chosen does not match the size of the instruction,
18485          then emit a warning.  */
18486       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18487            || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18488         {
18489           relax_substateT s;
18490           const char *msg;
18491
18492           s = subtype & (RELAX_DELAY_SLOT_16BIT
18493                          | RELAX_DELAY_SLOT_SIZE_FIRST
18494                          | RELAX_DELAY_SLOT_SIZE_SECOND);
18495           msg = macro_warning (s);
18496           if (msg != NULL)
18497             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18498           subtype &= ~s;
18499         }
18500
18501       /* Possibly emit a warning if we've chosen the longer option.  */
18502       if (use_second == second_longer)
18503         {
18504           relax_substateT s;
18505           const char *msg;
18506
18507           s = (subtype
18508                & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18509           msg = macro_warning (s);
18510           if (msg != NULL)
18511             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18512           subtype &= ~s;
18513         }
18514
18515       /* Go through all the fixups for the first sequence.  Disable them
18516          (by marking them as done) if we're going to use the second
18517          sequence instead.  */
18518       while (fixp
18519              && fixp->fx_frag == fragp
18520              && fixp->fx_where < fragp->fr_fix - second)
18521         {
18522           if (subtype & RELAX_USE_SECOND)
18523             fixp->fx_done = 1;
18524           fixp = fixp->fx_next;
18525         }
18526
18527       /* Go through the fixups for the second sequence.  Disable them if
18528          we're going to use the first sequence, otherwise adjust their
18529          addresses to account for the relaxation.  */
18530       while (fixp && fixp->fx_frag == fragp)
18531         {
18532           if (subtype & RELAX_USE_SECOND)
18533             fixp->fx_where -= first;
18534           else
18535             fixp->fx_done = 1;
18536           fixp = fixp->fx_next;
18537         }
18538
18539       /* Now modify the frag contents.  */
18540       if (subtype & RELAX_USE_SECOND)
18541         {
18542           char *start;
18543
18544           start = fragp->fr_literal + fragp->fr_fix - first - second;
18545           memmove (start, start + first, second);
18546           fragp->fr_fix -= first;
18547         }
18548       else
18549         fragp->fr_fix -= second;
18550     }
18551 }
18552
18553 #ifdef OBJ_ELF
18554
18555 /* This function is called after the relocs have been generated.
18556    We've been storing mips16 text labels as odd.  Here we convert them
18557    back to even for the convenience of the debugger.  */
18558
18559 void
18560 mips_frob_file_after_relocs (void)
18561 {
18562   asymbol **syms;
18563   unsigned int count, i;
18564
18565   if (!IS_ELF)
18566     return;
18567
18568   syms = bfd_get_outsymbols (stdoutput);
18569   count = bfd_get_symcount (stdoutput);
18570   for (i = 0; i < count; i++, syms++)
18571     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18572         && ((*syms)->value & 1) != 0)
18573       {
18574         (*syms)->value &= ~1;
18575         /* If the symbol has an odd size, it was probably computed
18576            incorrectly, so adjust that as well.  */
18577         if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18578           ++elf_symbol (*syms)->internal_elf_sym.st_size;
18579       }
18580 }
18581
18582 #endif
18583
18584 /* This function is called whenever a label is defined, including fake
18585    labels instantiated off the dot special symbol.  It is used when
18586    handling branch delays; if a branch has a label, we assume we cannot
18587    move it.  This also bumps the value of the symbol by 1 in compressed
18588    code.  */
18589
18590 static void
18591 mips_record_label (symbolS *sym)
18592 {
18593   segment_info_type *si = seg_info (now_seg);
18594   struct insn_label_list *l;
18595
18596   if (free_insn_labels == NULL)
18597     l = (struct insn_label_list *) xmalloc (sizeof *l);
18598   else
18599     {
18600       l = free_insn_labels;
18601       free_insn_labels = l->next;
18602     }
18603
18604   l->label = sym;
18605   l->next = si->label_list;
18606   si->label_list = l;
18607 }
18608
18609 /* This function is called as tc_frob_label() whenever a label is defined
18610    and adds a DWARF-2 record we only want for true labels.  */
18611
18612 void
18613 mips_define_label (symbolS *sym)
18614 {
18615   mips_record_label (sym);
18616 #ifdef OBJ_ELF
18617   dwarf2_emit_label (sym);
18618 #endif
18619 }
18620
18621 /* This function is called by tc_new_dot_label whenever a new dot symbol
18622    is defined.  */
18623
18624 void
18625 mips_add_dot_label (symbolS *sym)
18626 {
18627   mips_record_label (sym);
18628   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18629     mips_compressed_mark_label (sym);
18630 }
18631 \f
18632 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18633
18634 /* Some special processing for a MIPS ELF file.  */
18635
18636 void
18637 mips_elf_final_processing (void)
18638 {
18639   /* Write out the register information.  */
18640   if (mips_abi != N64_ABI)
18641     {
18642       Elf32_RegInfo s;
18643
18644       s.ri_gprmask = mips_gprmask;
18645       s.ri_cprmask[0] = mips_cprmask[0];
18646       s.ri_cprmask[1] = mips_cprmask[1];
18647       s.ri_cprmask[2] = mips_cprmask[2];
18648       s.ri_cprmask[3] = mips_cprmask[3];
18649       /* The gp_value field is set by the MIPS ELF backend.  */
18650
18651       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18652                                        ((Elf32_External_RegInfo *)
18653                                         mips_regmask_frag));
18654     }
18655   else
18656     {
18657       Elf64_Internal_RegInfo s;
18658
18659       s.ri_gprmask = mips_gprmask;
18660       s.ri_pad = 0;
18661       s.ri_cprmask[0] = mips_cprmask[0];
18662       s.ri_cprmask[1] = mips_cprmask[1];
18663       s.ri_cprmask[2] = mips_cprmask[2];
18664       s.ri_cprmask[3] = mips_cprmask[3];
18665       /* The gp_value field is set by the MIPS ELF backend.  */
18666
18667       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18668                                        ((Elf64_External_RegInfo *)
18669                                         mips_regmask_frag));
18670     }
18671
18672   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
18673      sort of BFD interface for this.  */
18674   if (mips_any_noreorder)
18675     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18676   if (mips_pic != NO_PIC)
18677     {
18678       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18679       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18680     }
18681   if (mips_abicalls)
18682     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18683
18684   /* Set MIPS ELF flags for ASEs.  */
18685   /* We may need to define a new flag for DSP ASE, and set this flag when
18686      file_ase_dsp is true.  */
18687   /* Same for DSP R2.  */
18688   /* We may need to define a new flag for MT ASE, and set this flag when
18689      file_ase_mt is true.  */
18690   if (file_ase_mips16)
18691     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18692   if (file_ase_micromips)
18693     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18694 #if 0 /* XXX FIXME */
18695   if (file_ase_mips3d)
18696     elf_elfheader (stdoutput)->e_flags |= ???;
18697 #endif
18698   if (file_ase_mdmx)
18699     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18700
18701   /* Set the MIPS ELF ABI flags.  */
18702   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18703     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18704   else if (mips_abi == O64_ABI)
18705     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18706   else if (mips_abi == EABI_ABI)
18707     {
18708       if (!file_mips_gp32)
18709         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18710       else
18711         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18712     }
18713   else if (mips_abi == N32_ABI)
18714     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18715
18716   /* Nothing to do for N64_ABI.  */
18717
18718   if (mips_32bitmode)
18719     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18720
18721 #if 0 /* XXX FIXME */
18722   /* 32 bit code with 64 bit FP registers.  */
18723   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18724     elf_elfheader (stdoutput)->e_flags |= ???;
18725 #endif
18726 }
18727
18728 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18729 \f
18730 typedef struct proc {
18731   symbolS *func_sym;
18732   symbolS *func_end_sym;
18733   unsigned long reg_mask;
18734   unsigned long reg_offset;
18735   unsigned long fpreg_mask;
18736   unsigned long fpreg_offset;
18737   unsigned long frame_offset;
18738   unsigned long frame_reg;
18739   unsigned long pc_reg;
18740 } procS;
18741
18742 static procS cur_proc;
18743 static procS *cur_proc_ptr;
18744 static int numprocs;
18745
18746 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
18747    as "2", and a normal nop as "0".  */
18748
18749 #define NOP_OPCODE_MIPS         0
18750 #define NOP_OPCODE_MIPS16       1
18751 #define NOP_OPCODE_MICROMIPS    2
18752
18753 char
18754 mips_nop_opcode (void)
18755 {
18756   if (seg_info (now_seg)->tc_segment_info_data.micromips)
18757     return NOP_OPCODE_MICROMIPS;
18758   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18759     return NOP_OPCODE_MIPS16;
18760   else
18761     return NOP_OPCODE_MIPS;
18762 }
18763
18764 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
18765    32-bit microMIPS NOPs here (if applicable).  */
18766
18767 void
18768 mips_handle_align (fragS *fragp)
18769 {
18770   char nop_opcode;
18771   char *p;
18772   int bytes, size, excess;
18773   valueT opcode;
18774
18775   if (fragp->fr_type != rs_align_code)
18776     return;
18777
18778   p = fragp->fr_literal + fragp->fr_fix;
18779   nop_opcode = *p;
18780   switch (nop_opcode)
18781     {
18782     case NOP_OPCODE_MICROMIPS:
18783       opcode = micromips_nop32_insn.insn_opcode;
18784       size = 4;
18785       break;
18786     case NOP_OPCODE_MIPS16:
18787       opcode = mips16_nop_insn.insn_opcode;
18788       size = 2;
18789       break;
18790     case NOP_OPCODE_MIPS:
18791     default:
18792       opcode = nop_insn.insn_opcode;
18793       size = 4;
18794       break;
18795     }
18796
18797   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18798   excess = bytes % size;
18799
18800   /* Handle the leading part if we're not inserting a whole number of
18801      instructions, and make it the end of the fixed part of the frag.
18802      Try to fit in a short microMIPS NOP if applicable and possible,
18803      and use zeroes otherwise.  */
18804   gas_assert (excess < 4);
18805   fragp->fr_fix += excess;
18806   switch (excess)
18807     {
18808     case 3:
18809       *p++ = '\0';
18810       /* Fall through.  */
18811     case 2:
18812       if (nop_opcode == NOP_OPCODE_MICROMIPS)
18813         {
18814           p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
18815           break;
18816         }
18817       *p++ = '\0';
18818       /* Fall through.  */
18819     case 1:
18820       *p++ = '\0';
18821       /* Fall through.  */
18822     case 0:
18823       break;
18824     }
18825
18826   md_number_to_chars (p, opcode, size);
18827   fragp->fr_var = size;
18828 }
18829
18830 static void
18831 md_obj_begin (void)
18832 {
18833 }
18834
18835 static void
18836 md_obj_end (void)
18837 {
18838   /* Check for premature end, nesting errors, etc.  */
18839   if (cur_proc_ptr)
18840     as_warn (_("missing .end at end of assembly"));
18841 }
18842
18843 static long
18844 get_number (void)
18845 {
18846   int negative = 0;
18847   long val = 0;
18848
18849   if (*input_line_pointer == '-')
18850     {
18851       ++input_line_pointer;
18852       negative = 1;
18853     }
18854   if (!ISDIGIT (*input_line_pointer))
18855     as_bad (_("expected simple number"));
18856   if (input_line_pointer[0] == '0')
18857     {
18858       if (input_line_pointer[1] == 'x')
18859         {
18860           input_line_pointer += 2;
18861           while (ISXDIGIT (*input_line_pointer))
18862             {
18863               val <<= 4;
18864               val |= hex_value (*input_line_pointer++);
18865             }
18866           return negative ? -val : val;
18867         }
18868       else
18869         {
18870           ++input_line_pointer;
18871           while (ISDIGIT (*input_line_pointer))
18872             {
18873               val <<= 3;
18874               val |= *input_line_pointer++ - '0';
18875             }
18876           return negative ? -val : val;
18877         }
18878     }
18879   if (!ISDIGIT (*input_line_pointer))
18880     {
18881       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18882               *input_line_pointer, *input_line_pointer);
18883       as_warn (_("invalid number"));
18884       return -1;
18885     }
18886   while (ISDIGIT (*input_line_pointer))
18887     {
18888       val *= 10;
18889       val += *input_line_pointer++ - '0';
18890     }
18891   return negative ? -val : val;
18892 }
18893
18894 /* The .file directive; just like the usual .file directive, but there
18895    is an initial number which is the ECOFF file index.  In the non-ECOFF
18896    case .file implies DWARF-2.  */
18897
18898 static void
18899 s_mips_file (int x ATTRIBUTE_UNUSED)
18900 {
18901   static int first_file_directive = 0;
18902
18903   if (ECOFF_DEBUGGING)
18904     {
18905       get_number ();
18906       s_app_file (0);
18907     }
18908   else
18909     {
18910       char *filename;
18911
18912       filename = dwarf2_directive_file (0);
18913
18914       /* Versions of GCC up to 3.1 start files with a ".file"
18915          directive even for stabs output.  Make sure that this
18916          ".file" is handled.  Note that you need a version of GCC
18917          after 3.1 in order to support DWARF-2 on MIPS.  */
18918       if (filename != NULL && ! first_file_directive)
18919         {
18920           (void) new_logical_line (filename, -1);
18921           s_app_file_string (filename, 0);
18922         }
18923       first_file_directive = 1;
18924     }
18925 }
18926
18927 /* The .loc directive, implying DWARF-2.  */
18928
18929 static void
18930 s_mips_loc (int x ATTRIBUTE_UNUSED)
18931 {
18932   if (!ECOFF_DEBUGGING)
18933     dwarf2_directive_loc (0);
18934 }
18935
18936 /* The .end directive.  */
18937
18938 static void
18939 s_mips_end (int x ATTRIBUTE_UNUSED)
18940 {
18941   symbolS *p;
18942
18943   /* Following functions need their own .frame and .cprestore directives.  */
18944   mips_frame_reg_valid = 0;
18945   mips_cprestore_valid = 0;
18946
18947   if (!is_end_of_line[(unsigned char) *input_line_pointer])
18948     {
18949       p = get_symbol ();
18950       demand_empty_rest_of_line ();
18951     }
18952   else
18953     p = NULL;
18954
18955   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18956     as_warn (_(".end not in text section"));
18957
18958   if (!cur_proc_ptr)
18959     {
18960       as_warn (_(".end directive without a preceding .ent directive."));
18961       demand_empty_rest_of_line ();
18962       return;
18963     }
18964
18965   if (p != NULL)
18966     {
18967       gas_assert (S_GET_NAME (p));
18968       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18969         as_warn (_(".end symbol does not match .ent symbol."));
18970
18971       if (debug_type == DEBUG_STABS)
18972         stabs_generate_asm_endfunc (S_GET_NAME (p),
18973                                     S_GET_NAME (p));
18974     }
18975   else
18976     as_warn (_(".end directive missing or unknown symbol"));
18977
18978 #ifdef OBJ_ELF
18979   /* Create an expression to calculate the size of the function.  */
18980   if (p && cur_proc_ptr)
18981     {
18982       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18983       expressionS *exp = xmalloc (sizeof (expressionS));
18984
18985       obj->size = exp;
18986       exp->X_op = O_subtract;
18987       exp->X_add_symbol = symbol_temp_new_now ();
18988       exp->X_op_symbol = p;
18989       exp->X_add_number = 0;
18990
18991       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18992     }
18993
18994   /* Generate a .pdr section.  */
18995   if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
18996     {
18997       segT saved_seg = now_seg;
18998       subsegT saved_subseg = now_subseg;
18999       expressionS exp;
19000       char *fragp;
19001
19002 #ifdef md_flush_pending_output
19003       md_flush_pending_output ();
19004 #endif
19005
19006       gas_assert (pdr_seg);
19007       subseg_set (pdr_seg, 0);
19008
19009       /* Write the symbol.  */
19010       exp.X_op = O_symbol;
19011       exp.X_add_symbol = p;
19012       exp.X_add_number = 0;
19013       emit_expr (&exp, 4);
19014
19015       fragp = frag_more (7 * 4);
19016
19017       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19018       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19019       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19020       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19021       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19022       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19023       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19024
19025       subseg_set (saved_seg, saved_subseg);
19026     }
19027 #endif /* OBJ_ELF */
19028
19029   cur_proc_ptr = NULL;
19030 }
19031
19032 /* The .aent and .ent directives.  */
19033
19034 static void
19035 s_mips_ent (int aent)
19036 {
19037   symbolS *symbolP;
19038
19039   symbolP = get_symbol ();
19040   if (*input_line_pointer == ',')
19041     ++input_line_pointer;
19042   SKIP_WHITESPACE ();
19043   if (ISDIGIT (*input_line_pointer)
19044       || *input_line_pointer == '-')
19045     get_number ();
19046
19047   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19048     as_warn (_(".ent or .aent not in text section."));
19049
19050   if (!aent && cur_proc_ptr)
19051     as_warn (_("missing .end"));
19052
19053   if (!aent)
19054     {
19055       /* This function needs its own .frame and .cprestore directives.  */
19056       mips_frame_reg_valid = 0;
19057       mips_cprestore_valid = 0;
19058
19059       cur_proc_ptr = &cur_proc;
19060       memset (cur_proc_ptr, '\0', sizeof (procS));
19061
19062       cur_proc_ptr->func_sym = symbolP;
19063
19064       ++numprocs;
19065
19066       if (debug_type == DEBUG_STABS)
19067         stabs_generate_asm_func (S_GET_NAME (symbolP),
19068                                  S_GET_NAME (symbolP));
19069     }
19070
19071   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19072
19073   demand_empty_rest_of_line ();
19074 }
19075
19076 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19077    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19078    s_mips_frame is used so that we can set the PDR information correctly.
19079    We can't use the ecoff routines because they make reference to the ecoff
19080    symbol table (in the mdebug section).  */
19081
19082 static void
19083 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19084 {
19085 #ifdef OBJ_ELF
19086   if (IS_ELF && !ECOFF_DEBUGGING)
19087     {
19088       long val;
19089
19090       if (cur_proc_ptr == (procS *) NULL)
19091         {
19092           as_warn (_(".frame outside of .ent"));
19093           demand_empty_rest_of_line ();
19094           return;
19095         }
19096
19097       cur_proc_ptr->frame_reg = tc_get_register (1);
19098
19099       SKIP_WHITESPACE ();
19100       if (*input_line_pointer++ != ','
19101           || get_absolute_expression_and_terminator (&val) != ',')
19102         {
19103           as_warn (_("Bad .frame directive"));
19104           --input_line_pointer;
19105           demand_empty_rest_of_line ();
19106           return;
19107         }
19108
19109       cur_proc_ptr->frame_offset = val;
19110       cur_proc_ptr->pc_reg = tc_get_register (0);
19111
19112       demand_empty_rest_of_line ();
19113     }
19114   else
19115 #endif /* OBJ_ELF */
19116     s_ignore (ignore);
19117 }
19118
19119 /* The .fmask and .mask directives. If the mdebug section is present
19120    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19121    embedded targets, s_mips_mask is used so that we can set the PDR
19122    information correctly. We can't use the ecoff routines because they
19123    make reference to the ecoff symbol table (in the mdebug section).  */
19124
19125 static void
19126 s_mips_mask (int reg_type)
19127 {
19128 #ifdef OBJ_ELF
19129   if (IS_ELF && !ECOFF_DEBUGGING)
19130     {
19131       long mask, off;
19132
19133       if (cur_proc_ptr == (procS *) NULL)
19134         {
19135           as_warn (_(".mask/.fmask outside of .ent"));
19136           demand_empty_rest_of_line ();
19137           return;
19138         }
19139
19140       if (get_absolute_expression_and_terminator (&mask) != ',')
19141         {
19142           as_warn (_("Bad .mask/.fmask directive"));
19143           --input_line_pointer;
19144           demand_empty_rest_of_line ();
19145           return;
19146         }
19147
19148       off = get_absolute_expression ();
19149
19150       if (reg_type == 'F')
19151         {
19152           cur_proc_ptr->fpreg_mask = mask;
19153           cur_proc_ptr->fpreg_offset = off;
19154         }
19155       else
19156         {
19157           cur_proc_ptr->reg_mask = mask;
19158           cur_proc_ptr->reg_offset = off;
19159         }
19160
19161       demand_empty_rest_of_line ();
19162     }
19163   else
19164 #endif /* OBJ_ELF */
19165     s_ignore (reg_type);
19166 }
19167
19168 /* A table describing all the processors gas knows about.  Names are
19169    matched in the order listed.
19170
19171    To ease comparison, please keep this table in the same order as
19172    gcc's mips_cpu_info_table[].  */
19173 static const struct mips_cpu_info mips_cpu_info_table[] =
19174 {
19175   /* Entries for generic ISAs */
19176   { "mips1",          MIPS_CPU_IS_ISA,          ISA_MIPS1,      CPU_R3000 },
19177   { "mips2",          MIPS_CPU_IS_ISA,          ISA_MIPS2,      CPU_R6000 },
19178   { "mips3",          MIPS_CPU_IS_ISA,          ISA_MIPS3,      CPU_R4000 },
19179   { "mips4",          MIPS_CPU_IS_ISA,          ISA_MIPS4,      CPU_R8000 },
19180   { "mips5",          MIPS_CPU_IS_ISA,          ISA_MIPS5,      CPU_MIPS5 },
19181   { "mips32",         MIPS_CPU_IS_ISA,          ISA_MIPS32,     CPU_MIPS32 },
19182   { "mips32r2",       MIPS_CPU_IS_ISA,          ISA_MIPS32R2,   CPU_MIPS32R2 },
19183   { "mips64",         MIPS_CPU_IS_ISA,          ISA_MIPS64,     CPU_MIPS64 },
19184   { "mips64r2",       MIPS_CPU_IS_ISA,          ISA_MIPS64R2,   CPU_MIPS64R2 },
19185
19186   /* MIPS I */
19187   { "r3000",          0,                        ISA_MIPS1,      CPU_R3000 },
19188   { "r2000",          0,                        ISA_MIPS1,      CPU_R3000 },
19189   { "r3900",          0,                        ISA_MIPS1,      CPU_R3900 },
19190
19191   /* MIPS II */
19192   { "r6000",          0,                        ISA_MIPS2,      CPU_R6000 },
19193
19194   /* MIPS III */
19195   { "r4000",          0,                        ISA_MIPS3,      CPU_R4000 },
19196   { "r4010",          0,                        ISA_MIPS2,      CPU_R4010 },
19197   { "vr4100",         0,                        ISA_MIPS3,      CPU_VR4100 },
19198   { "vr4111",         0,                        ISA_MIPS3,      CPU_R4111 },
19199   { "vr4120",         0,                        ISA_MIPS3,      CPU_VR4120 },
19200   { "vr4130",         0,                        ISA_MIPS3,      CPU_VR4120 },
19201   { "vr4181",         0,                        ISA_MIPS3,      CPU_R4111 },
19202   { "vr4300",         0,                        ISA_MIPS3,      CPU_R4300 },
19203   { "r4400",          0,                        ISA_MIPS3,      CPU_R4400 },
19204   { "r4600",          0,                        ISA_MIPS3,      CPU_R4600 },
19205   { "orion",          0,                        ISA_MIPS3,      CPU_R4600 },
19206   { "r4650",          0,                        ISA_MIPS3,      CPU_R4650 },
19207   { "r5900",          0,                        ISA_MIPS3,      CPU_R5900 },
19208   /* ST Microelectronics Loongson 2E and 2F cores */
19209   { "loongson2e",     0,                        ISA_MIPS3,   CPU_LOONGSON_2E },
19210   { "loongson2f",     0,                        ISA_MIPS3,   CPU_LOONGSON_2F },
19211
19212   /* MIPS IV */
19213   { "r8000",          0,                        ISA_MIPS4,      CPU_R8000 },
19214   { "r10000",         0,                        ISA_MIPS4,      CPU_R10000 },
19215   { "r12000",         0,                        ISA_MIPS4,      CPU_R12000 },
19216   { "r14000",         0,                        ISA_MIPS4,      CPU_R14000 },
19217   { "r16000",         0,                        ISA_MIPS4,      CPU_R16000 },
19218   { "vr5000",         0,                        ISA_MIPS4,      CPU_R5000 },
19219   { "vr5400",         0,                        ISA_MIPS4,      CPU_VR5400 },
19220   { "vr5500",         0,                        ISA_MIPS4,      CPU_VR5500 },
19221   { "rm5200",         0,                        ISA_MIPS4,      CPU_R5000 },
19222   { "rm5230",         0,                        ISA_MIPS4,      CPU_R5000 },
19223   { "rm5231",         0,                        ISA_MIPS4,      CPU_R5000 },
19224   { "rm5261",         0,                        ISA_MIPS4,      CPU_R5000 },
19225   { "rm5721",         0,                        ISA_MIPS4,      CPU_R5000 },
19226   { "rm7000",         0,                        ISA_MIPS4,      CPU_RM7000 },
19227   { "rm9000",         0,                        ISA_MIPS4,      CPU_RM9000 },
19228
19229   /* MIPS 32 */
19230   { "4kc",            0,                        ISA_MIPS32,     CPU_MIPS32 },
19231   { "4km",            0,                        ISA_MIPS32,     CPU_MIPS32 },
19232   { "4kp",            0,                        ISA_MIPS32,     CPU_MIPS32 },
19233   { "4ksc",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32,     CPU_MIPS32 },
19234
19235   /* MIPS 32 Release 2 */
19236   { "4kec",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19237   { "4kem",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19238   { "4kep",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19239   { "4ksd",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32R2,   CPU_MIPS32R2 },
19240   { "m4k",            0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19241   { "m4kp",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19242   { "m14k",           MIPS_CPU_ASE_MCU,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19243   { "m14kc",          MIPS_CPU_ASE_MCU,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19244   { "m14ke",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19245                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19246   { "m14kec",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19247                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19248   { "24kc",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19249   { "24kf2_1",        0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19250   { "24kf",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19251   { "24kf1_1",        0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19252   /* Deprecated forms of the above.  */
19253   { "24kfx",          0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19254   { "24kx",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19255   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
19256   { "24kec",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19257   { "24kef2_1",       MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19258   { "24kef",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19259   { "24kef1_1",       MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19260   /* Deprecated forms of the above.  */
19261   { "24kefx",         MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19262   { "24kex",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19263   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
19264   { "34kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19265                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19266   { "34kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19267                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19268   { "34kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19269                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19270   { "34kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19271                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19272   /* Deprecated forms of the above.  */
19273   { "34kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19274                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19275   { "34kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19276                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19277   /* 34Kn is a 34kc without DSP.  */
19278   { "34kn",           MIPS_CPU_ASE_MT,          ISA_MIPS32R2,   CPU_MIPS32R2 },
19279   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
19280   { "74kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19281                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19282   { "74kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19283                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19284   { "74kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19285                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19286   { "74kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19287                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19288   { "74kf3_2",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19289                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19290   /* Deprecated forms of the above.  */
19291   { "74kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19292                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19293   { "74kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19294                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19295   /* 1004K cores are multiprocessor versions of the 34K.  */
19296   { "1004kc",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19297                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19298   { "1004kf2_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19299                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19300   { "1004kf",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19301                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19302   { "1004kf1_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19303                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19304
19305   /* MIPS 64 */
19306   { "5kc",            0,                        ISA_MIPS64,     CPU_MIPS64 },
19307   { "5kf",            0,                        ISA_MIPS64,     CPU_MIPS64 },
19308   { "20kc",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64,     CPU_MIPS64 },
19309   { "25kf",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64,     CPU_MIPS64 },
19310
19311   /* Broadcom SB-1 CPU core */
19312   { "sb1",            MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19313                                                 ISA_MIPS64,     CPU_SB1 },
19314   /* Broadcom SB-1A CPU core */
19315   { "sb1a",           MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19316                                                 ISA_MIPS64,     CPU_SB1 },
19317   
19318   { "loongson3a",     0,                        ISA_MIPS64,     CPU_LOONGSON_3A },
19319
19320   /* MIPS 64 Release 2 */
19321
19322   /* Cavium Networks Octeon CPU core */
19323   { "octeon",         0,      ISA_MIPS64R2,   CPU_OCTEON },
19324   { "octeon+",        0,      ISA_MIPS64R2,   CPU_OCTEONP },
19325   { "octeon2",        0,      ISA_MIPS64R2,   CPU_OCTEON2 },
19326
19327   /* RMI Xlr */
19328   { "xlr",            0,      ISA_MIPS64,     CPU_XLR },
19329
19330   /* Broadcom XLP.
19331      XLP is mostly like XLR, with the prominent exception that it is
19332      MIPS64R2 rather than MIPS64.  */
19333   { "xlp",            0,      ISA_MIPS64R2,     CPU_XLR },
19334
19335   /* End marker */
19336   { NULL, 0, 0, 0 }
19337 };
19338
19339
19340 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19341    with a final "000" replaced by "k".  Ignore case.
19342
19343    Note: this function is shared between GCC and GAS.  */
19344
19345 static bfd_boolean
19346 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19347 {
19348   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19349     given++, canonical++;
19350
19351   return ((*given == 0 && *canonical == 0)
19352           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19353 }
19354
19355
19356 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19357    CPU name.  We've traditionally allowed a lot of variation here.
19358
19359    Note: this function is shared between GCC and GAS.  */
19360
19361 static bfd_boolean
19362 mips_matching_cpu_name_p (const char *canonical, const char *given)
19363 {
19364   /* First see if the name matches exactly, or with a final "000"
19365      turned into "k".  */
19366   if (mips_strict_matching_cpu_name_p (canonical, given))
19367     return TRUE;
19368
19369   /* If not, try comparing based on numerical designation alone.
19370      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
19371   if (TOLOWER (*given) == 'r')
19372     given++;
19373   if (!ISDIGIT (*given))
19374     return FALSE;
19375
19376   /* Skip over some well-known prefixes in the canonical name,
19377      hoping to find a number there too.  */
19378   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19379     canonical += 2;
19380   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19381     canonical += 2;
19382   else if (TOLOWER (canonical[0]) == 'r')
19383     canonical += 1;
19384
19385   return mips_strict_matching_cpu_name_p (canonical, given);
19386 }
19387
19388
19389 /* Parse an option that takes the name of a processor as its argument.
19390    OPTION is the name of the option and CPU_STRING is the argument.
19391    Return the corresponding processor enumeration if the CPU_STRING is
19392    recognized, otherwise report an error and return null.
19393
19394    A similar function exists in GCC.  */
19395
19396 static const struct mips_cpu_info *
19397 mips_parse_cpu (const char *option, const char *cpu_string)
19398 {
19399   const struct mips_cpu_info *p;
19400
19401   /* 'from-abi' selects the most compatible architecture for the given
19402      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
19403      EABIs, we have to decide whether we're using the 32-bit or 64-bit
19404      version.  Look first at the -mgp options, if given, otherwise base
19405      the choice on MIPS_DEFAULT_64BIT.
19406
19407      Treat NO_ABI like the EABIs.  One reason to do this is that the
19408      plain 'mips' and 'mips64' configs have 'from-abi' as their default
19409      architecture.  This code picks MIPS I for 'mips' and MIPS III for
19410      'mips64', just as we did in the days before 'from-abi'.  */
19411   if (strcasecmp (cpu_string, "from-abi") == 0)
19412     {
19413       if (ABI_NEEDS_32BIT_REGS (mips_abi))
19414         return mips_cpu_info_from_isa (ISA_MIPS1);
19415
19416       if (ABI_NEEDS_64BIT_REGS (mips_abi))
19417         return mips_cpu_info_from_isa (ISA_MIPS3);
19418
19419       if (file_mips_gp32 >= 0)
19420         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19421
19422       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19423                                      ? ISA_MIPS3
19424                                      : ISA_MIPS1);
19425     }
19426
19427   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
19428   if (strcasecmp (cpu_string, "default") == 0)
19429     return 0;
19430
19431   for (p = mips_cpu_info_table; p->name != 0; p++)
19432     if (mips_matching_cpu_name_p (p->name, cpu_string))
19433       return p;
19434
19435   as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19436   return 0;
19437 }
19438
19439 /* Return the canonical processor information for ISA (a member of the
19440    ISA_MIPS* enumeration).  */
19441
19442 static const struct mips_cpu_info *
19443 mips_cpu_info_from_isa (int isa)
19444 {
19445   int i;
19446
19447   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19448     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19449         && isa == mips_cpu_info_table[i].isa)
19450       return (&mips_cpu_info_table[i]);
19451
19452   return NULL;
19453 }
19454
19455 static const struct mips_cpu_info *
19456 mips_cpu_info_from_arch (int arch)
19457 {
19458   int i;
19459
19460   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19461     if (arch == mips_cpu_info_table[i].cpu)
19462       return (&mips_cpu_info_table[i]);
19463
19464   return NULL;
19465 }
19466 \f
19467 static void
19468 show (FILE *stream, const char *string, int *col_p, int *first_p)
19469 {
19470   if (*first_p)
19471     {
19472       fprintf (stream, "%24s", "");
19473       *col_p = 24;
19474     }
19475   else
19476     {
19477       fprintf (stream, ", ");
19478       *col_p += 2;
19479     }
19480
19481   if (*col_p + strlen (string) > 72)
19482     {
19483       fprintf (stream, "\n%24s", "");
19484       *col_p = 24;
19485     }
19486
19487   fprintf (stream, "%s", string);
19488   *col_p += strlen (string);
19489
19490   *first_p = 0;
19491 }
19492
19493 void
19494 md_show_usage (FILE *stream)
19495 {
19496   int column, first;
19497   size_t i;
19498
19499   fprintf (stream, _("\
19500 MIPS options:\n\
19501 -EB                     generate big endian output\n\
19502 -EL                     generate little endian output\n\
19503 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
19504 -G NUM                  allow referencing objects up to NUM bytes\n\
19505                         implicitly with the gp register [default 8]\n"));
19506   fprintf (stream, _("\
19507 -mips1                  generate MIPS ISA I instructions\n\
19508 -mips2                  generate MIPS ISA II instructions\n\
19509 -mips3                  generate MIPS ISA III instructions\n\
19510 -mips4                  generate MIPS ISA IV instructions\n\
19511 -mips5                  generate MIPS ISA V instructions\n\
19512 -mips32                 generate MIPS32 ISA instructions\n\
19513 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
19514 -mips64                 generate MIPS64 ISA instructions\n\
19515 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
19516 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
19517
19518   first = 1;
19519
19520   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19521     show (stream, mips_cpu_info_table[i].name, &column, &first);
19522   show (stream, "from-abi", &column, &first);
19523   fputc ('\n', stream);
19524
19525   fprintf (stream, _("\
19526 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19527 -no-mCPU                don't generate code specific to CPU.\n\
19528                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
19529
19530   first = 1;
19531
19532   show (stream, "3900", &column, &first);
19533   show (stream, "4010", &column, &first);
19534   show (stream, "4100", &column, &first);
19535   show (stream, "4650", &column, &first);
19536   fputc ('\n', stream);
19537
19538   fprintf (stream, _("\
19539 -mips16                 generate mips16 instructions\n\
19540 -no-mips16              do not generate mips16 instructions\n"));
19541   fprintf (stream, _("\
19542 -mmicromips             generate microMIPS instructions\n\
19543 -mno-micromips          do not generate microMIPS instructions\n"));
19544   fprintf (stream, _("\
19545 -msmartmips             generate smartmips instructions\n\
19546 -mno-smartmips          do not generate smartmips instructions\n"));  
19547   fprintf (stream, _("\
19548 -mdsp                   generate DSP instructions\n\
19549 -mno-dsp                do not generate DSP instructions\n"));
19550   fprintf (stream, _("\
19551 -mdspr2                 generate DSP R2 instructions\n\
19552 -mno-dspr2              do not generate DSP R2 instructions\n"));
19553   fprintf (stream, _("\
19554 -mmt                    generate MT instructions\n\
19555 -mno-mt                 do not generate MT instructions\n"));
19556   fprintf (stream, _("\
19557 -mmcu                   generate MCU instructions\n\
19558 -mno-mcu                do not generate MCU instructions\n"));
19559   fprintf (stream, _("\
19560 -mfix-loongson2f-jump   work around Loongson2F JUMP instructions\n\
19561 -mfix-loongson2f-nop    work around Loongson2F NOP errata\n\
19562 -mfix-vr4120            work around certain VR4120 errata\n\
19563 -mfix-vr4130            work around VR4130 mflo/mfhi errata\n\
19564 -mfix-24k               insert a nop after ERET and DERET instructions\n\
19565 -mfix-cn63xxp1          work around CN63XXP1 PREF errata\n\
19566 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
19567 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
19568 -msym32                 assume all symbols have 32-bit values\n\
19569 -O0                     remove unneeded NOPs, do not swap branches\n\
19570 -O                      remove unneeded NOPs and swap branches\n\
19571 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
19572 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
19573   fprintf (stream, _("\
19574 -mhard-float            allow floating-point instructions\n\
19575 -msoft-float            do not allow floating-point instructions\n\
19576 -msingle-float          only allow 32-bit floating-point operations\n\
19577 -mdouble-float          allow 32-bit and 64-bit floating-point operations\n\
19578 --[no-]construct-floats [dis]allow floating point values to be constructed\n"
19579                      ));
19580 #ifdef OBJ_ELF
19581   fprintf (stream, _("\
19582 -KPIC, -call_shared     generate SVR4 position independent code\n\
19583 -call_nonpic            generate non-PIC code that can operate with DSOs\n\
19584 -mvxworks-pic           generate VxWorks position independent code\n\
19585 -non_shared             do not generate code that can operate with DSOs\n\
19586 -xgot                   assume a 32 bit GOT\n\
19587 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
19588 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
19589                         position dependent (non shared) code\n\
19590 -mabi=ABI               create ABI conformant object file for:\n"));
19591
19592   first = 1;
19593
19594   show (stream, "32", &column, &first);
19595   show (stream, "o64", &column, &first);
19596   show (stream, "n32", &column, &first);
19597   show (stream, "64", &column, &first);
19598   show (stream, "eabi", &column, &first);
19599
19600   fputc ('\n', stream);
19601
19602   fprintf (stream, _("\
19603 -32                     create o32 ABI object file (default)\n\
19604 -n32                    create n32 ABI object file\n\
19605 -64                     create 64 ABI object file\n"));
19606 #endif
19607 }
19608
19609 #ifdef TE_IRIX
19610 enum dwarf2_format
19611 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19612 {
19613   if (HAVE_64BIT_SYMBOLS)
19614     return dwarf2_format_64bit_irix;
19615   else
19616     return dwarf2_format_32bit;
19617 }
19618 #endif
19619
19620 int
19621 mips_dwarf2_addr_size (void)
19622 {
19623   if (HAVE_64BIT_OBJECTS)
19624     return 8;
19625   else
19626     return 4;
19627 }
19628
19629 /* Standard calling conventions leave the CFA at SP on entry.  */
19630 void
19631 mips_cfi_frame_initial_instructions (void)
19632 {
19633   cfi_add_CFA_def_cfa_register (SP);
19634 }
19635
19636 int
19637 tc_mips_regname_to_dw2regnum (char *regname)
19638 {
19639   unsigned int regnum = -1;
19640   unsigned int reg;
19641
19642   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19643     regnum = reg;
19644
19645   return regnum;
19646 }