xref: /netbsd-src/external/gpl3/binutils/dist/gas/config/tc-mips.c (revision 9573673d78c64ea1eac42d7f2e9521be89932ae5)
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
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   /* True if this is a mips16 instruction and if we want the extended
149      form of INSN_MO.  */
150   bfd_boolean use_extend;
151 
152   /* The 16-bit extension instruction to use when USE_EXTEND is true.  */
153   unsigned short extend;
154 
155   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
156      a copy of INSN_MO->match with the operands filled in.  */
157   unsigned long insn_opcode;
158 
159   /* The frag that contains the instruction.  */
160   struct frag *frag;
161 
162   /* The offset into FRAG of the first instruction byte.  */
163   long where;
164 
165   /* The relocs associated with the instruction, if any.  */
166   fixS *fixp[3];
167 
168   /* True if this entry cannot be moved from its current position.  */
169   unsigned int fixed_p : 1;
170 
171   /* True if this instruction occurred in a .set noreorder block.  */
172   unsigned int noreorder_p : 1;
173 
174   /* True for mips16 instructions that jump to an absolute address.  */
175   unsigned int mips16_absolute_jump_p : 1;
176 
177   /* True if this instruction is complete.  */
178   unsigned int complete_p : 1;
179 };
180 
181 /* The ABI to use.  */
182 enum mips_abi_level
183 {
184   NO_ABI = 0,
185   O32_ABI,
186   O64_ABI,
187   N32_ABI,
188   N64_ABI,
189   EABI_ABI
190 };
191 
192 /* MIPS ABI we are using for this output file.  */
193 static enum mips_abi_level mips_abi = NO_ABI;
194 
195 /* Whether or not we have code that can call pic code.  */
196 int mips_abicalls = FALSE;
197 
198 /* Whether or not we have code which can be put into a shared
199    library.  */
200 static bfd_boolean mips_in_shared = TRUE;
201 
202 /* This is the set of options which may be modified by the .set
203    pseudo-op.  We use a struct so that .set push and .set pop are more
204    reliable.  */
205 
206 struct mips_set_options
207 {
208   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
209      if it has not been initialized.  Changed by `.set mipsN', and the
210      -mipsN command line option, and the default CPU.  */
211   int isa;
212   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
213      if they have not been initialized.  Changed by `.set <asename>', by
214      command line options, and based on the default architecture.  */
215   int ase_mips3d;
216   int ase_mdmx;
217   int ase_smartmips;
218   int ase_dsp;
219   int ase_dspr2;
220   int ase_mt;
221   int ase_mcu;
222   /* Whether we are assembling for the mips16 processor.  0 if we are
223      not, 1 if we are, and -1 if the value has not been initialized.
224      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
225      -nomips16 command line options, and the default CPU.  */
226   int mips16;
227   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
228      1 if we are, and -1 if the value has not been initialized.  Changed
229      by `.set micromips' and `.set nomicromips', and the -mmicromips
230      and -mno-micromips command line options, and the default CPU.  */
231   int micromips;
232   /* Non-zero if we should not reorder instructions.  Changed by `.set
233      reorder' and `.set noreorder'.  */
234   int noreorder;
235   /* Non-zero if we should not permit the register designated "assembler
236      temporary" to be used in instructions.  The value is the register
237      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
238      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
239   unsigned int at;
240   /* Non-zero if we should warn when a macro instruction expands into
241      more than one machine instruction.  Changed by `.set nomacro' and
242      `.set macro'.  */
243   int warn_about_macros;
244   /* Non-zero if we should not move instructions.  Changed by `.set
245      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
246   int nomove;
247   /* Non-zero if we should not optimize branches by moving the target
248      of the branch into the delay slot.  Actually, we don't perform
249      this optimization anyhow.  Changed by `.set bopt' and `.set
250      nobopt'.  */
251   int nobopt;
252   /* Non-zero if we should not autoextend mips16 instructions.
253      Changed by `.set autoextend' and `.set noautoextend'.  */
254   int noautoextend;
255   /* Restrict general purpose registers and floating point registers
256      to 32 bit.  This is initially determined when -mgp32 or -mfp32
257      is passed but can changed if the assembler code uses .set mipsN.  */
258   int gp32;
259   int fp32;
260   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
261      command line option, and the default CPU.  */
262   int arch;
263   /* True if ".set sym32" is in effect.  */
264   bfd_boolean sym32;
265   /* True if floating-point operations are not allowed.  Changed by .set
266      softfloat or .set hardfloat, by command line options -msoft-float or
267      -mhard-float.  The default is false.  */
268   bfd_boolean soft_float;
269 
270   /* True if only single-precision floating-point operations are allowed.
271      Changed by .set singlefloat or .set doublefloat, command-line options
272      -msingle-float or -mdouble-float.  The default is false.  */
273   bfd_boolean single_float;
274 };
275 
276 /* This is the struct we use to hold the current set of options.  Note
277    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
278    -1 to indicate that they have not been initialized.  */
279 
280 /* True if -mgp32 was passed.  */
281 static int file_mips_gp32 = -1;
282 
283 /* True if -mfp32 was passed.  */
284 static int file_mips_fp32 = -1;
285 
286 /* 1 if -msoft-float, 0 if -mhard-float.  The default is 0.  */
287 static int file_mips_soft_float = 0;
288 
289 /* 1 if -msingle-float, 0 if -mdouble-float.  The default is 0.   */
290 static int file_mips_single_float = 0;
291 
292 static struct mips_set_options mips_opts =
293 {
294   /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
295   /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
296   /* ase_mcu */ -1, /* mips16 */ -1, /* micromips */ -1, /* noreorder */ 0,
297   /* at */ ATREG, /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
298   /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
299   /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
300 };
301 
302 /* These variables are filled in with the masks of registers used.
303    The object format code reads them and puts them in the appropriate
304    place.  */
305 unsigned long mips_gprmask;
306 unsigned long mips_cprmask[4];
307 
308 /* MIPS ISA we are using for this output file.  */
309 static int file_mips_isa = ISA_UNKNOWN;
310 
311 /* True if any MIPS16 code was produced.  */
312 static int file_ase_mips16;
313 
314 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32		\
315 			      || mips_opts.isa == ISA_MIPS32R2		\
316 			      || mips_opts.isa == ISA_MIPS64		\
317 			      || mips_opts.isa == ISA_MIPS64R2)
318 
319 /* True if any microMIPS code was produced.  */
320 static int file_ase_micromips;
321 
322 /* True if we want to create R_MIPS_JALR for jalr $25.  */
323 #ifdef TE_IRIX
324 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
325 #else
326 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
327    because there's no place for any addend, the only acceptable
328    expression is a bare symbol.  */
329 #define MIPS_JALR_HINT_P(EXPR) \
330   (!HAVE_IN_PLACE_ADDENDS \
331    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
332 #endif
333 
334 /* True if -mips3d was passed or implied by arguments passed on the
335    command line (e.g., by -march).  */
336 static int file_ase_mips3d;
337 
338 /* True if -mdmx was passed or implied by arguments passed on the
339    command line (e.g., by -march).  */
340 static int file_ase_mdmx;
341 
342 /* True if -msmartmips was passed or implied by arguments passed on the
343    command line (e.g., by -march).  */
344 static int file_ase_smartmips;
345 
346 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32		\
347 				|| mips_opts.isa == ISA_MIPS32R2)
348 
349 /* True if -mdsp was passed or implied by arguments passed on the
350    command line (e.g., by -march).  */
351 static int file_ase_dsp;
352 
353 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2		\
354 			      || mips_opts.isa == ISA_MIPS64R2		\
355 			      || mips_opts.micromips)
356 
357 #define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
358 
359 /* True if -mdspr2 was passed or implied by arguments passed on the
360    command line (e.g., by -march).  */
361 static int file_ase_dspr2;
362 
363 #define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2		\
364 			        || mips_opts.isa == ISA_MIPS64R2	\
365 				|| mips_opts.micromips)
366 
367 /* True if -mmt was passed or implied by arguments passed on the
368    command line (e.g., by -march).  */
369 static int file_ase_mt;
370 
371 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2		\
372 			     || mips_opts.isa == ISA_MIPS64R2)
373 
374 #define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2		\
375 			      || mips_opts.isa == ISA_MIPS64R2		\
376 			      || mips_opts.micromips)
377 
378 /* The argument of the -march= flag.  The architecture we are assembling.  */
379 static int file_mips_arch = CPU_UNKNOWN;
380 static const char *mips_arch_string;
381 
382 /* The argument of the -mtune= flag.  The architecture for which we
383    are optimizing.  */
384 static int mips_tune = CPU_UNKNOWN;
385 static const char *mips_tune_string;
386 
387 /* True when generating 32-bit code for a 64-bit processor.  */
388 static int mips_32bitmode = 0;
389 
390 /* True if the given ABI requires 32-bit registers.  */
391 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
392 
393 /* Likewise 64-bit registers.  */
394 #define ABI_NEEDS_64BIT_REGS(ABI)	\
395   ((ABI) == N32_ABI 			\
396    || (ABI) == N64_ABI			\
397    || (ABI) == O64_ABI)
398 
399 /*  Return true if ISA supports 64 bit wide gp registers.  */
400 #define ISA_HAS_64BIT_REGS(ISA)		\
401   ((ISA) == ISA_MIPS3			\
402    || (ISA) == ISA_MIPS4		\
403    || (ISA) == ISA_MIPS5		\
404    || (ISA) == ISA_MIPS64		\
405    || (ISA) == ISA_MIPS64R2)
406 
407 /*  Return true if ISA supports 64 bit wide float registers.  */
408 #define ISA_HAS_64BIT_FPRS(ISA)		\
409   ((ISA) == ISA_MIPS3			\
410    || (ISA) == ISA_MIPS4		\
411    || (ISA) == ISA_MIPS5		\
412    || (ISA) == ISA_MIPS32R2		\
413    || (ISA) == ISA_MIPS64		\
414    || (ISA) == ISA_MIPS64R2)
415 
416 /* Return true if ISA supports 64-bit right rotate (dror et al.)
417    instructions.  */
418 #define ISA_HAS_DROR(ISA)		\
419   ((ISA) == ISA_MIPS64R2		\
420    || (mips_opts.micromips		\
421        && ISA_HAS_64BIT_REGS (ISA))	\
422    )
423 
424 /* Return true if ISA supports 32-bit right rotate (ror et al.)
425    instructions.  */
426 #define ISA_HAS_ROR(ISA)		\
427   ((ISA) == ISA_MIPS32R2		\
428    || (ISA) == ISA_MIPS64R2		\
429    || mips_opts.ase_smartmips		\
430    || mips_opts.micromips		\
431    )
432 
433 /* Return true if ISA supports single-precision floats in odd registers.  */
434 #define ISA_HAS_ODD_SINGLE_FPR(ISA)	\
435   ((ISA) == ISA_MIPS32			\
436    || (ISA) == ISA_MIPS32R2		\
437    || (ISA) == ISA_MIPS64		\
438    || (ISA) == ISA_MIPS64R2)
439 
440 /* Return true if ISA supports move to/from high part of a 64-bit
441    floating-point register. */
442 #define ISA_HAS_MXHC1(ISA)		\
443   ((ISA) == ISA_MIPS32R2		\
444    || (ISA) == ISA_MIPS64R2)
445 
446 #define HAVE_32BIT_GPRS		                   \
447     (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
448 
449 #define HAVE_32BIT_FPRS                            \
450     (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
451 
452 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
453 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
454 
455 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
456 
457 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
458 
459 /* True if relocations are stored in-place.  */
460 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
461 
462 /* The ABI-derived address size.  */
463 #define HAVE_64BIT_ADDRESSES \
464   (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
465 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
466 
467 /* The size of symbolic constants (i.e., expressions of the form
468    "SYMBOL" or "SYMBOL + OFFSET").  */
469 #define HAVE_32BIT_SYMBOLS \
470   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
471 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
472 
473 /* Addresses are loaded in different ways, depending on the address size
474    in use.  The n32 ABI Documentation also mandates the use of additions
475    with overflow checking, but existing implementations don't follow it.  */
476 #define ADDRESS_ADD_INSN						\
477    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
478 
479 #define ADDRESS_ADDI_INSN						\
480    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
481 
482 #define ADDRESS_LOAD_INSN						\
483    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
484 
485 #define ADDRESS_STORE_INSN						\
486    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
487 
488 /* Return true if the given CPU supports the MIPS16 ASE.  */
489 #define CPU_HAS_MIPS16(cpu)						\
490    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0		\
491     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
492 
493 /* Return true if the given CPU supports the microMIPS ASE.  */
494 #define CPU_HAS_MICROMIPS(cpu)	0
495 
496 /* True if CPU has a dror instruction.  */
497 #define CPU_HAS_DROR(CPU)	((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
498 
499 /* True if CPU has a ror instruction.  */
500 #define CPU_HAS_ROR(CPU)	CPU_HAS_DROR (CPU)
501 
502 /* True if CPU is in the Octeon family */
503 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
504 
505 /* True if CPU has seq/sne and seqi/snei instructions.  */
506 #define CPU_HAS_SEQ(CPU)	(CPU_IS_OCTEON (CPU))
507 
508 /* True if mflo and mfhi can be immediately followed by instructions
509    which write to the HI and LO registers.
510 
511    According to MIPS specifications, MIPS ISAs I, II, and III need
512    (at least) two instructions between the reads of HI/LO and
513    instructions which write them, and later ISAs do not.  Contradicting
514    the MIPS specifications, some MIPS IV processor user manuals (e.g.
515    the UM for the NEC Vr5000) document needing the instructions between
516    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
517    MIPS64 and later ISAs to have the interlocks, plus any specific
518    earlier-ISA CPUs for which CPU documentation declares that the
519    instructions are really interlocked.  */
520 #define hilo_interlocks \
521   (mips_opts.isa == ISA_MIPS32                        \
522    || mips_opts.isa == ISA_MIPS32R2                   \
523    || mips_opts.isa == ISA_MIPS64                     \
524    || mips_opts.isa == ISA_MIPS64R2                   \
525    || mips_opts.arch == CPU_R4010                     \
526    || mips_opts.arch == CPU_R10000                    \
527    || mips_opts.arch == CPU_R12000                    \
528    || mips_opts.arch == CPU_R14000                    \
529    || mips_opts.arch == CPU_R16000                    \
530    || mips_opts.arch == CPU_RM7000                    \
531    || mips_opts.arch == CPU_VR5500                    \
532    || mips_opts.micromips                             \
533    )
534 
535 /* Whether the processor uses hardware interlocks to protect reads
536    from the GPRs after they are loaded from memory, and thus does not
537    require nops to be inserted.  This applies to instructions marked
538    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
539    level I and microMIPS mode instructions are always interlocked.  */
540 #define gpr_interlocks                                \
541   (mips_opts.isa != ISA_MIPS1                         \
542    || mips_opts.arch == CPU_R3900                     \
543    || mips_opts.micromips                             \
544    )
545 
546 /* Whether the processor uses hardware interlocks to avoid delays
547    required by coprocessor instructions, and thus does not require
548    nops to be inserted.  This applies to instructions marked
549    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
550    between instructions marked INSN_WRITE_COND_CODE and ones marked
551    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
552    levels I, II, and III and microMIPS mode instructions are always
553    interlocked.  */
554 /* Itbl support may require additional care here.  */
555 #define cop_interlocks                                \
556   ((mips_opts.isa != ISA_MIPS1                        \
557     && mips_opts.isa != ISA_MIPS2                     \
558     && mips_opts.isa != ISA_MIPS3)                    \
559    || mips_opts.arch == CPU_R4300                     \
560    || mips_opts.micromips                             \
561    )
562 
563 /* Whether the processor uses hardware interlocks to protect reads
564    from coprocessor registers after they are loaded from memory, and
565    thus does not require nops to be inserted.  This applies to
566    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
567    requires at MIPS ISA level I and microMIPS mode instructions are
568    always interlocked.  */
569 #define cop_mem_interlocks                            \
570   (mips_opts.isa != ISA_MIPS1                         \
571    || mips_opts.micromips                             \
572    )
573 
574 /* Is this a mfhi or mflo instruction?  */
575 #define MF_HILO_INSN(PINFO) \
576   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
577 
578 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
579    has been selected.  This implies, in particular, that addresses of text
580    labels have their LSB set.  */
581 #define HAVE_CODE_COMPRESSION						\
582   ((mips_opts.mips16 | mips_opts.micromips) != 0)
583 
584 /* MIPS PIC level.  */
585 
586 enum mips_pic_level mips_pic;
587 
588 /* 1 if we should generate 32 bit offsets from the $gp register in
589    SVR4_PIC mode.  Currently has no meaning in other modes.  */
590 static int mips_big_got = 0;
591 
592 /* 1 if trap instructions should used for overflow rather than break
593    instructions.  */
594 static int mips_trap = 0;
595 
596 /* 1 if double width floating point constants should not be constructed
597    by assembling two single width halves into two single width floating
598    point registers which just happen to alias the double width destination
599    register.  On some architectures this aliasing can be disabled by a bit
600    in the status register, and the setting of this bit cannot be determined
601    automatically at assemble time.  */
602 static int mips_disable_float_construction;
603 
604 /* Non-zero if any .set noreorder directives were used.  */
605 
606 static int mips_any_noreorder;
607 
608 /* Non-zero if nops should be inserted when the register referenced in
609    an mfhi/mflo instruction is read in the next two instructions.  */
610 static int mips_7000_hilo_fix;
611 
612 /* The size of objects in the small data section.  */
613 static unsigned int g_switch_value = 8;
614 /* Whether the -G option was used.  */
615 static int g_switch_seen = 0;
616 
617 #define N_RMASK 0xc4
618 #define N_VFP   0xd4
619 
620 /* If we can determine in advance that GP optimization won't be
621    possible, we can skip the relaxation stuff that tries to produce
622    GP-relative references.  This makes delay slot optimization work
623    better.
624 
625    This function can only provide a guess, but it seems to work for
626    gcc output.  It needs to guess right for gcc, otherwise gcc
627    will put what it thinks is a GP-relative instruction in a branch
628    delay slot.
629 
630    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
631    fixed it for the non-PIC mode.  KR 95/04/07  */
632 static int nopic_need_relax (symbolS *, int);
633 
634 /* handle of the OPCODE hash table */
635 static struct hash_control *op_hash = NULL;
636 
637 /* The opcode hash table we use for the mips16.  */
638 static struct hash_control *mips16_op_hash = NULL;
639 
640 /* The opcode hash table we use for the microMIPS ASE.  */
641 static struct hash_control *micromips_op_hash = NULL;
642 
643 /* This array holds the chars that always start a comment.  If the
644     pre-processor is disabled, these aren't very useful */
645 const char comment_chars[] = "#";
646 
647 /* This array holds the chars that only start a comment at the beginning of
648    a line.  If the line seems to have the form '# 123 filename'
649    .line and .file directives will appear in the pre-processed output */
650 /* Note that input_file.c hand checks for '#' at the beginning of the
651    first line of the input file.  This is because the compiler outputs
652    #NO_APP at the beginning of its output.  */
653 /* Also note that C style comments are always supported.  */
654 const char line_comment_chars[] = "#";
655 
656 /* This array holds machine specific line separator characters.  */
657 const char line_separator_chars[] = ";";
658 
659 /* Chars that can be used to separate mant from exp in floating point nums */
660 const char EXP_CHARS[] = "eE";
661 
662 /* Chars that mean this number is a floating point constant */
663 /* As in 0f12.456 */
664 /* or    0d1.2345e12 */
665 const char FLT_CHARS[] = "rRsSfFdDxXpP";
666 
667 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
668    changed in read.c .  Ideally it shouldn't have to know about it at all,
669    but nothing is ideal around here.
670  */
671 
672 static char *insn_error;
673 
674 static int auto_align = 1;
675 
676 /* When outputting SVR4 PIC code, the assembler needs to know the
677    offset in the stack frame from which to restore the $gp register.
678    This is set by the .cprestore pseudo-op, and saved in this
679    variable.  */
680 static offsetT mips_cprestore_offset = -1;
681 
682 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
683    more optimizations, it can use a register value instead of a memory-saved
684    offset and even an other register than $gp as global pointer.  */
685 static offsetT mips_cpreturn_offset = -1;
686 static int mips_cpreturn_register = -1;
687 static int mips_gp_register = GP;
688 static int mips_gprel_offset = 0;
689 
690 /* Whether mips_cprestore_offset has been set in the current function
691    (or whether it has already been warned about, if not).  */
692 static int mips_cprestore_valid = 0;
693 
694 /* This is the register which holds the stack frame, as set by the
695    .frame pseudo-op.  This is needed to implement .cprestore.  */
696 static int mips_frame_reg = SP;
697 
698 /* Whether mips_frame_reg has been set in the current function
699    (or whether it has already been warned about, if not).  */
700 static int mips_frame_reg_valid = 0;
701 
702 /* To output NOP instructions correctly, we need to keep information
703    about the previous two instructions.  */
704 
705 /* Whether we are optimizing.  The default value of 2 means to remove
706    unneeded NOPs and swap branch instructions when possible.  A value
707    of 1 means to not swap branches.  A value of 0 means to always
708    insert NOPs.  */
709 static int mips_optimize = 2;
710 
711 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
712    equivalent to seeing no -g option at all.  */
713 static int mips_debug = 0;
714 
715 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
716 #define MAX_VR4130_NOPS 4
717 
718 /* The maximum number of NOPs needed to fill delay slots.  */
719 #define MAX_DELAY_NOPS 2
720 
721 /* The maximum number of NOPs needed for any purpose.  */
722 #define MAX_NOPS 4
723 
724 /* A list of previous instructions, with index 0 being the most recent.
725    We need to look back MAX_NOPS instructions when filling delay slots
726    or working around processor errata.  We need to look back one
727    instruction further if we're thinking about using history[0] to
728    fill a branch delay slot.  */
729 static struct mips_cl_insn history[1 + MAX_NOPS];
730 
731 /* Nop instructions used by emit_nop.  */
732 static struct mips_cl_insn nop_insn;
733 static struct mips_cl_insn mips16_nop_insn;
734 static struct mips_cl_insn micromips_nop16_insn;
735 static struct mips_cl_insn micromips_nop32_insn;
736 
737 /* The appropriate nop for the current mode.  */
738 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
739 		  : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
740 
741 /* The size of NOP_INSN in bytes.  */
742 #define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
743 
744 /* If this is set, it points to a frag holding nop instructions which
745    were inserted before the start of a noreorder section.  If those
746    nops turn out to be unnecessary, the size of the frag can be
747    decreased.  */
748 static fragS *prev_nop_frag;
749 
750 /* The number of nop instructions we created in prev_nop_frag.  */
751 static int prev_nop_frag_holds;
752 
753 /* The number of nop instructions that we know we need in
754    prev_nop_frag.  */
755 static int prev_nop_frag_required;
756 
757 /* The number of instructions we've seen since prev_nop_frag.  */
758 static int prev_nop_frag_since;
759 
760 /* For ECOFF and ELF, relocations against symbols are done in two
761    parts, with a HI relocation and a LO relocation.  Each relocation
762    has only 16 bits of space to store an addend.  This means that in
763    order for the linker to handle carries correctly, it must be able
764    to locate both the HI and the LO relocation.  This means that the
765    relocations must appear in order in the relocation table.
766 
767    In order to implement this, we keep track of each unmatched HI
768    relocation.  We then sort them so that they immediately precede the
769    corresponding LO relocation.  */
770 
771 struct mips_hi_fixup
772 {
773   /* Next HI fixup.  */
774   struct mips_hi_fixup *next;
775   /* This fixup.  */
776   fixS *fixp;
777   /* The section this fixup is in.  */
778   segT seg;
779 };
780 
781 /* The list of unmatched HI relocs.  */
782 
783 static struct mips_hi_fixup *mips_hi_fixup_list;
784 
785 /* The frag containing the last explicit relocation operator.
786    Null if explicit relocations have not been used.  */
787 
788 static fragS *prev_reloc_op_frag;
789 
790 /* Map normal MIPS register numbers to mips16 register numbers.  */
791 
792 #define X ILLEGAL_REG
793 static const int mips32_to_16_reg_map[] =
794 {
795   X, X, 2, 3, 4, 5, 6, 7,
796   X, X, X, X, X, X, X, X,
797   0, 1, X, X, X, X, X, X,
798   X, X, X, X, X, X, X, X
799 };
800 #undef X
801 
802 /* Map mips16 register numbers to normal MIPS register numbers.  */
803 
804 static const unsigned int mips16_to_32_reg_map[] =
805 {
806   16, 17, 2, 3, 4, 5, 6, 7
807 };
808 
809 /* Map normal MIPS register numbers to microMIPS register numbers.  */
810 
811 #define mips32_to_micromips_reg_b_map	mips32_to_16_reg_map
812 #define mips32_to_micromips_reg_c_map	mips32_to_16_reg_map
813 #define mips32_to_micromips_reg_d_map	mips32_to_16_reg_map
814 #define mips32_to_micromips_reg_e_map	mips32_to_16_reg_map
815 #define mips32_to_micromips_reg_f_map	mips32_to_16_reg_map
816 #define mips32_to_micromips_reg_g_map	mips32_to_16_reg_map
817 #define mips32_to_micromips_reg_l_map	mips32_to_16_reg_map
818 
819 #define X ILLEGAL_REG
820 /* reg type h: 4, 5, 6.  */
821 static const int mips32_to_micromips_reg_h_map[] =
822 {
823   X, X, X, X, 4, 5, 6, X,
824   X, X, X, X, X, X, X, X,
825   X, X, X, X, X, X, X, X,
826   X, X, X, X, X, X, X, X
827 };
828 
829 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20.  */
830 static const int mips32_to_micromips_reg_m_map[] =
831 {
832   0, X, 2, 3, X, X, X, X,
833   X, X, X, X, X, X, X, X,
834   4, 1, 5, 6, 7, X, X, X,
835   X, X, X, X, X, X, X, X
836 };
837 
838 /* reg type q: 0, 2-7. 17.  */
839 static const int mips32_to_micromips_reg_q_map[] =
840 {
841   0, X, 2, 3, 4, 5, 6, 7,
842   X, X, X, X, X, X, X, X,
843   X, 1, X, X, X, X, X, X,
844   X, X, X, X, X, X, X, X
845 };
846 
847 #define mips32_to_micromips_reg_n_map  mips32_to_micromips_reg_m_map
848 #undef X
849 
850 /* Map microMIPS register numbers to normal MIPS register numbers.  */
851 
852 #define micromips_to_32_reg_b_map	mips16_to_32_reg_map
853 #define micromips_to_32_reg_c_map	mips16_to_32_reg_map
854 #define micromips_to_32_reg_d_map	mips16_to_32_reg_map
855 #define micromips_to_32_reg_e_map	mips16_to_32_reg_map
856 #define micromips_to_32_reg_f_map	mips16_to_32_reg_map
857 #define micromips_to_32_reg_g_map	mips16_to_32_reg_map
858 
859 /* The microMIPS registers with type h.  */
860 static const unsigned int micromips_to_32_reg_h_map[] =
861 {
862   5, 5, 6, 4, 4, 4, 4, 4
863 };
864 
865 /* The microMIPS registers with type i.  */
866 static const unsigned int micromips_to_32_reg_i_map[] =
867 {
868   6, 7, 7, 21, 22, 5, 6, 7
869 };
870 
871 #define micromips_to_32_reg_l_map	mips16_to_32_reg_map
872 
873 /* The microMIPS registers with type m.  */
874 static const unsigned int micromips_to_32_reg_m_map[] =
875 {
876   0, 17, 2, 3, 16, 18, 19, 20
877 };
878 
879 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
880 
881 /* The microMIPS registers with type q.  */
882 static const unsigned int micromips_to_32_reg_q_map[] =
883 {
884   0, 17, 2, 3, 4, 5, 6, 7
885 };
886 
887 /* microMIPS imm type B.  */
888 static const int micromips_imm_b_map[] =
889 {
890   1, 4, 8, 12, 16, 20, 24, -1
891 };
892 
893 /* microMIPS imm type C.  */
894 static const int micromips_imm_c_map[] =
895 {
896   128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
897 };
898 
899 /* Classifies the kind of instructions we're interested in when
900    implementing -mfix-vr4120.  */
901 enum fix_vr4120_class
902 {
903   FIX_VR4120_MACC,
904   FIX_VR4120_DMACC,
905   FIX_VR4120_MULT,
906   FIX_VR4120_DMULT,
907   FIX_VR4120_DIV,
908   FIX_VR4120_MTHILO,
909   NUM_FIX_VR4120_CLASSES
910 };
911 
912 /* ...likewise -mtrap-zero-jump.  */
913 static bfd_boolean mips_trap_zero_jump;
914 
915 /* ...likewise -mfix-loongson2f-jump.  */
916 static bfd_boolean mips_fix_loongson2f_jump;
917 
918 /* ...likewise -mfix-loongson2f-nop.  */
919 static bfd_boolean mips_fix_loongson2f_nop;
920 
921 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
922 static bfd_boolean mips_fix_loongson2f;
923 
924 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
925    there must be at least one other instruction between an instruction
926    of type X and an instruction of type Y.  */
927 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
928 
929 /* True if -mfix-vr4120 is in force.  */
930 static int mips_fix_vr4120;
931 
932 /* ...likewise -mfix-vr4130.  */
933 static int mips_fix_vr4130;
934 
935 /* ...likewise -mfix-24k.  */
936 static int mips_fix_24k;
937 
938 /* ...likewise -mfix-cn63xxp1 */
939 static bfd_boolean mips_fix_cn63xxp1;
940 
941 /* We don't relax branches by default, since this causes us to expand
942    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
943    fail to compute the offset before expanding the macro to the most
944    efficient expansion.  */
945 
946 static int mips_relax_branch;
947 
948 static int mips_fix_loongson2f_btb;
949 
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 /* Is the given value a sign-extended 32-bit value?  */
1190 #define IS_SEXT_32BIT_NUM(x)						\
1191   (((x) &~ (offsetT) 0x7fffffff) == 0					\
1192    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1193 
1194 /* Is the given value a sign-extended 16-bit value?  */
1195 #define IS_SEXT_16BIT_NUM(x)						\
1196   (((x) &~ (offsetT) 0x7fff) == 0					\
1197    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1198 
1199 /* Is the given value a sign-extended 12-bit value?  */
1200 #define IS_SEXT_12BIT_NUM(x)						\
1201   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1202 
1203 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1204 #define IS_ZEXT_32BIT_NUM(x)						\
1205   (((x) &~ (offsetT) 0xffffffff) == 0					\
1206    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1207 
1208 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1209    VALUE << SHIFT.  VALUE is evaluated exactly once.  */
1210 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1211   (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1212 	      | (((VALUE) & (MASK)) << (SHIFT)))
1213 
1214 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1215    SHIFT places.  */
1216 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1217   (((STRUCT) >> (SHIFT)) & (MASK))
1218 
1219 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1220    INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1221 
1222    include/opcode/mips.h specifies operand fields using the macros
1223    OP_MASK_<FIELD> and OP_SH_<FIELD>.  The MIPS16 equivalents start
1224    with "MIPS16OP" instead of "OP".  */
1225 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1226   do \
1227     if (!(MICROMIPS)) \
1228       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1229 		   OP_MASK_##FIELD, OP_SH_##FIELD); \
1230     else \
1231       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1232 		   MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1233   while (0)
1234 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1235   INSERT_BITS ((INSN).insn_opcode, VALUE, \
1236 		MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1237 
1238 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1239 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1240   (!(MICROMIPS) \
1241    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1242    : EXTRACT_BITS ((INSN).insn_opcode, \
1243 		   MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1244 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1245   EXTRACT_BITS ((INSN).insn_opcode, \
1246 		MIPS16OP_MASK_##FIELD, \
1247 		MIPS16OP_SH_##FIELD)
1248 
1249 /* Whether or not we are emitting a branch-likely macro.  */
1250 static bfd_boolean emit_branch_likely_macro = FALSE;
1251 
1252 /* Global variables used when generating relaxable macros.  See the
1253    comment above RELAX_ENCODE for more details about how relaxation
1254    is used.  */
1255 static struct {
1256   /* 0 if we're not emitting a relaxable macro.
1257      1 if we're emitting the first of the two relaxation alternatives.
1258      2 if we're emitting the second alternative.  */
1259   int sequence;
1260 
1261   /* The first relaxable fixup in the current frag.  (In other words,
1262      the first fixup that refers to relaxable code.)  */
1263   fixS *first_fixup;
1264 
1265   /* sizes[0] says how many bytes of the first alternative are stored in
1266      the current frag.  Likewise sizes[1] for the second alternative.  */
1267   unsigned int sizes[2];
1268 
1269   /* The symbol on which the choice of sequence depends.  */
1270   symbolS *symbol;
1271 } mips_relax;
1272 
1273 /* Global variables used to decide whether a macro needs a warning.  */
1274 static struct {
1275   /* True if the macro is in a branch delay slot.  */
1276   bfd_boolean delay_slot_p;
1277 
1278   /* Set to the length in bytes required if the macro is in a delay slot
1279      that requires a specific length of instruction, otherwise zero.  */
1280   unsigned int delay_slot_length;
1281 
1282   /* For relaxable macros, sizes[0] is the length of the first alternative
1283      in bytes and sizes[1] is the length of the second alternative.
1284      For non-relaxable macros, both elements give the length of the
1285      macro in bytes.  */
1286   unsigned int sizes[2];
1287 
1288   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1289      instruction of the first alternative in bytes and first_insn_sizes[1]
1290      is the length of the first instruction of the second alternative.
1291      For non-relaxable macros, both elements give the length of the first
1292      instruction in bytes.
1293 
1294      Set to zero if we haven't yet seen the first instruction.  */
1295   unsigned int first_insn_sizes[2];
1296 
1297   /* For relaxable macros, insns[0] is the number of instructions for the
1298      first alternative and insns[1] is the number of instructions for the
1299      second alternative.
1300 
1301      For non-relaxable macros, both elements give the number of
1302      instructions for the macro.  */
1303   unsigned int insns[2];
1304 
1305   /* The first variant frag for this macro.  */
1306   fragS *first_frag;
1307 } mips_macro_warning;
1308 
1309 /* Prototypes for static functions.  */
1310 
1311 #define internalError()							\
1312     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
1313 
1314 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1315 
1316 static void append_insn
1317   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1318    bfd_boolean expansionp);
1319 static void mips_no_prev_insn (void);
1320 static void macro_build (expressionS *, const char *, const char *, ...);
1321 static void mips16_macro_build
1322   (expressionS *, const char *, const char *, va_list *);
1323 static void load_register (int, expressionS *, int);
1324 static void macro_build (expressionS *, const char *, const char *, ...);
1325 static void macro_start (void);
1326 static void macro_end (void);
1327 static void macro (struct mips_cl_insn * ip);
1328 static void mips16_macro (struct mips_cl_insn * ip);
1329 static void mips_ip (char *str, struct mips_cl_insn * ip);
1330 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1331 static void mips16_immed
1332   (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1333    unsigned long *, bfd_boolean *, unsigned short *);
1334 static size_t my_getSmallExpression
1335   (expressionS *, bfd_reloc_code_real_type *, char *);
1336 static void my_getExpression (expressionS *, char *);
1337 static void s_align (int);
1338 static void s_change_sec (int);
1339 static void s_change_section (int);
1340 static void s_cons (int);
1341 static void s_float_cons (int);
1342 static void s_mips_globl (int);
1343 static void s_option (int);
1344 static void s_mipsset (int);
1345 static void s_abicalls (int);
1346 static void s_cpload (int);
1347 static void s_cpsetup (int);
1348 static void s_cplocal (int);
1349 static void s_cprestore (int);
1350 static void s_cpreturn (int);
1351 static void s_dtprelword (int);
1352 static void s_dtpreldword (int);
1353 static void s_tprelword (int);
1354 static void s_tpreldword (int);
1355 static void s_gpvalue (int);
1356 static void s_gpword (int);
1357 static void s_gpdword (int);
1358 static void s_cpadd (int);
1359 static void s_insn (int);
1360 static void md_obj_begin (void);
1361 static void md_obj_end (void);
1362 static void s_mips_ent (int);
1363 static void s_mips_end (int);
1364 static void s_mips_frame (int);
1365 static void s_mips_mask (int reg_type);
1366 static void s_mips_stab (int);
1367 static void s_mips_weakext (int);
1368 static void s_mips_file (int);
1369 static void s_mips_loc (int);
1370 static bfd_boolean pic_need_relax (symbolS *, asection *);
1371 static int relaxed_branch_length (fragS *, asection *, int);
1372 static int validate_mips_insn (const struct mips_opcode *);
1373 static int validate_micromips_insn (const struct mips_opcode *);
1374 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1375 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1376 
1377 /* Table and functions used to map between CPU/ISA names, and
1378    ISA levels, and CPU numbers.  */
1379 
1380 struct mips_cpu_info
1381 {
1382   const char *name;           /* CPU or ISA name.  */
1383   int flags;                  /* ASEs available, or ISA flag.  */
1384   int isa;                    /* ISA level.  */
1385   int cpu;                    /* CPU number (default CPU if ISA).  */
1386 };
1387 
1388 #define MIPS_CPU_IS_ISA		0x0001	/* Is this an ISA?  (If 0, a CPU.) */
1389 #define MIPS_CPU_ASE_SMARTMIPS	0x0002	/* CPU implements SmartMIPS ASE */
1390 #define MIPS_CPU_ASE_DSP	0x0004	/* CPU implements DSP ASE */
1391 #define MIPS_CPU_ASE_MT		0x0008	/* CPU implements MT ASE */
1392 #define MIPS_CPU_ASE_MIPS3D	0x0010	/* CPU implements MIPS-3D ASE */
1393 #define MIPS_CPU_ASE_MDMX	0x0020	/* CPU implements MDMX ASE */
1394 #define MIPS_CPU_ASE_DSPR2	0x0040	/* CPU implements DSP R2 ASE */
1395 #define MIPS_CPU_ASE_MCU	0x0080	/* CPU implements MCU ASE */
1396 
1397 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1398 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1399 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1400 
1401 /* Pseudo-op table.
1402 
1403    The following pseudo-ops from the Kane and Heinrich MIPS book
1404    should be defined here, but are currently unsupported: .alias,
1405    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1406 
1407    The following pseudo-ops from the Kane and Heinrich MIPS book are
1408    specific to the type of debugging information being generated, and
1409    should be defined by the object format: .aent, .begin, .bend,
1410    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1411    .vreg.
1412 
1413    The following pseudo-ops from the Kane and Heinrich MIPS book are
1414    not MIPS CPU specific, but are also not specific to the object file
1415    format.  This file is probably the best place to define them, but
1416    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1417 
1418 static const pseudo_typeS mips_pseudo_table[] =
1419 {
1420   /* MIPS specific pseudo-ops.  */
1421   {"option", s_option, 0},
1422   {"set", s_mipsset, 0},
1423   {"rdata", s_change_sec, 'r'},
1424   {"sdata", s_change_sec, 's'},
1425   {"livereg", s_ignore, 0},
1426   {"abicalls", s_abicalls, 0},
1427   {"cpload", s_cpload, 0},
1428   {"cpsetup", s_cpsetup, 0},
1429   {"cplocal", s_cplocal, 0},
1430   {"cprestore", s_cprestore, 0},
1431   {"cpreturn", s_cpreturn, 0},
1432   {"dtprelword", s_dtprelword, 0},
1433   {"dtpreldword", s_dtpreldword, 0},
1434   {"tprelword", s_tprelword, 0},
1435   {"tpreldword", s_tpreldword, 0},
1436   {"gpvalue", s_gpvalue, 0},
1437   {"gpword", s_gpword, 0},
1438   {"gpdword", s_gpdword, 0},
1439   {"cpadd", s_cpadd, 0},
1440   {"insn", s_insn, 0},
1441 
1442   /* Relatively generic pseudo-ops that happen to be used on MIPS
1443      chips.  */
1444   {"asciiz", stringer, 8 + 1},
1445   {"bss", s_change_sec, 'b'},
1446   {"err", s_err, 0},
1447   {"half", s_cons, 1},
1448   {"dword", s_cons, 3},
1449   {"weakext", s_mips_weakext, 0},
1450   {"origin", s_org, 0},
1451   {"repeat", s_rept, 0},
1452 
1453   /* For MIPS this is non-standard, but we define it for consistency.  */
1454   {"sbss", s_change_sec, 'B'},
1455 
1456   /* These pseudo-ops are defined in read.c, but must be overridden
1457      here for one reason or another.  */
1458   {"align", s_align, 0},
1459   {"byte", s_cons, 0},
1460   {"data", s_change_sec, 'd'},
1461   {"double", s_float_cons, 'd'},
1462   {"float", s_float_cons, 'f'},
1463   {"globl", s_mips_globl, 0},
1464   {"global", s_mips_globl, 0},
1465   {"hword", s_cons, 1},
1466   {"int", s_cons, 2},
1467   {"long", s_cons, 2},
1468   {"octa", s_cons, 4},
1469   {"quad", s_cons, 3},
1470   {"section", s_change_section, 0},
1471   {"short", s_cons, 1},
1472   {"single", s_float_cons, 'f'},
1473   {"stabn", s_mips_stab, 'n'},
1474   {"text", s_change_sec, 't'},
1475   {"word", s_cons, 2},
1476 
1477   { "extern", ecoff_directive_extern, 0},
1478 
1479   { NULL, NULL, 0 },
1480 };
1481 
1482 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1483 {
1484   /* These pseudo-ops should be defined by the object file format.
1485      However, a.out doesn't support them, so we have versions here.  */
1486   {"aent", s_mips_ent, 1},
1487   {"bgnb", s_ignore, 0},
1488   {"end", s_mips_end, 0},
1489   {"endb", s_ignore, 0},
1490   {"ent", s_mips_ent, 0},
1491   {"file", s_mips_file, 0},
1492   {"fmask", s_mips_mask, 'F'},
1493   {"frame", s_mips_frame, 0},
1494   {"loc", s_mips_loc, 0},
1495   {"mask", s_mips_mask, 'R'},
1496   {"verstamp", s_ignore, 0},
1497   { NULL, NULL, 0 },
1498 };
1499 
1500 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1501    purpose of the `.dc.a' internal pseudo-op.  */
1502 
1503 int
1504 mips_address_bytes (void)
1505 {
1506   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1507 }
1508 
1509 extern void pop_insert (const pseudo_typeS *);
1510 
1511 void
1512 mips_pop_insert (void)
1513 {
1514   pop_insert (mips_pseudo_table);
1515   if (! ECOFF_DEBUGGING)
1516     pop_insert (mips_nonecoff_pseudo_table);
1517 }
1518 
1519 /* Symbols labelling the current insn.  */
1520 
1521 struct insn_label_list
1522 {
1523   struct insn_label_list *next;
1524   symbolS *label;
1525 };
1526 
1527 static struct insn_label_list *free_insn_labels;
1528 #define label_list tc_segment_info_data.labels
1529 
1530 static void mips_clear_insn_labels (void);
1531 static void mips_mark_labels (void);
1532 static void mips_compressed_mark_labels (void);
1533 
1534 static inline void
1535 mips_clear_insn_labels (void)
1536 {
1537   register struct insn_label_list **pl;
1538   segment_info_type *si;
1539 
1540   if (now_seg)
1541     {
1542       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1543 	;
1544 
1545       si = seg_info (now_seg);
1546       *pl = si->label_list;
1547       si->label_list = NULL;
1548     }
1549 }
1550 
1551 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1552 
1553 static inline void
1554 mips_mark_labels (void)
1555 {
1556   if (HAVE_CODE_COMPRESSION)
1557     mips_compressed_mark_labels ();
1558 }
1559 
1560 static char *expr_end;
1561 
1562 /* Expressions which appear in instructions.  These are set by
1563    mips_ip.  */
1564 
1565 static expressionS imm_expr;
1566 static expressionS imm2_expr;
1567 static expressionS offset_expr;
1568 
1569 /* Relocs associated with imm_expr and offset_expr.  */
1570 
1571 static bfd_reloc_code_real_type imm_reloc[3]
1572   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1573 static bfd_reloc_code_real_type offset_reloc[3]
1574   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1575 
1576 /* This is set to the resulting size of the instruction to be produced
1577    by mips16_ip if an explicit extension is used or by mips_ip if an
1578    explicit size is supplied.  */
1579 
1580 static unsigned int forced_insn_length;
1581 
1582 #ifdef OBJ_ELF
1583 /* The pdr segment for per procedure frame/regmask info.  Not used for
1584    ECOFF debugging.  */
1585 
1586 static segT pdr_seg;
1587 #endif
1588 
1589 /* The default target format to use.  */
1590 
1591 #if defined (TE_FreeBSD)
1592 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1593 #elif defined (TE_TMIPS)
1594 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1595 #else
1596 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1597 #endif
1598 
1599 const char *
1600 mips_target_format (void)
1601 {
1602   switch (OUTPUT_FLAVOR)
1603     {
1604     case bfd_target_ecoff_flavour:
1605       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1606     case bfd_target_coff_flavour:
1607       return "pe-mips";
1608     case bfd_target_elf_flavour:
1609 #ifdef TE_VXWORKS
1610       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1611 	return (target_big_endian
1612 		? "elf32-bigmips-vxworks"
1613 		: "elf32-littlemips-vxworks");
1614 #endif
1615       return (target_big_endian
1616 	      ? (HAVE_64BIT_OBJECTS
1617 		 ? ELF_TARGET ("elf64-", "big")
1618 		 : (HAVE_NEWABI
1619 		    ? ELF_TARGET ("elf32-n", "big")
1620 		    : ELF_TARGET ("elf32-", "big")))
1621 	      : (HAVE_64BIT_OBJECTS
1622 		 ? ELF_TARGET ("elf64-", "little")
1623 		 : (HAVE_NEWABI
1624 		    ? ELF_TARGET ("elf32-n", "little")
1625 		    : ELF_TARGET ("elf32-", "little"))));
1626     default:
1627       abort ();
1628       return NULL;
1629     }
1630 }
1631 
1632 /* Return the length of a microMIPS instruction in bytes.  If bits of
1633    the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1634    Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1635    major opcode) will require further modifications to the opcode
1636    table.  */
1637 
1638 static inline unsigned int
1639 micromips_insn_length (const struct mips_opcode *mo)
1640 {
1641   return (mo->mask >> 16) == 0 ? 2 : 4;
1642 }
1643 
1644 /* Return the length of instruction INSN.  */
1645 
1646 static inline unsigned int
1647 insn_length (const struct mips_cl_insn *insn)
1648 {
1649   if (mips_opts.micromips)
1650     return micromips_insn_length (insn->insn_mo);
1651   else if (mips_opts.mips16)
1652     return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1653   else
1654     return 4;
1655 }
1656 
1657 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
1658 
1659 static void
1660 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1661 {
1662   size_t i;
1663 
1664   insn->insn_mo = mo;
1665   insn->use_extend = FALSE;
1666   insn->extend = 0;
1667   insn->insn_opcode = mo->match;
1668   insn->frag = NULL;
1669   insn->where = 0;
1670   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1671     insn->fixp[i] = NULL;
1672   insn->fixed_p = (mips_opts.noreorder > 0);
1673   insn->noreorder_p = (mips_opts.noreorder > 0);
1674   insn->mips16_absolute_jump_p = 0;
1675   insn->complete_p = 0;
1676 }
1677 
1678 /* Record the current MIPS16/microMIPS mode in now_seg.  */
1679 
1680 static void
1681 mips_record_compressed_mode (void)
1682 {
1683   segment_info_type *si;
1684 
1685   si = seg_info (now_seg);
1686   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1687     si->tc_segment_info_data.mips16 = mips_opts.mips16;
1688   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1689     si->tc_segment_info_data.micromips = mips_opts.micromips;
1690 }
1691 
1692 /* Install INSN at the location specified by its "frag" and "where" fields.  */
1693 
1694 static void
1695 install_insn (const struct mips_cl_insn *insn)
1696 {
1697   char *f = insn->frag->fr_literal + insn->where;
1698   if (!HAVE_CODE_COMPRESSION)
1699     md_number_to_chars (f, insn->insn_opcode, 4);
1700   else if (mips_opts.micromips)
1701     {
1702       unsigned int length = insn_length (insn);
1703       if (length == 2)
1704 	md_number_to_chars (f, insn->insn_opcode, 2);
1705       else if (length == 4)
1706 	{
1707 	  md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1708 	  f += 2;
1709 	  md_number_to_chars (f, insn->insn_opcode & 0xffff, 2);
1710 	}
1711       else
1712 	as_bad (_("48-bit microMIPS instructions are not supported"));
1713     }
1714   else if (insn->mips16_absolute_jump_p)
1715     {
1716       md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1717       md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1718     }
1719   else
1720     {
1721       if (insn->use_extend)
1722 	{
1723 	  md_number_to_chars (f, 0xf000 | insn->extend, 2);
1724 	  f += 2;
1725 	}
1726       md_number_to_chars (f, insn->insn_opcode, 2);
1727     }
1728   mips_record_compressed_mode ();
1729 }
1730 
1731 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
1732    and install the opcode in the new location.  */
1733 
1734 static void
1735 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1736 {
1737   size_t i;
1738 
1739   insn->frag = frag;
1740   insn->where = where;
1741   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1742     if (insn->fixp[i] != NULL)
1743       {
1744 	insn->fixp[i]->fx_frag = frag;
1745 	insn->fixp[i]->fx_where = where;
1746       }
1747   install_insn (insn);
1748 }
1749 
1750 /* Add INSN to the end of the output.  */
1751 
1752 static void
1753 add_fixed_insn (struct mips_cl_insn *insn)
1754 {
1755   char *f = frag_more (insn_length (insn));
1756   move_insn (insn, frag_now, f - frag_now->fr_literal);
1757 }
1758 
1759 /* Start a variant frag and move INSN to the start of the variant part,
1760    marking it as fixed.  The other arguments are as for frag_var.  */
1761 
1762 static void
1763 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1764 		  relax_substateT subtype, symbolS *symbol, offsetT offset)
1765 {
1766   frag_grow (max_chars);
1767   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1768   insn->fixed_p = 1;
1769   frag_var (rs_machine_dependent, max_chars, var,
1770 	    subtype, symbol, offset, NULL);
1771 }
1772 
1773 /* Insert N copies of INSN into the history buffer, starting at
1774    position FIRST.  Neither FIRST nor N need to be clipped.  */
1775 
1776 static void
1777 insert_into_history (unsigned int first, unsigned int n,
1778 		     const struct mips_cl_insn *insn)
1779 {
1780   if (mips_relax.sequence != 2)
1781     {
1782       unsigned int i;
1783 
1784       for (i = ARRAY_SIZE (history); i-- > first;)
1785 	if (i >= first + n)
1786 	  history[i] = history[i - n];
1787 	else
1788 	  history[i] = *insn;
1789     }
1790 }
1791 
1792 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
1793    the idea is to make it obvious at a glance that each errata is
1794    included.  */
1795 
1796 static void
1797 init_vr4120_conflicts (void)
1798 {
1799 #define CONFLICT(FIRST, SECOND) \
1800     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1801 
1802   /* Errata 21 - [D]DIV[U] after [D]MACC */
1803   CONFLICT (MACC, DIV);
1804   CONFLICT (DMACC, DIV);
1805 
1806   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
1807   CONFLICT (DMULT, DMULT);
1808   CONFLICT (DMULT, DMACC);
1809   CONFLICT (DMACC, DMULT);
1810   CONFLICT (DMACC, DMACC);
1811 
1812   /* Errata 24 - MT{LO,HI} after [D]MACC */
1813   CONFLICT (MACC, MTHILO);
1814   CONFLICT (DMACC, MTHILO);
1815 
1816   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1817      instruction is executed immediately after a MACC or DMACC
1818      instruction, the result of [either instruction] is incorrect."  */
1819   CONFLICT (MACC, MULT);
1820   CONFLICT (MACC, DMULT);
1821   CONFLICT (DMACC, MULT);
1822   CONFLICT (DMACC, DMULT);
1823 
1824   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1825      executed immediately after a DMULT, DMULTU, DIV, DIVU,
1826      DDIV or DDIVU instruction, the result of the MACC or
1827      DMACC instruction is incorrect.".  */
1828   CONFLICT (DMULT, MACC);
1829   CONFLICT (DMULT, DMACC);
1830   CONFLICT (DIV, MACC);
1831   CONFLICT (DIV, DMACC);
1832 
1833 #undef CONFLICT
1834 }
1835 
1836 struct regname {
1837   const char *name;
1838   unsigned int num;
1839 };
1840 
1841 #define RTYPE_MASK	0x1ff00
1842 #define RTYPE_NUM	0x00100
1843 #define RTYPE_FPU	0x00200
1844 #define RTYPE_FCC	0x00400
1845 #define RTYPE_VEC	0x00800
1846 #define RTYPE_GP	0x01000
1847 #define RTYPE_CP0	0x02000
1848 #define RTYPE_PC	0x04000
1849 #define RTYPE_ACC	0x08000
1850 #define RTYPE_CCC	0x10000
1851 #define RNUM_MASK	0x000ff
1852 #define RWARN		0x80000
1853 
1854 #define GENERIC_REGISTER_NUMBERS \
1855     {"$0",	RTYPE_NUM | 0},  \
1856     {"$1",	RTYPE_NUM | 1},  \
1857     {"$2",	RTYPE_NUM | 2},  \
1858     {"$3",	RTYPE_NUM | 3},  \
1859     {"$4",	RTYPE_NUM | 4},  \
1860     {"$5",	RTYPE_NUM | 5},  \
1861     {"$6",	RTYPE_NUM | 6},  \
1862     {"$7",	RTYPE_NUM | 7},  \
1863     {"$8",	RTYPE_NUM | 8},  \
1864     {"$9",	RTYPE_NUM | 9},  \
1865     {"$10",	RTYPE_NUM | 10}, \
1866     {"$11",	RTYPE_NUM | 11}, \
1867     {"$12",	RTYPE_NUM | 12}, \
1868     {"$13",	RTYPE_NUM | 13}, \
1869     {"$14",	RTYPE_NUM | 14}, \
1870     {"$15",	RTYPE_NUM | 15}, \
1871     {"$16",	RTYPE_NUM | 16}, \
1872     {"$17",	RTYPE_NUM | 17}, \
1873     {"$18",	RTYPE_NUM | 18}, \
1874     {"$19",	RTYPE_NUM | 19}, \
1875     {"$20",	RTYPE_NUM | 20}, \
1876     {"$21",	RTYPE_NUM | 21}, \
1877     {"$22",	RTYPE_NUM | 22}, \
1878     {"$23",	RTYPE_NUM | 23}, \
1879     {"$24",	RTYPE_NUM | 24}, \
1880     {"$25",	RTYPE_NUM | 25}, \
1881     {"$26",	RTYPE_NUM | 26}, \
1882     {"$27",	RTYPE_NUM | 27}, \
1883     {"$28",	RTYPE_NUM | 28}, \
1884     {"$29",	RTYPE_NUM | 29}, \
1885     {"$30",	RTYPE_NUM | 30}, \
1886     {"$31",	RTYPE_NUM | 31}
1887 
1888 #define FPU_REGISTER_NAMES       \
1889     {"$f0",	RTYPE_FPU | 0},  \
1890     {"$f1",	RTYPE_FPU | 1},  \
1891     {"$f2",	RTYPE_FPU | 2},  \
1892     {"$f3",	RTYPE_FPU | 3},  \
1893     {"$f4",	RTYPE_FPU | 4},  \
1894     {"$f5",	RTYPE_FPU | 5},  \
1895     {"$f6",	RTYPE_FPU | 6},  \
1896     {"$f7",	RTYPE_FPU | 7},  \
1897     {"$f8",	RTYPE_FPU | 8},  \
1898     {"$f9",	RTYPE_FPU | 9},  \
1899     {"$f10",	RTYPE_FPU | 10}, \
1900     {"$f11",	RTYPE_FPU | 11}, \
1901     {"$f12",	RTYPE_FPU | 12}, \
1902     {"$f13",	RTYPE_FPU | 13}, \
1903     {"$f14",	RTYPE_FPU | 14}, \
1904     {"$f15",	RTYPE_FPU | 15}, \
1905     {"$f16",	RTYPE_FPU | 16}, \
1906     {"$f17",	RTYPE_FPU | 17}, \
1907     {"$f18",	RTYPE_FPU | 18}, \
1908     {"$f19",	RTYPE_FPU | 19}, \
1909     {"$f20",	RTYPE_FPU | 20}, \
1910     {"$f21",	RTYPE_FPU | 21}, \
1911     {"$f22",	RTYPE_FPU | 22}, \
1912     {"$f23",	RTYPE_FPU | 23}, \
1913     {"$f24",	RTYPE_FPU | 24}, \
1914     {"$f25",	RTYPE_FPU | 25}, \
1915     {"$f26",	RTYPE_FPU | 26}, \
1916     {"$f27",	RTYPE_FPU | 27}, \
1917     {"$f28",	RTYPE_FPU | 28}, \
1918     {"$f29",	RTYPE_FPU | 29}, \
1919     {"$f30",	RTYPE_FPU | 30}, \
1920     {"$f31",	RTYPE_FPU | 31}
1921 
1922 #define FPU_CONDITION_CODE_NAMES \
1923     {"$fcc0",	RTYPE_FCC | 0},  \
1924     {"$fcc1",	RTYPE_FCC | 1},  \
1925     {"$fcc2",	RTYPE_FCC | 2},  \
1926     {"$fcc3",	RTYPE_FCC | 3},  \
1927     {"$fcc4",	RTYPE_FCC | 4},  \
1928     {"$fcc5",	RTYPE_FCC | 5},  \
1929     {"$fcc6",	RTYPE_FCC | 6},  \
1930     {"$fcc7",	RTYPE_FCC | 7}
1931 
1932 #define COPROC_CONDITION_CODE_NAMES         \
1933     {"$cc0",	RTYPE_FCC | RTYPE_CCC | 0}, \
1934     {"$cc1",	RTYPE_FCC | RTYPE_CCC | 1}, \
1935     {"$cc2",	RTYPE_FCC | RTYPE_CCC | 2}, \
1936     {"$cc3",	RTYPE_FCC | RTYPE_CCC | 3}, \
1937     {"$cc4",	RTYPE_FCC | RTYPE_CCC | 4}, \
1938     {"$cc5",	RTYPE_FCC | RTYPE_CCC | 5}, \
1939     {"$cc6",	RTYPE_FCC | RTYPE_CCC | 6}, \
1940     {"$cc7",	RTYPE_FCC | RTYPE_CCC | 7}
1941 
1942 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1943     {"$a4",	RTYPE_GP | 8},  \
1944     {"$a5",	RTYPE_GP | 9},  \
1945     {"$a6",	RTYPE_GP | 10}, \
1946     {"$a7",	RTYPE_GP | 11}, \
1947     {"$ta0",	RTYPE_GP | 8},  /* alias for $a4 */ \
1948     {"$ta1",	RTYPE_GP | 9},  /* alias for $a5 */ \
1949     {"$ta2",	RTYPE_GP | 10}, /* alias for $a6 */ \
1950     {"$ta3",	RTYPE_GP | 11}, /* alias for $a7 */ \
1951     {"$t0",	RTYPE_GP | 12}, \
1952     {"$t1",	RTYPE_GP | 13}, \
1953     {"$t2",	RTYPE_GP | 14}, \
1954     {"$t3",	RTYPE_GP | 15}
1955 
1956 #define O32_SYMBOLIC_REGISTER_NAMES \
1957     {"$t0",	RTYPE_GP | 8},  \
1958     {"$t1",	RTYPE_GP | 9},  \
1959     {"$t2",	RTYPE_GP | 10}, \
1960     {"$t3",	RTYPE_GP | 11}, \
1961     {"$t4",	RTYPE_GP | 12}, \
1962     {"$t5",	RTYPE_GP | 13}, \
1963     {"$t6",	RTYPE_GP | 14}, \
1964     {"$t7",	RTYPE_GP | 15}, \
1965     {"$ta0",	RTYPE_GP | 12}, /* alias for $t4 */ \
1966     {"$ta1",	RTYPE_GP | 13}, /* alias for $t5 */ \
1967     {"$ta2",	RTYPE_GP | 14}, /* alias for $t6 */ \
1968     {"$ta3",	RTYPE_GP | 15}  /* alias for $t7 */
1969 
1970 /* Remaining symbolic register names */
1971 #define SYMBOLIC_REGISTER_NAMES \
1972     {"$zero",	RTYPE_GP | 0},  \
1973     {"$at",	RTYPE_GP | 1},  \
1974     {"$AT",	RTYPE_GP | 1},  \
1975     {"$v0",	RTYPE_GP | 2},  \
1976     {"$v1",	RTYPE_GP | 3},  \
1977     {"$a0",	RTYPE_GP | 4},  \
1978     {"$a1",	RTYPE_GP | 5},  \
1979     {"$a2",	RTYPE_GP | 6},  \
1980     {"$a3",	RTYPE_GP | 7},  \
1981     {"$s0",	RTYPE_GP | 16}, \
1982     {"$s1",	RTYPE_GP | 17}, \
1983     {"$s2",	RTYPE_GP | 18}, \
1984     {"$s3",	RTYPE_GP | 19}, \
1985     {"$s4",	RTYPE_GP | 20}, \
1986     {"$s5",	RTYPE_GP | 21}, \
1987     {"$s6",	RTYPE_GP | 22}, \
1988     {"$s7",	RTYPE_GP | 23}, \
1989     {"$t8",	RTYPE_GP | 24}, \
1990     {"$t9",	RTYPE_GP | 25}, \
1991     {"$k0",	RTYPE_GP | 26}, \
1992     {"$kt0",	RTYPE_GP | 26}, \
1993     {"$k1",	RTYPE_GP | 27}, \
1994     {"$kt1",	RTYPE_GP | 27}, \
1995     {"$gp",	RTYPE_GP | 28}, \
1996     {"$sp",	RTYPE_GP | 29}, \
1997     {"$s8",	RTYPE_GP | 30}, \
1998     {"$fp",	RTYPE_GP | 30}, \
1999     {"$ra",	RTYPE_GP | 31}
2000 
2001 #define MIPS16_SPECIAL_REGISTER_NAMES \
2002     {"$pc",	RTYPE_PC | 0}
2003 
2004 #define MDMX_VECTOR_REGISTER_NAMES \
2005     /* {"$v0",	RTYPE_VEC | 0},  clash with REG 2 above */ \
2006     /* {"$v1",	RTYPE_VEC | 1},  clash with REG 3 above */ \
2007     {"$v2",	RTYPE_VEC | 2},  \
2008     {"$v3",	RTYPE_VEC | 3},  \
2009     {"$v4",	RTYPE_VEC | 4},  \
2010     {"$v5",	RTYPE_VEC | 5},  \
2011     {"$v6",	RTYPE_VEC | 6},  \
2012     {"$v7",	RTYPE_VEC | 7},  \
2013     {"$v8",	RTYPE_VEC | 8},  \
2014     {"$v9",	RTYPE_VEC | 9},  \
2015     {"$v10",	RTYPE_VEC | 10}, \
2016     {"$v11",	RTYPE_VEC | 11}, \
2017     {"$v12",	RTYPE_VEC | 12}, \
2018     {"$v13",	RTYPE_VEC | 13}, \
2019     {"$v14",	RTYPE_VEC | 14}, \
2020     {"$v15",	RTYPE_VEC | 15}, \
2021     {"$v16",	RTYPE_VEC | 16}, \
2022     {"$v17",	RTYPE_VEC | 17}, \
2023     {"$v18",	RTYPE_VEC | 18}, \
2024     {"$v19",	RTYPE_VEC | 19}, \
2025     {"$v20",	RTYPE_VEC | 20}, \
2026     {"$v21",	RTYPE_VEC | 21}, \
2027     {"$v22",	RTYPE_VEC | 22}, \
2028     {"$v23",	RTYPE_VEC | 23}, \
2029     {"$v24",	RTYPE_VEC | 24}, \
2030     {"$v25",	RTYPE_VEC | 25}, \
2031     {"$v26",	RTYPE_VEC | 26}, \
2032     {"$v27",	RTYPE_VEC | 27}, \
2033     {"$v28",	RTYPE_VEC | 28}, \
2034     {"$v29",	RTYPE_VEC | 29}, \
2035     {"$v30",	RTYPE_VEC | 30}, \
2036     {"$v31",	RTYPE_VEC | 31}
2037 
2038 #define MIPS_DSP_ACCUMULATOR_NAMES \
2039     {"$ac0",	RTYPE_ACC | 0}, \
2040     {"$ac1",	RTYPE_ACC | 1}, \
2041     {"$ac2",	RTYPE_ACC | 2}, \
2042     {"$ac3",	RTYPE_ACC | 3}
2043 
2044 static const struct regname reg_names[] = {
2045   GENERIC_REGISTER_NUMBERS,
2046   FPU_REGISTER_NAMES,
2047   FPU_CONDITION_CODE_NAMES,
2048   COPROC_CONDITION_CODE_NAMES,
2049 
2050   /* The $txx registers depends on the abi,
2051      these will be added later into the symbol table from
2052      one of the tables below once mips_abi is set after
2053      parsing of arguments from the command line. */
2054   SYMBOLIC_REGISTER_NAMES,
2055 
2056   MIPS16_SPECIAL_REGISTER_NAMES,
2057   MDMX_VECTOR_REGISTER_NAMES,
2058   MIPS_DSP_ACCUMULATOR_NAMES,
2059   {0, 0}
2060 };
2061 
2062 static const struct regname reg_names_o32[] = {
2063   O32_SYMBOLIC_REGISTER_NAMES,
2064   {0, 0}
2065 };
2066 
2067 static const struct regname reg_names_n32n64[] = {
2068   N32N64_SYMBOLIC_REGISTER_NAMES,
2069   {0, 0}
2070 };
2071 
2072 /* Check if S points at a valid register specifier according to TYPES.
2073    If so, then return 1, advance S to consume the specifier and store
2074    the register's number in REGNOP, otherwise return 0.  */
2075 
2076 static int
2077 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2078 {
2079   symbolS *symbolP;
2080   char *e;
2081   char save_c;
2082   int reg = -1;
2083 
2084   /* Find end of name.  */
2085   e = *s;
2086   if (is_name_beginner (*e))
2087     ++e;
2088   while (is_part_of_name (*e))
2089     ++e;
2090 
2091   /* Terminate name.  */
2092   save_c = *e;
2093   *e = '\0';
2094 
2095   /* Look for a register symbol.  */
2096   if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2097     {
2098       int r = S_GET_VALUE (symbolP);
2099       if (r & types)
2100 	reg = r & RNUM_MASK;
2101       else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2102 	/* Convert GP reg $v0/1 to MDMX reg $v0/1!  */
2103 	reg = (r & RNUM_MASK) - 2;
2104     }
2105   /* Else see if this is a register defined in an itbl entry.  */
2106   else if ((types & RTYPE_GP) && itbl_have_entries)
2107     {
2108       char *n = *s;
2109       unsigned long r;
2110 
2111       if (*n == '$')
2112 	++n;
2113       if (itbl_get_reg_val (n, &r))
2114 	reg = r & RNUM_MASK;
2115     }
2116 
2117   /* Advance to next token if a register was recognised.  */
2118   if (reg >= 0)
2119     *s = e;
2120   else if (types & RWARN)
2121     as_warn (_("Unrecognized register name `%s'"), *s);
2122 
2123   *e = save_c;
2124   if (regnop)
2125     *regnop = reg;
2126   return reg >= 0;
2127 }
2128 
2129 /* Check if S points at a valid register list according to TYPES.
2130    If so, then return 1, advance S to consume the list and store
2131    the registers present on the list as a bitmask of ones in REGLISTP,
2132    otherwise return 0.  A valid list comprises a comma-separated
2133    enumeration of valid single registers and/or dash-separated
2134    contiguous register ranges as determined by their numbers.
2135 
2136    As a special exception if one of s0-s7 registers is specified as
2137    the range's lower delimiter and s8 (fp) is its upper one, then no
2138    registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2139    are selected; they have to be listed separately if needed.  */
2140 
2141 static int
2142 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2143 {
2144   unsigned int reglist = 0;
2145   unsigned int lastregno;
2146   bfd_boolean ok = TRUE;
2147   unsigned int regmask;
2148   char *s_endlist = *s;
2149   char *s_reset = *s;
2150   unsigned int regno;
2151 
2152   while (reg_lookup (s, types, &regno))
2153     {
2154       lastregno = regno;
2155       if (**s == '-')
2156 	{
2157 	  (*s)++;
2158 	  ok = reg_lookup (s, types, &lastregno);
2159 	  if (ok && lastregno < regno)
2160 	    ok = FALSE;
2161 	  if (!ok)
2162 	    break;
2163 	}
2164 
2165       if (lastregno == FP && regno >= S0 && regno <= S7)
2166 	{
2167 	  lastregno = S7;
2168 	  reglist |= 1 << FP;
2169 	}
2170       regmask = 1 << lastregno;
2171       regmask = (regmask << 1) - 1;
2172       regmask ^= (1 << regno) - 1;
2173       reglist |= regmask;
2174 
2175       s_endlist = *s;
2176       if (**s != ',')
2177 	break;
2178       (*s)++;
2179     }
2180 
2181   if (ok)
2182     *s = s_endlist;
2183   else
2184     *s = s_reset;
2185   if (reglistp)
2186     *reglistp = reglist;
2187   return ok && reglist != 0;
2188 }
2189 
2190 /* Return TRUE if opcode MO is valid on the currently selected ISA and
2191    architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
2192 
2193 static bfd_boolean
2194 is_opcode_valid (const struct mips_opcode *mo)
2195 {
2196   int isa = mips_opts.isa;
2197   int fp_s, fp_d;
2198 
2199   if (mips_opts.ase_mdmx)
2200     isa |= INSN_MDMX;
2201   if (mips_opts.ase_dsp)
2202     isa |= INSN_DSP;
2203   if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2204     isa |= INSN_DSP64;
2205   if (mips_opts.ase_dspr2)
2206     isa |= INSN_DSPR2;
2207   if (mips_opts.ase_mt)
2208     isa |= INSN_MT;
2209   if (mips_opts.ase_mips3d)
2210     isa |= INSN_MIPS3D;
2211   if (mips_opts.ase_smartmips)
2212     isa |= INSN_SMARTMIPS;
2213   if (mips_opts.ase_mcu)
2214     isa |= INSN_MCU;
2215 
2216   if (!opcode_is_member (mo, isa, mips_opts.arch))
2217     return FALSE;
2218 
2219   /* Check whether the instruction or macro requires single-precision or
2220      double-precision floating-point support.  Note that this information is
2221      stored differently in the opcode table for insns and macros.  */
2222   if (mo->pinfo == INSN_MACRO)
2223     {
2224       fp_s = mo->pinfo2 & INSN2_M_FP_S;
2225       fp_d = mo->pinfo2 & INSN2_M_FP_D;
2226     }
2227   else
2228     {
2229       fp_s = mo->pinfo & FP_S;
2230       fp_d = mo->pinfo & FP_D;
2231     }
2232 
2233   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2234     return FALSE;
2235 
2236   if (fp_s && mips_opts.soft_float)
2237     return FALSE;
2238 
2239   return TRUE;
2240 }
2241 
2242 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2243    selected ISA and architecture.  */
2244 
2245 static bfd_boolean
2246 is_opcode_valid_16 (const struct mips_opcode *mo)
2247 {
2248   return opcode_is_member (mo, mips_opts.isa, mips_opts.arch);
2249 }
2250 
2251 /* Return TRUE if the size of the microMIPS opcode MO matches one
2252    explicitly requested.  Always TRUE in the standard MIPS mode.  */
2253 
2254 static bfd_boolean
2255 is_size_valid (const struct mips_opcode *mo)
2256 {
2257   if (!mips_opts.micromips)
2258     return TRUE;
2259 
2260   if (!forced_insn_length)
2261     return TRUE;
2262   if (mo->pinfo == INSN_MACRO)
2263     return FALSE;
2264   return forced_insn_length == micromips_insn_length (mo);
2265 }
2266 
2267 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2268    of the preceding instruction.  Always TRUE in the standard MIPS mode.  */
2269 
2270 static bfd_boolean
2271 is_delay_slot_valid (const struct mips_opcode *mo)
2272 {
2273   if (!mips_opts.micromips)
2274     return TRUE;
2275 
2276   if (mo->pinfo == INSN_MACRO)
2277     return TRUE;
2278   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2279       && micromips_insn_length (mo) != 4)
2280     return FALSE;
2281   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2282       && micromips_insn_length (mo) != 2)
2283     return FALSE;
2284 
2285   return TRUE;
2286 }
2287 
2288 /* This function is called once, at assembler startup time.  It should set up
2289    all the tables, etc. that the MD part of the assembler will need.  */
2290 
2291 void
2292 md_begin (void)
2293 {
2294   const char *retval = NULL;
2295   int i = 0;
2296   int broken = 0;
2297 
2298   if (mips_pic != NO_PIC)
2299     {
2300       if (g_switch_seen && g_switch_value != 0)
2301 	as_bad (_("-G may not be used in position-independent code"));
2302       g_switch_value = 0;
2303     }
2304 
2305   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2306     as_warn (_("Could not set architecture and machine"));
2307 
2308   op_hash = hash_new ();
2309 
2310   for (i = 0; i < NUMOPCODES;)
2311     {
2312       const char *name = mips_opcodes[i].name;
2313 
2314       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2315       if (retval != NULL)
2316 	{
2317 	  fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2318 		   mips_opcodes[i].name, retval);
2319 	  /* Probably a memory allocation problem?  Give up now.  */
2320 	  as_fatal (_("Broken assembler.  No assembly attempted."));
2321 	}
2322       do
2323 	{
2324 	  if (mips_opcodes[i].pinfo != INSN_MACRO)
2325 	    {
2326 	      if (!validate_mips_insn (&mips_opcodes[i]))
2327 		broken = 1;
2328 	      if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2329 		{
2330 		  create_insn (&nop_insn, mips_opcodes + i);
2331 		  if (mips_fix_loongson2f_nop)
2332 		    nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2333 		  nop_insn.fixed_p = 1;
2334 		}
2335 	    }
2336 	  ++i;
2337 	}
2338       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2339     }
2340 
2341   mips16_op_hash = hash_new ();
2342 
2343   i = 0;
2344   while (i < bfd_mips16_num_opcodes)
2345     {
2346       const char *name = mips16_opcodes[i].name;
2347 
2348       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2349       if (retval != NULL)
2350 	as_fatal (_("internal: can't hash `%s': %s"),
2351 		  mips16_opcodes[i].name, retval);
2352       do
2353 	{
2354 	  if (mips16_opcodes[i].pinfo != INSN_MACRO
2355 	      && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2356 		  != mips16_opcodes[i].match))
2357 	    {
2358 	      fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2359 		       mips16_opcodes[i].name, mips16_opcodes[i].args);
2360 	      broken = 1;
2361 	    }
2362 	  if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2363 	    {
2364 	      create_insn (&mips16_nop_insn, mips16_opcodes + i);
2365 	      mips16_nop_insn.fixed_p = 1;
2366 	    }
2367 	  ++i;
2368 	}
2369       while (i < bfd_mips16_num_opcodes
2370 	     && strcmp (mips16_opcodes[i].name, name) == 0);
2371     }
2372 
2373   micromips_op_hash = hash_new ();
2374 
2375   i = 0;
2376   while (i < bfd_micromips_num_opcodes)
2377     {
2378       const char *name = micromips_opcodes[i].name;
2379 
2380       retval = hash_insert (micromips_op_hash, name,
2381 			    (void *) &micromips_opcodes[i]);
2382       if (retval != NULL)
2383 	as_fatal (_("internal: can't hash `%s': %s"),
2384 		  micromips_opcodes[i].name, retval);
2385       do
2386         if (micromips_opcodes[i].pinfo != INSN_MACRO)
2387           {
2388             struct mips_cl_insn *micromips_nop_insn;
2389 
2390             if (!validate_micromips_insn (&micromips_opcodes[i]))
2391               broken = 1;
2392 
2393 	    if (micromips_insn_length (micromips_opcodes + i) == 2)
2394 	      micromips_nop_insn = &micromips_nop16_insn;
2395 	    else if (micromips_insn_length (micromips_opcodes + i) == 4)
2396 	      micromips_nop_insn = &micromips_nop32_insn;
2397 	    else
2398 	      continue;
2399 
2400             if (micromips_nop_insn->insn_mo == NULL
2401 		&& strcmp (name, "nop") == 0)
2402               {
2403                 create_insn (micromips_nop_insn, micromips_opcodes + i);
2404                 micromips_nop_insn->fixed_p = 1;
2405               }
2406           }
2407       while (++i < bfd_micromips_num_opcodes
2408 	     && strcmp (micromips_opcodes[i].name, name) == 0);
2409     }
2410 
2411   if (broken)
2412     as_fatal (_("Broken assembler.  No assembly attempted."));
2413 
2414   /* We add all the general register names to the symbol table.  This
2415      helps us detect invalid uses of them.  */
2416   for (i = 0; reg_names[i].name; i++)
2417     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2418 				     reg_names[i].num, /* & RNUM_MASK, */
2419 				     &zero_address_frag));
2420   if (HAVE_NEWABI)
2421     for (i = 0; reg_names_n32n64[i].name; i++)
2422       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2423 				       reg_names_n32n64[i].num, /* & RNUM_MASK, */
2424 				       &zero_address_frag));
2425   else
2426     for (i = 0; reg_names_o32[i].name; i++)
2427       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2428 				       reg_names_o32[i].num, /* & RNUM_MASK, */
2429 				       &zero_address_frag));
2430 
2431   mips_no_prev_insn ();
2432 
2433   mips_gprmask = 0;
2434   mips_cprmask[0] = 0;
2435   mips_cprmask[1] = 0;
2436   mips_cprmask[2] = 0;
2437   mips_cprmask[3] = 0;
2438 
2439   /* set the default alignment for the text section (2**2) */
2440   record_alignment (text_section, 2);
2441 
2442   bfd_set_gp_size (stdoutput, g_switch_value);
2443 
2444 #ifdef OBJ_ELF
2445   if (IS_ELF)
2446     {
2447       /* On a native system other than VxWorks, sections must be aligned
2448 	 to 16 byte boundaries.  When configured for an embedded ELF
2449 	 target, we don't bother.  */
2450       if (strncmp (TARGET_OS, "elf", 3) != 0
2451 	  && strncmp (TARGET_OS, "vxworks", 7) != 0)
2452 	{
2453 	  (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2454 	  (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2455 	  (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2456 	}
2457 
2458       /* Create a .reginfo section for register masks and a .mdebug
2459 	 section for debugging information.  */
2460       {
2461 	segT seg;
2462 	subsegT subseg;
2463 	flagword flags;
2464 	segT sec;
2465 
2466 	seg = now_seg;
2467 	subseg = now_subseg;
2468 
2469 	/* The ABI says this section should be loaded so that the
2470 	   running program can access it.  However, we don't load it
2471 	   if we are configured for an embedded target */
2472 	flags = SEC_READONLY | SEC_DATA;
2473 	if (strncmp (TARGET_OS, "elf", 3) != 0)
2474 	  flags |= SEC_ALLOC | SEC_LOAD;
2475 
2476 	if (mips_abi != N64_ABI)
2477 	  {
2478 	    sec = subseg_new (".reginfo", (subsegT) 0);
2479 
2480 	    bfd_set_section_flags (stdoutput, sec, flags);
2481 	    bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2482 
2483 	    mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2484 	  }
2485 	else
2486 	  {
2487 	    /* The 64-bit ABI uses a .MIPS.options section rather than
2488                .reginfo section.  */
2489 	    sec = subseg_new (".MIPS.options", (subsegT) 0);
2490 	    bfd_set_section_flags (stdoutput, sec, flags);
2491 	    bfd_set_section_alignment (stdoutput, sec, 3);
2492 
2493 	    /* Set up the option header.  */
2494 	    {
2495 	      Elf_Internal_Options opthdr;
2496 	      char *f;
2497 
2498 	      opthdr.kind = ODK_REGINFO;
2499 	      opthdr.size = (sizeof (Elf_External_Options)
2500 			     + sizeof (Elf64_External_RegInfo));
2501 	      opthdr.section = 0;
2502 	      opthdr.info = 0;
2503 	      f = frag_more (sizeof (Elf_External_Options));
2504 	      bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2505 					     (Elf_External_Options *) f);
2506 
2507 	      mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2508 	    }
2509 	  }
2510 
2511 	if (ECOFF_DEBUGGING)
2512 	  {
2513 	    sec = subseg_new (".mdebug", (subsegT) 0);
2514 	    (void) bfd_set_section_flags (stdoutput, sec,
2515 					  SEC_HAS_CONTENTS | SEC_READONLY);
2516 	    (void) bfd_set_section_alignment (stdoutput, sec, 2);
2517 	  }
2518 	else if (mips_flag_pdr)
2519 	  {
2520 	    pdr_seg = subseg_new (".pdr", (subsegT) 0);
2521 	    (void) bfd_set_section_flags (stdoutput, pdr_seg,
2522 					  SEC_READONLY | SEC_RELOC
2523 					  | SEC_DEBUGGING);
2524 	    (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2525 	  }
2526 
2527 	subseg_set (seg, subseg);
2528       }
2529     }
2530 #endif /* OBJ_ELF */
2531 
2532   if (! ECOFF_DEBUGGING)
2533     md_obj_begin ();
2534 
2535   if (mips_fix_vr4120)
2536     init_vr4120_conflicts ();
2537 }
2538 
2539 void
2540 md_mips_end (void)
2541 {
2542   mips_emit_delays ();
2543   if (! ECOFF_DEBUGGING)
2544     md_obj_end ();
2545 }
2546 
2547 void
2548 md_assemble (char *str)
2549 {
2550   struct mips_cl_insn insn;
2551   bfd_reloc_code_real_type unused_reloc[3]
2552     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2553 
2554   imm_expr.X_op = O_absent;
2555   imm2_expr.X_op = O_absent;
2556   offset_expr.X_op = O_absent;
2557   imm_reloc[0] = BFD_RELOC_UNUSED;
2558   imm_reloc[1] = BFD_RELOC_UNUSED;
2559   imm_reloc[2] = BFD_RELOC_UNUSED;
2560   offset_reloc[0] = BFD_RELOC_UNUSED;
2561   offset_reloc[1] = BFD_RELOC_UNUSED;
2562   offset_reloc[2] = BFD_RELOC_UNUSED;
2563 
2564   if (mips_opts.mips16)
2565     mips16_ip (str, &insn);
2566   else
2567     {
2568       mips_ip (str, &insn);
2569       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2570 	    str, insn.insn_opcode));
2571     }
2572 
2573   if (insn_error)
2574     {
2575       as_bad ("%s `%s'", insn_error, str);
2576       return;
2577     }
2578 
2579   if (insn.insn_mo->pinfo == INSN_MACRO)
2580     {
2581       macro_start ();
2582       if (mips_opts.mips16)
2583 	mips16_macro (&insn);
2584       else
2585 	macro (&insn);
2586       macro_end ();
2587     }
2588   else
2589     {
2590       if (imm_expr.X_op != O_absent)
2591 	append_insn (&insn, &imm_expr, imm_reloc, FALSE);
2592       else if (offset_expr.X_op != O_absent)
2593 	append_insn (&insn, &offset_expr, offset_reloc, FALSE);
2594       else
2595 	append_insn (&insn, NULL, unused_reloc, FALSE);
2596     }
2597 }
2598 
2599 /* Convenience functions for abstracting away the differences between
2600    MIPS16 and non-MIPS16 relocations.  */
2601 
2602 static inline bfd_boolean
2603 mips16_reloc_p (bfd_reloc_code_real_type reloc)
2604 {
2605   switch (reloc)
2606     {
2607     case BFD_RELOC_MIPS16_JMP:
2608     case BFD_RELOC_MIPS16_GPREL:
2609     case BFD_RELOC_MIPS16_GOT16:
2610     case BFD_RELOC_MIPS16_CALL16:
2611     case BFD_RELOC_MIPS16_HI16_S:
2612     case BFD_RELOC_MIPS16_HI16:
2613     case BFD_RELOC_MIPS16_LO16:
2614       return TRUE;
2615 
2616     default:
2617       return FALSE;
2618     }
2619 }
2620 
2621 static inline bfd_boolean
2622 micromips_reloc_p (bfd_reloc_code_real_type reloc)
2623 {
2624   switch (reloc)
2625     {
2626     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2627     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2628     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2629     case BFD_RELOC_MICROMIPS_GPREL16:
2630     case BFD_RELOC_MICROMIPS_JMP:
2631     case BFD_RELOC_MICROMIPS_HI16:
2632     case BFD_RELOC_MICROMIPS_HI16_S:
2633     case BFD_RELOC_MICROMIPS_LO16:
2634     case BFD_RELOC_MICROMIPS_LITERAL:
2635     case BFD_RELOC_MICROMIPS_GOT16:
2636     case BFD_RELOC_MICROMIPS_CALL16:
2637     case BFD_RELOC_MICROMIPS_GOT_HI16:
2638     case BFD_RELOC_MICROMIPS_GOT_LO16:
2639     case BFD_RELOC_MICROMIPS_CALL_HI16:
2640     case BFD_RELOC_MICROMIPS_CALL_LO16:
2641     case BFD_RELOC_MICROMIPS_SUB:
2642     case BFD_RELOC_MICROMIPS_GOT_PAGE:
2643     case BFD_RELOC_MICROMIPS_GOT_OFST:
2644     case BFD_RELOC_MICROMIPS_GOT_DISP:
2645     case BFD_RELOC_MICROMIPS_HIGHEST:
2646     case BFD_RELOC_MICROMIPS_HIGHER:
2647     case BFD_RELOC_MICROMIPS_SCN_DISP:
2648     case BFD_RELOC_MICROMIPS_JALR:
2649       return TRUE;
2650 
2651     default:
2652       return FALSE;
2653     }
2654 }
2655 
2656 static inline bfd_boolean
2657 jmp_reloc_p (bfd_reloc_code_real_type reloc)
2658 {
2659   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2660 }
2661 
2662 static inline bfd_boolean
2663 got16_reloc_p (bfd_reloc_code_real_type reloc)
2664 {
2665   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
2666 	  || reloc == BFD_RELOC_MICROMIPS_GOT16);
2667 }
2668 
2669 static inline bfd_boolean
2670 hi16_reloc_p (bfd_reloc_code_real_type reloc)
2671 {
2672   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
2673 	  || reloc == BFD_RELOC_MICROMIPS_HI16_S);
2674 }
2675 
2676 static inline bfd_boolean
2677 lo16_reloc_p (bfd_reloc_code_real_type reloc)
2678 {
2679   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
2680 	  || reloc == BFD_RELOC_MICROMIPS_LO16);
2681 }
2682 
2683 static inline bfd_boolean
2684 jalr_reloc_p (bfd_reloc_code_real_type reloc)
2685 {
2686   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
2687 }
2688 
2689 /* Return true if the given relocation might need a matching %lo().
2690    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2691    need a matching %lo() when applied to local symbols.  */
2692 
2693 static inline bfd_boolean
2694 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2695 {
2696   return (HAVE_IN_PLACE_ADDENDS
2697 	  && (hi16_reloc_p (reloc)
2698 	      /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2699 		 all GOT16 relocations evaluate to "G".  */
2700 	      || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2701 }
2702 
2703 /* Return the type of %lo() reloc needed by RELOC, given that
2704    reloc_needs_lo_p.  */
2705 
2706 static inline bfd_reloc_code_real_type
2707 matching_lo_reloc (bfd_reloc_code_real_type reloc)
2708 {
2709   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2710 	  : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2711 	     : BFD_RELOC_LO16));
2712 }
2713 
2714 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2715    relocation.  */
2716 
2717 static inline bfd_boolean
2718 fixup_has_matching_lo_p (fixS *fixp)
2719 {
2720   return (fixp->fx_next != NULL
2721 	  && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
2722 	  && fixp->fx_addsy == fixp->fx_next->fx_addsy
2723 	  && fixp->fx_offset == fixp->fx_next->fx_offset);
2724 }
2725 
2726 /* This function returns true if modifying a register requires a
2727    delay.  */
2728 
2729 static int
2730 reg_needs_delay (unsigned int reg)
2731 {
2732   unsigned long prev_pinfo;
2733 
2734   prev_pinfo = history[0].insn_mo->pinfo;
2735   if (! mips_opts.noreorder
2736       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2737 	   && ! gpr_interlocks)
2738 	  || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2739 	      && ! cop_interlocks)))
2740     {
2741       /* A load from a coprocessor or from memory.  All load delays
2742 	 delay the use of general register rt for one instruction.  */
2743       /* Itbl support may require additional care here.  */
2744       know (prev_pinfo & INSN_WRITE_GPR_T);
2745       if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
2746 	return 1;
2747     }
2748 
2749   return 0;
2750 }
2751 
2752 /* Move all labels in LABELS to the current insertion point.  TEXT_P
2753    says whether the labels refer to text or data.  */
2754 
2755 static void
2756 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
2757 {
2758   struct insn_label_list *l;
2759   valueT val;
2760 
2761   for (l = labels; l != NULL; l = l->next)
2762     {
2763       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
2764       symbol_set_frag (l->label, frag_now);
2765       val = (valueT) frag_now_fix ();
2766       /* MIPS16/microMIPS text labels are stored as odd.  */
2767       if (text_p && HAVE_CODE_COMPRESSION)
2768 	++val;
2769       S_SET_VALUE (l->label, val);
2770     }
2771 }
2772 
2773 /* Move all labels in insn_labels to the current insertion point
2774    and treat them as text labels.  */
2775 
2776 static void
2777 mips_move_text_labels (void)
2778 {
2779   mips_move_labels (seg_info (now_seg)->label_list, TRUE);
2780 }
2781 
2782 static bfd_boolean
2783 s_is_linkonce (symbolS *sym, segT from_seg)
2784 {
2785   bfd_boolean linkonce = FALSE;
2786   segT symseg = S_GET_SEGMENT (sym);
2787 
2788   if (symseg != from_seg && !S_IS_LOCAL (sym))
2789     {
2790       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2791 	linkonce = TRUE;
2792 #ifdef OBJ_ELF
2793       /* The GNU toolchain uses an extension for ELF: a section
2794 	 beginning with the magic string .gnu.linkonce is a
2795 	 linkonce section.  */
2796       if (strncmp (segment_name (symseg), ".gnu.linkonce",
2797 		   sizeof ".gnu.linkonce" - 1) == 0)
2798 	linkonce = TRUE;
2799 #endif
2800     }
2801   return linkonce;
2802 }
2803 
2804 /* Mark instruction labels in MIPS16/microMIPS mode.  This permits the
2805    linker to handle them specially, such as generating jalx instructions
2806    when needed.  We also make them odd for the duration of the assembly,
2807    in order to generate the right sort of code.  We will make them even
2808    in the adjust_symtab routine, while leaving them marked.  This is
2809    convenient for the debugger and the disassembler.  The linker knows
2810    to make them odd again.  */
2811 
2812 static void
2813 mips_compressed_mark_labels (void)
2814 {
2815   segment_info_type *si = seg_info (now_seg);
2816   struct insn_label_list *l;
2817 
2818   gas_assert (HAVE_CODE_COMPRESSION);
2819 
2820   for (l = si->label_list; l != NULL; l = l->next)
2821    {
2822       symbolS *label = l->label;
2823 
2824 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2825       if (IS_ELF)
2826 	{
2827 	  if (mips_opts.mips16)
2828 	    S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2829 	  else
2830 	    S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
2831 	}
2832 #endif
2833       if ((S_GET_VALUE (label) & 1) == 0
2834 	/* Don't adjust the address if the label is global or weak, or
2835 	   in a link-once section, since we'll be emitting symbol reloc
2836 	   references to it which will be patched up by the linker, and
2837 	   the final value of the symbol may or may not be MIPS16/microMIPS.  */
2838 	  && ! S_IS_WEAK (label)
2839 	  && ! S_IS_EXTERNAL (label)
2840 	  && ! s_is_linkonce (label, now_seg))
2841 	S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2842     }
2843 }
2844 
2845 /* End the current frag.  Make it a variant frag and record the
2846    relaxation info.  */
2847 
2848 static void
2849 relax_close_frag (void)
2850 {
2851   mips_macro_warning.first_frag = frag_now;
2852   frag_var (rs_machine_dependent, 0, 0,
2853 	    RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2854 	    mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2855 
2856   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2857   mips_relax.first_fixup = 0;
2858 }
2859 
2860 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2861    See the comment above RELAX_ENCODE for more details.  */
2862 
2863 static void
2864 relax_start (symbolS *symbol)
2865 {
2866   gas_assert (mips_relax.sequence == 0);
2867   mips_relax.sequence = 1;
2868   mips_relax.symbol = symbol;
2869 }
2870 
2871 /* Start generating the second version of a relaxable sequence.
2872    See the comment above RELAX_ENCODE for more details.  */
2873 
2874 static void
2875 relax_switch (void)
2876 {
2877   gas_assert (mips_relax.sequence == 1);
2878   mips_relax.sequence = 2;
2879 }
2880 
2881 /* End the current relaxable sequence.  */
2882 
2883 static void
2884 relax_end (void)
2885 {
2886   gas_assert (mips_relax.sequence == 2);
2887   relax_close_frag ();
2888   mips_relax.sequence = 0;
2889 }
2890 
2891 /* Return true if IP is a delayed branch or jump.  */
2892 
2893 static inline bfd_boolean
2894 delayed_branch_p (const struct mips_cl_insn *ip)
2895 {
2896   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2897 				| INSN_COND_BRANCH_DELAY
2898 				| INSN_COND_BRANCH_LIKELY)) != 0;
2899 }
2900 
2901 /* Return true if IP is a compact branch or jump.  */
2902 
2903 static inline bfd_boolean
2904 compact_branch_p (const struct mips_cl_insn *ip)
2905 {
2906   if (mips_opts.mips16)
2907     return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2908 				  | MIPS16_INSN_COND_BRANCH)) != 0;
2909   else
2910     return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2911 				   | INSN2_COND_BRANCH)) != 0;
2912 }
2913 
2914 /* Return true if IP is an unconditional branch or jump.  */
2915 
2916 static inline bfd_boolean
2917 uncond_branch_p (const struct mips_cl_insn *ip)
2918 {
2919   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2920 	  || (mips_opts.mips16
2921 	      ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2922 	      : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2923 }
2924 
2925 /* Return true if IP is a branch-likely instruction.  */
2926 
2927 static inline bfd_boolean
2928 branch_likely_p (const struct mips_cl_insn *ip)
2929 {
2930   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2931 }
2932 
2933 /* Return the type of nop that should be used to fill the delay slot
2934    of delayed branch IP.  */
2935 
2936 static struct mips_cl_insn *
2937 get_delay_slot_nop (const struct mips_cl_insn *ip)
2938 {
2939   if (mips_opts.micromips
2940       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
2941     return &micromips_nop32_insn;
2942   return NOP_INSN;
2943 }
2944 
2945 /* Return the mask of core registers that IP reads or writes.  */
2946 
2947 static unsigned int
2948 gpr_mod_mask (const struct mips_cl_insn *ip)
2949 {
2950   unsigned long pinfo2;
2951   unsigned int mask;
2952 
2953   mask = 0;
2954   pinfo2 = ip->insn_mo->pinfo2;
2955   if (mips_opts.micromips)
2956     {
2957       if (pinfo2 & INSN2_MOD_GPR_MD)
2958 	mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
2959       if (pinfo2 & INSN2_MOD_GPR_MF)
2960 	mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
2961       if (pinfo2 & INSN2_MOD_SP)
2962 	mask |= 1 << SP;
2963     }
2964   return mask;
2965 }
2966 
2967 /* Return the mask of core registers that IP reads.  */
2968 
2969 static unsigned int
2970 gpr_read_mask (const struct mips_cl_insn *ip)
2971 {
2972   unsigned long pinfo, pinfo2;
2973   unsigned int mask;
2974 
2975   mask = gpr_mod_mask (ip);
2976   pinfo = ip->insn_mo->pinfo;
2977   pinfo2 = ip->insn_mo->pinfo2;
2978   if (mips_opts.mips16)
2979     {
2980       if (pinfo & MIPS16_INSN_READ_X)
2981 	mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
2982       if (pinfo & MIPS16_INSN_READ_Y)
2983 	mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
2984       if (pinfo & MIPS16_INSN_READ_T)
2985 	mask |= 1 << TREG;
2986       if (pinfo & MIPS16_INSN_READ_SP)
2987 	mask |= 1 << SP;
2988       if (pinfo & MIPS16_INSN_READ_31)
2989 	mask |= 1 << RA;
2990       if (pinfo & MIPS16_INSN_READ_Z)
2991 	mask |= 1 << (mips16_to_32_reg_map
2992 		      [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
2993       if (pinfo & MIPS16_INSN_READ_GPR_X)
2994 	mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2995     }
2996   else
2997     {
2998       if (pinfo2 & INSN2_READ_GPR_D)
2999 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3000       if (pinfo & INSN_READ_GPR_T)
3001 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3002       if (pinfo & INSN_READ_GPR_S)
3003 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3004       if (pinfo2 & INSN2_READ_GP)
3005 	mask |= 1 << GP;
3006       if (pinfo2 & INSN2_READ_GPR_31)
3007 	mask |= 1 << RA;
3008       if (pinfo2 & INSN2_READ_GPR_Z)
3009 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3010     }
3011   if (mips_opts.micromips)
3012     {
3013       if (pinfo2 & INSN2_READ_GPR_MC)
3014 	mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3015       if (pinfo2 & INSN2_READ_GPR_ME)
3016 	mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3017       if (pinfo2 & INSN2_READ_GPR_MG)
3018 	mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3019       if (pinfo2 & INSN2_READ_GPR_MJ)
3020 	mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3021       if (pinfo2 & INSN2_READ_GPR_MMN)
3022 	{
3023 	  mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3024 	  mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3025 	}
3026       if (pinfo2 & INSN2_READ_GPR_MP)
3027 	mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3028       if (pinfo2 & INSN2_READ_GPR_MQ)
3029 	mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3030     }
3031   /* Don't include register 0.  */
3032   return mask & ~1;
3033 }
3034 
3035 /* Return the mask of core registers that IP writes.  */
3036 
3037 static unsigned int
3038 gpr_write_mask (const struct mips_cl_insn *ip)
3039 {
3040   unsigned long pinfo, pinfo2;
3041   unsigned int mask;
3042 
3043   mask = gpr_mod_mask (ip);
3044   pinfo = ip->insn_mo->pinfo;
3045   pinfo2 = ip->insn_mo->pinfo2;
3046   if (mips_opts.mips16)
3047     {
3048       if (pinfo & MIPS16_INSN_WRITE_X)
3049 	mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3050       if (pinfo & MIPS16_INSN_WRITE_Y)
3051 	mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3052       if (pinfo & MIPS16_INSN_WRITE_Z)
3053 	mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3054       if (pinfo & MIPS16_INSN_WRITE_T)
3055 	mask |= 1 << TREG;
3056       if (pinfo & MIPS16_INSN_WRITE_SP)
3057 	mask |= 1 << SP;
3058       if (pinfo & MIPS16_INSN_WRITE_31)
3059 	mask |= 1 << RA;
3060       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3061 	mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3062     }
3063   else
3064     {
3065       if (pinfo & INSN_WRITE_GPR_D)
3066 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3067       if (pinfo & INSN_WRITE_GPR_T)
3068 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3069       if (pinfo & INSN_WRITE_GPR_S)
3070 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3071       if (pinfo & INSN_WRITE_GPR_31)
3072 	mask |= 1 << RA;
3073       if (pinfo2 & INSN2_WRITE_GPR_Z)
3074 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3075     }
3076   if (mips_opts.micromips)
3077     {
3078       if (pinfo2 & INSN2_WRITE_GPR_MB)
3079 	mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3080       if (pinfo2 & INSN2_WRITE_GPR_MHI)
3081 	{
3082 	  mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3083 	  mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3084 	}
3085       if (pinfo2 & INSN2_WRITE_GPR_MJ)
3086 	mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3087       if (pinfo2 & INSN2_WRITE_GPR_MP)
3088 	mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3089     }
3090   /* Don't include register 0.  */
3091   return mask & ~1;
3092 }
3093 
3094 /* Return the mask of floating-point registers that IP reads.  */
3095 
3096 static unsigned int
3097 fpr_read_mask (const struct mips_cl_insn *ip)
3098 {
3099   unsigned long pinfo, pinfo2;
3100   unsigned int mask;
3101 
3102   mask = 0;
3103   pinfo = ip->insn_mo->pinfo;
3104   pinfo2 = ip->insn_mo->pinfo2;
3105   if (!mips_opts.mips16)
3106     {
3107       if (pinfo2 & INSN2_READ_FPR_D)
3108 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3109       if (pinfo & INSN_READ_FPR_S)
3110 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3111       if (pinfo & INSN_READ_FPR_T)
3112 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3113       if (pinfo & INSN_READ_FPR_R)
3114 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3115       if (pinfo2 & INSN2_READ_FPR_Z)
3116 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3117     }
3118   /* Conservatively treat all operands to an FP_D instruction are doubles.
3119      (This is overly pessimistic for things like cvt.d.s.)  */
3120   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3121     mask |= mask << 1;
3122   return mask;
3123 }
3124 
3125 /* Return the mask of floating-point registers that IP writes.  */
3126 
3127 static unsigned int
3128 fpr_write_mask (const struct mips_cl_insn *ip)
3129 {
3130   unsigned long pinfo, pinfo2;
3131   unsigned int mask;
3132 
3133   mask = 0;
3134   pinfo = ip->insn_mo->pinfo;
3135   pinfo2 = ip->insn_mo->pinfo2;
3136   if (!mips_opts.mips16)
3137     {
3138       if (pinfo & INSN_WRITE_FPR_D)
3139 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3140       if (pinfo & INSN_WRITE_FPR_S)
3141 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3142       if (pinfo & INSN_WRITE_FPR_T)
3143 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3144       if (pinfo2 & INSN2_WRITE_FPR_Z)
3145 	mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3146     }
3147   /* Conservatively treat all operands to an FP_D instruction are doubles.
3148      (This is overly pessimistic for things like cvt.s.d.)  */
3149   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3150     mask |= mask << 1;
3151   return mask;
3152 }
3153 
3154 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3155    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3156    by VR4120 errata.  */
3157 
3158 static unsigned int
3159 classify_vr4120_insn (const char *name)
3160 {
3161   if (strncmp (name, "macc", 4) == 0)
3162     return FIX_VR4120_MACC;
3163   if (strncmp (name, "dmacc", 5) == 0)
3164     return FIX_VR4120_DMACC;
3165   if (strncmp (name, "mult", 4) == 0)
3166     return FIX_VR4120_MULT;
3167   if (strncmp (name, "dmult", 5) == 0)
3168     return FIX_VR4120_DMULT;
3169   if (strstr (name, "div"))
3170     return FIX_VR4120_DIV;
3171   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3172     return FIX_VR4120_MTHILO;
3173   return NUM_FIX_VR4120_CLASSES;
3174 }
3175 
3176 #define INSN_ERET  0x42000018
3177 #define INSN_DERET 0x4200001f
3178 
3179 /* Return the number of instructions that must separate INSN1 and INSN2,
3180    where INSN1 is the earlier instruction.  Return the worst-case value
3181    for any INSN2 if INSN2 is null.  */
3182 
3183 static unsigned int
3184 insns_between (const struct mips_cl_insn *insn1,
3185 	       const struct mips_cl_insn *insn2)
3186 {
3187   unsigned long pinfo1, pinfo2;
3188   unsigned int mask;
3189 
3190   /* This function needs to know which pinfo flags are set for INSN2
3191      and which registers INSN2 uses.  The former is stored in PINFO2 and
3192      the latter is tested via INSN2_USES_GPR.  If INSN2 is null, PINFO2
3193      will have every flag set and INSN2_USES_GPR will always return true.  */
3194   pinfo1 = insn1->insn_mo->pinfo;
3195   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3196 
3197 #define INSN2_USES_GPR(REG) \
3198   (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3199 
3200   /* For most targets, write-after-read dependencies on the HI and LO
3201      registers must be separated by at least two instructions.  */
3202   if (!hilo_interlocks)
3203     {
3204       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3205 	return 2;
3206       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3207 	return 2;
3208     }
3209 
3210   /* If we're working around r7000 errata, there must be two instructions
3211      between an mfhi or mflo and any instruction that uses the result.  */
3212   if (mips_7000_hilo_fix
3213       && !mips_opts.micromips
3214       && MF_HILO_INSN (pinfo1)
3215       && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3216     return 2;
3217 
3218   /* If we're working around 24K errata, one instruction is required
3219      if an ERET or DERET is followed by a branch instruction.  */
3220   if (mips_fix_24k && !mips_opts.micromips)
3221     {
3222       if (insn1->insn_opcode == INSN_ERET
3223 	  || insn1->insn_opcode == INSN_DERET)
3224 	{
3225 	  if (insn2 == NULL
3226 	      || insn2->insn_opcode == INSN_ERET
3227 	      || insn2->insn_opcode == INSN_DERET
3228 	      || delayed_branch_p (insn2))
3229 	    return 1;
3230 	}
3231     }
3232 
3233   /* If working around VR4120 errata, check for combinations that need
3234      a single intervening instruction.  */
3235   if (mips_fix_vr4120 && !mips_opts.micromips)
3236     {
3237       unsigned int class1, class2;
3238 
3239       class1 = classify_vr4120_insn (insn1->insn_mo->name);
3240       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3241 	{
3242 	  if (insn2 == NULL)
3243 	    return 1;
3244 	  class2 = classify_vr4120_insn (insn2->insn_mo->name);
3245 	  if (vr4120_conflicts[class1] & (1 << class2))
3246 	    return 1;
3247 	}
3248     }
3249 
3250   if (!HAVE_CODE_COMPRESSION)
3251     {
3252       /* Check for GPR or coprocessor load delays.  All such delays
3253 	 are on the RT register.  */
3254       /* Itbl support may require additional care here.  */
3255       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3256 	  || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3257 	{
3258 	  know (pinfo1 & INSN_WRITE_GPR_T);
3259 	  if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3260 	    return 1;
3261 	}
3262 
3263       /* Check for generic coprocessor hazards.
3264 
3265 	 This case is not handled very well.  There is no special
3266 	 knowledge of CP0 handling, and the coprocessors other than
3267 	 the floating point unit are not distinguished at all.  */
3268       /* Itbl support may require additional care here. FIXME!
3269 	 Need to modify this to include knowledge about
3270 	 user specified delays!  */
3271       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3272 	       || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3273 	{
3274 	  /* Handle cases where INSN1 writes to a known general coprocessor
3275 	     register.  There must be a one instruction delay before INSN2
3276 	     if INSN2 reads that register, otherwise no delay is needed.  */
3277 	  mask = fpr_write_mask (insn1);
3278 	  if (mask != 0)
3279 	    {
3280 	      if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3281 		return 1;
3282 	    }
3283 	  else
3284 	    {
3285 	      /* Read-after-write dependencies on the control registers
3286 		 require a two-instruction gap.  */
3287 	      if ((pinfo1 & INSN_WRITE_COND_CODE)
3288 		  && (pinfo2 & INSN_READ_COND_CODE))
3289 		return 2;
3290 
3291 	      /* We don't know exactly what INSN1 does.  If INSN2 is
3292 		 also a coprocessor instruction, assume there must be
3293 		 a one instruction gap.  */
3294 	      if (pinfo2 & INSN_COP)
3295 		return 1;
3296 	    }
3297 	}
3298 
3299       /* Check for read-after-write dependencies on the coprocessor
3300 	 control registers in cases where INSN1 does not need a general
3301 	 coprocessor delay.  This means that INSN1 is a floating point
3302 	 comparison instruction.  */
3303       /* Itbl support may require additional care here.  */
3304       else if (!cop_interlocks
3305 	       && (pinfo1 & INSN_WRITE_COND_CODE)
3306 	       && (pinfo2 & INSN_READ_COND_CODE))
3307 	return 1;
3308     }
3309 
3310 #undef INSN2_USES_GPR
3311 
3312   return 0;
3313 }
3314 
3315 /* Return the number of nops that would be needed to work around the
3316    VR4130 mflo/mfhi errata if instruction INSN immediately followed
3317    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
3318    that are contained within the first IGNORE instructions of HIST.  */
3319 
3320 static int
3321 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3322 		 const struct mips_cl_insn *insn)
3323 {
3324   int i, j;
3325   unsigned int mask;
3326 
3327   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
3328      are not affected by the errata.  */
3329   if (insn != 0
3330       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3331 	  || strcmp (insn->insn_mo->name, "mtlo") == 0
3332 	  || strcmp (insn->insn_mo->name, "mthi") == 0))
3333     return 0;
3334 
3335   /* Search for the first MFLO or MFHI.  */
3336   for (i = 0; i < MAX_VR4130_NOPS; i++)
3337     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3338       {
3339 	/* Extract the destination register.  */
3340 	mask = gpr_write_mask (&hist[i]);
3341 
3342 	/* No nops are needed if INSN reads that register.  */
3343 	if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3344 	  return 0;
3345 
3346 	/* ...or if any of the intervening instructions do.  */
3347 	for (j = 0; j < i; j++)
3348 	  if (gpr_read_mask (&hist[j]) & mask)
3349 	    return 0;
3350 
3351 	if (i >= ignore)
3352 	  return MAX_VR4130_NOPS - i;
3353       }
3354   return 0;
3355 }
3356 
3357 #define BASE_REG_EQ(INSN1, INSN2) 	\
3358   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3359       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3360 
3361 /* Return the minimum alignment for this store instruction.  */
3362 
3363 static int
3364 fix_24k_align_to (const struct mips_opcode *mo)
3365 {
3366   if (strcmp (mo->name, "sh") == 0)
3367     return 2;
3368 
3369   if (strcmp (mo->name, "swc1") == 0
3370       || strcmp (mo->name, "swc2") == 0
3371       || strcmp (mo->name, "sw") == 0
3372       || strcmp (mo->name, "sc") == 0
3373       || strcmp (mo->name, "s.s") == 0)
3374     return 4;
3375 
3376   if (strcmp (mo->name, "sdc1") == 0
3377       || strcmp (mo->name, "sdc2") == 0
3378       || strcmp (mo->name, "s.d") == 0)
3379     return 8;
3380 
3381   /* sb, swl, swr */
3382   return 1;
3383 }
3384 
3385 struct fix_24k_store_info
3386   {
3387     /* Immediate offset, if any, for this store instruction.  */
3388     short off;
3389     /* Alignment required by this store instruction.  */
3390     int align_to;
3391     /* True for register offsets.  */
3392     int register_offset;
3393   };
3394 
3395 /* Comparison function used by qsort.  */
3396 
3397 static int
3398 fix_24k_sort (const void *a, const void *b)
3399 {
3400   const struct fix_24k_store_info *pos1 = a;
3401   const struct fix_24k_store_info *pos2 = b;
3402 
3403   return (pos1->off - pos2->off);
3404 }
3405 
3406 /* INSN is a store instruction.  Try to record the store information
3407    in STINFO.  Return false if the information isn't known.  */
3408 
3409 static bfd_boolean
3410 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3411 			   const struct mips_cl_insn *insn)
3412 {
3413   /* The instruction must have a known offset.  */
3414   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3415     return FALSE;
3416 
3417   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3418   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3419   return TRUE;
3420 }
3421 
3422 /* Return the number of nops that would be needed to work around the 24k
3423    "lost data on stores during refill" errata if instruction INSN
3424    immediately followed the 2 instructions described by HIST.
3425    Ignore hazards that are contained within the first IGNORE
3426    instructions of HIST.
3427 
3428    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3429    for the data cache refills and store data. The following describes
3430    the scenario where the store data could be lost.
3431 
3432    * A data cache miss, due to either a load or a store, causing fill
3433      data to be supplied by the memory subsystem
3434    * The first three doublewords of fill data are returned and written
3435      into the cache
3436    * A sequence of four stores occurs in consecutive cycles around the
3437      final doubleword of the fill:
3438    * Store A
3439    * Store B
3440    * Store C
3441    * Zero, One or more instructions
3442    * Store D
3443 
3444    The four stores A-D must be to different doublewords of the line that
3445    is being filled. The fourth instruction in the sequence above permits
3446    the fill of the final doubleword to be transferred from the FSB into
3447    the cache. In the sequence above, the stores may be either integer
3448    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3449    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3450    different doublewords on the line. If the floating point unit is
3451    running in 1:2 mode, it is not possible to create the sequence above
3452    using only floating point store instructions.
3453 
3454    In this case, the cache line being filled is incorrectly marked
3455    invalid, thereby losing the data from any store to the line that
3456    occurs between the original miss and the completion of the five
3457    cycle sequence shown above.
3458 
3459    The workarounds are:
3460 
3461    * Run the data cache in write-through mode.
3462    * Insert a non-store instruction between
3463      Store A and Store B or Store B and Store C.  */
3464 
3465 static int
3466 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3467 	      const struct mips_cl_insn *insn)
3468 {
3469   struct fix_24k_store_info pos[3];
3470   int align, i, base_offset;
3471 
3472   if (ignore >= 2)
3473     return 0;
3474 
3475   /* If the previous instruction wasn't a store, there's nothing to
3476      worry about.  */
3477   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3478     return 0;
3479 
3480   /* If the instructions after the previous one are unknown, we have
3481      to assume the worst.  */
3482   if (!insn)
3483     return 1;
3484 
3485   /* Check whether we are dealing with three consecutive stores.  */
3486   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3487       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3488     return 0;
3489 
3490   /* If we don't know the relationship between the store addresses,
3491      assume the worst.  */
3492   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3493       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3494     return 1;
3495 
3496   if (!fix_24k_record_store_info (&pos[0], insn)
3497       || !fix_24k_record_store_info (&pos[1], &hist[0])
3498       || !fix_24k_record_store_info (&pos[2], &hist[1]))
3499     return 1;
3500 
3501   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3502 
3503   /* Pick a value of ALIGN and X such that all offsets are adjusted by
3504      X bytes and such that the base register + X is known to be aligned
3505      to align bytes.  */
3506 
3507   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3508     align = 8;
3509   else
3510     {
3511       align = pos[0].align_to;
3512       base_offset = pos[0].off;
3513       for (i = 1; i < 3; i++)
3514 	if (align < pos[i].align_to)
3515 	  {
3516 	    align = pos[i].align_to;
3517 	    base_offset = pos[i].off;
3518 	  }
3519       for (i = 0; i < 3; i++)
3520 	pos[i].off -= base_offset;
3521     }
3522 
3523   pos[0].off &= ~align + 1;
3524   pos[1].off &= ~align + 1;
3525   pos[2].off &= ~align + 1;
3526 
3527   /* If any two stores write to the same chunk, they also write to the
3528      same doubleword.  The offsets are still sorted at this point.  */
3529   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3530     return 0;
3531 
3532   /* A range of at least 9 bytes is needed for the stores to be in
3533      non-overlapping doublewords.  */
3534   if (pos[2].off - pos[0].off <= 8)
3535     return 0;
3536 
3537   if (pos[2].off - pos[1].off >= 24
3538       || pos[1].off - pos[0].off >= 24
3539       || pos[2].off - pos[0].off >= 32)
3540     return 0;
3541 
3542   return 1;
3543 }
3544 
3545 /* Return the number of nops that would be needed if instruction INSN
3546    immediately followed the MAX_NOPS instructions given by HIST,
3547    where HIST[0] is the most recent instruction.  Ignore hazards
3548    between INSN and the first IGNORE instructions in HIST.
3549 
3550    If INSN is null, return the worse-case number of nops for any
3551    instruction.  */
3552 
3553 static int
3554 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3555 	       const struct mips_cl_insn *insn)
3556 {
3557   int i, nops, tmp_nops;
3558 
3559   nops = 0;
3560   for (i = ignore; i < MAX_DELAY_NOPS; i++)
3561     {
3562       tmp_nops = insns_between (hist + i, insn) - i;
3563       if (tmp_nops > nops)
3564 	nops = tmp_nops;
3565     }
3566 
3567   if (mips_fix_vr4130 && !mips_opts.micromips)
3568     {
3569       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
3570       if (tmp_nops > nops)
3571 	nops = tmp_nops;
3572     }
3573 
3574   if (mips_fix_24k && !mips_opts.micromips)
3575     {
3576       tmp_nops = nops_for_24k (ignore, hist, insn);
3577       if (tmp_nops > nops)
3578 	nops = tmp_nops;
3579     }
3580 
3581   return nops;
3582 }
3583 
3584 /* The variable arguments provide NUM_INSNS extra instructions that
3585    might be added to HIST.  Return the largest number of nops that
3586    would be needed after the extended sequence, ignoring hazards
3587    in the first IGNORE instructions.  */
3588 
3589 static int
3590 nops_for_sequence (int num_insns, int ignore,
3591 		   const struct mips_cl_insn *hist, ...)
3592 {
3593   va_list args;
3594   struct mips_cl_insn buffer[MAX_NOPS];
3595   struct mips_cl_insn *cursor;
3596   int nops;
3597 
3598   va_start (args, hist);
3599   cursor = buffer + num_insns;
3600   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
3601   while (cursor > buffer)
3602     *--cursor = *va_arg (args, const struct mips_cl_insn *);
3603 
3604   nops = nops_for_insn (ignore, buffer, NULL);
3605   va_end (args);
3606   return nops;
3607 }
3608 
3609 /* Like nops_for_insn, but if INSN is a branch, take into account the
3610    worst-case delay for the branch target.  */
3611 
3612 static int
3613 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
3614 			 const struct mips_cl_insn *insn)
3615 {
3616   int nops, tmp_nops;
3617 
3618   nops = nops_for_insn (ignore, hist, insn);
3619   if (delayed_branch_p (insn))
3620     {
3621       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
3622 				    hist, insn, get_delay_slot_nop (insn));
3623       if (tmp_nops > nops)
3624 	nops = tmp_nops;
3625     }
3626   else if (compact_branch_p (insn))
3627     {
3628       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
3629       if (tmp_nops > nops)
3630 	nops = tmp_nops;
3631     }
3632   return nops;
3633 }
3634 
3635 static void
3636 trap_zero_jump (struct mips_cl_insn * ip)
3637 {
3638   if (strcmp (ip->insn_mo->name, "j") == 0
3639       || strcmp (ip->insn_mo->name, "jr") == 0
3640       || strcmp (ip->insn_mo->name, "jalr") == 0)
3641     {
3642       int sreg;
3643 
3644       if (mips_opts.warn_about_macros)
3645         return;
3646 
3647       sreg = EXTRACT_OPERAND (0, RS, *ip);
3648       if (mips_opts.isa == ISA_MIPS32
3649           || mips_opts.isa == ISA_MIPS32R2
3650           || mips_opts.isa == ISA_MIPS64
3651           || mips_opts.isa == ISA_MIPS64R2)
3652 	{
3653 	  expressionS ep;
3654 	  ep.X_op = O_constant;
3655 	  ep.X_add_number = 4096;
3656 	  macro_build (&ep, "tltiu", "s,j", sreg, BFD_RELOC_LO16);
3657 	}
3658       else if (mips_opts.isa != ISA_UNKNOWN
3659 	       && mips_opts.isa != ISA_MIPS1)
3660 	macro_build (NULL, "teq", "s,t", sreg, 0);
3661   }
3662 }
3663 
3664 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
3665 
3666 static void
3667 fix_loongson2f_nop (struct mips_cl_insn * ip)
3668 {
3669   gas_assert (!HAVE_CODE_COMPRESSION);
3670   if (strcmp (ip->insn_mo->name, "nop") == 0)
3671     ip->insn_opcode = LOONGSON2F_NOP_INSN;
3672 }
3673 
3674 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3675                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
3676 
3677 static void
3678 fix_loongson2f_jump (struct mips_cl_insn * ip)
3679 {
3680   gas_assert (!HAVE_CODE_COMPRESSION);
3681   if (strcmp (ip->insn_mo->name, "j") == 0
3682       || strcmp (ip->insn_mo->name, "jr") == 0
3683       || strcmp (ip->insn_mo->name, "jalr") == 0)
3684     {
3685       int sreg;
3686       expressionS ep;
3687 
3688       if (! mips_opts.at)
3689         return;
3690 
3691       sreg = EXTRACT_OPERAND (0, RS, *ip);
3692       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3693         return;
3694 
3695       ep.X_op = O_constant;
3696       ep.X_add_number = 0xcfff0000;
3697       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3698       ep.X_add_number = 0xffff;
3699       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3700       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3701       /* Hide these three instructions to avoid getting a ``macro expanded into
3702          multiple instructions'' warning. */
3703       if (mips_relax.sequence != 2) {
3704         mips_macro_warning.sizes[0] -= 3 * 4;
3705         mips_macro_warning.insns[0] -= 3;
3706       }
3707       if (mips_relax.sequence != 1) {
3708         mips_macro_warning.sizes[1] -= 3 * 4;
3709         mips_macro_warning.insns[1] -= 3;
3710       }
3711     }
3712 }
3713 
3714 static void
3715 fix_loongson2f (struct mips_cl_insn * ip)
3716 {
3717   if (mips_fix_loongson2f_nop)
3718     fix_loongson2f_nop (ip);
3719 
3720   if (mips_fix_loongson2f_jump)
3721     fix_loongson2f_jump (ip);
3722 }
3723 
3724 /* IP is a branch that has a delay slot, and we need to fill it
3725    automatically.   Return true if we can do that by swapping IP
3726    with the previous instruction.  */
3727 
3728 static bfd_boolean
3729 can_swap_branch_p (struct mips_cl_insn *ip)
3730 {
3731   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
3732   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3733 
3734   /* -O2 and above is required for this optimization.  */
3735   if (mips_optimize < 2)
3736     return FALSE;
3737 
3738   /* If we have seen .set volatile or .set nomove, don't optimize.  */
3739   if (mips_opts.nomove)
3740     return FALSE;
3741 
3742   /* We can't swap if the previous instruction's position is fixed.  */
3743   if (history[0].fixed_p)
3744     return FALSE;
3745 
3746   /* If the previous previous insn was in a .set noreorder, we can't
3747      swap.  Actually, the MIPS assembler will swap in this situation.
3748      However, gcc configured -with-gnu-as will generate code like
3749 
3750 	.set	noreorder
3751 	lw	$4,XXX
3752 	.set	reorder
3753 	INSN
3754 	bne	$4,$0,foo
3755 
3756      in which we can not swap the bne and INSN.  If gcc is not configured
3757      -with-gnu-as, it does not output the .set pseudo-ops.  */
3758   if (history[1].noreorder_p)
3759     return FALSE;
3760 
3761   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3762      This means that the previous instruction was a 4-byte one anyhow.  */
3763   if (mips_opts.mips16 && history[0].fixp[0])
3764     return FALSE;
3765 
3766   if (mips_fix_loongson2f)
3767     fix_loongson2f (ip);
3768   if (mips_trap_zero_jump)
3769     trap_zero_jump (ip);
3770 
3771   /* If the branch is itself the target of a branch, we can not swap.
3772      We cheat on this; all we check for is whether there is a label on
3773      this instruction.  If there are any branches to anything other than
3774      a label, users must use .set noreorder.  */
3775   if (seg_info (now_seg)->label_list)
3776     return FALSE;
3777 
3778   /* If the previous instruction is in a variant frag other than this
3779      branch's one, we cannot do the swap.  This does not apply to
3780      MIPS16 code, which uses variant frags for different purposes.  */
3781   if (!mips_opts.mips16
3782       && history[0].frag
3783       && history[0].frag->fr_type == rs_machine_dependent)
3784     return FALSE;
3785 
3786   /* We do not swap with instructions that cannot architecturally
3787      be placed in a branch delay slot, such as SYNC or ERET.  We
3788      also refrain from swapping with a trap instruction, since it
3789      complicates trap handlers to have the trap instruction be in
3790      a delay slot.  */
3791   prev_pinfo = history[0].insn_mo->pinfo;
3792   if (prev_pinfo & INSN_NO_DELAY_SLOT)
3793     return FALSE;
3794 
3795   /* Check for conflicts between the branch and the instructions
3796      before the candidate delay slot.  */
3797   if (nops_for_insn (0, history + 1, ip) > 0)
3798     return FALSE;
3799 
3800   /* Check for conflicts between the swapped sequence and the
3801      target of the branch.  */
3802   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3803     return FALSE;
3804 
3805   /* If the branch reads a register that the previous
3806      instruction sets, we can not swap.  */
3807   gpr_read = gpr_read_mask (ip);
3808   prev_gpr_write = gpr_write_mask (&history[0]);
3809   if (gpr_read & prev_gpr_write)
3810     return FALSE;
3811 
3812   /* If the branch writes a register that the previous
3813      instruction sets, we can not swap.  */
3814   gpr_write = gpr_write_mask (ip);
3815   if (gpr_write & prev_gpr_write)
3816     return FALSE;
3817 
3818   /* If the branch writes a register that the previous
3819      instruction reads, we can not swap.  */
3820   prev_gpr_read = gpr_read_mask (&history[0]);
3821   if (gpr_write & prev_gpr_read)
3822     return FALSE;
3823 
3824   /* If one instruction sets a condition code and the
3825      other one uses a condition code, we can not swap.  */
3826   pinfo = ip->insn_mo->pinfo;
3827   if ((pinfo & INSN_READ_COND_CODE)
3828       && (prev_pinfo & INSN_WRITE_COND_CODE))
3829     return FALSE;
3830   if ((pinfo & INSN_WRITE_COND_CODE)
3831       && (prev_pinfo & INSN_READ_COND_CODE))
3832     return FALSE;
3833 
3834   /* If the previous instruction uses the PC, we can not swap.  */
3835   prev_pinfo2 = history[0].insn_mo->pinfo2;
3836   if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3837     return FALSE;
3838   if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3839     return FALSE;
3840 
3841   /* If the previous instruction has an incorrect size for a fixed
3842      branch delay slot in microMIPS mode, we cannot swap.  */
3843   pinfo2 = ip->insn_mo->pinfo2;
3844   if (mips_opts.micromips
3845       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3846       && insn_length (history) != 2)
3847     return FALSE;
3848   if (mips_opts.micromips
3849       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3850       && insn_length (history) != 4)
3851     return FALSE;
3852 
3853   return TRUE;
3854 }
3855 
3856 /* Decide how we should add IP to the instruction stream.  */
3857 
3858 static enum append_method
3859 get_append_method (struct mips_cl_insn *ip)
3860 {
3861   unsigned long pinfo;
3862 
3863   /* The relaxed version of a macro sequence must be inherently
3864      hazard-free.  */
3865   if (mips_relax.sequence == 2)
3866     return APPEND_ADD;
3867 
3868   /* We must not dabble with instructions in a ".set norerorder" block.  */
3869   if (mips_opts.noreorder)
3870     return APPEND_ADD;
3871 
3872   /* Otherwise, it's our responsibility to fill branch delay slots.  */
3873   if (delayed_branch_p (ip))
3874     {
3875       if (!branch_likely_p (ip) && can_swap_branch_p (ip))
3876 	return APPEND_SWAP;
3877 
3878       pinfo = ip->insn_mo->pinfo;
3879       if (mips_opts.mips16
3880 	  && ISA_SUPPORTS_MIPS16E
3881 	  && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3882 	return APPEND_ADD_COMPACT;
3883 
3884       return APPEND_ADD_WITH_NOP;
3885     }
3886 
3887   return APPEND_ADD;
3888 }
3889 
3890 /* IP is a MIPS16 instruction whose opcode we have just changed.
3891    Point IP->insn_mo to the new opcode's definition.  */
3892 
3893 static void
3894 find_altered_mips16_opcode (struct mips_cl_insn *ip)
3895 {
3896   const struct mips_opcode *mo, *end;
3897 
3898   end = &mips16_opcodes[bfd_mips16_num_opcodes];
3899   for (mo = ip->insn_mo; mo < end; mo++)
3900     if ((ip->insn_opcode & mo->mask) == mo->match)
3901       {
3902 	ip->insn_mo = mo;
3903 	return;
3904       }
3905   abort ();
3906 }
3907 
3908 /* For microMIPS macros, we need to generate a local number label
3909    as the target of branches.  */
3910 #define MICROMIPS_LABEL_CHAR		'\037'
3911 static unsigned long micromips_target_label;
3912 static char micromips_target_name[32];
3913 
3914 static char *
3915 micromips_label_name (void)
3916 {
3917   char *p = micromips_target_name;
3918   char symbol_name_temporary[24];
3919   unsigned long l;
3920   int i;
3921 
3922   if (*p)
3923     return p;
3924 
3925   i = 0;
3926   l = micromips_target_label;
3927 #ifdef LOCAL_LABEL_PREFIX
3928   *p++ = LOCAL_LABEL_PREFIX;
3929 #endif
3930   *p++ = 'L';
3931   *p++ = MICROMIPS_LABEL_CHAR;
3932   do
3933     {
3934       symbol_name_temporary[i++] = l % 10 + '0';
3935       l /= 10;
3936     }
3937   while (l != 0);
3938   while (i > 0)
3939     *p++ = symbol_name_temporary[--i];
3940   *p = '\0';
3941 
3942   return micromips_target_name;
3943 }
3944 
3945 static void
3946 micromips_label_expr (expressionS *label_expr)
3947 {
3948   label_expr->X_op = O_symbol;
3949   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
3950   label_expr->X_add_number = 0;
3951 }
3952 
3953 static void
3954 micromips_label_inc (void)
3955 {
3956   micromips_target_label++;
3957   *micromips_target_name = '\0';
3958 }
3959 
3960 static void
3961 micromips_add_label (void)
3962 {
3963   symbolS *s;
3964 
3965   s = colon (micromips_label_name ());
3966   micromips_label_inc ();
3967 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
3968   if (IS_ELF)
3969     S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
3970 #else
3971   (void) s;
3972 #endif
3973 }
3974 
3975 /* If assembling microMIPS code, then return the microMIPS reloc
3976    corresponding to the requested one if any.  Otherwise return
3977    the reloc unchanged.  */
3978 
3979 static bfd_reloc_code_real_type
3980 micromips_map_reloc (bfd_reloc_code_real_type reloc)
3981 {
3982   static const bfd_reloc_code_real_type relocs[][2] =
3983     {
3984       /* Keep sorted incrementally by the left-hand key.  */
3985       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
3986       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
3987       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
3988       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
3989       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
3990       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
3991       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
3992       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
3993       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
3994       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
3995       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
3996       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
3997       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
3998       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
3999       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4000       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4001       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4002       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4003       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4004       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4005       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4006       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4007       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4008       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4009       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4010       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4011       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4012     };
4013   bfd_reloc_code_real_type r;
4014   size_t i;
4015 
4016   if (!mips_opts.micromips)
4017     return reloc;
4018   for (i = 0; i < ARRAY_SIZE (relocs); i++)
4019     {
4020       r = relocs[i][0];
4021       if (r > reloc)
4022 	return reloc;
4023       if (r == reloc)
4024 	return relocs[i][1];
4025     }
4026   return reloc;
4027 }
4028 
4029 /* Output an instruction.  IP is the instruction information.
4030    ADDRESS_EXPR is an operand of the instruction to be used with
4031    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
4032    a macro expansion.  */
4033 
4034 static void
4035 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
4036 	     bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
4037 {
4038   unsigned long prev_pinfo2, pinfo;
4039   bfd_boolean relaxed_branch = FALSE;
4040   enum append_method method;
4041   bfd_boolean relax32;
4042   int branch_disp;
4043 
4044   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4045     fix_loongson2f (ip);
4046 
4047   mips_mark_labels ();
4048 
4049   file_ase_mips16 |= mips_opts.mips16;
4050   file_ase_micromips |= mips_opts.micromips;
4051 
4052   prev_pinfo2 = history[0].insn_mo->pinfo2;
4053   pinfo = ip->insn_mo->pinfo;
4054 
4055   if (mips_opts.micromips
4056       && !expansionp
4057       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4058 	   && micromips_insn_length (ip->insn_mo) != 2)
4059 	  || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4060 	      && micromips_insn_length (ip->insn_mo) != 4)))
4061     as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4062 	     (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4063 
4064   if (address_expr == NULL)
4065     ip->complete_p = 1;
4066   else if (*reloc_type <= BFD_RELOC_UNUSED
4067 	   && address_expr->X_op == O_constant)
4068     {
4069       unsigned int tmp;
4070 
4071       ip->complete_p = 1;
4072       switch (*reloc_type)
4073 	{
4074 	case BFD_RELOC_32:
4075 	  ip->insn_opcode |= address_expr->X_add_number;
4076 	  break;
4077 
4078 	case BFD_RELOC_MIPS_HIGHEST:
4079 	  tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
4080 	  ip->insn_opcode |= tmp & 0xffff;
4081 	  break;
4082 
4083 	case BFD_RELOC_MIPS_HIGHER:
4084 	  tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
4085 	  ip->insn_opcode |= tmp & 0xffff;
4086 	  break;
4087 
4088 	case BFD_RELOC_HI16_S:
4089 	  tmp = (address_expr->X_add_number + 0x8000) >> 16;
4090 	  ip->insn_opcode |= tmp & 0xffff;
4091 	  break;
4092 
4093 	case BFD_RELOC_HI16:
4094 	  ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
4095 	  break;
4096 
4097 	case BFD_RELOC_UNUSED:
4098 	case BFD_RELOC_LO16:
4099 	case BFD_RELOC_MIPS_GOT_DISP:
4100 	  ip->insn_opcode |= address_expr->X_add_number & 0xffff;
4101 	  break;
4102 
4103 	case BFD_RELOC_MIPS_JMP:
4104 	  {
4105 	    int shift;
4106 
4107 	    shift = mips_opts.micromips ? 1 : 2;
4108 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4109 	      as_bad (_("jump to misaligned address (0x%lx)"),
4110 		      (unsigned long) address_expr->X_add_number);
4111 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4112 				& 0x3ffffff);
4113 	    ip->complete_p = 0;
4114 	  }
4115 	  break;
4116 
4117 	case BFD_RELOC_MIPS16_JMP:
4118 	  if ((address_expr->X_add_number & 3) != 0)
4119 	    as_bad (_("jump to misaligned address (0x%lx)"),
4120 	            (unsigned long) address_expr->X_add_number);
4121 	  ip->insn_opcode |=
4122 	    (((address_expr->X_add_number & 0x7c0000) << 3)
4123 	       | ((address_expr->X_add_number & 0xf800000) >> 7)
4124 	       | ((address_expr->X_add_number & 0x3fffc) >> 2));
4125 	  ip->complete_p = 0;
4126 	  break;
4127 
4128 	case BFD_RELOC_16_PCREL_S2:
4129 	  {
4130 	    int shift;
4131 
4132 	    shift = mips_opts.micromips ? 1 : 2;
4133 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4134 	      as_bad (_("branch to misaligned address (0x%lx)"),
4135 		      (unsigned long) address_expr->X_add_number);
4136 	    if (!mips_relax_branch)
4137 	      {
4138 		if ((address_expr->X_add_number + (1 << (shift + 15)))
4139 		    & ~((1 << (shift + 16)) - 1))
4140 		  as_bad (_("branch address range overflow (0x%lx)"),
4141 			  (unsigned long) address_expr->X_add_number);
4142 		ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4143 				    & 0xffff);
4144 	      }
4145 	    ip->complete_p = 0;
4146 	  }
4147 	  break;
4148 
4149 	default:
4150 	  internalError ();
4151 	}
4152     }
4153 
4154   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4155     {
4156       /* There are a lot of optimizations we could do that we don't.
4157 	 In particular, we do not, in general, reorder instructions.
4158 	 If you use gcc with optimization, it will reorder
4159 	 instructions and generally do much more optimization then we
4160 	 do here; repeating all that work in the assembler would only
4161 	 benefit hand written assembly code, and does not seem worth
4162 	 it.  */
4163       int nops = (mips_optimize == 0
4164 		  ? nops_for_insn (0, history, NULL)
4165 		  : nops_for_insn_or_target (0, history, ip));
4166       if (nops > 0)
4167 	{
4168 	  fragS *old_frag;
4169 	  unsigned long old_frag_offset;
4170 	  int i;
4171 
4172 	  old_frag = frag_now;
4173 	  old_frag_offset = frag_now_fix ();
4174 
4175 	  for (i = 0; i < nops; i++)
4176 	    add_fixed_insn (NOP_INSN);
4177 	  insert_into_history (0, nops, NOP_INSN);
4178 
4179 	  if (listing)
4180 	    {
4181 	      listing_prev_line ();
4182 	      /* We may be at the start of a variant frag.  In case we
4183                  are, make sure there is enough space for the frag
4184                  after the frags created by listing_prev_line.  The
4185                  argument to frag_grow here must be at least as large
4186                  as the argument to all other calls to frag_grow in
4187                  this file.  We don't have to worry about being in the
4188                  middle of a variant frag, because the variants insert
4189                  all needed nop instructions themselves.  */
4190 	      frag_grow (40);
4191 	    }
4192 
4193 	  mips_move_text_labels ();
4194 
4195 #ifndef NO_ECOFF_DEBUGGING
4196 	  if (ECOFF_DEBUGGING)
4197 	    ecoff_fix_loc (old_frag, old_frag_offset);
4198 #endif
4199 	}
4200     }
4201   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4202     {
4203       int nops;
4204 
4205       /* Work out how many nops in prev_nop_frag are needed by IP,
4206 	 ignoring hazards generated by the first prev_nop_frag_since
4207 	 instructions.  */
4208       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4209       gas_assert (nops <= prev_nop_frag_holds);
4210 
4211       /* Enforce NOPS as a minimum.  */
4212       if (nops > prev_nop_frag_required)
4213 	prev_nop_frag_required = nops;
4214 
4215       if (prev_nop_frag_holds == prev_nop_frag_required)
4216 	{
4217 	  /* Settle for the current number of nops.  Update the history
4218 	     accordingly (for the benefit of any future .set reorder code).  */
4219 	  prev_nop_frag = NULL;
4220 	  insert_into_history (prev_nop_frag_since,
4221 			       prev_nop_frag_holds, NOP_INSN);
4222 	}
4223       else
4224 	{
4225 	  /* Allow this instruction to replace one of the nops that was
4226 	     tentatively added to prev_nop_frag.  */
4227 	  prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4228 	  prev_nop_frag_holds--;
4229 	  prev_nop_frag_since++;
4230 	}
4231     }
4232 
4233   method = get_append_method (ip);
4234   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4235 
4236 #ifdef OBJ_ELF
4237   /* The value passed to dwarf2_emit_insn is the distance between
4238      the beginning of the current instruction and the address that
4239      should be recorded in the debug tables.  This is normally the
4240      current address.
4241 
4242      For MIPS16/microMIPS debug info we want to use ISA-encoded
4243      addresses, so we use -1 for an address higher by one than the
4244      current one.
4245 
4246      If the instruction produced is a branch that we will swap with
4247      the preceding instruction, then we add the displacement by which
4248      the branch will be moved backwards.  This is more appropriate
4249      and for MIPS16/microMIPS code also prevents a debugger from
4250      placing a breakpoint in the middle of the branch (and corrupting
4251      code if software breakpoints are used).  */
4252   dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
4253 #endif
4254 
4255   relax32 = (mips_relax_branch
4256 	     /* Don't try branch relaxation within .set nomacro, or within
4257 	        .set noat if we use $at for PIC computations.  If it turns
4258 	        out that the branch was out-of-range, we'll get an error.  */
4259 	     && !mips_opts.warn_about_macros
4260 	     && (mips_opts.at || mips_pic == NO_PIC)
4261 	     /* Don't relax BPOSGE32/64 as they have no complementing
4262 	        branches.  */
4263 	     && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
4264 
4265   if (!HAVE_CODE_COMPRESSION
4266       && address_expr
4267       && relax32
4268       && *reloc_type == BFD_RELOC_16_PCREL_S2
4269       && delayed_branch_p (ip))
4270     {
4271       relaxed_branch = TRUE;
4272       add_relaxed_insn (ip, (relaxed_branch_length
4273 			     (NULL, NULL,
4274 			      uncond_branch_p (ip) ? -1
4275 			      : branch_likely_p (ip) ? 1
4276 			      : 0)), 4,
4277 			RELAX_BRANCH_ENCODE
4278 			(AT,
4279 			 uncond_branch_p (ip),
4280 			 branch_likely_p (ip),
4281 			 pinfo & INSN_WRITE_GPR_31,
4282 			 0),
4283 			address_expr->X_add_symbol,
4284 			address_expr->X_add_number);
4285       *reloc_type = BFD_RELOC_UNUSED;
4286     }
4287   else if (mips_opts.micromips
4288 	   && address_expr
4289 	   && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4290 	       || *reloc_type > BFD_RELOC_UNUSED)
4291 	   && (delayed_branch_p (ip) || compact_branch_p (ip))
4292 	   /* Don't try branch relaxation when users specify
4293 	      16-bit/32-bit instructions.  */
4294 	   && !forced_insn_length)
4295     {
4296       bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4297       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4298       int uncond = uncond_branch_p (ip) ? -1 : 0;
4299       int compact = compact_branch_p (ip);
4300       int al = pinfo & INSN_WRITE_GPR_31;
4301       int length32;
4302 
4303       gas_assert (address_expr != NULL);
4304       gas_assert (!mips_relax.sequence);
4305 
4306       relaxed_branch = TRUE;
4307       length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4308       add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4309 			RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4310 						relax32, 0, 0),
4311 			address_expr->X_add_symbol,
4312 			address_expr->X_add_number);
4313       *reloc_type = BFD_RELOC_UNUSED;
4314     }
4315   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4316     {
4317       /* We need to set up a variant frag.  */
4318       gas_assert (address_expr != NULL);
4319       add_relaxed_insn (ip, 4, 0,
4320 			RELAX_MIPS16_ENCODE
4321 			(*reloc_type - BFD_RELOC_UNUSED,
4322 			 forced_insn_length == 2, forced_insn_length == 4,
4323 			 delayed_branch_p (&history[0]),
4324 			 history[0].mips16_absolute_jump_p),
4325 			make_expr_symbol (address_expr), 0);
4326     }
4327   else if (mips_opts.mips16
4328 	   && ! ip->use_extend
4329 	   && *reloc_type != BFD_RELOC_MIPS16_JMP)
4330     {
4331       if (!delayed_branch_p (ip))
4332 	/* Make sure there is enough room to swap this instruction with
4333 	   a following jump instruction.  */
4334 	frag_grow (6);
4335       add_fixed_insn (ip);
4336     }
4337   else
4338     {
4339       if (mips_opts.mips16
4340 	  && mips_opts.noreorder
4341 	  && delayed_branch_p (&history[0]))
4342 	as_warn (_("extended instruction in delay slot"));
4343 
4344       if (mips_relax.sequence)
4345 	{
4346 	  /* If we've reached the end of this frag, turn it into a variant
4347 	     frag and record the information for the instructions we've
4348 	     written so far.  */
4349 	  if (frag_room () < 4)
4350 	    relax_close_frag ();
4351 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4352 	}
4353 
4354       if (mips_relax.sequence != 2)
4355 	{
4356 	  if (mips_macro_warning.first_insn_sizes[0] == 0)
4357 	    mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4358 	  mips_macro_warning.sizes[0] += insn_length (ip);
4359 	  mips_macro_warning.insns[0]++;
4360 	}
4361       if (mips_relax.sequence != 1)
4362 	{
4363 	  if (mips_macro_warning.first_insn_sizes[1] == 0)
4364 	    mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4365 	  mips_macro_warning.sizes[1] += insn_length (ip);
4366 	  mips_macro_warning.insns[1]++;
4367 	}
4368 
4369       if (mips_opts.mips16)
4370 	{
4371 	  ip->fixed_p = 1;
4372 	  ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4373 	}
4374       add_fixed_insn (ip);
4375     }
4376 
4377   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4378     {
4379       bfd_reloc_code_real_type final_type[3];
4380       reloc_howto_type *howto0;
4381       reloc_howto_type *howto;
4382       int i;
4383 
4384       /* Perform any necessary conversion to microMIPS relocations
4385 	 and find out how many relocations there actually are.  */
4386       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4387 	final_type[i] = micromips_map_reloc (reloc_type[i]);
4388 
4389       /* In a compound relocation, it is the final (outermost)
4390 	 operator that determines the relocated field.  */
4391       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4392 
4393       if (howto == NULL)
4394 	{
4395 	  /* To reproduce this failure try assembling gas/testsuites/
4396 	     gas/mips/mips16-intermix.s with a mips-ecoff targeted
4397 	     assembler.  */
4398 	  as_bad (_("Unsupported MIPS relocation number %d"),
4399 		  final_type[i - 1]);
4400 	  howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4401 	}
4402 
4403       if (i > 1)
4404 	howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4405       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4406 				 bfd_get_reloc_size (howto),
4407 				 address_expr,
4408 				 howto0 && howto0->pc_relative,
4409 				 final_type[0]);
4410 
4411       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
4412       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4413 	*symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4414 
4415       /* These relocations can have an addend that won't fit in
4416 	 4 octets for 64bit assembly.  */
4417       if (HAVE_64BIT_GPRS
4418 	  && ! howto->partial_inplace
4419 	  && (reloc_type[0] == BFD_RELOC_16
4420 	      || reloc_type[0] == BFD_RELOC_32
4421 	      || reloc_type[0] == BFD_RELOC_MIPS_JMP
4422 	      || reloc_type[0] == BFD_RELOC_GPREL16
4423 	      || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4424 	      || reloc_type[0] == BFD_RELOC_GPREL32
4425 	      || reloc_type[0] == BFD_RELOC_64
4426 	      || reloc_type[0] == BFD_RELOC_CTOR
4427 	      || reloc_type[0] == BFD_RELOC_MIPS_SUB
4428 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4429 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4430 	      || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4431 	      || reloc_type[0] == BFD_RELOC_MIPS_REL16
4432 	      || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4433 	      || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4434 	      || hi16_reloc_p (reloc_type[0])
4435 	      || lo16_reloc_p (reloc_type[0])))
4436 	ip->fixp[0]->fx_no_overflow = 1;
4437 
4438       if (mips_relax.sequence)
4439 	{
4440 	  if (mips_relax.first_fixup == 0)
4441 	    mips_relax.first_fixup = ip->fixp[0];
4442 	}
4443       else if (reloc_needs_lo_p (*reloc_type))
4444 	{
4445 	  struct mips_hi_fixup *hi_fixup;
4446 
4447 	  /* Reuse the last entry if it already has a matching %lo.  */
4448 	  hi_fixup = mips_hi_fixup_list;
4449 	  if (hi_fixup == 0
4450 	      || !fixup_has_matching_lo_p (hi_fixup->fixp))
4451 	    {
4452 	      hi_fixup = ((struct mips_hi_fixup *)
4453 			  xmalloc (sizeof (struct mips_hi_fixup)));
4454 	      hi_fixup->next = mips_hi_fixup_list;
4455 	      mips_hi_fixup_list = hi_fixup;
4456 	    }
4457 	  hi_fixup->fixp = ip->fixp[0];
4458 	  hi_fixup->seg = now_seg;
4459 	}
4460 
4461       /* Add fixups for the second and third relocations, if given.
4462 	 Note that the ABI allows the second relocation to be
4463 	 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
4464 	 moment we only use RSS_UNDEF, but we could add support
4465 	 for the others if it ever becomes necessary.  */
4466       for (i = 1; i < 3; i++)
4467 	if (reloc_type[i] != BFD_RELOC_UNUSED)
4468 	  {
4469 	    ip->fixp[i] = fix_new (ip->frag, ip->where,
4470 				   ip->fixp[0]->fx_size, NULL, 0,
4471 				   FALSE, final_type[i]);
4472 
4473 	    /* Use fx_tcbit to mark compound relocs.  */
4474 	    ip->fixp[0]->fx_tcbit = 1;
4475 	    ip->fixp[i]->fx_tcbit = 1;
4476 	  }
4477     }
4478   install_insn (ip);
4479 
4480   /* Update the register mask information.  */
4481   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4482   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4483 
4484   switch (method)
4485     {
4486     case APPEND_ADD:
4487       insert_into_history (0, 1, ip);
4488       break;
4489 
4490     case APPEND_ADD_WITH_NOP:
4491       {
4492 	struct mips_cl_insn *nop;
4493 
4494 	insert_into_history (0, 1, ip);
4495 	nop = get_delay_slot_nop (ip);
4496 	add_fixed_insn (nop);
4497 	insert_into_history (0, 1, nop);
4498 	if (mips_relax.sequence)
4499 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4500       }
4501       break;
4502 
4503     case APPEND_ADD_COMPACT:
4504       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
4505       gas_assert (mips_opts.mips16);
4506       ip->insn_opcode |= 0x0080;
4507       find_altered_mips16_opcode (ip);
4508       install_insn (ip);
4509       insert_into_history (0, 1, ip);
4510       break;
4511 
4512     case APPEND_SWAP:
4513       {
4514 	struct mips_cl_insn delay = history[0];
4515 	if (mips_opts.mips16)
4516 	  {
4517 	    know (delay.frag == ip->frag);
4518 	    move_insn (ip, delay.frag, delay.where);
4519 	    move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4520 	  }
4521 	else if (relaxed_branch || delay.frag != ip->frag)
4522 	  {
4523 	    /* Add the delay slot instruction to the end of the
4524 	       current frag and shrink the fixed part of the
4525 	       original frag.  If the branch occupies the tail of
4526 	       the latter, move it backwards to cover the gap.  */
4527 	    delay.frag->fr_fix -= branch_disp;
4528 	    if (delay.frag == ip->frag)
4529 	      move_insn (ip, ip->frag, ip->where - branch_disp);
4530 	    add_fixed_insn (&delay);
4531 	  }
4532 	else
4533 	  {
4534 	    move_insn (&delay, ip->frag,
4535 		       ip->where - branch_disp + insn_length (ip));
4536 	    move_insn (ip, history[0].frag, history[0].where);
4537 	  }
4538 	history[0] = *ip;
4539 	delay.fixed_p = 1;
4540 	insert_into_history (0, 1, &delay);
4541       }
4542       break;
4543     }
4544 
4545   /* If we have just completed an unconditional branch, clear the history.  */
4546   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4547       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
4548     mips_no_prev_insn ();
4549 
4550   /* We need to emit a label at the end of branch-likely macros.  */
4551   if (emit_branch_likely_macro)
4552     {
4553       emit_branch_likely_macro = FALSE;
4554       micromips_add_label ();
4555     }
4556 
4557   /* We just output an insn, so the next one doesn't have a label.  */
4558   mips_clear_insn_labels ();
4559 }
4560 
4561 /* Forget that there was any previous instruction or label.  */
4562 
4563 static void
4564 mips_no_prev_insn (void)
4565 {
4566   prev_nop_frag = NULL;
4567   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
4568   mips_clear_insn_labels ();
4569 }
4570 
4571 /* This function must be called before we emit something other than
4572    instructions.  It is like mips_no_prev_insn except that it inserts
4573    any NOPS that might be needed by previous instructions.  */
4574 
4575 void
4576 mips_emit_delays (void)
4577 {
4578   if (! mips_opts.noreorder)
4579     {
4580       int nops = nops_for_insn (0, history, NULL);
4581       if (nops > 0)
4582 	{
4583 	  while (nops-- > 0)
4584 	    add_fixed_insn (NOP_INSN);
4585 	  mips_move_text_labels ();
4586 	}
4587     }
4588   mips_no_prev_insn ();
4589 }
4590 
4591 /* Start a (possibly nested) noreorder block.  */
4592 
4593 static void
4594 start_noreorder (void)
4595 {
4596   if (mips_opts.noreorder == 0)
4597     {
4598       unsigned int i;
4599       int nops;
4600 
4601       /* None of the instructions before the .set noreorder can be moved.  */
4602       for (i = 0; i < ARRAY_SIZE (history); i++)
4603 	history[i].fixed_p = 1;
4604 
4605       /* Insert any nops that might be needed between the .set noreorder
4606 	 block and the previous instructions.  We will later remove any
4607 	 nops that turn out not to be needed.  */
4608       nops = nops_for_insn (0, history, NULL);
4609       if (nops > 0)
4610 	{
4611 	  if (mips_optimize != 0)
4612 	    {
4613 	      /* Record the frag which holds the nop instructions, so
4614                  that we can remove them if we don't need them.  */
4615 	      frag_grow (nops * NOP_INSN_SIZE);
4616 	      prev_nop_frag = frag_now;
4617 	      prev_nop_frag_holds = nops;
4618 	      prev_nop_frag_required = 0;
4619 	      prev_nop_frag_since = 0;
4620 	    }
4621 
4622 	  for (; nops > 0; --nops)
4623 	    add_fixed_insn (NOP_INSN);
4624 
4625 	  /* Move on to a new frag, so that it is safe to simply
4626 	     decrease the size of prev_nop_frag.  */
4627 	  frag_wane (frag_now);
4628 	  frag_new (0);
4629 	  mips_move_text_labels ();
4630 	}
4631       mips_mark_labels ();
4632       mips_clear_insn_labels ();
4633     }
4634   mips_opts.noreorder++;
4635   mips_any_noreorder = 1;
4636 }
4637 
4638 /* End a nested noreorder block.  */
4639 
4640 static void
4641 end_noreorder (void)
4642 {
4643   mips_opts.noreorder--;
4644   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4645     {
4646       /* Commit to inserting prev_nop_frag_required nops and go back to
4647 	 handling nop insertion the .set reorder way.  */
4648       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
4649 				* NOP_INSN_SIZE);
4650       insert_into_history (prev_nop_frag_since,
4651 			   prev_nop_frag_required, NOP_INSN);
4652       prev_nop_frag = NULL;
4653     }
4654 }
4655 
4656 /* Set up global variables for the start of a new macro.  */
4657 
4658 static void
4659 macro_start (void)
4660 {
4661   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
4662   memset (&mips_macro_warning.first_insn_sizes, 0,
4663 	  sizeof (mips_macro_warning.first_insn_sizes));
4664   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
4665   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
4666 				     && delayed_branch_p (&history[0]));
4667   switch (history[0].insn_mo->pinfo2
4668 	  & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4669     {
4670     case INSN2_BRANCH_DELAY_32BIT:
4671       mips_macro_warning.delay_slot_length = 4;
4672       break;
4673     case INSN2_BRANCH_DELAY_16BIT:
4674       mips_macro_warning.delay_slot_length = 2;
4675       break;
4676     default:
4677       mips_macro_warning.delay_slot_length = 0;
4678       break;
4679     }
4680   mips_macro_warning.first_frag = NULL;
4681 }
4682 
4683 /* Given that a macro is longer than one instruction or of the wrong size,
4684    return the appropriate warning for it.  Return null if no warning is
4685    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4686    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4687    and RELAX_NOMACRO.  */
4688 
4689 static const char *
4690 macro_warning (relax_substateT subtype)
4691 {
4692   if (subtype & RELAX_DELAY_SLOT)
4693     return _("Macro instruction expanded into multiple instructions"
4694 	     " in a branch delay slot");
4695   else if (subtype & RELAX_NOMACRO)
4696     return _("Macro instruction expanded into multiple instructions");
4697   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4698 		      | RELAX_DELAY_SLOT_SIZE_SECOND))
4699     return ((subtype & RELAX_DELAY_SLOT_16BIT)
4700 	    ? _("Macro instruction expanded into a wrong size instruction"
4701 		" in a 16-bit branch delay slot")
4702 	    : _("Macro instruction expanded into a wrong size instruction"
4703 		" in a 32-bit branch delay slot"));
4704   else
4705     return 0;
4706 }
4707 
4708 /* Finish up a macro.  Emit warnings as appropriate.  */
4709 
4710 static void
4711 macro_end (void)
4712 {
4713   /* Relaxation warning flags.  */
4714   relax_substateT subtype = 0;
4715 
4716   /* Check delay slot size requirements.  */
4717   if (mips_macro_warning.delay_slot_length == 2)
4718     subtype |= RELAX_DELAY_SLOT_16BIT;
4719   if (mips_macro_warning.delay_slot_length != 0)
4720     {
4721       if (mips_macro_warning.delay_slot_length
4722 	  != mips_macro_warning.first_insn_sizes[0])
4723 	subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4724       if (mips_macro_warning.delay_slot_length
4725 	  != mips_macro_warning.first_insn_sizes[1])
4726 	subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4727     }
4728 
4729   /* Check instruction count requirements.  */
4730   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4731     {
4732       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
4733 	subtype |= RELAX_SECOND_LONGER;
4734       if (mips_opts.warn_about_macros)
4735 	subtype |= RELAX_NOMACRO;
4736       if (mips_macro_warning.delay_slot_p)
4737 	subtype |= RELAX_DELAY_SLOT;
4738     }
4739 
4740   /* If both alternatives fail to fill a delay slot correctly,
4741      emit the warning now.  */
4742   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4743       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4744     {
4745       relax_substateT s;
4746       const char *msg;
4747 
4748       s = subtype & (RELAX_DELAY_SLOT_16BIT
4749 		     | RELAX_DELAY_SLOT_SIZE_FIRST
4750 		     | RELAX_DELAY_SLOT_SIZE_SECOND);
4751       msg = macro_warning (s);
4752       if (msg != NULL)
4753 	as_warn ("%s", msg);
4754       subtype &= ~s;
4755     }
4756 
4757   /* If both implementations are longer than 1 instruction, then emit the
4758      warning now.  */
4759   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4760     {
4761       relax_substateT s;
4762       const char *msg;
4763 
4764       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4765       msg = macro_warning (s);
4766       if (msg != NULL)
4767 	as_warn ("%s", msg);
4768       subtype &= ~s;
4769     }
4770 
4771   /* If any flags still set, then one implementation might need a warning
4772      and the other either will need one of a different kind or none at all.
4773      Pass any remaining flags over to relaxation.  */
4774   if (mips_macro_warning.first_frag != NULL)
4775     mips_macro_warning.first_frag->fr_subtype |= subtype;
4776 }
4777 
4778 /* Instruction operand formats used in macros that vary between
4779    standard MIPS and microMIPS code.  */
4780 
4781 static const char * const brk_fmt[2] = { "c", "mF" };
4782 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4783 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4784 static const char * const lui_fmt[2] = { "t,u", "s,u" };
4785 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4786 static const char * const mfhl_fmt[2] = { "d", "mj" };
4787 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4788 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4789 
4790 #define BRK_FMT (brk_fmt[mips_opts.micromips])
4791 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
4792 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
4793 #define LUI_FMT (lui_fmt[mips_opts.micromips])
4794 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4795 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4796 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
4797 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
4798 
4799 /* Read a macro's relocation codes from *ARGS and store them in *R.
4800    The first argument in *ARGS will be either the code for a single
4801    relocation or -1 followed by the three codes that make up a
4802    composite relocation.  */
4803 
4804 static void
4805 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4806 {
4807   int i, next;
4808 
4809   next = va_arg (*args, int);
4810   if (next >= 0)
4811     r[0] = (bfd_reloc_code_real_type) next;
4812   else
4813     for (i = 0; i < 3; i++)
4814       r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4815 }
4816 
4817 /* Fix jump through register issue on loongson2f processor for kernel code:
4818    force a BTB clear before the jump to prevent it from being incorrectly
4819    prefetched by the branch prediction engine. */
4820 
4821 static void
4822 macro_build_jrpatch (expressionS *ep, unsigned int sreg)
4823 {
4824   if (!mips_fix_loongson2f_btb)
4825     return;
4826 
4827   if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == AT)
4828     return;
4829 
4830   if (!mips_opts.at)
4831     {
4832       as_warn (_("unable to apply loongson2f BTB workaround when .set noat"));
4833       return;
4834     }
4835 
4836   /* li $at, COP_0_BTB_CLEAR | COP_0_RAS_DISABLE */
4837   ep->X_op = O_constant;
4838   ep->X_add_number = 3;
4839   macro_build (ep, "ori", "t,r,i", AT, ZERO, BFD_RELOC_LO16);
4840 
4841   /* dmtc0 $at, COP_0_DIAG */
4842   macro_build (NULL, "dmtc0", "t,G", AT, 22);
4843 
4844   /* Hide these two instructions to avoid getting a ``macro expanded into
4845      multiple instructions'' warning. */
4846   if (mips_relax.sequence != 2) {
4847     mips_macro_warning.sizes[0] -= 2 * 4;
4848     mips_macro_warning.insns[0] -= 2;
4849   }
4850   if (mips_relax.sequence != 1) {
4851     mips_macro_warning.sizes[1] -= 2 * 4;
4852     mips_macro_warning.insns[1] -= 2;
4853   }
4854 }
4855 
4856 /* Build an instruction created by a macro expansion.  This is passed
4857    a pointer to the count of instructions created so far, an
4858    expression, the name of the instruction to build, an operand format
4859    string, and corresponding arguments.  */
4860 
4861 static void
4862 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
4863 {
4864   const struct mips_opcode *mo = NULL;
4865   bfd_reloc_code_real_type r[3];
4866   const struct mips_opcode *amo;
4867   struct hash_control *hash;
4868   struct mips_cl_insn insn;
4869   va_list args;
4870 
4871   va_start (args, fmt);
4872 
4873   if (mips_opts.mips16)
4874     {
4875       mips16_macro_build (ep, name, fmt, &args);
4876       va_end (args);
4877       return;
4878     }
4879 
4880   r[0] = BFD_RELOC_UNUSED;
4881   r[1] = BFD_RELOC_UNUSED;
4882   r[2] = BFD_RELOC_UNUSED;
4883   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4884   amo = (struct mips_opcode *) hash_find (hash, name);
4885   gas_assert (amo);
4886   gas_assert (strcmp (name, amo->name) == 0);
4887 
4888   do
4889     {
4890       /* Search until we get a match for NAME.  It is assumed here that
4891 	 macros will never generate MDMX, MIPS-3D, or MT instructions.
4892 	 We try to match an instruction that fulfils the branch delay
4893 	 slot instruction length requirement (if any) of the previous
4894 	 instruction.  While doing this we record the first instruction
4895 	 seen that matches all the other conditions and use it anyway
4896 	 if the requirement cannot be met; we will issue an appropriate
4897 	 warning later on.  */
4898       if (strcmp (fmt, amo->args) == 0
4899 	  && amo->pinfo != INSN_MACRO
4900 	  && is_opcode_valid (amo)
4901 	  && is_size_valid (amo))
4902 	{
4903 	  if (is_delay_slot_valid (amo))
4904 	    {
4905 	      mo = amo;
4906 	      break;
4907 	    }
4908 	  else if (!mo)
4909 	    mo = amo;
4910 	}
4911 
4912       ++amo;
4913       gas_assert (amo->name);
4914     }
4915   while (strcmp (name, amo->name) == 0);
4916 
4917   gas_assert (mo);
4918   create_insn (&insn, mo);
4919   for (;;)
4920     {
4921       switch (*fmt++)
4922 	{
4923 	case '\0':
4924 	  break;
4925 
4926 	case ',':
4927 	case '(':
4928 	case ')':
4929 	  continue;
4930 
4931 	case '+':
4932 	  switch (*fmt++)
4933 	    {
4934 	    case 'A':
4935 	    case 'E':
4936 	      INSERT_OPERAND (mips_opts.micromips,
4937 			      EXTLSB, insn, va_arg (args, int));
4938 	      continue;
4939 
4940 	    case 'B':
4941 	    case 'F':
4942 	      /* Note that in the macro case, these arguments are already
4943 		 in MSB form.  (When handling the instruction in the
4944 		 non-macro case, these arguments are sizes from which
4945 		 MSB values must be calculated.)  */
4946 	      INSERT_OPERAND (mips_opts.micromips,
4947 			      INSMSB, insn, va_arg (args, int));
4948 	      continue;
4949 
4950 	    case 'C':
4951 	    case 'G':
4952 	    case 'H':
4953 	      /* Note that in the macro case, these arguments are already
4954 		 in MSBD form.  (When handling the instruction in the
4955 		 non-macro case, these arguments are sizes from which
4956 		 MSBD values must be calculated.)  */
4957 	      INSERT_OPERAND (mips_opts.micromips,
4958 			      EXTMSBD, insn, va_arg (args, int));
4959 	      continue;
4960 
4961 	    case 'Q':
4962 	      gas_assert (!mips_opts.micromips);
4963 	      INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
4964 	      continue;
4965 
4966 	    default:
4967 	      internalError ();
4968 	    }
4969 	  continue;
4970 
4971 	case '2':
4972 	  INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
4973 	  continue;
4974 
4975 	case 'n':
4976 	  gas_assert (mips_opts.micromips);
4977 	case 't':
4978 	case 'w':
4979 	case 'E':
4980 	  INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
4981 	  continue;
4982 
4983 	case 'c':
4984 	  gas_assert (!mips_opts.micromips);
4985 	  INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
4986 	  continue;
4987 
4988 	case 'W':
4989 	  gas_assert (!mips_opts.micromips);
4990 	case 'T':
4991 	  INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
4992 	  continue;
4993 
4994 	case 'G':
4995 	  if (mips_opts.micromips)
4996 	    INSERT_OPERAND (1, RS, insn, va_arg (args, int));
4997 	  else
4998 	    INSERT_OPERAND (0, RD, insn, va_arg (args, int));
4999 	  continue;
5000 
5001 	case 'K':
5002 	  gas_assert (!mips_opts.micromips);
5003 	case 'd':
5004 	  INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
5005 	  continue;
5006 
5007 	case 'U':
5008 	  gas_assert (!mips_opts.micromips);
5009 	  {
5010 	    int tmp = va_arg (args, int);
5011 
5012 	    INSERT_OPERAND (0, RT, insn, tmp);
5013 	    INSERT_OPERAND (0, RD, insn, tmp);
5014 	  }
5015 	  continue;
5016 
5017 	case 'V':
5018 	case 'S':
5019 	  gas_assert (!mips_opts.micromips);
5020 	  INSERT_OPERAND (0, FS, insn, va_arg (args, int));
5021 	  continue;
5022 
5023 	case 'z':
5024 	  continue;
5025 
5026 	case '<':
5027 	  INSERT_OPERAND (mips_opts.micromips,
5028 			  SHAMT, insn, va_arg (args, int));
5029 	  continue;
5030 
5031 	case 'D':
5032 	  gas_assert (!mips_opts.micromips);
5033 	  INSERT_OPERAND (0, FD, insn, va_arg (args, int));
5034 	  continue;
5035 
5036 	case 'B':
5037 	  gas_assert (!mips_opts.micromips);
5038 	  INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
5039 	  continue;
5040 
5041 	case 'J':
5042 	  gas_assert (!mips_opts.micromips);
5043 	  INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
5044 	  continue;
5045 
5046 	case 'q':
5047 	  gas_assert (!mips_opts.micromips);
5048 	  INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
5049 	  continue;
5050 
5051 	case 'b':
5052 	case 's':
5053 	case 'r':
5054 	case 'v':
5055 	  INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
5056 	  continue;
5057 
5058 	case 'i':
5059 	case 'j':
5060 	  macro_read_relocs (&args, r);
5061 	  gas_assert (*r == BFD_RELOC_GPREL16
5062 		      || *r == BFD_RELOC_MIPS_HIGHER
5063 		      || *r == BFD_RELOC_HI16_S
5064 		      || *r == BFD_RELOC_LO16
5065 		      || *r == BFD_RELOC_MIPS_GOT_OFST);
5066 	  continue;
5067 
5068 	case 'o':
5069 	  macro_read_relocs (&args, r);
5070 	  continue;
5071 
5072 	case 'u':
5073 	  macro_read_relocs (&args, r);
5074 	  gas_assert (ep != NULL
5075 		      && (ep->X_op == O_constant
5076 			  || (ep->X_op == O_symbol
5077 			      && (*r == BFD_RELOC_MIPS_HIGHEST
5078 				  || *r == BFD_RELOC_HI16_S
5079 				  || *r == BFD_RELOC_HI16
5080 				  || *r == BFD_RELOC_GPREL16
5081 				  || *r == BFD_RELOC_MIPS_GOT_HI16
5082 				  || *r == BFD_RELOC_MIPS_CALL_HI16))));
5083 	  continue;
5084 
5085 	case 'p':
5086 	  gas_assert (ep != NULL);
5087 
5088 	  /*
5089 	   * This allows macro() to pass an immediate expression for
5090 	   * creating short branches without creating a symbol.
5091 	   *
5092 	   * We don't allow branch relaxation for these branches, as
5093 	   * they should only appear in ".set nomacro" anyway.
5094 	   */
5095 	  if (ep->X_op == O_constant)
5096 	    {
5097 	      /* For microMIPS we always use relocations for branches.
5098 	         So we should not resolve immediate values.  */
5099 	      gas_assert (!mips_opts.micromips);
5100 
5101 	      if ((ep->X_add_number & 3) != 0)
5102 		as_bad (_("branch to misaligned address (0x%lx)"),
5103 			(unsigned long) ep->X_add_number);
5104 	      if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5105 		as_bad (_("branch address range overflow (0x%lx)"),
5106 			(unsigned long) ep->X_add_number);
5107 	      insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5108 	      ep = NULL;
5109 	    }
5110 	  else
5111 	    *r = BFD_RELOC_16_PCREL_S2;
5112 	  continue;
5113 
5114 	case 'a':
5115 	  gas_assert (ep != NULL);
5116 	  *r = BFD_RELOC_MIPS_JMP;
5117 	  continue;
5118 
5119 	case 'C':
5120 	  gas_assert (!mips_opts.micromips);
5121 	  INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5122 	  continue;
5123 
5124 	case 'k':
5125 	  INSERT_OPERAND (mips_opts.micromips,
5126 			  CACHE, insn, va_arg (args, unsigned long));
5127 	  continue;
5128 
5129 	case '|':
5130 	  gas_assert (mips_opts.micromips);
5131 	  INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5132 	  continue;
5133 
5134 	case '.':
5135 	  gas_assert (mips_opts.micromips);
5136 	  INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5137 	  continue;
5138 
5139 	case '\\':
5140 	  INSERT_OPERAND (mips_opts.micromips,
5141 			  3BITPOS, insn, va_arg (args, unsigned int));
5142 	  continue;
5143 
5144 	case '~':
5145 	  INSERT_OPERAND (mips_opts.micromips,
5146 			  OFFSET12, insn, va_arg (args, unsigned long));
5147 	  continue;
5148 
5149 	case 'N':
5150 	  gas_assert (mips_opts.micromips);
5151 	  INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5152 	  continue;
5153 
5154 	case 'm':	/* Opcode extension character.  */
5155 	  gas_assert (mips_opts.micromips);
5156 	  switch (*fmt++)
5157 	    {
5158 	    case 'j':
5159 	      INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5160 	      break;
5161 
5162 	    case 'p':
5163 	      INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5164 	      break;
5165 
5166 	    case 'F':
5167 	      INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5168 	      break;
5169 
5170 	    default:
5171 	      internalError ();
5172 	    }
5173 	  continue;
5174 
5175 	default:
5176 	  internalError ();
5177 	}
5178       break;
5179     }
5180   va_end (args);
5181   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5182 
5183   append_insn (&insn, ep, r, TRUE);
5184 }
5185 
5186 static void
5187 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5188 		    va_list *args)
5189 {
5190   struct mips_opcode *mo;
5191   struct mips_cl_insn insn;
5192   bfd_reloc_code_real_type r[3]
5193     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5194 
5195   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5196   gas_assert (mo);
5197   gas_assert (strcmp (name, mo->name) == 0);
5198 
5199   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5200     {
5201       ++mo;
5202       gas_assert (mo->name);
5203       gas_assert (strcmp (name, mo->name) == 0);
5204     }
5205 
5206   create_insn (&insn, mo);
5207   for (;;)
5208     {
5209       int c;
5210 
5211       c = *fmt++;
5212       switch (c)
5213 	{
5214 	case '\0':
5215 	  break;
5216 
5217 	case ',':
5218 	case '(':
5219 	case ')':
5220 	  continue;
5221 
5222 	case 'y':
5223 	case 'w':
5224 	  MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5225 	  continue;
5226 
5227 	case 'x':
5228 	case 'v':
5229 	  MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5230 	  continue;
5231 
5232 	case 'z':
5233 	  MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5234 	  continue;
5235 
5236 	case 'Z':
5237 	  MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5238 	  continue;
5239 
5240 	case '0':
5241 	case 'S':
5242 	case 'P':
5243 	case 'R':
5244 	  continue;
5245 
5246 	case 'X':
5247 	  MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5248 	  continue;
5249 
5250 	case 'Y':
5251 	  {
5252 	    int regno;
5253 
5254 	    regno = va_arg (*args, int);
5255 	    regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5256 	    MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5257 	  }
5258 	  continue;
5259 
5260 	case '<':
5261 	case '>':
5262 	case '4':
5263 	case '5':
5264 	case 'H':
5265 	case 'W':
5266 	case 'D':
5267 	case 'j':
5268 	case '8':
5269 	case 'V':
5270 	case 'C':
5271 	case 'U':
5272 	case 'k':
5273 	case 'K':
5274 	case 'p':
5275 	case 'q':
5276 	  {
5277 	    gas_assert (ep != NULL);
5278 
5279 	    if (ep->X_op != O_constant)
5280 	      *r = (int) BFD_RELOC_UNUSED + c;
5281 	    else
5282 	      {
5283 		mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
5284 			      FALSE, &insn.insn_opcode, &insn.use_extend,
5285 			      &insn.extend);
5286 		ep = NULL;
5287 		*r = BFD_RELOC_UNUSED;
5288 	      }
5289 	  }
5290 	  continue;
5291 
5292 	case '6':
5293 	  MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5294 	  continue;
5295 	}
5296 
5297       break;
5298     }
5299 
5300   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5301 
5302   append_insn (&insn, ep, r, TRUE);
5303 }
5304 
5305 /*
5306  * Sign-extend 32-bit mode constants that have bit 31 set and all
5307  * higher bits unset.
5308  */
5309 static void
5310 normalize_constant_expr (expressionS *ex)
5311 {
5312   if (ex->X_op == O_constant
5313       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5314     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5315 			- 0x80000000);
5316 }
5317 
5318 /*
5319  * Sign-extend 32-bit mode address offsets that have bit 31 set and
5320  * all higher bits unset.
5321  */
5322 static void
5323 normalize_address_expr (expressionS *ex)
5324 {
5325   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5326 	|| (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5327       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5328     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5329 			- 0x80000000);
5330 }
5331 
5332 /*
5333  * Generate a "jalr" instruction with a relocation hint to the called
5334  * function.  This occurs in NewABI PIC code.
5335  */
5336 static void
5337 macro_build_jalr (expressionS *ep, int cprestore)
5338 {
5339   static const bfd_reloc_code_real_type jalr_relocs[2]
5340     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5341   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5342   const char *jalr;
5343   char *f = NULL;
5344 
5345   if (MIPS_JALR_HINT_P (ep))
5346     {
5347       frag_grow (8);
5348       f = frag_more (0);
5349     }
5350   if (mips_opts.micromips)
5351     {
5352       jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
5353       if (MIPS_JALR_HINT_P (ep))
5354 	macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5355       else
5356 	macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5357     }
5358   else
5359     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5360   if (MIPS_JALR_HINT_P (ep))
5361     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5362 }
5363 
5364 /*
5365  * Generate a "lui" instruction.
5366  */
5367 static void
5368 macro_build_lui (expressionS *ep, int regnum)
5369 {
5370   gas_assert (! mips_opts.mips16);
5371 
5372   if (ep->X_op != O_constant)
5373     {
5374       gas_assert (ep->X_op == O_symbol);
5375       /* _gp_disp is a special case, used from s_cpload.
5376 	 __gnu_local_gp is used if mips_no_shared.  */
5377       gas_assert (mips_pic == NO_PIC
5378 	      || (! HAVE_NEWABI
5379 		  && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5380 	      || (! mips_in_shared
5381 		  && strcmp (S_GET_NAME (ep->X_add_symbol),
5382                              "__gnu_local_gp") == 0));
5383     }
5384 
5385   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5386 }
5387 
5388 /* Generate a sequence of instructions to do a load or store from a constant
5389    offset off of a base register (breg) into/from a target register (treg),
5390    using AT if necessary.  */
5391 static void
5392 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5393 			      int treg, int breg, int dbl)
5394 {
5395   gas_assert (ep->X_op == O_constant);
5396 
5397   /* Sign-extending 32-bit constants makes their handling easier.  */
5398   if (!dbl)
5399     normalize_constant_expr (ep);
5400 
5401   /* Right now, this routine can only handle signed 32-bit constants.  */
5402   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5403     as_warn (_("operand overflow"));
5404 
5405   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5406     {
5407       /* Signed 16-bit offset will fit in the op.  Easy!  */
5408       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5409     }
5410   else
5411     {
5412       /* 32-bit offset, need multiple instructions and AT, like:
5413 	   lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
5414 	   addu     $tempreg,$tempreg,$breg
5415            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
5416          to handle the complete offset.  */
5417       macro_build_lui (ep, AT);
5418       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5419       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5420 
5421       if (!mips_opts.at)
5422 	as_bad (_("Macro used $at after \".set noat\""));
5423     }
5424 }
5425 
5426 /*			set_at()
5427  * Generates code to set the $at register to true (one)
5428  * if reg is less than the immediate expression.
5429  */
5430 static void
5431 set_at (int reg, int unsignedp)
5432 {
5433   if (imm_expr.X_op == O_constant
5434       && imm_expr.X_add_number >= -0x8000
5435       && imm_expr.X_add_number < 0x8000)
5436     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5437 		 AT, reg, BFD_RELOC_LO16);
5438   else
5439     {
5440       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5441       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5442     }
5443 }
5444 
5445 /* Warn if an expression is not a constant.  */
5446 
5447 static void
5448 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5449 {
5450   if (ex->X_op == O_big)
5451     as_bad (_("unsupported large constant"));
5452   else if (ex->X_op != O_constant)
5453     as_bad (_("Instruction %s requires absolute expression"),
5454 	    ip->insn_mo->name);
5455 
5456   if (HAVE_32BIT_GPRS)
5457     normalize_constant_expr (ex);
5458 }
5459 
5460 /* Count the leading zeroes by performing a binary chop. This is a
5461    bulky bit of source, but performance is a LOT better for the
5462    majority of values than a simple loop to count the bits:
5463        for (lcnt = 0; (lcnt < 32); lcnt++)
5464          if ((v) & (1 << (31 - lcnt)))
5465            break;
5466   However it is not code size friendly, and the gain will drop a bit
5467   on certain cached systems.
5468 */
5469 #define COUNT_TOP_ZEROES(v)             \
5470   (((v) & ~0xffff) == 0                 \
5471    ? ((v) & ~0xff) == 0                 \
5472      ? ((v) & ~0xf) == 0                \
5473        ? ((v) & ~0x3) == 0              \
5474          ? ((v) & ~0x1) == 0            \
5475            ? !(v)                       \
5476              ? 32                       \
5477              : 31                       \
5478            : 30                         \
5479          : ((v) & ~0x7) == 0            \
5480            ? 29                         \
5481            : 28                         \
5482        : ((v) & ~0x3f) == 0             \
5483          ? ((v) & ~0x1f) == 0           \
5484            ? 27                         \
5485            : 26                         \
5486          : ((v) & ~0x7f) == 0           \
5487            ? 25                         \
5488            : 24                         \
5489      : ((v) & ~0xfff) == 0              \
5490        ? ((v) & ~0x3ff) == 0            \
5491          ? ((v) & ~0x1ff) == 0          \
5492            ? 23                         \
5493            : 22                         \
5494          : ((v) & ~0x7ff) == 0          \
5495            ? 21                         \
5496            : 20                         \
5497        : ((v) & ~0x3fff) == 0           \
5498          ? ((v) & ~0x1fff) == 0         \
5499            ? 19                         \
5500            : 18                         \
5501          : ((v) & ~0x7fff) == 0         \
5502            ? 17                         \
5503            : 16                         \
5504    : ((v) & ~0xffffff) == 0             \
5505      ? ((v) & ~0xfffff) == 0            \
5506        ? ((v) & ~0x3ffff) == 0          \
5507          ? ((v) & ~0x1ffff) == 0        \
5508            ? 15                         \
5509            : 14                         \
5510          : ((v) & ~0x7ffff) == 0        \
5511            ? 13                         \
5512            : 12                         \
5513        : ((v) & ~0x3fffff) == 0         \
5514          ? ((v) & ~0x1fffff) == 0       \
5515            ? 11                         \
5516            : 10                         \
5517          : ((v) & ~0x7fffff) == 0       \
5518            ? 9                          \
5519            : 8                          \
5520      : ((v) & ~0xfffffff) == 0          \
5521        ? ((v) & ~0x3ffffff) == 0        \
5522          ? ((v) & ~0x1ffffff) == 0      \
5523            ? 7                          \
5524            : 6                          \
5525          : ((v) & ~0x7ffffff) == 0      \
5526            ? 5                          \
5527            : 4                          \
5528        : ((v) & ~0x3fffffff) == 0       \
5529          ? ((v) & ~0x1fffffff) == 0     \
5530            ? 3                          \
5531            : 2                          \
5532          : ((v) & ~0x7fffffff) == 0     \
5533            ? 1                          \
5534            : 0)
5535 
5536 /*			load_register()
5537  *  This routine generates the least number of instructions necessary to load
5538  *  an absolute expression value into a register.
5539  */
5540 static void
5541 load_register (int reg, expressionS *ep, int dbl)
5542 {
5543   int freg;
5544   expressionS hi32, lo32;
5545 
5546   if (ep->X_op != O_big)
5547     {
5548       gas_assert (ep->X_op == O_constant);
5549 
5550       /* Sign-extending 32-bit constants makes their handling easier.  */
5551       if (!dbl)
5552 	normalize_constant_expr (ep);
5553 
5554       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5555 	{
5556 	  /* We can handle 16 bit signed values with an addiu to
5557 	     $zero.  No need to ever use daddiu here, since $zero and
5558 	     the result are always correct in 32 bit mode.  */
5559 	  macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5560 	  return;
5561 	}
5562       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5563 	{
5564 	  /* We can handle 16 bit unsigned values with an ori to
5565              $zero.  */
5566 	  macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5567 	  return;
5568 	}
5569       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
5570 	{
5571 	  /* 32 bit values require an lui.  */
5572 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5573 	  if ((ep->X_add_number & 0xffff) != 0)
5574 	    macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5575 	  return;
5576 	}
5577     }
5578 
5579   /* The value is larger than 32 bits.  */
5580 
5581   if (!dbl || HAVE_32BIT_GPRS)
5582     {
5583       char value[32];
5584 
5585       sprintf_vma (value, ep->X_add_number);
5586       as_bad (_("Number (0x%s) larger than 32 bits"), value);
5587       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5588       return;
5589     }
5590 
5591   if (ep->X_op != O_big)
5592     {
5593       hi32 = *ep;
5594       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5595       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5596       hi32.X_add_number &= 0xffffffff;
5597       lo32 = *ep;
5598       lo32.X_add_number &= 0xffffffff;
5599     }
5600   else
5601     {
5602       gas_assert (ep->X_add_number > 2);
5603       if (ep->X_add_number == 3)
5604 	generic_bignum[3] = 0;
5605       else if (ep->X_add_number > 4)
5606 	as_bad (_("Number larger than 64 bits"));
5607       lo32.X_op = O_constant;
5608       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5609       hi32.X_op = O_constant;
5610       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5611     }
5612 
5613   if (hi32.X_add_number == 0)
5614     freg = 0;
5615   else
5616     {
5617       int shift, bit;
5618       unsigned long hi, lo;
5619 
5620       if (hi32.X_add_number == (offsetT) 0xffffffff)
5621 	{
5622 	  if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5623 	    {
5624 	      macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5625 	      return;
5626 	    }
5627 	  if (lo32.X_add_number & 0x80000000)
5628 	    {
5629 	      macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5630 	      if (lo32.X_add_number & 0xffff)
5631 		macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5632 	      return;
5633 	    }
5634 	}
5635 
5636       /* Check for 16bit shifted constant.  We know that hi32 is
5637          non-zero, so start the mask on the first bit of the hi32
5638          value.  */
5639       shift = 17;
5640       do
5641 	{
5642 	  unsigned long himask, lomask;
5643 
5644 	  if (shift < 32)
5645 	    {
5646 	      himask = 0xffff >> (32 - shift);
5647 	      lomask = (0xffff << shift) & 0xffffffff;
5648 	    }
5649 	  else
5650 	    {
5651 	      himask = 0xffff << (shift - 32);
5652 	      lomask = 0;
5653 	    }
5654 	  if ((hi32.X_add_number & ~(offsetT) himask) == 0
5655 	      && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5656 	    {
5657 	      expressionS tmp;
5658 
5659 	      tmp.X_op = O_constant;
5660 	      if (shift < 32)
5661 		tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5662 				    | (lo32.X_add_number >> shift));
5663 	      else
5664 		tmp.X_add_number = hi32.X_add_number >> (shift - 32);
5665 	      macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5666 	      macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5667 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
5668 	      return;
5669 	    }
5670 	  ++shift;
5671 	}
5672       while (shift <= (64 - 16));
5673 
5674       /* Find the bit number of the lowest one bit, and store the
5675          shifted value in hi/lo.  */
5676       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5677       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5678       if (lo != 0)
5679 	{
5680 	  bit = 0;
5681 	  while ((lo & 1) == 0)
5682 	    {
5683 	      lo >>= 1;
5684 	      ++bit;
5685 	    }
5686 	  lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5687 	  hi >>= bit;
5688 	}
5689       else
5690 	{
5691 	  bit = 32;
5692 	  while ((hi & 1) == 0)
5693 	    {
5694 	      hi >>= 1;
5695 	      ++bit;
5696 	    }
5697 	  lo = hi;
5698 	  hi = 0;
5699 	}
5700 
5701       /* Optimize if the shifted value is a (power of 2) - 1.  */
5702       if ((hi == 0 && ((lo + 1) & lo) == 0)
5703 	  || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
5704 	{
5705 	  shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
5706 	  if (shift != 0)
5707 	    {
5708 	      expressionS tmp;
5709 
5710 	      /* This instruction will set the register to be all
5711                  ones.  */
5712 	      tmp.X_op = O_constant;
5713 	      tmp.X_add_number = (offsetT) -1;
5714 	      macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5715 	      if (bit != 0)
5716 		{
5717 		  bit += shift;
5718 		  macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5719 			       reg, reg, (bit >= 32) ? bit - 32 : bit);
5720 		}
5721 	      macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
5722 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
5723 	      return;
5724 	    }
5725 	}
5726 
5727       /* Sign extend hi32 before calling load_register, because we can
5728          generally get better code when we load a sign extended value.  */
5729       if ((hi32.X_add_number & 0x80000000) != 0)
5730 	hi32.X_add_number |= ~(offsetT) 0xffffffff;
5731       load_register (reg, &hi32, 0);
5732       freg = reg;
5733     }
5734   if ((lo32.X_add_number & 0xffff0000) == 0)
5735     {
5736       if (freg != 0)
5737 	{
5738 	  macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
5739 	  freg = reg;
5740 	}
5741     }
5742   else
5743     {
5744       expressionS mid16;
5745 
5746       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
5747 	{
5748 	  macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5749 	  macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
5750 	  return;
5751 	}
5752 
5753       if (freg != 0)
5754 	{
5755 	  macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
5756 	  freg = reg;
5757 	}
5758       mid16 = lo32;
5759       mid16.X_add_number >>= 16;
5760       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5761       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5762       freg = reg;
5763     }
5764   if ((lo32.X_add_number & 0xffff) != 0)
5765     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5766 }
5767 
5768 static inline void
5769 load_delay_nop (void)
5770 {
5771   if (!gpr_interlocks)
5772     macro_build (NULL, "nop", "");
5773 }
5774 
5775 /* Load an address into a register.  */
5776 
5777 static void
5778 load_address (int reg, expressionS *ep, int *used_at)
5779 {
5780   if (ep->X_op != O_constant
5781       && ep->X_op != O_symbol)
5782     {
5783       as_bad (_("expression too complex"));
5784       ep->X_op = O_constant;
5785     }
5786 
5787   if (ep->X_op == O_constant)
5788     {
5789       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
5790       return;
5791     }
5792 
5793   if (mips_pic == NO_PIC)
5794     {
5795       /* If this is a reference to a GP relative symbol, we want
5796 	   addiu	$reg,$gp,<sym>		(BFD_RELOC_GPREL16)
5797 	 Otherwise we want
5798 	   lui		$reg,<sym>		(BFD_RELOC_HI16_S)
5799 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
5800 	 If we have an addend, we always use the latter form.
5801 
5802 	 With 64bit address space and a usable $at we want
5803 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
5804 	   lui		$at,<sym>		(BFD_RELOC_HI16_S)
5805 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
5806 	   daddiu	$at,<sym>		(BFD_RELOC_LO16)
5807 	   dsll32	$reg,0
5808 	   daddu	$reg,$reg,$at
5809 
5810 	 If $at is already in use, we use a path which is suboptimal
5811 	 on superscalar processors.
5812 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
5813 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
5814 	   dsll		$reg,16
5815 	   daddiu	$reg,<sym>		(BFD_RELOC_HI16_S)
5816 	   dsll		$reg,16
5817 	   daddiu	$reg,<sym>		(BFD_RELOC_LO16)
5818 
5819 	 For GP relative symbols in 64bit address space we can use
5820 	 the same sequence as in 32bit address space.  */
5821       if (HAVE_64BIT_SYMBOLS)
5822 	{
5823 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5824 	      && !nopic_need_relax (ep->X_add_symbol, 1))
5825 	    {
5826 	      relax_start (ep->X_add_symbol);
5827 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5828 			   mips_gp_register, BFD_RELOC_GPREL16);
5829 	      relax_switch ();
5830 	    }
5831 
5832 	  if (*used_at == 0 && mips_opts.at)
5833 	    {
5834 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5835 	      macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
5836 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
5837 			   BFD_RELOC_MIPS_HIGHER);
5838 	      macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
5839 	      macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
5840 	      macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
5841 	      *used_at = 1;
5842 	    }
5843 	  else
5844 	    {
5845 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5846 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
5847 			   BFD_RELOC_MIPS_HIGHER);
5848 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5849 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
5850 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5851 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
5852 	    }
5853 
5854 	  if (mips_relax.sequence)
5855 	    relax_end ();
5856 	}
5857       else
5858 	{
5859 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5860 	      && !nopic_need_relax (ep->X_add_symbol, 1))
5861 	    {
5862 	      relax_start (ep->X_add_symbol);
5863 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5864 			   mips_gp_register, BFD_RELOC_GPREL16);
5865 	      relax_switch ();
5866 	    }
5867 	  macro_build_lui (ep, reg);
5868 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5869 		       reg, reg, BFD_RELOC_LO16);
5870 	  if (mips_relax.sequence)
5871 	    relax_end ();
5872 	}
5873     }
5874   else if (!mips_big_got)
5875     {
5876       expressionS ex;
5877 
5878       /* If this is a reference to an external symbol, we want
5879 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
5880 	 Otherwise we want
5881 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
5882 	   nop
5883 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
5884 	 If there is a constant, it must be added in after.
5885 
5886 	 If we have NewABI, we want
5887 	   lw		$reg,<sym+cst>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
5888          unless we're referencing a global symbol with a non-zero
5889          offset, in which case cst must be added separately.  */
5890       if (HAVE_NEWABI)
5891 	{
5892 	  if (ep->X_add_number)
5893 	    {
5894 	      ex.X_add_number = ep->X_add_number;
5895 	      ep->X_add_number = 0;
5896 	      relax_start (ep->X_add_symbol);
5897 	      macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5898 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5899 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5900 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5901 	      ex.X_op = O_constant;
5902 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5903 			   reg, reg, BFD_RELOC_LO16);
5904 	      ep->X_add_number = ex.X_add_number;
5905 	      relax_switch ();
5906 	    }
5907 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5908 		       BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5909 	  if (mips_relax.sequence)
5910 	    relax_end ();
5911 	}
5912       else
5913 	{
5914 	  ex.X_add_number = ep->X_add_number;
5915 	  ep->X_add_number = 0;
5916 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5917 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
5918 	  load_delay_nop ();
5919 	  relax_start (ep->X_add_symbol);
5920 	  relax_switch ();
5921 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5922 		       BFD_RELOC_LO16);
5923 	  relax_end ();
5924 
5925 	  if (ex.X_add_number != 0)
5926 	    {
5927 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5928 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5929 	      ex.X_op = O_constant;
5930 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5931 			   reg, reg, BFD_RELOC_LO16);
5932 	    }
5933 	}
5934     }
5935   else if (mips_big_got)
5936     {
5937       expressionS ex;
5938 
5939       /* This is the large GOT case.  If this is a reference to an
5940 	 external symbol, we want
5941 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
5942 	   addu		$reg,$reg,$gp
5943 	   lw		$reg,<sym>($reg)	(BFD_RELOC_MIPS_GOT_LO16)
5944 
5945 	 Otherwise, for a reference to a local symbol in old ABI, we want
5946 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
5947 	   nop
5948 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
5949 	 If there is a constant, it must be added in after.
5950 
5951 	 In the NewABI, for local symbols, with or without offsets, we want:
5952 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
5953 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
5954       */
5955       if (HAVE_NEWABI)
5956 	{
5957 	  ex.X_add_number = ep->X_add_number;
5958 	  ep->X_add_number = 0;
5959 	  relax_start (ep->X_add_symbol);
5960 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
5961 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5962 		       reg, reg, mips_gp_register);
5963 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5964 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
5965 	  if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5966 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5967 	  else if (ex.X_add_number)
5968 	    {
5969 	      ex.X_op = O_constant;
5970 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5971 			   BFD_RELOC_LO16);
5972 	    }
5973 
5974 	  ep->X_add_number = ex.X_add_number;
5975 	  relax_switch ();
5976 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5977 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5978 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5979 		       BFD_RELOC_MIPS_GOT_OFST);
5980 	  relax_end ();
5981 	}
5982       else
5983 	{
5984 	  ex.X_add_number = ep->X_add_number;
5985 	  ep->X_add_number = 0;
5986 	  relax_start (ep->X_add_symbol);
5987 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
5988 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5989 		       reg, reg, mips_gp_register);
5990 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5991 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
5992 	  relax_switch ();
5993 	  if (reg_needs_delay (mips_gp_register))
5994 	    {
5995 	      /* We need a nop before loading from $gp.  This special
5996 		 check is required because the lui which starts the main
5997 		 instruction stream does not refer to $gp, and so will not
5998 		 insert the nop which may be required.  */
5999 	      macro_build (NULL, "nop", "");
6000 	    }
6001 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6002 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
6003 	  load_delay_nop ();
6004 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6005 		       BFD_RELOC_LO16);
6006 	  relax_end ();
6007 
6008 	  if (ex.X_add_number != 0)
6009 	    {
6010 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6011 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6012 	      ex.X_op = O_constant;
6013 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6014 			   BFD_RELOC_LO16);
6015 	    }
6016 	}
6017     }
6018   else
6019     abort ();
6020 
6021   if (!mips_opts.at && *used_at == 1)
6022     as_bad (_("Macro used $at after \".set noat\""));
6023 }
6024 
6025 /* Move the contents of register SOURCE into register DEST.  */
6026 
6027 static void
6028 move_register (int dest, int source)
6029 {
6030   /* Prefer to use a 16-bit microMIPS instruction unless the previous
6031      instruction specifically requires a 32-bit one.  */
6032   if (mips_opts.micromips
6033       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
6034     macro_build (NULL, "move", "mp,mj", dest, source);
6035   else
6036     macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6037 		 dest, source, 0);
6038 }
6039 
6040 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
6041    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6042    The two alternatives are:
6043 
6044    Global symbol		Local sybmol
6045    -------------		------------
6046    lw DEST,%got(SYMBOL)		lw DEST,%got(SYMBOL + OFFSET)
6047    ...				...
6048    addiu DEST,DEST,OFFSET	addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6049 
6050    load_got_offset emits the first instruction and add_got_offset
6051    emits the second for a 16-bit offset or add_got_offset_hilo emits
6052    a sequence to add a 32-bit offset using a scratch register.  */
6053 
6054 static void
6055 load_got_offset (int dest, expressionS *local)
6056 {
6057   expressionS global;
6058 
6059   global = *local;
6060   global.X_add_number = 0;
6061 
6062   relax_start (local->X_add_symbol);
6063   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6064 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
6065   relax_switch ();
6066   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6067 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
6068   relax_end ();
6069 }
6070 
6071 static void
6072 add_got_offset (int dest, expressionS *local)
6073 {
6074   expressionS global;
6075 
6076   global.X_op = O_constant;
6077   global.X_op_symbol = NULL;
6078   global.X_add_symbol = NULL;
6079   global.X_add_number = local->X_add_number;
6080 
6081   relax_start (local->X_add_symbol);
6082   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6083 	       dest, dest, BFD_RELOC_LO16);
6084   relax_switch ();
6085   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6086   relax_end ();
6087 }
6088 
6089 static void
6090 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6091 {
6092   expressionS global;
6093   int hold_mips_optimize;
6094 
6095   global.X_op = O_constant;
6096   global.X_op_symbol = NULL;
6097   global.X_add_symbol = NULL;
6098   global.X_add_number = local->X_add_number;
6099 
6100   relax_start (local->X_add_symbol);
6101   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6102   relax_switch ();
6103   /* Set mips_optimize around the lui instruction to avoid
6104      inserting an unnecessary nop after the lw.  */
6105   hold_mips_optimize = mips_optimize;
6106   mips_optimize = 2;
6107   macro_build_lui (&global, tmp);
6108   mips_optimize = hold_mips_optimize;
6109   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6110   relax_end ();
6111 
6112   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6113 }
6114 
6115 /* Emit a sequence of instructions to emulate a branch likely operation.
6116    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
6117    is its complementing branch with the original condition negated.
6118    CALL is set if the original branch specified the link operation.
6119    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6120 
6121    Code like this is produced in the noreorder mode:
6122 
6123 	BRNEG	<args>, 1f
6124 	 nop
6125 	b	<sym>
6126 	 delay slot (executed only if branch taken)
6127     1:
6128 
6129    or, if CALL is set:
6130 
6131 	BRNEG	<args>, 1f
6132 	 nop
6133 	bal	<sym>
6134 	 delay slot (executed only if branch taken)
6135     1:
6136 
6137    In the reorder mode the delay slot would be filled with a nop anyway,
6138    so code produced is simply:
6139 
6140 	BR	<args>, <sym>
6141 	 nop
6142 
6143    This function is used when producing code for the microMIPS ASE that
6144    does not implement branch likely instructions in hardware.  */
6145 
6146 static void
6147 macro_build_branch_likely (const char *br, const char *brneg,
6148 			   int call, expressionS *ep, const char *fmt,
6149 			   unsigned int sreg, unsigned int treg)
6150 {
6151   int noreorder = mips_opts.noreorder;
6152   expressionS expr1;
6153 
6154   gas_assert (mips_opts.micromips);
6155   start_noreorder ();
6156   if (noreorder)
6157     {
6158       micromips_label_expr (&expr1);
6159       macro_build (&expr1, brneg, fmt, sreg, treg);
6160       macro_build (NULL, "nop", "");
6161       macro_build (ep, call ? "bal" : "b", "p");
6162 
6163       /* Set to true so that append_insn adds a label.  */
6164       emit_branch_likely_macro = TRUE;
6165     }
6166   else
6167     {
6168       macro_build (ep, br, fmt, sreg, treg);
6169       macro_build (NULL, "nop", "");
6170     }
6171   end_noreorder ();
6172 }
6173 
6174 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6175    the condition code tested.  EP specifies the branch target.  */
6176 
6177 static void
6178 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6179 {
6180   const int call = 0;
6181   const char *brneg;
6182   const char *br;
6183 
6184   switch (type)
6185     {
6186     case M_BC1FL:
6187       br = "bc1f";
6188       brneg = "bc1t";
6189       break;
6190     case M_BC1TL:
6191       br = "bc1t";
6192       brneg = "bc1f";
6193       break;
6194     case M_BC2FL:
6195       br = "bc2f";
6196       brneg = "bc2t";
6197       break;
6198     case M_BC2TL:
6199       br = "bc2t";
6200       brneg = "bc2f";
6201       break;
6202     default:
6203       abort ();
6204     }
6205   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6206 }
6207 
6208 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6209    the register tested.  EP specifies the branch target.  */
6210 
6211 static void
6212 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6213 {
6214   const char *brneg = NULL;
6215   const char *br;
6216   int call = 0;
6217 
6218   switch (type)
6219     {
6220     case M_BGEZ:
6221       br = "bgez";
6222       break;
6223     case M_BGEZL:
6224       br = mips_opts.micromips ? "bgez" : "bgezl";
6225       brneg = "bltz";
6226       break;
6227     case M_BGEZALL:
6228       gas_assert (mips_opts.micromips);
6229       br = "bgezals";
6230       brneg = "bltz";
6231       call = 1;
6232       break;
6233     case M_BGTZ:
6234       br = "bgtz";
6235       break;
6236     case M_BGTZL:
6237       br = mips_opts.micromips ? "bgtz" : "bgtzl";
6238       brneg = "blez";
6239       break;
6240     case M_BLEZ:
6241       br = "blez";
6242       break;
6243     case M_BLEZL:
6244       br = mips_opts.micromips ? "blez" : "blezl";
6245       brneg = "bgtz";
6246       break;
6247     case M_BLTZ:
6248       br = "bltz";
6249       break;
6250     case M_BLTZL:
6251       br = mips_opts.micromips ? "bltz" : "bltzl";
6252       brneg = "bgez";
6253       break;
6254     case M_BLTZALL:
6255       gas_assert (mips_opts.micromips);
6256       br = "bltzals";
6257       brneg = "bgez";
6258       call = 1;
6259       break;
6260     default:
6261       abort ();
6262     }
6263   if (mips_opts.micromips && brneg)
6264     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6265   else
6266     macro_build (ep, br, "s,p", sreg);
6267 }
6268 
6269 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6270    TREG as the registers tested.  EP specifies the branch target.  */
6271 
6272 static void
6273 macro_build_branch_rsrt (int type, expressionS *ep,
6274 			 unsigned int sreg, unsigned int treg)
6275 {
6276   const char *brneg = NULL;
6277   const int call = 0;
6278   const char *br;
6279 
6280   switch (type)
6281     {
6282     case M_BEQ:
6283     case M_BEQ_I:
6284       br = "beq";
6285       break;
6286     case M_BEQL:
6287     case M_BEQL_I:
6288       br = mips_opts.micromips ? "beq" : "beql";
6289       brneg = "bne";
6290       break;
6291     case M_BNE:
6292     case M_BNE_I:
6293       br = "bne";
6294       break;
6295     case M_BNEL:
6296     case M_BNEL_I:
6297       br = mips_opts.micromips ? "bne" : "bnel";
6298       brneg = "beq";
6299       break;
6300     default:
6301       abort ();
6302     }
6303   if (mips_opts.micromips && brneg)
6304     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6305   else
6306     macro_build (ep, br, "s,t,p", sreg, treg);
6307 }
6308 
6309 /*
6310  *			Build macros
6311  *   This routine implements the seemingly endless macro or synthesized
6312  * instructions and addressing modes in the mips assembly language. Many
6313  * of these macros are simple and are similar to each other. These could
6314  * probably be handled by some kind of table or grammar approach instead of
6315  * this verbose method. Others are not simple macros but are more like
6316  * optimizing code generation.
6317  *   One interesting optimization is when several store macros appear
6318  * consecutively that would load AT with the upper half of the same address.
6319  * The ensuing load upper instructions are ommited. This implies some kind
6320  * of global optimization. We currently only optimize within a single macro.
6321  *   For many of the load and store macros if the address is specified as a
6322  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6323  * first load register 'at' with zero and use it as the base register. The
6324  * mips assembler simply uses register $zero. Just one tiny optimization
6325  * we're missing.
6326  */
6327 static void
6328 macro (struct mips_cl_insn *ip)
6329 {
6330   unsigned int treg, sreg, dreg, breg;
6331   unsigned int tempreg;
6332   int mask;
6333   int used_at = 0;
6334   expressionS label_expr;
6335   expressionS expr1;
6336   expressionS *ep;
6337   const char *s;
6338   const char *s2;
6339   const char *fmt;
6340   int likely = 0;
6341   int coproc = 0;
6342   int off12 = 0;
6343   int call = 0;
6344   int jals = 0;
6345   int dbl = 0;
6346   int imm = 0;
6347   int ust = 0;
6348   int lp = 0;
6349   int ab = 0;
6350   int off0 = 0;
6351   int off;
6352   offsetT maxnum;
6353   bfd_reloc_code_real_type r;
6354   int hold_mips_optimize;
6355 
6356   gas_assert (! mips_opts.mips16);
6357 
6358   treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6359   dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6360   sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6361   mask = ip->insn_mo->mask;
6362 
6363   label_expr.X_op = O_constant;
6364   label_expr.X_op_symbol = NULL;
6365   label_expr.X_add_symbol = NULL;
6366   label_expr.X_add_number = 0;
6367 
6368   expr1.X_op = O_constant;
6369   expr1.X_op_symbol = NULL;
6370   expr1.X_add_symbol = NULL;
6371   expr1.X_add_number = 1;
6372 
6373   switch (mask)
6374     {
6375     case M_DABS:
6376       dbl = 1;
6377     case M_ABS:
6378       /*    bgez    $a0,1f
6379 	    move    v0,$a0
6380 	    sub     v0,$zero,$a0
6381 	 1:
6382        */
6383 
6384       start_noreorder ();
6385 
6386       if (mips_opts.micromips)
6387 	micromips_label_expr (&label_expr);
6388       else
6389 	label_expr.X_add_number = 8;
6390       macro_build (&label_expr, "bgez", "s,p", sreg);
6391       if (dreg == sreg)
6392 	macro_build (NULL, "nop", "");
6393       else
6394 	move_register (dreg, sreg);
6395       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6396       if (mips_opts.micromips)
6397 	micromips_add_label ();
6398 
6399       end_noreorder ();
6400       break;
6401 
6402     case M_ADD_I:
6403       s = "addi";
6404       s2 = "add";
6405       goto do_addi;
6406     case M_ADDU_I:
6407       s = "addiu";
6408       s2 = "addu";
6409       goto do_addi;
6410     case M_DADD_I:
6411       dbl = 1;
6412       s = "daddi";
6413       s2 = "dadd";
6414       if (!mips_opts.micromips)
6415 	goto do_addi;
6416       if (imm_expr.X_op == O_constant
6417 	  && imm_expr.X_add_number >= -0x200
6418 	  && imm_expr.X_add_number < 0x200)
6419 	{
6420 	  macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6421 	  break;
6422 	}
6423       goto do_addi_i;
6424     case M_DADDU_I:
6425       dbl = 1;
6426       s = "daddiu";
6427       s2 = "daddu";
6428     do_addi:
6429       if (imm_expr.X_op == O_constant
6430 	  && imm_expr.X_add_number >= -0x8000
6431 	  && imm_expr.X_add_number < 0x8000)
6432 	{
6433 	  macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6434 	  break;
6435 	}
6436     do_addi_i:
6437       used_at = 1;
6438       load_register (AT, &imm_expr, dbl);
6439       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6440       break;
6441 
6442     case M_AND_I:
6443       s = "andi";
6444       s2 = "and";
6445       goto do_bit;
6446     case M_OR_I:
6447       s = "ori";
6448       s2 = "or";
6449       goto do_bit;
6450     case M_NOR_I:
6451       s = "";
6452       s2 = "nor";
6453       goto do_bit;
6454     case M_XOR_I:
6455       s = "xori";
6456       s2 = "xor";
6457     do_bit:
6458       if (imm_expr.X_op == O_constant
6459 	  && imm_expr.X_add_number >= 0
6460 	  && imm_expr.X_add_number < 0x10000)
6461 	{
6462 	  if (mask != M_NOR_I)
6463 	    macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6464 	  else
6465 	    {
6466 	      macro_build (&imm_expr, "ori", "t,r,i",
6467 			   treg, sreg, BFD_RELOC_LO16);
6468 	      macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6469 	    }
6470 	  break;
6471 	}
6472 
6473       used_at = 1;
6474       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6475       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6476       break;
6477 
6478     case M_BALIGN:
6479       switch (imm_expr.X_add_number)
6480 	{
6481 	case 0:
6482 	  macro_build (NULL, "nop", "");
6483 	  break;
6484 	case 2:
6485 	  macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6486 	  break;
6487 	case 1:
6488 	case 3:
6489 	  macro_build (NULL, "balign", "t,s,2", treg, sreg,
6490 		       (int) imm_expr.X_add_number);
6491 	  break;
6492 	default:
6493 	  as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6494 		  (unsigned long) imm_expr.X_add_number);
6495 	  break;
6496 	}
6497       break;
6498 
6499     case M_BC1FL:
6500     case M_BC1TL:
6501     case M_BC2FL:
6502     case M_BC2TL:
6503       gas_assert (mips_opts.micromips);
6504       macro_build_branch_ccl (mask, &offset_expr,
6505 			      EXTRACT_OPERAND (1, BCC, *ip));
6506       break;
6507 
6508     case M_BEQ_I:
6509     case M_BEQL_I:
6510     case M_BNE_I:
6511     case M_BNEL_I:
6512       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6513 	treg = 0;
6514       else
6515 	{
6516 	  treg = AT;
6517 	  used_at = 1;
6518 	  load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
6519 	}
6520       /* Fall through.  */
6521     case M_BEQL:
6522     case M_BNEL:
6523       macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
6524       break;
6525 
6526     case M_BGEL:
6527       likely = 1;
6528     case M_BGE:
6529       if (treg == 0)
6530 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6531       else if (sreg == 0)
6532 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6533       else
6534 	{
6535 	  used_at = 1;
6536 	  macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6537 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6538 				   &offset_expr, AT, ZERO);
6539 	}
6540       break;
6541 
6542     case M_BGEZL:
6543     case M_BGEZALL:
6544     case M_BGTZL:
6545     case M_BLEZL:
6546     case M_BLTZL:
6547     case M_BLTZALL:
6548       macro_build_branch_rs (mask, &offset_expr, sreg);
6549       break;
6550 
6551     case M_BGTL_I:
6552       likely = 1;
6553     case M_BGT_I:
6554       /* Check for > max integer.  */
6555       maxnum = 0x7fffffff;
6556       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6557 	{
6558 	  maxnum <<= 16;
6559 	  maxnum |= 0xffff;
6560 	  maxnum <<= 16;
6561 	  maxnum |= 0xffff;
6562 	}
6563       if (imm_expr.X_op == O_constant
6564 	  && imm_expr.X_add_number >= maxnum
6565 	  && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6566 	{
6567 	do_false:
6568 	  /* Result is always false.  */
6569 	  if (! likely)
6570 	    macro_build (NULL, "nop", "");
6571 	  else
6572 	    macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
6573 	  break;
6574 	}
6575       if (imm_expr.X_op != O_constant)
6576 	as_bad (_("Unsupported large constant"));
6577       ++imm_expr.X_add_number;
6578       /* FALLTHROUGH */
6579     case M_BGE_I:
6580     case M_BGEL_I:
6581       if (mask == M_BGEL_I)
6582 	likely = 1;
6583       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6584 	{
6585 	  macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6586 				 &offset_expr, sreg);
6587 	  break;
6588 	}
6589       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6590 	{
6591 	  macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6592 				 &offset_expr, sreg);
6593 	  break;
6594 	}
6595       maxnum = 0x7fffffff;
6596       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6597 	{
6598 	  maxnum <<= 16;
6599 	  maxnum |= 0xffff;
6600 	  maxnum <<= 16;
6601 	  maxnum |= 0xffff;
6602 	}
6603       maxnum = - maxnum - 1;
6604       if (imm_expr.X_op == O_constant
6605 	  && imm_expr.X_add_number <= maxnum
6606 	  && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6607 	{
6608 	do_true:
6609 	  /* result is always true */
6610 	  as_warn (_("Branch %s is always true"), ip->insn_mo->name);
6611 	  macro_build (&offset_expr, "b", "p");
6612 	  break;
6613 	}
6614       used_at = 1;
6615       set_at (sreg, 0);
6616       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6617 			       &offset_expr, AT, ZERO);
6618       break;
6619 
6620     case M_BGEUL:
6621       likely = 1;
6622     case M_BGEU:
6623       if (treg == 0)
6624 	goto do_true;
6625       else if (sreg == 0)
6626 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6627 				 &offset_expr, ZERO, treg);
6628       else
6629 	{
6630 	  used_at = 1;
6631 	  macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6632 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6633 				   &offset_expr, AT, ZERO);
6634 	}
6635       break;
6636 
6637     case M_BGTUL_I:
6638       likely = 1;
6639     case M_BGTU_I:
6640       if (sreg == 0
6641 	  || (HAVE_32BIT_GPRS
6642 	      && imm_expr.X_op == O_constant
6643 	      && imm_expr.X_add_number == -1))
6644 	goto do_false;
6645       if (imm_expr.X_op != O_constant)
6646 	as_bad (_("Unsupported large constant"));
6647       ++imm_expr.X_add_number;
6648       /* FALLTHROUGH */
6649     case M_BGEU_I:
6650     case M_BGEUL_I:
6651       if (mask == M_BGEUL_I)
6652 	likely = 1;
6653       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6654 	goto do_true;
6655       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6656 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6657 				 &offset_expr, sreg, ZERO);
6658       else
6659 	{
6660 	  used_at = 1;
6661 	  set_at (sreg, 1);
6662 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6663 				   &offset_expr, AT, ZERO);
6664 	}
6665       break;
6666 
6667     case M_BGTL:
6668       likely = 1;
6669     case M_BGT:
6670       if (treg == 0)
6671 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6672       else if (sreg == 0)
6673 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6674       else
6675 	{
6676 	  used_at = 1;
6677 	  macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6678 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6679 				   &offset_expr, AT, ZERO);
6680 	}
6681       break;
6682 
6683     case M_BGTUL:
6684       likely = 1;
6685     case M_BGTU:
6686       if (treg == 0)
6687 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6688 				 &offset_expr, sreg, ZERO);
6689       else if (sreg == 0)
6690 	goto do_false;
6691       else
6692 	{
6693 	  used_at = 1;
6694 	  macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6695 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6696 				   &offset_expr, AT, ZERO);
6697 	}
6698       break;
6699 
6700     case M_BLEL:
6701       likely = 1;
6702     case M_BLE:
6703       if (treg == 0)
6704 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6705       else if (sreg == 0)
6706 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6707       else
6708 	{
6709 	  used_at = 1;
6710 	  macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6711 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6712 				   &offset_expr, AT, ZERO);
6713 	}
6714       break;
6715 
6716     case M_BLEL_I:
6717       likely = 1;
6718     case M_BLE_I:
6719       maxnum = 0x7fffffff;
6720       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6721 	{
6722 	  maxnum <<= 16;
6723 	  maxnum |= 0xffff;
6724 	  maxnum <<= 16;
6725 	  maxnum |= 0xffff;
6726 	}
6727       if (imm_expr.X_op == O_constant
6728 	  && imm_expr.X_add_number >= maxnum
6729 	  && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6730 	goto do_true;
6731       if (imm_expr.X_op != O_constant)
6732 	as_bad (_("Unsupported large constant"));
6733       ++imm_expr.X_add_number;
6734       /* FALLTHROUGH */
6735     case M_BLT_I:
6736     case M_BLTL_I:
6737       if (mask == M_BLTL_I)
6738 	likely = 1;
6739       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6740 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6741       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6742 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6743       else
6744 	{
6745 	  used_at = 1;
6746 	  set_at (sreg, 0);
6747 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6748 				   &offset_expr, AT, ZERO);
6749 	}
6750       break;
6751 
6752     case M_BLEUL:
6753       likely = 1;
6754     case M_BLEU:
6755       if (treg == 0)
6756 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6757 				 &offset_expr, sreg, ZERO);
6758       else if (sreg == 0)
6759 	goto do_true;
6760       else
6761 	{
6762 	  used_at = 1;
6763 	  macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6764 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6765 				   &offset_expr, AT, ZERO);
6766 	}
6767       break;
6768 
6769     case M_BLEUL_I:
6770       likely = 1;
6771     case M_BLEU_I:
6772       if (sreg == 0
6773 	  || (HAVE_32BIT_GPRS
6774 	      && imm_expr.X_op == O_constant
6775 	      && imm_expr.X_add_number == -1))
6776 	goto do_true;
6777       if (imm_expr.X_op != O_constant)
6778 	as_bad (_("Unsupported large constant"));
6779       ++imm_expr.X_add_number;
6780       /* FALLTHROUGH */
6781     case M_BLTU_I:
6782     case M_BLTUL_I:
6783       if (mask == M_BLTUL_I)
6784 	likely = 1;
6785       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6786 	goto do_false;
6787       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6788 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6789 				 &offset_expr, sreg, ZERO);
6790       else
6791 	{
6792 	  used_at = 1;
6793 	  set_at (sreg, 1);
6794 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6795 				   &offset_expr, AT, ZERO);
6796 	}
6797       break;
6798 
6799     case M_BLTL:
6800       likely = 1;
6801     case M_BLT:
6802       if (treg == 0)
6803 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6804       else if (sreg == 0)
6805 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6806       else
6807 	{
6808 	  used_at = 1;
6809 	  macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6810 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6811 				   &offset_expr, AT, ZERO);
6812 	}
6813       break;
6814 
6815     case M_BLTUL:
6816       likely = 1;
6817     case M_BLTU:
6818       if (treg == 0)
6819 	goto do_false;
6820       else if (sreg == 0)
6821 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6822 				 &offset_expr, ZERO, treg);
6823       else
6824 	{
6825 	  used_at = 1;
6826 	  macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6827 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6828 				   &offset_expr, AT, ZERO);
6829 	}
6830       break;
6831 
6832     case M_DEXT:
6833       {
6834 	/* Use unsigned arithmetic.  */
6835 	addressT pos;
6836 	addressT size;
6837 
6838 	if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6839 	  {
6840 	    as_bad (_("Unsupported large constant"));
6841 	    pos = size = 1;
6842 	  }
6843 	else
6844 	  {
6845 	    pos = imm_expr.X_add_number;
6846 	    size = imm2_expr.X_add_number;
6847 	  }
6848 
6849 	if (pos > 63)
6850 	  {
6851 	    as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6852 	    pos = 1;
6853 	  }
6854 	if (size == 0 || size > 64 || (pos + size - 1) > 63)
6855 	  {
6856 	    as_bad (_("Improper extract size (%lu, position %lu)"),
6857 		    (unsigned long) size, (unsigned long) pos);
6858 	    size = 1;
6859 	  }
6860 
6861 	if (size <= 32 && pos < 32)
6862 	  {
6863 	    s = "dext";
6864 	    fmt = "t,r,+A,+C";
6865 	  }
6866 	else if (size <= 32)
6867 	  {
6868 	    s = "dextu";
6869 	    fmt = "t,r,+E,+H";
6870 	  }
6871 	else
6872 	  {
6873 	    s = "dextm";
6874 	    fmt = "t,r,+A,+G";
6875 	  }
6876 	macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6877 		     (int) (size - 1));
6878       }
6879       break;
6880 
6881     case M_DINS:
6882       {
6883 	/* Use unsigned arithmetic.  */
6884 	addressT pos;
6885 	addressT size;
6886 
6887 	if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6888 	  {
6889 	    as_bad (_("Unsupported large constant"));
6890 	    pos = size = 1;
6891 	  }
6892 	else
6893 	  {
6894 	    pos = imm_expr.X_add_number;
6895 	    size = imm2_expr.X_add_number;
6896 	  }
6897 
6898 	if (pos > 63)
6899 	  {
6900 	    as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6901 	    pos = 1;
6902 	  }
6903 	if (size == 0 || size > 64 || (pos + size - 1) > 63)
6904 	  {
6905 	    as_bad (_("Improper insert size (%lu, position %lu)"),
6906 		    (unsigned long) size, (unsigned long) pos);
6907 	    size = 1;
6908 	  }
6909 
6910 	if (pos < 32 && (pos + size - 1) < 32)
6911 	  {
6912 	    s = "dins";
6913 	    fmt = "t,r,+A,+B";
6914 	  }
6915 	else if (pos >= 32)
6916 	  {
6917 	    s = "dinsu";
6918 	    fmt = "t,r,+E,+F";
6919 	  }
6920 	else
6921 	  {
6922 	    s = "dinsm";
6923 	    fmt = "t,r,+A,+F";
6924 	  }
6925 	macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6926 		     (int) (pos + size - 1));
6927       }
6928       break;
6929 
6930     case M_DDIV_3:
6931       dbl = 1;
6932     case M_DIV_3:
6933       s = "mflo";
6934       goto do_div3;
6935     case M_DREM_3:
6936       dbl = 1;
6937     case M_REM_3:
6938       s = "mfhi";
6939     do_div3:
6940       if (treg == 0)
6941 	{
6942 	  as_warn (_("Divide by zero."));
6943 	  if (mips_trap)
6944 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
6945 	  else
6946 	    macro_build (NULL, "break", BRK_FMT, 7);
6947 	  break;
6948 	}
6949 
6950       start_noreorder ();
6951       if (mips_trap)
6952 	{
6953 	  macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
6954 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
6955 	}
6956       else
6957 	{
6958 	  if (mips_opts.micromips)
6959 	    micromips_label_expr (&label_expr);
6960 	  else
6961 	    label_expr.X_add_number = 8;
6962 	  macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
6963 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
6964 	  macro_build (NULL, "break", BRK_FMT, 7);
6965 	  if (mips_opts.micromips)
6966 	    micromips_add_label ();
6967 	}
6968       expr1.X_add_number = -1;
6969       used_at = 1;
6970       load_register (AT, &expr1, dbl);
6971       if (mips_opts.micromips)
6972 	micromips_label_expr (&label_expr);
6973       else
6974 	label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
6975       macro_build (&label_expr, "bne", "s,t,p", treg, AT);
6976       if (dbl)
6977 	{
6978 	  expr1.X_add_number = 1;
6979 	  load_register (AT, &expr1, dbl);
6980 	  macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
6981 	}
6982       else
6983 	{
6984 	  expr1.X_add_number = 0x80000000;
6985 	  macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
6986 	}
6987       if (mips_trap)
6988 	{
6989 	  macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
6990 	  /* We want to close the noreorder block as soon as possible, so
6991 	     that later insns are available for delay slot filling.  */
6992 	  end_noreorder ();
6993 	}
6994       else
6995 	{
6996 	  if (mips_opts.micromips)
6997 	    micromips_label_expr (&label_expr);
6998 	  else
6999 	    label_expr.X_add_number = 8;
7000 	  macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
7001 	  macro_build (NULL, "nop", "");
7002 
7003 	  /* We want to close the noreorder block as soon as possible, so
7004 	     that later insns are available for delay slot filling.  */
7005 	  end_noreorder ();
7006 
7007 	  macro_build (NULL, "break", BRK_FMT, 6);
7008 	}
7009       if (mips_opts.micromips)
7010 	micromips_add_label ();
7011       macro_build (NULL, s, MFHL_FMT, dreg);
7012       break;
7013 
7014     case M_DIV_3I:
7015       s = "div";
7016       s2 = "mflo";
7017       goto do_divi;
7018     case M_DIVU_3I:
7019       s = "divu";
7020       s2 = "mflo";
7021       goto do_divi;
7022     case M_REM_3I:
7023       s = "div";
7024       s2 = "mfhi";
7025       goto do_divi;
7026     case M_REMU_3I:
7027       s = "divu";
7028       s2 = "mfhi";
7029       goto do_divi;
7030     case M_DDIV_3I:
7031       dbl = 1;
7032       s = "ddiv";
7033       s2 = "mflo";
7034       goto do_divi;
7035     case M_DDIVU_3I:
7036       dbl = 1;
7037       s = "ddivu";
7038       s2 = "mflo";
7039       goto do_divi;
7040     case M_DREM_3I:
7041       dbl = 1;
7042       s = "ddiv";
7043       s2 = "mfhi";
7044       goto do_divi;
7045     case M_DREMU_3I:
7046       dbl = 1;
7047       s = "ddivu";
7048       s2 = "mfhi";
7049     do_divi:
7050       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7051 	{
7052 	  as_warn (_("Divide by zero."));
7053 	  if (mips_trap)
7054 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
7055 	  else
7056 	    macro_build (NULL, "break", BRK_FMT, 7);
7057 	  break;
7058 	}
7059       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7060 	{
7061 	  if (strcmp (s2, "mflo") == 0)
7062 	    move_register (dreg, sreg);
7063 	  else
7064 	    move_register (dreg, ZERO);
7065 	  break;
7066 	}
7067       if (imm_expr.X_op == O_constant
7068 	  && imm_expr.X_add_number == -1
7069 	  && s[strlen (s) - 1] != 'u')
7070 	{
7071 	  if (strcmp (s2, "mflo") == 0)
7072 	    {
7073 	      macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
7074 	    }
7075 	  else
7076 	    move_register (dreg, ZERO);
7077 	  break;
7078 	}
7079 
7080       used_at = 1;
7081       load_register (AT, &imm_expr, dbl);
7082       macro_build (NULL, s, "z,s,t", sreg, AT);
7083       macro_build (NULL, s2, MFHL_FMT, dreg);
7084       break;
7085 
7086     case M_DIVU_3:
7087       s = "divu";
7088       s2 = "mflo";
7089       goto do_divu3;
7090     case M_REMU_3:
7091       s = "divu";
7092       s2 = "mfhi";
7093       goto do_divu3;
7094     case M_DDIVU_3:
7095       s = "ddivu";
7096       s2 = "mflo";
7097       goto do_divu3;
7098     case M_DREMU_3:
7099       s = "ddivu";
7100       s2 = "mfhi";
7101     do_divu3:
7102       start_noreorder ();
7103       if (mips_trap)
7104 	{
7105 	  macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7106 	  macro_build (NULL, s, "z,s,t", sreg, treg);
7107 	  /* We want to close the noreorder block as soon as possible, so
7108 	     that later insns are available for delay slot filling.  */
7109 	  end_noreorder ();
7110 	}
7111       else
7112 	{
7113 	  if (mips_opts.micromips)
7114 	    micromips_label_expr (&label_expr);
7115 	  else
7116 	    label_expr.X_add_number = 8;
7117 	  macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7118 	  macro_build (NULL, s, "z,s,t", sreg, treg);
7119 
7120 	  /* We want to close the noreorder block as soon as possible, so
7121 	     that later insns are available for delay slot filling.  */
7122 	  end_noreorder ();
7123 	  macro_build (NULL, "break", BRK_FMT, 7);
7124 	  if (mips_opts.micromips)
7125 	    micromips_add_label ();
7126 	}
7127       macro_build (NULL, s2, MFHL_FMT, dreg);
7128       break;
7129 
7130     case M_DLCA_AB:
7131       dbl = 1;
7132     case M_LCA_AB:
7133       call = 1;
7134       goto do_la;
7135     case M_DLA_AB:
7136       dbl = 1;
7137     case M_LA_AB:
7138     do_la:
7139       /* Load the address of a symbol into a register.  If breg is not
7140 	 zero, we then add a base register to it.  */
7141 
7142       if (dbl && HAVE_32BIT_GPRS)
7143 	as_warn (_("dla used to load 32-bit register"));
7144 
7145       if (!dbl && HAVE_64BIT_OBJECTS)
7146 	as_warn (_("la used to load 64-bit address"));
7147 
7148       if (offset_expr.X_op == O_constant
7149 	  && offset_expr.X_add_number >= -0x8000
7150 	  && offset_expr.X_add_number < 0x8000)
7151 	{
7152 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN,
7153 		       "t,r,j", treg, sreg, BFD_RELOC_LO16);
7154 	  break;
7155 	}
7156 
7157       if (mips_opts.at && (treg == breg))
7158 	{
7159 	  tempreg = AT;
7160 	  used_at = 1;
7161 	}
7162       else
7163 	{
7164 	  tempreg = treg;
7165 	}
7166 
7167       if (offset_expr.X_op != O_symbol
7168 	  && offset_expr.X_op != O_constant)
7169 	{
7170 	  as_bad (_("Expression too complex"));
7171 	  offset_expr.X_op = O_constant;
7172 	}
7173 
7174       if (offset_expr.X_op == O_constant)
7175 	load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7176       else if (mips_pic == NO_PIC)
7177 	{
7178 	  /* If this is a reference to a GP relative symbol, we want
7179 	       addiu	$tempreg,$gp,<sym>	(BFD_RELOC_GPREL16)
7180 	     Otherwise we want
7181 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
7182 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
7183 	     If we have a constant, we need two instructions anyhow,
7184 	     so we may as well always use the latter form.
7185 
7186 	     With 64bit address space and a usable $at we want
7187 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
7188 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
7189 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
7190 	       daddiu	$at,<sym>		(BFD_RELOC_LO16)
7191 	       dsll32	$tempreg,0
7192 	       daddu	$tempreg,$tempreg,$at
7193 
7194 	     If $at is already in use, we use a path which is suboptimal
7195 	     on superscalar processors.
7196 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
7197 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
7198 	       dsll	$tempreg,16
7199 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
7200 	       dsll	$tempreg,16
7201 	       daddiu	$tempreg,<sym>		(BFD_RELOC_LO16)
7202 
7203 	     For GP relative symbols in 64bit address space we can use
7204 	     the same sequence as in 32bit address space.  */
7205 	  if (HAVE_64BIT_SYMBOLS)
7206 	    {
7207 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7208 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7209 		{
7210 		  relax_start (offset_expr.X_add_symbol);
7211 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7212 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7213 		  relax_switch ();
7214 		}
7215 
7216 	      if (used_at == 0 && mips_opts.at)
7217 		{
7218 		  macro_build (&offset_expr, "lui", LUI_FMT,
7219 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
7220 		  macro_build (&offset_expr, "lui", LUI_FMT,
7221 			       AT, BFD_RELOC_HI16_S);
7222 		  macro_build (&offset_expr, "daddiu", "t,r,j",
7223 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7224 		  macro_build (&offset_expr, "daddiu", "t,r,j",
7225 			       AT, AT, BFD_RELOC_LO16);
7226 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7227 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7228 		  used_at = 1;
7229 		}
7230 	      else
7231 		{
7232 		  macro_build (&offset_expr, "lui", LUI_FMT,
7233 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
7234 		  macro_build (&offset_expr, "daddiu", "t,r,j",
7235 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7236 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7237 		  macro_build (&offset_expr, "daddiu", "t,r,j",
7238 			       tempreg, tempreg, BFD_RELOC_HI16_S);
7239 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7240 		  macro_build (&offset_expr, "daddiu", "t,r,j",
7241 			       tempreg, tempreg, BFD_RELOC_LO16);
7242 		}
7243 
7244 	      if (mips_relax.sequence)
7245 		relax_end ();
7246 	    }
7247 	  else
7248 	    {
7249 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7250 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7251 		{
7252 		  relax_start (offset_expr.X_add_symbol);
7253 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7254 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7255 		  relax_switch ();
7256 		}
7257 	      if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7258 		as_bad (_("Offset too large"));
7259 	      macro_build_lui (&offset_expr, tempreg);
7260 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7261 			   tempreg, tempreg, BFD_RELOC_LO16);
7262 	      if (mips_relax.sequence)
7263 		relax_end ();
7264 	    }
7265 	}
7266       else if (!mips_big_got && !HAVE_NEWABI)
7267 	{
7268 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7269 
7270 	  /* If this is a reference to an external symbol, and there
7271 	     is no constant, we want
7272 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
7273 	     or for lca or if tempreg is PIC_CALL_REG
7274 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
7275 	     For a local symbol, we want
7276 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
7277 	       nop
7278 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
7279 
7280 	     If we have a small constant, and this is a reference to
7281 	     an external symbol, we want
7282 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
7283 	       nop
7284 	       addiu	$tempreg,$tempreg,<constant>
7285 	     For a local symbol, we want the same instruction
7286 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
7287 	     addiu instruction.
7288 
7289 	     If we have a large constant, and this is a reference to
7290 	     an external symbol, we want
7291 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
7292 	       lui	$at,<hiconstant>
7293 	       addiu	$at,$at,<loconstant>
7294 	       addu	$tempreg,$tempreg,$at
7295 	     For a local symbol, we want the same instruction
7296 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
7297 	     addiu instruction.
7298 	   */
7299 
7300 	  if (offset_expr.X_add_number == 0)
7301 	    {
7302 	      if (mips_pic == SVR4_PIC
7303 		  && breg == 0
7304 		  && (call || tempreg == PIC_CALL_REG))
7305 		lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7306 
7307 	      relax_start (offset_expr.X_add_symbol);
7308 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7309 			   lw_reloc_type, mips_gp_register);
7310 	      if (breg != 0)
7311 		{
7312 		  /* We're going to put in an addu instruction using
7313 		     tempreg, so we may as well insert the nop right
7314 		     now.  */
7315 		  load_delay_nop ();
7316 		}
7317 	      relax_switch ();
7318 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7319 			   tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7320 	      load_delay_nop ();
7321 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7322 			   tempreg, tempreg, BFD_RELOC_LO16);
7323 	      relax_end ();
7324 	      /* FIXME: If breg == 0, and the next instruction uses
7325 		 $tempreg, then if this variant case is used an extra
7326 		 nop will be generated.  */
7327 	    }
7328 	  else if (offset_expr.X_add_number >= -0x8000
7329 		   && offset_expr.X_add_number < 0x8000)
7330 	    {
7331 	      load_got_offset (tempreg, &offset_expr);
7332 	      load_delay_nop ();
7333 	      add_got_offset (tempreg, &offset_expr);
7334 	    }
7335 	  else
7336 	    {
7337 	      expr1.X_add_number = offset_expr.X_add_number;
7338 	      offset_expr.X_add_number =
7339 		((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
7340 	      load_got_offset (tempreg, &offset_expr);
7341 	      offset_expr.X_add_number = expr1.X_add_number;
7342 	      /* If we are going to add in a base register, and the
7343 		 target register and the base register are the same,
7344 		 then we are using AT as a temporary register.  Since
7345 		 we want to load the constant into AT, we add our
7346 		 current AT (from the global offset table) and the
7347 		 register into the register now, and pretend we were
7348 		 not using a base register.  */
7349 	      if (breg == treg)
7350 		{
7351 		  load_delay_nop ();
7352 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7353 			       treg, AT, breg);
7354 		  breg = 0;
7355 		  tempreg = treg;
7356 		}
7357 	      add_got_offset_hilo (tempreg, &offset_expr, AT);
7358 	      used_at = 1;
7359 	    }
7360 	}
7361       else if (!mips_big_got && HAVE_NEWABI)
7362 	{
7363 	  int add_breg_early = 0;
7364 
7365 	  /* If this is a reference to an external, and there is no
7366 	     constant, or local symbol (*), with or without a
7367 	     constant, we want
7368 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
7369 	     or for lca or if tempreg is PIC_CALL_REG
7370 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
7371 
7372 	     If we have a small constant, and this is a reference to
7373 	     an external symbol, we want
7374 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
7375 	       addiu	$tempreg,$tempreg,<constant>
7376 
7377 	     If we have a large constant, and this is a reference to
7378 	     an external symbol, we want
7379 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
7380 	       lui	$at,<hiconstant>
7381 	       addiu	$at,$at,<loconstant>
7382 	       addu	$tempreg,$tempreg,$at
7383 
7384 	     (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7385 	     local symbols, even though it introduces an additional
7386 	     instruction.  */
7387 
7388 	  if (offset_expr.X_add_number)
7389 	    {
7390 	      expr1.X_add_number = offset_expr.X_add_number;
7391 	      offset_expr.X_add_number = 0;
7392 
7393 	      relax_start (offset_expr.X_add_symbol);
7394 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7395 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7396 
7397 	      if (expr1.X_add_number >= -0x8000
7398 		  && expr1.X_add_number < 0x8000)
7399 		{
7400 		  macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7401 			       tempreg, tempreg, BFD_RELOC_LO16);
7402 		}
7403 	      else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7404 		{
7405 		  /* If we are going to add in a base register, and the
7406 		     target register and the base register are the same,
7407 		     then we are using AT as a temporary register.  Since
7408 		     we want to load the constant into AT, we add our
7409 		     current AT (from the global offset table) and the
7410 		     register into the register now, and pretend we were
7411 		     not using a base register.  */
7412 		  if (breg != treg)
7413 		    dreg = tempreg;
7414 		  else
7415 		    {
7416 		      gas_assert (tempreg == AT);
7417 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7418 				   treg, AT, breg);
7419 		      dreg = treg;
7420 		      add_breg_early = 1;
7421 		    }
7422 
7423 		  load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7424 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7425 			       dreg, dreg, AT);
7426 
7427 		  used_at = 1;
7428 		}
7429 	      else
7430 		as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7431 
7432 	      relax_switch ();
7433 	      offset_expr.X_add_number = expr1.X_add_number;
7434 
7435 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7436 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7437 	      if (add_breg_early)
7438 		{
7439 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7440 			       treg, tempreg, breg);
7441 		  breg = 0;
7442 		  tempreg = treg;
7443 		}
7444 	      relax_end ();
7445 	    }
7446 	  else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7447 	    {
7448 	      relax_start (offset_expr.X_add_symbol);
7449 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7450 			   BFD_RELOC_MIPS_CALL16, mips_gp_register);
7451 	      relax_switch ();
7452 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7453 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7454 	      relax_end ();
7455 	    }
7456 	  else
7457 	    {
7458 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7459 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7460 	    }
7461 	}
7462       else if (mips_big_got && !HAVE_NEWABI)
7463 	{
7464 	  int gpdelay;
7465 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7466 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7467 	  int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7468 
7469 	  /* This is the large GOT case.  If this is a reference to an
7470 	     external symbol, and there is no constant, we want
7471 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
7472 	       addu	$tempreg,$tempreg,$gp
7473 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7474 	     or for lca or if tempreg is PIC_CALL_REG
7475 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
7476 	       addu	$tempreg,$tempreg,$gp
7477 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7478 	     For a local symbol, we want
7479 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
7480 	       nop
7481 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
7482 
7483 	     If we have a small constant, and this is a reference to
7484 	     an external symbol, we want
7485 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
7486 	       addu	$tempreg,$tempreg,$gp
7487 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7488 	       nop
7489 	       addiu	$tempreg,$tempreg,<constant>
7490 	     For a local symbol, we want
7491 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
7492 	       nop
7493 	       addiu	$tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7494 
7495 	     If we have a large constant, and this is a reference to
7496 	     an external symbol, we want
7497 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
7498 	       addu	$tempreg,$tempreg,$gp
7499 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7500 	       lui	$at,<hiconstant>
7501 	       addiu	$at,$at,<loconstant>
7502 	       addu	$tempreg,$tempreg,$at
7503 	     For a local symbol, we want
7504 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
7505 	       lui	$at,<hiconstant>
7506 	       addiu	$at,$at,<loconstant>	(BFD_RELOC_LO16)
7507 	       addu	$tempreg,$tempreg,$at
7508 	  */
7509 
7510 	  expr1.X_add_number = offset_expr.X_add_number;
7511 	  offset_expr.X_add_number = 0;
7512 	  relax_start (offset_expr.X_add_symbol);
7513 	  gpdelay = reg_needs_delay (mips_gp_register);
7514 	  if (expr1.X_add_number == 0 && breg == 0
7515 	      && (call || tempreg == PIC_CALL_REG))
7516 	    {
7517 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7518 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7519 	    }
7520 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7521 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7522 		       tempreg, tempreg, mips_gp_register);
7523 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7524 		       tempreg, lw_reloc_type, tempreg);
7525 	  if (expr1.X_add_number == 0)
7526 	    {
7527 	      if (breg != 0)
7528 		{
7529 		  /* We're going to put in an addu instruction using
7530 		     tempreg, so we may as well insert the nop right
7531 		     now.  */
7532 		  load_delay_nop ();
7533 		}
7534 	    }
7535 	  else if (expr1.X_add_number >= -0x8000
7536 		   && expr1.X_add_number < 0x8000)
7537 	    {
7538 	      load_delay_nop ();
7539 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7540 			   tempreg, tempreg, BFD_RELOC_LO16);
7541 	    }
7542 	  else
7543 	    {
7544 	      /* If we are going to add in a base register, and the
7545 		 target register and the base register are the same,
7546 		 then we are using AT as a temporary register.  Since
7547 		 we want to load the constant into AT, we add our
7548 		 current AT (from the global offset table) and the
7549 		 register into the register now, and pretend we were
7550 		 not using a base register.  */
7551 	      if (breg != treg)
7552 		dreg = tempreg;
7553 	      else
7554 		{
7555 		  gas_assert (tempreg == AT);
7556 		  load_delay_nop ();
7557 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7558 			       treg, AT, breg);
7559 		  dreg = treg;
7560 		}
7561 
7562 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7563 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7564 
7565 	      used_at = 1;
7566 	    }
7567 	  offset_expr.X_add_number =
7568 	    ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
7569 	  relax_switch ();
7570 
7571 	  if (gpdelay)
7572 	    {
7573 	      /* This is needed because this instruction uses $gp, but
7574 		 the first instruction on the main stream does not.  */
7575 	      macro_build (NULL, "nop", "");
7576 	    }
7577 
7578 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7579 		       local_reloc_type, mips_gp_register);
7580 	  if (expr1.X_add_number >= -0x8000
7581 	      && expr1.X_add_number < 0x8000)
7582 	    {
7583 	      load_delay_nop ();
7584 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7585 			   tempreg, tempreg, BFD_RELOC_LO16);
7586 	      /* FIXME: If add_number is 0, and there was no base
7587 		 register, the external symbol case ended with a load,
7588 		 so if the symbol turns out to not be external, and
7589 		 the next instruction uses tempreg, an unnecessary nop
7590 		 will be inserted.  */
7591 	    }
7592 	  else
7593 	    {
7594 	      if (breg == treg)
7595 		{
7596 		  /* We must add in the base register now, as in the
7597 		     external symbol case.  */
7598 		  gas_assert (tempreg == AT);
7599 		  load_delay_nop ();
7600 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7601 			       treg, AT, breg);
7602 		  tempreg = treg;
7603 		  /* We set breg to 0 because we have arranged to add
7604 		     it in in both cases.  */
7605 		  breg = 0;
7606 		}
7607 
7608 	      macro_build_lui (&expr1, AT);
7609 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7610 			   AT, AT, BFD_RELOC_LO16);
7611 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7612 			   tempreg, tempreg, AT);
7613 	      used_at = 1;
7614 	    }
7615 	  relax_end ();
7616 	}
7617       else if (mips_big_got && HAVE_NEWABI)
7618 	{
7619 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7620 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7621 	  int add_breg_early = 0;
7622 
7623 	  /* This is the large GOT case.  If this is a reference to an
7624 	     external symbol, and there is no constant, we want
7625 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
7626 	       add	$tempreg,$tempreg,$gp
7627 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7628 	     or for lca or if tempreg is PIC_CALL_REG
7629 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
7630 	       add	$tempreg,$tempreg,$gp
7631 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7632 
7633 	     If we have a small constant, and this is a reference to
7634 	     an external symbol, we want
7635 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
7636 	       add	$tempreg,$tempreg,$gp
7637 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7638 	       addi	$tempreg,$tempreg,<constant>
7639 
7640 	     If we have a large constant, and this is a reference to
7641 	     an external symbol, we want
7642 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
7643 	       addu	$tempreg,$tempreg,$gp
7644 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7645 	       lui	$at,<hiconstant>
7646 	       addi	$at,$at,<loconstant>
7647 	       add	$tempreg,$tempreg,$at
7648 
7649 	     If we have NewABI, and we know it's a local symbol, we want
7650 	       lw	$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
7651 	       addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
7652 	     otherwise we have to resort to GOT_HI16/GOT_LO16.  */
7653 
7654 	  relax_start (offset_expr.X_add_symbol);
7655 
7656 	  expr1.X_add_number = offset_expr.X_add_number;
7657 	  offset_expr.X_add_number = 0;
7658 
7659 	  if (expr1.X_add_number == 0 && breg == 0
7660 	      && (call || tempreg == PIC_CALL_REG))
7661 	    {
7662 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7663 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7664 	    }
7665 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7666 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7667 		       tempreg, tempreg, mips_gp_register);
7668 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7669 		       tempreg, lw_reloc_type, tempreg);
7670 
7671 	  if (expr1.X_add_number == 0)
7672 	    ;
7673 	  else if (expr1.X_add_number >= -0x8000
7674 		   && expr1.X_add_number < 0x8000)
7675 	    {
7676 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7677 			   tempreg, tempreg, BFD_RELOC_LO16);
7678 	    }
7679 	  else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7680 	    {
7681 	      /* If we are going to add in a base register, and the
7682 		 target register and the base register are the same,
7683 		 then we are using AT as a temporary register.  Since
7684 		 we want to load the constant into AT, we add our
7685 		 current AT (from the global offset table) and the
7686 		 register into the register now, and pretend we were
7687 		 not using a base register.  */
7688 	      if (breg != treg)
7689 		dreg = tempreg;
7690 	      else
7691 		{
7692 		  gas_assert (tempreg == AT);
7693 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7694 			       treg, AT, breg);
7695 		  dreg = treg;
7696 		  add_breg_early = 1;
7697 		}
7698 
7699 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7700 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7701 
7702 	      used_at = 1;
7703 	    }
7704 	  else
7705 	    as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7706 
7707 	  relax_switch ();
7708 	  offset_expr.X_add_number = expr1.X_add_number;
7709 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7710 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7711 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7712 		       tempreg, BFD_RELOC_MIPS_GOT_OFST);
7713 	  if (add_breg_early)
7714 	    {
7715 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7716 			   treg, tempreg, breg);
7717 	      breg = 0;
7718 	      tempreg = treg;
7719 	    }
7720 	  relax_end ();
7721 	}
7722       else
7723 	abort ();
7724 
7725       if (breg != 0)
7726 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
7727       break;
7728 
7729     case M_JR_S:
7730       macro_build_jrpatch (&expr1, sreg);
7731       macro_build (NULL, "jr", "s", sreg);
7732       return;	/* didn't modify $at */
7733 
7734     case M_J_S:
7735       macro_build_jrpatch (&expr1, sreg);
7736       macro_build (NULL, "j", "s", sreg);
7737       return;	/* didn't modify $at */
7738 
7739     case M_JALR_S:
7740       macro_build_jrpatch (&expr1, sreg);
7741       macro_build (NULL, "jalr", "s", sreg);
7742       return;	/* didn't modify $at */
7743 
7744     case M_JALR_DS:
7745       macro_build_jrpatch (&expr1, sreg);
7746       macro_build (NULL, "jalr", "d,s", dreg, sreg);
7747       return;	/* didn't modify $at */
7748 
7749     case M_MSGSND:
7750       gas_assert (!mips_opts.micromips);
7751       {
7752 	unsigned long temp = (treg << 16) | (0x01);
7753 	macro_build (NULL, "c2", "C", temp);
7754       }
7755       break;
7756 
7757     case M_MSGLD:
7758       gas_assert (!mips_opts.micromips);
7759       {
7760 	unsigned long temp = (0x02);
7761 	macro_build (NULL, "c2", "C", temp);
7762       }
7763       break;
7764 
7765     case M_MSGLD_T:
7766       gas_assert (!mips_opts.micromips);
7767       {
7768 	unsigned long temp = (treg << 16) | (0x02);
7769 	macro_build (NULL, "c2", "C", temp);
7770       }
7771       break;
7772 
7773     case M_MSGWAIT:
7774       gas_assert (!mips_opts.micromips);
7775       macro_build (NULL, "c2", "C", 3);
7776       break;
7777 
7778     case M_MSGWAIT_T:
7779       gas_assert (!mips_opts.micromips);
7780       {
7781 	unsigned long temp = (treg << 16) | 0x03;
7782 	macro_build (NULL, "c2", "C", temp);
7783       }
7784       break;
7785 
7786     case M_J_A:
7787       /* The j instruction may not be used in PIC code, since it
7788 	 requires an absolute address.  We convert it to a b
7789 	 instruction.  */
7790       if (mips_pic == NO_PIC)
7791 	macro_build (&offset_expr, "j", "a");
7792       else
7793 	macro_build (&offset_expr, "b", "p");
7794       break;
7795 
7796       /* The jal instructions must be handled as macros because when
7797 	 generating PIC code they expand to multi-instruction
7798 	 sequences.  Normally they are simple instructions.  */
7799     case M_JALS_1:
7800       dreg = RA;
7801       /* Fall through.  */
7802     case M_JALS_2:
7803       gas_assert (mips_opts.micromips);
7804       jals = 1;
7805       goto jal;
7806     case M_JAL_1:
7807       dreg = RA;
7808       /* Fall through.  */
7809     case M_JAL_2:
7810     jal:
7811       if (mips_pic == NO_PIC)
7812 	{
7813 	  s = jals ? "jalrs" : "jalr";
7814 	  if (mips_opts.micromips && dreg == RA)
7815 	    macro_build (NULL, s, "mj", sreg);
7816 	  else
7817 	    macro_build (NULL, s, JALR_FMT, dreg, sreg);
7818 	}
7819       else
7820 	{
7821 	  int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7822 			   && mips_cprestore_offset >= 0);
7823 
7824 	  if (sreg != PIC_CALL_REG)
7825 	    as_warn (_("MIPS PIC call to register other than $25"));
7826 
7827 	  s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7828 	       ? "jalrs" : "jalr");
7829 	  if (mips_opts.micromips && dreg == RA)
7830 	    macro_build (NULL, s, "mj", sreg);
7831 	  else
7832 	    macro_build (NULL, s, JALR_FMT, dreg, sreg);
7833 	  if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
7834 	    {
7835 	      if (mips_cprestore_offset < 0)
7836 		as_warn (_("No .cprestore pseudo-op used in PIC code"));
7837 	      else
7838 		{
7839 		  if (!mips_frame_reg_valid)
7840 		    {
7841 		      as_warn (_("No .frame pseudo-op used in PIC code"));
7842 		      /* Quiet this warning.  */
7843 		      mips_frame_reg_valid = 1;
7844 		    }
7845 		  if (!mips_cprestore_valid)
7846 		    {
7847 		      as_warn (_("No .cprestore pseudo-op used in PIC code"));
7848 		      /* Quiet this warning.  */
7849 		      mips_cprestore_valid = 1;
7850 		    }
7851 		  if (mips_opts.noreorder)
7852 		    macro_build (NULL, "nop", "");
7853 		  expr1.X_add_number = mips_cprestore_offset;
7854   		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7855 						mips_gp_register,
7856 						mips_frame_reg,
7857 						HAVE_64BIT_ADDRESSES);
7858 		}
7859 	    }
7860 	}
7861 
7862       break;
7863 
7864     case M_JALS_A:
7865       gas_assert (mips_opts.micromips);
7866       jals = 1;
7867       /* Fall through.  */
7868     case M_JAL_A:
7869       if (mips_pic == NO_PIC)
7870 	macro_build (&offset_expr, jals ? "jals" : "jal", "a");
7871       else if (mips_pic == SVR4_PIC)
7872 	{
7873 	  /* If this is a reference to an external symbol, and we are
7874 	     using a small GOT, we want
7875 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_CALL16)
7876 	       nop
7877 	       jalr	$ra,$25
7878 	       nop
7879 	       lw	$gp,cprestore($sp)
7880 	     The cprestore value is set using the .cprestore
7881 	     pseudo-op.  If we are using a big GOT, we want
7882 	       lui	$25,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
7883 	       addu	$25,$25,$gp
7884 	       lw	$25,<sym>($25)		(BFD_RELOC_MIPS_CALL_LO16)
7885 	       nop
7886 	       jalr	$ra,$25
7887 	       nop
7888 	       lw	$gp,cprestore($sp)
7889 	     If the symbol is not external, we want
7890 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
7891 	       nop
7892 	       addiu	$25,$25,<sym>		(BFD_RELOC_LO16)
7893 	       jalr	$ra,$25
7894 	       nop
7895 	       lw $gp,cprestore($sp)
7896 
7897 	     For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7898 	     sequences above, minus nops, unless the symbol is local,
7899 	     which enables us to use GOT_PAGE/GOT_OFST (big got) or
7900 	     GOT_DISP.  */
7901 	  if (HAVE_NEWABI)
7902 	    {
7903 	      if (!mips_big_got)
7904 		{
7905 		  relax_start (offset_expr.X_add_symbol);
7906 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7907 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7908 			       mips_gp_register);
7909 		  relax_switch ();
7910 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7911 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
7912 			       mips_gp_register);
7913 		  relax_end ();
7914 		}
7915 	      else
7916 		{
7917 		  relax_start (offset_expr.X_add_symbol);
7918 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
7919 			       BFD_RELOC_MIPS_CALL_HI16);
7920 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7921 			       PIC_CALL_REG, mips_gp_register);
7922 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7923 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7924 			       PIC_CALL_REG);
7925 		  relax_switch ();
7926 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7927 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7928 			       mips_gp_register);
7929 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7930 			       PIC_CALL_REG, PIC_CALL_REG,
7931 			       BFD_RELOC_MIPS_GOT_OFST);
7932 		  relax_end ();
7933 		}
7934 
7935 	      macro_build_jalr (&offset_expr, 0);
7936 	    }
7937 	  else
7938 	    {
7939 	      relax_start (offset_expr.X_add_symbol);
7940 	      if (!mips_big_got)
7941 		{
7942 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7943 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7944 			       mips_gp_register);
7945 		  load_delay_nop ();
7946 		  relax_switch ();
7947 		}
7948 	      else
7949 		{
7950 		  int gpdelay;
7951 
7952 		  gpdelay = reg_needs_delay (mips_gp_register);
7953 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
7954 			       BFD_RELOC_MIPS_CALL_HI16);
7955 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7956 			       PIC_CALL_REG, mips_gp_register);
7957 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7958 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7959 			       PIC_CALL_REG);
7960 		  load_delay_nop ();
7961 		  relax_switch ();
7962 		  if (gpdelay)
7963 		    macro_build (NULL, "nop", "");
7964 		}
7965 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7966 			   PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
7967 			   mips_gp_register);
7968 	      load_delay_nop ();
7969 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7970 			   PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
7971 	      relax_end ();
7972 	      macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
7973 
7974 	      if (mips_cprestore_offset < 0)
7975 		as_warn (_("No .cprestore pseudo-op used in PIC code"));
7976 	      else
7977 		{
7978 		  if (!mips_frame_reg_valid)
7979 		    {
7980 		      as_warn (_("No .frame pseudo-op used in PIC code"));
7981 		      /* Quiet this warning.  */
7982 		      mips_frame_reg_valid = 1;
7983 		    }
7984 		  if (!mips_cprestore_valid)
7985 		    {
7986 		      as_warn (_("No .cprestore pseudo-op used in PIC code"));
7987 		      /* Quiet this warning.  */
7988 		      mips_cprestore_valid = 1;
7989 		    }
7990 		  if (mips_opts.noreorder)
7991 		    macro_build (NULL, "nop", "");
7992 		  expr1.X_add_number = mips_cprestore_offset;
7993   		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7994 						mips_gp_register,
7995 						mips_frame_reg,
7996 						HAVE_64BIT_ADDRESSES);
7997 		}
7998 	    }
7999 	}
8000       else if (mips_pic == VXWORKS_PIC)
8001 	as_bad (_("Non-PIC jump used in PIC library"));
8002       else
8003 	abort ();
8004 
8005       break;
8006 
8007     case M_ACLR_AB:
8008       ab = 1;
8009     case M_ACLR_OB:
8010       s = "aclr";
8011       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8012       fmt = "\\,~(b)";
8013       off12 = 1;
8014       goto ld_st;
8015     case M_ASET_AB:
8016       ab = 1;
8017     case M_ASET_OB:
8018       s = "aset";
8019       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8020       fmt = "\\,~(b)";
8021       off12 = 1;
8022       goto ld_st;
8023     case M_LB_AB:
8024       ab = 1;
8025       s = "lb";
8026       fmt = "t,o(b)";
8027       goto ld;
8028     case M_LBU_AB:
8029       ab = 1;
8030       s = "lbu";
8031       fmt = "t,o(b)";
8032       goto ld;
8033     case M_LH_AB:
8034       ab = 1;
8035       s = "lh";
8036       fmt = "t,o(b)";
8037       goto ld;
8038     case M_LHU_AB:
8039       ab = 1;
8040       s = "lhu";
8041       fmt = "t,o(b)";
8042       goto ld;
8043     case M_LW_AB:
8044       ab = 1;
8045       s = "lw";
8046       fmt = "t,o(b)";
8047       goto ld;
8048     case M_LWC0_AB:
8049       ab = 1;
8050       gas_assert (!mips_opts.micromips);
8051       s = "lwc0";
8052       fmt = "E,o(b)";
8053       /* Itbl support may require additional care here.  */
8054       coproc = 1;
8055       goto ld_st;
8056     case M_LWC1_AB:
8057       ab = 1;
8058       s = "lwc1";
8059       fmt = "T,o(b)";
8060       /* Itbl support may require additional care here.  */
8061       coproc = 1;
8062       goto ld_st;
8063     case M_LWC2_AB:
8064       ab = 1;
8065     case M_LWC2_OB:
8066       s = "lwc2";
8067       fmt = COP12_FMT;
8068       off12 = mips_opts.micromips;
8069       /* Itbl support may require additional care here.  */
8070       coproc = 1;
8071       goto ld_st;
8072     case M_LWC3_AB:
8073       ab = 1;
8074       gas_assert (!mips_opts.micromips);
8075       s = "lwc3";
8076       fmt = "E,o(b)";
8077       /* Itbl support may require additional care here.  */
8078       coproc = 1;
8079       goto ld_st;
8080     case M_LWL_AB:
8081       ab = 1;
8082     case M_LWL_OB:
8083       s = "lwl";
8084       fmt = MEM12_FMT;
8085       off12 = mips_opts.micromips;
8086       goto ld_st;
8087     case M_LWR_AB:
8088       ab = 1;
8089     case M_LWR_OB:
8090       s = "lwr";
8091       fmt = MEM12_FMT;
8092       off12 = mips_opts.micromips;
8093       goto ld_st;
8094     case M_LDC1_AB:
8095       ab = 1;
8096       s = "ldc1";
8097       fmt = "T,o(b)";
8098       /* Itbl support may require additional care here.  */
8099       coproc = 1;
8100       goto ld_st;
8101     case M_LDC2_AB:
8102       ab = 1;
8103     case M_LDC2_OB:
8104       s = "ldc2";
8105       fmt = COP12_FMT;
8106       off12 = mips_opts.micromips;
8107       /* Itbl support may require additional care here.  */
8108       coproc = 1;
8109       goto ld_st;
8110     case M_LDC3_AB:
8111       ab = 1;
8112       s = "ldc3";
8113       fmt = "E,o(b)";
8114       /* Itbl support may require additional care here.  */
8115       coproc = 1;
8116       goto ld_st;
8117     case M_LDL_AB:
8118       ab = 1;
8119     case M_LDL_OB:
8120       s = "ldl";
8121       fmt = MEM12_FMT;
8122       off12 = mips_opts.micromips;
8123       goto ld_st;
8124     case M_LDR_AB:
8125       ab = 1;
8126     case M_LDR_OB:
8127       s = "ldr";
8128       fmt = MEM12_FMT;
8129       off12 = mips_opts.micromips;
8130       goto ld_st;
8131     case M_LL_AB:
8132       ab = 1;
8133     case M_LL_OB:
8134       s = "ll";
8135       fmt = MEM12_FMT;
8136       off12 = mips_opts.micromips;
8137       goto ld;
8138     case M_LLD_AB:
8139       ab = 1;
8140     case M_LLD_OB:
8141       s = "lld";
8142       fmt = MEM12_FMT;
8143       off12 = mips_opts.micromips;
8144       goto ld;
8145     case M_LWU_AB:
8146       ab = 1;
8147     case M_LWU_OB:
8148       s = "lwu";
8149       fmt = MEM12_FMT;
8150       off12 = mips_opts.micromips;
8151       goto ld;
8152     case M_LWP_AB:
8153       ab = 1;
8154     case M_LWP_OB:
8155       gas_assert (mips_opts.micromips);
8156       s = "lwp";
8157       fmt = "t,~(b)";
8158       off12 = 1;
8159       lp = 1;
8160       goto ld;
8161     case M_LDP_AB:
8162       ab = 1;
8163     case M_LDP_OB:
8164       gas_assert (mips_opts.micromips);
8165       s = "ldp";
8166       fmt = "t,~(b)";
8167       off12 = 1;
8168       lp = 1;
8169       goto ld;
8170     case M_LWM_AB:
8171       ab = 1;
8172     case M_LWM_OB:
8173       gas_assert (mips_opts.micromips);
8174       s = "lwm";
8175       fmt = "n,~(b)";
8176       off12 = 1;
8177       goto ld_st;
8178     case M_LDM_AB:
8179       ab = 1;
8180     case M_LDM_OB:
8181       gas_assert (mips_opts.micromips);
8182       s = "ldm";
8183       fmt = "n,~(b)";
8184       off12 = 1;
8185       goto ld_st;
8186 
8187     ld:
8188       if (breg == treg + lp)
8189 	goto ld_st;
8190       else
8191 	tempreg = treg + lp;
8192       goto ld_noat;
8193 
8194     case M_SB_AB:
8195       ab = 1;
8196       s = "sb";
8197       fmt = "t,o(b)";
8198       goto ld_st;
8199     case M_SH_AB:
8200       ab = 1;
8201       s = "sh";
8202       fmt = "t,o(b)";
8203       goto ld_st;
8204     case M_SW_AB:
8205       ab = 1;
8206       s = "sw";
8207       fmt = "t,o(b)";
8208       goto ld_st;
8209     case M_SWC0_AB:
8210       ab = 1;
8211       gas_assert (!mips_opts.micromips);
8212       s = "swc0";
8213       fmt = "E,o(b)";
8214       /* Itbl support may require additional care here.  */
8215       coproc = 1;
8216       goto ld_st;
8217     case M_SWC1_AB:
8218       ab = 1;
8219       s = "swc1";
8220       fmt = "T,o(b)";
8221       /* Itbl support may require additional care here.  */
8222       coproc = 1;
8223       goto ld_st;
8224     case M_SWC2_AB:
8225       ab = 1;
8226     case M_SWC2_OB:
8227       s = "swc2";
8228       fmt = COP12_FMT;
8229       off12 = mips_opts.micromips;
8230       /* Itbl support may require additional care here.  */
8231       coproc = 1;
8232       goto ld_st;
8233     case M_SWC3_AB:
8234       ab = 1;
8235       gas_assert (!mips_opts.micromips);
8236       s = "swc3";
8237       fmt = "E,o(b)";
8238       /* Itbl support may require additional care here.  */
8239       coproc = 1;
8240       goto ld_st;
8241     case M_SWL_AB:
8242       ab = 1;
8243     case M_SWL_OB:
8244       s = "swl";
8245       fmt = MEM12_FMT;
8246       off12 = mips_opts.micromips;
8247       goto ld_st;
8248     case M_SWR_AB:
8249       ab = 1;
8250     case M_SWR_OB:
8251       s = "swr";
8252       fmt = MEM12_FMT;
8253       off12 = mips_opts.micromips;
8254       goto ld_st;
8255     case M_SC_AB:
8256       ab = 1;
8257     case M_SC_OB:
8258       s = "sc";
8259       fmt = MEM12_FMT;
8260       off12 = mips_opts.micromips;
8261       goto ld_st;
8262     case M_SCD_AB:
8263       ab = 1;
8264     case M_SCD_OB:
8265       s = "scd";
8266       fmt = MEM12_FMT;
8267       off12 = mips_opts.micromips;
8268       goto ld_st;
8269     case M_CACHE_AB:
8270       ab = 1;
8271     case M_CACHE_OB:
8272       s = "cache";
8273       fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8274       off12 = mips_opts.micromips;
8275       goto ld_st;
8276     case M_PREF_AB:
8277       ab = 1;
8278     case M_PREF_OB:
8279       s = "pref";
8280       fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8281       off12 = mips_opts.micromips;
8282       goto ld_st;
8283     case M_SDC1_AB:
8284       ab = 1;
8285       s = "sdc1";
8286       fmt = "T,o(b)";
8287       coproc = 1;
8288       /* Itbl support may require additional care here.  */
8289       goto ld_st;
8290     case M_SDC2_AB:
8291       ab = 1;
8292     case M_SDC2_OB:
8293       s = "sdc2";
8294       fmt = COP12_FMT;
8295       off12 = mips_opts.micromips;
8296       /* Itbl support may require additional care here.  */
8297       coproc = 1;
8298       goto ld_st;
8299     case M_SDC3_AB:
8300       ab = 1;
8301       gas_assert (!mips_opts.micromips);
8302       s = "sdc3";
8303       fmt = "E,o(b)";
8304       /* Itbl support may require additional care here.  */
8305       coproc = 1;
8306       goto ld_st;
8307     case M_SDL_AB:
8308       ab = 1;
8309     case M_SDL_OB:
8310       s = "sdl";
8311       fmt = MEM12_FMT;
8312       off12 = mips_opts.micromips;
8313       goto ld_st;
8314     case M_SDR_AB:
8315       ab = 1;
8316     case M_SDR_OB:
8317       s = "sdr";
8318       fmt = MEM12_FMT;
8319       off12 = mips_opts.micromips;
8320       goto ld_st;
8321     case M_SWP_AB:
8322       ab = 1;
8323     case M_SWP_OB:
8324       gas_assert (mips_opts.micromips);
8325       s = "swp";
8326       fmt = "t,~(b)";
8327       off12 = 1;
8328       goto ld_st;
8329     case M_SDP_AB:
8330       ab = 1;
8331     case M_SDP_OB:
8332       gas_assert (mips_opts.micromips);
8333       s = "sdp";
8334       fmt = "t,~(b)";
8335       off12 = 1;
8336       goto ld_st;
8337     case M_SWM_AB:
8338       ab = 1;
8339     case M_SWM_OB:
8340       gas_assert (mips_opts.micromips);
8341       s = "swm";
8342       fmt = "n,~(b)";
8343       off12 = 1;
8344       goto ld_st;
8345     case M_SDM_AB:
8346       ab = 1;
8347     case M_SDM_OB:
8348       gas_assert (mips_opts.micromips);
8349       s = "sdm";
8350       fmt = "n,~(b)";
8351       off12 = 1;
8352 
8353     ld_st:
8354       tempreg = AT;
8355       used_at = 1;
8356     ld_noat:
8357       if (offset_expr.X_op != O_constant
8358 	  && offset_expr.X_op != O_symbol)
8359 	{
8360 	  as_bad (_("Expression too complex"));
8361 	  offset_expr.X_op = O_constant;
8362 	}
8363 
8364       if (HAVE_32BIT_ADDRESSES
8365 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8366 	{
8367 	  char value [32];
8368 
8369 	  sprintf_vma (value, offset_expr.X_add_number);
8370 	  as_bad (_("Number (0x%s) larger than 32 bits"), value);
8371 	}
8372 
8373       /* A constant expression in PIC code can be handled just as it
8374 	 is in non PIC code.  */
8375       if (offset_expr.X_op == O_constant)
8376 	{
8377 	  int hipart = 0;
8378 
8379 	  expr1.X_add_number = offset_expr.X_add_number;
8380 	  normalize_address_expr (&expr1);
8381 	  if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
8382 	    {
8383 	      expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8384 				    & ~(bfd_vma) 0xffff);
8385 	      hipart = 1;
8386 	    }
8387 	  else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8388 	    {
8389 	      expr1.X_add_number = ((expr1.X_add_number + 0x800)
8390 				    & ~(bfd_vma) 0xfff);
8391 	      hipart = 1;
8392 	    }
8393 	  if (hipart)
8394 	    {
8395 	      load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8396 	      if (breg != 0)
8397 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8398 			     tempreg, tempreg, breg);
8399 	      breg = tempreg;
8400 	    }
8401 	  if (off0)
8402 	    {
8403 	      if (offset_expr.X_add_number == 0)
8404 		tempreg = breg;
8405 	      else
8406 		macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8407 			     "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8408 	      macro_build (NULL, s, fmt, treg, tempreg);
8409 	    }
8410 	  else if (!off12)
8411 	    macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8412 	  else
8413 	    macro_build (NULL, s, fmt,
8414 			 treg, (unsigned long) offset_expr.X_add_number, breg);
8415 	}
8416       else if (off12 || off0)
8417 	{
8418 	  /* A 12-bit or 0-bit offset field is too narrow to be used
8419 	     for a low-part relocation, so load the whole address into
8420 	     the auxillary register.  In the case of "A(b)" addresses,
8421 	     we first load absolute address "A" into the register and
8422 	     then add base register "b".  In the case of "o(b)" addresses,
8423 	     we simply need to add 16-bit offset "o" to base register "b", and
8424 	     offset_reloc already contains the relocations associated
8425 	     with "o".  */
8426 	  if (ab)
8427 	    {
8428 	      load_address (tempreg, &offset_expr, &used_at);
8429 	      if (breg != 0)
8430 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8431 			     tempreg, tempreg, breg);
8432 	    }
8433 	  else
8434 	    macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8435 			 tempreg, breg, -1,
8436 			 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8437 	  expr1.X_add_number = 0;
8438 	  if (off0)
8439 	    macro_build (NULL, s, fmt, treg, tempreg);
8440 	  else
8441 	    macro_build (NULL, s, fmt,
8442 		         treg, (unsigned long) expr1.X_add_number, tempreg);
8443 	}
8444       else if (mips_pic == NO_PIC)
8445 	{
8446 	  /* If this is a reference to a GP relative symbol, and there
8447 	     is no base register, we want
8448 	       <op>	$treg,<sym>($gp)	(BFD_RELOC_GPREL16)
8449 	     Otherwise, if there is no base register, we want
8450 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
8451 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
8452 	     If we have a constant, we need two instructions anyhow,
8453 	     so we always use the latter form.
8454 
8455 	     If we have a base register, and this is a reference to a
8456 	     GP relative symbol, we want
8457 	       addu	$tempreg,$breg,$gp
8458 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_GPREL16)
8459 	     Otherwise we want
8460 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
8461 	       addu	$tempreg,$tempreg,$breg
8462 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
8463 	     With a constant we always use the latter case.
8464 
8465 	     With 64bit address space and no base register and $at usable,
8466 	     we want
8467 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
8468 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
8469 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
8470 	       dsll32	$tempreg,0
8471 	       daddu	$tempreg,$at
8472 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
8473 	     If we have a base register, we want
8474 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
8475 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
8476 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
8477 	       daddu	$at,$breg
8478 	       dsll32	$tempreg,0
8479 	       daddu	$tempreg,$at
8480 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
8481 
8482 	     Without $at we can't generate the optimal path for superscalar
8483 	     processors here since this would require two temporary registers.
8484 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
8485 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
8486 	       dsll	$tempreg,16
8487 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
8488 	       dsll	$tempreg,16
8489 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
8490 	     If we have a base register, we want
8491 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
8492 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
8493 	       dsll	$tempreg,16
8494 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
8495 	       dsll	$tempreg,16
8496 	       daddu	$tempreg,$tempreg,$breg
8497 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
8498 
8499 	     For GP relative symbols in 64bit address space we can use
8500 	     the same sequence as in 32bit address space.  */
8501 	  if (HAVE_64BIT_SYMBOLS)
8502 	    {
8503 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8504 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8505 		{
8506 		  relax_start (offset_expr.X_add_symbol);
8507 		  if (breg == 0)
8508 		    {
8509 		      macro_build (&offset_expr, s, fmt, treg,
8510 				   BFD_RELOC_GPREL16, mips_gp_register);
8511 		    }
8512 		  else
8513 		    {
8514 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8515 				   tempreg, breg, mips_gp_register);
8516 		      macro_build (&offset_expr, s, fmt, treg,
8517 				   BFD_RELOC_GPREL16, tempreg);
8518 		    }
8519 		  relax_switch ();
8520 		}
8521 
8522 	      if (used_at == 0 && mips_opts.at)
8523 		{
8524 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8525 			       BFD_RELOC_MIPS_HIGHEST);
8526 		  macro_build (&offset_expr, "lui", LUI_FMT, AT,
8527 			       BFD_RELOC_HI16_S);
8528 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8529 			       tempreg, BFD_RELOC_MIPS_HIGHER);
8530 		  if (breg != 0)
8531 		    macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
8532 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8533 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8534 		  macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8535 			       tempreg);
8536 		  used_at = 1;
8537 		}
8538 	      else
8539 		{
8540 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8541 			       BFD_RELOC_MIPS_HIGHEST);
8542 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8543 			       tempreg, BFD_RELOC_MIPS_HIGHER);
8544 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8545 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8546 			       tempreg, BFD_RELOC_HI16_S);
8547 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8548 		  if (breg != 0)
8549 		    macro_build (NULL, "daddu", "d,v,t",
8550 				 tempreg, tempreg, breg);
8551 		  macro_build (&offset_expr, s, fmt, treg,
8552 			       BFD_RELOC_LO16, tempreg);
8553 		}
8554 
8555 	      if (mips_relax.sequence)
8556 		relax_end ();
8557 	      break;
8558 	    }
8559 
8560 	  if (breg == 0)
8561 	    {
8562 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8563 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8564 		{
8565 		  relax_start (offset_expr.X_add_symbol);
8566 		  macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8567 			       mips_gp_register);
8568 		  relax_switch ();
8569 		}
8570 	      macro_build_lui (&offset_expr, tempreg);
8571 	      macro_build (&offset_expr, s, fmt, treg,
8572 			   BFD_RELOC_LO16, tempreg);
8573 	      if (mips_relax.sequence)
8574 		relax_end ();
8575 	    }
8576 	  else
8577 	    {
8578 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8579 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8580 		{
8581 		  relax_start (offset_expr.X_add_symbol);
8582 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8583 			       tempreg, breg, mips_gp_register);
8584 		  macro_build (&offset_expr, s, fmt, treg,
8585 			       BFD_RELOC_GPREL16, tempreg);
8586 		  relax_switch ();
8587 		}
8588 	      macro_build_lui (&offset_expr, tempreg);
8589 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8590 			   tempreg, tempreg, breg);
8591 	      macro_build (&offset_expr, s, fmt, treg,
8592 			   BFD_RELOC_LO16, tempreg);
8593 	      if (mips_relax.sequence)
8594 		relax_end ();
8595 	    }
8596 	}
8597       else if (!mips_big_got)
8598 	{
8599 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
8600 
8601 	  /* If this is a reference to an external symbol, we want
8602 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
8603 	       nop
8604 	       <op>	$treg,0($tempreg)
8605 	     Otherwise we want
8606 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
8607 	       nop
8608 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
8609 	       <op>	$treg,0($tempreg)
8610 
8611 	     For NewABI, we want
8612 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
8613 	       <op>	$treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
8614 
8615 	     If there is a base register, we add it to $tempreg before
8616 	     the <op>.  If there is a constant, we stick it in the
8617 	     <op> instruction.  We don't handle constants larger than
8618 	     16 bits, because we have no way to load the upper 16 bits
8619 	     (actually, we could handle them for the subset of cases
8620 	     in which we are not using $at).  */
8621 	  gas_assert (offset_expr.X_op == O_symbol);
8622 	  if (HAVE_NEWABI)
8623 	    {
8624 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8625 			   BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8626 	      if (breg != 0)
8627 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8628 			     tempreg, tempreg, breg);
8629 	      macro_build (&offset_expr, s, fmt, treg,
8630 			   BFD_RELOC_MIPS_GOT_OFST, tempreg);
8631 	      break;
8632 	    }
8633 	  expr1.X_add_number = offset_expr.X_add_number;
8634 	  offset_expr.X_add_number = 0;
8635 	  if (expr1.X_add_number < -0x8000
8636 	      || expr1.X_add_number >= 0x8000)
8637 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8638 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8639 		       lw_reloc_type, mips_gp_register);
8640 	  load_delay_nop ();
8641 	  relax_start (offset_expr.X_add_symbol);
8642 	  relax_switch ();
8643 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8644 		       tempreg, BFD_RELOC_LO16);
8645 	  relax_end ();
8646 	  if (breg != 0)
8647 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8648 			 tempreg, tempreg, breg);
8649 	  macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8650 	}
8651       else if (mips_big_got && !HAVE_NEWABI)
8652 	{
8653 	  int gpdelay;
8654 
8655 	  /* If this is a reference to an external symbol, we want
8656 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
8657 	       addu	$tempreg,$tempreg,$gp
8658 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8659 	       <op>	$treg,0($tempreg)
8660 	     Otherwise we want
8661 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
8662 	       nop
8663 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
8664 	       <op>	$treg,0($tempreg)
8665 	     If there is a base register, we add it to $tempreg before
8666 	     the <op>.  If there is a constant, we stick it in the
8667 	     <op> instruction.  We don't handle constants larger than
8668 	     16 bits, because we have no way to load the upper 16 bits
8669 	     (actually, we could handle them for the subset of cases
8670 	     in which we are not using $at).  */
8671 	  gas_assert (offset_expr.X_op == O_symbol);
8672 	  expr1.X_add_number = offset_expr.X_add_number;
8673 	  offset_expr.X_add_number = 0;
8674 	  if (expr1.X_add_number < -0x8000
8675 	      || expr1.X_add_number >= 0x8000)
8676 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8677 	  gpdelay = reg_needs_delay (mips_gp_register);
8678 	  relax_start (offset_expr.X_add_symbol);
8679 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8680 		       BFD_RELOC_MIPS_GOT_HI16);
8681 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8682 		       mips_gp_register);
8683 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8684 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
8685 	  relax_switch ();
8686 	  if (gpdelay)
8687 	    macro_build (NULL, "nop", "");
8688 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8689 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
8690 	  load_delay_nop ();
8691 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8692 		       tempreg, BFD_RELOC_LO16);
8693 	  relax_end ();
8694 
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 	  /* If this is a reference to an external symbol, we want
8703 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
8704 	       add	$tempreg,$tempreg,$gp
8705 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8706 	       <op>	$treg,<ofst>($tempreg)
8707 	     Otherwise, for local symbols, we want:
8708 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
8709 	       <op>	$treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
8710 	  gas_assert (offset_expr.X_op == O_symbol);
8711 	  expr1.X_add_number = offset_expr.X_add_number;
8712 	  offset_expr.X_add_number = 0;
8713 	  if (expr1.X_add_number < -0x8000
8714 	      || expr1.X_add_number >= 0x8000)
8715 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8716 	  relax_start (offset_expr.X_add_symbol);
8717 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8718 		       BFD_RELOC_MIPS_GOT_HI16);
8719 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8720 		       mips_gp_register);
8721 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8722 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
8723 	  if (breg != 0)
8724 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8725 			 tempreg, tempreg, breg);
8726 	  macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8727 
8728 	  relax_switch ();
8729 	  offset_expr.X_add_number = expr1.X_add_number;
8730 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8731 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8732 	  if (breg != 0)
8733 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8734 			 tempreg, tempreg, breg);
8735 	  macro_build (&offset_expr, s, fmt, treg,
8736 		       BFD_RELOC_MIPS_GOT_OFST, tempreg);
8737 	  relax_end ();
8738 	}
8739       else
8740 	abort ();
8741 
8742       break;
8743 
8744     case M_LI:
8745     case M_LI_S:
8746       load_register (treg, &imm_expr, 0);
8747       break;
8748 
8749     case M_DLI:
8750       load_register (treg, &imm_expr, 1);
8751       break;
8752 
8753     case M_LI_SS:
8754       if (imm_expr.X_op == O_constant)
8755 	{
8756 	  used_at = 1;
8757 	  load_register (AT, &imm_expr, 0);
8758 	  macro_build (NULL, "mtc1", "t,G", AT, treg);
8759 	  break;
8760 	}
8761       else
8762 	{
8763 	  gas_assert (offset_expr.X_op == O_symbol
8764 		      && strcmp (segment_name (S_GET_SEGMENT
8765 					       (offset_expr.X_add_symbol)),
8766 				 ".lit4") == 0
8767 		      && offset_expr.X_add_number == 0);
8768 	  macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
8769 		       BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8770 	  break;
8771 	}
8772 
8773     case M_LI_D:
8774       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
8775          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
8776          order 32 bits of the value and the low order 32 bits are either
8777          zero or in OFFSET_EXPR.  */
8778       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8779 	{
8780 	  if (HAVE_64BIT_GPRS)
8781 	    load_register (treg, &imm_expr, 1);
8782 	  else
8783 	    {
8784 	      int hreg, lreg;
8785 
8786 	      if (target_big_endian)
8787 		{
8788 		  hreg = treg;
8789 		  lreg = treg + 1;
8790 		}
8791 	      else
8792 		{
8793 		  hreg = treg + 1;
8794 		  lreg = treg;
8795 		}
8796 
8797 	      if (hreg <= 31)
8798 		load_register (hreg, &imm_expr, 0);
8799 	      if (lreg <= 31)
8800 		{
8801 		  if (offset_expr.X_op == O_absent)
8802 		    move_register (lreg, 0);
8803 		  else
8804 		    {
8805 		      gas_assert (offset_expr.X_op == O_constant);
8806 		      load_register (lreg, &offset_expr, 0);
8807 		    }
8808 		}
8809 	    }
8810 	  break;
8811 	}
8812 
8813       /* We know that sym is in the .rdata section.  First we get the
8814 	 upper 16 bits of the address.  */
8815       if (mips_pic == NO_PIC)
8816 	{
8817 	  macro_build_lui (&offset_expr, AT);
8818 	  used_at = 1;
8819 	}
8820       else
8821 	{
8822 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8823 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
8824 	  used_at = 1;
8825 	}
8826 
8827       /* Now we load the register(s).  */
8828       if (HAVE_64BIT_GPRS)
8829 	{
8830 	  used_at = 1;
8831 	  macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8832 	}
8833       else
8834 	{
8835 	  used_at = 1;
8836 	  macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8837 	  if (treg != RA)
8838 	    {
8839 	      /* FIXME: How in the world do we deal with the possible
8840 		 overflow here?  */
8841 	      offset_expr.X_add_number += 4;
8842 	      macro_build (&offset_expr, "lw", "t,o(b)",
8843 			   treg + 1, BFD_RELOC_LO16, AT);
8844 	    }
8845 	}
8846       break;
8847 
8848     case M_LI_DD:
8849       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
8850          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8851          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
8852          the value and the low order 32 bits are either zero or in
8853          OFFSET_EXPR.  */
8854       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8855 	{
8856 	  used_at = 1;
8857 	  load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
8858 	  if (HAVE_64BIT_FPRS)
8859 	    {
8860 	      gas_assert (HAVE_64BIT_GPRS);
8861 	      macro_build (NULL, "dmtc1", "t,S", AT, treg);
8862 	    }
8863 	  else
8864 	    {
8865 	      macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
8866 	      if (offset_expr.X_op == O_absent)
8867 		macro_build (NULL, "mtc1", "t,G", 0, treg);
8868 	      else
8869 		{
8870 		  gas_assert (offset_expr.X_op == O_constant);
8871 		  load_register (AT, &offset_expr, 0);
8872 		  macro_build (NULL, "mtc1", "t,G", AT, treg);
8873 		}
8874 	    }
8875 	  break;
8876 	}
8877 
8878       gas_assert (offset_expr.X_op == O_symbol
8879 		  && offset_expr.X_add_number == 0);
8880       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8881       if (strcmp (s, ".lit8") == 0)
8882 	{
8883 	  if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
8884 	    {
8885 	      macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
8886 			   BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8887 	      break;
8888 	    }
8889 	  breg = mips_gp_register;
8890 	  r = BFD_RELOC_MIPS_LITERAL;
8891 	  goto dob;
8892 	}
8893       else
8894 	{
8895 	  gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8896 	  used_at = 1;
8897 	  if (mips_pic != NO_PIC)
8898 	    macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8899 			 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8900 	  else
8901 	    {
8902 	      /* FIXME: This won't work for a 64 bit address.  */
8903 	      macro_build_lui (&offset_expr, AT);
8904 	    }
8905 
8906 	  if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
8907 	    {
8908 	      macro_build (&offset_expr, "ldc1", "T,o(b)",
8909 			   treg, BFD_RELOC_LO16, AT);
8910 	      break;
8911 	    }
8912 	  breg = AT;
8913 	  r = BFD_RELOC_LO16;
8914 	  goto dob;
8915 	}
8916 
8917     case M_L_DOB:
8918       /* Even on a big endian machine $fn comes before $fn+1.  We have
8919 	 to adjust when loading from memory.  */
8920       r = BFD_RELOC_LO16;
8921     dob:
8922       gas_assert (!mips_opts.micromips);
8923       gas_assert (mips_opts.isa == ISA_MIPS1);
8924       macro_build (&offset_expr, "lwc1", "T,o(b)",
8925 		   target_big_endian ? treg + 1 : treg, r, breg);
8926       /* FIXME: A possible overflow which I don't know how to deal
8927 	 with.  */
8928       offset_expr.X_add_number += 4;
8929       macro_build (&offset_expr, "lwc1", "T,o(b)",
8930 		   target_big_endian ? treg : treg + 1, r, breg);
8931       break;
8932 
8933     case M_S_DOB:
8934       gas_assert (!mips_opts.micromips);
8935       gas_assert (mips_opts.isa == ISA_MIPS1);
8936       /* Even on a big endian machine $fn comes before $fn+1.  We have
8937 	 to adjust when storing to memory.  */
8938       macro_build (&offset_expr, "swc1", "T,o(b)",
8939 		   target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8940       offset_expr.X_add_number += 4;
8941       macro_build (&offset_expr, "swc1", "T,o(b)",
8942 		   target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8943       break;
8944 
8945     case M_L_DAB:
8946       gas_assert (!mips_opts.micromips);
8947       /*
8948        * The MIPS assembler seems to check for X_add_number not
8949        * being double aligned and generating:
8950        *	lui	at,%hi(foo+1)
8951        *	addu	at,at,v1
8952        *	addiu	at,at,%lo(foo+1)
8953        *	lwc1	f2,0(at)
8954        *	lwc1	f3,4(at)
8955        * But, the resulting address is the same after relocation so why
8956        * generate the extra instruction?
8957        */
8958       /* Itbl support may require additional care here.  */
8959       coproc = 1;
8960       fmt = "T,o(b)";
8961       if (mips_opts.isa != ISA_MIPS1)
8962 	{
8963 	  s = "ldc1";
8964 	  goto ld_st;
8965 	}
8966       s = "lwc1";
8967       goto ldd_std;
8968 
8969     case M_S_DAB:
8970       gas_assert (!mips_opts.micromips);
8971       /* Itbl support may require additional care here.  */
8972       coproc = 1;
8973       fmt = "T,o(b)";
8974       if (mips_opts.isa != ISA_MIPS1)
8975 	{
8976 	  s = "sdc1";
8977 	  goto ld_st;
8978 	}
8979       s = "swc1";
8980       goto ldd_std;
8981 
8982     case M_LD_AB:
8983       fmt = "t,o(b)";
8984       if (HAVE_64BIT_GPRS)
8985 	{
8986 	  s = "ld";
8987 	  goto ld;
8988 	}
8989       s = "lw";
8990       goto ldd_std;
8991 
8992     case M_SD_AB:
8993       fmt = "t,o(b)";
8994       if (HAVE_64BIT_GPRS)
8995 	{
8996 	  s = "sd";
8997 	  goto ld_st;
8998 	}
8999       s = "sw";
9000 
9001     ldd_std:
9002       if (offset_expr.X_op != O_symbol
9003 	  && offset_expr.X_op != O_constant)
9004 	{
9005 	  as_bad (_("Expression too complex"));
9006 	  offset_expr.X_op = O_constant;
9007 	}
9008 
9009       if (HAVE_32BIT_ADDRESSES
9010 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
9011 	{
9012 	  char value [32];
9013 
9014 	  sprintf_vma (value, offset_expr.X_add_number);
9015 	  as_bad (_("Number (0x%s) larger than 32 bits"), value);
9016 	}
9017 
9018       /* Even on a big endian machine $fn comes before $fn+1.  We have
9019 	 to adjust when loading from memory.  We set coproc if we must
9020 	 load $fn+1 first.  */
9021       /* Itbl support may require additional care here.  */
9022       if (!target_big_endian)
9023 	coproc = 0;
9024 
9025       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
9026 	{
9027 	  /* If this is a reference to a GP relative symbol, we want
9028 	       <op>	$treg,<sym>($gp)	(BFD_RELOC_GPREL16)
9029 	       <op>	$treg+1,<sym>+4($gp)	(BFD_RELOC_GPREL16)
9030 	     If we have a base register, we use this
9031 	       addu	$at,$breg,$gp
9032 	       <op>	$treg,<sym>($at)	(BFD_RELOC_GPREL16)
9033 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_GPREL16)
9034 	     If this is not a GP relative symbol, we want
9035 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
9036 	       <op>	$treg,<sym>($at)	(BFD_RELOC_LO16)
9037 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_LO16)
9038 	     If there is a base register, we add it to $at after the
9039 	     lui instruction.  If there is a constant, we always use
9040 	     the last case.  */
9041 	  if (offset_expr.X_op == O_symbol
9042 	      && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
9043 	      && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9044 	    {
9045 	      relax_start (offset_expr.X_add_symbol);
9046 	      if (breg == 0)
9047 		{
9048 		  tempreg = mips_gp_register;
9049 		}
9050 	      else
9051 		{
9052 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9053 			       AT, breg, mips_gp_register);
9054 		  tempreg = AT;
9055 		  used_at = 1;
9056 		}
9057 
9058 	      /* Itbl support may require additional care here.  */
9059 	      macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9060 			   BFD_RELOC_GPREL16, tempreg);
9061 	      offset_expr.X_add_number += 4;
9062 
9063 	      /* Set mips_optimize to 2 to avoid inserting an
9064                  undesired nop.  */
9065 	      hold_mips_optimize = mips_optimize;
9066 	      mips_optimize = 2;
9067 	      /* Itbl support may require additional care here.  */
9068 	      macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9069 			   BFD_RELOC_GPREL16, tempreg);
9070 	      mips_optimize = hold_mips_optimize;
9071 
9072 	      relax_switch ();
9073 
9074 	      offset_expr.X_add_number -= 4;
9075 	    }
9076 	  used_at = 1;
9077 	  macro_build_lui (&offset_expr, AT);
9078 	  if (breg != 0)
9079 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9080 	  /* Itbl support may require additional care here.  */
9081 	  macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9082 		       BFD_RELOC_LO16, AT);
9083 	  /* FIXME: How do we handle overflow here?  */
9084 	  offset_expr.X_add_number += 4;
9085 	  /* Itbl support may require additional care here.  */
9086 	  macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9087 		       BFD_RELOC_LO16, AT);
9088 	  if (mips_relax.sequence)
9089 	    relax_end ();
9090 	}
9091       else if (!mips_big_got)
9092 	{
9093 	  /* If this is a reference to an external symbol, we want
9094 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9095 	       nop
9096 	       <op>	$treg,0($at)
9097 	       <op>	$treg+1,4($at)
9098 	     Otherwise we want
9099 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9100 	       nop
9101 	       <op>	$treg,<sym>($at)	(BFD_RELOC_LO16)
9102 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_LO16)
9103 	     If there is a base register we add it to $at before the
9104 	     lwc1 instructions.  If there is a constant we include it
9105 	     in the lwc1 instructions.  */
9106 	  used_at = 1;
9107 	  expr1.X_add_number = offset_expr.X_add_number;
9108 	  if (expr1.X_add_number < -0x8000
9109 	      || expr1.X_add_number >= 0x8000 - 4)
9110 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9111 	  load_got_offset (AT, &offset_expr);
9112 	  load_delay_nop ();
9113 	  if (breg != 0)
9114 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9115 
9116 	  /* Set mips_optimize to 2 to avoid inserting an undesired
9117              nop.  */
9118 	  hold_mips_optimize = mips_optimize;
9119 	  mips_optimize = 2;
9120 
9121 	  /* Itbl support may require additional care here.  */
9122 	  relax_start (offset_expr.X_add_symbol);
9123 	  macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9124 		       BFD_RELOC_LO16, AT);
9125 	  expr1.X_add_number += 4;
9126 	  macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9127 		       BFD_RELOC_LO16, AT);
9128 	  relax_switch ();
9129 	  macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9130 		       BFD_RELOC_LO16, AT);
9131 	  offset_expr.X_add_number += 4;
9132 	  macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9133 		       BFD_RELOC_LO16, AT);
9134 	  relax_end ();
9135 
9136 	  mips_optimize = hold_mips_optimize;
9137 	}
9138       else if (mips_big_got)
9139 	{
9140 	  int gpdelay;
9141 
9142 	  /* If this is a reference to an external symbol, we want
9143 	       lui	$at,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
9144 	       addu	$at,$at,$gp
9145 	       lw	$at,<sym>($at)		(BFD_RELOC_MIPS_GOT_LO16)
9146 	       nop
9147 	       <op>	$treg,0($at)
9148 	       <op>	$treg+1,4($at)
9149 	     Otherwise we want
9150 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9151 	       nop
9152 	       <op>	$treg,<sym>($at)	(BFD_RELOC_LO16)
9153 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_LO16)
9154 	     If there is a base register we add it to $at before the
9155 	     lwc1 instructions.  If there is a constant we include it
9156 	     in the lwc1 instructions.  */
9157 	  used_at = 1;
9158 	  expr1.X_add_number = offset_expr.X_add_number;
9159 	  offset_expr.X_add_number = 0;
9160 	  if (expr1.X_add_number < -0x8000
9161 	      || expr1.X_add_number >= 0x8000 - 4)
9162 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9163 	  gpdelay = reg_needs_delay (mips_gp_register);
9164 	  relax_start (offset_expr.X_add_symbol);
9165 	  macro_build (&offset_expr, "lui", LUI_FMT,
9166 		       AT, BFD_RELOC_MIPS_GOT_HI16);
9167 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9168 		       AT, AT, mips_gp_register);
9169 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9170 		       AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9171 	  load_delay_nop ();
9172 	  if (breg != 0)
9173 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9174 	  /* Itbl support may require additional care here.  */
9175 	  macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9176 		       BFD_RELOC_LO16, AT);
9177 	  expr1.X_add_number += 4;
9178 
9179 	  /* Set mips_optimize to 2 to avoid inserting an undesired
9180              nop.  */
9181 	  hold_mips_optimize = mips_optimize;
9182 	  mips_optimize = 2;
9183 	  /* Itbl support may require additional care here.  */
9184 	  macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9185 		       BFD_RELOC_LO16, AT);
9186 	  mips_optimize = hold_mips_optimize;
9187 	  expr1.X_add_number -= 4;
9188 
9189 	  relax_switch ();
9190 	  offset_expr.X_add_number = expr1.X_add_number;
9191 	  if (gpdelay)
9192 	    macro_build (NULL, "nop", "");
9193 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9194 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9195 	  load_delay_nop ();
9196 	  if (breg != 0)
9197 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9198 	  /* Itbl support may require additional care here.  */
9199 	  macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9200 		       BFD_RELOC_LO16, AT);
9201 	  offset_expr.X_add_number += 4;
9202 
9203 	  /* Set mips_optimize to 2 to avoid inserting an undesired
9204              nop.  */
9205 	  hold_mips_optimize = mips_optimize;
9206 	  mips_optimize = 2;
9207 	  /* Itbl support may require additional care here.  */
9208 	  macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9209 		       BFD_RELOC_LO16, AT);
9210 	  mips_optimize = hold_mips_optimize;
9211 	  relax_end ();
9212 	}
9213       else
9214 	abort ();
9215 
9216       break;
9217 
9218     case M_LD_OB:
9219       s = HAVE_64BIT_GPRS ? "ld" : "lw";
9220       goto sd_ob;
9221     case M_SD_OB:
9222       s = HAVE_64BIT_GPRS ? "sd" : "sw";
9223     sd_ob:
9224       macro_build (&offset_expr, s, "t,o(b)", treg,
9225 		   -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9226 		   breg);
9227       if (!HAVE_64BIT_GPRS)
9228 	{
9229 	  offset_expr.X_add_number += 4;
9230 	  macro_build (&offset_expr, s, "t,o(b)", treg + 1,
9231 		       -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9232 		       breg);
9233 	}
9234       break;
9235 
9236 
9237     case M_SAA_AB:
9238       ab = (offset_expr.X_op != O_constant || offset_expr.X_add_number != 0);
9239     case M_SAA_OB:
9240       s = "saa";
9241       off0 = 1;
9242       fmt = "t,(b)";
9243       if (!ab)
9244 	{
9245 	  tempreg = AT;
9246 	  goto ld_noat;
9247 	}
9248       goto ld_st;
9249     case M_SAAD_AB:
9250       ab = (offset_expr.X_op != O_constant || offset_expr.X_add_number != 0);
9251     case M_SAAD_OB:
9252       s = "saad";
9253       off0 = 1;
9254       fmt = "t,(b)";
9255       if (!ab)
9256 	{
9257 	  tempreg = AT;
9258 	  goto ld_noat;
9259 	}
9260       goto ld_st;
9261 
9262    /* New code added to support COPZ instructions.
9263       This code builds table entries out of the macros in mip_opcodes.
9264       R4000 uses interlocks to handle coproc delays.
9265       Other chips (like the R3000) require nops to be inserted for delays.
9266 
9267       FIXME: Currently, we require that the user handle delays.
9268       In order to fill delay slots for non-interlocked chips,
9269       we must have a way to specify delays based on the coprocessor.
9270       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9271       What are the side-effects of the cop instruction?
9272       What cache support might we have and what are its effects?
9273       Both coprocessor & memory require delays. how long???
9274       What registers are read/set/modified?
9275 
9276       If an itbl is provided to interpret cop instructions,
9277       this knowledge can be encoded in the itbl spec.  */
9278 
9279     case M_COP0:
9280       s = "c0";
9281       goto copz;
9282     case M_COP1:
9283       s = "c1";
9284       goto copz;
9285     case M_COP2:
9286       s = "c2";
9287       goto copz;
9288     case M_COP3:
9289       s = "c3";
9290     copz:
9291       gas_assert (!mips_opts.micromips);
9292       /* For now we just do C (same as Cz).  The parameter will be
9293          stored in insn_opcode by mips_ip.  */
9294       macro_build (NULL, s, "C", ip->insn_opcode);
9295       break;
9296 
9297     case M_MOVE:
9298       move_register (dreg, sreg);
9299       break;
9300 
9301     case M_DMUL:
9302       dbl = 1;
9303     case M_MUL:
9304       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9305       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9306       break;
9307 
9308     case M_DMUL_I:
9309       dbl = 1;
9310     case M_MUL_I:
9311       /* The MIPS assembler some times generates shifts and adds.  I'm
9312 	 not trying to be that fancy. GCC should do this for us
9313 	 anyway.  */
9314       used_at = 1;
9315       load_register (AT, &imm_expr, dbl);
9316       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9317       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9318       break;
9319 
9320     case M_DMULO_I:
9321       dbl = 1;
9322     case M_MULO_I:
9323       imm = 1;
9324       goto do_mulo;
9325 
9326     case M_DMULO:
9327       dbl = 1;
9328     case M_MULO:
9329     do_mulo:
9330       start_noreorder ();
9331       used_at = 1;
9332       if (imm)
9333 	load_register (AT, &imm_expr, dbl);
9334       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9335       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9336       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9337       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9338       if (mips_trap)
9339 	macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9340       else
9341 	{
9342 	  if (mips_opts.micromips)
9343 	    micromips_label_expr (&label_expr);
9344 	  else
9345 	    label_expr.X_add_number = 8;
9346 	  macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9347 	  macro_build (NULL, "nop", "");
9348 	  macro_build (NULL, "break", BRK_FMT, 6);
9349 	  if (mips_opts.micromips)
9350 	    micromips_add_label ();
9351 	}
9352       end_noreorder ();
9353       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9354       break;
9355 
9356     case M_DMULOU_I:
9357       dbl = 1;
9358     case M_MULOU_I:
9359       imm = 1;
9360       goto do_mulou;
9361 
9362     case M_DMULOU:
9363       dbl = 1;
9364     case M_MULOU:
9365     do_mulou:
9366       start_noreorder ();
9367       used_at = 1;
9368       if (imm)
9369 	load_register (AT, &imm_expr, dbl);
9370       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9371 		   sreg, imm ? AT : treg);
9372       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9373       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9374       if (mips_trap)
9375 	macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9376       else
9377 	{
9378 	  if (mips_opts.micromips)
9379 	    micromips_label_expr (&label_expr);
9380 	  else
9381 	    label_expr.X_add_number = 8;
9382 	  macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9383 	  macro_build (NULL, "nop", "");
9384 	  macro_build (NULL, "break", BRK_FMT, 6);
9385 	  if (mips_opts.micromips)
9386 	    micromips_add_label ();
9387 	}
9388       end_noreorder ();
9389       break;
9390 
9391     case M_DROL:
9392       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9393 	{
9394 	  if (dreg == sreg)
9395 	    {
9396 	      tempreg = AT;
9397 	      used_at = 1;
9398 	    }
9399 	  else
9400 	    {
9401 	      tempreg = dreg;
9402 	    }
9403 	  macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9404 	  macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9405 	  break;
9406 	}
9407       used_at = 1;
9408       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9409       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9410       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9411       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9412       break;
9413 
9414     case M_ROL:
9415       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9416 	{
9417 	  if (dreg == sreg)
9418 	    {
9419 	      tempreg = AT;
9420 	      used_at = 1;
9421 	    }
9422 	  else
9423 	    {
9424 	      tempreg = dreg;
9425 	    }
9426 	  macro_build (NULL, "negu", "d,w", tempreg, treg);
9427 	  macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9428 	  break;
9429 	}
9430       used_at = 1;
9431       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9432       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9433       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9434       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9435       break;
9436 
9437     case M_DROL_I:
9438       {
9439 	unsigned int rot;
9440 	char *l;
9441 	char *rr;
9442 
9443 	if (imm_expr.X_op != O_constant)
9444 	  as_bad (_("Improper rotate count"));
9445 	rot = imm_expr.X_add_number & 0x3f;
9446 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9447 	  {
9448 	    rot = (64 - rot) & 0x3f;
9449 	    if (rot >= 32)
9450 	      macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9451 	    else
9452 	      macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9453 	    break;
9454 	  }
9455 	if (rot == 0)
9456 	  {
9457 	    macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9458 	    break;
9459 	  }
9460 	l = (rot < 0x20) ? "dsll" : "dsll32";
9461 	rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9462 	rot &= 0x1f;
9463 	used_at = 1;
9464 	macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9465 	macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9466 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9467       }
9468       break;
9469 
9470     case M_ROL_I:
9471       {
9472 	unsigned int rot;
9473 
9474 	if (imm_expr.X_op != O_constant)
9475 	  as_bad (_("Improper rotate count"));
9476 	rot = imm_expr.X_add_number & 0x1f;
9477 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9478 	  {
9479 	    macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9480 	    break;
9481 	  }
9482 	if (rot == 0)
9483 	  {
9484 	    macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9485 	    break;
9486 	  }
9487 	used_at = 1;
9488 	macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9489 	macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9490 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9491       }
9492       break;
9493 
9494     case M_DROR:
9495       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9496 	{
9497 	  macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9498 	  break;
9499 	}
9500       used_at = 1;
9501       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9502       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9503       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9504       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9505       break;
9506 
9507     case M_ROR:
9508       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9509 	{
9510 	  macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9511 	  break;
9512 	}
9513       used_at = 1;
9514       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9515       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9516       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9517       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9518       break;
9519 
9520     case M_DROR_I:
9521       {
9522 	unsigned int rot;
9523 	char *l;
9524 	char *rr;
9525 
9526 	if (imm_expr.X_op != O_constant)
9527 	  as_bad (_("Improper rotate count"));
9528 	rot = imm_expr.X_add_number & 0x3f;
9529 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9530 	  {
9531 	    if (rot >= 32)
9532 	      macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9533 	    else
9534 	      macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9535 	    break;
9536 	  }
9537 	if (rot == 0)
9538 	  {
9539 	    macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9540 	    break;
9541 	  }
9542 	rr = (rot < 0x20) ? "dsrl" : "dsrl32";
9543 	l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9544 	rot &= 0x1f;
9545 	used_at = 1;
9546 	macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9547 	macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9548 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9549       }
9550       break;
9551 
9552     case M_ROR_I:
9553       {
9554 	unsigned int rot;
9555 
9556 	if (imm_expr.X_op != O_constant)
9557 	  as_bad (_("Improper rotate count"));
9558 	rot = imm_expr.X_add_number & 0x1f;
9559 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9560 	  {
9561 	    macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
9562 	    break;
9563 	  }
9564 	if (rot == 0)
9565 	  {
9566 	    macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9567 	    break;
9568 	  }
9569 	used_at = 1;
9570 	macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9571 	macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9572 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9573       }
9574       break;
9575 
9576     case M_SEQ:
9577       if (sreg == 0)
9578 	macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
9579       else if (treg == 0)
9580 	macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9581       else
9582 	{
9583 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9584 	  macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9585 	}
9586       break;
9587 
9588     case M_SEQ_I:
9589       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9590 	{
9591 	  macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9592 	  break;
9593 	}
9594       if (sreg == 0)
9595 	{
9596 	  as_warn (_("Instruction %s: result is always false"),
9597 		   ip->insn_mo->name);
9598 	  move_register (dreg, 0);
9599 	  break;
9600 	}
9601       if (CPU_HAS_SEQ (mips_opts.arch)
9602 	  && -512 <= imm_expr.X_add_number
9603 	  && imm_expr.X_add_number < 512)
9604 	{
9605 	  macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
9606 		       (int) imm_expr.X_add_number);
9607 	  break;
9608 	}
9609       if (imm_expr.X_op == O_constant
9610 	  && imm_expr.X_add_number >= 0
9611 	  && imm_expr.X_add_number < 0x10000)
9612 	{
9613 	  macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9614 	}
9615       else if (imm_expr.X_op == O_constant
9616 	       && imm_expr.X_add_number > -0x8000
9617 	       && imm_expr.X_add_number < 0)
9618 	{
9619 	  imm_expr.X_add_number = -imm_expr.X_add_number;
9620 	  macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9621 		       "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9622 	}
9623       else if (CPU_HAS_SEQ (mips_opts.arch))
9624 	{
9625 	  used_at = 1;
9626 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9627 	  macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9628 	  break;
9629 	}
9630       else
9631 	{
9632 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9633 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9634 	  used_at = 1;
9635 	}
9636       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9637       break;
9638 
9639     case M_SGE:		/* sreg >= treg <==> not (sreg < treg) */
9640       s = "slt";
9641       goto sge;
9642     case M_SGEU:
9643       s = "sltu";
9644     sge:
9645       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9646       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9647       break;
9648 
9649     case M_SGE_I:		/* sreg >= I <==> not (sreg < I) */
9650     case M_SGEU_I:
9651       if (imm_expr.X_op == O_constant
9652 	  && imm_expr.X_add_number >= -0x8000
9653 	  && imm_expr.X_add_number < 0x8000)
9654 	{
9655 	  macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9656 		       dreg, sreg, BFD_RELOC_LO16);
9657 	}
9658       else
9659 	{
9660 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9661 	  macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9662 		       dreg, sreg, AT);
9663 	  used_at = 1;
9664 	}
9665       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9666       break;
9667 
9668     case M_SGT:		/* sreg > treg  <==>  treg < sreg */
9669       s = "slt";
9670       goto sgt;
9671     case M_SGTU:
9672       s = "sltu";
9673     sgt:
9674       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9675       break;
9676 
9677     case M_SGT_I:		/* sreg > I  <==>  I < sreg */
9678       s = "slt";
9679       goto sgti;
9680     case M_SGTU_I:
9681       s = "sltu";
9682     sgti:
9683       used_at = 1;
9684       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9685       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9686       break;
9687 
9688     case M_SLE:	/* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
9689       s = "slt";
9690       goto sle;
9691     case M_SLEU:
9692       s = "sltu";
9693     sle:
9694       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9695       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9696       break;
9697 
9698     case M_SLE_I:	/* sreg <= I <==> I >= sreg <==> not (I < sreg) */
9699       s = "slt";
9700       goto slei;
9701     case M_SLEU_I:
9702       s = "sltu";
9703     slei:
9704       used_at = 1;
9705       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9706       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9707       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9708       break;
9709 
9710     case M_SLT_I:
9711       if (imm_expr.X_op == O_constant
9712 	  && imm_expr.X_add_number >= -0x8000
9713 	  && imm_expr.X_add_number < 0x8000)
9714 	{
9715 	  macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9716 	  break;
9717 	}
9718       used_at = 1;
9719       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9720       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
9721       break;
9722 
9723     case M_SLTU_I:
9724       if (imm_expr.X_op == O_constant
9725 	  && imm_expr.X_add_number >= -0x8000
9726 	  && imm_expr.X_add_number < 0x8000)
9727 	{
9728 	  macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
9729 		       BFD_RELOC_LO16);
9730 	  break;
9731 	}
9732       used_at = 1;
9733       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9734       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
9735       break;
9736 
9737     case M_SNE:
9738       if (sreg == 0)
9739 	macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
9740       else if (treg == 0)
9741 	macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9742       else
9743 	{
9744 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9745 	  macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9746 	}
9747       break;
9748 
9749     case M_SNE_I:
9750       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9751 	{
9752 	  macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9753 	  break;
9754 	}
9755       if (sreg == 0)
9756 	{
9757 	  as_warn (_("Instruction %s: result is always true"),
9758 		   ip->insn_mo->name);
9759 	  macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9760 		       dreg, 0, BFD_RELOC_LO16);
9761 	  break;
9762 	}
9763       if (CPU_HAS_SEQ (mips_opts.arch)
9764 	  && -512 <= imm_expr.X_add_number
9765 	  && imm_expr.X_add_number < 512)
9766 	{
9767 	  macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
9768 		       (int) imm_expr.X_add_number);
9769 	  break;
9770 	}
9771       if (imm_expr.X_op == O_constant
9772 	  && imm_expr.X_add_number >= 0
9773 	  && imm_expr.X_add_number < 0x10000)
9774 	{
9775 	  macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9776 	}
9777       else if (imm_expr.X_op == O_constant
9778 	       && imm_expr.X_add_number > -0x8000
9779 	       && imm_expr.X_add_number < 0)
9780 	{
9781 	  imm_expr.X_add_number = -imm_expr.X_add_number;
9782 	  macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9783 		       "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9784 	}
9785       else if (CPU_HAS_SEQ (mips_opts.arch))
9786 	{
9787 	  used_at = 1;
9788 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9789 	  macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9790 	  break;
9791 	}
9792       else
9793 	{
9794 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9795 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9796 	  used_at = 1;
9797 	}
9798       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9799       break;
9800 
9801     case M_SUB_I:
9802       s = "addi";
9803       s2 = "sub";
9804       goto do_subi;
9805     case M_SUBU_I:
9806       s = "addiu";
9807       s2 = "subu";
9808       goto do_subi;
9809     case M_DSUB_I:
9810       dbl = 1;
9811       s = "daddi";
9812       s2 = "dsub";
9813       if (!mips_opts.micromips)
9814 	goto do_subi;
9815       if (imm_expr.X_op == O_constant
9816 	  && imm_expr.X_add_number > -0x200
9817 	  && imm_expr.X_add_number <= 0x200)
9818 	{
9819 	  macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
9820 	  break;
9821 	}
9822       goto do_subi_i;
9823     case M_DSUBU_I:
9824       dbl = 1;
9825       s = "daddiu";
9826       s2 = "dsubu";
9827     do_subi:
9828       if (imm_expr.X_op == O_constant
9829 	  && imm_expr.X_add_number > -0x8000
9830 	  && imm_expr.X_add_number <= 0x8000)
9831 	{
9832 	  imm_expr.X_add_number = -imm_expr.X_add_number;
9833 	  macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9834 	  break;
9835 	}
9836     do_subi_i:
9837       used_at = 1;
9838       load_register (AT, &imm_expr, dbl);
9839       macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
9840       break;
9841 
9842     case M_TEQ_I:
9843       s = "teq";
9844       goto trap;
9845     case M_TGE_I:
9846       s = "tge";
9847       goto trap;
9848     case M_TGEU_I:
9849       s = "tgeu";
9850       goto trap;
9851     case M_TLT_I:
9852       s = "tlt";
9853       goto trap;
9854     case M_TLTU_I:
9855       s = "tltu";
9856       goto trap;
9857     case M_TNE_I:
9858       s = "tne";
9859     trap:
9860       used_at = 1;
9861       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9862       macro_build (NULL, s, "s,t", sreg, AT);
9863       break;
9864 
9865     case M_TRUNCWS:
9866     case M_TRUNCWD:
9867       gas_assert (!mips_opts.micromips);
9868       gas_assert (mips_opts.isa == ISA_MIPS1);
9869       used_at = 1;
9870       sreg = (ip->insn_opcode >> 11) & 0x1f;	/* floating reg */
9871       dreg = (ip->insn_opcode >> 06) & 0x1f;	/* floating reg */
9872 
9873       /*
9874        * Is the double cfc1 instruction a bug in the mips assembler;
9875        * or is there a reason for it?
9876        */
9877       start_noreorder ();
9878       macro_build (NULL, "cfc1", "t,G", treg, RA);
9879       macro_build (NULL, "cfc1", "t,G", treg, RA);
9880       macro_build (NULL, "nop", "");
9881       expr1.X_add_number = 3;
9882       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
9883       expr1.X_add_number = 2;
9884       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9885       macro_build (NULL, "ctc1", "t,G", AT, RA);
9886       macro_build (NULL, "nop", "");
9887       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9888 		   dreg, sreg);
9889       macro_build (NULL, "ctc1", "t,G", treg, RA);
9890       macro_build (NULL, "nop", "");
9891       end_noreorder ();
9892       break;
9893 
9894     case M_ULH_A:
9895       ab = 1;
9896     case M_ULH:
9897       s = "lb";
9898       s2 = "lbu";
9899       off = 1;
9900       goto uld_st;
9901     case M_ULHU_A:
9902       ab = 1;
9903     case M_ULHU:
9904       s = "lbu";
9905       s2 = "lbu";
9906       off = 1;
9907       goto uld_st;
9908     case M_ULW_A:
9909       ab = 1;
9910     case M_ULW:
9911       s = "lwl";
9912       s2 = "lwr";
9913       off12 = mips_opts.micromips;
9914       off = 3;
9915       goto uld_st;
9916     case M_ULD_A:
9917       ab = 1;
9918     case M_ULD:
9919       s = "ldl";
9920       s2 = "ldr";
9921       off12 = mips_opts.micromips;
9922       off = 7;
9923       goto uld_st;
9924     case M_USH_A:
9925       ab = 1;
9926     case M_USH:
9927       s = "sb";
9928       s2 = "sb";
9929       off = 1;
9930       ust = 1;
9931       goto uld_st;
9932     case M_USW_A:
9933       ab = 1;
9934     case M_USW:
9935       s = "swl";
9936       s2 = "swr";
9937       off12 = mips_opts.micromips;
9938       off = 3;
9939       ust = 1;
9940       goto uld_st;
9941     case M_USD_A:
9942       ab = 1;
9943     case M_USD:
9944       s = "sdl";
9945       s2 = "sdr";
9946       off12 = mips_opts.micromips;
9947       off = 7;
9948       ust = 1;
9949 
9950     uld_st:
9951       if (!ab && offset_expr.X_add_number >= 0x8000 - off)
9952 	as_bad (_("Operand overflow"));
9953 
9954       ep = &offset_expr;
9955       expr1.X_add_number = 0;
9956       if (ab)
9957 	{
9958 	  used_at = 1;
9959 	  tempreg = AT;
9960 	  load_address (tempreg, ep, &used_at);
9961 	  if (breg != 0)
9962 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9963 			 tempreg, tempreg, breg);
9964 	  breg = tempreg;
9965 	  tempreg = treg;
9966 	  ep = &expr1;
9967 	}
9968       else if (off12
9969 	       && (offset_expr.X_op != O_constant
9970 		   || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
9971 		   || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
9972 	{
9973 	  used_at = 1;
9974 	  tempreg = AT;
9975 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
9976 		       -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
9977 	  breg = tempreg;
9978 	  tempreg = treg;
9979 	  ep = &expr1;
9980 	}
9981       else if (!ust && treg == breg)
9982 	{
9983 	  used_at = 1;
9984 	  tempreg = AT;
9985 	}
9986       else
9987 	tempreg = treg;
9988 
9989       if (off == 1)
9990 	goto ulh_sh;
9991 
9992       if (!target_big_endian)
9993 	ep->X_add_number += off;
9994       if (!off12)
9995 	macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9996       else
9997 	macro_build (NULL, s, "t,~(b)",
9998 		     tempreg, (unsigned long) ep->X_add_number, breg);
9999 
10000       if (!target_big_endian)
10001 	ep->X_add_number -= off;
10002       else
10003 	ep->X_add_number += off;
10004       if (!off12)
10005 	macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10006       else
10007 	macro_build (NULL, s2, "t,~(b)",
10008 		     tempreg, (unsigned long) ep->X_add_number, breg);
10009 
10010       /* If necessary, move the result in tempreg to the final destination.  */
10011       if (!ust && treg != tempreg)
10012         {
10013 	  /* Protect second load's delay slot.  */
10014 	  load_delay_nop ();
10015 	  move_register (treg, tempreg);
10016 	}
10017       break;
10018 
10019     ulh_sh:
10020       used_at = 1;
10021       if (target_big_endian == ust)
10022 	ep->X_add_number += off;
10023       tempreg = ust || ab ? treg : AT;
10024       macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10025 
10026       /* For halfword transfers we need a temporary register to shuffle
10027          bytes.  Unfortunately for M_USH_A we have none available before
10028          the next store as AT holds the base address.  We deal with this
10029          case by clobbering TREG and then restoring it as with ULH.  */
10030       tempreg = ust == ab ? treg : AT;
10031       if (ust)
10032 	macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10033 
10034       if (target_big_endian == ust)
10035 	ep->X_add_number -= off;
10036       else
10037 	ep->X_add_number += off;
10038       macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10039 
10040       /* For M_USH_A re-retrieve the LSB.  */
10041       if (ust && ab)
10042 	{
10043 	  if (target_big_endian)
10044 	    ep->X_add_number += off;
10045 	  else
10046 	    ep->X_add_number -= off;
10047 	  macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10048 	}
10049       /* For ULH and M_USH_A OR the LSB in.  */
10050       if (!ust || ab)
10051 	{
10052 	  tempreg = !ab ? AT : treg;
10053 	  macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10054 	  macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10055 	}
10056       break;
10057 
10058     default:
10059       /* FIXME: Check if this is one of the itbl macros, since they
10060 	 are added dynamically.  */
10061       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10062       break;
10063     }
10064   if (!mips_opts.at && used_at)
10065     as_bad (_("Macro used $at after \".set noat\""));
10066 }
10067 
10068 /* Implement macros in mips16 mode.  */
10069 
10070 static void
10071 mips16_macro (struct mips_cl_insn *ip)
10072 {
10073   int mask;
10074   int xreg, yreg, zreg, tmp;
10075   expressionS expr1;
10076   int dbl;
10077   const char *s, *s2, *s3;
10078 
10079   mask = ip->insn_mo->mask;
10080 
10081   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10082   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10083   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10084 
10085   expr1.X_op = O_constant;
10086   expr1.X_op_symbol = NULL;
10087   expr1.X_add_symbol = NULL;
10088   expr1.X_add_number = 1;
10089 
10090   dbl = 0;
10091 
10092   switch (mask)
10093     {
10094     default:
10095       internalError ();
10096 
10097     case M_DDIV_3:
10098       dbl = 1;
10099     case M_DIV_3:
10100       s = "mflo";
10101       goto do_div3;
10102     case M_DREM_3:
10103       dbl = 1;
10104     case M_REM_3:
10105       s = "mfhi";
10106     do_div3:
10107       start_noreorder ();
10108       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10109       expr1.X_add_number = 2;
10110       macro_build (&expr1, "bnez", "x,p", yreg);
10111       macro_build (NULL, "break", "6", 7);
10112 
10113       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10114          since that causes an overflow.  We should do that as well,
10115          but I don't see how to do the comparisons without a temporary
10116          register.  */
10117       end_noreorder ();
10118       macro_build (NULL, s, "x", zreg);
10119       break;
10120 
10121     case M_DIVU_3:
10122       s = "divu";
10123       s2 = "mflo";
10124       goto do_divu3;
10125     case M_REMU_3:
10126       s = "divu";
10127       s2 = "mfhi";
10128       goto do_divu3;
10129     case M_DDIVU_3:
10130       s = "ddivu";
10131       s2 = "mflo";
10132       goto do_divu3;
10133     case M_DREMU_3:
10134       s = "ddivu";
10135       s2 = "mfhi";
10136     do_divu3:
10137       start_noreorder ();
10138       macro_build (NULL, s, "0,x,y", xreg, yreg);
10139       expr1.X_add_number = 2;
10140       macro_build (&expr1, "bnez", "x,p", yreg);
10141       macro_build (NULL, "break", "6", 7);
10142       end_noreorder ();
10143       macro_build (NULL, s2, "x", zreg);
10144       break;
10145 
10146     case M_DMUL:
10147       dbl = 1;
10148     case M_MUL:
10149       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10150       macro_build (NULL, "mflo", "x", zreg);
10151       break;
10152 
10153     case M_DSUBU_I:
10154       dbl = 1;
10155       goto do_subu;
10156     case M_SUBU_I:
10157     do_subu:
10158       if (imm_expr.X_op != O_constant)
10159 	as_bad (_("Unsupported large constant"));
10160       imm_expr.X_add_number = -imm_expr.X_add_number;
10161       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10162       break;
10163 
10164     case M_SUBU_I_2:
10165       if (imm_expr.X_op != O_constant)
10166 	as_bad (_("Unsupported large constant"));
10167       imm_expr.X_add_number = -imm_expr.X_add_number;
10168       macro_build (&imm_expr, "addiu", "x,k", xreg);
10169       break;
10170 
10171     case M_DSUBU_I_2:
10172       if (imm_expr.X_op != O_constant)
10173 	as_bad (_("Unsupported large constant"));
10174       imm_expr.X_add_number = -imm_expr.X_add_number;
10175       macro_build (&imm_expr, "daddiu", "y,j", yreg);
10176       break;
10177 
10178     case M_BEQ:
10179       s = "cmp";
10180       s2 = "bteqz";
10181       goto do_branch;
10182     case M_BNE:
10183       s = "cmp";
10184       s2 = "btnez";
10185       goto do_branch;
10186     case M_BLT:
10187       s = "slt";
10188       s2 = "btnez";
10189       goto do_branch;
10190     case M_BLTU:
10191       s = "sltu";
10192       s2 = "btnez";
10193       goto do_branch;
10194     case M_BLE:
10195       s = "slt";
10196       s2 = "bteqz";
10197       goto do_reverse_branch;
10198     case M_BLEU:
10199       s = "sltu";
10200       s2 = "bteqz";
10201       goto do_reverse_branch;
10202     case M_BGE:
10203       s = "slt";
10204       s2 = "bteqz";
10205       goto do_branch;
10206     case M_BGEU:
10207       s = "sltu";
10208       s2 = "bteqz";
10209       goto do_branch;
10210     case M_BGT:
10211       s = "slt";
10212       s2 = "btnez";
10213       goto do_reverse_branch;
10214     case M_BGTU:
10215       s = "sltu";
10216       s2 = "btnez";
10217 
10218     do_reverse_branch:
10219       tmp = xreg;
10220       xreg = yreg;
10221       yreg = tmp;
10222 
10223     do_branch:
10224       macro_build (NULL, s, "x,y", xreg, yreg);
10225       macro_build (&offset_expr, s2, "p");
10226       break;
10227 
10228     case M_BEQ_I:
10229       s = "cmpi";
10230       s2 = "bteqz";
10231       s3 = "x,U";
10232       goto do_branch_i;
10233     case M_BNE_I:
10234       s = "cmpi";
10235       s2 = "btnez";
10236       s3 = "x,U";
10237       goto do_branch_i;
10238     case M_BLT_I:
10239       s = "slti";
10240       s2 = "btnez";
10241       s3 = "x,8";
10242       goto do_branch_i;
10243     case M_BLTU_I:
10244       s = "sltiu";
10245       s2 = "btnez";
10246       s3 = "x,8";
10247       goto do_branch_i;
10248     case M_BLE_I:
10249       s = "slti";
10250       s2 = "btnez";
10251       s3 = "x,8";
10252       goto do_addone_branch_i;
10253     case M_BLEU_I:
10254       s = "sltiu";
10255       s2 = "btnez";
10256       s3 = "x,8";
10257       goto do_addone_branch_i;
10258     case M_BGE_I:
10259       s = "slti";
10260       s2 = "bteqz";
10261       s3 = "x,8";
10262       goto do_branch_i;
10263     case M_BGEU_I:
10264       s = "sltiu";
10265       s2 = "bteqz";
10266       s3 = "x,8";
10267       goto do_branch_i;
10268     case M_BGT_I:
10269       s = "slti";
10270       s2 = "bteqz";
10271       s3 = "x,8";
10272       goto do_addone_branch_i;
10273     case M_BGTU_I:
10274       s = "sltiu";
10275       s2 = "bteqz";
10276       s3 = "x,8";
10277 
10278     do_addone_branch_i:
10279       if (imm_expr.X_op != O_constant)
10280 	as_bad (_("Unsupported large constant"));
10281       ++imm_expr.X_add_number;
10282 
10283     do_branch_i:
10284       macro_build (&imm_expr, s, s3, xreg);
10285       macro_build (&offset_expr, s2, "p");
10286       break;
10287 
10288     case M_ABS:
10289       expr1.X_add_number = 0;
10290       macro_build (&expr1, "slti", "x,8", yreg);
10291       if (xreg != yreg)
10292 	move_register (xreg, yreg);
10293       expr1.X_add_number = 2;
10294       macro_build (&expr1, "bteqz", "p");
10295       macro_build (NULL, "neg", "x,w", xreg, xreg);
10296     }
10297 }
10298 
10299 /* For consistency checking, verify that all bits are specified either
10300    by the match/mask part of the instruction definition, or by the
10301    operand list.  */
10302 static int
10303 validate_mips_insn (const struct mips_opcode *opc)
10304 {
10305   const char *p = opc->args;
10306   char c;
10307   unsigned long used_bits = opc->mask;
10308 
10309   if ((used_bits & opc->match) != opc->match)
10310     {
10311       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10312 	      opc->name, opc->args);
10313       return 0;
10314     }
10315 #define USE_BITS(mask,shift)	(used_bits |= ((mask) << (shift)))
10316   while (*p)
10317     switch (c = *p++)
10318       {
10319       case ',': break;
10320       case '(': break;
10321       case ')': break;
10322       case '+':
10323     	switch (c = *p++)
10324 	  {
10325 	  case '1': USE_BITS (OP_MASK_UDI1,     OP_SH_UDI1); 	break;
10326 	  case '2': USE_BITS (OP_MASK_UDI2,	OP_SH_UDI2); 	break;
10327 	  case '3': USE_BITS (OP_MASK_UDI3,	OP_SH_UDI3); 	break;
10328 	  case '4': USE_BITS (OP_MASK_UDI4,	OP_SH_UDI4); 	break;
10329 	  case 'A': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10330 	  case 'B': USE_BITS (OP_MASK_INSMSB,	OP_SH_INSMSB);	break;
10331 	  case 'C': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
10332 	  case 'D': USE_BITS (OP_MASK_RD,	OP_SH_RD);
10333 		    USE_BITS (OP_MASK_SEL,	OP_SH_SEL);	break;
10334 	  case 'E': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10335 	  case 'F': USE_BITS (OP_MASK_INSMSB,	OP_SH_INSMSB);	break;
10336 	  case 'G': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
10337 	  case 'H': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
10338 	  case 'I': break;
10339 	  case 't': USE_BITS (OP_MASK_RT,	OP_SH_RT);	break;
10340 	  case 'T': USE_BITS (OP_MASK_RT,	OP_SH_RT);
10341 		    USE_BITS (OP_MASK_SEL,	OP_SH_SEL);	break;
10342 	  case 'x': USE_BITS (OP_MASK_BBITIND,	OP_SH_BBITIND);	break;
10343 	  case 'X': USE_BITS (OP_MASK_BBITIND,	OP_SH_BBITIND);	break;
10344 	  case 'p': USE_BITS (OP_MASK_CINSPOS,	OP_SH_CINSPOS);	break;
10345 	  case 'P': USE_BITS (OP_MASK_CINSPOS,	OP_SH_CINSPOS);	break;
10346 	  case 'Q': USE_BITS (OP_MASK_SEQI,	OP_SH_SEQI);	break;
10347 	  case 's': USE_BITS (OP_MASK_CINSLM1,	OP_SH_CINSLM1);	break;
10348 	  case 'S': USE_BITS (OP_MASK_CINSLM1,	OP_SH_CINSLM1);	break;
10349 	  case 'z': USE_BITS (OP_MASK_RZ,	OP_SH_RZ);	break;
10350 	  case 'Z': USE_BITS (OP_MASK_FZ,	OP_SH_FZ);	break;
10351 	  case 'a': USE_BITS (OP_MASK_OFFSET_A,	OP_SH_OFFSET_A); break;
10352 	  case 'b': USE_BITS (OP_MASK_OFFSET_B,	OP_SH_OFFSET_B); break;
10353 	  case 'c': USE_BITS (OP_MASK_OFFSET_C,	OP_SH_OFFSET_C); break;
10354 
10355 	  default:
10356 	    as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10357 		    c, opc->name, opc->args);
10358 	    return 0;
10359 	  }
10360 	break;
10361       case '<': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10362       case '>':	USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10363       case 'A': break;
10364       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
10365       case 'C':	USE_BITS (OP_MASK_COPZ,		OP_SH_COPZ);	break;
10366       case 'D':	USE_BITS (OP_MASK_FD,		OP_SH_FD);	break;
10367       case 'E':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
10368       case 'F': break;
10369       case 'G':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10370       case 'H': USE_BITS (OP_MASK_SEL,		OP_SH_SEL);	break;
10371       case 'I': break;
10372       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
10373       case 'K':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10374       case 'L': break;
10375       case 'M':	USE_BITS (OP_MASK_CCC,		OP_SH_CCC);	break;
10376       case 'N':	USE_BITS (OP_MASK_BCC,		OP_SH_BCC);	break;
10377       case 'O':	USE_BITS (OP_MASK_ALN,		OP_SH_ALN);	break;
10378       case 'Q':	USE_BITS (OP_MASK_VSEL,		OP_SH_VSEL);
10379 		USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10380       case 'R':	USE_BITS (OP_MASK_FR,		OP_SH_FR);	break;
10381       case 'S':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
10382       case 'T':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10383       case 'V':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
10384       case 'W':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10385       case 'X':	USE_BITS (OP_MASK_FD,		OP_SH_FD);	break;
10386       case 'Y':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
10387       case 'Z':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10388       case 'a':	USE_BITS (OP_MASK_TARGET,	OP_SH_TARGET);	break;
10389       case 'b':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10390       case 'c':	USE_BITS (OP_MASK_CODE,		OP_SH_CODE);	break;
10391       case 'd':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10392       case 'f': break;
10393       case 'h':	USE_BITS (OP_MASK_PREFX,	OP_SH_PREFX);	break;
10394       case 'i':	USE_BITS (OP_MASK_IMMEDIATE,	OP_SH_IMMEDIATE); break;
10395       case 'j':	USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
10396       case 'k':	USE_BITS (OP_MASK_CACHE,	OP_SH_CACHE);	break;
10397       case 'l': break;
10398       case 'o': USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
10399       case 'p':	USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
10400       case 'q':	USE_BITS (OP_MASK_CODE2,	OP_SH_CODE2);	break;
10401       case 'r': USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10402       case 's':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10403       case 't':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
10404       case 'u':	USE_BITS (OP_MASK_IMMEDIATE,	OP_SH_IMMEDIATE); break;
10405       case 'v':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10406       case 'w':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
10407       case 'x': break;
10408       case 'z': break;
10409       case 'P': USE_BITS (OP_MASK_PERFREG,	OP_SH_PERFREG);	break;
10410       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
10411 	        USE_BITS (OP_MASK_RT,           OP_SH_RT);	break;
10412       case 'e': USE_BITS (OP_MASK_VECBYTE,	OP_SH_VECBYTE);	break;
10413       case '%': USE_BITS (OP_MASK_VECALIGN,	OP_SH_VECALIGN); break;
10414       case '[': break;
10415       case ']': break;
10416       case '1':	USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10417       case '2': USE_BITS (OP_MASK_BP,		OP_SH_BP);	break;
10418       case '3': USE_BITS (OP_MASK_SA3,  	OP_SH_SA3);	break;
10419       case '4': USE_BITS (OP_MASK_SA4,  	OP_SH_SA4);	break;
10420       case '5': USE_BITS (OP_MASK_IMM8, 	OP_SH_IMM8);	break;
10421       case '6': USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10422       case '7': USE_BITS (OP_MASK_DSPACC,	OP_SH_DSPACC);	break;
10423       case '8': USE_BITS (OP_MASK_WRDSP,	OP_SH_WRDSP);	break;
10424       case '9': USE_BITS (OP_MASK_DSPACC_S,	OP_SH_DSPACC_S);break;
10425       case '0': USE_BITS (OP_MASK_DSPSFT,	OP_SH_DSPSFT);	break;
10426       case '\'': USE_BITS (OP_MASK_RDDSP,	OP_SH_RDDSP);	break;
10427       case ':': USE_BITS (OP_MASK_DSPSFT_7,	OP_SH_DSPSFT_7);break;
10428       case '@': USE_BITS (OP_MASK_IMM10,	OP_SH_IMM10);	break;
10429       case '!': USE_BITS (OP_MASK_MT_U,		OP_SH_MT_U);	break;
10430       case '$': USE_BITS (OP_MASK_MT_H,		OP_SH_MT_H);	break;
10431       case '*': USE_BITS (OP_MASK_MTACC_T,	OP_SH_MTACC_T);	break;
10432       case '&': USE_BITS (OP_MASK_MTACC_D,	OP_SH_MTACC_D);	break;
10433       case '\\': USE_BITS (OP_MASK_3BITPOS,	OP_SH_3BITPOS);	break;
10434       case '~': USE_BITS (OP_MASK_OFFSET12,	OP_SH_OFFSET12); break;
10435       case 'g': USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10436       default:
10437 	as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10438 		c, opc->name, opc->args);
10439 	return 0;
10440       }
10441 #undef USE_BITS
10442   if (used_bits != 0xffffffff)
10443     {
10444       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10445 	      ~used_bits & 0xffffffff, opc->name, opc->args);
10446       return 0;
10447     }
10448   return 1;
10449 }
10450 
10451 /* For consistency checking, verify that the length implied matches the
10452    major opcode and that all bits are specified either by the match/mask
10453    part of the instruction definition, or by the operand list.  */
10454 
10455 static int
10456 validate_micromips_insn (const struct mips_opcode *opc)
10457 {
10458   unsigned long match = opc->match;
10459   unsigned long mask = opc->mask;
10460   const char *p = opc->args;
10461   unsigned long insn_bits;
10462   unsigned long used_bits;
10463   unsigned long major;
10464   unsigned int length;
10465   char e;
10466   char c;
10467 
10468   if ((mask & match) != match)
10469     {
10470       as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10471 	      opc->name, opc->args);
10472       return 0;
10473     }
10474   length = micromips_insn_length (opc);
10475   if (length != 2 && length != 4)
10476     {
10477       as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10478 		"%s %s"), length, opc->name, opc->args);
10479       return 0;
10480     }
10481   major = match >> (10 + 8 * (length - 2));
10482   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10483       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10484     {
10485       as_bad (_("Internal error: bad microMIPS opcode "
10486 		"(opcode/length mismatch): %s %s"), opc->name, opc->args);
10487       return 0;
10488     }
10489 
10490   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
10491   insn_bits = 1 << 4 * length;
10492   insn_bits <<= 4 * length;
10493   insn_bits -= 1;
10494   used_bits = mask;
10495 #define USE_BITS(field) \
10496   (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10497   while (*p)
10498     switch (c = *p++)
10499       {
10500       case ',': break;
10501       case '(': break;
10502       case ')': break;
10503       case '+':
10504 	e = c;
10505     	switch (c = *p++)
10506 	  {
10507 	  case 'A': USE_BITS (EXTLSB);	break;
10508 	  case 'B': USE_BITS (INSMSB);	break;
10509 	  case 'C': USE_BITS (EXTMSBD);	break;
10510 	  case 'D': USE_BITS (RS);	USE_BITS (SEL);	break;
10511 	  case 'E': USE_BITS (EXTLSB);	break;
10512 	  case 'F': USE_BITS (INSMSB);	break;
10513 	  case 'G': USE_BITS (EXTMSBD);	break;
10514 	  case 'H': USE_BITS (EXTMSBD);	break;
10515 	  default:
10516 	    as_bad (_("Internal error: bad mips opcode "
10517 		      "(unknown extension operand type `%c%c'): %s %s"),
10518 		    e, c, opc->name, opc->args);
10519 	    return 0;
10520 	  }
10521 	break;
10522       case 'm':
10523 	e = c;
10524     	switch (c = *p++)
10525 	  {
10526 	  case 'A': USE_BITS (IMMA);	break;
10527 	  case 'B': USE_BITS (IMMB);	break;
10528 	  case 'C': USE_BITS (IMMC);	break;
10529 	  case 'D': USE_BITS (IMMD);	break;
10530 	  case 'E': USE_BITS (IMME);	break;
10531 	  case 'F': USE_BITS (IMMF);	break;
10532 	  case 'G': USE_BITS (IMMG);	break;
10533 	  case 'H': USE_BITS (IMMH);	break;
10534 	  case 'I': USE_BITS (IMMI);	break;
10535 	  case 'J': USE_BITS (IMMJ);	break;
10536 	  case 'L': USE_BITS (IMML);	break;
10537 	  case 'M': USE_BITS (IMMM);	break;
10538 	  case 'N': USE_BITS (IMMN);	break;
10539 	  case 'O': USE_BITS (IMMO);	break;
10540 	  case 'P': USE_BITS (IMMP);	break;
10541 	  case 'Q': USE_BITS (IMMQ);	break;
10542 	  case 'U': USE_BITS (IMMU);	break;
10543 	  case 'W': USE_BITS (IMMW);	break;
10544 	  case 'X': USE_BITS (IMMX);	break;
10545 	  case 'Y': USE_BITS (IMMY);	break;
10546 	  case 'Z': break;
10547 	  case 'a': break;
10548 	  case 'b': USE_BITS (MB);	break;
10549 	  case 'c': USE_BITS (MC);	break;
10550 	  case 'd': USE_BITS (MD);	break;
10551 	  case 'e': USE_BITS (ME);	break;
10552 	  case 'f': USE_BITS (MF);	break;
10553 	  case 'g': USE_BITS (MG);	break;
10554 	  case 'h': USE_BITS (MH);	break;
10555 	  case 'i': USE_BITS (MI);	break;
10556 	  case 'j': USE_BITS (MJ);	break;
10557 	  case 'l': USE_BITS (ML);	break;
10558 	  case 'm': USE_BITS (MM);	break;
10559 	  case 'n': USE_BITS (MN);	break;
10560 	  case 'p': USE_BITS (MP);	break;
10561 	  case 'q': USE_BITS (MQ);	break;
10562 	  case 'r': break;
10563 	  case 's': break;
10564 	  case 't': break;
10565 	  case 'x': break;
10566 	  case 'y': break;
10567 	  case 'z': break;
10568 	  default:
10569 	    as_bad (_("Internal error: bad mips opcode "
10570 		      "(unknown extension operand type `%c%c'): %s %s"),
10571 		    e, c, opc->name, opc->args);
10572 	    return 0;
10573 	  }
10574 	break;
10575       case '.': USE_BITS (OFFSET10);	break;
10576       case '1': USE_BITS (STYPE);	break;
10577       case '2': USE_BITS (BP);		break;
10578       case '3': USE_BITS (SA3);		break;
10579       case '4': USE_BITS (SA4);		break;
10580       case '5': USE_BITS (IMM8);	break;
10581       case '6': USE_BITS (RS);		break;
10582       case '7': USE_BITS (DSPACC);	break;
10583       case '8': USE_BITS (WRDSP);	break;
10584       case '0': USE_BITS (DSPSFT);	break;
10585       case '<': USE_BITS (SHAMT);	break;
10586       case '>': USE_BITS (SHAMT);	break;
10587       case '@': USE_BITS (IMM10);	break;
10588       case 'B': USE_BITS (CODE10);	break;
10589       case 'C': USE_BITS (COPZ);	break;
10590       case 'D': USE_BITS (FD);		break;
10591       case 'E': USE_BITS (RT);		break;
10592       case 'G': USE_BITS (RS);		break;
10593       case 'H': USE_BITS (SEL);		break;
10594       case 'K': USE_BITS (RS);		break;
10595       case 'M': USE_BITS (CCC);		break;
10596       case 'N': USE_BITS (BCC);		break;
10597       case 'R': USE_BITS (FR);		break;
10598       case 'S': USE_BITS (FS);		break;
10599       case 'T': USE_BITS (FT);		break;
10600       case 'V': USE_BITS (FS);		break;
10601       case '\\': USE_BITS (3BITPOS);	break;
10602       case '^': USE_BITS (RD);		break;
10603       case 'a': USE_BITS (TARGET);	break;
10604       case 'b': USE_BITS (RS);		break;
10605       case 'c': USE_BITS (CODE);	break;
10606       case 'd': USE_BITS (RD);		break;
10607       case 'h': USE_BITS (PREFX);	break;
10608       case 'i': USE_BITS (IMMEDIATE);	break;
10609       case 'j': USE_BITS (DELTA);	break;
10610       case 'k': USE_BITS (CACHE);	break;
10611       case 'n': USE_BITS (RT);		break;
10612       case 'o': USE_BITS (DELTA);	break;
10613       case 'p': USE_BITS (DELTA);	break;
10614       case 'q': USE_BITS (CODE2);	break;
10615       case 'r': USE_BITS (RS);		break;
10616       case 's': USE_BITS (RS);		break;
10617       case 't': USE_BITS (RT);		break;
10618       case 'u': USE_BITS (IMMEDIATE);	break;
10619       case 'v': USE_BITS (RS);		break;
10620       case 'w': USE_BITS (RT);		break;
10621       case 'y': USE_BITS (RS3);		break;
10622       case 'z': break;
10623       case '|': USE_BITS (TRAP);	break;
10624       case '~': USE_BITS (OFFSET12);	break;
10625       default:
10626 	as_bad (_("Internal error: bad microMIPS opcode "
10627 		  "(unknown operand type `%c'): %s %s"),
10628 		c, opc->name, opc->args);
10629 	return 0;
10630       }
10631 #undef USE_BITS
10632   if (used_bits != insn_bits)
10633     {
10634       if (~used_bits & insn_bits)
10635 	as_bad (_("Internal error: bad microMIPS opcode "
10636 		  "(bits 0x%lx undefined): %s %s"),
10637 		~used_bits & insn_bits, opc->name, opc->args);
10638       if (used_bits & ~insn_bits)
10639 	as_bad (_("Internal error: bad microMIPS opcode "
10640 		  "(bits 0x%lx defined): %s %s"),
10641 		used_bits & ~insn_bits, opc->name, opc->args);
10642       return 0;
10643     }
10644   return 1;
10645 }
10646 
10647 /* UDI immediates.  */
10648 struct mips_immed {
10649   char		type;
10650   unsigned int	shift;
10651   unsigned long	mask;
10652   const char *	desc;
10653 };
10654 
10655 static const struct mips_immed mips_immed[] = {
10656   { '1',	OP_SH_UDI1,	OP_MASK_UDI1,		0},
10657   { '2',	OP_SH_UDI2,	OP_MASK_UDI2,		0},
10658   { '3',	OP_SH_UDI3,	OP_MASK_UDI3,		0},
10659   { '4',	OP_SH_UDI4,	OP_MASK_UDI4,		0},
10660   { 0,0,0,0 }
10661 };
10662 
10663 /* Check whether an odd floating-point register is allowed.  */
10664 static int
10665 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10666 {
10667   const char *s = insn->name;
10668 
10669   if (insn->pinfo == INSN_MACRO)
10670     /* Let a macro pass, we'll catch it later when it is expanded.  */
10671     return 1;
10672 
10673   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
10674     {
10675       /* Allow odd registers for single-precision ops.  */
10676       switch (insn->pinfo & (FP_S | FP_D))
10677 	{
10678 	case FP_S:
10679 	case 0:
10680 	  return 1;	/* both single precision - ok */
10681 	case FP_D:
10682 	  return 0;	/* both double precision - fail */
10683 	default:
10684 	  break;
10685 	}
10686 
10687       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
10688       s = strchr (insn->name, '.');
10689       if (argnum == 2)
10690 	s = s != NULL ? strchr (s + 1, '.') : NULL;
10691       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10692     }
10693 
10694   /* Single-precision coprocessor loads and moves are OK too.  */
10695   if ((insn->pinfo & FP_S)
10696       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10697 			 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10698     return 1;
10699 
10700   return 0;
10701 }
10702 
10703 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10704    taking bits from BIT up.  */
10705 static int
10706 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10707 {
10708   return (ep->X_op == O_constant
10709 	  && (ep->X_add_number & ((1 << bit) - 1)) == 0
10710 	  && ep->X_add_number >= min << bit
10711 	  && ep->X_add_number < max << bit);
10712 }
10713 
10714 /* This routine assembles an instruction into its binary format.  As a
10715    side effect, it sets one of the global variables imm_reloc or
10716    offset_reloc to the type of relocation to do if one of the operands
10717    is an address expression.  */
10718 
10719 static void
10720 mips_ip (char *str, struct mips_cl_insn *ip)
10721 {
10722   bfd_boolean wrong_delay_slot_insns = FALSE;
10723   bfd_boolean need_delay_slot_ok = TRUE;
10724   struct mips_opcode *firstinsn = NULL;
10725   const struct mips_opcode *past;
10726   struct hash_control *hash;
10727   char *s;
10728   const char *args;
10729   char c = 0;
10730   struct mips_opcode *insn;
10731   char *argsStart;
10732   unsigned int regno;
10733   unsigned int lastregno;
10734   unsigned int destregno = 0;
10735   unsigned int lastpos = 0;
10736   unsigned int limlo, limhi;
10737   char *s_reset;
10738   offsetT min_range, max_range;
10739   long opend;
10740   char *name;
10741   int argnum;
10742   unsigned int rtype;
10743   char *dot;
10744   long end;
10745 
10746   insn_error = NULL;
10747 
10748   if (mips_opts.micromips)
10749     {
10750       hash = micromips_op_hash;
10751       past = &micromips_opcodes[bfd_micromips_num_opcodes];
10752     }
10753   else
10754     {
10755       hash = op_hash;
10756       past = &mips_opcodes[NUMOPCODES];
10757     }
10758   forced_insn_length = 0;
10759   insn = NULL;
10760 
10761   /* We first try to match an instruction up to a space or to the end.  */
10762   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10763     continue;
10764 
10765   /* Make a copy of the instruction so that we can fiddle with it.  */
10766   name = alloca (end + 1);
10767   memcpy (name, str, end);
10768   name[end] = '\0';
10769 
10770   for (;;)
10771     {
10772       insn = (struct mips_opcode *) hash_find (hash, name);
10773 
10774       if (insn != NULL || !mips_opts.micromips)
10775 	break;
10776       if (forced_insn_length)
10777 	break;
10778 
10779       /* See if there's an instruction size override suffix,
10780          either `16' or `32', at the end of the mnemonic proper,
10781          that defines the operation, i.e. before the first `.'
10782          character if any.  Strip it and retry.  */
10783       dot = strchr (name, '.');
10784       opend = dot != NULL ? dot - name : end;
10785       if (opend < 3)
10786 	break;
10787       if (name[opend - 2] == '1' && name[opend - 1] == '6')
10788 	forced_insn_length = 2;
10789       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10790 	forced_insn_length = 4;
10791       else
10792 	break;
10793       memcpy (name + opend - 2, name + opend, end - opend + 1);
10794     }
10795   if (insn == NULL)
10796     {
10797       insn_error = _("Unrecognized opcode");
10798       return;
10799     }
10800 
10801   /* For microMIPS instructions placed in a fixed-length branch delay slot
10802      we make up to two passes over the relevant fragment of the opcode
10803      table.  First we try instructions that meet the delay slot's length
10804      requirement.  If none matched, then we retry with the remaining ones
10805      and if one matches, then we use it and then issue an appropriate
10806      warning later on.  */
10807   argsStart = s = str + end;
10808   for (;;)
10809     {
10810       bfd_boolean delay_slot_ok;
10811       bfd_boolean size_ok;
10812       bfd_boolean ok;
10813 
10814       gas_assert (strcmp (insn->name, name) == 0);
10815 
10816       ok = is_opcode_valid (insn);
10817       size_ok = is_size_valid (insn);
10818       delay_slot_ok = is_delay_slot_valid (insn);
10819       if (!delay_slot_ok && !wrong_delay_slot_insns)
10820 	{
10821 	  firstinsn = insn;
10822 	  wrong_delay_slot_insns = TRUE;
10823 	}
10824       if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10825 	{
10826 	  static char buf[256];
10827 
10828 	  if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
10829 	    {
10830 	      ++insn;
10831 	      continue;
10832 	    }
10833 	  if (wrong_delay_slot_insns && need_delay_slot_ok)
10834 	    {
10835 	      gas_assert (firstinsn);
10836 	      need_delay_slot_ok = FALSE;
10837 	      past = insn + 1;
10838 	      insn = firstinsn;
10839 	      continue;
10840 	    }
10841 
10842 	  if (insn_error)
10843 	    return;
10844 
10845 	  if (!ok)
10846 	    sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
10847 		     mips_cpu_info_from_arch (mips_opts.arch)->name,
10848 		     mips_cpu_info_from_isa (mips_opts.isa)->name);
10849 	  else
10850 	    sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10851 		     8 * forced_insn_length);
10852 	  insn_error = buf;
10853 
10854 	  return;
10855 	}
10856 
10857       create_insn (ip, insn);
10858       insn_error = NULL;
10859       argnum = 1;
10860       lastregno = 0xffffffff;
10861       for (args = insn->args;; ++args)
10862 	{
10863 	  int is_mdmx;
10864 
10865 	  s += strspn (s, " \t");
10866 	  is_mdmx = 0;
10867 	  switch (*args)
10868 	    {
10869 	    case '\0':		/* end of args */
10870 	      if (*s == '\0')
10871 		return;
10872 	      break;
10873 
10874 	    case '2':
10875 	      /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10876 	         code) or 14 (for microMIPS code).  */
10877 	      my_getExpression (&imm_expr, s);
10878 	      check_absolute_expr (ip, &imm_expr);
10879 	      if ((unsigned long) imm_expr.X_add_number != 1
10880 		  && (unsigned long) imm_expr.X_add_number != 3)
10881 		{
10882 		  as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10883 			  (unsigned long) imm_expr.X_add_number);
10884 		}
10885 	      INSERT_OPERAND (mips_opts.micromips,
10886 			      BP, *ip, imm_expr.X_add_number);
10887 	      imm_expr.X_op = O_absent;
10888 	      s = expr_end;
10889 	      continue;
10890 
10891 	    case '3':
10892 	      /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
10893 	         code) or 21 (for microMIPS code).  */
10894 	      {
10895 		unsigned long mask = (mips_opts.micromips
10896 				      ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
10897 
10898 		my_getExpression (&imm_expr, s);
10899 		check_absolute_expr (ip, &imm_expr);
10900 		if ((unsigned long) imm_expr.X_add_number > mask)
10901 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10902 			  mask, (unsigned long) imm_expr.X_add_number);
10903 		INSERT_OPERAND (mips_opts.micromips,
10904 				SA3, *ip, imm_expr.X_add_number);
10905 		imm_expr.X_op = O_absent;
10906 		s = expr_end;
10907 	      }
10908 	      continue;
10909 
10910 	    case '4':
10911 	      /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
10912 	         code) or 21 (for microMIPS code).  */
10913 	      {
10914 		unsigned long mask = (mips_opts.micromips
10915 				      ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
10916 
10917 		my_getExpression (&imm_expr, s);
10918 		check_absolute_expr (ip, &imm_expr);
10919 		if ((unsigned long) imm_expr.X_add_number > mask)
10920 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10921 			  mask, (unsigned long) imm_expr.X_add_number);
10922 		INSERT_OPERAND (mips_opts.micromips,
10923 				SA4, *ip, imm_expr.X_add_number);
10924 		imm_expr.X_op = O_absent;
10925 		s = expr_end;
10926 	      }
10927 	      continue;
10928 
10929 	    case '5':
10930 	      /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
10931 	         code) or 16 (for microMIPS code).  */
10932 	      {
10933 		unsigned long mask = (mips_opts.micromips
10934 				      ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
10935 
10936 		my_getExpression (&imm_expr, s);
10937 		check_absolute_expr (ip, &imm_expr);
10938 		if ((unsigned long) imm_expr.X_add_number > mask)
10939 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10940 			  mask, (unsigned long) imm_expr.X_add_number);
10941 		INSERT_OPERAND (mips_opts.micromips,
10942 				IMM8, *ip, imm_expr.X_add_number);
10943 		imm_expr.X_op = O_absent;
10944 		s = expr_end;
10945 	      }
10946 	      continue;
10947 
10948 	    case '6':
10949 	      /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
10950 	         code) or 21 (for microMIPS code).  */
10951 	      {
10952 		unsigned long mask = (mips_opts.micromips
10953 				      ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
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 				RS, *ip, imm_expr.X_add_number);
10962 		imm_expr.X_op = O_absent;
10963 		s = expr_end;
10964 	      }
10965 	      continue;
10966 
10967 	    case '7': /* Four DSP accumulators in bits 11,12.  */
10968 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
10969 		  && s[3] >= '0' && s[3] <= '3')
10970 		{
10971 		  regno = s[3] - '0';
10972 		  s += 4;
10973 		  INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
10974 		  continue;
10975 		}
10976 	      else
10977 		as_bad (_("Invalid dsp acc register"));
10978 	      break;
10979 
10980 	    case '8':
10981 	      /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
10982 	         code) or 14 (for microMIPS code).  */
10983 	      {
10984 		unsigned long mask = (mips_opts.micromips
10985 				      ? MICROMIPSOP_MASK_WRDSP
10986 				      : OP_MASK_WRDSP);
10987 
10988 		my_getExpression (&imm_expr, s);
10989 		check_absolute_expr (ip, &imm_expr);
10990 		if ((unsigned long) imm_expr.X_add_number > mask)
10991 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10992 			  mask, (unsigned long) imm_expr.X_add_number);
10993 		INSERT_OPERAND (mips_opts.micromips,
10994 				WRDSP, *ip, imm_expr.X_add_number);
10995 		imm_expr.X_op = O_absent;
10996 		s = expr_end;
10997 	      }
10998 	      continue;
10999 
11000 	    case '9': /* Four DSP accumulators in bits 21,22.  */
11001 	      gas_assert (!mips_opts.micromips);
11002 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11003 		  && s[3] >= '0' && s[3] <= '3')
11004 		{
11005 		  regno = s[3] - '0';
11006 		  s += 4;
11007 		  INSERT_OPERAND (0, DSPACC_S, *ip, regno);
11008 		  continue;
11009 		}
11010 	      else
11011 		as_bad (_("Invalid dsp acc register"));
11012 	      break;
11013 
11014 	    case '0':
11015 	      /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11016 	         code) or 20 (for microMIPS code).  */
11017 	      {
11018 		long mask = (mips_opts.micromips
11019 			     ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11020 
11021 		my_getExpression (&imm_expr, s);
11022 		check_absolute_expr (ip, &imm_expr);
11023 		min_range = -((mask + 1) >> 1);
11024 		max_range = ((mask + 1) >> 1) - 1;
11025 		if (imm_expr.X_add_number < min_range
11026 		    || imm_expr.X_add_number > max_range)
11027 		  as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11028 			  (long) min_range, (long) max_range,
11029 			  (long) imm_expr.X_add_number);
11030 		INSERT_OPERAND (mips_opts.micromips,
11031 				DSPSFT, *ip, imm_expr.X_add_number);
11032 		imm_expr.X_op = O_absent;
11033 		s = expr_end;
11034 	      }
11035 	      continue;
11036 
11037 	    case '\'': /* DSP 6-bit unsigned immediate in bit 16.  */
11038 	      gas_assert (!mips_opts.micromips);
11039 	      my_getExpression (&imm_expr, s);
11040 	      check_absolute_expr (ip, &imm_expr);
11041 	      if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11042 		{
11043 		  as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11044 			  OP_MASK_RDDSP,
11045 			  (unsigned long) imm_expr.X_add_number);
11046 		}
11047 	      INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
11048 	      imm_expr.X_op = O_absent;
11049 	      s = expr_end;
11050 	      continue;
11051 
11052 	    case ':': /* DSP 7-bit signed immediate in bit 19.  */
11053 	      gas_assert (!mips_opts.micromips);
11054 	      my_getExpression (&imm_expr, s);
11055 	      check_absolute_expr (ip, &imm_expr);
11056 	      min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11057 	      max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11058 	      if (imm_expr.X_add_number < min_range ||
11059 		  imm_expr.X_add_number > max_range)
11060 		{
11061 		  as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11062 			  (long) min_range, (long) max_range,
11063 			  (long) imm_expr.X_add_number);
11064 		}
11065 	      INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
11066 	      imm_expr.X_op = O_absent;
11067 	      s = expr_end;
11068 	      continue;
11069 
11070 	    case '@': /* DSP 10-bit signed immediate in bit 16.  */
11071 	      {
11072 		long mask = (mips_opts.micromips
11073 			     ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11074 
11075 		my_getExpression (&imm_expr, s);
11076 		check_absolute_expr (ip, &imm_expr);
11077 		min_range = -((mask + 1) >> 1);
11078 		max_range = ((mask + 1) >> 1) - 1;
11079 		if (imm_expr.X_add_number < min_range
11080 		    || imm_expr.X_add_number > max_range)
11081 		  as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11082 			  (long) min_range, (long) max_range,
11083 			  (long) imm_expr.X_add_number);
11084 		INSERT_OPERAND (mips_opts.micromips,
11085 				IMM10, *ip, imm_expr.X_add_number);
11086 		imm_expr.X_op = O_absent;
11087 		s = expr_end;
11088 	      }
11089 	      continue;
11090 
11091 	    case '^': /* DSP 5-bit unsigned immediate in bit 11.  */
11092 	      gas_assert (mips_opts.micromips);
11093 	      my_getExpression (&imm_expr, s);
11094 	      check_absolute_expr (ip, &imm_expr);
11095 	      if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11096 		as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11097 			MICROMIPSOP_MASK_RD,
11098 			(unsigned long) imm_expr.X_add_number);
11099 	      INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
11100 	      imm_expr.X_op = O_absent;
11101 	      s = expr_end;
11102 	      continue;
11103 
11104             case '!': /* MT usermode flag bit.  */
11105 	      gas_assert (!mips_opts.micromips);
11106 	      my_getExpression (&imm_expr, s);
11107 	      check_absolute_expr (ip, &imm_expr);
11108 	      if (imm_expr.X_add_number & ~OP_MASK_MT_U)
11109 		as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11110 			(unsigned long) imm_expr.X_add_number);
11111 	      INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
11112 	      imm_expr.X_op = O_absent;
11113 	      s = expr_end;
11114 	      continue;
11115 
11116             case '$': /* MT load high flag bit.  */
11117 	      gas_assert (!mips_opts.micromips);
11118 	      my_getExpression (&imm_expr, s);
11119 	      check_absolute_expr (ip, &imm_expr);
11120 	      if (imm_expr.X_add_number & ~OP_MASK_MT_H)
11121 		as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11122 			(unsigned long) imm_expr.X_add_number);
11123 	      INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
11124 	      imm_expr.X_op = O_absent;
11125 	      s = expr_end;
11126 	      continue;
11127 
11128 	    case '*': /* Four DSP accumulators in bits 18,19.  */
11129 	      gas_assert (!mips_opts.micromips);
11130 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11131 		  s[3] >= '0' && s[3] <= '3')
11132 		{
11133 		  regno = s[3] - '0';
11134 		  s += 4;
11135 		  INSERT_OPERAND (0, MTACC_T, *ip, regno);
11136 		  continue;
11137 		}
11138 	      else
11139 		as_bad (_("Invalid dsp/smartmips acc register"));
11140 	      break;
11141 
11142 	    case '&': /* Four DSP accumulators in bits 13,14.  */
11143 	      gas_assert (!mips_opts.micromips);
11144 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11145 		  s[3] >= '0' && s[3] <= '3')
11146 		{
11147 		  regno = s[3] - '0';
11148 		  s += 4;
11149 		  INSERT_OPERAND (0, MTACC_D, *ip, regno);
11150 		  continue;
11151 		}
11152 	      else
11153 		as_bad (_("Invalid dsp/smartmips acc register"));
11154 	      break;
11155 
11156 	    case '\\':		/* 3-bit bit position.  */
11157 	      {
11158 		unsigned long mask = (mips_opts.micromips
11159 				      ? MICROMIPSOP_MASK_3BITPOS
11160 				      : OP_MASK_3BITPOS);
11161 
11162 		my_getExpression (&imm_expr, s);
11163 		check_absolute_expr (ip, &imm_expr);
11164 		if ((unsigned long) imm_expr.X_add_number > mask)
11165 		  as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11166 			   ip->insn_mo->name,
11167 			   mask, (unsigned long) imm_expr.X_add_number);
11168 		INSERT_OPERAND (mips_opts.micromips,
11169 				3BITPOS, *ip, imm_expr.X_add_number);
11170 		imm_expr.X_op = O_absent;
11171 		s = expr_end;
11172 	      }
11173 	      continue;
11174 
11175 	    case ',':
11176 	      ++argnum;
11177 	      if (*s++ == *args)
11178 		continue;
11179 	      s--;
11180 	      switch (*++args)
11181 		{
11182 		case 'r':
11183 		case 'v':
11184 		  INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11185 		  continue;
11186 
11187 		case 'w':
11188 		  INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11189 		  continue;
11190 
11191 		case 'W':
11192 		  gas_assert (!mips_opts.micromips);
11193 		  INSERT_OPERAND (0, FT, *ip, lastregno);
11194 		  continue;
11195 
11196 		case 'V':
11197 		  INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11198 		  continue;
11199 		}
11200 	      break;
11201 
11202 	    case '(':
11203 	      /* Handle optional base register.
11204 		 Either the base register is omitted or
11205 		 we must have a left paren.  */
11206 	      /* This is dependent on the next operand specifier
11207 		 is a base register specification.  */
11208 	      gas_assert (args[1] == 'b'
11209 			  || (mips_opts.micromips
11210 			      && args[1] == 'm'
11211 			      && (args[2] == 'l' || args[2] == 'n'
11212 				  || args[2] == 's' || args[2] == 'a')));
11213 	      if (*s == '\0' && args[1] == 'b')
11214 		return;
11215 	      /* Fall through.  */
11216 
11217 	    case ')':		/* These must match exactly.  */
11218 	      if (*s++ == *args)
11219 		continue;
11220 	      break;
11221 
11222 	    case '[':		/* These must match exactly.  */
11223 	    case ']':
11224 	      gas_assert (!mips_opts.micromips);
11225 	      if (*s++ == *args)
11226 		continue;
11227 	      break;
11228 
11229 	    case '+':		/* Opcode extension character.  */
11230 	      switch (*++args)
11231 		{
11232 		case '1':	/* UDI immediates.  */
11233 		case '2':
11234 		case '3':
11235 		case '4':
11236 		  gas_assert (!mips_opts.micromips);
11237 		  {
11238 		    const struct mips_immed *imm = mips_immed;
11239 
11240 		    while (imm->type && imm->type != *args)
11241 		      ++imm;
11242 		    if (! imm->type)
11243 		      internalError ();
11244 		    my_getExpression (&imm_expr, s);
11245 		    check_absolute_expr (ip, &imm_expr);
11246 		    if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11247 		      {
11248 		        as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11249 				 imm->desc ? imm->desc : ip->insn_mo->name,
11250 				 (unsigned long) imm_expr.X_add_number,
11251 				 (unsigned long) imm_expr.X_add_number);
11252 			imm_expr.X_add_number &= imm->mask;
11253 		      }
11254 		    ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11255 					<< imm->shift);
11256 		    imm_expr.X_op = O_absent;
11257 		    s = expr_end;
11258 		  }
11259 		  continue;
11260 
11261 		case 'A':		/* ins/ext position, becomes LSB.  */
11262 		  limlo = 0;
11263 		  limhi = 31;
11264 		  goto do_lsb;
11265 		case 'E':
11266 		  limlo = 32;
11267 		  limhi = 63;
11268 		  goto do_lsb;
11269 		do_lsb:
11270 		  my_getExpression (&imm_expr, s);
11271 		  check_absolute_expr (ip, &imm_expr);
11272 		  if ((unsigned long) imm_expr.X_add_number < limlo
11273 		      || (unsigned long) imm_expr.X_add_number > limhi)
11274 		    {
11275 		      as_bad (_("Improper position (%lu)"),
11276 			      (unsigned long) imm_expr.X_add_number);
11277 		      imm_expr.X_add_number = limlo;
11278 		    }
11279 		  lastpos = imm_expr.X_add_number;
11280 		  INSERT_OPERAND (mips_opts.micromips,
11281 				  EXTLSB, *ip, imm_expr.X_add_number);
11282 		  imm_expr.X_op = O_absent;
11283 		  s = expr_end;
11284 		  continue;
11285 
11286 		case 'B':		/* ins size, becomes MSB.  */
11287 		  limlo = 1;
11288 		  limhi = 32;
11289 		  goto do_msb;
11290 		case 'F':
11291 		  limlo = 33;
11292 		  limhi = 64;
11293 		  goto do_msb;
11294 		do_msb:
11295 		  my_getExpression (&imm_expr, s);
11296 		  check_absolute_expr (ip, &imm_expr);
11297 		  /* Check for negative input so that small negative numbers
11298 		     will not succeed incorrectly.  The checks against
11299 		     (pos+size) transitively check "size" itself,
11300 		     assuming that "pos" is reasonable.  */
11301 		  if ((long) imm_expr.X_add_number < 0
11302 		      || ((unsigned long) imm_expr.X_add_number
11303 			  + lastpos) < limlo
11304 		      || ((unsigned long) imm_expr.X_add_number
11305 			  + lastpos) > limhi)
11306 		    {
11307 		      as_bad (_("Improper insert size (%lu, position %lu)"),
11308 			      (unsigned long) imm_expr.X_add_number,
11309 			      (unsigned long) lastpos);
11310 		      imm_expr.X_add_number = limlo - lastpos;
11311 		    }
11312 		  INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11313 				  lastpos + imm_expr.X_add_number - 1);
11314 		  imm_expr.X_op = O_absent;
11315 		  s = expr_end;
11316 		  continue;
11317 
11318 		case 'C':		/* ext size, becomes MSBD.  */
11319 		  limlo = 1;
11320 		  limhi = 32;
11321 		  goto do_msbd;
11322 		case 'G':
11323 		  limlo = 33;
11324 		  limhi = 64;
11325 		  goto do_msbd;
11326 		case 'H':
11327 		  limlo = 33;
11328 		  limhi = 64;
11329 		  goto do_msbd;
11330 		do_msbd:
11331 		  my_getExpression (&imm_expr, s);
11332 		  check_absolute_expr (ip, &imm_expr);
11333 		  /* Check for negative input so that small negative numbers
11334 		     will not succeed incorrectly.  The checks against
11335 		     (pos+size) transitively check "size" itself,
11336 		     assuming that "pos" is reasonable.  */
11337 		  if ((long) imm_expr.X_add_number < 0
11338 		      || ((unsigned long) imm_expr.X_add_number
11339 			  + lastpos) < limlo
11340 		      || ((unsigned long) imm_expr.X_add_number
11341 			  + lastpos) > limhi)
11342 		    {
11343 		      as_bad (_("Improper extract size (%lu, position %lu)"),
11344 			      (unsigned long) imm_expr.X_add_number,
11345 			      (unsigned long) lastpos);
11346 		      imm_expr.X_add_number = limlo - lastpos;
11347 		    }
11348 		  INSERT_OPERAND (mips_opts.micromips,
11349 				  EXTMSBD, *ip, imm_expr.X_add_number - 1);
11350 		  imm_expr.X_op = O_absent;
11351 		  s = expr_end;
11352 		  continue;
11353 
11354 		case 'D':
11355 		  /* +D is for disassembly only; never match.  */
11356 		  break;
11357 
11358 		case 'I':
11359 		  /* "+I" is like "I", except that imm2_expr is used.  */
11360 		  my_getExpression (&imm2_expr, s);
11361 		  if (imm2_expr.X_op != O_big
11362 		      && imm2_expr.X_op != O_constant)
11363 		  insn_error = _("absolute expression required");
11364 		  if (HAVE_32BIT_GPRS)
11365 		    normalize_constant_expr (&imm2_expr);
11366 		  s = expr_end;
11367 		  continue;
11368 
11369 		case 'T': /* Coprocessor register.  */
11370 		  gas_assert (!mips_opts.micromips);
11371 		  /* +T is for disassembly only; never match.  */
11372 		  break;
11373 
11374 		case 't': /* Coprocessor register number.  */
11375 		  gas_assert (!mips_opts.micromips);
11376 		  if (s[0] == '$' && ISDIGIT (s[1]))
11377 		    {
11378 		      ++s;
11379 		      regno = 0;
11380 		      do
11381 		        {
11382 			  regno *= 10;
11383 			  regno += *s - '0';
11384 			  ++s;
11385 			}
11386 		      while (ISDIGIT (*s));
11387 		      if (regno > 31)
11388 			as_bad (_("Invalid register number (%d)"), regno);
11389 		      else
11390 			{
11391 			  INSERT_OPERAND (0, RT, *ip, regno);
11392 			  continue;
11393 			}
11394 		    }
11395 		  else
11396 		    as_bad (_("Invalid coprocessor 0 register number"));
11397 		  break;
11398 
11399 		case 'x':
11400 		  /* bbit[01] and bbit[01]32 bit index.  Give error if index
11401 		     is not in the valid range.  */
11402 		  gas_assert (!mips_opts.micromips);
11403 		  my_getExpression (&imm_expr, s);
11404 		  check_absolute_expr (ip, &imm_expr);
11405 		  if ((unsigned) imm_expr.X_add_number > 31)
11406 		    {
11407 		      as_bad (_("Improper bit index (%lu)"),
11408 			      (unsigned long) imm_expr.X_add_number);
11409 		      imm_expr.X_add_number = 0;
11410 		    }
11411 		  INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11412 		  imm_expr.X_op = O_absent;
11413 		  s = expr_end;
11414 		  continue;
11415 
11416 		case 'X':
11417 		  /* bbit[01] bit index when bbit is used but we generate
11418 		     bbit[01]32 because the index is over 32.  Move to the
11419 		     next candidate if index is not in the valid range.  */
11420 		  gas_assert (!mips_opts.micromips);
11421 		  my_getExpression (&imm_expr, s);
11422 		  check_absolute_expr (ip, &imm_expr);
11423 		  if ((unsigned) imm_expr.X_add_number < 32
11424 		      || (unsigned) imm_expr.X_add_number > 63)
11425 		    break;
11426 		  INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11427 		  imm_expr.X_op = O_absent;
11428 		  s = expr_end;
11429 		  continue;
11430 
11431 		case 'p':
11432 		  /* cins, cins32, exts and exts32 position field.  Give error
11433 		     if it's not in the valid range.  */
11434 		  gas_assert (!mips_opts.micromips);
11435 		  my_getExpression (&imm_expr, s);
11436 		  check_absolute_expr (ip, &imm_expr);
11437 		  if ((unsigned) imm_expr.X_add_number > 31)
11438 		    {
11439 		      as_bad (_("Improper position (%lu)"),
11440 			      (unsigned long) imm_expr.X_add_number);
11441 		      imm_expr.X_add_number = 0;
11442 		    }
11443 		  /* Make the pos explicit to simplify +S.  */
11444 		  lastpos = imm_expr.X_add_number + 32;
11445 		  INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11446 		  imm_expr.X_op = O_absent;
11447 		  s = expr_end;
11448 		  continue;
11449 
11450 		case 'P':
11451 		  /* cins, cins32, exts and exts32 position field.  Move to
11452 		     the next candidate if it's not in the valid range.  */
11453 		  gas_assert (!mips_opts.micromips);
11454 		  my_getExpression (&imm_expr, s);
11455 		  check_absolute_expr (ip, &imm_expr);
11456 		  if ((unsigned) imm_expr.X_add_number < 32
11457 		      || (unsigned) imm_expr.X_add_number > 63)
11458 		    break;
11459  		  lastpos = imm_expr.X_add_number;
11460 		  INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11461 		  imm_expr.X_op = O_absent;
11462 		  s = expr_end;
11463 		  continue;
11464 
11465 		case 's':
11466 		  /* cins and exts length-minus-one field.  */
11467 		  gas_assert (!mips_opts.micromips);
11468 		  my_getExpression (&imm_expr, s);
11469 		  check_absolute_expr (ip, &imm_expr);
11470 		  if ((unsigned long) imm_expr.X_add_number > 31)
11471 		    {
11472 		      as_bad (_("Improper size (%lu)"),
11473 			      (unsigned long) imm_expr.X_add_number);
11474 		      imm_expr.X_add_number = 0;
11475 		    }
11476 		  INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11477 		  imm_expr.X_op = O_absent;
11478 		  s = expr_end;
11479 		  continue;
11480 
11481 		case 'S':
11482 		  /* cins32/exts32 and cins/exts aliasing cint32/exts32
11483 		     length-minus-one field.  */
11484 		  gas_assert (!mips_opts.micromips);
11485 		  my_getExpression (&imm_expr, s);
11486 		  check_absolute_expr (ip, &imm_expr);
11487 		  if ((long) imm_expr.X_add_number < 0
11488 		      || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11489 		    {
11490 		      as_bad (_("Improper size (%lu)"),
11491 			      (unsigned long) imm_expr.X_add_number);
11492 		      imm_expr.X_add_number = 0;
11493 		    }
11494 		  INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11495 		  imm_expr.X_op = O_absent;
11496 		  s = expr_end;
11497 		  continue;
11498 
11499 		case 'Q':
11500 		  /* seqi/snei immediate field.  */
11501 		  gas_assert (!mips_opts.micromips);
11502 		  my_getExpression (&imm_expr, s);
11503 		  check_absolute_expr (ip, &imm_expr);
11504 		  if ((long) imm_expr.X_add_number < -512
11505 		      || (long) imm_expr.X_add_number >= 512)
11506 		    {
11507 		      as_bad (_("Improper immediate (%ld)"),
11508 			       (long) imm_expr.X_add_number);
11509 		      imm_expr.X_add_number = 0;
11510 		    }
11511 		  INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11512 		  imm_expr.X_op = O_absent;
11513 		  s = expr_end;
11514 		  continue;
11515 
11516 		case 'a': /* 8-bit signed offset in bit 6 */
11517 		  gas_assert (!mips_opts.micromips);
11518 		  my_getExpression (&imm_expr, s);
11519 		  check_absolute_expr (ip, &imm_expr);
11520 		  min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11521 		  max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11522 		  if (imm_expr.X_add_number < min_range
11523 		      || imm_expr.X_add_number > max_range)
11524 		    {
11525 		      as_bad (_("Offset not in range %ld..%ld (%ld)"),
11526 		              (long) min_range, (long) max_range,
11527 		              (long) imm_expr.X_add_number);
11528 		    }
11529 		  INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
11530 		  imm_expr.X_op = O_absent;
11531 		  s = expr_end;
11532 		  continue;
11533 
11534 		case 'b': /* 8-bit signed offset in bit 3 */
11535 		  gas_assert (!mips_opts.micromips);
11536 		  my_getExpression (&imm_expr, s);
11537 		  check_absolute_expr (ip, &imm_expr);
11538 		  min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11539 		  max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11540 		  if (imm_expr.X_add_number < min_range
11541 		      || imm_expr.X_add_number > max_range)
11542 		    {
11543 		      as_bad (_("Offset not in range %ld..%ld (%ld)"),
11544 		              (long) min_range, (long) max_range,
11545 		              (long) imm_expr.X_add_number);
11546 		    }
11547 		  INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
11548 		  imm_expr.X_op = O_absent;
11549 		  s = expr_end;
11550 		  continue;
11551 
11552 		case 'c': /* 9-bit signed offset in bit 6 */
11553 		  gas_assert (!mips_opts.micromips);
11554 		  my_getExpression (&imm_expr, s);
11555 		  check_absolute_expr (ip, &imm_expr);
11556 		  min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11557 		  max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
11558 		  /* We check the offset range before adjusted.  */
11559 		  min_range <<= 4;
11560 		  max_range <<= 4;
11561 		  if (imm_expr.X_add_number < min_range
11562 		      || imm_expr.X_add_number > max_range)
11563 		    {
11564 		      as_bad (_("Offset not in range %ld..%ld (%ld)"),
11565 		              (long) min_range, (long) max_range,
11566 		              (long) imm_expr.X_add_number);
11567 		    }
11568 		  if (imm_expr.X_add_number & 0xf)
11569 		    {
11570 		      as_bad (_("Offset not 16 bytes alignment (%ld)"),
11571 			      (long) imm_expr.X_add_number);
11572 		    }
11573 		  /* Right shift 4 bits to adjust the offset operand.  */
11574 		  INSERT_OPERAND (0, OFFSET_C, *ip,
11575 				  imm_expr.X_add_number >> 4);
11576 		  imm_expr.X_op = O_absent;
11577 		  s = expr_end;
11578 		  continue;
11579 
11580 		case 'z':
11581 		  gas_assert (!mips_opts.micromips);
11582 		  if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11583 		    break;
11584 		  if (regno == AT && mips_opts.at)
11585 		    {
11586 		      if (mips_opts.at == ATREG)
11587 			as_warn (_("used $at without \".set noat\""));
11588 		      else
11589 			as_warn (_("used $%u with \".set at=$%u\""),
11590 				 regno, mips_opts.at);
11591 		    }
11592 		  INSERT_OPERAND (0, RZ, *ip, regno);
11593 		  continue;
11594 
11595 		case 'Z':
11596 		  gas_assert (!mips_opts.micromips);
11597 		  if (!reg_lookup (&s, RTYPE_FPU, &regno))
11598 		    break;
11599 		  INSERT_OPERAND (0, FZ, *ip, regno);
11600 		  continue;
11601 
11602 		default:
11603 		  as_bad (_("Internal error: bad %s opcode "
11604 			    "(unknown extension operand type `+%c'): %s %s"),
11605 			  mips_opts.micromips ? "microMIPS" : "MIPS",
11606 			  *args, insn->name, insn->args);
11607 		  /* Further processing is fruitless.  */
11608 		  return;
11609 		}
11610 	      break;
11611 
11612 	    case '.':		/* 10-bit offset.  */
11613 	      gas_assert (mips_opts.micromips);
11614 	    case '~':		/* 12-bit offset.  */
11615 	      {
11616 		int shift = *args == '.' ? 9 : 11;
11617 		size_t i;
11618 
11619 		/* Check whether there is only a single bracketed expression
11620 		   left.  If so, it must be the base register and the
11621 		   constant must be zero.  */
11622 		if (*s == '(' && strchr (s + 1, '(') == 0)
11623 		  continue;
11624 
11625 		/* If this value won't fit into the offset, then go find
11626 		   a macro that will generate a 16- or 32-bit offset code
11627 		   pattern.  */
11628 		i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11629 		if ((i == 0 && (imm_expr.X_op != O_constant
11630 				|| imm_expr.X_add_number >= 1 << shift
11631 				|| imm_expr.X_add_number < -1 << shift))
11632 		    || i > 0)
11633 		  {
11634 		    imm_expr.X_op = O_absent;
11635 		    break;
11636 		  }
11637 		if (shift == 9)
11638 		  INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11639 		else
11640 		  INSERT_OPERAND (mips_opts.micromips,
11641 				  OFFSET12, *ip, imm_expr.X_add_number);
11642 		imm_expr.X_op = O_absent;
11643 		s = expr_end;
11644 	      }
11645 	      continue;
11646 
11647 	    case '<':		/* must be at least one digit */
11648 	      /*
11649 	       * According to the manual, if the shift amount is greater
11650 	       * than 31 or less than 0, then the shift amount should be
11651 	       * mod 32.  In reality the mips assembler issues an error.
11652 	       * We issue a warning and mask out all but the low 5 bits.
11653 	       */
11654 	      my_getExpression (&imm_expr, s);
11655 	      check_absolute_expr (ip, &imm_expr);
11656 	      if ((unsigned long) imm_expr.X_add_number > 31)
11657 		as_warn (_("Improper shift amount (%lu)"),
11658 			 (unsigned long) imm_expr.X_add_number);
11659 	      INSERT_OPERAND (mips_opts.micromips,
11660 			      SHAMT, *ip, imm_expr.X_add_number);
11661 	      imm_expr.X_op = O_absent;
11662 	      s = expr_end;
11663 	      continue;
11664 
11665 	    case '>':		/* shift amount minus 32 */
11666 	      my_getExpression (&imm_expr, s);
11667 	      check_absolute_expr (ip, &imm_expr);
11668 	      if ((unsigned long) imm_expr.X_add_number < 32
11669 		  || (unsigned long) imm_expr.X_add_number > 63)
11670 		break;
11671 	      INSERT_OPERAND (mips_opts.micromips,
11672 			      SHAMT, *ip, imm_expr.X_add_number - 32);
11673 	      imm_expr.X_op = O_absent;
11674 	      s = expr_end;
11675 	      continue;
11676 
11677 	    case 'k':		/* CACHE code.  */
11678 	    case 'h':		/* PREFX code.  */
11679 	    case '1':		/* SYNC type.  */
11680 	      my_getExpression (&imm_expr, s);
11681 	      check_absolute_expr (ip, &imm_expr);
11682 	      if ((unsigned long) imm_expr.X_add_number > 31)
11683 		as_warn (_("Invalid value for `%s' (%lu)"),
11684 			 ip->insn_mo->name,
11685 			 (unsigned long) imm_expr.X_add_number);
11686 	      switch (*args)
11687 		{
11688 		case 'k':
11689 		  if (mips_fix_cn63xxp1
11690 		      && !mips_opts.micromips
11691 		      && strcmp ("pref", insn->name) == 0)
11692 		    switch (imm_expr.X_add_number)
11693 		      {
11694 		      case 5:
11695 		      case 25:
11696 		      case 26:
11697 		      case 27:
11698 		      case 28:
11699 		      case 29:
11700 		      case 30:
11701 		      case 31:  /* These are ok.  */
11702 			break;
11703 
11704 		      default:  /* The rest must be changed to 28.  */
11705 			imm_expr.X_add_number = 28;
11706 			break;
11707 		      }
11708 		  INSERT_OPERAND (mips_opts.micromips,
11709 				  CACHE, *ip, imm_expr.X_add_number);
11710 		  break;
11711 		case 'h':
11712 		  INSERT_OPERAND (mips_opts.micromips,
11713 				  PREFX, *ip, imm_expr.X_add_number);
11714 		  break;
11715 		case '1':
11716 		  INSERT_OPERAND (mips_opts.micromips,
11717 				  STYPE, *ip, imm_expr.X_add_number);
11718 		  break;
11719 		}
11720 	      imm_expr.X_op = O_absent;
11721 	      s = expr_end;
11722 	      continue;
11723 
11724 	    case 'c':		/* BREAK code.  */
11725 	      {
11726 		unsigned long mask = (mips_opts.micromips
11727 				      ? MICROMIPSOP_MASK_CODE
11728 				      : OP_MASK_CODE);
11729 
11730 		my_getExpression (&imm_expr, s);
11731 		check_absolute_expr (ip, &imm_expr);
11732 		if ((unsigned long) imm_expr.X_add_number > mask)
11733 		  as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11734 			   ip->insn_mo->name,
11735 			   mask, (unsigned long) imm_expr.X_add_number);
11736 		INSERT_OPERAND (mips_opts.micromips,
11737 				CODE, *ip, imm_expr.X_add_number);
11738 		imm_expr.X_op = O_absent;
11739 		s = expr_end;
11740 	      }
11741 	      continue;
11742 
11743 	    case 'q':		/* Lower BREAK code.  */
11744 	      {
11745 		unsigned long mask = (mips_opts.micromips
11746 				      ? MICROMIPSOP_MASK_CODE2
11747 				      : OP_MASK_CODE2);
11748 
11749 		my_getExpression (&imm_expr, s);
11750 		check_absolute_expr (ip, &imm_expr);
11751 		if ((unsigned long) imm_expr.X_add_number > mask)
11752 		  as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11753 			   ip->insn_mo->name,
11754 			   mask, (unsigned long) imm_expr.X_add_number);
11755 		INSERT_OPERAND (mips_opts.micromips,
11756 				CODE2, *ip, imm_expr.X_add_number);
11757 		imm_expr.X_op = O_absent;
11758 		s = expr_end;
11759 	      }
11760 	      continue;
11761 
11762 	    case 'B':		/* 20- or 10-bit syscall/break/wait code.  */
11763 	      {
11764 		unsigned long mask = (mips_opts.micromips
11765 				      ? MICROMIPSOP_MASK_CODE10
11766 				      : OP_MASK_CODE20);
11767 
11768 		my_getExpression (&imm_expr, s);
11769 		check_absolute_expr (ip, &imm_expr);
11770 		if ((unsigned long) imm_expr.X_add_number > mask)
11771 		  as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11772 			   ip->insn_mo->name,
11773 			   mask, (unsigned long) imm_expr.X_add_number);
11774 		if (mips_opts.micromips)
11775 		  INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11776 		else
11777 		  INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11778 		imm_expr.X_op = O_absent;
11779 		s = expr_end;
11780 	      }
11781 	      continue;
11782 
11783 	    case 'C':		/* 25- or 23-bit coprocessor code.  */
11784 	      {
11785 		unsigned long mask = (mips_opts.micromips
11786 				      ? MICROMIPSOP_MASK_COPZ
11787 				      : OP_MASK_COPZ);
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 (_("Coproccesor code > %u bits (%lu)"),
11793 			   mips_opts.micromips ? 23U : 25U,
11794 			   (unsigned long) imm_expr.X_add_number);
11795 		INSERT_OPERAND (mips_opts.micromips,
11796 				COPZ, *ip, imm_expr.X_add_number);
11797 		imm_expr.X_op = O_absent;
11798 		s = expr_end;
11799 	      }
11800 	      continue;
11801 
11802 	    case 'J':		/* 19-bit WAIT code.  */
11803 	      gas_assert (!mips_opts.micromips);
11804 	      my_getExpression (&imm_expr, s);
11805 	      check_absolute_expr (ip, &imm_expr);
11806 	      if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
11807 	        {
11808 	          as_warn (_("Illegal 19-bit code (%lu)"),
11809 			   (unsigned long) imm_expr.X_add_number);
11810 	          imm_expr.X_add_number &= OP_MASK_CODE19;
11811 	        }
11812 	      INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
11813 	      imm_expr.X_op = O_absent;
11814 	      s = expr_end;
11815 	      continue;
11816 
11817 	    case 'P':		/* Performance register.  */
11818 	      gas_assert (!mips_opts.micromips);
11819 	      my_getExpression (&imm_expr, s);
11820 	      check_absolute_expr (ip, &imm_expr);
11821 	      if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
11822 		as_warn (_("Invalid performance register (%lu)"),
11823 			 (unsigned long) imm_expr.X_add_number);
11824 	      INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
11825 	      imm_expr.X_op = O_absent;
11826 	      s = expr_end;
11827 	      continue;
11828 
11829 	    case 'G':		/* Coprocessor destination register.  */
11830 	      {
11831 		unsigned long opcode = ip->insn_opcode;
11832 		unsigned long mask;
11833 		unsigned int types;
11834 		int cop0;
11835 
11836 		if (mips_opts.micromips)
11837 		  {
11838 		    mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11839 			     | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11840 			     | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11841 		    opcode &= mask;
11842 		    switch (opcode)
11843 		      {
11844 		      case 0x000000fc:				/* mfc0  */
11845 		      case 0x000002fc:				/* mtc0  */
11846 		      case 0x580000fc:				/* dmfc0 */
11847 		      case 0x580002fc:				/* dmtc0 */
11848 			cop0 = 1;
11849 			break;
11850 		      default:
11851 			cop0 = 0;
11852 			break;
11853 		      }
11854 		  }
11855 		else
11856 		  {
11857 		    opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11858 		    cop0 = opcode == OP_OP_COP0;
11859 		  }
11860 		types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11861 		ok = reg_lookup (&s, types, &regno);
11862 		if (mips_opts.micromips)
11863 		  INSERT_OPERAND (1, RS, *ip, regno);
11864 		else
11865 		  INSERT_OPERAND (0, RD, *ip, regno);
11866 		if (ok)
11867 		  {
11868 		    lastregno = regno;
11869 		    continue;
11870 		  }
11871 	      }
11872 	      break;
11873 
11874 	    case 'y':		/* ALNV.PS source register.  */
11875 	      gas_assert (mips_opts.micromips);
11876 	      goto do_reg;
11877 	    case 'x':		/* Ignore register name.  */
11878 	    case 'U':           /* Destination register (CLO/CLZ).  */
11879 	    case 'g':		/* Coprocessor destination register.  */
11880 	      gas_assert (!mips_opts.micromips);
11881 	    case 'b':		/* Base register.  */
11882 	    case 'd':		/* Destination register.  */
11883 	    case 's':		/* Source register.  */
11884 	    case 't':		/* Target register.  */
11885 	    case 'r':		/* Both target and source.  */
11886 	    case 'v':		/* Both dest and source.  */
11887 	    case 'w':		/* Both dest and target.  */
11888 	    case 'E':		/* Coprocessor target register.  */
11889 	    case 'K':		/* RDHWR destination register.  */
11890 	    case 'z':		/* Must be zero register.  */
11891 	    do_reg:
11892 	      s_reset = s;
11893 	      if (*args == 'E' || *args == 'K')
11894 		ok = reg_lookup (&s, RTYPE_NUM, &regno);
11895 	      else
11896 		{
11897 		  ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
11898 		  if (regno == AT && mips_opts.at)
11899 		    {
11900 		      if (mips_opts.at == ATREG)
11901 			as_warn (_("Used $at without \".set noat\""));
11902 		      else
11903 			as_warn (_("Used $%u with \".set at=$%u\""),
11904 				 regno, mips_opts.at);
11905 		    }
11906 		}
11907 	      if (ok)
11908 		{
11909 		  c = *args;
11910 		  if (*s == ' ')
11911 		    ++s;
11912 		  if (args[1] != *s)
11913 		    {
11914 		      if (c == 'r' || c == 'v' || c == 'w')
11915 			{
11916 			  regno = lastregno;
11917 			  s = s_reset;
11918 			  ++args;
11919 			}
11920 		    }
11921 		  /* 'z' only matches $0.  */
11922 		  if (c == 'z' && regno != 0)
11923 		    break;
11924 
11925 		  if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
11926 		    {
11927 		      if (regno == lastregno)
11928 			{
11929 			  insn_error
11930 			    = _("Source and destination must be different");
11931 			  continue;
11932 			}
11933 		      if (regno == 31 && lastregno == 0xffffffff)
11934 			{
11935 			  insn_error
11936 			    = _("A destination register must be supplied");
11937 			  continue;
11938 			}
11939 		    }
11940 		  /* Now that we have assembled one operand, we use the args
11941 		     string to figure out where it goes in the instruction.  */
11942 		  switch (c)
11943 		    {
11944 		    case 'r':
11945 		    case 's':
11946 		    case 'v':
11947 		    case 'b':
11948 		      INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
11949 		      break;
11950 
11951 		    case 'K':
11952 		      if (mips_opts.micromips)
11953 			INSERT_OPERAND (1, RS, *ip, regno);
11954 		      else
11955 			INSERT_OPERAND (0, RD, *ip, regno);
11956 		      break;
11957 
11958 		    case 'd':
11959 		    case 'g':
11960 		      INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
11961 		      break;
11962 
11963 		    case 'U':
11964 		      gas_assert (!mips_opts.micromips);
11965 		      INSERT_OPERAND (0, RD, *ip, regno);
11966 		      INSERT_OPERAND (0, RT, *ip, regno);
11967 		      break;
11968 
11969 		    case 'w':
11970 		    case 't':
11971 		    case 'E':
11972 		      INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
11973 		      break;
11974 
11975 		    case 'y':
11976 		      gas_assert (mips_opts.micromips);
11977 		      INSERT_OPERAND (1, RS3, *ip, regno);
11978 		      break;
11979 
11980 		    case 'x':
11981 		      /* This case exists because on the r3000 trunc
11982 			 expands into a macro which requires a gp
11983 			 register.  On the r6000 or r4000 it is
11984 			 assembled into a single instruction which
11985 			 ignores the register.  Thus the insn version
11986 			 is MIPS_ISA2 and uses 'x', and the macro
11987 			 version is MIPS_ISA1 and uses 't'.  */
11988 		      break;
11989 
11990 		    case 'z':
11991 		      /* This case is for the div instruction, which
11992 			 acts differently if the destination argument
11993 			 is $0.  This only matches $0, and is checked
11994 			 outside the switch.  */
11995 		      break;
11996 		    }
11997 		  lastregno = regno;
11998 		  continue;
11999 		}
12000 	      switch (*args++)
12001 		{
12002 		case 'r':
12003 		case 'v':
12004 		  INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
12005 		  continue;
12006 
12007 		case 'w':
12008 		  INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
12009 		  continue;
12010 		}
12011 	      break;
12012 
12013 	    case 'O':		/* MDMX alignment immediate constant.  */
12014 	      gas_assert (!mips_opts.micromips);
12015 	      my_getExpression (&imm_expr, s);
12016 	      check_absolute_expr (ip, &imm_expr);
12017 	      if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12018 		as_warn (_("Improper align amount (%ld), using low bits"),
12019 			 (long) imm_expr.X_add_number);
12020 	      INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12021 	      imm_expr.X_op = O_absent;
12022 	      s = expr_end;
12023 	      continue;
12024 
12025 	    case 'Q':		/* MDMX vector, element sel, or const.  */
12026 	      if (s[0] != '$')
12027 		{
12028 		  /* MDMX Immediate.  */
12029 		  gas_assert (!mips_opts.micromips);
12030 		  my_getExpression (&imm_expr, s);
12031 		  check_absolute_expr (ip, &imm_expr);
12032 		  if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12033 		    as_warn (_("Invalid MDMX Immediate (%ld)"),
12034 			     (long) imm_expr.X_add_number);
12035 		  INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12036 		  if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12037 		    ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12038 		  else
12039 		    ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12040 		  imm_expr.X_op = O_absent;
12041 		  s = expr_end;
12042 		  continue;
12043 		}
12044 	      /* Not MDMX Immediate.  Fall through.  */
12045 	    case 'X':           /* MDMX destination register.  */
12046 	    case 'Y':           /* MDMX source register.  */
12047 	    case 'Z':           /* MDMX target register.  */
12048 	      is_mdmx = 1;
12049 	    case 'W':
12050 	      gas_assert (!mips_opts.micromips);
12051 	    case 'D':		/* Floating point destination register.  */
12052 	    case 'S':		/* Floating point source register.  */
12053 	    case 'T':		/* Floating point target register.  */
12054 	    case 'R':		/* Floating point source register.  */
12055 	    case 'V':
12056 	      rtype = RTYPE_FPU;
12057 	      if (is_mdmx
12058 		  || (mips_opts.ase_mdmx
12059 		      && (ip->insn_mo->pinfo & FP_D)
12060 		      && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12061 						| INSN_COPROC_MEMORY_DELAY
12062 						| INSN_LOAD_COPROC_DELAY
12063 						| INSN_LOAD_MEMORY_DELAY
12064 						| INSN_STORE_MEMORY))))
12065 		rtype |= RTYPE_VEC;
12066 	      s_reset = s;
12067 	      if (reg_lookup (&s, rtype, &regno))
12068 		{
12069 		  if ((regno & 1) != 0
12070 		      && HAVE_32BIT_FPRS
12071 		      && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12072 		    as_warn (_("Float register should be even, was %d"),
12073 			     regno);
12074 
12075 		  c = *args;
12076 		  if (*s == ' ')
12077 		    ++s;
12078 		  if (args[1] != *s)
12079 		    {
12080 		      if (c == 'V' || c == 'W')
12081 			{
12082 			  regno = lastregno;
12083 			  s = s_reset;
12084 			  ++args;
12085 			}
12086 		    }
12087 		  switch (c)
12088 		    {
12089 		    case 'D':
12090 		    case 'X':
12091 		      INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12092 		      break;
12093 
12094 		    case 'V':
12095 		    case 'S':
12096 		    case 'Y':
12097 		      INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12098 		      break;
12099 
12100 		    case 'Q':
12101 		      /* This is like 'Z', but also needs to fix the MDMX
12102 			 vector/scalar select bits.  Note that the
12103 			 scalar immediate case is handled above.  */
12104 		      if (*s == '[')
12105 			{
12106 			  int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12107 			  int max_el = (is_qh ? 3 : 7);
12108 			  s++;
12109 			  my_getExpression(&imm_expr, s);
12110 			  check_absolute_expr (ip, &imm_expr);
12111 			  s = expr_end;
12112 			  if (imm_expr.X_add_number > max_el)
12113 			    as_bad (_("Bad element selector %ld"),
12114 				    (long) imm_expr.X_add_number);
12115 			  imm_expr.X_add_number &= max_el;
12116 			  ip->insn_opcode |= (imm_expr.X_add_number
12117 					      << (OP_SH_VSEL +
12118 						  (is_qh ? 2 : 1)));
12119 			  imm_expr.X_op = O_absent;
12120 			  if (*s != ']')
12121 			    as_warn (_("Expecting ']' found '%s'"), s);
12122 			  else
12123 			    s++;
12124 			}
12125 		      else
12126                         {
12127                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12128                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12129 						<< OP_SH_VSEL);
12130 			  else
12131 			    ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12132 						OP_SH_VSEL);
12133 			}
12134                       /* Fall through.  */
12135 		    case 'W':
12136 		    case 'T':
12137 		    case 'Z':
12138 		      INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12139 		      break;
12140 
12141 		    case 'R':
12142 		      INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12143 		      break;
12144 		    }
12145 		  lastregno = regno;
12146 		  continue;
12147 		}
12148 
12149 	      switch (*args++)
12150 		{
12151 		case 'V':
12152 		  INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12153 		  continue;
12154 
12155 		case 'W':
12156 		  INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12157 		  continue;
12158 		}
12159 	      break;
12160 
12161 	    case 'I':
12162 	      my_getExpression (&imm_expr, s);
12163 	      if (imm_expr.X_op != O_big
12164 		  && imm_expr.X_op != O_constant)
12165 		insn_error = _("absolute expression required");
12166 	      if (HAVE_32BIT_GPRS)
12167 		normalize_constant_expr (&imm_expr);
12168 	      s = expr_end;
12169 	      continue;
12170 
12171 	    case 'A':
12172 	      my_getExpression (&offset_expr, s);
12173 	      normalize_address_expr (&offset_expr);
12174 	      *imm_reloc = BFD_RELOC_32;
12175 	      s = expr_end;
12176 	      continue;
12177 
12178 	    case 'F':
12179 	    case 'L':
12180 	    case 'f':
12181 	    case 'l':
12182 	      {
12183 		int f64;
12184 		int using_gprs;
12185 		char *save_in;
12186 		char *err;
12187 		unsigned char temp[8];
12188 		int len;
12189 		unsigned int length;
12190 		segT seg;
12191 		subsegT subseg;
12192 		char *p;
12193 
12194 		/* These only appear as the last operand in an
12195 		   instruction, and every instruction that accepts
12196 		   them in any variant accepts them in all variants.
12197 		   This means we don't have to worry about backing out
12198 		   any changes if the instruction does not match.
12199 
12200 		   The difference between them is the size of the
12201 		   floating point constant and where it goes.  For 'F'
12202 		   and 'L' the constant is 64 bits; for 'f' and 'l' it
12203 		   is 32 bits.  Where the constant is placed is based
12204 		   on how the MIPS assembler does things:
12205 		    F -- .rdata
12206 		    L -- .lit8
12207 		    f -- immediate value
12208 		    l -- .lit4
12209 
12210 		    The .lit4 and .lit8 sections are only used if
12211 		    permitted by the -G argument.
12212 
12213 		    The code below needs to know whether the target register
12214 		    is 32 or 64 bits wide.  It relies on the fact 'f' and
12215 		    'F' are used with GPR-based instructions and 'l' and
12216 		    'L' are used with FPR-based instructions.  */
12217 
12218 		f64 = *args == 'F' || *args == 'L';
12219 		using_gprs = *args == 'F' || *args == 'f';
12220 
12221 		save_in = input_line_pointer;
12222 		input_line_pointer = s;
12223 		err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12224 		length = len;
12225 		s = input_line_pointer;
12226 		input_line_pointer = save_in;
12227 		if (err != NULL && *err != '\0')
12228 		  {
12229 		    as_bad (_("Bad floating point constant: %s"), err);
12230 		    memset (temp, '\0', sizeof temp);
12231 		    length = f64 ? 8 : 4;
12232 		  }
12233 
12234 		gas_assert (length == (unsigned) (f64 ? 8 : 4));
12235 
12236 		if (*args == 'f'
12237 		    || (*args == 'l'
12238 			&& (g_switch_value < 4
12239 			    || (temp[0] == 0 && temp[1] == 0)
12240 			    || (temp[2] == 0 && temp[3] == 0))))
12241 		  {
12242 		    imm_expr.X_op = O_constant;
12243 		    if (!target_big_endian)
12244 		      imm_expr.X_add_number = bfd_getl32 (temp);
12245 		    else
12246 		      imm_expr.X_add_number = bfd_getb32 (temp);
12247 		  }
12248 		else if (length > 4
12249 			 && !mips_disable_float_construction
12250 			 /* Constants can only be constructed in GPRs and
12251 			    copied to FPRs if the GPRs are at least as wide
12252 			    as the FPRs.  Force the constant into memory if
12253 			    we are using 64-bit FPRs but the GPRs are only
12254 			    32 bits wide.  */
12255 			 && (using_gprs
12256 			     || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12257 			 && ((temp[0] == 0 && temp[1] == 0)
12258 			     || (temp[2] == 0 && temp[3] == 0))
12259 			 && ((temp[4] == 0 && temp[5] == 0)
12260 			     || (temp[6] == 0 && temp[7] == 0)))
12261 		  {
12262 		    /* The value is simple enough to load with a couple of
12263 		       instructions.  If using 32-bit registers, set
12264 		       imm_expr to the high order 32 bits and offset_expr to
12265 		       the low order 32 bits.  Otherwise, set imm_expr to
12266 		       the entire 64 bit constant.  */
12267 		    if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12268 		      {
12269 			imm_expr.X_op = O_constant;
12270 			offset_expr.X_op = O_constant;
12271 			if (!target_big_endian)
12272 			  {
12273 			    imm_expr.X_add_number = bfd_getl32 (temp + 4);
12274 			    offset_expr.X_add_number = bfd_getl32 (temp);
12275 			  }
12276 			else
12277 			  {
12278 			    imm_expr.X_add_number = bfd_getb32 (temp);
12279 			    offset_expr.X_add_number = bfd_getb32 (temp + 4);
12280 			  }
12281 			if (offset_expr.X_add_number == 0)
12282 			  offset_expr.X_op = O_absent;
12283 		      }
12284 		    else if (sizeof (imm_expr.X_add_number) > 4)
12285 		      {
12286 			imm_expr.X_op = O_constant;
12287 			if (!target_big_endian)
12288 			  imm_expr.X_add_number = bfd_getl64 (temp);
12289 			else
12290 			  imm_expr.X_add_number = bfd_getb64 (temp);
12291 		      }
12292 		    else
12293 		      {
12294 			imm_expr.X_op = O_big;
12295 			imm_expr.X_add_number = 4;
12296 			if (!target_big_endian)
12297 			  {
12298 			    generic_bignum[0] = bfd_getl16 (temp);
12299 			    generic_bignum[1] = bfd_getl16 (temp + 2);
12300 			    generic_bignum[2] = bfd_getl16 (temp + 4);
12301 			    generic_bignum[3] = bfd_getl16 (temp + 6);
12302 			  }
12303 			else
12304 			  {
12305 			    generic_bignum[0] = bfd_getb16 (temp + 6);
12306 			    generic_bignum[1] = bfd_getb16 (temp + 4);
12307 			    generic_bignum[2] = bfd_getb16 (temp + 2);
12308 			    generic_bignum[3] = bfd_getb16 (temp);
12309 			  }
12310 		      }
12311 		  }
12312 		else
12313 		  {
12314 		    const char *newname;
12315 		    segT new_seg;
12316 
12317 		    /* Switch to the right section.  */
12318 		    seg = now_seg;
12319 		    subseg = now_subseg;
12320 		    switch (*args)
12321 		      {
12322 		      default: /* unused default case avoids warnings.  */
12323 		      case 'L':
12324 			newname = RDATA_SECTION_NAME;
12325 			if (g_switch_value >= 8)
12326 			  newname = ".lit8";
12327 			break;
12328 		      case 'F':
12329 			newname = RDATA_SECTION_NAME;
12330 			break;
12331 		      case 'l':
12332 			gas_assert (g_switch_value >= 4);
12333 			newname = ".lit4";
12334 			break;
12335 		      }
12336 		    new_seg = subseg_new (newname, (subsegT) 0);
12337 		    if (IS_ELF)
12338 		      bfd_set_section_flags (stdoutput, new_seg,
12339 					     (SEC_ALLOC
12340 					      | SEC_LOAD
12341 					      | SEC_READONLY
12342 					      | SEC_DATA));
12343 		    frag_align (*args == 'l' ? 2 : 3, 0, 0);
12344 		    if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
12345 		      record_alignment (new_seg, 4);
12346 		    else
12347 		      record_alignment (new_seg, *args == 'l' ? 2 : 3);
12348 		    if (seg == now_seg)
12349 		      as_bad (_("Can't use floating point insn in this section"));
12350 
12351 		    /* Set the argument to the current address in the
12352 		       section.  */
12353 		    offset_expr.X_op = O_symbol;
12354 		    offset_expr.X_add_symbol = symbol_temp_new_now ();
12355 		    offset_expr.X_add_number = 0;
12356 
12357 		    /* Put the floating point number into the section.  */
12358 		    p = frag_more ((int) length);
12359 		    memcpy (p, temp, length);
12360 
12361 		    /* Switch back to the original section.  */
12362 		    subseg_set (seg, subseg);
12363 		  }
12364 	      }
12365 	      continue;
12366 
12367 	    case 'i':		/* 16-bit unsigned immediate.  */
12368 	    case 'j':		/* 16-bit signed immediate.  */
12369 	      *imm_reloc = BFD_RELOC_LO16;
12370 	      if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12371 		{
12372 		  int more;
12373 		  offsetT minval, maxval;
12374 
12375 		  more = (insn + 1 < past
12376 			  && strcmp (insn->name, insn[1].name) == 0);
12377 
12378 		  /* If the expression was written as an unsigned number,
12379 		     only treat it as signed if there are no more
12380 		     alternatives.  */
12381 		  if (more
12382 		      && *args == 'j'
12383 		      && sizeof (imm_expr.X_add_number) <= 4
12384 		      && imm_expr.X_op == O_constant
12385 		      && imm_expr.X_add_number < 0
12386 		      && imm_expr.X_unsigned
12387 		      && HAVE_64BIT_GPRS)
12388 		    break;
12389 
12390 		  /* For compatibility with older assemblers, we accept
12391 		     0x8000-0xffff as signed 16-bit numbers when only
12392 		     signed numbers are allowed.  */
12393 		  if (*args == 'i')
12394 		    minval = 0, maxval = 0xffff;
12395 		  else if (more)
12396 		    minval = -0x8000, maxval = 0x7fff;
12397 		  else
12398 		    minval = -0x8000, maxval = 0xffff;
12399 
12400 		  if (imm_expr.X_op != O_constant
12401 		      || imm_expr.X_add_number < minval
12402 		      || imm_expr.X_add_number > maxval)
12403 		    {
12404 		      if (more)
12405 			break;
12406 		      if (imm_expr.X_op == O_constant
12407 			  || imm_expr.X_op == O_big)
12408 			as_bad (_("Expression out of range"));
12409 		    }
12410 		}
12411 	      s = expr_end;
12412 	      continue;
12413 
12414 	    case 'o':		/* 16-bit offset.  */
12415 	      offset_reloc[0] = BFD_RELOC_LO16;
12416 	      offset_reloc[1] = BFD_RELOC_UNUSED;
12417 	      offset_reloc[2] = BFD_RELOC_UNUSED;
12418 
12419 	      /* Check whether there is only a single bracketed expression
12420 		 left.  If so, it must be the base register and the
12421 		 constant must be zero.  */
12422 	      if (*s == '(' && strchr (s + 1, '(') == 0)
12423 		{
12424 		  offset_expr.X_op = O_constant;
12425 		  offset_expr.X_add_number = 0;
12426 		  continue;
12427 		}
12428 
12429 	      /* If this value won't fit into a 16 bit offset, then go
12430 		 find a macro that will generate the 32 bit offset
12431 		 code pattern.  */
12432 	      if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12433 		  && (offset_expr.X_op != O_constant
12434 		      || offset_expr.X_add_number >= 0x8000
12435 		      || offset_expr.X_add_number < -0x8000))
12436 		break;
12437 
12438 	      s = expr_end;
12439 	      continue;
12440 
12441 	    case 'p':		/* PC-relative offset.  */
12442 	      *offset_reloc = BFD_RELOC_16_PCREL_S2;
12443 	      my_getExpression (&offset_expr, s);
12444 	      s = expr_end;
12445 	      continue;
12446 
12447 	    case 'u':		/* Upper 16 bits.  */
12448 	      if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12449 		  && imm_expr.X_op == O_constant
12450 		  && (imm_expr.X_add_number < 0
12451 		      || imm_expr.X_add_number >= 0x10000))
12452 		as_bad (_("lui expression (%lu) not in range 0..65535"),
12453 			(unsigned long) imm_expr.X_add_number);
12454 	      s = expr_end;
12455 	      continue;
12456 
12457 	    case 'a':		/* 26-bit address.  */
12458 	      *offset_reloc = BFD_RELOC_MIPS_JMP;
12459 	      my_getExpression (&offset_expr, s);
12460 	      s = expr_end;
12461 	      continue;
12462 
12463 	    case 'N':		/* 3-bit branch condition code.  */
12464 	    case 'M':		/* 3-bit compare condition code.  */
12465 	      rtype = RTYPE_CCC;
12466 	      if (ip->insn_mo->pinfo & (FP_D | FP_S))
12467 		rtype |= RTYPE_FCC;
12468 	      if (!reg_lookup (&s, rtype, &regno))
12469 		break;
12470 	      if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12471 		   || strcmp (str + strlen (str) - 5, "any2f") == 0
12472 		   || strcmp (str + strlen (str) - 5, "any2t") == 0)
12473 		  && (regno & 1) != 0)
12474 		as_warn (_("Condition code register should be even for %s, "
12475 			   "was %d"),
12476 			 str, regno);
12477 	      if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12478 		   || strcmp (str + strlen (str) - 5, "any4t") == 0)
12479 		  && (regno & 3) != 0)
12480 		as_warn (_("Condition code register should be 0 or 4 for %s, "
12481 			   "was %d"),
12482 			 str, regno);
12483 	      if (*args == 'N')
12484 		INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12485 	      else
12486 		INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12487 	      continue;
12488 
12489 	    case 'H':
12490 	      if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12491 		s += 2;
12492 	      if (ISDIGIT (*s))
12493 		{
12494 		  c = 0;
12495 		  do
12496 		    {
12497 		      c *= 10;
12498 		      c += *s - '0';
12499 		      ++s;
12500 		    }
12501 		  while (ISDIGIT (*s));
12502 		}
12503 	      else
12504 		c = 8; /* Invalid sel value.  */
12505 
12506 	      if (c > 7)
12507 		as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12508 	      INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12509 	      continue;
12510 
12511 	    case 'e':
12512 	      gas_assert (!mips_opts.micromips);
12513 	      /* Must be at least one digit.  */
12514 	      my_getExpression (&imm_expr, s);
12515 	      check_absolute_expr (ip, &imm_expr);
12516 
12517 	      if ((unsigned long) imm_expr.X_add_number
12518 		  > (unsigned long) OP_MASK_VECBYTE)
12519 		{
12520 		  as_bad (_("bad byte vector index (%ld)"),
12521 			   (long) imm_expr.X_add_number);
12522 		  imm_expr.X_add_number = 0;
12523 		}
12524 
12525 	      INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12526 	      imm_expr.X_op = O_absent;
12527 	      s = expr_end;
12528 	      continue;
12529 
12530 	    case '%':
12531 	      gas_assert (!mips_opts.micromips);
12532 	      my_getExpression (&imm_expr, s);
12533 	      check_absolute_expr (ip, &imm_expr);
12534 
12535 	      if ((unsigned long) imm_expr.X_add_number
12536 		  > (unsigned long) OP_MASK_VECALIGN)
12537 		{
12538 		  as_bad (_("bad byte vector index (%ld)"),
12539 			   (long) imm_expr.X_add_number);
12540 		  imm_expr.X_add_number = 0;
12541 		}
12542 
12543 	      INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12544 	      imm_expr.X_op = O_absent;
12545 	      s = expr_end;
12546 	      continue;
12547 
12548 	    case 'm':		/* Opcode extension character.  */
12549 	      gas_assert (mips_opts.micromips);
12550 	      c = *++args;
12551 	      switch (c)
12552 		{
12553 		case 'r':
12554 		  if (strncmp (s, "$pc", 3) == 0)
12555 		    {
12556 		      s += 3;
12557 		      continue;
12558 		    }
12559 		  break;
12560 
12561 		case 'a':
12562 		case 'b':
12563 		case 'c':
12564 		case 'd':
12565 		case 'e':
12566 		case 'f':
12567 		case 'g':
12568 		case 'h':
12569 		case 'i':
12570 		case 'j':
12571 		case 'l':
12572 		case 'm':
12573 		case 'n':
12574 		case 'p':
12575 		case 'q':
12576 		case 's':
12577 		case 't':
12578 		case 'x':
12579 		case 'y':
12580 		case 'z':
12581 		  s_reset = s;
12582 		  ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12583 		  if (regno == AT && mips_opts.at)
12584 		    {
12585 		      if (mips_opts.at == ATREG)
12586 			as_warn (_("Used $at without \".set noat\""));
12587 		      else
12588 			as_warn (_("Used $%u with \".set at=$%u\""),
12589 				 regno, mips_opts.at);
12590 		    }
12591 		  if (!ok)
12592 		    {
12593 		      if (c == 'c')
12594 			{
12595 			  gas_assert (args[1] == ',');
12596 			  regno = lastregno;
12597 			  ++args;
12598 			}
12599 		      else if (c == 't')
12600 			{
12601 			  gas_assert (args[1] == ',');
12602 			  ++args;
12603 			  continue;			/* Nothing to do.  */
12604 			}
12605 		      else
12606 			break;
12607 		    }
12608 
12609 		  if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12610 		    {
12611 		      if (regno == lastregno)
12612 			{
12613 			  insn_error
12614 			    = _("Source and destination must be different");
12615 			  continue;
12616 			}
12617 		      if (regno == 31 && lastregno == 0xffffffff)
12618 			{
12619 			  insn_error
12620 			    = _("A destination register must be supplied");
12621 			  continue;
12622 			}
12623 		    }
12624 
12625 		  if (*s == ' ')
12626 		    ++s;
12627 		  if (args[1] != *s)
12628 		    {
12629 		      if (c == 'e')
12630 			{
12631 			  gas_assert (args[1] == ',');
12632 			  regno = lastregno;
12633 			  s = s_reset;
12634 			  ++args;
12635 			}
12636 		      else if (c == 't')
12637 			{
12638 			  gas_assert (args[1] == ',');
12639 			  s = s_reset;
12640 			  ++args;
12641 			  continue;			/* Nothing to do.  */
12642 			}
12643 		    }
12644 
12645 		  /* Make sure regno is the same as lastregno.  */
12646 		  if (c == 't' && regno != lastregno)
12647 		    break;
12648 
12649 		  /* Make sure regno is the same as destregno.  */
12650 		  if (c == 'x' && regno != destregno)
12651 		    break;
12652 
12653 		  /* We need to save regno, before regno maps to the
12654 		     microMIPS register encoding.  */
12655 		  lastregno = regno;
12656 
12657 		  if (c == 'f')
12658 		    destregno = regno;
12659 
12660 		  switch (c)
12661 		    {
12662 		      case 'a':
12663 			if (regno != GP)
12664 			  regno = ILLEGAL_REG;
12665 			break;
12666 
12667 		      case 'b':
12668 			regno = mips32_to_micromips_reg_b_map[regno];
12669 			break;
12670 
12671 		      case 'c':
12672 			regno = mips32_to_micromips_reg_c_map[regno];
12673 			break;
12674 
12675 		      case 'd':
12676 			regno = mips32_to_micromips_reg_d_map[regno];
12677 			break;
12678 
12679 		      case 'e':
12680 			regno = mips32_to_micromips_reg_e_map[regno];
12681 			break;
12682 
12683 		      case 'f':
12684 			regno = mips32_to_micromips_reg_f_map[regno];
12685 			break;
12686 
12687 		      case 'g':
12688 			regno = mips32_to_micromips_reg_g_map[regno];
12689 			break;
12690 
12691 		      case 'h':
12692 			regno = mips32_to_micromips_reg_h_map[regno];
12693 			break;
12694 
12695 		      case 'i':
12696 			switch (EXTRACT_OPERAND (1, MI, *ip))
12697 			  {
12698 			    case 4:
12699 			      if (regno == 21)
12700 				regno = 3;
12701 			      else if (regno == 22)
12702 				regno = 4;
12703 			      else if (regno == 5)
12704 				regno = 5;
12705 			      else if (regno == 6)
12706 				regno = 6;
12707 			      else if (regno == 7)
12708 				regno = 7;
12709 			      else
12710 				regno = ILLEGAL_REG;
12711 			      break;
12712 
12713 			    case 5:
12714 			      if (regno == 6)
12715 				regno = 0;
12716 			      else if (regno == 7)
12717 				regno = 1;
12718 			      else
12719 				regno = ILLEGAL_REG;
12720 			      break;
12721 
12722 			    case 6:
12723 			      if (regno == 7)
12724 				regno = 2;
12725 			      else
12726 				regno = ILLEGAL_REG;
12727 			      break;
12728 
12729 			    default:
12730 			      regno = ILLEGAL_REG;
12731 			      break;
12732 			  }
12733 			break;
12734 
12735 		      case 'l':
12736 			regno = mips32_to_micromips_reg_l_map[regno];
12737 			break;
12738 
12739 		      case 'm':
12740 			regno = mips32_to_micromips_reg_m_map[regno];
12741 			break;
12742 
12743 		      case 'n':
12744 			regno = mips32_to_micromips_reg_n_map[regno];
12745 			break;
12746 
12747 		      case 'q':
12748 			regno = mips32_to_micromips_reg_q_map[regno];
12749 			break;
12750 
12751 		      case 's':
12752 			if (regno != SP)
12753 			  regno = ILLEGAL_REG;
12754 			break;
12755 
12756 		      case 'y':
12757 			if (regno != 31)
12758 			  regno = ILLEGAL_REG;
12759 			break;
12760 
12761 		      case 'z':
12762 			if (regno != ZERO)
12763 			  regno = ILLEGAL_REG;
12764 			break;
12765 
12766 		      case 'j': /* Do nothing.  */
12767 		      case 'p':
12768 		      case 't':
12769 		      case 'x':
12770 			break;
12771 
12772 		      default:
12773 			internalError ();
12774 		    }
12775 
12776 		  if (regno == ILLEGAL_REG)
12777 		    break;
12778 
12779 		  switch (c)
12780 		    {
12781 		      case 'b':
12782 			INSERT_OPERAND (1, MB, *ip, regno);
12783 			break;
12784 
12785 		      case 'c':
12786 			INSERT_OPERAND (1, MC, *ip, regno);
12787 			break;
12788 
12789 		      case 'd':
12790 			INSERT_OPERAND (1, MD, *ip, regno);
12791 			break;
12792 
12793 		      case 'e':
12794 			INSERT_OPERAND (1, ME, *ip, regno);
12795 			break;
12796 
12797 		      case 'f':
12798 			INSERT_OPERAND (1, MF, *ip, regno);
12799 			break;
12800 
12801 		      case 'g':
12802 			INSERT_OPERAND (1, MG, *ip, regno);
12803 			break;
12804 
12805 		      case 'h':
12806 			INSERT_OPERAND (1, MH, *ip, regno);
12807 			break;
12808 
12809 		      case 'i':
12810 			INSERT_OPERAND (1, MI, *ip, regno);
12811 			break;
12812 
12813 		      case 'j':
12814 			INSERT_OPERAND (1, MJ, *ip, regno);
12815 			break;
12816 
12817 		      case 'l':
12818 			INSERT_OPERAND (1, ML, *ip, regno);
12819 			break;
12820 
12821 		      case 'm':
12822 			INSERT_OPERAND (1, MM, *ip, regno);
12823 			break;
12824 
12825 		      case 'n':
12826 			INSERT_OPERAND (1, MN, *ip, regno);
12827 			break;
12828 
12829 		      case 'p':
12830 			INSERT_OPERAND (1, MP, *ip, regno);
12831 			break;
12832 
12833 		      case 'q':
12834 			INSERT_OPERAND (1, MQ, *ip, regno);
12835 			break;
12836 
12837 		      case 'a':	/* Do nothing.  */
12838 		      case 's':	/* Do nothing.  */
12839 		      case 't':	/* Do nothing.  */
12840 		      case 'x':	/* Do nothing.  */
12841 		      case 'y':	/* Do nothing.  */
12842 		      case 'z':	/* Do nothing.  */
12843 			break;
12844 
12845 		      default:
12846 			internalError ();
12847 		    }
12848 		  continue;
12849 
12850 		case 'A':
12851 		  {
12852 		    bfd_reloc_code_real_type r[3];
12853 		    expressionS ep;
12854 		    int imm;
12855 
12856 		    /* Check whether there is only a single bracketed
12857 		       expression left.  If so, it must be the base register
12858 		       and the constant must be zero.  */
12859 		    if (*s == '(' && strchr (s + 1, '(') == 0)
12860 		      {
12861 			INSERT_OPERAND (1, IMMA, *ip, 0);
12862 			continue;
12863 		      }
12864 
12865 		    if (my_getSmallExpression (&ep, r, s) > 0
12866 			|| !expr_const_in_range (&ep, -64, 64, 2))
12867 		      break;
12868 
12869 		    imm = ep.X_add_number >> 2;
12870 		    INSERT_OPERAND (1, IMMA, *ip, imm);
12871 		  }
12872 		  s = expr_end;
12873 		  continue;
12874 
12875 		case 'B':
12876 		  {
12877 		    bfd_reloc_code_real_type r[3];
12878 		    expressionS ep;
12879 		    int imm;
12880 
12881 		    if (my_getSmallExpression (&ep, r, s) > 0
12882 			|| ep.X_op != O_constant)
12883 		      break;
12884 
12885 		    for (imm = 0; imm < 8; imm++)
12886 		      if (micromips_imm_b_map[imm] == ep.X_add_number)
12887 			break;
12888 		    if (imm >= 8)
12889 		      break;
12890 
12891 		    INSERT_OPERAND (1, IMMB, *ip, imm);
12892 		  }
12893 		  s = expr_end;
12894 		  continue;
12895 
12896 		case 'C':
12897 		  {
12898 		    bfd_reloc_code_real_type r[3];
12899 		    expressionS ep;
12900 		    int imm;
12901 
12902 		    if (my_getSmallExpression (&ep, r, s) > 0
12903 			|| ep.X_op != O_constant)
12904 		      break;
12905 
12906 		    for (imm = 0; imm < 16; imm++)
12907 		      if (micromips_imm_c_map[imm] == ep.X_add_number)
12908 			break;
12909 		    if (imm >= 16)
12910 		      break;
12911 
12912 		    INSERT_OPERAND (1, IMMC, *ip, imm);
12913 		  }
12914 		  s = expr_end;
12915 		  continue;
12916 
12917 		case 'D':	/* pc relative offset */
12918 		case 'E':	/* pc relative offset */
12919 		  my_getExpression (&offset_expr, s);
12920 		  if (offset_expr.X_op == O_register)
12921 		    break;
12922 
12923 		  if (!forced_insn_length)
12924 		    *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12925 		  else if (c == 'D')
12926 		    *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12927 		  else
12928 		    *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
12929 		  s = expr_end;
12930 		  continue;
12931 
12932 		case 'F':
12933 		  {
12934 		    bfd_reloc_code_real_type r[3];
12935 		    expressionS ep;
12936 		    int imm;
12937 
12938 		    if (my_getSmallExpression (&ep, r, s) > 0
12939 			|| !expr_const_in_range (&ep, 0, 16, 0))
12940 		      break;
12941 
12942 		    imm = ep.X_add_number;
12943 		    INSERT_OPERAND (1, IMMF, *ip, imm);
12944 		  }
12945 		  s = expr_end;
12946 		  continue;
12947 
12948 		case 'G':
12949 		  {
12950 		    bfd_reloc_code_real_type r[3];
12951 		    expressionS ep;
12952 		    int imm;
12953 
12954 		    /* Check whether there is only a single bracketed
12955 		       expression left.  If so, it must be the base register
12956 		       and the constant must be zero.  */
12957 		    if (*s == '(' && strchr (s + 1, '(') == 0)
12958 		      {
12959 			INSERT_OPERAND (1, IMMG, *ip, 0);
12960 			continue;
12961 		      }
12962 
12963 		    if (my_getSmallExpression (&ep, r, s) > 0
12964 			|| !expr_const_in_range (&ep, -1, 15, 0))
12965 		      break;
12966 
12967 		    imm = ep.X_add_number & 15;
12968 		    INSERT_OPERAND (1, IMMG, *ip, imm);
12969 		  }
12970 		  s = expr_end;
12971 		  continue;
12972 
12973 		case 'H':
12974 		  {
12975 		    bfd_reloc_code_real_type r[3];
12976 		    expressionS ep;
12977 		    int imm;
12978 
12979 		    /* Check whether there is only a single bracketed
12980 		       expression left.  If so, it must be the base register
12981 		       and the constant must be zero.  */
12982 		    if (*s == '(' && strchr (s + 1, '(') == 0)
12983 		      {
12984 			INSERT_OPERAND (1, IMMH, *ip, 0);
12985 			continue;
12986 		      }
12987 
12988 		    if (my_getSmallExpression (&ep, r, s) > 0
12989 			|| !expr_const_in_range (&ep, 0, 16, 1))
12990 		      break;
12991 
12992 		    imm = ep.X_add_number >> 1;
12993 		    INSERT_OPERAND (1, IMMH, *ip, imm);
12994 		  }
12995 		  s = expr_end;
12996 		  continue;
12997 
12998 		case 'I':
12999 		  {
13000 		    bfd_reloc_code_real_type r[3];
13001 		    expressionS ep;
13002 		    int imm;
13003 
13004 		    if (my_getSmallExpression (&ep, r, s) > 0
13005 			|| !expr_const_in_range (&ep, -1, 127, 0))
13006 		      break;
13007 
13008 		    imm = ep.X_add_number & 127;
13009 		    INSERT_OPERAND (1, IMMI, *ip, imm);
13010 		  }
13011 		  s = expr_end;
13012 		  continue;
13013 
13014 		case 'J':
13015 		  {
13016 		    bfd_reloc_code_real_type r[3];
13017 		    expressionS ep;
13018 		    int imm;
13019 
13020 		    /* Check whether there is only a single bracketed
13021 		       expression left.  If so, it must be the base register
13022 		       and the constant must be zero.  */
13023 		    if (*s == '(' && strchr (s + 1, '(') == 0)
13024 		      {
13025 			INSERT_OPERAND (1, IMMJ, *ip, 0);
13026 			continue;
13027 		      }
13028 
13029 		    if (my_getSmallExpression (&ep, r, s) > 0
13030 			|| !expr_const_in_range (&ep, 0, 16, 2))
13031 		      break;
13032 
13033 		    imm = ep.X_add_number >> 2;
13034 		    INSERT_OPERAND (1, IMMJ, *ip, imm);
13035 		  }
13036 		  s = expr_end;
13037 		  continue;
13038 
13039 		case 'L':
13040 		  {
13041 		    bfd_reloc_code_real_type r[3];
13042 		    expressionS ep;
13043 		    int imm;
13044 
13045 		    /* Check whether there is only a single bracketed
13046 		       expression left.  If so, it must be the base register
13047 		       and the constant must be zero.  */
13048 		    if (*s == '(' && strchr (s + 1, '(') == 0)
13049 		      {
13050 			INSERT_OPERAND (1, IMML, *ip, 0);
13051 			continue;
13052 		      }
13053 
13054 		    if (my_getSmallExpression (&ep, r, s) > 0
13055 			|| !expr_const_in_range (&ep, 0, 16, 0))
13056 		      break;
13057 
13058 		    imm = ep.X_add_number;
13059 		    INSERT_OPERAND (1, IMML, *ip, imm);
13060 		  }
13061 		  s = expr_end;
13062 		  continue;
13063 
13064 		case 'M':
13065 		  {
13066 		    bfd_reloc_code_real_type r[3];
13067 		    expressionS ep;
13068 		    int imm;
13069 
13070 		    if (my_getSmallExpression (&ep, r, s) > 0
13071 			|| !expr_const_in_range (&ep, 1, 9, 0))
13072 		      break;
13073 
13074 		    imm = ep.X_add_number & 7;
13075 		    INSERT_OPERAND (1, IMMM, *ip, imm);
13076 		  }
13077 		  s = expr_end;
13078 		  continue;
13079 
13080 		case 'N':	/* Register list for lwm and swm.  */
13081 		  {
13082 		    /* A comma-separated list of registers and/or
13083 		       dash-separated contiguous ranges including
13084 		       both ra and a set of one or more registers
13085 		       starting at s0 up to s3 which have to be
13086 		       consecutive, e.g.:
13087 
13088 		       s0, ra
13089 		       s0, s1, ra, s2, s3
13090 		       s0-s2, ra
13091 
13092 		       and any permutations of these.  */
13093 		    unsigned int reglist;
13094 		    int imm;
13095 
13096 		    if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13097 		      break;
13098 
13099 		    if ((reglist & 0xfff1ffff) != 0x80010000)
13100 		      break;
13101 
13102 		    reglist = (reglist >> 17) & 7;
13103 		    reglist += 1;
13104 		    if ((reglist & -reglist) != reglist)
13105 		      break;
13106 
13107 		    imm = ffs (reglist) - 1;
13108 		    INSERT_OPERAND (1, IMMN, *ip, imm);
13109 		  }
13110 		  continue;
13111 
13112 		case 'O':	/* sdbbp 4-bit code.  */
13113 		  {
13114 		    bfd_reloc_code_real_type r[3];
13115 		    expressionS ep;
13116 		    int imm;
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, IMMO, *ip, imm);
13124 		  }
13125 		  s = expr_end;
13126 		  continue;
13127 
13128 		case 'P':
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, 0, 32, 2))
13136 		      break;
13137 
13138 		    imm = ep.X_add_number >> 2;
13139 		    INSERT_OPERAND (1, IMMP, *ip, imm);
13140 		  }
13141 		  s = expr_end;
13142 		  continue;
13143 
13144 		case 'Q':
13145 		  {
13146 		    bfd_reloc_code_real_type r[3];
13147 		    expressionS ep;
13148 		    int imm;
13149 
13150 		    if (my_getSmallExpression (&ep, r, s) > 0
13151 			|| !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13152 		      break;
13153 
13154 		    imm = ep.X_add_number >> 2;
13155 		    INSERT_OPERAND (1, IMMQ, *ip, imm);
13156 		  }
13157 		  s = expr_end;
13158 		  continue;
13159 
13160 		case 'U':
13161 		  {
13162 		    bfd_reloc_code_real_type r[3];
13163 		    expressionS ep;
13164 		    int imm;
13165 
13166 		    /* Check whether there is only a single bracketed
13167 		       expression left.  If so, it must be the base register
13168 		       and the constant must be zero.  */
13169 		    if (*s == '(' && strchr (s + 1, '(') == 0)
13170 		      {
13171 			INSERT_OPERAND (1, IMMU, *ip, 0);
13172 			continue;
13173 		      }
13174 
13175 		    if (my_getSmallExpression (&ep, r, s) > 0
13176 			|| !expr_const_in_range (&ep, 0, 32, 2))
13177 		      break;
13178 
13179 		    imm = ep.X_add_number >> 2;
13180 		    INSERT_OPERAND (1, IMMU, *ip, imm);
13181 		  }
13182 		  s = expr_end;
13183 		  continue;
13184 
13185 		case 'W':
13186 		  {
13187 		    bfd_reloc_code_real_type r[3];
13188 		    expressionS ep;
13189 		    int imm;
13190 
13191 		    if (my_getSmallExpression (&ep, r, s) > 0
13192 			|| !expr_const_in_range (&ep, 0, 64, 2))
13193 		      break;
13194 
13195 		    imm = ep.X_add_number >> 2;
13196 		    INSERT_OPERAND (1, IMMW, *ip, imm);
13197 		  }
13198 		  s = expr_end;
13199 		  continue;
13200 
13201 		case 'X':
13202 		  {
13203 		    bfd_reloc_code_real_type r[3];
13204 		    expressionS ep;
13205 		    int imm;
13206 
13207 		    if (my_getSmallExpression (&ep, r, s) > 0
13208 			|| !expr_const_in_range (&ep, -8, 8, 0))
13209 		      break;
13210 
13211 		    imm = ep.X_add_number;
13212 		    INSERT_OPERAND (1, IMMX, *ip, imm);
13213 		  }
13214 		  s = expr_end;
13215 		  continue;
13216 
13217 		case 'Y':
13218 		  {
13219 		    bfd_reloc_code_real_type r[3];
13220 		    expressionS ep;
13221 		    int imm;
13222 
13223 		    if (my_getSmallExpression (&ep, r, s) > 0
13224 			|| expr_const_in_range (&ep, -2, 2, 2)
13225 			|| !expr_const_in_range (&ep, -258, 258, 2))
13226 		      break;
13227 
13228 		    imm = ep.X_add_number >> 2;
13229 		    imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13230 		    INSERT_OPERAND (1, IMMY, *ip, imm);
13231 		  }
13232 		  s = expr_end;
13233 		  continue;
13234 
13235 		case 'Z':
13236 		  {
13237 		    bfd_reloc_code_real_type r[3];
13238 		    expressionS ep;
13239 
13240 		    if (my_getSmallExpression (&ep, r, s) > 0
13241 			|| !expr_const_in_range (&ep, 0, 1, 0))
13242 		      break;
13243 		  }
13244 		  s = expr_end;
13245 		  continue;
13246 
13247 		default:
13248 		  as_bad (_("Internal error: bad microMIPS opcode "
13249 			    "(unknown extension operand type `m%c'): %s %s"),
13250 			  *args, insn->name, insn->args);
13251 		  /* Further processing is fruitless.  */
13252 		  return;
13253 		}
13254 	      break;
13255 
13256 	    case 'n':		/* Register list for 32-bit lwm and swm.  */
13257 	      gas_assert (mips_opts.micromips);
13258 	      {
13259 		/* A comma-separated list of registers and/or
13260 		   dash-separated contiguous ranges including
13261 		   at least one of ra and a set of one or more
13262 		   registers starting at s0 up to s7 and then
13263 		   s8 which have to be consecutive, e.g.:
13264 
13265 		   ra
13266 		   s0
13267 		   ra, s0, s1, s2
13268 		   s0-s8
13269 		   s0-s5, ra
13270 
13271 		   and any permutations of these.  */
13272 		unsigned int reglist;
13273 		int imm;
13274 		int ra;
13275 
13276 		if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13277 		  break;
13278 
13279 		if ((reglist & 0x3f00ffff) != 0)
13280 		  break;
13281 
13282 		ra = (reglist >> 27) & 0x10;
13283 		reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13284 		reglist += 1;
13285 		if ((reglist & -reglist) != reglist)
13286 		  break;
13287 
13288 		imm = (ffs (reglist) - 1) | ra;
13289 		INSERT_OPERAND (1, RT, *ip, imm);
13290 		imm_expr.X_op = O_absent;
13291 	      }
13292 	      continue;
13293 
13294 	    case '|':		/* 4-bit trap code.  */
13295 	      gas_assert (mips_opts.micromips);
13296 	      my_getExpression (&imm_expr, s);
13297 	      check_absolute_expr (ip, &imm_expr);
13298 	      if ((unsigned long) imm_expr.X_add_number
13299 		  > MICROMIPSOP_MASK_TRAP)
13300 		as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13301 			(unsigned long) imm_expr.X_add_number,
13302 			ip->insn_mo->name);
13303 	      INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13304 	      imm_expr.X_op = O_absent;
13305 	      s = expr_end;
13306 	      continue;
13307 
13308 	    default:
13309 	      as_bad (_("Bad char = '%c'\n"), *args);
13310 	      internalError ();
13311 	    }
13312 	  break;
13313 	}
13314       /* Args don't match.  */
13315       s = argsStart;
13316       insn_error = _("Illegal operands");
13317       if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13318 	{
13319 	  ++insn;
13320 	  continue;
13321 	}
13322       else if (wrong_delay_slot_insns && need_delay_slot_ok)
13323 	{
13324 	  gas_assert (firstinsn);
13325 	  need_delay_slot_ok = FALSE;
13326 	  past = insn + 1;
13327 	  insn = firstinsn;
13328 	  continue;
13329 	}
13330       return;
13331     }
13332 }
13333 
13334 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13335 
13336 /* This routine assembles an instruction into its binary format when
13337    assembling for the mips16.  As a side effect, it sets one of the
13338    global variables imm_reloc or offset_reloc to the type of relocation
13339    to do if one of the operands is an address expression.  It also sets
13340    forced_insn_length to the resulting instruction size in bytes if the
13341    user explicitly requested a small or extended instruction.  */
13342 
13343 static void
13344 mips16_ip (char *str, struct mips_cl_insn *ip)
13345 {
13346   char *s;
13347   const char *args;
13348   struct mips_opcode *insn;
13349   char *argsstart;
13350   unsigned int regno;
13351   unsigned int lastregno = 0;
13352   char *s_reset;
13353   size_t i;
13354 
13355   insn_error = NULL;
13356 
13357   forced_insn_length = 0;
13358 
13359   for (s = str; ISLOWER (*s); ++s)
13360     ;
13361   switch (*s)
13362     {
13363     case '\0':
13364       break;
13365 
13366     case ' ':
13367       *s++ = '\0';
13368       break;
13369 
13370     case '.':
13371       if (s[1] == 't' && s[2] == ' ')
13372 	{
13373 	  *s = '\0';
13374 	  forced_insn_length = 2;
13375 	  s += 3;
13376 	  break;
13377 	}
13378       else if (s[1] == 'e' && s[2] == ' ')
13379 	{
13380 	  *s = '\0';
13381 	  forced_insn_length = 4;
13382 	  s += 3;
13383 	  break;
13384 	}
13385       /* Fall through.  */
13386     default:
13387       insn_error = _("unknown opcode");
13388       return;
13389     }
13390 
13391   if (mips_opts.noautoextend && !forced_insn_length)
13392     forced_insn_length = 2;
13393 
13394   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13395     {
13396       insn_error = _("unrecognized opcode");
13397       return;
13398     }
13399 
13400   argsstart = s;
13401   for (;;)
13402     {
13403       bfd_boolean ok;
13404 
13405       gas_assert (strcmp (insn->name, str) == 0);
13406 
13407       ok = is_opcode_valid_16 (insn);
13408       if (! ok)
13409 	{
13410 	  if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13411 	      && strcmp (insn->name, insn[1].name) == 0)
13412 	    {
13413 	      ++insn;
13414 	      continue;
13415 	    }
13416 	  else
13417 	    {
13418 	      if (!insn_error)
13419 		{
13420 		  static char buf[100];
13421 		  sprintf (buf,
13422 			   _("Opcode not supported on this processor: %s (%s)"),
13423 			   mips_cpu_info_from_arch (mips_opts.arch)->name,
13424 			   mips_cpu_info_from_isa (mips_opts.isa)->name);
13425 		  insn_error = buf;
13426 		}
13427 	      return;
13428 	    }
13429 	}
13430 
13431       create_insn (ip, insn);
13432       imm_expr.X_op = O_absent;
13433       imm_reloc[0] = BFD_RELOC_UNUSED;
13434       imm_reloc[1] = BFD_RELOC_UNUSED;
13435       imm_reloc[2] = BFD_RELOC_UNUSED;
13436       imm2_expr.X_op = O_absent;
13437       offset_expr.X_op = O_absent;
13438       offset_reloc[0] = BFD_RELOC_UNUSED;
13439       offset_reloc[1] = BFD_RELOC_UNUSED;
13440       offset_reloc[2] = BFD_RELOC_UNUSED;
13441       for (args = insn->args; 1; ++args)
13442 	{
13443 	  int c;
13444 
13445 	  if (*s == ' ')
13446 	    ++s;
13447 
13448 	  /* In this switch statement we call break if we did not find
13449              a match, continue if we did find a match, or return if we
13450              are done.  */
13451 
13452 	  c = *args;
13453 	  switch (c)
13454 	    {
13455 	    case '\0':
13456 	      if (*s == '\0')
13457 		{
13458 		  /* Stuff the immediate value in now, if we can.  */
13459 		  if (imm_expr.X_op == O_constant
13460 		      && *imm_reloc > BFD_RELOC_UNUSED
13461 		      && *imm_reloc != BFD_RELOC_MIPS16_GOT16
13462 		      && *imm_reloc != BFD_RELOC_MIPS16_CALL16
13463 		      && insn->pinfo != INSN_MACRO)
13464 		    {
13465 		      valueT tmp;
13466 
13467 		      switch (*offset_reloc)
13468 			{
13469 			  case BFD_RELOC_MIPS16_HI16_S:
13470 			    tmp = (imm_expr.X_add_number + 0x8000) >> 16;
13471 			    break;
13472 
13473 			  case BFD_RELOC_MIPS16_HI16:
13474 			    tmp = imm_expr.X_add_number >> 16;
13475 			    break;
13476 
13477 			  case BFD_RELOC_MIPS16_LO16:
13478 			    tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
13479 				  - 0x8000;
13480 			    break;
13481 
13482 			  case BFD_RELOC_UNUSED:
13483 			    tmp = imm_expr.X_add_number;
13484 			    break;
13485 
13486 			  default:
13487 			    internalError ();
13488 			}
13489 		      *offset_reloc = BFD_RELOC_UNUSED;
13490 
13491 		      mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13492 				    tmp, TRUE, forced_insn_length == 2,
13493 				    forced_insn_length == 4, &ip->insn_opcode,
13494 				    &ip->use_extend, &ip->extend);
13495 		      imm_expr.X_op = O_absent;
13496 		      *imm_reloc = BFD_RELOC_UNUSED;
13497 		    }
13498 
13499 		  return;
13500 		}
13501 	      break;
13502 
13503 	    case ',':
13504 	      if (*s++ == c)
13505 		continue;
13506 	      s--;
13507 	      switch (*++args)
13508 		{
13509 		case 'v':
13510 		  MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13511 		  continue;
13512 		case 'w':
13513 		  MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13514 		  continue;
13515 		}
13516 	      break;
13517 
13518 	    case '(':
13519 	    case ')':
13520 	      if (*s++ == c)
13521 		continue;
13522 	      break;
13523 
13524 	    case 'v':
13525 	    case 'w':
13526 	      if (s[0] != '$')
13527 		{
13528 		  if (c == 'v')
13529 		    MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13530 		  else
13531 		    MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13532 		  ++args;
13533 		  continue;
13534 		}
13535 	      /* Fall through.  */
13536 	    case 'x':
13537 	    case 'y':
13538 	    case 'z':
13539 	    case 'Z':
13540 	    case '0':
13541 	    case 'S':
13542 	    case 'R':
13543 	    case 'X':
13544 	    case 'Y':
13545   	      s_reset = s;
13546 	      if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
13547 		{
13548 		  if (c == 'v' || c == 'w')
13549 		    {
13550 		      if (c == 'v')
13551 			MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13552 		      else
13553 			MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13554 		      ++args;
13555 		      continue;
13556 		    }
13557 		  break;
13558 		}
13559 
13560 	      if (*s == ' ')
13561 		++s;
13562 	      if (args[1] != *s)
13563 		{
13564 		  if (c == 'v' || c == 'w')
13565 		    {
13566 		      regno = mips16_to_32_reg_map[lastregno];
13567 		      s = s_reset;
13568 		      ++args;
13569 		    }
13570 		}
13571 
13572 	      switch (c)
13573 		{
13574 		case 'x':
13575 		case 'y':
13576 		case 'z':
13577 		case 'v':
13578 		case 'w':
13579 		case 'Z':
13580 		  regno = mips32_to_16_reg_map[regno];
13581 		  break;
13582 
13583 		case '0':
13584 		  if (regno != 0)
13585 		    regno = ILLEGAL_REG;
13586 		  break;
13587 
13588 		case 'S':
13589 		  if (regno != SP)
13590 		    regno = ILLEGAL_REG;
13591 		  break;
13592 
13593 		case 'R':
13594 		  if (regno != RA)
13595 		    regno = ILLEGAL_REG;
13596 		  break;
13597 
13598 		case 'X':
13599 		case 'Y':
13600 		  if (regno == AT && mips_opts.at)
13601 		    {
13602 		      if (mips_opts.at == ATREG)
13603 			as_warn (_("used $at without \".set noat\""));
13604 		      else
13605 			as_warn (_("used $%u with \".set at=$%u\""),
13606 				 regno, mips_opts.at);
13607 		    }
13608 		  break;
13609 
13610 		default:
13611 		  internalError ();
13612 		}
13613 
13614 	      if (regno == ILLEGAL_REG)
13615 		break;
13616 
13617 	      switch (c)
13618 		{
13619 		case 'x':
13620 		case 'v':
13621 		  MIPS16_INSERT_OPERAND (RX, *ip, regno);
13622 		  break;
13623 		case 'y':
13624 		case 'w':
13625 		  MIPS16_INSERT_OPERAND (RY, *ip, regno);
13626 		  break;
13627 		case 'z':
13628 		  MIPS16_INSERT_OPERAND (RZ, *ip, regno);
13629 		  break;
13630 		case 'Z':
13631 		  MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
13632 		case '0':
13633 		case 'S':
13634 		case 'R':
13635 		  break;
13636 		case 'X':
13637 		  MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
13638 		  break;
13639 		case 'Y':
13640 		  regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
13641 		  MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
13642 		  break;
13643 		default:
13644 		  internalError ();
13645 		}
13646 
13647 	      lastregno = regno;
13648 	      continue;
13649 
13650 	    case 'P':
13651 	      if (strncmp (s, "$pc", 3) == 0)
13652 		{
13653 		  s += 3;
13654 		  continue;
13655 		}
13656 	      break;
13657 
13658 	    case '5':
13659 	    case 'H':
13660 	    case 'W':
13661 	    case 'D':
13662 	    case 'j':
13663 	    case 'V':
13664 	    case 'C':
13665 	    case 'U':
13666 	    case 'k':
13667 	    case 'K':
13668 	      i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13669 	      if (i > 0)
13670 		{
13671 		  if (imm_expr.X_op != O_constant)
13672 		    {
13673 		      forced_insn_length = 4;
13674 		      ip->use_extend = TRUE;
13675 		      ip->extend = 0;
13676 		    }
13677 		  else
13678 		    {
13679 		      /* We need to relax this instruction.  */
13680 		      *offset_reloc = *imm_reloc;
13681 		      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13682 		    }
13683 		  s = expr_end;
13684 		  continue;
13685 		}
13686 	      *imm_reloc = BFD_RELOC_UNUSED;
13687 	      /* Fall through.  */
13688 	    case '<':
13689 	    case '>':
13690 	    case '[':
13691 	    case ']':
13692 	    case '4':
13693 	    case '8':
13694 	      my_getExpression (&imm_expr, s);
13695 	      if (imm_expr.X_op == O_register)
13696 		{
13697 		  /* What we thought was an expression turned out to
13698                      be a register.  */
13699 
13700 		  if (s[0] == '(' && args[1] == '(')
13701 		    {
13702 		      /* It looks like the expression was omitted
13703 			 before a register indirection, which means
13704 			 that the expression is implicitly zero.  We
13705 			 still set up imm_expr, so that we handle
13706 			 explicit extensions correctly.  */
13707 		      imm_expr.X_op = O_constant;
13708 		      imm_expr.X_add_number = 0;
13709 		      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13710 		      continue;
13711 		    }
13712 
13713 		  break;
13714 		}
13715 
13716 	      /* We need to relax this instruction.  */
13717 	      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13718 	      s = expr_end;
13719 	      continue;
13720 
13721 	    case 'p':
13722 	    case 'q':
13723 	    case 'A':
13724 	    case 'B':
13725 	    case 'E':
13726 	      /* We use offset_reloc rather than imm_reloc for the PC
13727                  relative operands.  This lets macros with both
13728                  immediate and address operands work correctly.  */
13729 	      my_getExpression (&offset_expr, s);
13730 
13731 	      if (offset_expr.X_op == O_register)
13732 		break;
13733 
13734 	      /* We need to relax this instruction.  */
13735 	      *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13736 	      s = expr_end;
13737 	      continue;
13738 
13739 	    case '6':		/* break code */
13740 	      my_getExpression (&imm_expr, s);
13741 	      check_absolute_expr (ip, &imm_expr);
13742 	      if ((unsigned long) imm_expr.X_add_number > 63)
13743 		as_warn (_("Invalid value for `%s' (%lu)"),
13744 			 ip->insn_mo->name,
13745 			 (unsigned long) imm_expr.X_add_number);
13746 	      MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
13747 	      imm_expr.X_op = O_absent;
13748 	      s = expr_end;
13749 	      continue;
13750 
13751 	    case 'a':		/* 26 bit address */
13752 	      my_getExpression (&offset_expr, s);
13753 	      s = expr_end;
13754 	      *offset_reloc = BFD_RELOC_MIPS16_JMP;
13755 	      ip->insn_opcode <<= 16;
13756 	      continue;
13757 
13758 	    case 'l':		/* register list for entry macro */
13759 	    case 'L':		/* register list for exit macro */
13760 	      {
13761 		int mask;
13762 
13763 		if (c == 'l')
13764 		  mask = 0;
13765 		else
13766 		  mask = 7 << 3;
13767 		while (*s != '\0')
13768 		  {
13769 		    unsigned int freg, reg1, reg2;
13770 
13771 		    while (*s == ' ' || *s == ',')
13772 		      ++s;
13773 		    if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13774 		      freg = 0;
13775 		    else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13776 		      freg = 1;
13777 		    else
13778 		      {
13779 			as_bad (_("can't parse register list"));
13780 			break;
13781 		      }
13782 		    if (*s == ' ')
13783 		      ++s;
13784 		    if (*s != '-')
13785 		      reg2 = reg1;
13786 		    else
13787 		      {
13788 			++s;
13789 			if (!reg_lookup (&s, freg ? RTYPE_FPU
13790 					 : (RTYPE_GP | RTYPE_NUM), &reg2))
13791 			  {
13792 			    as_bad (_("invalid register list"));
13793 			    break;
13794 			  }
13795 		      }
13796 		    if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13797 		      {
13798 			mask &= ~ (7 << 3);
13799 			mask |= 5 << 3;
13800 		      }
13801 		    else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13802 		      {
13803 			mask &= ~ (7 << 3);
13804 			mask |= 6 << 3;
13805 		      }
13806 		    else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13807 		      mask |= (reg2 - 3) << 3;
13808 		    else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13809 		      mask |= (reg2 - 15) << 1;
13810 		    else if (reg1 == RA && reg2 == RA)
13811 		      mask |= 1;
13812 		    else
13813 		      {
13814 			as_bad (_("invalid register list"));
13815 			break;
13816 		      }
13817 		  }
13818 		/* The mask is filled in in the opcode table for the
13819                    benefit of the disassembler.  We remove it before
13820                    applying the actual mask.  */
13821 		ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13822 		ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13823 	      }
13824 	    continue;
13825 
13826 	    case 'm':		/* Register list for save insn.  */
13827 	    case 'M':		/* Register list for restore insn.  */
13828 	      {
13829 		int opcode = 0;
13830 		int framesz = 0, seen_framesz = 0;
13831 		int nargs = 0, statics = 0, sregs = 0;
13832 
13833 		while (*s != '\0')
13834 		  {
13835 		    unsigned int reg1, reg2;
13836 
13837 		    SKIP_SPACE_TABS (s);
13838 		    while (*s == ',')
13839 		      ++s;
13840 		    SKIP_SPACE_TABS (s);
13841 
13842 		    my_getExpression (&imm_expr, s);
13843 		    if (imm_expr.X_op == O_constant)
13844 		      {
13845 			/* Handle the frame size.  */
13846 			if (seen_framesz)
13847 			  {
13848 			    as_bad (_("more than one frame size in list"));
13849 			    break;
13850 			  }
13851 			seen_framesz = 1;
13852 			framesz = imm_expr.X_add_number;
13853 			imm_expr.X_op = O_absent;
13854 			s = expr_end;
13855 			continue;
13856 		      }
13857 
13858 		    if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13859 		      {
13860 			as_bad (_("can't parse register list"));
13861 			break;
13862 		      }
13863 
13864 		    while (*s == ' ')
13865 		      ++s;
13866 
13867 		    if (*s != '-')
13868 		      reg2 = reg1;
13869 		    else
13870 		      {
13871 			++s;
13872 			if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13873 			    || reg2 < reg1)
13874 			  {
13875 			    as_bad (_("can't parse register list"));
13876 			    break;
13877 			  }
13878 		      }
13879 
13880 		    while (reg1 <= reg2)
13881 		      {
13882 			if (reg1 >= 4 && reg1 <= 7)
13883 			  {
13884 			    if (!seen_framesz)
13885 				/* args $a0-$a3 */
13886 				nargs |= 1 << (reg1 - 4);
13887 			    else
13888 				/* statics $a0-$a3 */
13889 				statics |= 1 << (reg1 - 4);
13890 			  }
13891 			else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13892 			  {
13893 			    /* $s0-$s8 */
13894 			    sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13895 			  }
13896 			else if (reg1 == 31)
13897 			  {
13898 			    /* Add $ra to insn.  */
13899 			    opcode |= 0x40;
13900 			  }
13901 			else
13902 			  {
13903 			    as_bad (_("unexpected register in list"));
13904 			    break;
13905 			  }
13906 			if (++reg1 == 24)
13907 			  reg1 = 30;
13908 		      }
13909 		  }
13910 
13911 		/* Encode args/statics combination.  */
13912 		if (nargs & statics)
13913 		  as_bad (_("arg/static registers overlap"));
13914 		else if (nargs == 0xf)
13915 		  /* All $a0-$a3 are args.  */
13916 		  opcode |= MIPS16_ALL_ARGS << 16;
13917 		else if (statics == 0xf)
13918 		  /* All $a0-$a3 are statics.  */
13919 		  opcode |= MIPS16_ALL_STATICS << 16;
13920 		else
13921 		  {
13922 		    int narg = 0, nstat = 0;
13923 
13924 		    /* Count arg registers.  */
13925 		    while (nargs & 0x1)
13926 		      {
13927 			nargs >>= 1;
13928 			narg++;
13929 		      }
13930 		    if (nargs != 0)
13931 		      as_bad (_("invalid arg register list"));
13932 
13933 		    /* Count static registers.  */
13934 		    while (statics & 0x8)
13935 		      {
13936 			statics = (statics << 1) & 0xf;
13937 			nstat++;
13938 		      }
13939 		    if (statics != 0)
13940 		      as_bad (_("invalid static register list"));
13941 
13942 		    /* Encode args/statics.  */
13943 		    opcode |= ((narg << 2) | nstat) << 16;
13944 		  }
13945 
13946 		/* Encode $s0/$s1.  */
13947 		if (sregs & (1 << 0))		/* $s0 */
13948 		  opcode |= 0x20;
13949 		if (sregs & (1 << 1))		/* $s1 */
13950 		  opcode |= 0x10;
13951 		sregs >>= 2;
13952 
13953 		if (sregs != 0)
13954 		  {
13955 		    /* Count regs $s2-$s8.  */
13956 		    int nsreg = 0;
13957 		    while (sregs & 1)
13958 		      {
13959 			sregs >>= 1;
13960 			nsreg++;
13961 		      }
13962 		    if (sregs != 0)
13963 		      as_bad (_("invalid static register list"));
13964 		    /* Encode $s2-$s8. */
13965 		    opcode |= nsreg << 24;
13966 		  }
13967 
13968 		/* Encode frame size.  */
13969 		if (!seen_framesz)
13970 		  as_bad (_("missing frame size"));
13971 		else if ((framesz & 7) != 0 || framesz < 0
13972 			 || framesz > 0xff * 8)
13973 		  as_bad (_("invalid frame size"));
13974 		else if (framesz != 128 || (opcode >> 16) != 0)
13975 		  {
13976 		    framesz /= 8;
13977 		    opcode |= (((framesz & 0xf0) << 16)
13978 			     | (framesz & 0x0f));
13979 		  }
13980 
13981 		/* Finally build the instruction.  */
13982 		if ((opcode >> 16) != 0 || framesz == 0)
13983 		  {
13984 		    ip->use_extend = TRUE;
13985 		    ip->extend = opcode >> 16;
13986 		  }
13987 		ip->insn_opcode |= opcode & 0x7f;
13988 	      }
13989 	    continue;
13990 
13991 	    case 'e':		/* extend code */
13992 	      my_getExpression (&imm_expr, s);
13993 	      check_absolute_expr (ip, &imm_expr);
13994 	      if ((unsigned long) imm_expr.X_add_number > 0x7ff)
13995 		{
13996 		  as_warn (_("Invalid value for `%s' (%lu)"),
13997 			   ip->insn_mo->name,
13998 			   (unsigned long) imm_expr.X_add_number);
13999 		  imm_expr.X_add_number &= 0x7ff;
14000 		}
14001 	      ip->insn_opcode |= imm_expr.X_add_number;
14002 	      imm_expr.X_op = O_absent;
14003 	      s = expr_end;
14004 	      continue;
14005 
14006 	    default:
14007 	      internalError ();
14008 	    }
14009 	  break;
14010 	}
14011 
14012       /* Args don't match.  */
14013       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14014 	  strcmp (insn->name, insn[1].name) == 0)
14015 	{
14016 	  ++insn;
14017 	  s = argsstart;
14018 	  continue;
14019 	}
14020 
14021       insn_error = _("illegal operands");
14022 
14023       return;
14024     }
14025 }
14026 
14027 /* This structure holds information we know about a mips16 immediate
14028    argument type.  */
14029 
14030 struct mips16_immed_operand
14031 {
14032   /* The type code used in the argument string in the opcode table.  */
14033   int type;
14034   /* The number of bits in the short form of the opcode.  */
14035   int nbits;
14036   /* The number of bits in the extended form of the opcode.  */
14037   int extbits;
14038   /* The amount by which the short form is shifted when it is used;
14039      for example, the sw instruction has a shift count of 2.  */
14040   int shift;
14041   /* The amount by which the short form is shifted when it is stored
14042      into the instruction code.  */
14043   int op_shift;
14044   /* Non-zero if the short form is unsigned.  */
14045   int unsp;
14046   /* Non-zero if the extended form is unsigned.  */
14047   int extu;
14048   /* Non-zero if the value is PC relative.  */
14049   int pcrel;
14050 };
14051 
14052 /* The mips16 immediate operand types.  */
14053 
14054 static const struct mips16_immed_operand mips16_immed_operands[] =
14055 {
14056   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14057   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14058   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14059   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14060   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14061   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14062   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14063   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14064   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14065   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14066   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14067   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14068   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14069   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14070   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14071   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14072   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14073   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14074   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14075   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14076   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14077 };
14078 
14079 #define MIPS16_NUM_IMMED \
14080   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14081 
14082 /* Handle a mips16 instruction with an immediate value.  This or's the
14083    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
14084    whether an extended value is needed; if one is needed, it sets
14085    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
14086    If SMALL is true, an unextended opcode was explicitly requested.
14087    If EXT is true, an extended opcode was explicitly requested.  If
14088    WARN is true, warn if EXT does not match reality.  */
14089 
14090 static void
14091 mips16_immed (char *file, unsigned int line, int type, offsetT val,
14092 	      bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
14093 	      unsigned long *insn, bfd_boolean *use_extend,
14094 	      unsigned short *extend)
14095 {
14096   const struct mips16_immed_operand *op;
14097   int mintiny, maxtiny;
14098   bfd_boolean needext;
14099 
14100   op = mips16_immed_operands;
14101   while (op->type != type)
14102     {
14103       ++op;
14104       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14105     }
14106 
14107   if (op->unsp)
14108     {
14109       if (type == '<' || type == '>' || type == '[' || type == ']')
14110 	{
14111 	  mintiny = 1;
14112 	  maxtiny = 1 << op->nbits;
14113 	}
14114       else
14115 	{
14116 	  mintiny = 0;
14117 	  maxtiny = (1 << op->nbits) - 1;
14118 	}
14119     }
14120   else
14121     {
14122       mintiny = - (1 << (op->nbits - 1));
14123       maxtiny = (1 << (op->nbits - 1)) - 1;
14124     }
14125 
14126   /* Branch offsets have an implicit 0 in the lowest bit.  */
14127   if (type == 'p' || type == 'q')
14128     val /= 2;
14129 
14130   if ((val & ((1 << op->shift) - 1)) != 0
14131       || val < (mintiny << op->shift)
14132       || val > (maxtiny << op->shift))
14133     needext = TRUE;
14134   else
14135     needext = FALSE;
14136 
14137   if (warn && ext && ! needext)
14138     as_warn_where (file, line,
14139 		   _("extended operand requested but not required"));
14140   if (small && needext)
14141     as_bad_where (file, line, _("invalid unextended operand value"));
14142 
14143   if (small || (! ext && ! needext))
14144     {
14145       int insnval;
14146 
14147       *use_extend = FALSE;
14148       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14149       insnval <<= op->op_shift;
14150       *insn |= insnval;
14151     }
14152   else
14153     {
14154       long minext, maxext;
14155       int extval;
14156 
14157       if (op->extu)
14158 	{
14159 	  minext = 0;
14160 	  maxext = (1 << op->extbits) - 1;
14161 	}
14162       else
14163 	{
14164 	  minext = - (1 << (op->extbits - 1));
14165 	  maxext = (1 << (op->extbits - 1)) - 1;
14166 	}
14167       if (val < minext || val > maxext)
14168 	as_bad_where (file, line,
14169 		      _("operand value out of range for instruction"));
14170 
14171       *use_extend = TRUE;
14172       if (op->extbits == 16)
14173 	{
14174 	  extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14175 	  val &= 0x1f;
14176 	}
14177       else if (op->extbits == 15)
14178 	{
14179 	  extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14180 	  val &= 0xf;
14181 	}
14182       else
14183 	{
14184 	  extval = ((val & 0x1f) << 6) | (val & 0x20);
14185 	  val = 0;
14186 	}
14187 
14188       *extend = (unsigned short) extval;
14189       *insn |= val;
14190     }
14191 }
14192 
14193 struct percent_op_match
14194 {
14195   const char *str;
14196   bfd_reloc_code_real_type reloc;
14197 };
14198 
14199 static const struct percent_op_match mips_percent_op[] =
14200 {
14201   {"%lo", BFD_RELOC_LO16},
14202 #ifdef OBJ_ELF
14203   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14204   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14205   {"%call16", BFD_RELOC_MIPS_CALL16},
14206   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14207   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14208   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14209   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14210   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14211   {"%got", BFD_RELOC_MIPS_GOT16},
14212   {"%gp_rel", BFD_RELOC_GPREL16},
14213   {"%half", BFD_RELOC_16},
14214   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14215   {"%higher", BFD_RELOC_MIPS_HIGHER},
14216   {"%neg", BFD_RELOC_MIPS_SUB},
14217   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14218   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14219   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14220   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14221   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14222   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14223   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14224 #endif
14225   {"%hi", BFD_RELOC_HI16_S}
14226 };
14227 
14228 static const struct percent_op_match mips16_percent_op[] =
14229 {
14230   {"%lo", BFD_RELOC_MIPS16_LO16},
14231   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14232   {"%got", BFD_RELOC_MIPS16_GOT16},
14233   {"%call16", BFD_RELOC_MIPS16_CALL16},
14234   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14235   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14236   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14237   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14238   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14239   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14240   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14241   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14242 };
14243 
14244 
14245 /* Return true if *STR points to a relocation operator.  When returning true,
14246    move *STR over the operator and store its relocation code in *RELOC.
14247    Leave both *STR and *RELOC alone when returning false.  */
14248 
14249 static bfd_boolean
14250 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14251 {
14252   const struct percent_op_match *percent_op;
14253   size_t limit, i;
14254 
14255   if (mips_opts.mips16)
14256     {
14257       percent_op = mips16_percent_op;
14258       limit = ARRAY_SIZE (mips16_percent_op);
14259     }
14260   else
14261     {
14262       percent_op = mips_percent_op;
14263       limit = ARRAY_SIZE (mips_percent_op);
14264     }
14265 
14266   for (i = 0; i < limit; i++)
14267     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14268       {
14269 	int len = strlen (percent_op[i].str);
14270 
14271 	if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14272 	  continue;
14273 
14274 	*str += strlen (percent_op[i].str);
14275 	*reloc = percent_op[i].reloc;
14276 
14277 	/* Check whether the output BFD supports this relocation.
14278 	   If not, issue an error and fall back on something safe.  */
14279 	if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14280 	  {
14281 	    as_bad (_("relocation %s isn't supported by the current ABI"),
14282 		    percent_op[i].str);
14283 	    *reloc = BFD_RELOC_UNUSED;
14284 	  }
14285 	return TRUE;
14286       }
14287   return FALSE;
14288 }
14289 
14290 
14291 /* Parse string STR as a 16-bit relocatable operand.  Store the
14292    expression in *EP and the relocations in the array starting
14293    at RELOC.  Return the number of relocation operators used.
14294 
14295    On exit, EXPR_END points to the first character after the expression.  */
14296 
14297 static size_t
14298 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14299 		       char *str)
14300 {
14301   bfd_reloc_code_real_type reversed_reloc[3];
14302   size_t reloc_index, i;
14303   int crux_depth, str_depth;
14304   char *crux;
14305 
14306   /* Search for the start of the main expression, recoding relocations
14307      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14308      of the main expression and with CRUX_DEPTH containing the number
14309      of open brackets at that point.  */
14310   reloc_index = -1;
14311   str_depth = 0;
14312   do
14313     {
14314       reloc_index++;
14315       crux = str;
14316       crux_depth = str_depth;
14317 
14318       /* Skip over whitespace and brackets, keeping count of the number
14319 	 of brackets.  */
14320       while (*str == ' ' || *str == '\t' || *str == '(')
14321 	if (*str++ == '(')
14322 	  str_depth++;
14323     }
14324   while (*str == '%'
14325 	 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14326 	 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14327 
14328   my_getExpression (ep, crux);
14329   str = expr_end;
14330 
14331   /* Match every open bracket.  */
14332   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14333     if (*str++ == ')')
14334       crux_depth--;
14335 
14336   if (crux_depth > 0)
14337     as_bad (_("unclosed '('"));
14338 
14339   expr_end = str;
14340 
14341   if (reloc_index != 0)
14342     {
14343       prev_reloc_op_frag = frag_now;
14344       for (i = 0; i < reloc_index; i++)
14345 	reloc[i] = reversed_reloc[reloc_index - 1 - i];
14346     }
14347 
14348   return reloc_index;
14349 }
14350 
14351 static void
14352 my_getExpression (expressionS *ep, char *str)
14353 {
14354   char *save_in;
14355 
14356   save_in = input_line_pointer;
14357   input_line_pointer = str;
14358   expression (ep);
14359   expr_end = input_line_pointer;
14360   input_line_pointer = save_in;
14361 }
14362 
14363 char *
14364 md_atof (int type, char *litP, int *sizeP)
14365 {
14366   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14367 }
14368 
14369 void
14370 md_number_to_chars (char *buf, valueT val, int n)
14371 {
14372   if (target_big_endian)
14373     number_to_chars_bigendian (buf, val, n);
14374   else
14375     number_to_chars_littleendian (buf, val, n);
14376 }
14377 
14378 #ifdef OBJ_ELF
14379 static int support_64bit_objects(void)
14380 {
14381   const char **list, **l;
14382   int yes;
14383 
14384   list = bfd_target_list ();
14385   for (l = list; *l != NULL; l++)
14386     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14387 	|| strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14388       break;
14389   yes = (*l != NULL);
14390   free (list);
14391   return yes;
14392 }
14393 #endif /* OBJ_ELF */
14394 
14395 const char *md_shortopts = "O::g::G:";
14396 
14397 enum options
14398   {
14399     OPTION_MARCH = OPTION_MD_BASE,
14400     OPTION_MTUNE,
14401     OPTION_MIPS1,
14402     OPTION_MIPS2,
14403     OPTION_MIPS3,
14404     OPTION_MIPS4,
14405     OPTION_MIPS5,
14406     OPTION_MIPS32,
14407     OPTION_MIPS64,
14408     OPTION_MIPS32R2,
14409     OPTION_MIPS64R2,
14410     OPTION_MIPS16,
14411     OPTION_NO_MIPS16,
14412     OPTION_MIPS3D,
14413     OPTION_NO_MIPS3D,
14414     OPTION_MDMX,
14415     OPTION_NO_MDMX,
14416     OPTION_DSP,
14417     OPTION_NO_DSP,
14418     OPTION_MT,
14419     OPTION_NO_MT,
14420     OPTION_SMARTMIPS,
14421     OPTION_NO_SMARTMIPS,
14422     OPTION_DSPR2,
14423     OPTION_NO_DSPR2,
14424     OPTION_MICROMIPS,
14425     OPTION_NO_MICROMIPS,
14426     OPTION_MCU,
14427     OPTION_NO_MCU,
14428     OPTION_COMPAT_ARCH_BASE,
14429     OPTION_M4650,
14430     OPTION_NO_M4650,
14431     OPTION_M4010,
14432     OPTION_NO_M4010,
14433     OPTION_M4100,
14434     OPTION_NO_M4100,
14435     OPTION_M3900,
14436     OPTION_NO_M3900,
14437     OPTION_M7000_HILO_FIX,
14438     OPTION_MNO_7000_HILO_FIX,
14439     OPTION_FIX_24K,
14440     OPTION_NO_FIX_24K,
14441     OPTION_FIX_LOONGSON2F_JUMP,
14442     OPTION_NO_FIX_LOONGSON2F_JUMP,
14443     OPTION_FIX_LOONGSON2F_NOP,
14444     OPTION_NO_FIX_LOONGSON2F_NOP,
14445     OPTION_FIX_VR4120,
14446     OPTION_NO_FIX_VR4120,
14447     OPTION_FIX_VR4130,
14448     OPTION_NO_FIX_VR4130,
14449     OPTION_FIX_CN63XXP1,
14450     OPTION_NO_FIX_CN63XXP1,
14451     OPTION_TRAP,
14452     OPTION_BREAK,
14453     OPTION_EB,
14454     OPTION_EL,
14455     OPTION_FP32,
14456     OPTION_GP32,
14457     OPTION_CONSTRUCT_FLOATS,
14458     OPTION_NO_CONSTRUCT_FLOATS,
14459     OPTION_FP64,
14460     OPTION_GP64,
14461     OPTION_RELAX_BRANCH,
14462     OPTION_NO_RELAX_BRANCH,
14463     OPTION_MSHARED,
14464     OPTION_MNO_SHARED,
14465     OPTION_MSYM32,
14466     OPTION_MNO_SYM32,
14467     OPTION_SOFT_FLOAT,
14468     OPTION_HARD_FLOAT,
14469     OPTION_SINGLE_FLOAT,
14470     OPTION_DOUBLE_FLOAT,
14471     OPTION_32,
14472     OPTION_TRAP_ZERO_JUMP,
14473     OPTION_NO_TRAP_ZERO_JUMP,
14474 #ifdef OBJ_ELF
14475     OPTION_CALL_SHARED,
14476     OPTION_CALL_NONPIC,
14477     OPTION_NON_SHARED,
14478     OPTION_XGOT,
14479     OPTION_MABI,
14480     OPTION_N32,
14481     OPTION_64,
14482     OPTION_MDEBUG,
14483     OPTION_NO_MDEBUG,
14484     OPTION_PDR,
14485     OPTION_NO_PDR,
14486     OPTION_MVXWORKS_PIC,
14487 #endif /* OBJ_ELF */
14488     OPTION_FIX_LOONGSON2F_BTB,
14489     OPTION_NO_FIX_LOONGSON2F_BTB,
14490     OPTION_END_OF_ENUM
14491   };
14492 
14493 struct option md_longopts[] =
14494 {
14495   /* Options which specify architecture.  */
14496   {"march", required_argument, NULL, OPTION_MARCH},
14497   {"mtune", required_argument, NULL, OPTION_MTUNE},
14498   {"mips0", no_argument, NULL, OPTION_MIPS1},
14499   {"mips1", no_argument, NULL, OPTION_MIPS1},
14500   {"mips2", no_argument, NULL, OPTION_MIPS2},
14501   {"mips3", no_argument, NULL, OPTION_MIPS3},
14502   {"mips4", no_argument, NULL, OPTION_MIPS4},
14503   {"mips5", no_argument, NULL, OPTION_MIPS5},
14504   {"mips32", no_argument, NULL, OPTION_MIPS32},
14505   {"mips64", no_argument, NULL, OPTION_MIPS64},
14506   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
14507   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
14508 
14509   /* Options which specify Application Specific Extensions (ASEs).  */
14510   {"mips16", no_argument, NULL, OPTION_MIPS16},
14511   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
14512   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
14513   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
14514   {"mdmx", no_argument, NULL, OPTION_MDMX},
14515   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
14516   {"mdsp", no_argument, NULL, OPTION_DSP},
14517   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
14518   {"mmt", no_argument, NULL, OPTION_MT},
14519   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
14520   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
14521   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
14522   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
14523   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
14524   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14525   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
14526   {"mmcu", no_argument, NULL, OPTION_MCU},
14527   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
14528 
14529   /* Old-style architecture options.  Don't add more of these.  */
14530   {"m4650", no_argument, NULL, OPTION_M4650},
14531   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
14532   {"m4010", no_argument, NULL, OPTION_M4010},
14533   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
14534   {"m4100", no_argument, NULL, OPTION_M4100},
14535   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
14536   {"m3900", no_argument, NULL, OPTION_M3900},
14537   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14538 
14539   /* Options which enable bug fixes.  */
14540   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
14541   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14542   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14543   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14544   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14545   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14546   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
14547   {"mfix-loongson2f-btb", no_argument, NULL, OPTION_FIX_LOONGSON2F_BTB},
14548   {"mno-fix-loongson2f-btb", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_BTB},
14549   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
14550   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
14551   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
14552   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
14553   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
14554   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
14555   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14556   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
14557 
14558   /* Miscellaneous options.  */
14559   {"trap", no_argument, NULL, OPTION_TRAP},
14560   {"no-break", no_argument, NULL, OPTION_TRAP},
14561   {"break", no_argument, NULL, OPTION_BREAK},
14562   {"no-trap", no_argument, NULL, OPTION_BREAK},
14563   {"EB", no_argument, NULL, OPTION_EB},
14564   {"EL", no_argument, NULL, OPTION_EL},
14565   {"mfp32", no_argument, NULL, OPTION_FP32},
14566   {"mgp32", no_argument, NULL, OPTION_GP32},
14567   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
14568   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
14569   {"mfp64", no_argument, NULL, OPTION_FP64},
14570   {"mgp64", no_argument, NULL, OPTION_GP64},
14571   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14572   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
14573   {"mshared", no_argument, NULL, OPTION_MSHARED},
14574   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
14575   {"msym32", no_argument, NULL, OPTION_MSYM32},
14576   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
14577   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14578   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
14579   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14580   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
14581 
14582   /* Strictly speaking this next option is ELF specific,
14583      but we allow it for other ports as well in order to
14584      make testing easier.  */
14585   {"32",          no_argument, NULL, OPTION_32},
14586 
14587   {"mtrap-zero-jump", no_argument, NULL, OPTION_TRAP_ZERO_JUMP},
14588   {"mno-trap-zero-jump", no_argument, NULL, OPTION_NO_TRAP_ZERO_JUMP},
14589 
14590   /* ELF-specific options.  */
14591 #ifdef OBJ_ELF
14592   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
14593   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
14594   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
14595   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
14596   {"xgot",        no_argument, NULL, OPTION_XGOT},
14597   {"mabi", required_argument, NULL, OPTION_MABI},
14598   {"n32",         no_argument, NULL, OPTION_N32},
14599   {"64",          no_argument, NULL, OPTION_64},
14600   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
14601   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
14602   {"mpdr", no_argument, NULL, OPTION_PDR},
14603   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
14604   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
14605 #endif /* OBJ_ELF */
14606 
14607   {NULL, no_argument, NULL, 0}
14608 };
14609 size_t md_longopts_size = sizeof (md_longopts);
14610 
14611 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14612    NEW_VALUE.  Warn if another value was already specified.  Note:
14613    we have to defer parsing the -march and -mtune arguments in order
14614    to handle 'from-abi' correctly, since the ABI might be specified
14615    in a later argument.  */
14616 
14617 static void
14618 mips_set_option_string (const char **string_ptr, const char *new_value)
14619 {
14620   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14621     as_warn (_("A different %s was already specified, is now %s"),
14622 	     string_ptr == &mips_arch_string ? "-march" : "-mtune",
14623 	     new_value);
14624 
14625   *string_ptr = new_value;
14626 }
14627 
14628 int
14629 md_parse_option (int c, char *arg)
14630 {
14631   switch (c)
14632     {
14633     case OPTION_CONSTRUCT_FLOATS:
14634       mips_disable_float_construction = 0;
14635       break;
14636 
14637     case OPTION_NO_CONSTRUCT_FLOATS:
14638       mips_disable_float_construction = 1;
14639       break;
14640 
14641     case OPTION_TRAP:
14642       mips_trap = 1;
14643       break;
14644 
14645     case OPTION_BREAK:
14646       mips_trap = 0;
14647       break;
14648 
14649     case OPTION_EB:
14650       target_big_endian = 1;
14651       break;
14652 
14653     case OPTION_EL:
14654       target_big_endian = 0;
14655       break;
14656 
14657     case 'O':
14658       if (arg == NULL)
14659 	mips_optimize = 1;
14660       else if (arg[0] == '0')
14661 	mips_optimize = 0;
14662       else if (arg[0] == '1')
14663 	mips_optimize = 1;
14664       else
14665 	mips_optimize = 2;
14666       break;
14667 
14668     case 'g':
14669       if (arg == NULL)
14670 	mips_debug = 2;
14671       else
14672 	mips_debug = atoi (arg);
14673       break;
14674 
14675     case OPTION_MIPS1:
14676       file_mips_isa = ISA_MIPS1;
14677       break;
14678 
14679     case OPTION_MIPS2:
14680       file_mips_isa = ISA_MIPS2;
14681       break;
14682 
14683     case OPTION_MIPS3:
14684       file_mips_isa = ISA_MIPS3;
14685       break;
14686 
14687     case OPTION_MIPS4:
14688       file_mips_isa = ISA_MIPS4;
14689       break;
14690 
14691     case OPTION_MIPS5:
14692       file_mips_isa = ISA_MIPS5;
14693       break;
14694 
14695     case OPTION_MIPS32:
14696       file_mips_isa = ISA_MIPS32;
14697       break;
14698 
14699     case OPTION_MIPS32R2:
14700       file_mips_isa = ISA_MIPS32R2;
14701       break;
14702 
14703     case OPTION_MIPS64R2:
14704       file_mips_isa = ISA_MIPS64R2;
14705       break;
14706 
14707     case OPTION_MIPS64:
14708       file_mips_isa = ISA_MIPS64;
14709       break;
14710 
14711     case OPTION_MTUNE:
14712       mips_set_option_string (&mips_tune_string, arg);
14713       break;
14714 
14715     case OPTION_MARCH:
14716       mips_set_option_string (&mips_arch_string, arg);
14717       break;
14718 
14719     case OPTION_M4650:
14720       mips_set_option_string (&mips_arch_string, "4650");
14721       mips_set_option_string (&mips_tune_string, "4650");
14722       break;
14723 
14724     case OPTION_NO_M4650:
14725       break;
14726 
14727     case OPTION_M4010:
14728       mips_set_option_string (&mips_arch_string, "4010");
14729       mips_set_option_string (&mips_tune_string, "4010");
14730       break;
14731 
14732     case OPTION_NO_M4010:
14733       break;
14734 
14735     case OPTION_M4100:
14736       mips_set_option_string (&mips_arch_string, "4100");
14737       mips_set_option_string (&mips_tune_string, "4100");
14738       break;
14739 
14740     case OPTION_NO_M4100:
14741       break;
14742 
14743     case OPTION_M3900:
14744       mips_set_option_string (&mips_arch_string, "3900");
14745       mips_set_option_string (&mips_tune_string, "3900");
14746       break;
14747 
14748     case OPTION_NO_M3900:
14749       break;
14750 
14751     case OPTION_MDMX:
14752       mips_opts.ase_mdmx = 1;
14753       break;
14754 
14755     case OPTION_NO_MDMX:
14756       mips_opts.ase_mdmx = 0;
14757       break;
14758 
14759     case OPTION_DSP:
14760       mips_opts.ase_dsp = 1;
14761       mips_opts.ase_dspr2 = 0;
14762       break;
14763 
14764     case OPTION_NO_DSP:
14765       mips_opts.ase_dsp = 0;
14766       mips_opts.ase_dspr2 = 0;
14767       break;
14768 
14769     case OPTION_DSPR2:
14770       mips_opts.ase_dspr2 = 1;
14771       mips_opts.ase_dsp = 1;
14772       break;
14773 
14774     case OPTION_NO_DSPR2:
14775       mips_opts.ase_dspr2 = 0;
14776       mips_opts.ase_dsp = 0;
14777       break;
14778 
14779     case OPTION_MT:
14780       mips_opts.ase_mt = 1;
14781       break;
14782 
14783     case OPTION_NO_MT:
14784       mips_opts.ase_mt = 0;
14785       break;
14786 
14787     case OPTION_MCU:
14788       mips_opts.ase_mcu = 1;
14789       break;
14790 
14791     case OPTION_NO_MCU:
14792       mips_opts.ase_mcu = 0;
14793       break;
14794 
14795     case OPTION_MICROMIPS:
14796       if (mips_opts.mips16 == 1)
14797 	{
14798 	  as_bad (_("-mmicromips cannot be used with -mips16"));
14799 	  return 0;
14800 	}
14801       mips_opts.micromips = 1;
14802       mips_no_prev_insn ();
14803       break;
14804 
14805     case OPTION_NO_MICROMIPS:
14806       mips_opts.micromips = 0;
14807       mips_no_prev_insn ();
14808       break;
14809 
14810     case OPTION_MIPS16:
14811       if (mips_opts.micromips == 1)
14812 	{
14813 	  as_bad (_("-mips16 cannot be used with -micromips"));
14814 	  return 0;
14815 	}
14816       mips_opts.mips16 = 1;
14817       mips_no_prev_insn ();
14818       break;
14819 
14820     case OPTION_NO_MIPS16:
14821       mips_opts.mips16 = 0;
14822       mips_no_prev_insn ();
14823       break;
14824 
14825     case OPTION_MIPS3D:
14826       mips_opts.ase_mips3d = 1;
14827       break;
14828 
14829     case OPTION_NO_MIPS3D:
14830       mips_opts.ase_mips3d = 0;
14831       break;
14832 
14833     case OPTION_SMARTMIPS:
14834       mips_opts.ase_smartmips = 1;
14835       break;
14836 
14837     case OPTION_NO_SMARTMIPS:
14838       mips_opts.ase_smartmips = 0;
14839       break;
14840 
14841     case OPTION_FIX_24K:
14842       mips_fix_24k = 1;
14843       break;
14844 
14845     case OPTION_NO_FIX_24K:
14846       mips_fix_24k = 0;
14847       break;
14848 
14849     case OPTION_FIX_LOONGSON2F_JUMP:
14850       mips_fix_loongson2f_jump = TRUE;
14851       break;
14852 
14853     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14854       mips_fix_loongson2f_jump = FALSE;
14855       break;
14856 
14857     case OPTION_FIX_LOONGSON2F_NOP:
14858       mips_fix_loongson2f_nop = TRUE;
14859       break;
14860 
14861     case OPTION_NO_FIX_LOONGSON2F_NOP:
14862       mips_fix_loongson2f_nop = FALSE;
14863       break;
14864 
14865     case OPTION_FIX_VR4120:
14866       mips_fix_vr4120 = 1;
14867       break;
14868 
14869     case OPTION_NO_FIX_VR4120:
14870       mips_fix_vr4120 = 0;
14871       break;
14872 
14873     case OPTION_FIX_VR4130:
14874       mips_fix_vr4130 = 1;
14875       break;
14876 
14877     case OPTION_NO_FIX_VR4130:
14878       mips_fix_vr4130 = 0;
14879       break;
14880 
14881     case OPTION_FIX_LOONGSON2F_BTB:
14882       mips_fix_loongson2f_btb = 1;
14883       break;
14884 
14885     case OPTION_NO_FIX_LOONGSON2F_BTB:
14886       mips_fix_loongson2f_btb = 0;
14887       break;
14888 
14889     case OPTION_FIX_CN63XXP1:
14890       mips_fix_cn63xxp1 = TRUE;
14891       break;
14892 
14893     case OPTION_NO_FIX_CN63XXP1:
14894       mips_fix_cn63xxp1 = FALSE;
14895       break;
14896 
14897     case OPTION_RELAX_BRANCH:
14898       mips_relax_branch = 1;
14899       break;
14900 
14901     case OPTION_NO_RELAX_BRANCH:
14902       mips_relax_branch = 0;
14903       break;
14904 
14905     case OPTION_MSHARED:
14906       mips_in_shared = TRUE;
14907       break;
14908 
14909     case OPTION_MNO_SHARED:
14910       mips_in_shared = FALSE;
14911       break;
14912 
14913     case OPTION_MSYM32:
14914       mips_opts.sym32 = TRUE;
14915       break;
14916 
14917     case OPTION_MNO_SYM32:
14918       mips_opts.sym32 = FALSE;
14919       break;
14920 
14921     case OPTION_TRAP_ZERO_JUMP:
14922       mips_trap_zero_jump = TRUE;
14923       break;
14924 
14925     case OPTION_NO_TRAP_ZERO_JUMP:
14926       mips_trap_zero_jump = FALSE;
14927       break;
14928 
14929 #ifdef OBJ_ELF
14930       /* When generating ELF code, we permit -KPIC and -call_shared to
14931 	 select SVR4_PIC, and -non_shared to select no PIC.  This is
14932 	 intended to be compatible with Irix 5.  */
14933     case OPTION_CALL_SHARED:
14934       if (!IS_ELF)
14935 	{
14936 	  as_bad (_("-call_shared is supported only for ELF format"));
14937 	  return 0;
14938 	}
14939       mips_pic = SVR4_PIC;
14940       mips_abicalls = TRUE;
14941       break;
14942 
14943     case OPTION_CALL_NONPIC:
14944       if (!IS_ELF)
14945 	{
14946 	  as_bad (_("-call_nonpic is supported only for ELF format"));
14947 	  return 0;
14948 	}
14949       mips_pic = NO_PIC;
14950       mips_abicalls = TRUE;
14951       break;
14952 
14953     case OPTION_NON_SHARED:
14954       if (!IS_ELF)
14955 	{
14956 	  as_bad (_("-non_shared is supported only for ELF format"));
14957 	  return 0;
14958 	}
14959       mips_pic = NO_PIC;
14960       mips_abicalls = FALSE;
14961       break;
14962 
14963       /* The -xgot option tells the assembler to use 32 bit offsets
14964          when accessing the got in SVR4_PIC mode.  It is for Irix
14965          compatibility.  */
14966     case OPTION_XGOT:
14967       mips_big_got = 1;
14968       break;
14969 #endif /* OBJ_ELF */
14970 
14971     case 'G':
14972       g_switch_value = atoi (arg);
14973       g_switch_seen = 1;
14974       break;
14975 
14976       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14977 	 and -mabi=64.  */
14978     case OPTION_32:
14979       if (IS_ELF)
14980 	mips_abi = O32_ABI;
14981       /* We silently ignore -32 for non-ELF targets.  This greatly
14982 	 simplifies the construction of the MIPS GAS test cases.  */
14983       break;
14984 
14985 #ifdef OBJ_ELF
14986     case OPTION_N32:
14987       if (!IS_ELF)
14988 	{
14989 	  as_bad (_("-n32 is supported for ELF format only"));
14990 	  return 0;
14991 	}
14992       mips_abi = N32_ABI;
14993       break;
14994 
14995     case OPTION_64:
14996       if (!IS_ELF)
14997 	{
14998 	  as_bad (_("-64 is supported for ELF format only"));
14999 	  return 0;
15000 	}
15001       mips_abi = N64_ABI;
15002       if (!support_64bit_objects())
15003 	as_fatal (_("No compiled in support for 64 bit object file format"));
15004       break;
15005 #endif /* OBJ_ELF */
15006 
15007     case OPTION_GP32:
15008       file_mips_gp32 = 1;
15009       break;
15010 
15011     case OPTION_GP64:
15012       file_mips_gp32 = 0;
15013       break;
15014 
15015     case OPTION_FP32:
15016       file_mips_fp32 = 1;
15017       break;
15018 
15019     case OPTION_FP64:
15020       file_mips_fp32 = 0;
15021       break;
15022 
15023     case OPTION_SINGLE_FLOAT:
15024       file_mips_single_float = 1;
15025       break;
15026 
15027     case OPTION_DOUBLE_FLOAT:
15028       file_mips_single_float = 0;
15029       break;
15030 
15031     case OPTION_SOFT_FLOAT:
15032       file_mips_soft_float = 1;
15033       break;
15034 
15035     case OPTION_HARD_FLOAT:
15036       file_mips_soft_float = 0;
15037       break;
15038 
15039 #ifdef OBJ_ELF
15040     case OPTION_MABI:
15041       if (!IS_ELF)
15042 	{
15043 	  as_bad (_("-mabi is supported for ELF format only"));
15044 	  return 0;
15045 	}
15046       if (strcmp (arg, "32") == 0)
15047 	mips_abi = O32_ABI;
15048       else if (strcmp (arg, "o64") == 0)
15049 	mips_abi = O64_ABI;
15050       else if (strcmp (arg, "n32") == 0)
15051 	mips_abi = N32_ABI;
15052       else if (strcmp (arg, "64") == 0)
15053 	{
15054 	  mips_abi = N64_ABI;
15055 	  if (! support_64bit_objects())
15056 	    as_fatal (_("No compiled in support for 64 bit object file "
15057 			"format"));
15058 	}
15059       else if (strcmp (arg, "eabi") == 0)
15060 	mips_abi = EABI_ABI;
15061       else
15062 	{
15063 	  as_fatal (_("invalid abi -mabi=%s"), arg);
15064 	  return 0;
15065 	}
15066       break;
15067 #endif /* OBJ_ELF */
15068 
15069     case OPTION_M7000_HILO_FIX:
15070       mips_7000_hilo_fix = TRUE;
15071       break;
15072 
15073     case OPTION_MNO_7000_HILO_FIX:
15074       mips_7000_hilo_fix = FALSE;
15075       break;
15076 
15077 #ifdef OBJ_ELF
15078     case OPTION_MDEBUG:
15079       mips_flag_mdebug = TRUE;
15080       break;
15081 
15082     case OPTION_NO_MDEBUG:
15083       mips_flag_mdebug = FALSE;
15084       break;
15085 
15086     case OPTION_PDR:
15087       mips_flag_pdr = TRUE;
15088       break;
15089 
15090     case OPTION_NO_PDR:
15091       mips_flag_pdr = FALSE;
15092       break;
15093 
15094     case OPTION_MVXWORKS_PIC:
15095       mips_pic = VXWORKS_PIC;
15096       break;
15097 #endif /* OBJ_ELF */
15098 
15099     default:
15100       return 0;
15101     }
15102 
15103     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15104 
15105   return 1;
15106 }
15107 
15108 /* Set up globals to generate code for the ISA or processor
15109    described by INFO.  */
15110 
15111 static void
15112 mips_set_architecture (const struct mips_cpu_info *info)
15113 {
15114   if (info != 0)
15115     {
15116       file_mips_arch = info->cpu;
15117       mips_opts.arch = info->cpu;
15118       mips_opts.isa = info->isa;
15119     }
15120 }
15121 
15122 
15123 /* Likewise for tuning.  */
15124 
15125 static void
15126 mips_set_tune (const struct mips_cpu_info *info)
15127 {
15128   if (info != 0)
15129     mips_tune = info->cpu;
15130 }
15131 
15132 
15133 void
15134 mips_after_parse_args (void)
15135 {
15136   const struct mips_cpu_info *arch_info = 0;
15137   const struct mips_cpu_info *tune_info = 0;
15138 
15139   /* GP relative stuff not working for PE */
15140   if (strncmp (TARGET_OS, "pe", 2) == 0)
15141     {
15142       if (g_switch_seen && g_switch_value != 0)
15143 	as_bad (_("-G not supported in this configuration."));
15144       g_switch_value = 0;
15145     }
15146 
15147   if (mips_abi == NO_ABI)
15148     mips_abi = MIPS_DEFAULT_ABI;
15149 
15150   /* The following code determines the architecture and register size.
15151      Similar code was added to GCC 3.3 (see override_options() in
15152      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15153      as much as possible.  */
15154 
15155   if (mips_arch_string != 0)
15156     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15157 
15158   if (file_mips_isa != ISA_UNKNOWN)
15159     {
15160       /* Handle -mipsN.  At this point, file_mips_isa contains the
15161 	 ISA level specified by -mipsN, while arch_info->isa contains
15162 	 the -march selection (if any).  */
15163       if (arch_info != 0)
15164 	{
15165 	  /* -march takes precedence over -mipsN, since it is more descriptive.
15166 	     There's no harm in specifying both as long as the ISA levels
15167 	     are the same.  */
15168 	  if (file_mips_isa != arch_info->isa)
15169 	    as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15170 		    mips_cpu_info_from_isa (file_mips_isa)->name,
15171 		    mips_cpu_info_from_isa (arch_info->isa)->name);
15172 	}
15173       else
15174 	arch_info = mips_cpu_info_from_isa (file_mips_isa);
15175     }
15176 
15177   if (arch_info == 0)
15178     {
15179       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15180       gas_assert (arch_info);
15181     }
15182 
15183   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15184     as_bad (_("-march=%s is not compatible with the selected ABI"),
15185 	    arch_info->name);
15186 
15187   mips_set_architecture (arch_info);
15188 
15189   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
15190   if (mips_tune_string != 0)
15191     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15192 
15193   if (tune_info == 0)
15194     mips_set_tune (arch_info);
15195   else
15196     mips_set_tune (tune_info);
15197 
15198   if (file_mips_gp32 >= 0)
15199     {
15200       /* The user specified the size of the integer registers.  Make sure
15201 	 it agrees with the ABI and ISA.  */
15202       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15203 	as_bad (_("-mgp64 used with a 32-bit processor"));
15204       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15205 	as_bad (_("-mgp32 used with a 64-bit ABI"));
15206       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15207 	as_bad (_("-mgp64 used with a 32-bit ABI"));
15208     }
15209   else
15210     {
15211       /* Infer the integer register size from the ABI and processor.
15212 	 Restrict ourselves to 32-bit registers if that's all the
15213 	 processor has, or if the ABI cannot handle 64-bit registers.  */
15214       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15215 			|| !ISA_HAS_64BIT_REGS (mips_opts.isa));
15216     }
15217 
15218   switch (file_mips_fp32)
15219     {
15220     default:
15221     case -1:
15222       /* No user specified float register size.
15223 	 ??? GAS treats single-float processors as though they had 64-bit
15224 	 float registers (although it complains when double-precision
15225 	 instructions are used).  As things stand, saying they have 32-bit
15226 	 registers would lead to spurious "register must be even" messages.
15227 	 So here we assume float registers are never smaller than the
15228 	 integer ones.  */
15229       if (file_mips_gp32 == 0)
15230 	/* 64-bit integer registers implies 64-bit float registers.  */
15231 	file_mips_fp32 = 0;
15232       else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15233 	       && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15234 	/* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
15235 	file_mips_fp32 = 0;
15236       else
15237 	/* 32-bit float registers.  */
15238 	file_mips_fp32 = 1;
15239       break;
15240 
15241     /* The user specified the size of the float registers.  Check if it
15242        agrees with the ABI and ISA.  */
15243     case 0:
15244       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15245 	as_bad (_("-mfp64 used with a 32-bit fpu"));
15246       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15247 	       && !ISA_HAS_MXHC1 (mips_opts.isa))
15248 	as_warn (_("-mfp64 used with a 32-bit ABI"));
15249       break;
15250     case 1:
15251       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15252 	as_warn (_("-mfp32 used with a 64-bit ABI"));
15253       break;
15254     }
15255 
15256   /* End of GCC-shared inference code.  */
15257 
15258   /* This flag is set when we have a 64-bit capable CPU but use only
15259      32-bit wide registers.  Note that EABI does not use it.  */
15260   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15261       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15262 	  || mips_abi == O32_ABI))
15263     mips_32bitmode = 1;
15264 
15265   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15266     as_bad (_("trap exception not supported at ISA 1"));
15267 
15268   /* If the selected architecture includes support for ASEs, enable
15269      generation of code for them.  */
15270   if (mips_opts.mips16 == -1)
15271     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15272   if (mips_opts.micromips == -1)
15273     mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15274   if (mips_opts.ase_mips3d == -1)
15275     mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
15276 			    && file_mips_fp32 == 0) ? 1 : 0;
15277   if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15278     as_bad (_("-mfp32 used with -mips3d"));
15279 
15280   if (mips_opts.ase_mdmx == -1)
15281     mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
15282 			  && file_mips_fp32 == 0) ? 1 : 0;
15283   if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15284     as_bad (_("-mfp32 used with -mdmx"));
15285 
15286   if (mips_opts.ase_smartmips == -1)
15287     mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15288   if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
15289     as_warn (_("%s ISA does not support SmartMIPS"),
15290 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15291 
15292   if (mips_opts.ase_dsp == -1)
15293     mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15294   if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
15295     as_warn (_("%s ISA does not support DSP ASE"),
15296 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15297 
15298   if (mips_opts.ase_dspr2 == -1)
15299     {
15300       mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15301       mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15302     }
15303   if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
15304     as_warn (_("%s ISA does not support DSP R2 ASE"),
15305 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15306 
15307   if (mips_opts.ase_mt == -1)
15308     mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15309   if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
15310     as_warn (_("%s ISA does not support MT ASE"),
15311 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15312 
15313   if (mips_opts.ase_mcu == -1)
15314     mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15315   if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15316       as_warn (_("%s ISA does not support MCU ASE"),
15317 	       mips_cpu_info_from_isa (mips_opts.isa)->name);
15318 
15319   file_mips_isa = mips_opts.isa;
15320   file_ase_mips3d = mips_opts.ase_mips3d;
15321   file_ase_mdmx = mips_opts.ase_mdmx;
15322   file_ase_smartmips = mips_opts.ase_smartmips;
15323   file_ase_dsp = mips_opts.ase_dsp;
15324   file_ase_dspr2 = mips_opts.ase_dspr2;
15325   file_ase_mt = mips_opts.ase_mt;
15326   mips_opts.gp32 = file_mips_gp32;
15327   mips_opts.fp32 = file_mips_fp32;
15328   mips_opts.soft_float = file_mips_soft_float;
15329   mips_opts.single_float = file_mips_single_float;
15330 
15331   if (mips_flag_mdebug < 0)
15332     {
15333 #ifdef OBJ_MAYBE_ECOFF
15334       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15335 	mips_flag_mdebug = 1;
15336       else
15337 #endif /* OBJ_MAYBE_ECOFF */
15338 	mips_flag_mdebug = 0;
15339     }
15340 }
15341 
15342 void
15343 mips_init_after_args (void)
15344 {
15345   /* initialize opcodes */
15346   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15347   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15348 }
15349 
15350 long
15351 md_pcrel_from (fixS *fixP)
15352 {
15353   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15354   switch (fixP->fx_r_type)
15355     {
15356     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15357     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15358       /* Return the address of the delay slot.  */
15359       return addr + 2;
15360 
15361     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15362     case BFD_RELOC_MICROMIPS_JMP:
15363     case BFD_RELOC_16_PCREL_S2:
15364     case BFD_RELOC_MIPS_JMP:
15365       /* Return the address of the delay slot.  */
15366       return addr + 4;
15367 
15368     default:
15369       /* We have no relocation type for PC relative MIPS16 instructions.  */
15370       if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15371 	as_bad_where (fixP->fx_file, fixP->fx_line,
15372 		      _("PC relative MIPS16 instruction references a different section"));
15373       return addr;
15374     }
15375 }
15376 
15377 /* This is called before the symbol table is processed.  In order to
15378    work with gcc when using mips-tfile, we must keep all local labels.
15379    However, in other cases, we want to discard them.  If we were
15380    called with -g, but we didn't see any debugging information, it may
15381    mean that gcc is smuggling debugging information through to
15382    mips-tfile, in which case we must generate all local labels.  */
15383 
15384 void
15385 mips_frob_file_before_adjust (void)
15386 {
15387 #ifndef NO_ECOFF_DEBUGGING
15388   if (ECOFF_DEBUGGING
15389       && mips_debug != 0
15390       && ! ecoff_debugging_seen)
15391     flag_keep_locals = 1;
15392 #endif
15393 }
15394 
15395 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15396    the corresponding LO16 reloc.  This is called before md_apply_fix and
15397    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15398    relocation operators.
15399 
15400    For our purposes, a %lo() expression matches a %got() or %hi()
15401    expression if:
15402 
15403       (a) it refers to the same symbol; and
15404       (b) the offset applied in the %lo() expression is no lower than
15405 	  the offset applied in the %got() or %hi().
15406 
15407    (b) allows us to cope with code like:
15408 
15409 	lui	$4,%hi(foo)
15410 	lh	$4,%lo(foo+2)($4)
15411 
15412    ...which is legal on RELA targets, and has a well-defined behaviour
15413    if the user knows that adding 2 to "foo" will not induce a carry to
15414    the high 16 bits.
15415 
15416    When several %lo()s match a particular %got() or %hi(), we use the
15417    following rules to distinguish them:
15418 
15419      (1) %lo()s with smaller offsets are a better match than %lo()s with
15420          higher offsets.
15421 
15422      (2) %lo()s with no matching %got() or %hi() are better than those
15423          that already have a matching %got() or %hi().
15424 
15425      (3) later %lo()s are better than earlier %lo()s.
15426 
15427    These rules are applied in order.
15428 
15429    (1) means, among other things, that %lo()s with identical offsets are
15430    chosen if they exist.
15431 
15432    (2) means that we won't associate several high-part relocations with
15433    the same low-part relocation unless there's no alternative.  Having
15434    several high parts for the same low part is a GNU extension; this rule
15435    allows careful users to avoid it.
15436 
15437    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15438    with the last high-part relocation being at the front of the list.
15439    It therefore makes sense to choose the last matching low-part
15440    relocation, all other things being equal.  It's also easier
15441    to code that way.  */
15442 
15443 void
15444 mips_frob_file (void)
15445 {
15446   struct mips_hi_fixup *l;
15447   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15448 
15449   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15450     {
15451       segment_info_type *seginfo;
15452       bfd_boolean matched_lo_p;
15453       fixS **hi_pos, **lo_pos, **pos;
15454 
15455       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15456 
15457       /* If a GOT16 relocation turns out to be against a global symbol,
15458 	 there isn't supposed to be a matching LO.  */
15459       if (got16_reloc_p (l->fixp->fx_r_type)
15460 	  && !pic_need_relax (l->fixp->fx_addsy, l->seg))
15461 	continue;
15462 
15463       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15464       if (fixup_has_matching_lo_p (l->fixp))
15465 	continue;
15466 
15467       seginfo = seg_info (l->seg);
15468 
15469       /* Set HI_POS to the position of this relocation in the chain.
15470 	 Set LO_POS to the position of the chosen low-part relocation.
15471 	 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15472 	 relocation that matches an immediately-preceding high-part
15473 	 relocation.  */
15474       hi_pos = NULL;
15475       lo_pos = NULL;
15476       matched_lo_p = FALSE;
15477       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15478 
15479       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15480 	{
15481 	  if (*pos == l->fixp)
15482 	    hi_pos = pos;
15483 
15484 	  if ((*pos)->fx_r_type == looking_for_rtype
15485 	      && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15486 	      && (*pos)->fx_offset >= l->fixp->fx_offset
15487 	      && (lo_pos == NULL
15488 		  || (*pos)->fx_offset < (*lo_pos)->fx_offset
15489 		  || (!matched_lo_p
15490 		      && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15491 	    lo_pos = pos;
15492 
15493 	  matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15494 			  && fixup_has_matching_lo_p (*pos));
15495 	}
15496 
15497       /* If we found a match, remove the high-part relocation from its
15498 	 current position and insert it before the low-part relocation.
15499 	 Make the offsets match so that fixup_has_matching_lo_p()
15500 	 will return true.
15501 
15502 	 We don't warn about unmatched high-part relocations since some
15503 	 versions of gcc have been known to emit dead "lui ...%hi(...)"
15504 	 instructions.  */
15505       if (lo_pos != NULL)
15506 	{
15507 	  l->fixp->fx_offset = (*lo_pos)->fx_offset;
15508 	  if (l->fixp->fx_next != *lo_pos)
15509 	    {
15510 	      *hi_pos = l->fixp->fx_next;
15511 	      l->fixp->fx_next = *lo_pos;
15512 	      *lo_pos = l->fixp;
15513 	    }
15514 	}
15515     }
15516 }
15517 
15518 /* We may have combined relocations without symbols in the N32/N64 ABI.
15519    We have to prevent gas from dropping them.  */
15520 
15521 int
15522 mips_force_relocation (fixS *fixp)
15523 {
15524   if (generic_force_reloc (fixp))
15525     return 1;
15526 
15527   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15528      so that the linker relaxation can update targets.  */
15529   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15530       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15531       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15532     return 1;
15533 
15534   if (HAVE_NEWABI
15535       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
15536       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
15537 	  || hi16_reloc_p (fixp->fx_r_type)
15538 	  || lo16_reloc_p (fixp->fx_r_type)))
15539     return 1;
15540 
15541   return 0;
15542 }
15543 
15544 /* Apply a fixup to the object file.  */
15545 
15546 void
15547 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15548 {
15549   bfd_byte *buf;
15550   long insn;
15551   reloc_howto_type *howto;
15552 
15553   /* We ignore generic BFD relocations we don't know about.  */
15554   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15555   if (! howto)
15556     return;
15557 
15558   gas_assert (fixP->fx_size == 2
15559 	      || fixP->fx_size == 4
15560 	      || fixP->fx_r_type == BFD_RELOC_16
15561 	      || fixP->fx_r_type == BFD_RELOC_64
15562 	      || fixP->fx_r_type == BFD_RELOC_CTOR
15563 	      || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15564 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15565 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15566 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15567 	      || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15568 
15569   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
15570 
15571   gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15572 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15573 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15574 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
15575 
15576   /* Don't treat parts of a composite relocation as done.  There are two
15577      reasons for this:
15578 
15579      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15580 	 should nevertheless be emitted if the first part is.
15581 
15582      (2) In normal usage, composite relocations are never assembly-time
15583 	 constants.  The easiest way of dealing with the pathological
15584 	 exceptions is to generate a relocation against STN_UNDEF and
15585 	 leave everything up to the linker.  */
15586   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15587     fixP->fx_done = 1;
15588 
15589   switch (fixP->fx_r_type)
15590     {
15591     case BFD_RELOC_MIPS_TLS_GD:
15592     case BFD_RELOC_MIPS_TLS_LDM:
15593     case BFD_RELOC_MIPS_TLS_DTPREL32:
15594     case BFD_RELOC_MIPS_TLS_DTPREL64:
15595     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15596     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15597     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15598     case BFD_RELOC_MIPS_TLS_TPREL32:
15599     case BFD_RELOC_MIPS_TLS_TPREL64:
15600     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15601     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15602     case BFD_RELOC_MICROMIPS_TLS_GD:
15603     case BFD_RELOC_MICROMIPS_TLS_LDM:
15604     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15605     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15606     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15607     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15608     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15609     case BFD_RELOC_MIPS16_TLS_GD:
15610     case BFD_RELOC_MIPS16_TLS_LDM:
15611     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15612     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15613     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15614     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15615     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15616       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15617       /* fall through */
15618 
15619     case BFD_RELOC_MIPS_JMP:
15620     case BFD_RELOC_MIPS_SHIFT5:
15621     case BFD_RELOC_MIPS_SHIFT6:
15622     case BFD_RELOC_MIPS_GOT_DISP:
15623     case BFD_RELOC_MIPS_GOT_PAGE:
15624     case BFD_RELOC_MIPS_GOT_OFST:
15625     case BFD_RELOC_MIPS_SUB:
15626     case BFD_RELOC_MIPS_INSERT_A:
15627     case BFD_RELOC_MIPS_INSERT_B:
15628     case BFD_RELOC_MIPS_DELETE:
15629     case BFD_RELOC_MIPS_HIGHEST:
15630     case BFD_RELOC_MIPS_HIGHER:
15631     case BFD_RELOC_MIPS_SCN_DISP:
15632     case BFD_RELOC_MIPS_REL16:
15633     case BFD_RELOC_MIPS_RELGOT:
15634     case BFD_RELOC_MIPS_JALR:
15635     case BFD_RELOC_HI16:
15636     case BFD_RELOC_HI16_S:
15637     case BFD_RELOC_GPREL16:
15638     case BFD_RELOC_MIPS_LITERAL:
15639     case BFD_RELOC_MIPS_CALL16:
15640     case BFD_RELOC_MIPS_GOT16:
15641     case BFD_RELOC_GPREL32:
15642     case BFD_RELOC_MIPS_GOT_HI16:
15643     case BFD_RELOC_MIPS_GOT_LO16:
15644     case BFD_RELOC_MIPS_CALL_HI16:
15645     case BFD_RELOC_MIPS_CALL_LO16:
15646     case BFD_RELOC_MIPS16_GPREL:
15647     case BFD_RELOC_MIPS16_GOT16:
15648     case BFD_RELOC_MIPS16_CALL16:
15649     case BFD_RELOC_MIPS16_HI16:
15650     case BFD_RELOC_MIPS16_HI16_S:
15651     case BFD_RELOC_MIPS16_JMP:
15652     case BFD_RELOC_MICROMIPS_JMP:
15653     case BFD_RELOC_MICROMIPS_GOT_DISP:
15654     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15655     case BFD_RELOC_MICROMIPS_GOT_OFST:
15656     case BFD_RELOC_MICROMIPS_SUB:
15657     case BFD_RELOC_MICROMIPS_HIGHEST:
15658     case BFD_RELOC_MICROMIPS_HIGHER:
15659     case BFD_RELOC_MICROMIPS_SCN_DISP:
15660     case BFD_RELOC_MICROMIPS_JALR:
15661     case BFD_RELOC_MICROMIPS_HI16:
15662     case BFD_RELOC_MICROMIPS_HI16_S:
15663     case BFD_RELOC_MICROMIPS_GPREL16:
15664     case BFD_RELOC_MICROMIPS_LITERAL:
15665     case BFD_RELOC_MICROMIPS_CALL16:
15666     case BFD_RELOC_MICROMIPS_GOT16:
15667     case BFD_RELOC_MICROMIPS_GOT_HI16:
15668     case BFD_RELOC_MICROMIPS_GOT_LO16:
15669     case BFD_RELOC_MICROMIPS_CALL_HI16:
15670     case BFD_RELOC_MICROMIPS_CALL_LO16:
15671       /* Nothing needed to do.  The value comes from the reloc entry.  */
15672       break;
15673 
15674     case BFD_RELOC_64:
15675       /* This is handled like BFD_RELOC_32, but we output a sign
15676          extended value if we are only 32 bits.  */
15677       if (fixP->fx_done)
15678 	{
15679 	  if (8 <= sizeof (valueT))
15680 	    md_number_to_chars ((char *) buf, *valP, 8);
15681 	  else
15682 	    {
15683 	      valueT hiv;
15684 
15685 	      if ((*valP & 0x80000000) != 0)
15686 		hiv = 0xffffffff;
15687 	      else
15688 		hiv = 0;
15689 	      md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
15690 				  *valP, 4);
15691 	      md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
15692 				  hiv, 4);
15693 	    }
15694 	}
15695       break;
15696 
15697     case BFD_RELOC_RVA:
15698     case BFD_RELOC_32:
15699     case BFD_RELOC_16:
15700       /* If we are deleting this reloc entry, we must fill in the
15701 	 value now.  This can happen if we have a .word which is not
15702 	 resolved when it appears but is later defined.  */
15703       if (fixP->fx_done)
15704 	md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
15705       break;
15706 
15707     case BFD_RELOC_LO16:
15708     case BFD_RELOC_MIPS16_LO16:
15709     case BFD_RELOC_MICROMIPS_LO16:
15710       /* FIXME: Now that embedded-PIC is gone, some of this code/comment
15711 	 may be safe to remove, but if so it's not obvious.  */
15712       /* When handling an embedded PIC switch statement, we can wind
15713 	 up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
15714       if (fixP->fx_done)
15715 	{
15716 	  if (*valP + 0x8000 > 0xffff)
15717 	    as_bad_where (fixP->fx_file, fixP->fx_line,
15718 			  _("relocation overflow"));
15719 	  /* 32-bit microMIPS instructions are divided into two halfwords.
15720 	     Relocations always refer to the second halfword, regardless
15721 	     of endianness.  */
15722 	  if (target_big_endian || fixP->fx_r_type == BFD_RELOC_MICROMIPS_LO16)
15723 	    buf += 2;
15724 	  md_number_to_chars ((char *) buf, *valP, 2);
15725 	}
15726       break;
15727 
15728     case BFD_RELOC_16_PCREL_S2:
15729       if ((*valP & 0x3) != 0)
15730 	as_bad_where (fixP->fx_file, fixP->fx_line,
15731 		      _("Branch to misaligned address (%lx)"), (long) *valP);
15732 
15733       /* We need to save the bits in the instruction since fixup_segment()
15734 	 might be deleting the relocation entry (i.e., a branch within
15735 	 the current segment).  */
15736       if (! fixP->fx_done)
15737 	break;
15738 
15739       /* Update old instruction data.  */
15740       if (target_big_endian)
15741 	insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
15742       else
15743 	insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
15744 
15745       if (*valP + 0x20000 <= 0x3ffff)
15746 	{
15747 	  insn |= (*valP >> 2) & 0xffff;
15748 	  md_number_to_chars ((char *) buf, insn, 4);
15749 	}
15750       else if (mips_pic == NO_PIC
15751 	       && fixP->fx_done
15752 	       && fixP->fx_frag->fr_address >= text_section->vma
15753 	       && (fixP->fx_frag->fr_address
15754 		   < text_section->vma + bfd_get_section_size (text_section))
15755 	       && ((insn & 0xffff0000) == 0x10000000	 /* beq $0,$0 */
15756 		   || (insn & 0xffff0000) == 0x04010000	 /* bgez $0 */
15757 		   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15758 	{
15759 	  /* The branch offset is too large.  If this is an
15760              unconditional branch, and we are not generating PIC code,
15761              we can convert it to an absolute jump instruction.  */
15762 	  if ((insn & 0xffff0000) == 0x04110000)	 /* bgezal $0 */
15763 	    insn = 0x0c000000;	/* jal */
15764 	  else
15765 	    insn = 0x08000000;	/* j */
15766 	  fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15767 	  fixP->fx_done = 0;
15768 	  fixP->fx_addsy = section_symbol (text_section);
15769 	  *valP += md_pcrel_from (fixP);
15770 	  md_number_to_chars ((char *) buf, insn, 4);
15771 	}
15772       else
15773 	{
15774 	  /* If we got here, we have branch-relaxation disabled,
15775 	     and there's nothing we can do to fix this instruction
15776 	     without turning it into a longer sequence.  */
15777 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15778 			_("Branch out of range"));
15779 	}
15780       break;
15781 
15782     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15783     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15784     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15785       /* We adjust the offset back to even.  */
15786       if ((*valP & 0x1) != 0)
15787 	--(*valP);
15788 
15789       if (! fixP->fx_done)
15790 	break;
15791 
15792       /* Should never visit here, because we keep the relocation.  */
15793       abort ();
15794       break;
15795 
15796     case BFD_RELOC_VTABLE_INHERIT:
15797       fixP->fx_done = 0;
15798       if (fixP->fx_addsy
15799           && !S_IS_DEFINED (fixP->fx_addsy)
15800           && !S_IS_WEAK (fixP->fx_addsy))
15801         S_SET_WEAK (fixP->fx_addsy);
15802       break;
15803 
15804     case BFD_RELOC_VTABLE_ENTRY:
15805       fixP->fx_done = 0;
15806       break;
15807 
15808     default:
15809       internalError ();
15810     }
15811 
15812   /* Remember value for tc_gen_reloc.  */
15813   fixP->fx_addnumber = *valP;
15814 }
15815 
15816 static symbolS *
15817 get_symbol (void)
15818 {
15819   int c;
15820   char *name;
15821   symbolS *p;
15822 
15823   name = input_line_pointer;
15824   c = get_symbol_end ();
15825   p = (symbolS *) symbol_find_or_make (name);
15826   *input_line_pointer = c;
15827   return p;
15828 }
15829 
15830 /* Align the current frag to a given power of two.  If a particular
15831    fill byte should be used, FILL points to an integer that contains
15832    that byte, otherwise FILL is null.
15833 
15834    This function used to have the comment:
15835 
15836       The MIPS assembler also automatically adjusts any preceding label.
15837 
15838    The implementation therefore applied the adjustment to a maximum of
15839    one label.  However, other label adjustments are applied to batches
15840    of labels, and adjusting just one caused problems when new labels
15841    were added for the sake of debugging or unwind information.
15842    We therefore adjust all preceding labels (given as LABELS) instead.  */
15843 
15844 static void
15845 mips_align (int to, int *fill, struct insn_label_list *labels)
15846 {
15847   mips_emit_delays ();
15848   mips_record_compressed_mode ();
15849   if (fill == NULL && subseg_text_p (now_seg))
15850     frag_align_code (to, 0);
15851   else
15852     frag_align (to, fill ? *fill : 0, 0);
15853   record_alignment (now_seg, to);
15854   mips_move_labels (labels, FALSE);
15855 }
15856 
15857 /* Align to a given power of two.  .align 0 turns off the automatic
15858    alignment used by the data creating pseudo-ops.  */
15859 
15860 static void
15861 s_align (int x ATTRIBUTE_UNUSED)
15862 {
15863   int temp, fill_value, *fill_ptr;
15864   long max_alignment = 28;
15865 
15866   /* o Note that the assembler pulls down any immediately preceding label
15867        to the aligned address.
15868      o It's not documented but auto alignment is reinstated by
15869        a .align pseudo instruction.
15870      o Note also that after auto alignment is turned off the mips assembler
15871        issues an error on attempt to assemble an improperly aligned data item.
15872        We don't.  */
15873 
15874   temp = get_absolute_expression ();
15875   if (temp > max_alignment)
15876     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15877   else if (temp < 0)
15878     {
15879       as_warn (_("Alignment negative: 0 assumed."));
15880       temp = 0;
15881     }
15882   if (*input_line_pointer == ',')
15883     {
15884       ++input_line_pointer;
15885       fill_value = get_absolute_expression ();
15886       fill_ptr = &fill_value;
15887     }
15888   else
15889     fill_ptr = 0;
15890   if (temp)
15891     {
15892       segment_info_type *si = seg_info (now_seg);
15893       struct insn_label_list *l = si->label_list;
15894       /* Auto alignment should be switched on by next section change.  */
15895       auto_align = 1;
15896       mips_align (temp, fill_ptr, l);
15897     }
15898   else
15899     {
15900       auto_align = 0;
15901     }
15902 
15903   demand_empty_rest_of_line ();
15904 }
15905 
15906 static void
15907 s_change_sec (int sec)
15908 {
15909   segT seg;
15910 
15911 #ifdef OBJ_ELF
15912   /* The ELF backend needs to know that we are changing sections, so
15913      that .previous works correctly.  We could do something like check
15914      for an obj_section_change_hook macro, but that might be confusing
15915      as it would not be appropriate to use it in the section changing
15916      functions in read.c, since obj-elf.c intercepts those.  FIXME:
15917      This should be cleaner, somehow.  */
15918   if (IS_ELF)
15919     obj_elf_section_change_hook ();
15920 #endif
15921 
15922   mips_emit_delays ();
15923 
15924   switch (sec)
15925     {
15926     case 't':
15927       s_text (0);
15928       break;
15929     case 'd':
15930       s_data (0);
15931       break;
15932     case 'b':
15933       subseg_set (bss_section, (subsegT) get_absolute_expression ());
15934       demand_empty_rest_of_line ();
15935       break;
15936 
15937     case 'r':
15938       seg = subseg_new (RDATA_SECTION_NAME,
15939 			(subsegT) get_absolute_expression ());
15940       if (IS_ELF)
15941 	{
15942 	  bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15943 						  | SEC_READONLY | SEC_RELOC
15944 						  | SEC_DATA));
15945 	  if (strncmp (TARGET_OS, "elf", 3) != 0)
15946 	    record_alignment (seg, 4);
15947 	}
15948       demand_empty_rest_of_line ();
15949       break;
15950 
15951     case 's':
15952       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15953       if (IS_ELF)
15954 	{
15955 	  bfd_set_section_flags (stdoutput, seg,
15956 				 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15957 	  if (strncmp (TARGET_OS, "elf", 3) != 0)
15958 	    record_alignment (seg, 4);
15959 	}
15960       demand_empty_rest_of_line ();
15961       break;
15962 
15963     case 'B':
15964       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15965       if (IS_ELF)
15966 	{
15967 	  bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15968 	  if (strncmp (TARGET_OS, "elf", 3) != 0)
15969 	    record_alignment (seg, 4);
15970 	}
15971       demand_empty_rest_of_line ();
15972       break;
15973     }
15974 
15975   auto_align = 1;
15976 }
15977 
15978 void
15979 s_change_section (int ignore ATTRIBUTE_UNUSED)
15980 {
15981 #ifdef OBJ_ELF
15982   char *section_name;
15983   char c;
15984   char next_c = 0;
15985   int section_type;
15986   int section_flag;
15987   int section_entry_size;
15988   int section_alignment;
15989 
15990   if (!IS_ELF)
15991     return;
15992 
15993   section_name = input_line_pointer;
15994   c = get_symbol_end ();
15995   if (c)
15996     next_c = *(input_line_pointer + 1);
15997 
15998   /* Do we have .section Name<,"flags">?  */
15999   if (c != ',' || (c == ',' && next_c == '"'))
16000     {
16001       /* just after name is now '\0'.  */
16002       *input_line_pointer = c;
16003       input_line_pointer = section_name;
16004       obj_elf_section (ignore);
16005       return;
16006     }
16007   input_line_pointer++;
16008 
16009   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16010   if (c == ',')
16011     section_type = get_absolute_expression ();
16012   else
16013     section_type = 0;
16014   if (*input_line_pointer++ == ',')
16015     section_flag = get_absolute_expression ();
16016   else
16017     section_flag = 0;
16018   if (*input_line_pointer++ == ',')
16019     section_entry_size = get_absolute_expression ();
16020   else
16021     section_entry_size = 0;
16022   if (*input_line_pointer++ == ',')
16023     section_alignment = get_absolute_expression ();
16024   else
16025     section_alignment = 0;
16026   /* FIXME: really ignore?  */
16027   (void) section_alignment;
16028 
16029   section_name = xstrdup (section_name);
16030 
16031   /* When using the generic form of .section (as implemented by obj-elf.c),
16032      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16033      traditionally had to fall back on the more common @progbits instead.
16034 
16035      There's nothing really harmful in this, since bfd will correct
16036      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16037      means that, for backwards compatibility, the special_section entries
16038      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16039 
16040      Even so, we shouldn't force users of the MIPS .section syntax to
16041      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16042      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16043      generic type-checking code.  */
16044   if (section_type == SHT_MIPS_DWARF)
16045     section_type = SHT_PROGBITS;
16046 
16047   obj_elf_change_section (section_name, section_type, section_flag,
16048 			  section_entry_size, 0, 0, 0);
16049 
16050   if (now_seg->name != section_name)
16051     free (section_name);
16052 #endif /* OBJ_ELF */
16053 }
16054 
16055 void
16056 mips_enable_auto_align (void)
16057 {
16058   auto_align = 1;
16059 }
16060 
16061 static void
16062 s_cons (int log_size)
16063 {
16064   segment_info_type *si = seg_info (now_seg);
16065   struct insn_label_list *l = si->label_list;
16066 
16067   mips_emit_delays ();
16068   if (log_size > 0 && auto_align)
16069     mips_align (log_size, 0, l);
16070   cons (1 << log_size);
16071   mips_clear_insn_labels ();
16072 }
16073 
16074 static void
16075 s_float_cons (int type)
16076 {
16077   segment_info_type *si = seg_info (now_seg);
16078   struct insn_label_list *l = si->label_list;
16079 
16080   mips_emit_delays ();
16081 
16082   if (auto_align)
16083     {
16084       if (type == 'd')
16085 	mips_align (3, 0, l);
16086       else
16087 	mips_align (2, 0, l);
16088     }
16089 
16090   float_cons (type);
16091   mips_clear_insn_labels ();
16092 }
16093 
16094 /* Handle .globl.  We need to override it because on Irix 5 you are
16095    permitted to say
16096        .globl foo .text
16097    where foo is an undefined symbol, to mean that foo should be
16098    considered to be the address of a function.  */
16099 
16100 static void
16101 s_mips_globl (int x ATTRIBUTE_UNUSED)
16102 {
16103   char *name;
16104   int c;
16105   symbolS *symbolP;
16106   flagword flag;
16107 
16108   do
16109     {
16110       name = input_line_pointer;
16111       c = get_symbol_end ();
16112       symbolP = symbol_find_or_make (name);
16113       S_SET_EXTERNAL (symbolP);
16114 
16115       *input_line_pointer = c;
16116       SKIP_WHITESPACE ();
16117 
16118       /* On Irix 5, every global symbol that is not explicitly labelled as
16119          being a function is apparently labelled as being an object.  */
16120       flag = BSF_OBJECT;
16121 
16122       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16123 	  && (*input_line_pointer != ','))
16124 	{
16125 	  char *secname;
16126 	  asection *sec;
16127 
16128 	  secname = input_line_pointer;
16129 	  c = get_symbol_end ();
16130 	  sec = bfd_get_section_by_name (stdoutput, secname);
16131 	  if (sec == NULL)
16132 	    as_bad (_("%s: no such section"), secname);
16133 	  *input_line_pointer = c;
16134 
16135 	  if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16136 	    flag = BSF_FUNCTION;
16137 	}
16138 
16139       symbol_get_bfdsym (symbolP)->flags |= flag;
16140 
16141       c = *input_line_pointer;
16142       if (c == ',')
16143 	{
16144 	  input_line_pointer++;
16145 	  SKIP_WHITESPACE ();
16146 	  if (is_end_of_line[(unsigned char) *input_line_pointer])
16147 	    c = '\n';
16148 	}
16149     }
16150   while (c == ',');
16151 
16152   demand_empty_rest_of_line ();
16153 }
16154 
16155 static void
16156 s_option (int x ATTRIBUTE_UNUSED)
16157 {
16158   char *opt;
16159   char c;
16160 
16161   opt = input_line_pointer;
16162   c = get_symbol_end ();
16163 
16164   if (*opt == 'O')
16165     {
16166       /* FIXME: What does this mean?  */
16167     }
16168   else if (strncmp (opt, "pic", 3) == 0)
16169     {
16170       int i;
16171 
16172       i = atoi (opt + 3);
16173       if (i == 0)
16174 	mips_pic = NO_PIC;
16175       else if (i == 2)
16176 	{
16177 	  mips_pic = SVR4_PIC;
16178 	  mips_abicalls = TRUE;
16179 	}
16180       else
16181 	as_bad (_(".option pic%d not supported"), i);
16182 
16183       if (mips_pic == SVR4_PIC)
16184 	{
16185 	  if (g_switch_seen && g_switch_value != 0)
16186 	    as_warn (_("-G may not be used with SVR4 PIC code"));
16187 	  g_switch_value = 0;
16188 	  bfd_set_gp_size (stdoutput, 0);
16189 	}
16190     }
16191   else
16192     as_warn (_("Unrecognized option \"%s\""), opt);
16193 
16194   *input_line_pointer = c;
16195   demand_empty_rest_of_line ();
16196 }
16197 
16198 /* This structure is used to hold a stack of .set values.  */
16199 
16200 struct mips_option_stack
16201 {
16202   struct mips_option_stack *next;
16203   struct mips_set_options options;
16204 };
16205 
16206 static struct mips_option_stack *mips_opts_stack;
16207 
16208 /* Handle the .set pseudo-op.  */
16209 
16210 static void
16211 s_mipsset (int x ATTRIBUTE_UNUSED)
16212 {
16213   char *name = input_line_pointer, ch;
16214 
16215   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16216     ++input_line_pointer;
16217   ch = *input_line_pointer;
16218   *input_line_pointer = '\0';
16219 
16220   if (strcmp (name, "reorder") == 0)
16221     {
16222       if (mips_opts.noreorder)
16223 	end_noreorder ();
16224     }
16225   else if (strcmp (name, "noreorder") == 0)
16226     {
16227       if (!mips_opts.noreorder)
16228 	start_noreorder ();
16229     }
16230   else if (strncmp (name, "at=", 3) == 0)
16231     {
16232       char *s = name + 3;
16233 
16234       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16235 	as_bad (_("Unrecognized register name `%s'"), s);
16236     }
16237   else if (strcmp (name, "at") == 0)
16238     {
16239       mips_opts.at = ATREG;
16240     }
16241   else if (strcmp (name, "noat") == 0)
16242     {
16243       mips_opts.at = ZERO;
16244     }
16245   else if (strcmp (name, "macro") == 0)
16246     {
16247       mips_opts.warn_about_macros = 0;
16248     }
16249   else if (strcmp (name, "nomacro") == 0)
16250     {
16251       if (mips_opts.noreorder == 0)
16252 	as_bad (_("`noreorder' must be set before `nomacro'"));
16253       mips_opts.warn_about_macros = 1;
16254     }
16255   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16256     {
16257       mips_opts.nomove = 0;
16258     }
16259   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16260     {
16261       mips_opts.nomove = 1;
16262     }
16263   else if (strcmp (name, "bopt") == 0)
16264     {
16265       mips_opts.nobopt = 0;
16266     }
16267   else if (strcmp (name, "nobopt") == 0)
16268     {
16269       mips_opts.nobopt = 1;
16270     }
16271   else if (strcmp (name, "gp=default") == 0)
16272     mips_opts.gp32 = file_mips_gp32;
16273   else if (strcmp (name, "gp=32") == 0)
16274     mips_opts.gp32 = 1;
16275   else if (strcmp (name, "gp=64") == 0)
16276     {
16277       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16278 	as_warn (_("%s isa does not support 64-bit registers"),
16279 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16280       mips_opts.gp32 = 0;
16281     }
16282   else if (strcmp (name, "fp=default") == 0)
16283     mips_opts.fp32 = file_mips_fp32;
16284   else if (strcmp (name, "fp=32") == 0)
16285     mips_opts.fp32 = 1;
16286   else if (strcmp (name, "fp=64") == 0)
16287     {
16288       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16289 	as_warn (_("%s isa does not support 64-bit floating point registers"),
16290 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16291       mips_opts.fp32 = 0;
16292     }
16293   else if (strcmp (name, "softfloat") == 0)
16294     mips_opts.soft_float = 1;
16295   else if (strcmp (name, "hardfloat") == 0)
16296     mips_opts.soft_float = 0;
16297   else if (strcmp (name, "singlefloat") == 0)
16298     mips_opts.single_float = 1;
16299   else if (strcmp (name, "doublefloat") == 0)
16300     mips_opts.single_float = 0;
16301   else if (strcmp (name, "mips16") == 0
16302 	   || strcmp (name, "MIPS-16") == 0)
16303     {
16304       if (mips_opts.micromips == 1)
16305 	as_fatal (_("`mips16' cannot be used with `micromips'"));
16306       mips_opts.mips16 = 1;
16307     }
16308   else if (strcmp (name, "nomips16") == 0
16309 	   || strcmp (name, "noMIPS-16") == 0)
16310     mips_opts.mips16 = 0;
16311   else if (strcmp (name, "micromips") == 0)
16312     {
16313       if (mips_opts.mips16 == 1)
16314 	as_fatal (_("`micromips' cannot be used with `mips16'"));
16315       mips_opts.micromips = 1;
16316     }
16317   else if (strcmp (name, "nomicromips") == 0)
16318     mips_opts.micromips = 0;
16319   else if (strcmp (name, "smartmips") == 0)
16320     {
16321       if (!ISA_SUPPORTS_SMARTMIPS)
16322 	as_warn (_("%s ISA does not support SmartMIPS ASE"),
16323 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16324       mips_opts.ase_smartmips = 1;
16325     }
16326   else if (strcmp (name, "nosmartmips") == 0)
16327     mips_opts.ase_smartmips = 0;
16328   else if (strcmp (name, "mips3d") == 0)
16329     mips_opts.ase_mips3d = 1;
16330   else if (strcmp (name, "nomips3d") == 0)
16331     mips_opts.ase_mips3d = 0;
16332   else if (strcmp (name, "mdmx") == 0)
16333     mips_opts.ase_mdmx = 1;
16334   else if (strcmp (name, "nomdmx") == 0)
16335     mips_opts.ase_mdmx = 0;
16336   else if (strcmp (name, "dsp") == 0)
16337     {
16338       if (!ISA_SUPPORTS_DSP_ASE)
16339 	as_warn (_("%s ISA does not support DSP ASE"),
16340 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16341       mips_opts.ase_dsp = 1;
16342       mips_opts.ase_dspr2 = 0;
16343     }
16344   else if (strcmp (name, "nodsp") == 0)
16345     {
16346       mips_opts.ase_dsp = 0;
16347       mips_opts.ase_dspr2 = 0;
16348     }
16349   else if (strcmp (name, "dspr2") == 0)
16350     {
16351       if (!ISA_SUPPORTS_DSPR2_ASE)
16352 	as_warn (_("%s ISA does not support DSP R2 ASE"),
16353 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16354       mips_opts.ase_dspr2 = 1;
16355       mips_opts.ase_dsp = 1;
16356     }
16357   else if (strcmp (name, "nodspr2") == 0)
16358     {
16359       mips_opts.ase_dspr2 = 0;
16360       mips_opts.ase_dsp = 0;
16361     }
16362   else if (strcmp (name, "mt") == 0)
16363     {
16364       if (!ISA_SUPPORTS_MT_ASE)
16365 	as_warn (_("%s ISA does not support MT ASE"),
16366 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16367       mips_opts.ase_mt = 1;
16368     }
16369   else if (strcmp (name, "nomt") == 0)
16370     mips_opts.ase_mt = 0;
16371   else if (strcmp (name, "mcu") == 0)
16372     mips_opts.ase_mcu = 1;
16373   else if (strcmp (name, "nomcu") == 0)
16374     mips_opts.ase_mcu = 0;
16375   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16376     {
16377       int reset = 0;
16378 
16379       /* Permit the user to change the ISA and architecture on the fly.
16380 	 Needless to say, misuse can cause serious problems.  */
16381       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16382 	{
16383 	  reset = 1;
16384 	  mips_opts.isa = file_mips_isa;
16385 	  mips_opts.arch = file_mips_arch;
16386 	}
16387       else if (strncmp (name, "arch=", 5) == 0)
16388 	{
16389 	  const struct mips_cpu_info *p;
16390 
16391 	  p = mips_parse_cpu("internal use", name + 5);
16392 	  if (!p)
16393 	    as_bad (_("unknown architecture %s"), name + 5);
16394 	  else
16395 	    {
16396 	      mips_opts.arch = p->cpu;
16397 	      mips_opts.isa = p->isa;
16398 	    }
16399 	}
16400       else if (strncmp (name, "mips", 4) == 0)
16401 	{
16402 	  const struct mips_cpu_info *p;
16403 
16404 	  p = mips_parse_cpu("internal use", name);
16405 	  if (!p)
16406 	    as_bad (_("unknown ISA level %s"), name + 4);
16407 	  else
16408 	    {
16409 	      mips_opts.arch = p->cpu;
16410 	      mips_opts.isa = p->isa;
16411 	    }
16412 	}
16413       else
16414 	as_bad (_("unknown ISA or architecture %s"), name);
16415 
16416       switch (mips_opts.isa)
16417 	{
16418 	case  0:
16419 	  break;
16420 	case ISA_MIPS1:
16421 	case ISA_MIPS2:
16422 	case ISA_MIPS32:
16423 	case ISA_MIPS32R2:
16424 	  mips_opts.gp32 = 1;
16425 	  mips_opts.fp32 = 1;
16426 	  break;
16427 	case ISA_MIPS3:
16428 	case ISA_MIPS4:
16429 	case ISA_MIPS5:
16430 	case ISA_MIPS64:
16431 	case ISA_MIPS64R2:
16432 	  mips_opts.gp32 = 0;
16433 	  mips_opts.fp32 = 0;
16434 	  break;
16435 	default:
16436 	  as_bad (_("unknown ISA level %s"), name + 4);
16437 	  break;
16438 	}
16439       if (reset)
16440 	{
16441 	  mips_opts.gp32 = file_mips_gp32;
16442 	  mips_opts.fp32 = file_mips_fp32;
16443 	}
16444     }
16445   else if (strcmp (name, "autoextend") == 0)
16446     mips_opts.noautoextend = 0;
16447   else if (strcmp (name, "noautoextend") == 0)
16448     mips_opts.noautoextend = 1;
16449   else if (strcmp (name, "push") == 0)
16450     {
16451       struct mips_option_stack *s;
16452 
16453       s = (struct mips_option_stack *) xmalloc (sizeof *s);
16454       s->next = mips_opts_stack;
16455       s->options = mips_opts;
16456       mips_opts_stack = s;
16457     }
16458   else if (strcmp (name, "pop") == 0)
16459     {
16460       struct mips_option_stack *s;
16461 
16462       s = mips_opts_stack;
16463       if (s == NULL)
16464 	as_bad (_(".set pop with no .set push"));
16465       else
16466 	{
16467 	  /* If we're changing the reorder mode we need to handle
16468              delay slots correctly.  */
16469 	  if (s->options.noreorder && ! mips_opts.noreorder)
16470 	    start_noreorder ();
16471 	  else if (! s->options.noreorder && mips_opts.noreorder)
16472 	    end_noreorder ();
16473 
16474 	  mips_opts = s->options;
16475 	  mips_opts_stack = s->next;
16476 	  free (s);
16477 	}
16478     }
16479   else if (strcmp (name, "sym32") == 0)
16480     mips_opts.sym32 = TRUE;
16481   else if (strcmp (name, "nosym32") == 0)
16482     mips_opts.sym32 = FALSE;
16483   else if (strchr (name, ','))
16484     {
16485       /* Generic ".set" directive; use the generic handler.  */
16486       *input_line_pointer = ch;
16487       input_line_pointer = name;
16488       s_set (0);
16489       return;
16490     }
16491   else
16492     {
16493       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16494     }
16495   *input_line_pointer = ch;
16496   demand_empty_rest_of_line ();
16497 }
16498 
16499 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16500    .option pic2.  It means to generate SVR4 PIC calls.  */
16501 
16502 static void
16503 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16504 {
16505   mips_pic = SVR4_PIC;
16506   mips_abicalls = TRUE;
16507 
16508   if (g_switch_seen && g_switch_value != 0)
16509     as_warn (_("-G may not be used with SVR4 PIC code"));
16510   g_switch_value = 0;
16511 
16512   bfd_set_gp_size (stdoutput, 0);
16513   demand_empty_rest_of_line ();
16514 }
16515 
16516 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16517    PIC code.  It sets the $gp register for the function based on the
16518    function address, which is in the register named in the argument.
16519    This uses a relocation against _gp_disp, which is handled specially
16520    by the linker.  The result is:
16521 	lui	$gp,%hi(_gp_disp)
16522 	addiu	$gp,$gp,%lo(_gp_disp)
16523 	addu	$gp,$gp,.cpload argument
16524    The .cpload argument is normally $25 == $t9.
16525 
16526    The -mno-shared option changes this to:
16527 	lui	$gp,%hi(__gnu_local_gp)
16528 	addiu	$gp,$gp,%lo(__gnu_local_gp)
16529    and the argument is ignored.  This saves an instruction, but the
16530    resulting code is not position independent; it uses an absolute
16531    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16532    can go into an ordinary executable, but not into a shared library.  */
16533 
16534 static void
16535 s_cpload (int ignore ATTRIBUTE_UNUSED)
16536 {
16537   expressionS ex;
16538   int reg;
16539   int in_shared;
16540 
16541   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16542      .cpload is ignored.  */
16543   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16544     {
16545       s_ignore (0);
16546       return;
16547     }
16548 
16549   if (mips_opts.mips16)
16550     {
16551       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16552       ignore_rest_of_line ();
16553       return;
16554     }
16555 
16556   /* .cpload should be in a .set noreorder section.  */
16557   if (mips_opts.noreorder == 0)
16558     as_warn (_(".cpload not in noreorder section"));
16559 
16560   reg = tc_get_register (0);
16561 
16562   /* If we need to produce a 64-bit address, we are better off using
16563      the default instruction sequence.  */
16564   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16565 
16566   ex.X_op = O_symbol;
16567   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16568                                          "__gnu_local_gp");
16569   ex.X_op_symbol = NULL;
16570   ex.X_add_number = 0;
16571 
16572   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16573   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16574 
16575   macro_start ();
16576   macro_build_lui (&ex, mips_gp_register);
16577   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16578 	       mips_gp_register, BFD_RELOC_LO16);
16579   if (in_shared)
16580     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16581 		 mips_gp_register, reg);
16582   macro_end ();
16583 
16584   demand_empty_rest_of_line ();
16585 }
16586 
16587 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16588      .cpsetup $reg1, offset|$reg2, label
16589 
16590    If offset is given, this results in:
16591      sd		$gp, offset($sp)
16592      lui	$gp, %hi(%neg(%gp_rel(label)))
16593      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16594      daddu	$gp, $gp, $reg1
16595 
16596    If $reg2 is given, this results in:
16597      daddu	$reg2, $gp, $0
16598      lui	$gp, %hi(%neg(%gp_rel(label)))
16599      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16600      daddu	$gp, $gp, $reg1
16601    $reg1 is normally $25 == $t9.
16602 
16603    The -mno-shared option replaces the last three instructions with
16604 	lui	$gp,%hi(_gp)
16605 	addiu	$gp,$gp,%lo(_gp)  */
16606 
16607 static void
16608 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16609 {
16610   expressionS ex_off;
16611   expressionS ex_sym;
16612   int reg1;
16613 
16614   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16615      We also need NewABI support.  */
16616   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16617     {
16618       s_ignore (0);
16619       return;
16620     }
16621 
16622   if (mips_opts.mips16)
16623     {
16624       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16625       ignore_rest_of_line ();
16626       return;
16627     }
16628 
16629   reg1 = tc_get_register (0);
16630   SKIP_WHITESPACE ();
16631   if (*input_line_pointer != ',')
16632     {
16633       as_bad (_("missing argument separator ',' for .cpsetup"));
16634       return;
16635     }
16636   else
16637     ++input_line_pointer;
16638   SKIP_WHITESPACE ();
16639   if (*input_line_pointer == '$')
16640     {
16641       mips_cpreturn_register = tc_get_register (0);
16642       mips_cpreturn_offset = -1;
16643     }
16644   else
16645     {
16646       mips_cpreturn_offset = get_absolute_expression ();
16647       mips_cpreturn_register = -1;
16648     }
16649   SKIP_WHITESPACE ();
16650   if (*input_line_pointer != ',')
16651     {
16652       as_bad (_("missing argument separator ',' for .cpsetup"));
16653       return;
16654     }
16655   else
16656     ++input_line_pointer;
16657   SKIP_WHITESPACE ();
16658   expression (&ex_sym);
16659 
16660   macro_start ();
16661   if (mips_cpreturn_register == -1)
16662     {
16663       ex_off.X_op = O_constant;
16664       ex_off.X_add_symbol = NULL;
16665       ex_off.X_op_symbol = NULL;
16666       ex_off.X_add_number = mips_cpreturn_offset;
16667 
16668       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16669 		   BFD_RELOC_LO16, SP);
16670     }
16671   else
16672     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16673 		 mips_gp_register, 0);
16674 
16675   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16676     {
16677       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16678 		   -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16679 		   BFD_RELOC_HI16_S);
16680 
16681       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16682 		   mips_gp_register, -1, BFD_RELOC_GPREL16,
16683 		   BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16684 
16685       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16686 		   mips_gp_register, reg1);
16687     }
16688   else
16689     {
16690       expressionS ex;
16691 
16692       ex.X_op = O_symbol;
16693       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16694       ex.X_op_symbol = NULL;
16695       ex.X_add_number = 0;
16696 
16697       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16698       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16699 
16700       macro_build_lui (&ex, mips_gp_register);
16701       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16702 		   mips_gp_register, BFD_RELOC_LO16);
16703     }
16704 
16705   macro_end ();
16706 
16707   demand_empty_rest_of_line ();
16708 }
16709 
16710 static void
16711 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16712 {
16713   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16714      .cplocal is ignored.  */
16715   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16716     {
16717       s_ignore (0);
16718       return;
16719     }
16720 
16721   if (mips_opts.mips16)
16722     {
16723       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16724       ignore_rest_of_line ();
16725       return;
16726     }
16727 
16728   mips_gp_register = tc_get_register (0);
16729   demand_empty_rest_of_line ();
16730 }
16731 
16732 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16733    offset from $sp.  The offset is remembered, and after making a PIC
16734    call $gp is restored from that location.  */
16735 
16736 static void
16737 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16738 {
16739   expressionS ex;
16740 
16741   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16742      .cprestore is ignored.  */
16743   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16744     {
16745       s_ignore (0);
16746       return;
16747     }
16748 
16749   if (mips_opts.mips16)
16750     {
16751       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16752       ignore_rest_of_line ();
16753       return;
16754     }
16755 
16756   mips_cprestore_offset = get_absolute_expression ();
16757   mips_cprestore_valid = 1;
16758 
16759   ex.X_op = O_constant;
16760   ex.X_add_symbol = NULL;
16761   ex.X_op_symbol = NULL;
16762   ex.X_add_number = mips_cprestore_offset;
16763 
16764   macro_start ();
16765   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16766 				SP, HAVE_64BIT_ADDRESSES);
16767   macro_end ();
16768 
16769   demand_empty_rest_of_line ();
16770 }
16771 
16772 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16773    was given in the preceding .cpsetup, it results in:
16774      ld		$gp, offset($sp)
16775 
16776    If a register $reg2 was given there, it results in:
16777      daddu	$gp, $reg2, $0  */
16778 
16779 static void
16780 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16781 {
16782   expressionS ex;
16783 
16784   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16785      We also need NewABI support.  */
16786   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16787     {
16788       s_ignore (0);
16789       return;
16790     }
16791 
16792   if (mips_opts.mips16)
16793     {
16794       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16795       ignore_rest_of_line ();
16796       return;
16797     }
16798 
16799   macro_start ();
16800   if (mips_cpreturn_register == -1)
16801     {
16802       ex.X_op = O_constant;
16803       ex.X_add_symbol = NULL;
16804       ex.X_op_symbol = NULL;
16805       ex.X_add_number = mips_cpreturn_offset;
16806 
16807       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16808     }
16809   else
16810     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16811 		 mips_cpreturn_register, 0);
16812   macro_end ();
16813 
16814   demand_empty_rest_of_line ();
16815 }
16816 
16817 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16818    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16819    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16820    debug information or MIPS16 TLS.  */
16821 
16822 static void
16823 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16824 		     bfd_reloc_code_real_type rtype)
16825 {
16826   expressionS ex;
16827   char *p;
16828 
16829   expression (&ex);
16830 
16831   if (ex.X_op != O_symbol)
16832     {
16833       as_bad (_("Unsupported use of %s"), dirstr);
16834       ignore_rest_of_line ();
16835     }
16836 
16837   p = frag_more (bytes);
16838   md_number_to_chars (p, 0, bytes);
16839   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16840   demand_empty_rest_of_line ();
16841   mips_clear_insn_labels ();
16842 }
16843 
16844 /* Handle .dtprelword.  */
16845 
16846 static void
16847 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16848 {
16849   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16850 }
16851 
16852 /* Handle .dtpreldword.  */
16853 
16854 static void
16855 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16856 {
16857   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16858 }
16859 
16860 /* Handle .tprelword.  */
16861 
16862 static void
16863 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16864 {
16865   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16866 }
16867 
16868 /* Handle .tpreldword.  */
16869 
16870 static void
16871 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16872 {
16873   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16874 }
16875 
16876 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
16877    code.  It sets the offset to use in gp_rel relocations.  */
16878 
16879 static void
16880 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16881 {
16882   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16883      We also need NewABI support.  */
16884   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16885     {
16886       s_ignore (0);
16887       return;
16888     }
16889 
16890   mips_gprel_offset = get_absolute_expression ();
16891 
16892   demand_empty_rest_of_line ();
16893 }
16894 
16895 /* Handle the .gpword pseudo-op.  This is used when generating PIC
16896    code.  It generates a 32 bit GP relative reloc.  */
16897 
16898 static void
16899 s_gpword (int ignore ATTRIBUTE_UNUSED)
16900 {
16901   segment_info_type *si;
16902   struct insn_label_list *l;
16903   expressionS ex;
16904   char *p;
16905 
16906   /* When not generating PIC code, this is treated as .word.  */
16907   if (mips_pic != SVR4_PIC)
16908     {
16909       s_cons (2);
16910       return;
16911     }
16912 
16913   si = seg_info (now_seg);
16914   l = si->label_list;
16915   mips_emit_delays ();
16916   if (auto_align)
16917     mips_align (2, 0, l);
16918 
16919   expression (&ex);
16920   mips_clear_insn_labels ();
16921 
16922   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16923     {
16924       as_bad (_("Unsupported use of .gpword"));
16925       ignore_rest_of_line ();
16926     }
16927 
16928   p = frag_more (4);
16929   md_number_to_chars (p, 0, 4);
16930   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16931 	       BFD_RELOC_GPREL32);
16932 
16933   demand_empty_rest_of_line ();
16934 }
16935 
16936 static void
16937 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16938 {
16939   segment_info_type *si;
16940   struct insn_label_list *l;
16941   expressionS ex;
16942   char *p;
16943 
16944   /* When not generating PIC code, this is treated as .dword.  */
16945   if (mips_pic != SVR4_PIC)
16946     {
16947       s_cons (3);
16948       return;
16949     }
16950 
16951   si = seg_info (now_seg);
16952   l = si->label_list;
16953   mips_emit_delays ();
16954   if (auto_align)
16955     mips_align (3, 0, l);
16956 
16957   expression (&ex);
16958   mips_clear_insn_labels ();
16959 
16960   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16961     {
16962       as_bad (_("Unsupported use of .gpdword"));
16963       ignore_rest_of_line ();
16964     }
16965 
16966   p = frag_more (8);
16967   md_number_to_chars (p, 0, 8);
16968   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16969 	       BFD_RELOC_GPREL32)->fx_tcbit = 1;
16970 
16971   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
16972   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16973 	   FALSE, BFD_RELOC_64)->fx_tcbit = 1;
16974 
16975   demand_empty_rest_of_line ();
16976 }
16977 
16978 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
16979    tables in SVR4 PIC code.  */
16980 
16981 static void
16982 s_cpadd (int ignore ATTRIBUTE_UNUSED)
16983 {
16984   int reg;
16985 
16986   /* This is ignored when not generating SVR4 PIC code.  */
16987   if (mips_pic != SVR4_PIC)
16988     {
16989       s_ignore (0);
16990       return;
16991     }
16992 
16993   /* Add $gp to the register named as an argument.  */
16994   macro_start ();
16995   reg = tc_get_register (0);
16996   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
16997   macro_end ();
16998 
16999   demand_empty_rest_of_line ();
17000 }
17001 
17002 /* Handle the .insn pseudo-op.  This marks instruction labels in
17003    mips16/micromips mode.  This permits the linker to handle them specially,
17004    such as generating jalx instructions when needed.  We also make
17005    them odd for the duration of the assembly, in order to generate the
17006    right sort of code.  We will make them even in the adjust_symtab
17007    routine, while leaving them marked.  This is convenient for the
17008    debugger and the disassembler.  The linker knows to make them odd
17009    again.  */
17010 
17011 static void
17012 s_insn (int ignore ATTRIBUTE_UNUSED)
17013 {
17014   mips_mark_labels ();
17015 
17016   demand_empty_rest_of_line ();
17017 }
17018 
17019 /* Handle a .stabn directive.  We need these in order to mark a label
17020    as being a mips16 text label correctly.  Sometimes the compiler
17021    will emit a label, followed by a .stabn, and then switch sections.
17022    If the label and .stabn are in mips16 mode, then the label is
17023    really a mips16 text label.  */
17024 
17025 static void
17026 s_mips_stab (int type)
17027 {
17028   if (type == 'n')
17029     mips_mark_labels ();
17030 
17031   s_stab (type);
17032 }
17033 
17034 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17035 
17036 static void
17037 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17038 {
17039   char *name;
17040   int c;
17041   symbolS *symbolP;
17042   expressionS exp;
17043 
17044   name = input_line_pointer;
17045   c = get_symbol_end ();
17046   symbolP = symbol_find_or_make (name);
17047   S_SET_WEAK (symbolP);
17048   *input_line_pointer = c;
17049 
17050   SKIP_WHITESPACE ();
17051 
17052   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17053     {
17054       if (S_IS_DEFINED (symbolP))
17055 	{
17056 	  as_bad (_("ignoring attempt to redefine symbol %s"),
17057 		  S_GET_NAME (symbolP));
17058 	  ignore_rest_of_line ();
17059 	  return;
17060 	}
17061 
17062       if (*input_line_pointer == ',')
17063 	{
17064 	  ++input_line_pointer;
17065 	  SKIP_WHITESPACE ();
17066 	}
17067 
17068       expression (&exp);
17069       if (exp.X_op != O_symbol)
17070 	{
17071 	  as_bad (_("bad .weakext directive"));
17072 	  ignore_rest_of_line ();
17073 	  return;
17074 	}
17075       symbol_set_value_expression (symbolP, &exp);
17076     }
17077 
17078   demand_empty_rest_of_line ();
17079 }
17080 
17081 /* Parse a register string into a number.  Called from the ECOFF code
17082    to parse .frame.  The argument is non-zero if this is the frame
17083    register, so that we can record it in mips_frame_reg.  */
17084 
17085 int
17086 tc_get_register (int frame)
17087 {
17088   unsigned int reg;
17089 
17090   SKIP_WHITESPACE ();
17091   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17092     reg = 0;
17093   if (frame)
17094     {
17095       mips_frame_reg = reg != 0 ? reg : SP;
17096       mips_frame_reg_valid = 1;
17097       mips_cprestore_valid = 0;
17098     }
17099   return reg;
17100 }
17101 
17102 valueT
17103 md_section_align (asection *seg, valueT addr)
17104 {
17105   int align = bfd_get_section_alignment (stdoutput, seg);
17106 
17107   if (IS_ELF)
17108     {
17109       /* We don't need to align ELF sections to the full alignment.
17110 	 However, Irix 5 may prefer that we align them at least to a 16
17111 	 byte boundary.  We don't bother to align the sections if we
17112 	 are targeted for an embedded system.  */
17113       if (strncmp (TARGET_OS, "elf", 3) == 0)
17114         return addr;
17115       if (align > 4)
17116         align = 4;
17117     }
17118 
17119   return ((addr + (1 << align) - 1) & (-1 << align));
17120 }
17121 
17122 /* Utility routine, called from above as well.  If called while the
17123    input file is still being read, it's only an approximation.  (For
17124    example, a symbol may later become defined which appeared to be
17125    undefined earlier.)  */
17126 
17127 static int
17128 nopic_need_relax (symbolS *sym, int before_relaxing)
17129 {
17130   if (sym == 0)
17131     return 0;
17132 
17133   if (g_switch_value > 0)
17134     {
17135       const char *symname;
17136       int change;
17137 
17138       /* Find out whether this symbol can be referenced off the $gp
17139 	 register.  It can be if it is smaller than the -G size or if
17140 	 it is in the .sdata or .sbss section.  Certain symbols can
17141 	 not be referenced off the $gp, although it appears as though
17142 	 they can.  */
17143       symname = S_GET_NAME (sym);
17144       if (symname != (const char *) NULL
17145 	  && (strcmp (symname, "eprol") == 0
17146 	      || strcmp (symname, "etext") == 0
17147 	      || strcmp (symname, "_gp") == 0
17148 	      || strcmp (symname, "edata") == 0
17149 	      || strcmp (symname, "_fbss") == 0
17150 	      || strcmp (symname, "_fdata") == 0
17151 	      || strcmp (symname, "_ftext") == 0
17152 	      || strcmp (symname, "end") == 0
17153 	      || strcmp (symname, "_gp_disp") == 0))
17154 	change = 1;
17155       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17156 	       && (0
17157 #ifndef NO_ECOFF_DEBUGGING
17158 		   || (symbol_get_obj (sym)->ecoff_extern_size != 0
17159 		       && (symbol_get_obj (sym)->ecoff_extern_size
17160 			   <= g_switch_value))
17161 #endif
17162 		   /* We must defer this decision until after the whole
17163 		      file has been read, since there might be a .extern
17164 		      after the first use of this symbol.  */
17165 		   || (before_relaxing
17166 #ifndef NO_ECOFF_DEBUGGING
17167 		       && symbol_get_obj (sym)->ecoff_extern_size == 0
17168 #endif
17169 		       && S_GET_VALUE (sym) == 0)
17170 		   || (S_GET_VALUE (sym) != 0
17171 		       && S_GET_VALUE (sym) <= g_switch_value)))
17172 	change = 0;
17173       else
17174 	{
17175 	  const char *segname;
17176 
17177 	  segname = segment_name (S_GET_SEGMENT (sym));
17178 	  gas_assert (strcmp (segname, ".lit8") != 0
17179 		  && strcmp (segname, ".lit4") != 0);
17180 	  change = (strcmp (segname, ".sdata") != 0
17181 		    && strcmp (segname, ".sbss") != 0
17182 		    && strncmp (segname, ".sdata.", 7) != 0
17183 		    && strncmp (segname, ".sbss.", 6) != 0
17184 		    && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17185 		    && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17186 	}
17187       return change;
17188     }
17189   else
17190     /* We are not optimizing for the $gp register.  */
17191     return 1;
17192 }
17193 
17194 
17195 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17196 
17197 static bfd_boolean
17198 pic_need_relax (symbolS *sym, asection *segtype)
17199 {
17200   asection *symsec;
17201 
17202   /* Handle the case of a symbol equated to another symbol.  */
17203   while (symbol_equated_reloc_p (sym))
17204     {
17205       symbolS *n;
17206 
17207       /* It's possible to get a loop here in a badly written program.  */
17208       n = symbol_get_value_expression (sym)->X_add_symbol;
17209       if (n == sym)
17210 	break;
17211       sym = n;
17212     }
17213 
17214   if (symbol_section_p (sym))
17215     return TRUE;
17216 
17217   symsec = S_GET_SEGMENT (sym);
17218 
17219   /* This must duplicate the test in adjust_reloc_syms.  */
17220   return (!bfd_is_und_section (symsec)
17221 	  && !bfd_is_abs_section (symsec)
17222 	  && !bfd_is_com_section (symsec)
17223 	  && !s_is_linkonce (sym, segtype)
17224 #ifdef OBJ_ELF
17225 	  /* A global or weak symbol is treated as external.  */
17226 	  && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
17227 #endif
17228 	  );
17229 }
17230 
17231 
17232 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17233    extended opcode.  SEC is the section the frag is in.  */
17234 
17235 static int
17236 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17237 {
17238   int type;
17239   const struct mips16_immed_operand *op;
17240   offsetT val;
17241   int mintiny, maxtiny;
17242   segT symsec;
17243   fragS *sym_frag;
17244 
17245   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17246     return 0;
17247   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17248     return 1;
17249 
17250   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17251   op = mips16_immed_operands;
17252   while (op->type != type)
17253     {
17254       ++op;
17255       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17256     }
17257 
17258   if (op->unsp)
17259     {
17260       if (type == '<' || type == '>' || type == '[' || type == ']')
17261 	{
17262 	  mintiny = 1;
17263 	  maxtiny = 1 << op->nbits;
17264 	}
17265       else
17266 	{
17267 	  mintiny = 0;
17268 	  maxtiny = (1 << op->nbits) - 1;
17269 	}
17270     }
17271   else
17272     {
17273       mintiny = - (1 << (op->nbits - 1));
17274       maxtiny = (1 << (op->nbits - 1)) - 1;
17275     }
17276 
17277   sym_frag = symbol_get_frag (fragp->fr_symbol);
17278   val = S_GET_VALUE (fragp->fr_symbol);
17279   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17280 
17281   if (op->pcrel)
17282     {
17283       addressT addr;
17284 
17285       /* We won't have the section when we are called from
17286          mips_relax_frag.  However, we will always have been called
17287          from md_estimate_size_before_relax first.  If this is a
17288          branch to a different section, we mark it as such.  If SEC is
17289          NULL, and the frag is not marked, then it must be a branch to
17290          the same section.  */
17291       if (sec == NULL)
17292 	{
17293 	  if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17294 	    return 1;
17295 	}
17296       else
17297 	{
17298 	  /* Must have been called from md_estimate_size_before_relax.  */
17299 	  if (symsec != sec)
17300 	    {
17301 	      fragp->fr_subtype =
17302 		RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17303 
17304 	      /* FIXME: We should support this, and let the linker
17305                  catch branches and loads that are out of range.  */
17306 	      as_bad_where (fragp->fr_file, fragp->fr_line,
17307 			    _("unsupported PC relative reference to different section"));
17308 
17309 	      return 1;
17310 	    }
17311 	  if (fragp != sym_frag && sym_frag->fr_address == 0)
17312 	    /* Assume non-extended on the first relaxation pass.
17313 	       The address we have calculated will be bogus if this is
17314 	       a forward branch to another frag, as the forward frag
17315 	       will have fr_address == 0.  */
17316 	    return 0;
17317 	}
17318 
17319       /* In this case, we know for sure that the symbol fragment is in
17320 	 the same section.  If the relax_marker of the symbol fragment
17321 	 differs from the relax_marker of this fragment, we have not
17322 	 yet adjusted the symbol fragment fr_address.  We want to add
17323 	 in STRETCH in order to get a better estimate of the address.
17324 	 This particularly matters because of the shift bits.  */
17325       if (stretch != 0
17326 	  && sym_frag->relax_marker != fragp->relax_marker)
17327 	{
17328 	  fragS *f;
17329 
17330 	  /* Adjust stretch for any alignment frag.  Note that if have
17331              been expanding the earlier code, the symbol may be
17332              defined in what appears to be an earlier frag.  FIXME:
17333              This doesn't handle the fr_subtype field, which specifies
17334              a maximum number of bytes to skip when doing an
17335              alignment.  */
17336 	  for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17337 	    {
17338 	      if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17339 		{
17340 		  if (stretch < 0)
17341 		    stretch = - ((- stretch)
17342 				 & ~ ((1 << (int) f->fr_offset) - 1));
17343 		  else
17344 		    stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17345 		  if (stretch == 0)
17346 		    break;
17347 		}
17348 	    }
17349 	  if (f != NULL)
17350 	    val += stretch;
17351 	}
17352 
17353       addr = fragp->fr_address + fragp->fr_fix;
17354 
17355       /* The base address rules are complicated.  The base address of
17356          a branch is the following instruction.  The base address of a
17357          PC relative load or add is the instruction itself, but if it
17358          is in a delay slot (in which case it can not be extended) use
17359          the address of the instruction whose delay slot it is in.  */
17360       if (type == 'p' || type == 'q')
17361 	{
17362 	  addr += 2;
17363 
17364 	  /* If we are currently assuming that this frag should be
17365 	     extended, then, the current address is two bytes
17366 	     higher.  */
17367 	  if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17368 	    addr += 2;
17369 
17370 	  /* Ignore the low bit in the target, since it will be set
17371              for a text label.  */
17372 	  if ((val & 1) != 0)
17373 	    --val;
17374 	}
17375       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17376 	addr -= 4;
17377       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17378 	addr -= 2;
17379 
17380       val -= addr & ~ ((1 << op->shift) - 1);
17381 
17382       /* Branch offsets have an implicit 0 in the lowest bit.  */
17383       if (type == 'p' || type == 'q')
17384 	val /= 2;
17385 
17386       /* If any of the shifted bits are set, we must use an extended
17387          opcode.  If the address depends on the size of this
17388          instruction, this can lead to a loop, so we arrange to always
17389          use an extended opcode.  We only check this when we are in
17390          the main relaxation loop, when SEC is NULL.  */
17391       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17392 	{
17393 	  fragp->fr_subtype =
17394 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17395 	  return 1;
17396 	}
17397 
17398       /* If we are about to mark a frag as extended because the value
17399          is precisely maxtiny + 1, then there is a chance of an
17400          infinite loop as in the following code:
17401 	     la	$4,foo
17402 	     .skip	1020
17403 	     .align	2
17404 	   foo:
17405 	 In this case when the la is extended, foo is 0x3fc bytes
17406 	 away, so the la can be shrunk, but then foo is 0x400 away, so
17407 	 the la must be extended.  To avoid this loop, we mark the
17408 	 frag as extended if it was small, and is about to become
17409 	 extended with a value of maxtiny + 1.  */
17410       if (val == ((maxtiny + 1) << op->shift)
17411 	  && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17412 	  && sec == NULL)
17413 	{
17414 	  fragp->fr_subtype =
17415 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17416 	  return 1;
17417 	}
17418     }
17419   else if (symsec != absolute_section && sec != NULL)
17420     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17421 
17422   if ((val & ((1 << op->shift) - 1)) != 0
17423       || val < (mintiny << op->shift)
17424       || val > (maxtiny << op->shift))
17425     return 1;
17426   else
17427     return 0;
17428 }
17429 
17430 /* Compute the length of a branch sequence, and adjust the
17431    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17432    worst-case length is computed, with UPDATE being used to indicate
17433    whether an unconditional (-1), branch-likely (+1) or regular (0)
17434    branch is to be computed.  */
17435 static int
17436 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17437 {
17438   bfd_boolean toofar;
17439   int length;
17440 
17441   if (fragp
17442       && S_IS_DEFINED (fragp->fr_symbol)
17443       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17444     {
17445       addressT addr;
17446       offsetT val;
17447 
17448       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17449 
17450       addr = fragp->fr_address + fragp->fr_fix + 4;
17451 
17452       val -= addr;
17453 
17454       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17455     }
17456   else if (fragp)
17457     /* If the symbol is not defined or it's in a different segment,
17458        assume the user knows what's going on and emit a short
17459        branch.  */
17460     toofar = FALSE;
17461   else
17462     toofar = TRUE;
17463 
17464   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17465     fragp->fr_subtype
17466       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17467 			     RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17468 			     RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17469 			     RELAX_BRANCH_LINK (fragp->fr_subtype),
17470 			     toofar);
17471 
17472   length = 4;
17473   if (toofar)
17474     {
17475       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17476 	length += 8;
17477 
17478       if (mips_pic != NO_PIC)
17479 	{
17480 	  /* Additional space for PIC loading of target address.  */
17481 	  length += 8;
17482 	  if (mips_opts.isa == ISA_MIPS1)
17483 	    /* Additional space for $at-stabilizing nop.  */
17484 	    length += 4;
17485 	}
17486 
17487       /* If branch is conditional.  */
17488       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17489 	length += 8;
17490     }
17491 
17492   return length;
17493 }
17494 
17495 /* Compute the length of a branch sequence, and adjust the
17496    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
17497    worst-case length is computed, with UPDATE being used to indicate
17498    whether an unconditional (-1), or regular (0) branch is to be
17499    computed.  */
17500 
17501 static int
17502 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17503 {
17504   bfd_boolean toofar;
17505   int length;
17506 
17507   if (fragp
17508       && S_IS_DEFINED (fragp->fr_symbol)
17509       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17510     {
17511       addressT addr;
17512       offsetT val;
17513 
17514       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17515       /* Ignore the low bit in the target, since it will be set
17516 	 for a text label.  */
17517       if ((val & 1) != 0)
17518 	--val;
17519 
17520       addr = fragp->fr_address + fragp->fr_fix + 4;
17521 
17522       val -= addr;
17523 
17524       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17525     }
17526   else if (fragp)
17527     /* If the symbol is not defined or it's in a different segment,
17528        assume the user knows what's going on and emit a short
17529        branch.  */
17530     toofar = FALSE;
17531   else
17532     toofar = TRUE;
17533 
17534   if (fragp && update
17535       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17536     fragp->fr_subtype = (toofar
17537 			 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17538 			 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17539 
17540   length = 4;
17541   if (toofar)
17542     {
17543       bfd_boolean compact_known = fragp != NULL;
17544       bfd_boolean compact = FALSE;
17545       bfd_boolean uncond;
17546 
17547       if (compact_known)
17548 	compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17549       if (fragp)
17550 	uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17551       else
17552 	uncond = update < 0;
17553 
17554       /* If label is out of range, we turn branch <br>:
17555 
17556 		<br>	label			# 4 bytes
17557 	    0:
17558 
17559          into:
17560 
17561 		j	label			# 4 bytes
17562 		nop				# 2 bytes if compact && !PIC
17563 	    0:
17564        */
17565       if (mips_pic == NO_PIC && (!compact_known || compact))
17566 	length += 2;
17567 
17568       /* If assembling PIC code, we further turn:
17569 
17570 			j	label			# 4 bytes
17571 
17572          into:
17573 
17574 			lw/ld	at, %got(label)(gp)	# 4 bytes
17575 			d/addiu	at, %lo(label)		# 4 bytes
17576 			jr/c	at			# 2 bytes
17577        */
17578       if (mips_pic != NO_PIC)
17579 	length += 6;
17580 
17581       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17582 
17583 			<brneg>	0f			# 4 bytes
17584 			nop				# 2 bytes if !compact
17585        */
17586       if (!uncond)
17587 	length += (compact_known && compact) ? 4 : 6;
17588     }
17589 
17590   return length;
17591 }
17592 
17593 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17594    bit accordingly.  */
17595 
17596 static int
17597 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17598 {
17599   bfd_boolean toofar;
17600 
17601   if (fragp
17602       && S_IS_DEFINED (fragp->fr_symbol)
17603       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17604     {
17605       addressT addr;
17606       offsetT val;
17607       int type;
17608 
17609       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17610       /* Ignore the low bit in the target, since it will be set
17611 	 for a text label.  */
17612       if ((val & 1) != 0)
17613 	--val;
17614 
17615       /* Assume this is a 2-byte branch.  */
17616       addr = fragp->fr_address + fragp->fr_fix + 2;
17617 
17618       /* We try to avoid the infinite loop by not adding 2 more bytes for
17619 	 long branches.  */
17620 
17621       val -= addr;
17622 
17623       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17624       if (type == 'D')
17625 	toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17626       else if (type == 'E')
17627 	toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17628       else
17629 	abort ();
17630     }
17631   else
17632     /* If the symbol is not defined or it's in a different segment,
17633        we emit a normal 32-bit branch.  */
17634     toofar = TRUE;
17635 
17636   if (fragp && update
17637       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17638     fragp->fr_subtype
17639       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17640 	       : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17641 
17642   if (toofar)
17643     return 4;
17644 
17645   return 2;
17646 }
17647 
17648 /* Estimate the size of a frag before relaxing.  Unless this is the
17649    mips16, we are not really relaxing here, and the final size is
17650    encoded in the subtype information.  For the mips16, we have to
17651    decide whether we are using an extended opcode or not.  */
17652 
17653 int
17654 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17655 {
17656   int change;
17657 
17658   if (RELAX_BRANCH_P (fragp->fr_subtype))
17659     {
17660 
17661       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17662 
17663       return fragp->fr_var;
17664     }
17665 
17666   if (RELAX_MIPS16_P (fragp->fr_subtype))
17667     /* We don't want to modify the EXTENDED bit here; it might get us
17668        into infinite loops.  We change it only in mips_relax_frag().  */
17669     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17670 
17671   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17672     {
17673       int length = 4;
17674 
17675       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17676 	length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17677       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17678 	length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17679       fragp->fr_var = length;
17680 
17681       return length;
17682     }
17683 
17684   if (mips_pic == NO_PIC)
17685     change = nopic_need_relax (fragp->fr_symbol, 0);
17686   else if (mips_pic == SVR4_PIC)
17687     change = pic_need_relax (fragp->fr_symbol, segtype);
17688   else if (mips_pic == VXWORKS_PIC)
17689     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17690     change = 0;
17691   else
17692     abort ();
17693 
17694   if (change)
17695     {
17696       fragp->fr_subtype |= RELAX_USE_SECOND;
17697       return -RELAX_FIRST (fragp->fr_subtype);
17698     }
17699   else
17700     return -RELAX_SECOND (fragp->fr_subtype);
17701 }
17702 
17703 /* This is called to see whether a reloc against a defined symbol
17704    should be converted into a reloc against a section.  */
17705 
17706 int
17707 mips_fix_adjustable (fixS *fixp)
17708 {
17709   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17710       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17711     return 0;
17712 
17713   if (fixp->fx_addsy == NULL)
17714     return 1;
17715 
17716   /* If symbol SYM is in a mergeable section, relocations of the form
17717      SYM + 0 can usually be made section-relative.  The mergeable data
17718      is then identified by the section offset rather than by the symbol.
17719 
17720      However, if we're generating REL LO16 relocations, the offset is split
17721      between the LO16 and parterning high part relocation.  The linker will
17722      need to recalculate the complete offset in order to correctly identify
17723      the merge data.
17724 
17725      The linker has traditionally not looked for the parterning high part
17726      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17727      placed anywhere.  Rather than break backwards compatibility by changing
17728      this, it seems better not to force the issue, and instead keep the
17729      original symbol.  This will work with either linker behavior.  */
17730   if ((lo16_reloc_p (fixp->fx_r_type)
17731        || reloc_needs_lo_p (fixp->fx_r_type))
17732       && HAVE_IN_PLACE_ADDENDS
17733       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17734     return 0;
17735 
17736   /* There is no place to store an in-place offset for JALR relocations.
17737      Likewise an in-range offset of PC-relative relocations may overflow
17738      the in-place relocatable field if recalculated against the start
17739      address of the symbol's containing section.  */
17740   if (HAVE_IN_PLACE_ADDENDS
17741       && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
17742     return 0;
17743 
17744 #ifdef OBJ_ELF
17745   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17746      to a floating-point stub.  The same is true for non-R_MIPS16_26
17747      relocations against MIPS16 functions; in this case, the stub becomes
17748      the function's canonical address.
17749 
17750      Floating-point stubs are stored in unique .mips16.call.* or
17751      .mips16.fn.* sections.  If a stub T for function F is in section S,
17752      the first relocation in section S must be against F; this is how the
17753      linker determines the target function.  All relocations that might
17754      resolve to T must also be against F.  We therefore have the following
17755      restrictions, which are given in an intentionally-redundant way:
17756 
17757        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17758 	  symbols.
17759 
17760        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17761 	  if that stub might be used.
17762 
17763        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17764 	  symbols.
17765 
17766        4. We cannot reduce a stub's relocations against MIPS16 symbols if
17767 	  that stub might be used.
17768 
17769      There is a further restriction:
17770 
17771        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17772 	  R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17773 	  targets with in-place addends; the relocation field cannot
17774 	  encode the low bit.
17775 
17776      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17777      against a MIPS16 symbol.  We deal with (5) by by not reducing any
17778      such relocations on REL targets.
17779 
17780      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17781      relocation against some symbol R, no relocation against R may be
17782      reduced.  (Note that this deals with (2) as well as (1) because
17783      relocations against global symbols will never be reduced on ELF
17784      targets.)  This approach is a little simpler than trying to detect
17785      stub sections, and gives the "all or nothing" per-symbol consistency
17786      that we have for MIPS16 symbols.  */
17787   if (IS_ELF
17788       && fixp->fx_subsy == NULL
17789       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17790 	  || *symbol_get_tc (fixp->fx_addsy)
17791 	  || (HAVE_IN_PLACE_ADDENDS
17792 	      && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17793 	      && jmp_reloc_p (fixp->fx_r_type))))
17794     return 0;
17795 #endif
17796 
17797   return 1;
17798 }
17799 
17800 /* Translate internal representation of relocation info to BFD target
17801    format.  */
17802 
17803 arelent **
17804 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17805 {
17806   static arelent *retval[4];
17807   arelent *reloc;
17808   bfd_reloc_code_real_type code;
17809 
17810   memset (retval, 0, sizeof(retval));
17811   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17812   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17813   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17814   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17815 
17816   if (fixp->fx_pcrel)
17817     {
17818       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17819 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17820 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17821 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
17822 
17823       /* At this point, fx_addnumber is "symbol offset - pcrel address".
17824 	 Relocations want only the symbol offset.  */
17825       reloc->addend = fixp->fx_addnumber + reloc->address;
17826       if (!IS_ELF)
17827 	{
17828 	  /* A gruesome hack which is a result of the gruesome gas
17829 	     reloc handling.  What's worse, for COFF (as opposed to
17830 	     ECOFF), we might need yet another copy of reloc->address.
17831 	     See bfd_install_relocation.  */
17832 	  reloc->addend += reloc->address;
17833 	}
17834     }
17835   else
17836     reloc->addend = fixp->fx_addnumber;
17837 
17838   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17839      entry to be used in the relocation's section offset.  */
17840   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17841     {
17842       reloc->address = reloc->addend;
17843       reloc->addend = 0;
17844     }
17845 
17846   code = fixp->fx_r_type;
17847 
17848   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17849   if (reloc->howto == NULL)
17850     {
17851       as_bad_where (fixp->fx_file, fixp->fx_line,
17852 		    _("Can not represent %s relocation in this object file format"),
17853 		    bfd_get_reloc_code_name (code));
17854       retval[0] = NULL;
17855     }
17856 
17857   return retval;
17858 }
17859 
17860 /* Relax a machine dependent frag.  This returns the amount by which
17861    the current size of the frag should change.  */
17862 
17863 int
17864 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17865 {
17866   if (RELAX_BRANCH_P (fragp->fr_subtype))
17867     {
17868       offsetT old_var = fragp->fr_var;
17869 
17870       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17871 
17872       return fragp->fr_var - old_var;
17873     }
17874 
17875   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17876     {
17877       offsetT old_var = fragp->fr_var;
17878       offsetT new_var = 4;
17879 
17880       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17881 	new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17882       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17883 	new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17884       fragp->fr_var = new_var;
17885 
17886       return new_var - old_var;
17887     }
17888 
17889   if (! RELAX_MIPS16_P (fragp->fr_subtype))
17890     return 0;
17891 
17892   if (mips16_extended_frag (fragp, NULL, stretch))
17893     {
17894       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17895 	return 0;
17896       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17897       return 2;
17898     }
17899   else
17900     {
17901       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17902 	return 0;
17903       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17904       return -2;
17905     }
17906 
17907   return 0;
17908 }
17909 
17910 /* Convert a machine dependent frag.  */
17911 
17912 void
17913 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17914 {
17915   if (RELAX_BRANCH_P (fragp->fr_subtype))
17916     {
17917       bfd_byte *buf;
17918       unsigned long insn;
17919       expressionS exp;
17920       fixS *fixp;
17921 
17922       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
17923 
17924       if (target_big_endian)
17925 	insn = bfd_getb32 (buf);
17926       else
17927 	insn = bfd_getl32 (buf);
17928 
17929       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17930 	{
17931 	  /* We generate a fixup instead of applying it right now
17932 	     because, if there are linker relaxations, we're going to
17933 	     need the relocations.  */
17934 	  exp.X_op = O_symbol;
17935 	  exp.X_add_symbol = fragp->fr_symbol;
17936 	  exp.X_add_number = fragp->fr_offset;
17937 
17938 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
17939 			      4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
17940 	  fixp->fx_file = fragp->fr_file;
17941 	  fixp->fx_line = fragp->fr_line;
17942 
17943 	  md_number_to_chars ((char *) buf, insn, 4);
17944 	  buf += 4;
17945 	}
17946       else
17947 	{
17948 	  int i;
17949 
17950 	  as_warn_where (fragp->fr_file, fragp->fr_line,
17951 			 _("Relaxed out-of-range branch into a jump"));
17952 
17953 	  if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17954 	    goto uncond;
17955 
17956 	  if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17957 	    {
17958 	      /* Reverse the branch.  */
17959 	      switch ((insn >> 28) & 0xf)
17960 		{
17961 		case 4:
17962 		  /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
17963 		     have the condition reversed by tweaking a single
17964 		     bit, and their opcodes all have 0x4???????.  */
17965 		  gas_assert ((insn & 0xf1000000) == 0x41000000);
17966 		  insn ^= 0x00010000;
17967 		  break;
17968 
17969 		case 0:
17970 		  /* bltz	0x04000000	bgez	0x04010000
17971 		     bltzal	0x04100000	bgezal	0x04110000  */
17972 		  gas_assert ((insn & 0xfc0e0000) == 0x04000000);
17973 		  insn ^= 0x00010000;
17974 		  break;
17975 
17976 		case 1:
17977 		  /* beq	0x10000000	bne	0x14000000
17978 		     blez	0x18000000	bgtz	0x1c000000  */
17979 		  insn ^= 0x04000000;
17980 		  break;
17981 
17982 		default:
17983 		  abort ();
17984 		}
17985 	    }
17986 
17987 	  if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17988 	    {
17989 	      /* Clear the and-link bit.  */
17990 	      gas_assert ((insn & 0xfc1c0000) == 0x04100000);
17991 
17992 	      /* bltzal		0x04100000	bgezal	0x04110000
17993 		 bltzall	0x04120000	bgezall	0x04130000  */
17994 	      insn &= ~0x00100000;
17995 	    }
17996 
17997 	  /* Branch over the branch (if the branch was likely) or the
17998 	     full jump (not likely case).  Compute the offset from the
17999 	     current instruction to branch to.  */
18000 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18001 	    i = 16;
18002 	  else
18003 	    {
18004 	      /* How many bytes in instructions we've already emitted?  */
18005 	      i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
18006 	      /* How many bytes in instructions from here to the end?  */
18007 	      i = fragp->fr_var - i;
18008 	    }
18009 	  /* Convert to instruction count.  */
18010 	  i >>= 2;
18011 	  /* Branch counts from the next instruction.  */
18012 	  i--;
18013 	  insn |= i;
18014 	  /* Branch over the jump.  */
18015 	  md_number_to_chars ((char *) buf, insn, 4);
18016 	  buf += 4;
18017 
18018 	  /* nop */
18019 	  md_number_to_chars ((char *) buf, 0, 4);
18020 	  buf += 4;
18021 
18022 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18023 	    {
18024 	      /* beql $0, $0, 2f */
18025 	      insn = 0x50000000;
18026 	      /* Compute the PC offset from the current instruction to
18027 		 the end of the variable frag.  */
18028 	      /* How many bytes in instructions we've already emitted?  */
18029 	      i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
18030 	      /* How many bytes in instructions from here to the end?  */
18031 	      i = fragp->fr_var - i;
18032 	      /* Convert to instruction count.  */
18033 	      i >>= 2;
18034 	      /* Don't decrement i, because we want to branch over the
18035 		 delay slot.  */
18036 
18037 	      insn |= i;
18038 	      md_number_to_chars ((char *) buf, insn, 4);
18039 	      buf += 4;
18040 
18041 	      md_number_to_chars ((char *) buf, 0, 4);
18042 	      buf += 4;
18043 	    }
18044 
18045 	uncond:
18046 	  if (mips_pic == NO_PIC)
18047 	    {
18048 	      /* j or jal.  */
18049 	      insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18050 		      ? 0x0c000000 : 0x08000000);
18051 	      exp.X_op = O_symbol;
18052 	      exp.X_add_symbol = fragp->fr_symbol;
18053 	      exp.X_add_number = fragp->fr_offset;
18054 
18055 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
18056 				  4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
18057 	      fixp->fx_file = fragp->fr_file;
18058 	      fixp->fx_line = fragp->fr_line;
18059 
18060 	      md_number_to_chars ((char *) buf, insn, 4);
18061 	      buf += 4;
18062 	    }
18063 	  else
18064 	    {
18065 	      unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18066 
18067 	      /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18068 	      insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18069 	      insn |= at << OP_SH_RT;
18070 	      exp.X_op = O_symbol;
18071 	      exp.X_add_symbol = fragp->fr_symbol;
18072 	      exp.X_add_number = fragp->fr_offset;
18073 
18074 	      if (fragp->fr_offset)
18075 		{
18076 		  exp.X_add_symbol = make_expr_symbol (&exp);
18077 		  exp.X_add_number = 0;
18078 		}
18079 
18080 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
18081 				  4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
18082 	      fixp->fx_file = fragp->fr_file;
18083 	      fixp->fx_line = fragp->fr_line;
18084 
18085 	      md_number_to_chars ((char *) buf, insn, 4);
18086 	      buf += 4;
18087 
18088 	      if (mips_opts.isa == ISA_MIPS1)
18089 		{
18090 		  /* nop */
18091 		  md_number_to_chars ((char *) buf, 0, 4);
18092 		  buf += 4;
18093 		}
18094 
18095 	      /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18096 	      insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18097 	      insn |= at << OP_SH_RS | at << OP_SH_RT;
18098 
18099 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
18100 				  4, &exp, FALSE, BFD_RELOC_LO16);
18101 	      fixp->fx_file = fragp->fr_file;
18102 	      fixp->fx_line = fragp->fr_line;
18103 
18104 	      md_number_to_chars ((char *) buf, insn, 4);
18105 	      buf += 4;
18106 
18107 	      /* j(al)r $at.  */
18108 	      if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18109 		insn = 0x0000f809;
18110 	      else
18111 		insn = 0x00000008;
18112 	      insn |= at << OP_SH_RS;
18113 
18114 	      md_number_to_chars ((char *) buf, insn, 4);
18115 	      buf += 4;
18116 	    }
18117 	}
18118 
18119       gas_assert (buf == (bfd_byte *)fragp->fr_literal
18120 	      + fragp->fr_fix + fragp->fr_var);
18121 
18122       fragp->fr_fix += fragp->fr_var;
18123 
18124       return;
18125     }
18126 
18127   /* Relax microMIPS branches.  */
18128   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18129     {
18130       bfd_byte *buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
18131       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18132       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18133       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18134       bfd_boolean short_ds;
18135       unsigned long insn;
18136       expressionS exp;
18137       fixS *fixp;
18138 
18139       exp.X_op = O_symbol;
18140       exp.X_add_symbol = fragp->fr_symbol;
18141       exp.X_add_number = fragp->fr_offset;
18142 
18143       fragp->fr_fix += fragp->fr_var;
18144 
18145       /* Handle 16-bit branches that fit or are forced to fit.  */
18146       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18147 	{
18148 	  /* We generate a fixup instead of applying it right now,
18149 	     because if there is linker relaxation, we're going to
18150 	     need the relocations.  */
18151 	  if (type == 'D')
18152 	    fixp = fix_new_exp (fragp,
18153 				buf - (bfd_byte *) fragp->fr_literal,
18154 				2, &exp, TRUE,
18155 				BFD_RELOC_MICROMIPS_10_PCREL_S1);
18156 	  else if (type == 'E')
18157 	    fixp = fix_new_exp (fragp,
18158 				buf - (bfd_byte *) fragp->fr_literal,
18159 				2, &exp, TRUE,
18160 				BFD_RELOC_MICROMIPS_7_PCREL_S1);
18161 	  else
18162 	    abort ();
18163 
18164 	  fixp->fx_file = fragp->fr_file;
18165 	  fixp->fx_line = fragp->fr_line;
18166 
18167 	  /* These relocations can have an addend that won't fit in
18168 	     2 octets.  */
18169 	  fixp->fx_no_overflow = 1;
18170 
18171 	  return;
18172 	}
18173 
18174       /* Handle 32-bit branches that fit or are forced to fit.  */
18175       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18176 	  || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18177 	{
18178 	  /* We generate a fixup instead of applying it right now,
18179 	     because if there is linker relaxation, we're going to
18180 	     need the relocations.  */
18181 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18182 			      4, &exp, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18183 	  fixp->fx_file = fragp->fr_file;
18184 	  fixp->fx_line = fragp->fr_line;
18185 
18186 	  if (type == 0)
18187 	    return;
18188 	}
18189 
18190       /* Relax 16-bit branches to 32-bit branches.  */
18191       if (type != 0)
18192 	{
18193 	  if (target_big_endian)
18194 	    insn = bfd_getb16 (buf);
18195 	  else
18196 	    insn = bfd_getl16 (buf);
18197 
18198 	  if ((insn & 0xfc00) == 0xcc00)		/* b16  */
18199 	    insn = 0x94000000;				/* beq  */
18200 	  else if ((insn & 0xdc00) == 0x8c00)		/* beqz16/bnez16  */
18201 	    {
18202 	      unsigned long regno;
18203 
18204 	      regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18205 	      regno = micromips_to_32_reg_d_map [regno];
18206 	      insn = ((insn & 0x2000) << 16) | 0x94000000;	/* beq/bne  */
18207 	      insn |= regno << MICROMIPSOP_SH_RS;
18208 	    }
18209 	  else
18210 	    abort ();
18211 
18212 	  /* Nothing else to do, just write it out.  */
18213 	  if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18214 	      || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18215 	    {
18216 	      md_number_to_chars ((char *) buf, insn >> 16, 2);
18217 	      buf += 2;
18218 	      md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18219 	      buf += 2;
18220 
18221 	      gas_assert (buf == ((bfd_byte *) fragp->fr_literal
18222 				  + fragp->fr_fix));
18223 	      return;
18224 	    }
18225 	}
18226       else
18227 	{
18228 	  unsigned long next;
18229 
18230 	  if (target_big_endian)
18231 	    {
18232 	      insn = bfd_getb16 (buf);
18233 	      next = bfd_getb16 (buf + 2);
18234 	    }
18235 	  else
18236 	    {
18237 	      insn = bfd_getl16 (buf);
18238 	      next = bfd_getl16 (buf + 2);
18239 	    }
18240 	  insn = (insn << 16) | next;
18241 	}
18242 
18243       /* Relax 32-bit branches to a sequence of instructions.  */
18244       as_warn_where (fragp->fr_file, fragp->fr_line,
18245 		     _("Relaxed out-of-range branch into a jump"));
18246 
18247       /* Set the short-delay-slot bit.  */
18248       short_ds = al && (insn & 0x02000000) != 0;
18249 
18250       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18251 	{
18252 	  symbolS *l;
18253 
18254 	  /* Reverse the branch.  */
18255 	  if ((insn & 0xfc000000) == 0x94000000			/* beq  */
18256 	      || (insn & 0xfc000000) == 0xb4000000)		/* bne  */
18257 	    insn ^= 0x20000000;
18258 	  else if ((insn & 0xffe00000) == 0x40000000		/* bltz  */
18259 		   || (insn & 0xffe00000) == 0x40400000		/* bgez  */
18260 		   || (insn & 0xffe00000) == 0x40800000		/* blez  */
18261 		   || (insn & 0xffe00000) == 0x40c00000		/* bgtz  */
18262 		   || (insn & 0xffe00000) == 0x40a00000		/* bnezc  */
18263 		   || (insn & 0xffe00000) == 0x40e00000		/* beqzc  */
18264 		   || (insn & 0xffe00000) == 0x40200000		/* bltzal  */
18265 		   || (insn & 0xffe00000) == 0x40600000		/* bgezal  */
18266 		   || (insn & 0xffe00000) == 0x42200000		/* bltzals  */
18267 		   || (insn & 0xffe00000) == 0x42600000)	/* bgezals  */
18268 	    insn ^= 0x00400000;
18269 	  else if ((insn & 0xffe30000) == 0x43800000		/* bc1f  */
18270 		   || (insn & 0xffe30000) == 0x43a00000		/* bc1t  */
18271 		   || (insn & 0xffe30000) == 0x42800000		/* bc2f  */
18272 		   || (insn & 0xffe30000) == 0x42a00000)	/* bc2t  */
18273 	    insn ^= 0x00200000;
18274 	  else
18275 	    abort ();
18276 
18277 	  if (al)
18278 	    {
18279 	      /* Clear the and-link and short-delay-slot bits.  */
18280 	      gas_assert ((insn & 0xfda00000) == 0x40200000);
18281 
18282 	      /* bltzal  0x40200000	bgezal  0x40600000  */
18283 	      /* bltzals 0x42200000	bgezals 0x42600000  */
18284 	      insn &= ~0x02200000;
18285 	    }
18286 
18287 	  /* Make a label at the end for use with the branch.  */
18288 	  l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18289 	  micromips_label_inc ();
18290 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18291 	  if (IS_ELF)
18292 	    S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18293 #endif
18294 
18295 	  /* Refer to it.  */
18296 	  fixp = fix_new (fragp, buf - (bfd_byte *) fragp->fr_literal,
18297 			  4, l, 0, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18298 	  fixp->fx_file = fragp->fr_file;
18299 	  fixp->fx_line = fragp->fr_line;
18300 
18301 	  /* Branch over the jump.  */
18302 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18303 	  buf += 2;
18304 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18305 	  buf += 2;
18306 
18307 	  if (!compact)
18308 	    {
18309 	      /* nop  */
18310 	      insn = 0x0c00;
18311 	      md_number_to_chars ((char *) buf, insn, 2);
18312 	      buf += 2;
18313 	    }
18314 	}
18315 
18316       if (mips_pic == NO_PIC)
18317 	{
18318 	  unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
18319 
18320 	  /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18321 	  insn = al ? jal : 0xd4000000;
18322 
18323 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18324 			      4, &exp, FALSE, BFD_RELOC_MICROMIPS_JMP);
18325 	  fixp->fx_file = fragp->fr_file;
18326 	  fixp->fx_line = fragp->fr_line;
18327 
18328 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18329 	  buf += 2;
18330 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18331 	  buf += 2;
18332 
18333 	  if (compact)
18334 	    {
18335 	      /* nop  */
18336 	      insn = 0x0c00;
18337 	      md_number_to_chars ((char *) buf, insn, 2);
18338 	      buf += 2;
18339 	    }
18340 	}
18341       else
18342 	{
18343 	  unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18344 	  unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;	/* jalr/s  */
18345 	  unsigned long jr = compact ? 0x45a0 : 0x4580;		/* jr/c  */
18346 
18347 	  /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18348 	  insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18349 	  insn |= at << MICROMIPSOP_SH_RT;
18350 
18351 	  if (exp.X_add_number)
18352 	    {
18353 	      exp.X_add_symbol = make_expr_symbol (&exp);
18354 	      exp.X_add_number = 0;
18355 	    }
18356 
18357 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18358 			      4, &exp, FALSE, BFD_RELOC_MICROMIPS_GOT16);
18359 	  fixp->fx_file = fragp->fr_file;
18360 	  fixp->fx_line = fragp->fr_line;
18361 
18362 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18363 	  buf += 2;
18364 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18365 	  buf += 2;
18366 
18367 	  /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18368 	  insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18369 	  insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18370 
18371 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18372 			      4, &exp, FALSE, BFD_RELOC_MICROMIPS_LO16);
18373 	  fixp->fx_file = fragp->fr_file;
18374 	  fixp->fx_line = fragp->fr_line;
18375 
18376 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18377 	  buf += 2;
18378 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18379 	  buf += 2;
18380 
18381 	  /* jr/jrc/jalr/jalrs $at  */
18382 	  insn = al ? jalr : jr;
18383 	  insn |= at << MICROMIPSOP_SH_MJ;
18384 
18385 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18386 	  buf += 2;
18387 	}
18388 
18389       gas_assert (buf == (bfd_byte *) fragp->fr_literal + fragp->fr_fix);
18390       return;
18391     }
18392 
18393   if (RELAX_MIPS16_P (fragp->fr_subtype))
18394     {
18395       int type;
18396       const struct mips16_immed_operand *op;
18397       bfd_boolean small, ext;
18398       offsetT val;
18399       bfd_byte *buf;
18400       unsigned long insn;
18401       bfd_boolean use_extend;
18402       unsigned short extend;
18403 
18404       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18405       op = mips16_immed_operands;
18406       while (op->type != type)
18407 	++op;
18408 
18409       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18410 	{
18411 	  small = FALSE;
18412 	  ext = TRUE;
18413 	}
18414       else
18415 	{
18416 	  small = TRUE;
18417 	  ext = FALSE;
18418 	}
18419 
18420       val = resolve_symbol_value (fragp->fr_symbol);
18421       if (op->pcrel)
18422 	{
18423 	  addressT addr;
18424 
18425 	  addr = fragp->fr_address + fragp->fr_fix;
18426 
18427 	  /* The rules for the base address of a PC relative reloc are
18428              complicated; see mips16_extended_frag.  */
18429 	  if (type == 'p' || type == 'q')
18430 	    {
18431 	      addr += 2;
18432 	      if (ext)
18433 		addr += 2;
18434 	      /* Ignore the low bit in the target, since it will be
18435                  set for a text label.  */
18436 	      if ((val & 1) != 0)
18437 		--val;
18438 	    }
18439 	  else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18440 	    addr -= 4;
18441 	  else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18442 	    addr -= 2;
18443 
18444 	  addr &= ~ (addressT) ((1 << op->shift) - 1);
18445 	  val -= addr;
18446 
18447 	  /* Make sure the section winds up with the alignment we have
18448              assumed.  */
18449 	  if (op->shift > 0)
18450 	    record_alignment (asec, op->shift);
18451 	}
18452 
18453       if (ext
18454 	  && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18455 	      || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18456 	as_warn_where (fragp->fr_file, fragp->fr_line,
18457 		       _("extended instruction in delay slot"));
18458 
18459       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
18460 
18461       if (target_big_endian)
18462 	insn = bfd_getb16 (buf);
18463       else
18464 	insn = bfd_getl16 (buf);
18465 
18466       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
18467 		    RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
18468 		    small, ext, &insn, &use_extend, &extend);
18469 
18470       if (use_extend)
18471 	{
18472 	  md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
18473 	  fragp->fr_fix += 2;
18474 	  buf += 2;
18475 	}
18476 
18477       md_number_to_chars ((char *) buf, insn, 2);
18478       fragp->fr_fix += 2;
18479       buf += 2;
18480     }
18481   else
18482     {
18483       relax_substateT subtype = fragp->fr_subtype;
18484       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18485       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18486       int first, second;
18487       fixS *fixp;
18488 
18489       first = RELAX_FIRST (subtype);
18490       second = RELAX_SECOND (subtype);
18491       fixp = (fixS *) fragp->fr_opcode;
18492 
18493       /* If the delay slot chosen does not match the size of the instruction,
18494          then emit a warning.  */
18495       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18496 	   || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18497 	{
18498 	  relax_substateT s;
18499 	  const char *msg;
18500 
18501 	  s = subtype & (RELAX_DELAY_SLOT_16BIT
18502 			 | RELAX_DELAY_SLOT_SIZE_FIRST
18503 			 | RELAX_DELAY_SLOT_SIZE_SECOND);
18504 	  msg = macro_warning (s);
18505 	  if (msg != NULL)
18506 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18507 	  subtype &= ~s;
18508 	}
18509 
18510       /* Possibly emit a warning if we've chosen the longer option.  */
18511       if (use_second == second_longer)
18512 	{
18513 	  relax_substateT s;
18514 	  const char *msg;
18515 
18516 	  s = (subtype
18517 	       & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18518 	  msg = macro_warning (s);
18519 	  if (msg != NULL)
18520 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18521 	  subtype &= ~s;
18522 	}
18523 
18524       /* Go through all the fixups for the first sequence.  Disable them
18525 	 (by marking them as done) if we're going to use the second
18526 	 sequence instead.  */
18527       while (fixp
18528 	     && fixp->fx_frag == fragp
18529 	     && fixp->fx_where < fragp->fr_fix - second)
18530 	{
18531 	  if (subtype & RELAX_USE_SECOND)
18532 	    fixp->fx_done = 1;
18533 	  fixp = fixp->fx_next;
18534 	}
18535 
18536       /* Go through the fixups for the second sequence.  Disable them if
18537 	 we're going to use the first sequence, otherwise adjust their
18538 	 addresses to account for the relaxation.  */
18539       while (fixp && fixp->fx_frag == fragp)
18540 	{
18541 	  if (subtype & RELAX_USE_SECOND)
18542 	    fixp->fx_where -= first;
18543 	  else
18544 	    fixp->fx_done = 1;
18545 	  fixp = fixp->fx_next;
18546 	}
18547 
18548       /* Now modify the frag contents.  */
18549       if (subtype & RELAX_USE_SECOND)
18550 	{
18551 	  char *start;
18552 
18553 	  start = fragp->fr_literal + fragp->fr_fix - first - second;
18554 	  memmove (start, start + first, second);
18555 	  fragp->fr_fix -= first;
18556 	}
18557       else
18558 	fragp->fr_fix -= second;
18559     }
18560 }
18561 
18562 #ifdef OBJ_ELF
18563 
18564 /* This function is called after the relocs have been generated.
18565    We've been storing mips16 text labels as odd.  Here we convert them
18566    back to even for the convenience of the debugger.  */
18567 
18568 void
18569 mips_frob_file_after_relocs (void)
18570 {
18571   asymbol **syms;
18572   unsigned int count, i;
18573 
18574   if (!IS_ELF)
18575     return;
18576 
18577   syms = bfd_get_outsymbols (stdoutput);
18578   count = bfd_get_symcount (stdoutput);
18579   for (i = 0; i < count; i++, syms++)
18580     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18581 	&& ((*syms)->value & 1) != 0)
18582       {
18583 	(*syms)->value &= ~1;
18584 	/* If the symbol has an odd size, it was probably computed
18585 	   incorrectly, so adjust that as well.  */
18586 	if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18587 	  ++elf_symbol (*syms)->internal_elf_sym.st_size;
18588       }
18589 }
18590 
18591 #endif
18592 
18593 /* This function is called whenever a label is defined, including fake
18594    labels instantiated off the dot special symbol.  It is used when
18595    handling branch delays; if a branch has a label, we assume we cannot
18596    move it.  This also bumps the value of the symbol by 1 in compressed
18597    code.  */
18598 
18599 void
18600 mips_record_label (symbolS *sym)
18601 {
18602   segment_info_type *si = seg_info (now_seg);
18603   struct insn_label_list *l;
18604 
18605   if (free_insn_labels == NULL)
18606     l = (struct insn_label_list *) xmalloc (sizeof *l);
18607   else
18608     {
18609       l = free_insn_labels;
18610       free_insn_labels = l->next;
18611     }
18612 
18613   l->label = sym;
18614   l->next = si->label_list;
18615   si->label_list = l;
18616 }
18617 
18618 /* This function is called as tc_frob_label() whenever a label is defined
18619    and adds a DWARF-2 record we only want for true labels.  */
18620 
18621 void
18622 mips_define_label (symbolS *sym)
18623 {
18624   mips_record_label (sym);
18625 #ifdef OBJ_ELF
18626   dwarf2_emit_label (sym);
18627 #endif
18628 }
18629 
18630 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18631 
18632 /* Some special processing for a MIPS ELF file.  */
18633 
18634 void
18635 mips_elf_final_processing (void)
18636 {
18637   /* Write out the register information.  */
18638   if (mips_abi != N64_ABI)
18639     {
18640       Elf32_RegInfo s;
18641 
18642       s.ri_gprmask = mips_gprmask;
18643       s.ri_cprmask[0] = mips_cprmask[0];
18644       s.ri_cprmask[1] = mips_cprmask[1];
18645       s.ri_cprmask[2] = mips_cprmask[2];
18646       s.ri_cprmask[3] = mips_cprmask[3];
18647       /* The gp_value field is set by the MIPS ELF backend.  */
18648 
18649       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18650 				       ((Elf32_External_RegInfo *)
18651 					mips_regmask_frag));
18652     }
18653   else
18654     {
18655       Elf64_Internal_RegInfo s;
18656 
18657       s.ri_gprmask = mips_gprmask;
18658       s.ri_pad = 0;
18659       s.ri_cprmask[0] = mips_cprmask[0];
18660       s.ri_cprmask[1] = mips_cprmask[1];
18661       s.ri_cprmask[2] = mips_cprmask[2];
18662       s.ri_cprmask[3] = mips_cprmask[3];
18663       /* The gp_value field is set by the MIPS ELF backend.  */
18664 
18665       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18666 				       ((Elf64_External_RegInfo *)
18667 					mips_regmask_frag));
18668     }
18669 
18670   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
18671      sort of BFD interface for this.  */
18672   if (mips_any_noreorder)
18673     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18674   if (mips_pic != NO_PIC)
18675     {
18676       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18677       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18678     }
18679   if (mips_abicalls)
18680     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18681 
18682   /* Set MIPS ELF flags for ASEs.  */
18683   /* We may need to define a new flag for DSP ASE, and set this flag when
18684      file_ase_dsp is true.  */
18685   /* Same for DSP R2.  */
18686   /* We may need to define a new flag for MT ASE, and set this flag when
18687      file_ase_mt is true.  */
18688   if (file_ase_mips16)
18689     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18690   if (file_ase_micromips)
18691     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18692 #if 0 /* XXX FIXME */
18693   if (file_ase_mips3d)
18694     elf_elfheader (stdoutput)->e_flags |= ???;
18695 #endif
18696   if (file_ase_mdmx)
18697     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18698 
18699   /* Set the MIPS ELF ABI flags.  */
18700   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18701     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18702   else if (mips_abi == O64_ABI)
18703     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18704   else if (mips_abi == EABI_ABI)
18705     {
18706       if (!file_mips_gp32)
18707 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18708       else
18709 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18710     }
18711   else if (mips_abi == N32_ABI)
18712     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18713 
18714   /* Nothing to do for N64_ABI.  */
18715 
18716   if (mips_32bitmode)
18717     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18718 
18719 #if 0 /* XXX FIXME */
18720   /* 32 bit code with 64 bit FP registers.  */
18721   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18722     elf_elfheader (stdoutput)->e_flags |= ???;
18723 #endif
18724 }
18725 
18726 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18727 
18728 typedef struct proc {
18729   symbolS *func_sym;
18730   symbolS *func_end_sym;
18731   unsigned long reg_mask;
18732   unsigned long reg_offset;
18733   unsigned long fpreg_mask;
18734   unsigned long fpreg_offset;
18735   unsigned long frame_offset;
18736   unsigned long frame_reg;
18737   unsigned long pc_reg;
18738 } procS;
18739 
18740 static procS cur_proc;
18741 static procS *cur_proc_ptr;
18742 static int numprocs;
18743 
18744 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
18745    as "2", and a normal nop as "0".  */
18746 
18747 #define NOP_OPCODE_MIPS		0
18748 #define NOP_OPCODE_MIPS16	1
18749 #define NOP_OPCODE_MICROMIPS	2
18750 
18751 char
18752 mips_nop_opcode (void)
18753 {
18754   if (seg_info (now_seg)->tc_segment_info_data.micromips)
18755     return NOP_OPCODE_MICROMIPS;
18756   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18757     return NOP_OPCODE_MIPS16;
18758   else
18759     return NOP_OPCODE_MIPS;
18760 }
18761 
18762 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
18763    32-bit microMIPS NOPs here (if applicable).  */
18764 
18765 void
18766 mips_handle_align (fragS *fragp)
18767 {
18768   char nop_opcode;
18769   char *p;
18770   int bytes, size, excess;
18771   valueT opcode;
18772 
18773   if (fragp->fr_type != rs_align_code)
18774     return;
18775 
18776   p = fragp->fr_literal + fragp->fr_fix;
18777   nop_opcode = *p;
18778   switch (nop_opcode)
18779     {
18780     case NOP_OPCODE_MICROMIPS:
18781       opcode = micromips_nop32_insn.insn_opcode;
18782       size = 4;
18783       break;
18784     case NOP_OPCODE_MIPS16:
18785       opcode = mips16_nop_insn.insn_opcode;
18786       size = 2;
18787       break;
18788     case NOP_OPCODE_MIPS:
18789     default:
18790       opcode = nop_insn.insn_opcode;
18791       size = 4;
18792       break;
18793     }
18794 
18795   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18796   excess = bytes % size;
18797 
18798   /* Handle the leading part if we're not inserting a whole number of
18799      instructions, and make it the end of the fixed part of the frag.
18800      Try to fit in a short microMIPS NOP if applicable and possible,
18801      and use zeroes otherwise.  */
18802   gas_assert (excess < 4);
18803   fragp->fr_fix += excess;
18804   switch (excess)
18805     {
18806     case 3:
18807       *p++ = '\0';
18808       /* Fall through.  */
18809     case 2:
18810       if (nop_opcode == NOP_OPCODE_MICROMIPS)
18811 	{
18812 	  md_number_to_chars (p, micromips_nop16_insn.insn_opcode, 2);
18813 	  p += 2;
18814 	  break;
18815 	}
18816       *p++ = '\0';
18817       /* Fall through.  */
18818     case 1:
18819       *p++ = '\0';
18820       /* Fall through.  */
18821     case 0:
18822       break;
18823     }
18824 
18825   md_number_to_chars (p, opcode, size);
18826   fragp->fr_var = size;
18827 }
18828 
18829 static void
18830 md_obj_begin (void)
18831 {
18832 }
18833 
18834 static void
18835 md_obj_end (void)
18836 {
18837   /* Check for premature end, nesting errors, etc.  */
18838   if (cur_proc_ptr)
18839     as_warn (_("missing .end at end of assembly"));
18840 }
18841 
18842 static long
18843 get_number (void)
18844 {
18845   int negative = 0;
18846   long val = 0;
18847 
18848   if (*input_line_pointer == '-')
18849     {
18850       ++input_line_pointer;
18851       negative = 1;
18852     }
18853   if (!ISDIGIT (*input_line_pointer))
18854     as_bad (_("expected simple number"));
18855   if (input_line_pointer[0] == '0')
18856     {
18857       if (input_line_pointer[1] == 'x')
18858 	{
18859 	  input_line_pointer += 2;
18860 	  while (ISXDIGIT (*input_line_pointer))
18861 	    {
18862 	      val <<= 4;
18863 	      val |= hex_value (*input_line_pointer++);
18864 	    }
18865 	  return negative ? -val : val;
18866 	}
18867       else
18868 	{
18869 	  ++input_line_pointer;
18870 	  while (ISDIGIT (*input_line_pointer))
18871 	    {
18872 	      val <<= 3;
18873 	      val |= *input_line_pointer++ - '0';
18874 	    }
18875 	  return negative ? -val : val;
18876 	}
18877     }
18878   if (!ISDIGIT (*input_line_pointer))
18879     {
18880       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18881 	      *input_line_pointer, *input_line_pointer);
18882       as_warn (_("invalid number"));
18883       return -1;
18884     }
18885   while (ISDIGIT (*input_line_pointer))
18886     {
18887       val *= 10;
18888       val += *input_line_pointer++ - '0';
18889     }
18890   return negative ? -val : val;
18891 }
18892 
18893 /* The .file directive; just like the usual .file directive, but there
18894    is an initial number which is the ECOFF file index.  In the non-ECOFF
18895    case .file implies DWARF-2.  */
18896 
18897 static void
18898 s_mips_file (int x ATTRIBUTE_UNUSED)
18899 {
18900   static int first_file_directive = 0;
18901 
18902   if (ECOFF_DEBUGGING)
18903     {
18904       get_number ();
18905       s_app_file (0);
18906     }
18907   else
18908     {
18909       char *filename;
18910 
18911       filename = dwarf2_directive_file (0);
18912 
18913       /* Versions of GCC up to 3.1 start files with a ".file"
18914 	 directive even for stabs output.  Make sure that this
18915 	 ".file" is handled.  Note that you need a version of GCC
18916          after 3.1 in order to support DWARF-2 on MIPS.  */
18917       if (filename != NULL && ! first_file_directive)
18918 	{
18919 	  (void) new_logical_line (filename, -1);
18920 	  s_app_file_string (filename, 0);
18921 	}
18922       first_file_directive = 1;
18923     }
18924 }
18925 
18926 /* The .loc directive, implying DWARF-2.  */
18927 
18928 static void
18929 s_mips_loc (int x ATTRIBUTE_UNUSED)
18930 {
18931   if (!ECOFF_DEBUGGING)
18932     dwarf2_directive_loc (0);
18933 }
18934 
18935 /* The .end directive.  */
18936 
18937 static void
18938 s_mips_end (int x ATTRIBUTE_UNUSED)
18939 {
18940   symbolS *p;
18941 
18942   /* Following functions need their own .frame and .cprestore directives.  */
18943   mips_frame_reg_valid = 0;
18944   mips_cprestore_valid = 0;
18945 
18946   if (!is_end_of_line[(unsigned char) *input_line_pointer])
18947     {
18948       p = get_symbol ();
18949       demand_empty_rest_of_line ();
18950     }
18951   else
18952     p = NULL;
18953 
18954   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18955     as_warn (_(".end not in text section"));
18956 
18957   if (!cur_proc_ptr)
18958     {
18959       as_warn (_(".end directive without a preceding .ent directive."));
18960       demand_empty_rest_of_line ();
18961       return;
18962     }
18963 
18964   if (p != NULL)
18965     {
18966       gas_assert (S_GET_NAME (p));
18967       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18968 	as_warn (_(".end symbol does not match .ent symbol."));
18969 
18970       if (debug_type == DEBUG_STABS)
18971 	stabs_generate_asm_endfunc (S_GET_NAME (p),
18972 				    S_GET_NAME (p));
18973     }
18974   else
18975     as_warn (_(".end directive missing or unknown symbol"));
18976 
18977 #ifdef OBJ_ELF
18978   /* Create an expression to calculate the size of the function.  */
18979   if (p && cur_proc_ptr)
18980     {
18981       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18982       expressionS *exp = xmalloc (sizeof (expressionS));
18983 
18984       obj->size = exp;
18985       exp->X_op = O_subtract;
18986       exp->X_add_symbol = symbol_temp_new_now ();
18987       exp->X_op_symbol = p;
18988       exp->X_add_number = 0;
18989 
18990       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18991     }
18992 
18993   /* Generate a .pdr section.  */
18994   if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
18995     {
18996       segT saved_seg = now_seg;
18997       subsegT saved_subseg = now_subseg;
18998       expressionS exp;
18999       char *fragp;
19000 
19001 #ifdef md_flush_pending_output
19002       md_flush_pending_output ();
19003 #endif
19004 
19005       gas_assert (pdr_seg);
19006       subseg_set (pdr_seg, 0);
19007 
19008       /* Write the symbol.  */
19009       exp.X_op = O_symbol;
19010       exp.X_add_symbol = p;
19011       exp.X_add_number = 0;
19012       emit_expr (&exp, 4);
19013 
19014       fragp = frag_more (7 * 4);
19015 
19016       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19017       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19018       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19019       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19020       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19021       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19022       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19023 
19024       subseg_set (saved_seg, saved_subseg);
19025     }
19026 #endif /* OBJ_ELF */
19027 
19028   cur_proc_ptr = NULL;
19029 }
19030 
19031 /* The .aent and .ent directives.  */
19032 
19033 static void
19034 s_mips_ent (int aent)
19035 {
19036   symbolS *symbolP;
19037 
19038   symbolP = get_symbol ();
19039   if (*input_line_pointer == ',')
19040     ++input_line_pointer;
19041   SKIP_WHITESPACE ();
19042   if (ISDIGIT (*input_line_pointer)
19043       || *input_line_pointer == '-')
19044     get_number ();
19045 
19046   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19047     as_warn (_(".ent or .aent not in text section."));
19048 
19049   if (!aent && cur_proc_ptr)
19050     as_warn (_("missing .end"));
19051 
19052   if (!aent)
19053     {
19054       /* This function needs its own .frame and .cprestore directives.  */
19055       mips_frame_reg_valid = 0;
19056       mips_cprestore_valid = 0;
19057 
19058       cur_proc_ptr = &cur_proc;
19059       memset (cur_proc_ptr, '\0', sizeof (procS));
19060 
19061       cur_proc_ptr->func_sym = symbolP;
19062 
19063       ++numprocs;
19064 
19065       if (debug_type == DEBUG_STABS)
19066         stabs_generate_asm_func (S_GET_NAME (symbolP),
19067 				 S_GET_NAME (symbolP));
19068     }
19069 
19070   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19071 
19072   demand_empty_rest_of_line ();
19073 }
19074 
19075 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19076    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19077    s_mips_frame is used so that we can set the PDR information correctly.
19078    We can't use the ecoff routines because they make reference to the ecoff
19079    symbol table (in the mdebug section).  */
19080 
19081 static void
19082 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19083 {
19084 #ifdef OBJ_ELF
19085   if (IS_ELF && !ECOFF_DEBUGGING)
19086     {
19087       long val;
19088 
19089       if (cur_proc_ptr == (procS *) NULL)
19090 	{
19091 	  as_warn (_(".frame outside of .ent"));
19092 	  demand_empty_rest_of_line ();
19093 	  return;
19094 	}
19095 
19096       cur_proc_ptr->frame_reg = tc_get_register (1);
19097 
19098       SKIP_WHITESPACE ();
19099       if (*input_line_pointer++ != ','
19100 	  || get_absolute_expression_and_terminator (&val) != ',')
19101 	{
19102 	  as_warn (_("Bad .frame directive"));
19103 	  --input_line_pointer;
19104 	  demand_empty_rest_of_line ();
19105 	  return;
19106 	}
19107 
19108       cur_proc_ptr->frame_offset = val;
19109       cur_proc_ptr->pc_reg = tc_get_register (0);
19110 
19111       demand_empty_rest_of_line ();
19112     }
19113   else
19114 #endif /* OBJ_ELF */
19115     s_ignore (ignore);
19116 }
19117 
19118 /* The .fmask and .mask directives. If the mdebug section is present
19119    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19120    embedded targets, s_mips_mask is used so that we can set the PDR
19121    information correctly. We can't use the ecoff routines because they
19122    make reference to the ecoff symbol table (in the mdebug section).  */
19123 
19124 static void
19125 s_mips_mask (int reg_type)
19126 {
19127 #ifdef OBJ_ELF
19128   if (IS_ELF && !ECOFF_DEBUGGING)
19129     {
19130       long mask, off;
19131 
19132       if (cur_proc_ptr == (procS *) NULL)
19133 	{
19134 	  as_warn (_(".mask/.fmask outside of .ent"));
19135 	  demand_empty_rest_of_line ();
19136 	  return;
19137 	}
19138 
19139       if (get_absolute_expression_and_terminator (&mask) != ',')
19140 	{
19141 	  as_warn (_("Bad .mask/.fmask directive"));
19142 	  --input_line_pointer;
19143 	  demand_empty_rest_of_line ();
19144 	  return;
19145 	}
19146 
19147       off = get_absolute_expression ();
19148 
19149       if (reg_type == 'F')
19150 	{
19151 	  cur_proc_ptr->fpreg_mask = mask;
19152 	  cur_proc_ptr->fpreg_offset = off;
19153 	}
19154       else
19155 	{
19156 	  cur_proc_ptr->reg_mask = mask;
19157 	  cur_proc_ptr->reg_offset = off;
19158 	}
19159 
19160       demand_empty_rest_of_line ();
19161     }
19162   else
19163 #endif /* OBJ_ELF */
19164     s_ignore (reg_type);
19165 }
19166 
19167 /* A table describing all the processors gas knows about.  Names are
19168    matched in the order listed.
19169 
19170    To ease comparison, please keep this table in the same order as
19171    gcc's mips_cpu_info_table[].  */
19172 static const struct mips_cpu_info mips_cpu_info_table[] =
19173 {
19174   /* Entries for generic ISAs */
19175   { "mips1",          MIPS_CPU_IS_ISA,		ISA_MIPS1,      CPU_R3000 },
19176   { "mips2",          MIPS_CPU_IS_ISA,		ISA_MIPS2,      CPU_R6000 },
19177   { "mips3",          MIPS_CPU_IS_ISA,		ISA_MIPS3,      CPU_R4000 },
19178   { "mips4",          MIPS_CPU_IS_ISA,		ISA_MIPS4,      CPU_R8000 },
19179   { "mips5",          MIPS_CPU_IS_ISA,		ISA_MIPS5,      CPU_MIPS5 },
19180   { "mips32",         MIPS_CPU_IS_ISA,		ISA_MIPS32,     CPU_MIPS32 },
19181   { "mips32r2",       MIPS_CPU_IS_ISA,		ISA_MIPS32R2,   CPU_MIPS32R2 },
19182   { "mips64",         MIPS_CPU_IS_ISA,		ISA_MIPS64,     CPU_MIPS64 },
19183   { "mips64r2",       MIPS_CPU_IS_ISA,		ISA_MIPS64R2,   CPU_MIPS64R2 },
19184 
19185   /* MIPS I */
19186   { "r3000",          0,			ISA_MIPS1,      CPU_R3000 },
19187   { "r2000",          0,			ISA_MIPS1,      CPU_R3000 },
19188   { "r3900",          0,			ISA_MIPS1,      CPU_R3900 },
19189 
19190   /* MIPS II */
19191   { "r6000",          0,			ISA_MIPS2,      CPU_R6000 },
19192 
19193   /* MIPS III */
19194   { "r4000",          0,			ISA_MIPS3,      CPU_R4000 },
19195   { "r4010",          0,			ISA_MIPS2,      CPU_R4010 },
19196   { "vr4100",         0,			ISA_MIPS3,      CPU_VR4100 },
19197   { "vr4111",         0,			ISA_MIPS3,      CPU_R4111 },
19198   { "vr4120",         0,			ISA_MIPS3,      CPU_VR4120 },
19199   { "vr4130",         0,			ISA_MIPS3,      CPU_VR4120 },
19200   { "vr4181",         0,			ISA_MIPS3,      CPU_R4111 },
19201   { "vr4300",         0,			ISA_MIPS3,      CPU_R4300 },
19202   { "r4400",          0,			ISA_MIPS3,      CPU_R4400 },
19203   { "r4600",          0,			ISA_MIPS3,      CPU_R4600 },
19204   { "orion",          0,			ISA_MIPS3,      CPU_R4600 },
19205   { "r4650",          0,			ISA_MIPS3,      CPU_R4650 },
19206   /* ST Microelectronics Loongson 2E and 2F cores */
19207   { "loongson2e",     0,			ISA_MIPS3,   CPU_LOONGSON_2E },
19208   { "loongson2f",     0,			ISA_MIPS3,   CPU_LOONGSON_2F },
19209 
19210   /* MIPS IV */
19211   { "r8000",          0,			ISA_MIPS4,      CPU_R8000 },
19212   { "r10000",         0,			ISA_MIPS4,      CPU_R10000 },
19213   { "r12000",         0,			ISA_MIPS4,      CPU_R12000 },
19214   { "r14000",         0,			ISA_MIPS4,      CPU_R14000 },
19215   { "r16000",         0,			ISA_MIPS4,      CPU_R16000 },
19216   { "vr5000",         0,			ISA_MIPS4,      CPU_R5000 },
19217   { "vr5400",         0,			ISA_MIPS4,      CPU_VR5400 },
19218   { "vr5500",         0,			ISA_MIPS4,      CPU_VR5500 },
19219   { "rm5200",         0,			ISA_MIPS4,      CPU_R5000 },
19220   { "rm5230",         0,			ISA_MIPS4,      CPU_R5000 },
19221   { "rm5231",         0,			ISA_MIPS4,      CPU_R5000 },
19222   { "rm5261",         0,			ISA_MIPS4,      CPU_R5000 },
19223   { "rm5721",         0,			ISA_MIPS4,      CPU_R5000 },
19224   { "rm7000",         0,			ISA_MIPS4,      CPU_RM7000 },
19225   { "rm9000",         0,			ISA_MIPS4,      CPU_RM9000 },
19226 
19227   /* MIPS 32 */
19228   { "4kc",            0,			ISA_MIPS32,	CPU_MIPS32 },
19229   { "4km",            0,			ISA_MIPS32,	CPU_MIPS32 },
19230   { "4kp",            0,			ISA_MIPS32,	CPU_MIPS32 },
19231   { "4ksc",           MIPS_CPU_ASE_SMARTMIPS,	ISA_MIPS32,	CPU_MIPS32 },
19232 
19233   /* MIPS 32 Release 2 */
19234   { "4kec",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19235   { "4kem",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19236   { "4kep",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19237   { "4ksd",           MIPS_CPU_ASE_SMARTMIPS,	ISA_MIPS32R2,   CPU_MIPS32R2 },
19238   { "m4k",            0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19239   { "m4kp",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19240   { "m14k",           MIPS_CPU_ASE_MCU,		ISA_MIPS32R2,   CPU_MIPS32R2 },
19241   { "m14kc",          MIPS_CPU_ASE_MCU,		ISA_MIPS32R2,   CPU_MIPS32R2 },
19242   { "m14ke",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19243 						ISA_MIPS32R2,   CPU_MIPS32R2 },
19244   { "m14kec",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19245 						ISA_MIPS32R2,   CPU_MIPS32R2 },
19246   { "24kc",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19247   { "24kf2_1",        0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19248   { "24kf",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19249   { "24kf1_1",        0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19250   /* Deprecated forms of the above.  */
19251   { "24kfx",          0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19252   { "24kx",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19253   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
19254   { "24kec",          MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19255   { "24kef2_1",       MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19256   { "24kef",          MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19257   { "24kef1_1",       MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19258   /* Deprecated forms of the above.  */
19259   { "24kefx",         MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19260   { "24kex",          MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19261   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
19262   { "34kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19263 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19264   { "34kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19265 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19266   { "34kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19267 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19268   { "34kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19269 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19270   /* Deprecated forms of the above.  */
19271   { "34kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19272 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19273   { "34kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19274 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19275   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
19276   { "74kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19277 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19278   { "74kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19279 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19280   { "74kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19281 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19282   { "74kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19283 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19284   { "74kf3_2",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19285 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19286   /* Deprecated forms of the above.  */
19287   { "74kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19288 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19289   { "74kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19290 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19291   /* 1004K cores are multiprocessor versions of the 34K.  */
19292   { "1004kc",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19293 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19294   { "1004kf2_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19295 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19296   { "1004kf",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19297 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19298   { "1004kf1_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19299 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19300 
19301   /* MIPS 64 */
19302   { "5kc",            0,			ISA_MIPS64,	CPU_MIPS64 },
19303   { "5kf",            0,			ISA_MIPS64,	CPU_MIPS64 },
19304   { "20kc",           MIPS_CPU_ASE_MIPS3D,	ISA_MIPS64,	CPU_MIPS64 },
19305   { "25kf",           MIPS_CPU_ASE_MIPS3D,	ISA_MIPS64,     CPU_MIPS64 },
19306 
19307   /* Broadcom SB-1 CPU core */
19308   { "sb1",            MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19309 						ISA_MIPS64,	CPU_SB1 },
19310   /* Broadcom SB-1A CPU core */
19311   { "sb1a",           MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19312 						ISA_MIPS64,	CPU_SB1 },
19313 
19314   { "loongson3a",     0,			ISA_MIPS64,	CPU_LOONGSON_3A },
19315 
19316   /* MIPS 64 Release 2 */
19317 
19318   /* Cavium Networks Octeon CPU core */
19319   { "octeon",	      0,      ISA_MIPS64R2,   CPU_OCTEON },
19320   { "octeon+",	      0,      ISA_MIPS64R2,   CPU_OCTEONP },
19321   { "octeon2",	      0,      ISA_MIPS64R2,   CPU_OCTEON2 },
19322 
19323   /* RMI Xlr */
19324   { "xlr",	      0,      ISA_MIPS64,     CPU_XLR },
19325 
19326   /* Broadcom XLP.
19327      XLP is mostly like XLR, with the prominent exception that it is
19328      MIPS64R2 rather than MIPS64.  */
19329   { "xlp",	      0,      ISA_MIPS64R2,     CPU_XLR },
19330 
19331   /* End marker */
19332   { NULL, 0, 0, 0 }
19333 };
19334 
19335 
19336 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19337    with a final "000" replaced by "k".  Ignore case.
19338 
19339    Note: this function is shared between GCC and GAS.  */
19340 
19341 static bfd_boolean
19342 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19343 {
19344   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19345     given++, canonical++;
19346 
19347   return ((*given == 0 && *canonical == 0)
19348 	  || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19349 }
19350 
19351 
19352 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19353    CPU name.  We've traditionally allowed a lot of variation here.
19354 
19355    Note: this function is shared between GCC and GAS.  */
19356 
19357 static bfd_boolean
19358 mips_matching_cpu_name_p (const char *canonical, const char *given)
19359 {
19360   /* First see if the name matches exactly, or with a final "000"
19361      turned into "k".  */
19362   if (mips_strict_matching_cpu_name_p (canonical, given))
19363     return TRUE;
19364 
19365   /* If not, try comparing based on numerical designation alone.
19366      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
19367   if (TOLOWER (*given) == 'r')
19368     given++;
19369   if (!ISDIGIT (*given))
19370     return FALSE;
19371 
19372   /* Skip over some well-known prefixes in the canonical name,
19373      hoping to find a number there too.  */
19374   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19375     canonical += 2;
19376   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19377     canonical += 2;
19378   else if (TOLOWER (canonical[0]) == 'r')
19379     canonical += 1;
19380 
19381   return mips_strict_matching_cpu_name_p (canonical, given);
19382 }
19383 
19384 
19385 /* Parse an option that takes the name of a processor as its argument.
19386    OPTION is the name of the option and CPU_STRING is the argument.
19387    Return the corresponding processor enumeration if the CPU_STRING is
19388    recognized, otherwise report an error and return null.
19389 
19390    A similar function exists in GCC.  */
19391 
19392 static const struct mips_cpu_info *
19393 mips_parse_cpu (const char *option, const char *cpu_string)
19394 {
19395   const struct mips_cpu_info *p;
19396 
19397   /* 'from-abi' selects the most compatible architecture for the given
19398      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
19399      EABIs, we have to decide whether we're using the 32-bit or 64-bit
19400      version.  Look first at the -mgp options, if given, otherwise base
19401      the choice on MIPS_DEFAULT_64BIT.
19402 
19403      Treat NO_ABI like the EABIs.  One reason to do this is that the
19404      plain 'mips' and 'mips64' configs have 'from-abi' as their default
19405      architecture.  This code picks MIPS I for 'mips' and MIPS III for
19406      'mips64', just as we did in the days before 'from-abi'.  */
19407   if (strcasecmp (cpu_string, "from-abi") == 0)
19408     {
19409       if (ABI_NEEDS_32BIT_REGS (mips_abi))
19410 	return mips_cpu_info_from_isa (ISA_MIPS1);
19411 
19412       if (ABI_NEEDS_64BIT_REGS (mips_abi))
19413 	return mips_cpu_info_from_isa (ISA_MIPS3);
19414 
19415       if (file_mips_gp32 >= 0)
19416 	return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19417 
19418       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19419 				     ? ISA_MIPS3
19420 				     : ISA_MIPS1);
19421     }
19422 
19423   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
19424   if (strcasecmp (cpu_string, "default") == 0)
19425     return 0;
19426 
19427   for (p = mips_cpu_info_table; p->name != 0; p++)
19428     if (mips_matching_cpu_name_p (p->name, cpu_string))
19429       return p;
19430 
19431   as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19432   return 0;
19433 }
19434 
19435 /* Return the canonical processor information for ISA (a member of the
19436    ISA_MIPS* enumeration).  */
19437 
19438 static const struct mips_cpu_info *
19439 mips_cpu_info_from_isa (int isa)
19440 {
19441   int i;
19442 
19443   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19444     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19445 	&& isa == mips_cpu_info_table[i].isa)
19446       return (&mips_cpu_info_table[i]);
19447 
19448   return NULL;
19449 }
19450 
19451 static const struct mips_cpu_info *
19452 mips_cpu_info_from_arch (int arch)
19453 {
19454   int i;
19455 
19456   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19457     if (arch == mips_cpu_info_table[i].cpu)
19458       return (&mips_cpu_info_table[i]);
19459 
19460   return NULL;
19461 }
19462 
19463 static void
19464 show (FILE *stream, const char *string, int *col_p, int *first_p)
19465 {
19466   if (*first_p)
19467     {
19468       fprintf (stream, "%24s", "");
19469       *col_p = 24;
19470     }
19471   else
19472     {
19473       fprintf (stream, ", ");
19474       *col_p += 2;
19475     }
19476 
19477   if (*col_p + strlen (string) > 72)
19478     {
19479       fprintf (stream, "\n%24s", "");
19480       *col_p = 24;
19481     }
19482 
19483   fprintf (stream, "%s", string);
19484   *col_p += strlen (string);
19485 
19486   *first_p = 0;
19487 }
19488 
19489 void
19490 md_show_usage (FILE *stream)
19491 {
19492   int column, first;
19493   size_t i;
19494 
19495   fprintf (stream, _("\
19496 MIPS options:\n\
19497 -EB			generate big endian output\n\
19498 -EL			generate little endian output\n\
19499 -g, -g2			do not remove unneeded NOPs or swap branches\n\
19500 -G NUM			allow referencing objects up to NUM bytes\n\
19501 			implicitly with the gp register [default 8]\n"));
19502   fprintf (stream, _("\
19503 -mips1			generate MIPS ISA I instructions\n\
19504 -mips2			generate MIPS ISA II instructions\n\
19505 -mips3			generate MIPS ISA III instructions\n\
19506 -mips4			generate MIPS ISA IV instructions\n\
19507 -mips5                  generate MIPS ISA V instructions\n\
19508 -mips32                 generate MIPS32 ISA instructions\n\
19509 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
19510 -mips64                 generate MIPS64 ISA instructions\n\
19511 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
19512 -march=CPU/-mtune=CPU	generate code/schedule for CPU, where CPU is one of:\n"));
19513 
19514   first = 1;
19515 
19516   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19517     show (stream, mips_cpu_info_table[i].name, &column, &first);
19518   show (stream, "from-abi", &column, &first);
19519   fputc ('\n', stream);
19520 
19521   fprintf (stream, _("\
19522 -mCPU			equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19523 -no-mCPU		don't generate code specific to CPU.\n\
19524 			For -mCPU and -no-mCPU, CPU must be one of:\n"));
19525 
19526   first = 1;
19527 
19528   show (stream, "3900", &column, &first);
19529   show (stream, "4010", &column, &first);
19530   show (stream, "4100", &column, &first);
19531   show (stream, "4650", &column, &first);
19532   fputc ('\n', stream);
19533 
19534   fprintf (stream, _("\
19535 -mips16			generate mips16 instructions\n\
19536 -no-mips16		do not generate mips16 instructions\n"));
19537   fprintf (stream, _("\
19538 -mmicromips		generate microMIPS instructions\n\
19539 -mno-micromips		do not generate microMIPS instructions\n"));
19540   fprintf (stream, _("\
19541 -msmartmips		generate smartmips instructions\n\
19542 -mno-smartmips		do not generate smartmips instructions\n"));
19543   fprintf (stream, _("\
19544 -mdsp			generate DSP instructions\n\
19545 -mno-dsp		do not generate DSP instructions\n"));
19546   fprintf (stream, _("\
19547 -mdspr2			generate DSP R2 instructions\n\
19548 -mno-dspr2		do not generate DSP R2 instructions\n"));
19549   fprintf (stream, _("\
19550 -mmt			generate MT instructions\n\
19551 -mno-mt			do not generate MT instructions\n"));
19552   fprintf (stream, _("\
19553 -mmcu			generate MCU instructions\n\
19554 -mno-mcu		do not generate MCU instructions\n"));
19555   fprintf (stream, _("\
19556 -mfix-loongson2f-jump	work around Loongson2F JUMP instructions\n\
19557 -mfix-loongson2f-nop	work around Loongson2F NOP errata\n\
19558 -mfix-loongson2f-btb	work around Loongson2F BTB errata\n\
19559 -mfix-vr4120		work around certain VR4120 errata\n\
19560 -mfix-vr4130		work around VR4130 mflo/mfhi errata\n\
19561 -mfix-24k		insert a nop after ERET and DERET instructions\n\
19562 -mfix-cn63xxp1		work around CN63XXP1 PREF errata\n\
19563 -mgp32			use 32-bit GPRs, regardless of the chosen ISA\n\
19564 -mfp32			use 32-bit FPRs, regardless of the chosen ISA\n\
19565 -msym32			assume all symbols have 32-bit values\n\
19566 -O0			remove unneeded NOPs, do not swap branches\n\
19567 -O			remove unneeded NOPs and swap branches\n\
19568 --trap, --no-break	trap exception on div by 0 and mult overflow\n\
19569 --break, --no-trap	break exception on div by 0 and mult overflow\n"));
19570   fprintf (stream, _("\
19571 -mhard-float		allow floating-point instructions\n\
19572 -msoft-float		do not allow floating-point instructions\n\
19573 -msingle-float		only allow 32-bit floating-point operations\n\
19574 -mdouble-float		allow 32-bit and 64-bit floating-point operations\n\
19575 --[no-]construct-floats [dis]allow floating point values to be constructed\n"
19576 		     ));
19577 #ifdef OBJ_ELF
19578   fprintf (stream, _("\
19579 -KPIC, -call_shared	generate SVR4 position independent code\n\
19580 -call_nonpic		generate non-PIC code that can operate with DSOs\n\
19581 -mvxworks-pic		generate VxWorks position independent code\n\
19582 -non_shared		do not generate code that can operate with DSOs\n\
19583 -xgot			assume a 32 bit GOT\n\
19584 -mpdr, -mno-pdr		enable/disable creation of .pdr sections\n\
19585 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
19586                         position dependent (non shared) code\n\
19587 -mabi=ABI		create ABI conformant object file for:\n"));
19588 
19589   first = 1;
19590 
19591   show (stream, "32", &column, &first);
19592   show (stream, "o64", &column, &first);
19593   show (stream, "n32", &column, &first);
19594   show (stream, "64", &column, &first);
19595   show (stream, "eabi", &column, &first);
19596 
19597   fputc ('\n', stream);
19598 
19599   fprintf (stream, _("\
19600 -32			create o32 ABI object file (default)\n\
19601 -n32			create n32 ABI object file\n\
19602 -64			create 64 ABI object file\n"));
19603 #endif
19604 }
19605 
19606 #ifdef TE_IRIX
19607 enum dwarf2_format
19608 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19609 {
19610   if (HAVE_64BIT_SYMBOLS)
19611     return dwarf2_format_64bit_irix;
19612   else
19613     return dwarf2_format_32bit;
19614 }
19615 #endif
19616 
19617 int
19618 mips_dwarf2_addr_size (void)
19619 {
19620   if (HAVE_64BIT_OBJECTS)
19621     return 8;
19622   else
19623     return 4;
19624 }
19625 
19626 /* Standard calling conventions leave the CFA at SP on entry.  */
19627 void
19628 mips_cfi_frame_initial_instructions (void)
19629 {
19630   cfi_add_CFA_def_cfa_register (SP);
19631 }
19632 
19633 int
19634 tc_mips_regname_to_dw2regnum (char *regname)
19635 {
19636   unsigned int regnum = -1;
19637   unsigned int reg;
19638 
19639   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19640     regnum = reg;
19641 
19642   return regnum;
19643 }
19644