xref: /netbsd-src/external/gpl3/binutils/dist/gas/config/tc-mips.c (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
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 = 1;
9239     case M_SAA_OB:
9240       s = "saa";
9241       off0 = 1;
9242       fmt = "t,(b)";
9243       goto ld_st;
9244     case M_SAAD_AB:
9245       ab = 1;
9246     case M_SAAD_OB:
9247       s = "saad";
9248       off0 = 1;
9249       fmt = "t,(b)";
9250       goto ld_st;
9251 
9252    /* New code added to support COPZ instructions.
9253       This code builds table entries out of the macros in mip_opcodes.
9254       R4000 uses interlocks to handle coproc delays.
9255       Other chips (like the R3000) require nops to be inserted for delays.
9256 
9257       FIXME: Currently, we require that the user handle delays.
9258       In order to fill delay slots for non-interlocked chips,
9259       we must have a way to specify delays based on the coprocessor.
9260       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9261       What are the side-effects of the cop instruction?
9262       What cache support might we have and what are its effects?
9263       Both coprocessor & memory require delays. how long???
9264       What registers are read/set/modified?
9265 
9266       If an itbl is provided to interpret cop instructions,
9267       this knowledge can be encoded in the itbl spec.  */
9268 
9269     case M_COP0:
9270       s = "c0";
9271       goto copz;
9272     case M_COP1:
9273       s = "c1";
9274       goto copz;
9275     case M_COP2:
9276       s = "c2";
9277       goto copz;
9278     case M_COP3:
9279       s = "c3";
9280     copz:
9281       gas_assert (!mips_opts.micromips);
9282       /* For now we just do C (same as Cz).  The parameter will be
9283          stored in insn_opcode by mips_ip.  */
9284       macro_build (NULL, s, "C", ip->insn_opcode);
9285       break;
9286 
9287     case M_MOVE:
9288       move_register (dreg, sreg);
9289       break;
9290 
9291     case M_DMUL:
9292       dbl = 1;
9293     case M_MUL:
9294       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9295       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9296       break;
9297 
9298     case M_DMUL_I:
9299       dbl = 1;
9300     case M_MUL_I:
9301       /* The MIPS assembler some times generates shifts and adds.  I'm
9302 	 not trying to be that fancy. GCC should do this for us
9303 	 anyway.  */
9304       used_at = 1;
9305       load_register (AT, &imm_expr, dbl);
9306       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9307       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9308       break;
9309 
9310     case M_DMULO_I:
9311       dbl = 1;
9312     case M_MULO_I:
9313       imm = 1;
9314       goto do_mulo;
9315 
9316     case M_DMULO:
9317       dbl = 1;
9318     case M_MULO:
9319     do_mulo:
9320       start_noreorder ();
9321       used_at = 1;
9322       if (imm)
9323 	load_register (AT, &imm_expr, dbl);
9324       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9325       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9326       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9327       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9328       if (mips_trap)
9329 	macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9330       else
9331 	{
9332 	  if (mips_opts.micromips)
9333 	    micromips_label_expr (&label_expr);
9334 	  else
9335 	    label_expr.X_add_number = 8;
9336 	  macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9337 	  macro_build (NULL, "nop", "");
9338 	  macro_build (NULL, "break", BRK_FMT, 6);
9339 	  if (mips_opts.micromips)
9340 	    micromips_add_label ();
9341 	}
9342       end_noreorder ();
9343       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9344       break;
9345 
9346     case M_DMULOU_I:
9347       dbl = 1;
9348     case M_MULOU_I:
9349       imm = 1;
9350       goto do_mulou;
9351 
9352     case M_DMULOU:
9353       dbl = 1;
9354     case M_MULOU:
9355     do_mulou:
9356       start_noreorder ();
9357       used_at = 1;
9358       if (imm)
9359 	load_register (AT, &imm_expr, dbl);
9360       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9361 		   sreg, imm ? AT : treg);
9362       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9363       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9364       if (mips_trap)
9365 	macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9366       else
9367 	{
9368 	  if (mips_opts.micromips)
9369 	    micromips_label_expr (&label_expr);
9370 	  else
9371 	    label_expr.X_add_number = 8;
9372 	  macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9373 	  macro_build (NULL, "nop", "");
9374 	  macro_build (NULL, "break", BRK_FMT, 6);
9375 	  if (mips_opts.micromips)
9376 	    micromips_add_label ();
9377 	}
9378       end_noreorder ();
9379       break;
9380 
9381     case M_DROL:
9382       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9383 	{
9384 	  if (dreg == sreg)
9385 	    {
9386 	      tempreg = AT;
9387 	      used_at = 1;
9388 	    }
9389 	  else
9390 	    {
9391 	      tempreg = dreg;
9392 	    }
9393 	  macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9394 	  macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9395 	  break;
9396 	}
9397       used_at = 1;
9398       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9399       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9400       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9401       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9402       break;
9403 
9404     case M_ROL:
9405       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9406 	{
9407 	  if (dreg == sreg)
9408 	    {
9409 	      tempreg = AT;
9410 	      used_at = 1;
9411 	    }
9412 	  else
9413 	    {
9414 	      tempreg = dreg;
9415 	    }
9416 	  macro_build (NULL, "negu", "d,w", tempreg, treg);
9417 	  macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9418 	  break;
9419 	}
9420       used_at = 1;
9421       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9422       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9423       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9424       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9425       break;
9426 
9427     case M_DROL_I:
9428       {
9429 	unsigned int rot;
9430 	char *l;
9431 	char *rr;
9432 
9433 	if (imm_expr.X_op != O_constant)
9434 	  as_bad (_("Improper rotate count"));
9435 	rot = imm_expr.X_add_number & 0x3f;
9436 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9437 	  {
9438 	    rot = (64 - rot) & 0x3f;
9439 	    if (rot >= 32)
9440 	      macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9441 	    else
9442 	      macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9443 	    break;
9444 	  }
9445 	if (rot == 0)
9446 	  {
9447 	    macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9448 	    break;
9449 	  }
9450 	l = (rot < 0x20) ? "dsll" : "dsll32";
9451 	rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9452 	rot &= 0x1f;
9453 	used_at = 1;
9454 	macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9455 	macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9456 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9457       }
9458       break;
9459 
9460     case M_ROL_I:
9461       {
9462 	unsigned int rot;
9463 
9464 	if (imm_expr.X_op != O_constant)
9465 	  as_bad (_("Improper rotate count"));
9466 	rot = imm_expr.X_add_number & 0x1f;
9467 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9468 	  {
9469 	    macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9470 	    break;
9471 	  }
9472 	if (rot == 0)
9473 	  {
9474 	    macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9475 	    break;
9476 	  }
9477 	used_at = 1;
9478 	macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9479 	macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9480 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9481       }
9482       break;
9483 
9484     case M_DROR:
9485       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9486 	{
9487 	  macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9488 	  break;
9489 	}
9490       used_at = 1;
9491       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9492       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9493       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9494       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9495       break;
9496 
9497     case M_ROR:
9498       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9499 	{
9500 	  macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9501 	  break;
9502 	}
9503       used_at = 1;
9504       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9505       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9506       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9507       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9508       break;
9509 
9510     case M_DROR_I:
9511       {
9512 	unsigned int rot;
9513 	char *l;
9514 	char *rr;
9515 
9516 	if (imm_expr.X_op != O_constant)
9517 	  as_bad (_("Improper rotate count"));
9518 	rot = imm_expr.X_add_number & 0x3f;
9519 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9520 	  {
9521 	    if (rot >= 32)
9522 	      macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9523 	    else
9524 	      macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9525 	    break;
9526 	  }
9527 	if (rot == 0)
9528 	  {
9529 	    macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9530 	    break;
9531 	  }
9532 	rr = (rot < 0x20) ? "dsrl" : "dsrl32";
9533 	l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9534 	rot &= 0x1f;
9535 	used_at = 1;
9536 	macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9537 	macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9538 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9539       }
9540       break;
9541 
9542     case M_ROR_I:
9543       {
9544 	unsigned int rot;
9545 
9546 	if (imm_expr.X_op != O_constant)
9547 	  as_bad (_("Improper rotate count"));
9548 	rot = imm_expr.X_add_number & 0x1f;
9549 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9550 	  {
9551 	    macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
9552 	    break;
9553 	  }
9554 	if (rot == 0)
9555 	  {
9556 	    macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9557 	    break;
9558 	  }
9559 	used_at = 1;
9560 	macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9561 	macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9562 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9563       }
9564       break;
9565 
9566     case M_SEQ:
9567       if (sreg == 0)
9568 	macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
9569       else if (treg == 0)
9570 	macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9571       else
9572 	{
9573 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9574 	  macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9575 	}
9576       break;
9577 
9578     case M_SEQ_I:
9579       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9580 	{
9581 	  macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9582 	  break;
9583 	}
9584       if (sreg == 0)
9585 	{
9586 	  as_warn (_("Instruction %s: result is always false"),
9587 		   ip->insn_mo->name);
9588 	  move_register (dreg, 0);
9589 	  break;
9590 	}
9591       if (CPU_HAS_SEQ (mips_opts.arch)
9592 	  && -512 <= imm_expr.X_add_number
9593 	  && imm_expr.X_add_number < 512)
9594 	{
9595 	  macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
9596 		       (int) imm_expr.X_add_number);
9597 	  break;
9598 	}
9599       if (imm_expr.X_op == O_constant
9600 	  && imm_expr.X_add_number >= 0
9601 	  && imm_expr.X_add_number < 0x10000)
9602 	{
9603 	  macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9604 	}
9605       else if (imm_expr.X_op == O_constant
9606 	       && imm_expr.X_add_number > -0x8000
9607 	       && imm_expr.X_add_number < 0)
9608 	{
9609 	  imm_expr.X_add_number = -imm_expr.X_add_number;
9610 	  macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9611 		       "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9612 	}
9613       else if (CPU_HAS_SEQ (mips_opts.arch))
9614 	{
9615 	  used_at = 1;
9616 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9617 	  macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9618 	  break;
9619 	}
9620       else
9621 	{
9622 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9623 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9624 	  used_at = 1;
9625 	}
9626       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9627       break;
9628 
9629     case M_SGE:		/* sreg >= treg <==> not (sreg < treg) */
9630       s = "slt";
9631       goto sge;
9632     case M_SGEU:
9633       s = "sltu";
9634     sge:
9635       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9636       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9637       break;
9638 
9639     case M_SGE_I:		/* sreg >= I <==> not (sreg < I) */
9640     case M_SGEU_I:
9641       if (imm_expr.X_op == O_constant
9642 	  && imm_expr.X_add_number >= -0x8000
9643 	  && imm_expr.X_add_number < 0x8000)
9644 	{
9645 	  macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9646 		       dreg, sreg, BFD_RELOC_LO16);
9647 	}
9648       else
9649 	{
9650 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9651 	  macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9652 		       dreg, sreg, AT);
9653 	  used_at = 1;
9654 	}
9655       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9656       break;
9657 
9658     case M_SGT:		/* sreg > treg  <==>  treg < sreg */
9659       s = "slt";
9660       goto sgt;
9661     case M_SGTU:
9662       s = "sltu";
9663     sgt:
9664       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9665       break;
9666 
9667     case M_SGT_I:		/* sreg > I  <==>  I < sreg */
9668       s = "slt";
9669       goto sgti;
9670     case M_SGTU_I:
9671       s = "sltu";
9672     sgti:
9673       used_at = 1;
9674       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9675       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9676       break;
9677 
9678     case M_SLE:	/* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
9679       s = "slt";
9680       goto sle;
9681     case M_SLEU:
9682       s = "sltu";
9683     sle:
9684       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9685       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9686       break;
9687 
9688     case M_SLE_I:	/* sreg <= I <==> I >= sreg <==> not (I < sreg) */
9689       s = "slt";
9690       goto slei;
9691     case M_SLEU_I:
9692       s = "sltu";
9693     slei:
9694       used_at = 1;
9695       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9696       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9697       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9698       break;
9699 
9700     case M_SLT_I:
9701       if (imm_expr.X_op == O_constant
9702 	  && imm_expr.X_add_number >= -0x8000
9703 	  && imm_expr.X_add_number < 0x8000)
9704 	{
9705 	  macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9706 	  break;
9707 	}
9708       used_at = 1;
9709       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9710       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
9711       break;
9712 
9713     case M_SLTU_I:
9714       if (imm_expr.X_op == O_constant
9715 	  && imm_expr.X_add_number >= -0x8000
9716 	  && imm_expr.X_add_number < 0x8000)
9717 	{
9718 	  macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
9719 		       BFD_RELOC_LO16);
9720 	  break;
9721 	}
9722       used_at = 1;
9723       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9724       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
9725       break;
9726 
9727     case M_SNE:
9728       if (sreg == 0)
9729 	macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
9730       else if (treg == 0)
9731 	macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9732       else
9733 	{
9734 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9735 	  macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9736 	}
9737       break;
9738 
9739     case M_SNE_I:
9740       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9741 	{
9742 	  macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9743 	  break;
9744 	}
9745       if (sreg == 0)
9746 	{
9747 	  as_warn (_("Instruction %s: result is always true"),
9748 		   ip->insn_mo->name);
9749 	  macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9750 		       dreg, 0, BFD_RELOC_LO16);
9751 	  break;
9752 	}
9753       if (CPU_HAS_SEQ (mips_opts.arch)
9754 	  && -512 <= imm_expr.X_add_number
9755 	  && imm_expr.X_add_number < 512)
9756 	{
9757 	  macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
9758 		       (int) imm_expr.X_add_number);
9759 	  break;
9760 	}
9761       if (imm_expr.X_op == O_constant
9762 	  && imm_expr.X_add_number >= 0
9763 	  && imm_expr.X_add_number < 0x10000)
9764 	{
9765 	  macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9766 	}
9767       else if (imm_expr.X_op == O_constant
9768 	       && imm_expr.X_add_number > -0x8000
9769 	       && imm_expr.X_add_number < 0)
9770 	{
9771 	  imm_expr.X_add_number = -imm_expr.X_add_number;
9772 	  macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9773 		       "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9774 	}
9775       else if (CPU_HAS_SEQ (mips_opts.arch))
9776 	{
9777 	  used_at = 1;
9778 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9779 	  macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9780 	  break;
9781 	}
9782       else
9783 	{
9784 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9785 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9786 	  used_at = 1;
9787 	}
9788       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9789       break;
9790 
9791     case M_SUB_I:
9792       s = "addi";
9793       s2 = "sub";
9794       goto do_subi;
9795     case M_SUBU_I:
9796       s = "addiu";
9797       s2 = "subu";
9798       goto do_subi;
9799     case M_DSUB_I:
9800       dbl = 1;
9801       s = "daddi";
9802       s2 = "dsub";
9803       if (!mips_opts.micromips)
9804 	goto do_subi;
9805       if (imm_expr.X_op == O_constant
9806 	  && imm_expr.X_add_number > -0x200
9807 	  && imm_expr.X_add_number <= 0x200)
9808 	{
9809 	  macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
9810 	  break;
9811 	}
9812       goto do_subi_i;
9813     case M_DSUBU_I:
9814       dbl = 1;
9815       s = "daddiu";
9816       s2 = "dsubu";
9817     do_subi:
9818       if (imm_expr.X_op == O_constant
9819 	  && imm_expr.X_add_number > -0x8000
9820 	  && imm_expr.X_add_number <= 0x8000)
9821 	{
9822 	  imm_expr.X_add_number = -imm_expr.X_add_number;
9823 	  macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9824 	  break;
9825 	}
9826     do_subi_i:
9827       used_at = 1;
9828       load_register (AT, &imm_expr, dbl);
9829       macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
9830       break;
9831 
9832     case M_TEQ_I:
9833       s = "teq";
9834       goto trap;
9835     case M_TGE_I:
9836       s = "tge";
9837       goto trap;
9838     case M_TGEU_I:
9839       s = "tgeu";
9840       goto trap;
9841     case M_TLT_I:
9842       s = "tlt";
9843       goto trap;
9844     case M_TLTU_I:
9845       s = "tltu";
9846       goto trap;
9847     case M_TNE_I:
9848       s = "tne";
9849     trap:
9850       used_at = 1;
9851       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9852       macro_build (NULL, s, "s,t", sreg, AT);
9853       break;
9854 
9855     case M_TRUNCWS:
9856     case M_TRUNCWD:
9857       gas_assert (!mips_opts.micromips);
9858       gas_assert (mips_opts.isa == ISA_MIPS1);
9859       used_at = 1;
9860       sreg = (ip->insn_opcode >> 11) & 0x1f;	/* floating reg */
9861       dreg = (ip->insn_opcode >> 06) & 0x1f;	/* floating reg */
9862 
9863       /*
9864        * Is the double cfc1 instruction a bug in the mips assembler;
9865        * or is there a reason for it?
9866        */
9867       start_noreorder ();
9868       macro_build (NULL, "cfc1", "t,G", treg, RA);
9869       macro_build (NULL, "cfc1", "t,G", treg, RA);
9870       macro_build (NULL, "nop", "");
9871       expr1.X_add_number = 3;
9872       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
9873       expr1.X_add_number = 2;
9874       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9875       macro_build (NULL, "ctc1", "t,G", AT, RA);
9876       macro_build (NULL, "nop", "");
9877       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9878 		   dreg, sreg);
9879       macro_build (NULL, "ctc1", "t,G", treg, RA);
9880       macro_build (NULL, "nop", "");
9881       end_noreorder ();
9882       break;
9883 
9884     case M_ULH_A:
9885       ab = 1;
9886     case M_ULH:
9887       s = "lb";
9888       s2 = "lbu";
9889       off = 1;
9890       goto uld_st;
9891     case M_ULHU_A:
9892       ab = 1;
9893     case M_ULHU:
9894       s = "lbu";
9895       s2 = "lbu";
9896       off = 1;
9897       goto uld_st;
9898     case M_ULW_A:
9899       ab = 1;
9900     case M_ULW:
9901       s = "lwl";
9902       s2 = "lwr";
9903       off12 = mips_opts.micromips;
9904       off = 3;
9905       goto uld_st;
9906     case M_ULD_A:
9907       ab = 1;
9908     case M_ULD:
9909       s = "ldl";
9910       s2 = "ldr";
9911       off12 = mips_opts.micromips;
9912       off = 7;
9913       goto uld_st;
9914     case M_USH_A:
9915       ab = 1;
9916     case M_USH:
9917       s = "sb";
9918       s2 = "sb";
9919       off = 1;
9920       ust = 1;
9921       goto uld_st;
9922     case M_USW_A:
9923       ab = 1;
9924     case M_USW:
9925       s = "swl";
9926       s2 = "swr";
9927       off12 = mips_opts.micromips;
9928       off = 3;
9929       ust = 1;
9930       goto uld_st;
9931     case M_USD_A:
9932       ab = 1;
9933     case M_USD:
9934       s = "sdl";
9935       s2 = "sdr";
9936       off12 = mips_opts.micromips;
9937       off = 7;
9938       ust = 1;
9939 
9940     uld_st:
9941       if (!ab && offset_expr.X_add_number >= 0x8000 - off)
9942 	as_bad (_("Operand overflow"));
9943 
9944       ep = &offset_expr;
9945       expr1.X_add_number = 0;
9946       if (ab)
9947 	{
9948 	  used_at = 1;
9949 	  tempreg = AT;
9950 	  load_address (tempreg, ep, &used_at);
9951 	  if (breg != 0)
9952 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9953 			 tempreg, tempreg, breg);
9954 	  breg = tempreg;
9955 	  tempreg = treg;
9956 	  ep = &expr1;
9957 	}
9958       else if (off12
9959 	       && (offset_expr.X_op != O_constant
9960 		   || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
9961 		   || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
9962 	{
9963 	  used_at = 1;
9964 	  tempreg = AT;
9965 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
9966 		       -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
9967 	  breg = tempreg;
9968 	  tempreg = treg;
9969 	  ep = &expr1;
9970 	}
9971       else if (!ust && treg == breg)
9972 	{
9973 	  used_at = 1;
9974 	  tempreg = AT;
9975 	}
9976       else
9977 	tempreg = treg;
9978 
9979       if (off == 1)
9980 	goto ulh_sh;
9981 
9982       if (!target_big_endian)
9983 	ep->X_add_number += off;
9984       if (!off12)
9985 	macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9986       else
9987 	macro_build (NULL, s, "t,~(b)",
9988 		     tempreg, (unsigned long) ep->X_add_number, breg);
9989 
9990       if (!target_big_endian)
9991 	ep->X_add_number -= off;
9992       else
9993 	ep->X_add_number += off;
9994       if (!off12)
9995 	macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9996       else
9997 	macro_build (NULL, s2, "t,~(b)",
9998 		     tempreg, (unsigned long) ep->X_add_number, breg);
9999 
10000       /* If necessary, move the result in tempreg to the final destination.  */
10001       if (!ust && treg != tempreg)
10002         {
10003 	  /* Protect second load's delay slot.  */
10004 	  load_delay_nop ();
10005 	  move_register (treg, tempreg);
10006 	}
10007       break;
10008 
10009     ulh_sh:
10010       used_at = 1;
10011       if (target_big_endian == ust)
10012 	ep->X_add_number += off;
10013       tempreg = ust || ab ? treg : AT;
10014       macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10015 
10016       /* For halfword transfers we need a temporary register to shuffle
10017          bytes.  Unfortunately for M_USH_A we have none available before
10018          the next store as AT holds the base address.  We deal with this
10019          case by clobbering TREG and then restoring it as with ULH.  */
10020       tempreg = ust == ab ? treg : AT;
10021       if (ust)
10022 	macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10023 
10024       if (target_big_endian == ust)
10025 	ep->X_add_number -= off;
10026       else
10027 	ep->X_add_number += off;
10028       macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10029 
10030       /* For M_USH_A re-retrieve the LSB.  */
10031       if (ust && ab)
10032 	{
10033 	  if (target_big_endian)
10034 	    ep->X_add_number += off;
10035 	  else
10036 	    ep->X_add_number -= off;
10037 	  macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10038 	}
10039       /* For ULH and M_USH_A OR the LSB in.  */
10040       if (!ust || ab)
10041 	{
10042 	  tempreg = !ab ? AT : treg;
10043 	  macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10044 	  macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10045 	}
10046       break;
10047 
10048     default:
10049       /* FIXME: Check if this is one of the itbl macros, since they
10050 	 are added dynamically.  */
10051       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10052       break;
10053     }
10054   if (!mips_opts.at && used_at)
10055     as_bad (_("Macro used $at after \".set noat\""));
10056 }
10057 
10058 /* Implement macros in mips16 mode.  */
10059 
10060 static void
10061 mips16_macro (struct mips_cl_insn *ip)
10062 {
10063   int mask;
10064   int xreg, yreg, zreg, tmp;
10065   expressionS expr1;
10066   int dbl;
10067   const char *s, *s2, *s3;
10068 
10069   mask = ip->insn_mo->mask;
10070 
10071   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10072   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10073   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
10074 
10075   expr1.X_op = O_constant;
10076   expr1.X_op_symbol = NULL;
10077   expr1.X_add_symbol = NULL;
10078   expr1.X_add_number = 1;
10079 
10080   dbl = 0;
10081 
10082   switch (mask)
10083     {
10084     default:
10085       internalError ();
10086 
10087     case M_DDIV_3:
10088       dbl = 1;
10089     case M_DIV_3:
10090       s = "mflo";
10091       goto do_div3;
10092     case M_DREM_3:
10093       dbl = 1;
10094     case M_REM_3:
10095       s = "mfhi";
10096     do_div3:
10097       start_noreorder ();
10098       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10099       expr1.X_add_number = 2;
10100       macro_build (&expr1, "bnez", "x,p", yreg);
10101       macro_build (NULL, "break", "6", 7);
10102 
10103       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10104          since that causes an overflow.  We should do that as well,
10105          but I don't see how to do the comparisons without a temporary
10106          register.  */
10107       end_noreorder ();
10108       macro_build (NULL, s, "x", zreg);
10109       break;
10110 
10111     case M_DIVU_3:
10112       s = "divu";
10113       s2 = "mflo";
10114       goto do_divu3;
10115     case M_REMU_3:
10116       s = "divu";
10117       s2 = "mfhi";
10118       goto do_divu3;
10119     case M_DDIVU_3:
10120       s = "ddivu";
10121       s2 = "mflo";
10122       goto do_divu3;
10123     case M_DREMU_3:
10124       s = "ddivu";
10125       s2 = "mfhi";
10126     do_divu3:
10127       start_noreorder ();
10128       macro_build (NULL, s, "0,x,y", xreg, yreg);
10129       expr1.X_add_number = 2;
10130       macro_build (&expr1, "bnez", "x,p", yreg);
10131       macro_build (NULL, "break", "6", 7);
10132       end_noreorder ();
10133       macro_build (NULL, s2, "x", zreg);
10134       break;
10135 
10136     case M_DMUL:
10137       dbl = 1;
10138     case M_MUL:
10139       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10140       macro_build (NULL, "mflo", "x", zreg);
10141       break;
10142 
10143     case M_DSUBU_I:
10144       dbl = 1;
10145       goto do_subu;
10146     case M_SUBU_I:
10147     do_subu:
10148       if (imm_expr.X_op != O_constant)
10149 	as_bad (_("Unsupported large constant"));
10150       imm_expr.X_add_number = -imm_expr.X_add_number;
10151       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10152       break;
10153 
10154     case M_SUBU_I_2:
10155       if (imm_expr.X_op != O_constant)
10156 	as_bad (_("Unsupported large constant"));
10157       imm_expr.X_add_number = -imm_expr.X_add_number;
10158       macro_build (&imm_expr, "addiu", "x,k", xreg);
10159       break;
10160 
10161     case M_DSUBU_I_2:
10162       if (imm_expr.X_op != O_constant)
10163 	as_bad (_("Unsupported large constant"));
10164       imm_expr.X_add_number = -imm_expr.X_add_number;
10165       macro_build (&imm_expr, "daddiu", "y,j", yreg);
10166       break;
10167 
10168     case M_BEQ:
10169       s = "cmp";
10170       s2 = "bteqz";
10171       goto do_branch;
10172     case M_BNE:
10173       s = "cmp";
10174       s2 = "btnez";
10175       goto do_branch;
10176     case M_BLT:
10177       s = "slt";
10178       s2 = "btnez";
10179       goto do_branch;
10180     case M_BLTU:
10181       s = "sltu";
10182       s2 = "btnez";
10183       goto do_branch;
10184     case M_BLE:
10185       s = "slt";
10186       s2 = "bteqz";
10187       goto do_reverse_branch;
10188     case M_BLEU:
10189       s = "sltu";
10190       s2 = "bteqz";
10191       goto do_reverse_branch;
10192     case M_BGE:
10193       s = "slt";
10194       s2 = "bteqz";
10195       goto do_branch;
10196     case M_BGEU:
10197       s = "sltu";
10198       s2 = "bteqz";
10199       goto do_branch;
10200     case M_BGT:
10201       s = "slt";
10202       s2 = "btnez";
10203       goto do_reverse_branch;
10204     case M_BGTU:
10205       s = "sltu";
10206       s2 = "btnez";
10207 
10208     do_reverse_branch:
10209       tmp = xreg;
10210       xreg = yreg;
10211       yreg = tmp;
10212 
10213     do_branch:
10214       macro_build (NULL, s, "x,y", xreg, yreg);
10215       macro_build (&offset_expr, s2, "p");
10216       break;
10217 
10218     case M_BEQ_I:
10219       s = "cmpi";
10220       s2 = "bteqz";
10221       s3 = "x,U";
10222       goto do_branch_i;
10223     case M_BNE_I:
10224       s = "cmpi";
10225       s2 = "btnez";
10226       s3 = "x,U";
10227       goto do_branch_i;
10228     case M_BLT_I:
10229       s = "slti";
10230       s2 = "btnez";
10231       s3 = "x,8";
10232       goto do_branch_i;
10233     case M_BLTU_I:
10234       s = "sltiu";
10235       s2 = "btnez";
10236       s3 = "x,8";
10237       goto do_branch_i;
10238     case M_BLE_I:
10239       s = "slti";
10240       s2 = "btnez";
10241       s3 = "x,8";
10242       goto do_addone_branch_i;
10243     case M_BLEU_I:
10244       s = "sltiu";
10245       s2 = "btnez";
10246       s3 = "x,8";
10247       goto do_addone_branch_i;
10248     case M_BGE_I:
10249       s = "slti";
10250       s2 = "bteqz";
10251       s3 = "x,8";
10252       goto do_branch_i;
10253     case M_BGEU_I:
10254       s = "sltiu";
10255       s2 = "bteqz";
10256       s3 = "x,8";
10257       goto do_branch_i;
10258     case M_BGT_I:
10259       s = "slti";
10260       s2 = "bteqz";
10261       s3 = "x,8";
10262       goto do_addone_branch_i;
10263     case M_BGTU_I:
10264       s = "sltiu";
10265       s2 = "bteqz";
10266       s3 = "x,8";
10267 
10268     do_addone_branch_i:
10269       if (imm_expr.X_op != O_constant)
10270 	as_bad (_("Unsupported large constant"));
10271       ++imm_expr.X_add_number;
10272 
10273     do_branch_i:
10274       macro_build (&imm_expr, s, s3, xreg);
10275       macro_build (&offset_expr, s2, "p");
10276       break;
10277 
10278     case M_ABS:
10279       expr1.X_add_number = 0;
10280       macro_build (&expr1, "slti", "x,8", yreg);
10281       if (xreg != yreg)
10282 	move_register (xreg, yreg);
10283       expr1.X_add_number = 2;
10284       macro_build (&expr1, "bteqz", "p");
10285       macro_build (NULL, "neg", "x,w", xreg, xreg);
10286     }
10287 }
10288 
10289 /* For consistency checking, verify that all bits are specified either
10290    by the match/mask part of the instruction definition, or by the
10291    operand list.  */
10292 static int
10293 validate_mips_insn (const struct mips_opcode *opc)
10294 {
10295   const char *p = opc->args;
10296   char c;
10297   unsigned long used_bits = opc->mask;
10298 
10299   if ((used_bits & opc->match) != opc->match)
10300     {
10301       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10302 	      opc->name, opc->args);
10303       return 0;
10304     }
10305 #define USE_BITS(mask,shift)	(used_bits |= ((mask) << (shift)))
10306   while (*p)
10307     switch (c = *p++)
10308       {
10309       case ',': break;
10310       case '(': break;
10311       case ')': break;
10312       case '+':
10313     	switch (c = *p++)
10314 	  {
10315 	  case '1': USE_BITS (OP_MASK_UDI1,     OP_SH_UDI1); 	break;
10316 	  case '2': USE_BITS (OP_MASK_UDI2,	OP_SH_UDI2); 	break;
10317 	  case '3': USE_BITS (OP_MASK_UDI3,	OP_SH_UDI3); 	break;
10318 	  case '4': USE_BITS (OP_MASK_UDI4,	OP_SH_UDI4); 	break;
10319 	  case 'A': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10320 	  case 'B': USE_BITS (OP_MASK_INSMSB,	OP_SH_INSMSB);	break;
10321 	  case 'C': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
10322 	  case 'D': USE_BITS (OP_MASK_RD,	OP_SH_RD);
10323 		    USE_BITS (OP_MASK_SEL,	OP_SH_SEL);	break;
10324 	  case 'E': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10325 	  case 'F': USE_BITS (OP_MASK_INSMSB,	OP_SH_INSMSB);	break;
10326 	  case 'G': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
10327 	  case 'H': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
10328 	  case 'I': break;
10329 	  case 't': USE_BITS (OP_MASK_RT,	OP_SH_RT);	break;
10330 	  case 'T': USE_BITS (OP_MASK_RT,	OP_SH_RT);
10331 		    USE_BITS (OP_MASK_SEL,	OP_SH_SEL);	break;
10332 	  case 'x': USE_BITS (OP_MASK_BBITIND,	OP_SH_BBITIND);	break;
10333 	  case 'X': USE_BITS (OP_MASK_BBITIND,	OP_SH_BBITIND);	break;
10334 	  case 'p': USE_BITS (OP_MASK_CINSPOS,	OP_SH_CINSPOS);	break;
10335 	  case 'P': USE_BITS (OP_MASK_CINSPOS,	OP_SH_CINSPOS);	break;
10336 	  case 'Q': USE_BITS (OP_MASK_SEQI,	OP_SH_SEQI);	break;
10337 	  case 's': USE_BITS (OP_MASK_CINSLM1,	OP_SH_CINSLM1);	break;
10338 	  case 'S': USE_BITS (OP_MASK_CINSLM1,	OP_SH_CINSLM1);	break;
10339 	  case 'z': USE_BITS (OP_MASK_RZ,	OP_SH_RZ);	break;
10340 	  case 'Z': USE_BITS (OP_MASK_FZ,	OP_SH_FZ);	break;
10341 	  case 'a': USE_BITS (OP_MASK_OFFSET_A,	OP_SH_OFFSET_A); break;
10342 	  case 'b': USE_BITS (OP_MASK_OFFSET_B,	OP_SH_OFFSET_B); break;
10343 	  case 'c': USE_BITS (OP_MASK_OFFSET_C,	OP_SH_OFFSET_C); break;
10344 
10345 	  default:
10346 	    as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10347 		    c, opc->name, opc->args);
10348 	    return 0;
10349 	  }
10350 	break;
10351       case '<': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10352       case '>':	USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10353       case 'A': break;
10354       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
10355       case 'C':	USE_BITS (OP_MASK_COPZ,		OP_SH_COPZ);	break;
10356       case 'D':	USE_BITS (OP_MASK_FD,		OP_SH_FD);	break;
10357       case 'E':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
10358       case 'F': break;
10359       case 'G':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10360       case 'H': USE_BITS (OP_MASK_SEL,		OP_SH_SEL);	break;
10361       case 'I': break;
10362       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
10363       case 'K':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10364       case 'L': break;
10365       case 'M':	USE_BITS (OP_MASK_CCC,		OP_SH_CCC);	break;
10366       case 'N':	USE_BITS (OP_MASK_BCC,		OP_SH_BCC);	break;
10367       case 'O':	USE_BITS (OP_MASK_ALN,		OP_SH_ALN);	break;
10368       case 'Q':	USE_BITS (OP_MASK_VSEL,		OP_SH_VSEL);
10369 		USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10370       case 'R':	USE_BITS (OP_MASK_FR,		OP_SH_FR);	break;
10371       case 'S':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
10372       case 'T':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10373       case 'V':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
10374       case 'W':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10375       case 'X':	USE_BITS (OP_MASK_FD,		OP_SH_FD);	break;
10376       case 'Y':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
10377       case 'Z':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
10378       case 'a':	USE_BITS (OP_MASK_TARGET,	OP_SH_TARGET);	break;
10379       case 'b':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10380       case 'c':	USE_BITS (OP_MASK_CODE,		OP_SH_CODE);	break;
10381       case 'd':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10382       case 'f': break;
10383       case 'h':	USE_BITS (OP_MASK_PREFX,	OP_SH_PREFX);	break;
10384       case 'i':	USE_BITS (OP_MASK_IMMEDIATE,	OP_SH_IMMEDIATE); break;
10385       case 'j':	USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
10386       case 'k':	USE_BITS (OP_MASK_CACHE,	OP_SH_CACHE);	break;
10387       case 'l': break;
10388       case 'o': USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
10389       case 'p':	USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
10390       case 'q':	USE_BITS (OP_MASK_CODE2,	OP_SH_CODE2);	break;
10391       case 'r': USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10392       case 's':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10393       case 't':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
10394       case 'u':	USE_BITS (OP_MASK_IMMEDIATE,	OP_SH_IMMEDIATE); break;
10395       case 'v':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10396       case 'w':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
10397       case 'x': break;
10398       case 'z': break;
10399       case 'P': USE_BITS (OP_MASK_PERFREG,	OP_SH_PERFREG);	break;
10400       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
10401 	        USE_BITS (OP_MASK_RT,           OP_SH_RT);	break;
10402       case 'e': USE_BITS (OP_MASK_VECBYTE,	OP_SH_VECBYTE);	break;
10403       case '%': USE_BITS (OP_MASK_VECALIGN,	OP_SH_VECALIGN); break;
10404       case '[': break;
10405       case ']': break;
10406       case '1':	USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
10407       case '2': USE_BITS (OP_MASK_BP,		OP_SH_BP);	break;
10408       case '3': USE_BITS (OP_MASK_SA3,  	OP_SH_SA3);	break;
10409       case '4': USE_BITS (OP_MASK_SA4,  	OP_SH_SA4);	break;
10410       case '5': USE_BITS (OP_MASK_IMM8, 	OP_SH_IMM8);	break;
10411       case '6': USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
10412       case '7': USE_BITS (OP_MASK_DSPACC,	OP_SH_DSPACC);	break;
10413       case '8': USE_BITS (OP_MASK_WRDSP,	OP_SH_WRDSP);	break;
10414       case '9': USE_BITS (OP_MASK_DSPACC_S,	OP_SH_DSPACC_S);break;
10415       case '0': USE_BITS (OP_MASK_DSPSFT,	OP_SH_DSPSFT);	break;
10416       case '\'': USE_BITS (OP_MASK_RDDSP,	OP_SH_RDDSP);	break;
10417       case ':': USE_BITS (OP_MASK_DSPSFT_7,	OP_SH_DSPSFT_7);break;
10418       case '@': USE_BITS (OP_MASK_IMM10,	OP_SH_IMM10);	break;
10419       case '!': USE_BITS (OP_MASK_MT_U,		OP_SH_MT_U);	break;
10420       case '$': USE_BITS (OP_MASK_MT_H,		OP_SH_MT_H);	break;
10421       case '*': USE_BITS (OP_MASK_MTACC_T,	OP_SH_MTACC_T);	break;
10422       case '&': USE_BITS (OP_MASK_MTACC_D,	OP_SH_MTACC_D);	break;
10423       case '\\': USE_BITS (OP_MASK_3BITPOS,	OP_SH_3BITPOS);	break;
10424       case '~': USE_BITS (OP_MASK_OFFSET12,	OP_SH_OFFSET12); break;
10425       case 'g': USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
10426       default:
10427 	as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10428 		c, opc->name, opc->args);
10429 	return 0;
10430       }
10431 #undef USE_BITS
10432   if (used_bits != 0xffffffff)
10433     {
10434       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10435 	      ~used_bits & 0xffffffff, opc->name, opc->args);
10436       return 0;
10437     }
10438   return 1;
10439 }
10440 
10441 /* For consistency checking, verify that the length implied matches the
10442    major opcode and that all bits are specified either by the match/mask
10443    part of the instruction definition, or by the operand list.  */
10444 
10445 static int
10446 validate_micromips_insn (const struct mips_opcode *opc)
10447 {
10448   unsigned long match = opc->match;
10449   unsigned long mask = opc->mask;
10450   const char *p = opc->args;
10451   unsigned long insn_bits;
10452   unsigned long used_bits;
10453   unsigned long major;
10454   unsigned int length;
10455   char e;
10456   char c;
10457 
10458   if ((mask & match) != match)
10459     {
10460       as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10461 	      opc->name, opc->args);
10462       return 0;
10463     }
10464   length = micromips_insn_length (opc);
10465   if (length != 2 && length != 4)
10466     {
10467       as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10468 		"%s %s"), length, opc->name, opc->args);
10469       return 0;
10470     }
10471   major = match >> (10 + 8 * (length - 2));
10472   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10473       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10474     {
10475       as_bad (_("Internal error: bad microMIPS opcode "
10476 		"(opcode/length mismatch): %s %s"), opc->name, opc->args);
10477       return 0;
10478     }
10479 
10480   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
10481   insn_bits = 1 << 4 * length;
10482   insn_bits <<= 4 * length;
10483   insn_bits -= 1;
10484   used_bits = mask;
10485 #define USE_BITS(field) \
10486   (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10487   while (*p)
10488     switch (c = *p++)
10489       {
10490       case ',': break;
10491       case '(': break;
10492       case ')': break;
10493       case '+':
10494 	e = c;
10495     	switch (c = *p++)
10496 	  {
10497 	  case 'A': USE_BITS (EXTLSB);	break;
10498 	  case 'B': USE_BITS (INSMSB);	break;
10499 	  case 'C': USE_BITS (EXTMSBD);	break;
10500 	  case 'D': USE_BITS (RS);	USE_BITS (SEL);	break;
10501 	  case 'E': USE_BITS (EXTLSB);	break;
10502 	  case 'F': USE_BITS (INSMSB);	break;
10503 	  case 'G': USE_BITS (EXTMSBD);	break;
10504 	  case 'H': USE_BITS (EXTMSBD);	break;
10505 	  default:
10506 	    as_bad (_("Internal error: bad mips opcode "
10507 		      "(unknown extension operand type `%c%c'): %s %s"),
10508 		    e, c, opc->name, opc->args);
10509 	    return 0;
10510 	  }
10511 	break;
10512       case 'm':
10513 	e = c;
10514     	switch (c = *p++)
10515 	  {
10516 	  case 'A': USE_BITS (IMMA);	break;
10517 	  case 'B': USE_BITS (IMMB);	break;
10518 	  case 'C': USE_BITS (IMMC);	break;
10519 	  case 'D': USE_BITS (IMMD);	break;
10520 	  case 'E': USE_BITS (IMME);	break;
10521 	  case 'F': USE_BITS (IMMF);	break;
10522 	  case 'G': USE_BITS (IMMG);	break;
10523 	  case 'H': USE_BITS (IMMH);	break;
10524 	  case 'I': USE_BITS (IMMI);	break;
10525 	  case 'J': USE_BITS (IMMJ);	break;
10526 	  case 'L': USE_BITS (IMML);	break;
10527 	  case 'M': USE_BITS (IMMM);	break;
10528 	  case 'N': USE_BITS (IMMN);	break;
10529 	  case 'O': USE_BITS (IMMO);	break;
10530 	  case 'P': USE_BITS (IMMP);	break;
10531 	  case 'Q': USE_BITS (IMMQ);	break;
10532 	  case 'U': USE_BITS (IMMU);	break;
10533 	  case 'W': USE_BITS (IMMW);	break;
10534 	  case 'X': USE_BITS (IMMX);	break;
10535 	  case 'Y': USE_BITS (IMMY);	break;
10536 	  case 'Z': break;
10537 	  case 'a': break;
10538 	  case 'b': USE_BITS (MB);	break;
10539 	  case 'c': USE_BITS (MC);	break;
10540 	  case 'd': USE_BITS (MD);	break;
10541 	  case 'e': USE_BITS (ME);	break;
10542 	  case 'f': USE_BITS (MF);	break;
10543 	  case 'g': USE_BITS (MG);	break;
10544 	  case 'h': USE_BITS (MH);	break;
10545 	  case 'i': USE_BITS (MI);	break;
10546 	  case 'j': USE_BITS (MJ);	break;
10547 	  case 'l': USE_BITS (ML);	break;
10548 	  case 'm': USE_BITS (MM);	break;
10549 	  case 'n': USE_BITS (MN);	break;
10550 	  case 'p': USE_BITS (MP);	break;
10551 	  case 'q': USE_BITS (MQ);	break;
10552 	  case 'r': break;
10553 	  case 's': break;
10554 	  case 't': break;
10555 	  case 'x': break;
10556 	  case 'y': break;
10557 	  case 'z': break;
10558 	  default:
10559 	    as_bad (_("Internal error: bad mips opcode "
10560 		      "(unknown extension operand type `%c%c'): %s %s"),
10561 		    e, c, opc->name, opc->args);
10562 	    return 0;
10563 	  }
10564 	break;
10565       case '.': USE_BITS (OFFSET10);	break;
10566       case '1': USE_BITS (STYPE);	break;
10567       case '2': USE_BITS (BP);		break;
10568       case '3': USE_BITS (SA3);		break;
10569       case '4': USE_BITS (SA4);		break;
10570       case '5': USE_BITS (IMM8);	break;
10571       case '6': USE_BITS (RS);		break;
10572       case '7': USE_BITS (DSPACC);	break;
10573       case '8': USE_BITS (WRDSP);	break;
10574       case '0': USE_BITS (DSPSFT);	break;
10575       case '<': USE_BITS (SHAMT);	break;
10576       case '>': USE_BITS (SHAMT);	break;
10577       case '@': USE_BITS (IMM10);	break;
10578       case 'B': USE_BITS (CODE10);	break;
10579       case 'C': USE_BITS (COPZ);	break;
10580       case 'D': USE_BITS (FD);		break;
10581       case 'E': USE_BITS (RT);		break;
10582       case 'G': USE_BITS (RS);		break;
10583       case 'H': USE_BITS (SEL);		break;
10584       case 'K': USE_BITS (RS);		break;
10585       case 'M': USE_BITS (CCC);		break;
10586       case 'N': USE_BITS (BCC);		break;
10587       case 'R': USE_BITS (FR);		break;
10588       case 'S': USE_BITS (FS);		break;
10589       case 'T': USE_BITS (FT);		break;
10590       case 'V': USE_BITS (FS);		break;
10591       case '\\': USE_BITS (3BITPOS);	break;
10592       case '^': USE_BITS (RD);		break;
10593       case 'a': USE_BITS (TARGET);	break;
10594       case 'b': USE_BITS (RS);		break;
10595       case 'c': USE_BITS (CODE);	break;
10596       case 'd': USE_BITS (RD);		break;
10597       case 'h': USE_BITS (PREFX);	break;
10598       case 'i': USE_BITS (IMMEDIATE);	break;
10599       case 'j': USE_BITS (DELTA);	break;
10600       case 'k': USE_BITS (CACHE);	break;
10601       case 'n': USE_BITS (RT);		break;
10602       case 'o': USE_BITS (DELTA);	break;
10603       case 'p': USE_BITS (DELTA);	break;
10604       case 'q': USE_BITS (CODE2);	break;
10605       case 'r': USE_BITS (RS);		break;
10606       case 's': USE_BITS (RS);		break;
10607       case 't': USE_BITS (RT);		break;
10608       case 'u': USE_BITS (IMMEDIATE);	break;
10609       case 'v': USE_BITS (RS);		break;
10610       case 'w': USE_BITS (RT);		break;
10611       case 'y': USE_BITS (RS3);		break;
10612       case 'z': break;
10613       case '|': USE_BITS (TRAP);	break;
10614       case '~': USE_BITS (OFFSET12);	break;
10615       default:
10616 	as_bad (_("Internal error: bad microMIPS opcode "
10617 		  "(unknown operand type `%c'): %s %s"),
10618 		c, opc->name, opc->args);
10619 	return 0;
10620       }
10621 #undef USE_BITS
10622   if (used_bits != insn_bits)
10623     {
10624       if (~used_bits & insn_bits)
10625 	as_bad (_("Internal error: bad microMIPS opcode "
10626 		  "(bits 0x%lx undefined): %s %s"),
10627 		~used_bits & insn_bits, opc->name, opc->args);
10628       if (used_bits & ~insn_bits)
10629 	as_bad (_("Internal error: bad microMIPS opcode "
10630 		  "(bits 0x%lx defined): %s %s"),
10631 		used_bits & ~insn_bits, opc->name, opc->args);
10632       return 0;
10633     }
10634   return 1;
10635 }
10636 
10637 /* UDI immediates.  */
10638 struct mips_immed {
10639   char		type;
10640   unsigned int	shift;
10641   unsigned long	mask;
10642   const char *	desc;
10643 };
10644 
10645 static const struct mips_immed mips_immed[] = {
10646   { '1',	OP_SH_UDI1,	OP_MASK_UDI1,		0},
10647   { '2',	OP_SH_UDI2,	OP_MASK_UDI2,		0},
10648   { '3',	OP_SH_UDI3,	OP_MASK_UDI3,		0},
10649   { '4',	OP_SH_UDI4,	OP_MASK_UDI4,		0},
10650   { 0,0,0,0 }
10651 };
10652 
10653 /* Check whether an odd floating-point register is allowed.  */
10654 static int
10655 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10656 {
10657   const char *s = insn->name;
10658 
10659   if (insn->pinfo == INSN_MACRO)
10660     /* Let a macro pass, we'll catch it later when it is expanded.  */
10661     return 1;
10662 
10663   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
10664     {
10665       /* Allow odd registers for single-precision ops.  */
10666       switch (insn->pinfo & (FP_S | FP_D))
10667 	{
10668 	case FP_S:
10669 	case 0:
10670 	  return 1;	/* both single precision - ok */
10671 	case FP_D:
10672 	  return 0;	/* both double precision - fail */
10673 	default:
10674 	  break;
10675 	}
10676 
10677       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
10678       s = strchr (insn->name, '.');
10679       if (argnum == 2)
10680 	s = s != NULL ? strchr (s + 1, '.') : NULL;
10681       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10682     }
10683 
10684   /* Single-precision coprocessor loads and moves are OK too.  */
10685   if ((insn->pinfo & FP_S)
10686       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10687 			 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10688     return 1;
10689 
10690   return 0;
10691 }
10692 
10693 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10694    taking bits from BIT up.  */
10695 static int
10696 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10697 {
10698   return (ep->X_op == O_constant
10699 	  && (ep->X_add_number & ((1 << bit) - 1)) == 0
10700 	  && ep->X_add_number >= min << bit
10701 	  && ep->X_add_number < max << bit);
10702 }
10703 
10704 /* This routine assembles an instruction into its binary format.  As a
10705    side effect, it sets one of the global variables imm_reloc or
10706    offset_reloc to the type of relocation to do if one of the operands
10707    is an address expression.  */
10708 
10709 static void
10710 mips_ip (char *str, struct mips_cl_insn *ip)
10711 {
10712   bfd_boolean wrong_delay_slot_insns = FALSE;
10713   bfd_boolean need_delay_slot_ok = TRUE;
10714   struct mips_opcode *firstinsn = NULL;
10715   const struct mips_opcode *past;
10716   struct hash_control *hash;
10717   char *s;
10718   const char *args;
10719   char c = 0;
10720   struct mips_opcode *insn;
10721   char *argsStart;
10722   unsigned int regno;
10723   unsigned int lastregno;
10724   unsigned int destregno = 0;
10725   unsigned int lastpos = 0;
10726   unsigned int limlo, limhi;
10727   char *s_reset;
10728   offsetT min_range, max_range;
10729   long opend;
10730   char *name;
10731   int argnum;
10732   unsigned int rtype;
10733   char *dot;
10734   long end;
10735 
10736   insn_error = NULL;
10737 
10738   if (mips_opts.micromips)
10739     {
10740       hash = micromips_op_hash;
10741       past = &micromips_opcodes[bfd_micromips_num_opcodes];
10742     }
10743   else
10744     {
10745       hash = op_hash;
10746       past = &mips_opcodes[NUMOPCODES];
10747     }
10748   forced_insn_length = 0;
10749   insn = NULL;
10750 
10751   /* We first try to match an instruction up to a space or to the end.  */
10752   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10753     continue;
10754 
10755   /* Make a copy of the instruction so that we can fiddle with it.  */
10756   name = alloca (end + 1);
10757   memcpy (name, str, end);
10758   name[end] = '\0';
10759 
10760   for (;;)
10761     {
10762       insn = (struct mips_opcode *) hash_find (hash, name);
10763 
10764       if (insn != NULL || !mips_opts.micromips)
10765 	break;
10766       if (forced_insn_length)
10767 	break;
10768 
10769       /* See if there's an instruction size override suffix,
10770          either `16' or `32', at the end of the mnemonic proper,
10771          that defines the operation, i.e. before the first `.'
10772          character if any.  Strip it and retry.  */
10773       dot = strchr (name, '.');
10774       opend = dot != NULL ? dot - name : end;
10775       if (opend < 3)
10776 	break;
10777       if (name[opend - 2] == '1' && name[opend - 1] == '6')
10778 	forced_insn_length = 2;
10779       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10780 	forced_insn_length = 4;
10781       else
10782 	break;
10783       memcpy (name + opend - 2, name + opend, end - opend + 1);
10784     }
10785   if (insn == NULL)
10786     {
10787       insn_error = _("Unrecognized opcode");
10788       return;
10789     }
10790 
10791   /* For microMIPS instructions placed in a fixed-length branch delay slot
10792      we make up to two passes over the relevant fragment of the opcode
10793      table.  First we try instructions that meet the delay slot's length
10794      requirement.  If none matched, then we retry with the remaining ones
10795      and if one matches, then we use it and then issue an appropriate
10796      warning later on.  */
10797   argsStart = s = str + end;
10798   for (;;)
10799     {
10800       bfd_boolean delay_slot_ok;
10801       bfd_boolean size_ok;
10802       bfd_boolean ok;
10803 
10804       gas_assert (strcmp (insn->name, name) == 0);
10805 
10806       ok = is_opcode_valid (insn);
10807       size_ok = is_size_valid (insn);
10808       delay_slot_ok = is_delay_slot_valid (insn);
10809       if (!delay_slot_ok && !wrong_delay_slot_insns)
10810 	{
10811 	  firstinsn = insn;
10812 	  wrong_delay_slot_insns = TRUE;
10813 	}
10814       if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10815 	{
10816 	  static char buf[256];
10817 
10818 	  if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
10819 	    {
10820 	      ++insn;
10821 	      continue;
10822 	    }
10823 	  if (wrong_delay_slot_insns && need_delay_slot_ok)
10824 	    {
10825 	      gas_assert (firstinsn);
10826 	      need_delay_slot_ok = FALSE;
10827 	      past = insn + 1;
10828 	      insn = firstinsn;
10829 	      continue;
10830 	    }
10831 
10832 	  if (insn_error)
10833 	    return;
10834 
10835 	  if (!ok)
10836 	    sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
10837 		     mips_cpu_info_from_arch (mips_opts.arch)->name,
10838 		     mips_cpu_info_from_isa (mips_opts.isa)->name);
10839 	  else
10840 	    sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10841 		     8 * forced_insn_length);
10842 	  insn_error = buf;
10843 
10844 	  return;
10845 	}
10846 
10847       create_insn (ip, insn);
10848       insn_error = NULL;
10849       argnum = 1;
10850       lastregno = 0xffffffff;
10851       for (args = insn->args;; ++args)
10852 	{
10853 	  int is_mdmx;
10854 
10855 	  s += strspn (s, " \t");
10856 	  is_mdmx = 0;
10857 	  switch (*args)
10858 	    {
10859 	    case '\0':		/* end of args */
10860 	      if (*s == '\0')
10861 		return;
10862 	      break;
10863 
10864 	    case '2':
10865 	      /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
10866 	         code) or 14 (for microMIPS code).  */
10867 	      my_getExpression (&imm_expr, s);
10868 	      check_absolute_expr (ip, &imm_expr);
10869 	      if ((unsigned long) imm_expr.X_add_number != 1
10870 		  && (unsigned long) imm_expr.X_add_number != 3)
10871 		{
10872 		  as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10873 			  (unsigned long) imm_expr.X_add_number);
10874 		}
10875 	      INSERT_OPERAND (mips_opts.micromips,
10876 			      BP, *ip, imm_expr.X_add_number);
10877 	      imm_expr.X_op = O_absent;
10878 	      s = expr_end;
10879 	      continue;
10880 
10881 	    case '3':
10882 	      /* DSP 3-bit unsigned immediate in bit 13 (for standard MIPS
10883 	         code) or 21 (for microMIPS code).  */
10884 	      {
10885 		unsigned long mask = (mips_opts.micromips
10886 				      ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
10887 
10888 		my_getExpression (&imm_expr, s);
10889 		check_absolute_expr (ip, &imm_expr);
10890 		if ((unsigned long) imm_expr.X_add_number > mask)
10891 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10892 			  mask, (unsigned long) imm_expr.X_add_number);
10893 		INSERT_OPERAND (mips_opts.micromips,
10894 				SA3, *ip, imm_expr.X_add_number);
10895 		imm_expr.X_op = O_absent;
10896 		s = expr_end;
10897 	      }
10898 	      continue;
10899 
10900 	    case '4':
10901 	      /* DSP 4-bit unsigned immediate in bit 12 (for standard MIPS
10902 	         code) or 21 (for microMIPS code).  */
10903 	      {
10904 		unsigned long mask = (mips_opts.micromips
10905 				      ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
10906 
10907 		my_getExpression (&imm_expr, s);
10908 		check_absolute_expr (ip, &imm_expr);
10909 		if ((unsigned long) imm_expr.X_add_number > mask)
10910 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10911 			  mask, (unsigned long) imm_expr.X_add_number);
10912 		INSERT_OPERAND (mips_opts.micromips,
10913 				SA4, *ip, imm_expr.X_add_number);
10914 		imm_expr.X_op = O_absent;
10915 		s = expr_end;
10916 	      }
10917 	      continue;
10918 
10919 	    case '5':
10920 	      /* DSP 8-bit unsigned immediate in bit 13 (for standard MIPS
10921 	         code) or 16 (for microMIPS code).  */
10922 	      {
10923 		unsigned long mask = (mips_opts.micromips
10924 				      ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
10925 
10926 		my_getExpression (&imm_expr, s);
10927 		check_absolute_expr (ip, &imm_expr);
10928 		if ((unsigned long) imm_expr.X_add_number > mask)
10929 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10930 			  mask, (unsigned long) imm_expr.X_add_number);
10931 		INSERT_OPERAND (mips_opts.micromips,
10932 				IMM8, *ip, imm_expr.X_add_number);
10933 		imm_expr.X_op = O_absent;
10934 		s = expr_end;
10935 	      }
10936 	      continue;
10937 
10938 	    case '6':
10939 	      /* DSP 5-bit unsigned immediate in bit 16 (for standard MIPS
10940 	         code) or 21 (for microMIPS code).  */
10941 	      {
10942 		unsigned long mask = (mips_opts.micromips
10943 				      ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
10944 
10945 		my_getExpression (&imm_expr, s);
10946 		check_absolute_expr (ip, &imm_expr);
10947 		if ((unsigned long) imm_expr.X_add_number > mask)
10948 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10949 			  mask, (unsigned long) imm_expr.X_add_number);
10950 		INSERT_OPERAND (mips_opts.micromips,
10951 				RS, *ip, imm_expr.X_add_number);
10952 		imm_expr.X_op = O_absent;
10953 		s = expr_end;
10954 	      }
10955 	      continue;
10956 
10957 	    case '7': /* Four DSP accumulators in bits 11,12.  */
10958 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
10959 		  && s[3] >= '0' && s[3] <= '3')
10960 		{
10961 		  regno = s[3] - '0';
10962 		  s += 4;
10963 		  INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
10964 		  continue;
10965 		}
10966 	      else
10967 		as_bad (_("Invalid dsp acc register"));
10968 	      break;
10969 
10970 	    case '8':
10971 	      /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
10972 	         code) or 14 (for microMIPS code).  */
10973 	      {
10974 		unsigned long mask = (mips_opts.micromips
10975 				      ? MICROMIPSOP_MASK_WRDSP
10976 				      : OP_MASK_WRDSP);
10977 
10978 		my_getExpression (&imm_expr, s);
10979 		check_absolute_expr (ip, &imm_expr);
10980 		if ((unsigned long) imm_expr.X_add_number > mask)
10981 		  as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
10982 			  mask, (unsigned long) imm_expr.X_add_number);
10983 		INSERT_OPERAND (mips_opts.micromips,
10984 				WRDSP, *ip, imm_expr.X_add_number);
10985 		imm_expr.X_op = O_absent;
10986 		s = expr_end;
10987 	      }
10988 	      continue;
10989 
10990 	    case '9': /* Four DSP accumulators in bits 21,22.  */
10991 	      gas_assert (!mips_opts.micromips);
10992 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
10993 		  && s[3] >= '0' && s[3] <= '3')
10994 		{
10995 		  regno = s[3] - '0';
10996 		  s += 4;
10997 		  INSERT_OPERAND (0, DSPACC_S, *ip, regno);
10998 		  continue;
10999 		}
11000 	      else
11001 		as_bad (_("Invalid dsp acc register"));
11002 	      break;
11003 
11004 	    case '0':
11005 	      /* DSP 6-bit signed immediate in bit 16 (for standard MIPS
11006 	         code) or 20 (for microMIPS code).  */
11007 	      {
11008 		long mask = (mips_opts.micromips
11009 			     ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11010 
11011 		my_getExpression (&imm_expr, s);
11012 		check_absolute_expr (ip, &imm_expr);
11013 		min_range = -((mask + 1) >> 1);
11014 		max_range = ((mask + 1) >> 1) - 1;
11015 		if (imm_expr.X_add_number < min_range
11016 		    || imm_expr.X_add_number > max_range)
11017 		  as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11018 			  (long) min_range, (long) max_range,
11019 			  (long) imm_expr.X_add_number);
11020 		INSERT_OPERAND (mips_opts.micromips,
11021 				DSPSFT, *ip, imm_expr.X_add_number);
11022 		imm_expr.X_op = O_absent;
11023 		s = expr_end;
11024 	      }
11025 	      continue;
11026 
11027 	    case '\'': /* DSP 6-bit unsigned immediate in bit 16.  */
11028 	      gas_assert (!mips_opts.micromips);
11029 	      my_getExpression (&imm_expr, s);
11030 	      check_absolute_expr (ip, &imm_expr);
11031 	      if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11032 		{
11033 		  as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11034 			  OP_MASK_RDDSP,
11035 			  (unsigned long) imm_expr.X_add_number);
11036 		}
11037 	      INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
11038 	      imm_expr.X_op = O_absent;
11039 	      s = expr_end;
11040 	      continue;
11041 
11042 	    case ':': /* DSP 7-bit signed immediate in bit 19.  */
11043 	      gas_assert (!mips_opts.micromips);
11044 	      my_getExpression (&imm_expr, s);
11045 	      check_absolute_expr (ip, &imm_expr);
11046 	      min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11047 	      max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11048 	      if (imm_expr.X_add_number < min_range ||
11049 		  imm_expr.X_add_number > max_range)
11050 		{
11051 		  as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11052 			  (long) min_range, (long) max_range,
11053 			  (long) imm_expr.X_add_number);
11054 		}
11055 	      INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
11056 	      imm_expr.X_op = O_absent;
11057 	      s = expr_end;
11058 	      continue;
11059 
11060 	    case '@': /* DSP 10-bit signed immediate in bit 16.  */
11061 	      {
11062 		long mask = (mips_opts.micromips
11063 			     ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11064 
11065 		my_getExpression (&imm_expr, s);
11066 		check_absolute_expr (ip, &imm_expr);
11067 		min_range = -((mask + 1) >> 1);
11068 		max_range = ((mask + 1) >> 1) - 1;
11069 		if (imm_expr.X_add_number < min_range
11070 		    || imm_expr.X_add_number > max_range)
11071 		  as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11072 			  (long) min_range, (long) max_range,
11073 			  (long) imm_expr.X_add_number);
11074 		INSERT_OPERAND (mips_opts.micromips,
11075 				IMM10, *ip, imm_expr.X_add_number);
11076 		imm_expr.X_op = O_absent;
11077 		s = expr_end;
11078 	      }
11079 	      continue;
11080 
11081 	    case '^': /* DSP 5-bit unsigned immediate in bit 11.  */
11082 	      gas_assert (mips_opts.micromips);
11083 	      my_getExpression (&imm_expr, s);
11084 	      check_absolute_expr (ip, &imm_expr);
11085 	      if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11086 		as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11087 			MICROMIPSOP_MASK_RD,
11088 			(unsigned long) imm_expr.X_add_number);
11089 	      INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
11090 	      imm_expr.X_op = O_absent;
11091 	      s = expr_end;
11092 	      continue;
11093 
11094             case '!': /* MT usermode flag bit.  */
11095 	      gas_assert (!mips_opts.micromips);
11096 	      my_getExpression (&imm_expr, s);
11097 	      check_absolute_expr (ip, &imm_expr);
11098 	      if (imm_expr.X_add_number & ~OP_MASK_MT_U)
11099 		as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11100 			(unsigned long) imm_expr.X_add_number);
11101 	      INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
11102 	      imm_expr.X_op = O_absent;
11103 	      s = expr_end;
11104 	      continue;
11105 
11106             case '$': /* MT load high flag bit.  */
11107 	      gas_assert (!mips_opts.micromips);
11108 	      my_getExpression (&imm_expr, s);
11109 	      check_absolute_expr (ip, &imm_expr);
11110 	      if (imm_expr.X_add_number & ~OP_MASK_MT_H)
11111 		as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11112 			(unsigned long) imm_expr.X_add_number);
11113 	      INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
11114 	      imm_expr.X_op = O_absent;
11115 	      s = expr_end;
11116 	      continue;
11117 
11118 	    case '*': /* Four DSP accumulators in bits 18,19.  */
11119 	      gas_assert (!mips_opts.micromips);
11120 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11121 		  s[3] >= '0' && s[3] <= '3')
11122 		{
11123 		  regno = s[3] - '0';
11124 		  s += 4;
11125 		  INSERT_OPERAND (0, MTACC_T, *ip, regno);
11126 		  continue;
11127 		}
11128 	      else
11129 		as_bad (_("Invalid dsp/smartmips acc register"));
11130 	      break;
11131 
11132 	    case '&': /* Four DSP accumulators in bits 13,14.  */
11133 	      gas_assert (!mips_opts.micromips);
11134 	      if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11135 		  s[3] >= '0' && s[3] <= '3')
11136 		{
11137 		  regno = s[3] - '0';
11138 		  s += 4;
11139 		  INSERT_OPERAND (0, MTACC_D, *ip, regno);
11140 		  continue;
11141 		}
11142 	      else
11143 		as_bad (_("Invalid dsp/smartmips acc register"));
11144 	      break;
11145 
11146 	    case '\\':		/* 3-bit bit position.  */
11147 	      {
11148 		unsigned long mask = (mips_opts.micromips
11149 				      ? MICROMIPSOP_MASK_3BITPOS
11150 				      : OP_MASK_3BITPOS);
11151 
11152 		my_getExpression (&imm_expr, s);
11153 		check_absolute_expr (ip, &imm_expr);
11154 		if ((unsigned long) imm_expr.X_add_number > mask)
11155 		  as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11156 			   ip->insn_mo->name,
11157 			   mask, (unsigned long) imm_expr.X_add_number);
11158 		INSERT_OPERAND (mips_opts.micromips,
11159 				3BITPOS, *ip, imm_expr.X_add_number);
11160 		imm_expr.X_op = O_absent;
11161 		s = expr_end;
11162 	      }
11163 	      continue;
11164 
11165 	    case ',':
11166 	      ++argnum;
11167 	      if (*s++ == *args)
11168 		continue;
11169 	      s--;
11170 	      switch (*++args)
11171 		{
11172 		case 'r':
11173 		case 'v':
11174 		  INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11175 		  continue;
11176 
11177 		case 'w':
11178 		  INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11179 		  continue;
11180 
11181 		case 'W':
11182 		  gas_assert (!mips_opts.micromips);
11183 		  INSERT_OPERAND (0, FT, *ip, lastregno);
11184 		  continue;
11185 
11186 		case 'V':
11187 		  INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11188 		  continue;
11189 		}
11190 	      break;
11191 
11192 	    case '(':
11193 	      /* Handle optional base register.
11194 		 Either the base register is omitted or
11195 		 we must have a left paren.  */
11196 	      /* This is dependent on the next operand specifier
11197 		 is a base register specification.  */
11198 	      gas_assert (args[1] == 'b'
11199 			  || (mips_opts.micromips
11200 			      && args[1] == 'm'
11201 			      && (args[2] == 'l' || args[2] == 'n'
11202 				  || args[2] == 's' || args[2] == 'a')));
11203 	      if (*s == '\0' && args[1] == 'b')
11204 		return;
11205 	      /* Fall through.  */
11206 
11207 	    case ')':		/* These must match exactly.  */
11208 	      if (*s++ == *args)
11209 		continue;
11210 	      break;
11211 
11212 	    case '[':		/* These must match exactly.  */
11213 	    case ']':
11214 	      gas_assert (!mips_opts.micromips);
11215 	      if (*s++ == *args)
11216 		continue;
11217 	      break;
11218 
11219 	    case '+':		/* Opcode extension character.  */
11220 	      switch (*++args)
11221 		{
11222 		case '1':	/* UDI immediates.  */
11223 		case '2':
11224 		case '3':
11225 		case '4':
11226 		  gas_assert (!mips_opts.micromips);
11227 		  {
11228 		    const struct mips_immed *imm = mips_immed;
11229 
11230 		    while (imm->type && imm->type != *args)
11231 		      ++imm;
11232 		    if (! imm->type)
11233 		      internalError ();
11234 		    my_getExpression (&imm_expr, s);
11235 		    check_absolute_expr (ip, &imm_expr);
11236 		    if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11237 		      {
11238 		        as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11239 				 imm->desc ? imm->desc : ip->insn_mo->name,
11240 				 (unsigned long) imm_expr.X_add_number,
11241 				 (unsigned long) imm_expr.X_add_number);
11242 			imm_expr.X_add_number &= imm->mask;
11243 		      }
11244 		    ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11245 					<< imm->shift);
11246 		    imm_expr.X_op = O_absent;
11247 		    s = expr_end;
11248 		  }
11249 		  continue;
11250 
11251 		case 'A':		/* ins/ext position, becomes LSB.  */
11252 		  limlo = 0;
11253 		  limhi = 31;
11254 		  goto do_lsb;
11255 		case 'E':
11256 		  limlo = 32;
11257 		  limhi = 63;
11258 		  goto do_lsb;
11259 		do_lsb:
11260 		  my_getExpression (&imm_expr, s);
11261 		  check_absolute_expr (ip, &imm_expr);
11262 		  if ((unsigned long) imm_expr.X_add_number < limlo
11263 		      || (unsigned long) imm_expr.X_add_number > limhi)
11264 		    {
11265 		      as_bad (_("Improper position (%lu)"),
11266 			      (unsigned long) imm_expr.X_add_number);
11267 		      imm_expr.X_add_number = limlo;
11268 		    }
11269 		  lastpos = imm_expr.X_add_number;
11270 		  INSERT_OPERAND (mips_opts.micromips,
11271 				  EXTLSB, *ip, imm_expr.X_add_number);
11272 		  imm_expr.X_op = O_absent;
11273 		  s = expr_end;
11274 		  continue;
11275 
11276 		case 'B':		/* ins size, becomes MSB.  */
11277 		  limlo = 1;
11278 		  limhi = 32;
11279 		  goto do_msb;
11280 		case 'F':
11281 		  limlo = 33;
11282 		  limhi = 64;
11283 		  goto do_msb;
11284 		do_msb:
11285 		  my_getExpression (&imm_expr, s);
11286 		  check_absolute_expr (ip, &imm_expr);
11287 		  /* Check for negative input so that small negative numbers
11288 		     will not succeed incorrectly.  The checks against
11289 		     (pos+size) transitively check "size" itself,
11290 		     assuming that "pos" is reasonable.  */
11291 		  if ((long) imm_expr.X_add_number < 0
11292 		      || ((unsigned long) imm_expr.X_add_number
11293 			  + lastpos) < limlo
11294 		      || ((unsigned long) imm_expr.X_add_number
11295 			  + lastpos) > limhi)
11296 		    {
11297 		      as_bad (_("Improper insert size (%lu, position %lu)"),
11298 			      (unsigned long) imm_expr.X_add_number,
11299 			      (unsigned long) lastpos);
11300 		      imm_expr.X_add_number = limlo - lastpos;
11301 		    }
11302 		  INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11303 				  lastpos + imm_expr.X_add_number - 1);
11304 		  imm_expr.X_op = O_absent;
11305 		  s = expr_end;
11306 		  continue;
11307 
11308 		case 'C':		/* ext size, becomes MSBD.  */
11309 		  limlo = 1;
11310 		  limhi = 32;
11311 		  goto do_msbd;
11312 		case 'G':
11313 		  limlo = 33;
11314 		  limhi = 64;
11315 		  goto do_msbd;
11316 		case 'H':
11317 		  limlo = 33;
11318 		  limhi = 64;
11319 		  goto do_msbd;
11320 		do_msbd:
11321 		  my_getExpression (&imm_expr, s);
11322 		  check_absolute_expr (ip, &imm_expr);
11323 		  /* Check for negative input so that small negative numbers
11324 		     will not succeed incorrectly.  The checks against
11325 		     (pos+size) transitively check "size" itself,
11326 		     assuming that "pos" is reasonable.  */
11327 		  if ((long) imm_expr.X_add_number < 0
11328 		      || ((unsigned long) imm_expr.X_add_number
11329 			  + lastpos) < limlo
11330 		      || ((unsigned long) imm_expr.X_add_number
11331 			  + lastpos) > limhi)
11332 		    {
11333 		      as_bad (_("Improper extract size (%lu, position %lu)"),
11334 			      (unsigned long) imm_expr.X_add_number,
11335 			      (unsigned long) lastpos);
11336 		      imm_expr.X_add_number = limlo - lastpos;
11337 		    }
11338 		  INSERT_OPERAND (mips_opts.micromips,
11339 				  EXTMSBD, *ip, imm_expr.X_add_number - 1);
11340 		  imm_expr.X_op = O_absent;
11341 		  s = expr_end;
11342 		  continue;
11343 
11344 		case 'D':
11345 		  /* +D is for disassembly only; never match.  */
11346 		  break;
11347 
11348 		case 'I':
11349 		  /* "+I" is like "I", except that imm2_expr is used.  */
11350 		  my_getExpression (&imm2_expr, s);
11351 		  if (imm2_expr.X_op != O_big
11352 		      && imm2_expr.X_op != O_constant)
11353 		  insn_error = _("absolute expression required");
11354 		  if (HAVE_32BIT_GPRS)
11355 		    normalize_constant_expr (&imm2_expr);
11356 		  s = expr_end;
11357 		  continue;
11358 
11359 		case 'T': /* Coprocessor register.  */
11360 		  gas_assert (!mips_opts.micromips);
11361 		  /* +T is for disassembly only; never match.  */
11362 		  break;
11363 
11364 		case 't': /* Coprocessor register number.  */
11365 		  gas_assert (!mips_opts.micromips);
11366 		  if (s[0] == '$' && ISDIGIT (s[1]))
11367 		    {
11368 		      ++s;
11369 		      regno = 0;
11370 		      do
11371 		        {
11372 			  regno *= 10;
11373 			  regno += *s - '0';
11374 			  ++s;
11375 			}
11376 		      while (ISDIGIT (*s));
11377 		      if (regno > 31)
11378 			as_bad (_("Invalid register number (%d)"), regno);
11379 		      else
11380 			{
11381 			  INSERT_OPERAND (0, RT, *ip, regno);
11382 			  continue;
11383 			}
11384 		    }
11385 		  else
11386 		    as_bad (_("Invalid coprocessor 0 register number"));
11387 		  break;
11388 
11389 		case 'x':
11390 		  /* bbit[01] and bbit[01]32 bit index.  Give error if index
11391 		     is not in the valid range.  */
11392 		  gas_assert (!mips_opts.micromips);
11393 		  my_getExpression (&imm_expr, s);
11394 		  check_absolute_expr (ip, &imm_expr);
11395 		  if ((unsigned) imm_expr.X_add_number > 31)
11396 		    {
11397 		      as_bad (_("Improper bit index (%lu)"),
11398 			      (unsigned long) imm_expr.X_add_number);
11399 		      imm_expr.X_add_number = 0;
11400 		    }
11401 		  INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11402 		  imm_expr.X_op = O_absent;
11403 		  s = expr_end;
11404 		  continue;
11405 
11406 		case 'X':
11407 		  /* bbit[01] bit index when bbit is used but we generate
11408 		     bbit[01]32 because the index is over 32.  Move to the
11409 		     next candidate if index is not in the valid range.  */
11410 		  gas_assert (!mips_opts.micromips);
11411 		  my_getExpression (&imm_expr, s);
11412 		  check_absolute_expr (ip, &imm_expr);
11413 		  if ((unsigned) imm_expr.X_add_number < 32
11414 		      || (unsigned) imm_expr.X_add_number > 63)
11415 		    break;
11416 		  INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11417 		  imm_expr.X_op = O_absent;
11418 		  s = expr_end;
11419 		  continue;
11420 
11421 		case 'p':
11422 		  /* cins, cins32, exts and exts32 position field.  Give error
11423 		     if it's not in the valid range.  */
11424 		  gas_assert (!mips_opts.micromips);
11425 		  my_getExpression (&imm_expr, s);
11426 		  check_absolute_expr (ip, &imm_expr);
11427 		  if ((unsigned) imm_expr.X_add_number > 31)
11428 		    {
11429 		      as_bad (_("Improper position (%lu)"),
11430 			      (unsigned long) imm_expr.X_add_number);
11431 		      imm_expr.X_add_number = 0;
11432 		    }
11433 		  /* Make the pos explicit to simplify +S.  */
11434 		  lastpos = imm_expr.X_add_number + 32;
11435 		  INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11436 		  imm_expr.X_op = O_absent;
11437 		  s = expr_end;
11438 		  continue;
11439 
11440 		case 'P':
11441 		  /* cins, cins32, exts and exts32 position field.  Move to
11442 		     the next candidate if it's not in the valid range.  */
11443 		  gas_assert (!mips_opts.micromips);
11444 		  my_getExpression (&imm_expr, s);
11445 		  check_absolute_expr (ip, &imm_expr);
11446 		  if ((unsigned) imm_expr.X_add_number < 32
11447 		      || (unsigned) imm_expr.X_add_number > 63)
11448 		    break;
11449  		  lastpos = imm_expr.X_add_number;
11450 		  INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11451 		  imm_expr.X_op = O_absent;
11452 		  s = expr_end;
11453 		  continue;
11454 
11455 		case 's':
11456 		  /* cins and exts length-minus-one field.  */
11457 		  gas_assert (!mips_opts.micromips);
11458 		  my_getExpression (&imm_expr, s);
11459 		  check_absolute_expr (ip, &imm_expr);
11460 		  if ((unsigned long) imm_expr.X_add_number > 31)
11461 		    {
11462 		      as_bad (_("Improper size (%lu)"),
11463 			      (unsigned long) imm_expr.X_add_number);
11464 		      imm_expr.X_add_number = 0;
11465 		    }
11466 		  INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11467 		  imm_expr.X_op = O_absent;
11468 		  s = expr_end;
11469 		  continue;
11470 
11471 		case 'S':
11472 		  /* cins32/exts32 and cins/exts aliasing cint32/exts32
11473 		     length-minus-one field.  */
11474 		  gas_assert (!mips_opts.micromips);
11475 		  my_getExpression (&imm_expr, s);
11476 		  check_absolute_expr (ip, &imm_expr);
11477 		  if ((long) imm_expr.X_add_number < 0
11478 		      || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11479 		    {
11480 		      as_bad (_("Improper size (%lu)"),
11481 			      (unsigned long) imm_expr.X_add_number);
11482 		      imm_expr.X_add_number = 0;
11483 		    }
11484 		  INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11485 		  imm_expr.X_op = O_absent;
11486 		  s = expr_end;
11487 		  continue;
11488 
11489 		case 'Q':
11490 		  /* seqi/snei immediate field.  */
11491 		  gas_assert (!mips_opts.micromips);
11492 		  my_getExpression (&imm_expr, s);
11493 		  check_absolute_expr (ip, &imm_expr);
11494 		  if ((long) imm_expr.X_add_number < -512
11495 		      || (long) imm_expr.X_add_number >= 512)
11496 		    {
11497 		      as_bad (_("Improper immediate (%ld)"),
11498 			       (long) imm_expr.X_add_number);
11499 		      imm_expr.X_add_number = 0;
11500 		    }
11501 		  INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11502 		  imm_expr.X_op = O_absent;
11503 		  s = expr_end;
11504 		  continue;
11505 
11506 		case 'a': /* 8-bit signed offset in bit 6 */
11507 		  gas_assert (!mips_opts.micromips);
11508 		  my_getExpression (&imm_expr, s);
11509 		  check_absolute_expr (ip, &imm_expr);
11510 		  min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11511 		  max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11512 		  if (imm_expr.X_add_number < min_range
11513 		      || imm_expr.X_add_number > max_range)
11514 		    {
11515 		      as_bad (_("Offset not in range %ld..%ld (%ld)"),
11516 		              (long) min_range, (long) max_range,
11517 		              (long) imm_expr.X_add_number);
11518 		    }
11519 		  INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
11520 		  imm_expr.X_op = O_absent;
11521 		  s = expr_end;
11522 		  continue;
11523 
11524 		case 'b': /* 8-bit signed offset in bit 3 */
11525 		  gas_assert (!mips_opts.micromips);
11526 		  my_getExpression (&imm_expr, s);
11527 		  check_absolute_expr (ip, &imm_expr);
11528 		  min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11529 		  max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11530 		  if (imm_expr.X_add_number < min_range
11531 		      || imm_expr.X_add_number > max_range)
11532 		    {
11533 		      as_bad (_("Offset not in range %ld..%ld (%ld)"),
11534 		              (long) min_range, (long) max_range,
11535 		              (long) imm_expr.X_add_number);
11536 		    }
11537 		  INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
11538 		  imm_expr.X_op = O_absent;
11539 		  s = expr_end;
11540 		  continue;
11541 
11542 		case 'c': /* 9-bit signed offset in bit 6 */
11543 		  gas_assert (!mips_opts.micromips);
11544 		  my_getExpression (&imm_expr, s);
11545 		  check_absolute_expr (ip, &imm_expr);
11546 		  min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11547 		  max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
11548 		  /* We check the offset range before adjusted.  */
11549 		  min_range <<= 4;
11550 		  max_range <<= 4;
11551 		  if (imm_expr.X_add_number < min_range
11552 		      || imm_expr.X_add_number > max_range)
11553 		    {
11554 		      as_bad (_("Offset not in range %ld..%ld (%ld)"),
11555 		              (long) min_range, (long) max_range,
11556 		              (long) imm_expr.X_add_number);
11557 		    }
11558 		  if (imm_expr.X_add_number & 0xf)
11559 		    {
11560 		      as_bad (_("Offset not 16 bytes alignment (%ld)"),
11561 			      (long) imm_expr.X_add_number);
11562 		    }
11563 		  /* Right shift 4 bits to adjust the offset operand.  */
11564 		  INSERT_OPERAND (0, OFFSET_C, *ip,
11565 				  imm_expr.X_add_number >> 4);
11566 		  imm_expr.X_op = O_absent;
11567 		  s = expr_end;
11568 		  continue;
11569 
11570 		case 'z':
11571 		  gas_assert (!mips_opts.micromips);
11572 		  if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11573 		    break;
11574 		  if (regno == AT && mips_opts.at)
11575 		    {
11576 		      if (mips_opts.at == ATREG)
11577 			as_warn (_("used $at without \".set noat\""));
11578 		      else
11579 			as_warn (_("used $%u with \".set at=$%u\""),
11580 				 regno, mips_opts.at);
11581 		    }
11582 		  INSERT_OPERAND (0, RZ, *ip, regno);
11583 		  continue;
11584 
11585 		case 'Z':
11586 		  gas_assert (!mips_opts.micromips);
11587 		  if (!reg_lookup (&s, RTYPE_FPU, &regno))
11588 		    break;
11589 		  INSERT_OPERAND (0, FZ, *ip, regno);
11590 		  continue;
11591 
11592 		default:
11593 		  as_bad (_("Internal error: bad %s opcode "
11594 			    "(unknown extension operand type `+%c'): %s %s"),
11595 			  mips_opts.micromips ? "microMIPS" : "MIPS",
11596 			  *args, insn->name, insn->args);
11597 		  /* Further processing is fruitless.  */
11598 		  return;
11599 		}
11600 	      break;
11601 
11602 	    case '.':		/* 10-bit offset.  */
11603 	      gas_assert (mips_opts.micromips);
11604 	    case '~':		/* 12-bit offset.  */
11605 	      {
11606 		int shift = *args == '.' ? 9 : 11;
11607 		size_t i;
11608 
11609 		/* Check whether there is only a single bracketed expression
11610 		   left.  If so, it must be the base register and the
11611 		   constant must be zero.  */
11612 		if (*s == '(' && strchr (s + 1, '(') == 0)
11613 		  continue;
11614 
11615 		/* If this value won't fit into the offset, then go find
11616 		   a macro that will generate a 16- or 32-bit offset code
11617 		   pattern.  */
11618 		i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11619 		if ((i == 0 && (imm_expr.X_op != O_constant
11620 				|| imm_expr.X_add_number >= 1 << shift
11621 				|| imm_expr.X_add_number < -1 << shift))
11622 		    || i > 0)
11623 		  {
11624 		    imm_expr.X_op = O_absent;
11625 		    break;
11626 		  }
11627 		if (shift == 9)
11628 		  INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11629 		else
11630 		  INSERT_OPERAND (mips_opts.micromips,
11631 				  OFFSET12, *ip, imm_expr.X_add_number);
11632 		imm_expr.X_op = O_absent;
11633 		s = expr_end;
11634 	      }
11635 	      continue;
11636 
11637 	    case '<':		/* must be at least one digit */
11638 	      /*
11639 	       * According to the manual, if the shift amount is greater
11640 	       * than 31 or less than 0, then the shift amount should be
11641 	       * mod 32.  In reality the mips assembler issues an error.
11642 	       * We issue a warning and mask out all but the low 5 bits.
11643 	       */
11644 	      my_getExpression (&imm_expr, s);
11645 	      check_absolute_expr (ip, &imm_expr);
11646 	      if ((unsigned long) imm_expr.X_add_number > 31)
11647 		as_warn (_("Improper shift amount (%lu)"),
11648 			 (unsigned long) imm_expr.X_add_number);
11649 	      INSERT_OPERAND (mips_opts.micromips,
11650 			      SHAMT, *ip, imm_expr.X_add_number);
11651 	      imm_expr.X_op = O_absent;
11652 	      s = expr_end;
11653 	      continue;
11654 
11655 	    case '>':		/* shift amount minus 32 */
11656 	      my_getExpression (&imm_expr, s);
11657 	      check_absolute_expr (ip, &imm_expr);
11658 	      if ((unsigned long) imm_expr.X_add_number < 32
11659 		  || (unsigned long) imm_expr.X_add_number > 63)
11660 		break;
11661 	      INSERT_OPERAND (mips_opts.micromips,
11662 			      SHAMT, *ip, imm_expr.X_add_number - 32);
11663 	      imm_expr.X_op = O_absent;
11664 	      s = expr_end;
11665 	      continue;
11666 
11667 	    case 'k':		/* CACHE code.  */
11668 	    case 'h':		/* PREFX code.  */
11669 	    case '1':		/* SYNC type.  */
11670 	      my_getExpression (&imm_expr, s);
11671 	      check_absolute_expr (ip, &imm_expr);
11672 	      if ((unsigned long) imm_expr.X_add_number > 31)
11673 		as_warn (_("Invalid value for `%s' (%lu)"),
11674 			 ip->insn_mo->name,
11675 			 (unsigned long) imm_expr.X_add_number);
11676 	      switch (*args)
11677 		{
11678 		case 'k':
11679 		  if (mips_fix_cn63xxp1
11680 		      && !mips_opts.micromips
11681 		      && strcmp ("pref", insn->name) == 0)
11682 		    switch (imm_expr.X_add_number)
11683 		      {
11684 		      case 5:
11685 		      case 25:
11686 		      case 26:
11687 		      case 27:
11688 		      case 28:
11689 		      case 29:
11690 		      case 30:
11691 		      case 31:  /* These are ok.  */
11692 			break;
11693 
11694 		      default:  /* The rest must be changed to 28.  */
11695 			imm_expr.X_add_number = 28;
11696 			break;
11697 		      }
11698 		  INSERT_OPERAND (mips_opts.micromips,
11699 				  CACHE, *ip, imm_expr.X_add_number);
11700 		  break;
11701 		case 'h':
11702 		  INSERT_OPERAND (mips_opts.micromips,
11703 				  PREFX, *ip, imm_expr.X_add_number);
11704 		  break;
11705 		case '1':
11706 		  INSERT_OPERAND (mips_opts.micromips,
11707 				  STYPE, *ip, imm_expr.X_add_number);
11708 		  break;
11709 		}
11710 	      imm_expr.X_op = O_absent;
11711 	      s = expr_end;
11712 	      continue;
11713 
11714 	    case 'c':		/* BREAK code.  */
11715 	      {
11716 		unsigned long mask = (mips_opts.micromips
11717 				      ? MICROMIPSOP_MASK_CODE
11718 				      : OP_MASK_CODE);
11719 
11720 		my_getExpression (&imm_expr, s);
11721 		check_absolute_expr (ip, &imm_expr);
11722 		if ((unsigned long) imm_expr.X_add_number > mask)
11723 		  as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11724 			   ip->insn_mo->name,
11725 			   mask, (unsigned long) imm_expr.X_add_number);
11726 		INSERT_OPERAND (mips_opts.micromips,
11727 				CODE, *ip, imm_expr.X_add_number);
11728 		imm_expr.X_op = O_absent;
11729 		s = expr_end;
11730 	      }
11731 	      continue;
11732 
11733 	    case 'q':		/* Lower BREAK code.  */
11734 	      {
11735 		unsigned long mask = (mips_opts.micromips
11736 				      ? MICROMIPSOP_MASK_CODE2
11737 				      : OP_MASK_CODE2);
11738 
11739 		my_getExpression (&imm_expr, s);
11740 		check_absolute_expr (ip, &imm_expr);
11741 		if ((unsigned long) imm_expr.X_add_number > mask)
11742 		  as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11743 			   ip->insn_mo->name,
11744 			   mask, (unsigned long) imm_expr.X_add_number);
11745 		INSERT_OPERAND (mips_opts.micromips,
11746 				CODE2, *ip, imm_expr.X_add_number);
11747 		imm_expr.X_op = O_absent;
11748 		s = expr_end;
11749 	      }
11750 	      continue;
11751 
11752 	    case 'B':		/* 20- or 10-bit syscall/break/wait code.  */
11753 	      {
11754 		unsigned long mask = (mips_opts.micromips
11755 				      ? MICROMIPSOP_MASK_CODE10
11756 				      : OP_MASK_CODE20);
11757 
11758 		my_getExpression (&imm_expr, s);
11759 		check_absolute_expr (ip, &imm_expr);
11760 		if ((unsigned long) imm_expr.X_add_number > mask)
11761 		  as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11762 			   ip->insn_mo->name,
11763 			   mask, (unsigned long) imm_expr.X_add_number);
11764 		if (mips_opts.micromips)
11765 		  INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11766 		else
11767 		  INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11768 		imm_expr.X_op = O_absent;
11769 		s = expr_end;
11770 	      }
11771 	      continue;
11772 
11773 	    case 'C':		/* 25- or 23-bit coprocessor code.  */
11774 	      {
11775 		unsigned long mask = (mips_opts.micromips
11776 				      ? MICROMIPSOP_MASK_COPZ
11777 				      : OP_MASK_COPZ);
11778 
11779 		my_getExpression (&imm_expr, s);
11780 		check_absolute_expr (ip, &imm_expr);
11781 		if ((unsigned long) imm_expr.X_add_number > mask)
11782 		  as_warn (_("Coproccesor code > %u bits (%lu)"),
11783 			   mips_opts.micromips ? 23U : 25U,
11784 			   (unsigned long) imm_expr.X_add_number);
11785 		INSERT_OPERAND (mips_opts.micromips,
11786 				COPZ, *ip, imm_expr.X_add_number);
11787 		imm_expr.X_op = O_absent;
11788 		s = expr_end;
11789 	      }
11790 	      continue;
11791 
11792 	    case 'J':		/* 19-bit WAIT code.  */
11793 	      gas_assert (!mips_opts.micromips);
11794 	      my_getExpression (&imm_expr, s);
11795 	      check_absolute_expr (ip, &imm_expr);
11796 	      if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
11797 	        {
11798 	          as_warn (_("Illegal 19-bit code (%lu)"),
11799 			   (unsigned long) imm_expr.X_add_number);
11800 	          imm_expr.X_add_number &= OP_MASK_CODE19;
11801 	        }
11802 	      INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
11803 	      imm_expr.X_op = O_absent;
11804 	      s = expr_end;
11805 	      continue;
11806 
11807 	    case 'P':		/* Performance register.  */
11808 	      gas_assert (!mips_opts.micromips);
11809 	      my_getExpression (&imm_expr, s);
11810 	      check_absolute_expr (ip, &imm_expr);
11811 	      if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
11812 		as_warn (_("Invalid performance register (%lu)"),
11813 			 (unsigned long) imm_expr.X_add_number);
11814 	      INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
11815 	      imm_expr.X_op = O_absent;
11816 	      s = expr_end;
11817 	      continue;
11818 
11819 	    case 'G':		/* Coprocessor destination register.  */
11820 	      {
11821 		unsigned long opcode = ip->insn_opcode;
11822 		unsigned long mask;
11823 		unsigned int types;
11824 		int cop0;
11825 
11826 		if (mips_opts.micromips)
11827 		  {
11828 		    mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11829 			     | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11830 			     | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11831 		    opcode &= mask;
11832 		    switch (opcode)
11833 		      {
11834 		      case 0x000000fc:				/* mfc0  */
11835 		      case 0x000002fc:				/* mtc0  */
11836 		      case 0x580000fc:				/* dmfc0 */
11837 		      case 0x580002fc:				/* dmtc0 */
11838 			cop0 = 1;
11839 			break;
11840 		      default:
11841 			cop0 = 0;
11842 			break;
11843 		      }
11844 		  }
11845 		else
11846 		  {
11847 		    opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11848 		    cop0 = opcode == OP_OP_COP0;
11849 		  }
11850 		types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11851 		ok = reg_lookup (&s, types, &regno);
11852 		if (mips_opts.micromips)
11853 		  INSERT_OPERAND (1, RS, *ip, regno);
11854 		else
11855 		  INSERT_OPERAND (0, RD, *ip, regno);
11856 		if (ok)
11857 		  {
11858 		    lastregno = regno;
11859 		    continue;
11860 		  }
11861 	      }
11862 	      break;
11863 
11864 	    case 'y':		/* ALNV.PS source register.  */
11865 	      gas_assert (mips_opts.micromips);
11866 	      goto do_reg;
11867 	    case 'x':		/* Ignore register name.  */
11868 	    case 'U':           /* Destination register (CLO/CLZ).  */
11869 	    case 'g':		/* Coprocessor destination register.  */
11870 	      gas_assert (!mips_opts.micromips);
11871 	    case 'b':		/* Base register.  */
11872 	    case 'd':		/* Destination register.  */
11873 	    case 's':		/* Source register.  */
11874 	    case 't':		/* Target register.  */
11875 	    case 'r':		/* Both target and source.  */
11876 	    case 'v':		/* Both dest and source.  */
11877 	    case 'w':		/* Both dest and target.  */
11878 	    case 'E':		/* Coprocessor target register.  */
11879 	    case 'K':		/* RDHWR destination register.  */
11880 	    case 'z':		/* Must be zero register.  */
11881 	    do_reg:
11882 	      s_reset = s;
11883 	      if (*args == 'E' || *args == 'K')
11884 		ok = reg_lookup (&s, RTYPE_NUM, &regno);
11885 	      else
11886 		{
11887 		  ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
11888 		  if (regno == AT && mips_opts.at)
11889 		    {
11890 		      if (mips_opts.at == ATREG)
11891 			as_warn (_("Used $at without \".set noat\""));
11892 		      else
11893 			as_warn (_("Used $%u with \".set at=$%u\""),
11894 				 regno, mips_opts.at);
11895 		    }
11896 		}
11897 	      if (ok)
11898 		{
11899 		  c = *args;
11900 		  if (*s == ' ')
11901 		    ++s;
11902 		  if (args[1] != *s)
11903 		    {
11904 		      if (c == 'r' || c == 'v' || c == 'w')
11905 			{
11906 			  regno = lastregno;
11907 			  s = s_reset;
11908 			  ++args;
11909 			}
11910 		    }
11911 		  /* 'z' only matches $0.  */
11912 		  if (c == 'z' && regno != 0)
11913 		    break;
11914 
11915 		  if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
11916 		    {
11917 		      if (regno == lastregno)
11918 			{
11919 			  insn_error
11920 			    = _("Source and destination must be different");
11921 			  continue;
11922 			}
11923 		      if (regno == 31 && lastregno == 0xffffffff)
11924 			{
11925 			  insn_error
11926 			    = _("A destination register must be supplied");
11927 			  continue;
11928 			}
11929 		    }
11930 		  /* Now that we have assembled one operand, we use the args
11931 		     string to figure out where it goes in the instruction.  */
11932 		  switch (c)
11933 		    {
11934 		    case 'r':
11935 		    case 's':
11936 		    case 'v':
11937 		    case 'b':
11938 		      INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
11939 		      break;
11940 
11941 		    case 'K':
11942 		      if (mips_opts.micromips)
11943 			INSERT_OPERAND (1, RS, *ip, regno);
11944 		      else
11945 			INSERT_OPERAND (0, RD, *ip, regno);
11946 		      break;
11947 
11948 		    case 'd':
11949 		    case 'g':
11950 		      INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
11951 		      break;
11952 
11953 		    case 'U':
11954 		      gas_assert (!mips_opts.micromips);
11955 		      INSERT_OPERAND (0, RD, *ip, regno);
11956 		      INSERT_OPERAND (0, RT, *ip, regno);
11957 		      break;
11958 
11959 		    case 'w':
11960 		    case 't':
11961 		    case 'E':
11962 		      INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
11963 		      break;
11964 
11965 		    case 'y':
11966 		      gas_assert (mips_opts.micromips);
11967 		      INSERT_OPERAND (1, RS3, *ip, regno);
11968 		      break;
11969 
11970 		    case 'x':
11971 		      /* This case exists because on the r3000 trunc
11972 			 expands into a macro which requires a gp
11973 			 register.  On the r6000 or r4000 it is
11974 			 assembled into a single instruction which
11975 			 ignores the register.  Thus the insn version
11976 			 is MIPS_ISA2 and uses 'x', and the macro
11977 			 version is MIPS_ISA1 and uses 't'.  */
11978 		      break;
11979 
11980 		    case 'z':
11981 		      /* This case is for the div instruction, which
11982 			 acts differently if the destination argument
11983 			 is $0.  This only matches $0, and is checked
11984 			 outside the switch.  */
11985 		      break;
11986 		    }
11987 		  lastregno = regno;
11988 		  continue;
11989 		}
11990 	      switch (*args++)
11991 		{
11992 		case 'r':
11993 		case 'v':
11994 		  INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11995 		  continue;
11996 
11997 		case 'w':
11998 		  INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11999 		  continue;
12000 		}
12001 	      break;
12002 
12003 	    case 'O':		/* MDMX alignment immediate constant.  */
12004 	      gas_assert (!mips_opts.micromips);
12005 	      my_getExpression (&imm_expr, s);
12006 	      check_absolute_expr (ip, &imm_expr);
12007 	      if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
12008 		as_warn (_("Improper align amount (%ld), using low bits"),
12009 			 (long) imm_expr.X_add_number);
12010 	      INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
12011 	      imm_expr.X_op = O_absent;
12012 	      s = expr_end;
12013 	      continue;
12014 
12015 	    case 'Q':		/* MDMX vector, element sel, or const.  */
12016 	      if (s[0] != '$')
12017 		{
12018 		  /* MDMX Immediate.  */
12019 		  gas_assert (!mips_opts.micromips);
12020 		  my_getExpression (&imm_expr, s);
12021 		  check_absolute_expr (ip, &imm_expr);
12022 		  if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
12023 		    as_warn (_("Invalid MDMX Immediate (%ld)"),
12024 			     (long) imm_expr.X_add_number);
12025 		  INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
12026 		  if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12027 		    ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12028 		  else
12029 		    ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
12030 		  imm_expr.X_op = O_absent;
12031 		  s = expr_end;
12032 		  continue;
12033 		}
12034 	      /* Not MDMX Immediate.  Fall through.  */
12035 	    case 'X':           /* MDMX destination register.  */
12036 	    case 'Y':           /* MDMX source register.  */
12037 	    case 'Z':           /* MDMX target register.  */
12038 	      is_mdmx = 1;
12039 	    case 'W':
12040 	      gas_assert (!mips_opts.micromips);
12041 	    case 'D':		/* Floating point destination register.  */
12042 	    case 'S':		/* Floating point source register.  */
12043 	    case 'T':		/* Floating point target register.  */
12044 	    case 'R':		/* Floating point source register.  */
12045 	    case 'V':
12046 	      rtype = RTYPE_FPU;
12047 	      if (is_mdmx
12048 		  || (mips_opts.ase_mdmx
12049 		      && (ip->insn_mo->pinfo & FP_D)
12050 		      && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12051 						| INSN_COPROC_MEMORY_DELAY
12052 						| INSN_LOAD_COPROC_DELAY
12053 						| INSN_LOAD_MEMORY_DELAY
12054 						| INSN_STORE_MEMORY))))
12055 		rtype |= RTYPE_VEC;
12056 	      s_reset = s;
12057 	      if (reg_lookup (&s, rtype, &regno))
12058 		{
12059 		  if ((regno & 1) != 0
12060 		      && HAVE_32BIT_FPRS
12061 		      && !mips_oddfpreg_ok (ip->insn_mo, argnum))
12062 		    as_warn (_("Float register should be even, was %d"),
12063 			     regno);
12064 
12065 		  c = *args;
12066 		  if (*s == ' ')
12067 		    ++s;
12068 		  if (args[1] != *s)
12069 		    {
12070 		      if (c == 'V' || c == 'W')
12071 			{
12072 			  regno = lastregno;
12073 			  s = s_reset;
12074 			  ++args;
12075 			}
12076 		    }
12077 		  switch (c)
12078 		    {
12079 		    case 'D':
12080 		    case 'X':
12081 		      INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
12082 		      break;
12083 
12084 		    case 'V':
12085 		    case 'S':
12086 		    case 'Y':
12087 		      INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
12088 		      break;
12089 
12090 		    case 'Q':
12091 		      /* This is like 'Z', but also needs to fix the MDMX
12092 			 vector/scalar select bits.  Note that the
12093 			 scalar immediate case is handled above.  */
12094 		      if (*s == '[')
12095 			{
12096 			  int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12097 			  int max_el = (is_qh ? 3 : 7);
12098 			  s++;
12099 			  my_getExpression(&imm_expr, s);
12100 			  check_absolute_expr (ip, &imm_expr);
12101 			  s = expr_end;
12102 			  if (imm_expr.X_add_number > max_el)
12103 			    as_bad (_("Bad element selector %ld"),
12104 				    (long) imm_expr.X_add_number);
12105 			  imm_expr.X_add_number &= max_el;
12106 			  ip->insn_opcode |= (imm_expr.X_add_number
12107 					      << (OP_SH_VSEL +
12108 						  (is_qh ? 2 : 1)));
12109 			  imm_expr.X_op = O_absent;
12110 			  if (*s != ']')
12111 			    as_warn (_("Expecting ']' found '%s'"), s);
12112 			  else
12113 			    s++;
12114 			}
12115 		      else
12116                         {
12117                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12118                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12119 						<< OP_SH_VSEL);
12120 			  else
12121 			    ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12122 						OP_SH_VSEL);
12123 			}
12124                       /* Fall through.  */
12125 		    case 'W':
12126 		    case 'T':
12127 		    case 'Z':
12128 		      INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
12129 		      break;
12130 
12131 		    case 'R':
12132 		      INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
12133 		      break;
12134 		    }
12135 		  lastregno = regno;
12136 		  continue;
12137 		}
12138 
12139 	      switch (*args++)
12140 		{
12141 		case 'V':
12142 		  INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
12143 		  continue;
12144 
12145 		case 'W':
12146 		  INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
12147 		  continue;
12148 		}
12149 	      break;
12150 
12151 	    case 'I':
12152 	      my_getExpression (&imm_expr, s);
12153 	      if (imm_expr.X_op != O_big
12154 		  && imm_expr.X_op != O_constant)
12155 		insn_error = _("absolute expression required");
12156 	      if (HAVE_32BIT_GPRS)
12157 		normalize_constant_expr (&imm_expr);
12158 	      s = expr_end;
12159 	      continue;
12160 
12161 	    case 'A':
12162 	      my_getExpression (&offset_expr, s);
12163 	      normalize_address_expr (&offset_expr);
12164 	      *imm_reloc = BFD_RELOC_32;
12165 	      s = expr_end;
12166 	      continue;
12167 
12168 	    case 'F':
12169 	    case 'L':
12170 	    case 'f':
12171 	    case 'l':
12172 	      {
12173 		int f64;
12174 		int using_gprs;
12175 		char *save_in;
12176 		char *err;
12177 		unsigned char temp[8];
12178 		int len;
12179 		unsigned int length;
12180 		segT seg;
12181 		subsegT subseg;
12182 		char *p;
12183 
12184 		/* These only appear as the last operand in an
12185 		   instruction, and every instruction that accepts
12186 		   them in any variant accepts them in all variants.
12187 		   This means we don't have to worry about backing out
12188 		   any changes if the instruction does not match.
12189 
12190 		   The difference between them is the size of the
12191 		   floating point constant and where it goes.  For 'F'
12192 		   and 'L' the constant is 64 bits; for 'f' and 'l' it
12193 		   is 32 bits.  Where the constant is placed is based
12194 		   on how the MIPS assembler does things:
12195 		    F -- .rdata
12196 		    L -- .lit8
12197 		    f -- immediate value
12198 		    l -- .lit4
12199 
12200 		    The .lit4 and .lit8 sections are only used if
12201 		    permitted by the -G argument.
12202 
12203 		    The code below needs to know whether the target register
12204 		    is 32 or 64 bits wide.  It relies on the fact 'f' and
12205 		    'F' are used with GPR-based instructions and 'l' and
12206 		    'L' are used with FPR-based instructions.  */
12207 
12208 		f64 = *args == 'F' || *args == 'L';
12209 		using_gprs = *args == 'F' || *args == 'f';
12210 
12211 		save_in = input_line_pointer;
12212 		input_line_pointer = s;
12213 		err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12214 		length = len;
12215 		s = input_line_pointer;
12216 		input_line_pointer = save_in;
12217 		if (err != NULL && *err != '\0')
12218 		  {
12219 		    as_bad (_("Bad floating point constant: %s"), err);
12220 		    memset (temp, '\0', sizeof temp);
12221 		    length = f64 ? 8 : 4;
12222 		  }
12223 
12224 		gas_assert (length == (unsigned) (f64 ? 8 : 4));
12225 
12226 		if (*args == 'f'
12227 		    || (*args == 'l'
12228 			&& (g_switch_value < 4
12229 			    || (temp[0] == 0 && temp[1] == 0)
12230 			    || (temp[2] == 0 && temp[3] == 0))))
12231 		  {
12232 		    imm_expr.X_op = O_constant;
12233 		    if (!target_big_endian)
12234 		      imm_expr.X_add_number = bfd_getl32 (temp);
12235 		    else
12236 		      imm_expr.X_add_number = bfd_getb32 (temp);
12237 		  }
12238 		else if (length > 4
12239 			 && !mips_disable_float_construction
12240 			 /* Constants can only be constructed in GPRs and
12241 			    copied to FPRs if the GPRs are at least as wide
12242 			    as the FPRs.  Force the constant into memory if
12243 			    we are using 64-bit FPRs but the GPRs are only
12244 			    32 bits wide.  */
12245 			 && (using_gprs
12246 			     || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12247 			 && ((temp[0] == 0 && temp[1] == 0)
12248 			     || (temp[2] == 0 && temp[3] == 0))
12249 			 && ((temp[4] == 0 && temp[5] == 0)
12250 			     || (temp[6] == 0 && temp[7] == 0)))
12251 		  {
12252 		    /* The value is simple enough to load with a couple of
12253 		       instructions.  If using 32-bit registers, set
12254 		       imm_expr to the high order 32 bits and offset_expr to
12255 		       the low order 32 bits.  Otherwise, set imm_expr to
12256 		       the entire 64 bit constant.  */
12257 		    if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12258 		      {
12259 			imm_expr.X_op = O_constant;
12260 			offset_expr.X_op = O_constant;
12261 			if (!target_big_endian)
12262 			  {
12263 			    imm_expr.X_add_number = bfd_getl32 (temp + 4);
12264 			    offset_expr.X_add_number = bfd_getl32 (temp);
12265 			  }
12266 			else
12267 			  {
12268 			    imm_expr.X_add_number = bfd_getb32 (temp);
12269 			    offset_expr.X_add_number = bfd_getb32 (temp + 4);
12270 			  }
12271 			if (offset_expr.X_add_number == 0)
12272 			  offset_expr.X_op = O_absent;
12273 		      }
12274 		    else if (sizeof (imm_expr.X_add_number) > 4)
12275 		      {
12276 			imm_expr.X_op = O_constant;
12277 			if (!target_big_endian)
12278 			  imm_expr.X_add_number = bfd_getl64 (temp);
12279 			else
12280 			  imm_expr.X_add_number = bfd_getb64 (temp);
12281 		      }
12282 		    else
12283 		      {
12284 			imm_expr.X_op = O_big;
12285 			imm_expr.X_add_number = 4;
12286 			if (!target_big_endian)
12287 			  {
12288 			    generic_bignum[0] = bfd_getl16 (temp);
12289 			    generic_bignum[1] = bfd_getl16 (temp + 2);
12290 			    generic_bignum[2] = bfd_getl16 (temp + 4);
12291 			    generic_bignum[3] = bfd_getl16 (temp + 6);
12292 			  }
12293 			else
12294 			  {
12295 			    generic_bignum[0] = bfd_getb16 (temp + 6);
12296 			    generic_bignum[1] = bfd_getb16 (temp + 4);
12297 			    generic_bignum[2] = bfd_getb16 (temp + 2);
12298 			    generic_bignum[3] = bfd_getb16 (temp);
12299 			  }
12300 		      }
12301 		  }
12302 		else
12303 		  {
12304 		    const char *newname;
12305 		    segT new_seg;
12306 
12307 		    /* Switch to the right section.  */
12308 		    seg = now_seg;
12309 		    subseg = now_subseg;
12310 		    switch (*args)
12311 		      {
12312 		      default: /* unused default case avoids warnings.  */
12313 		      case 'L':
12314 			newname = RDATA_SECTION_NAME;
12315 			if (g_switch_value >= 8)
12316 			  newname = ".lit8";
12317 			break;
12318 		      case 'F':
12319 			newname = RDATA_SECTION_NAME;
12320 			break;
12321 		      case 'l':
12322 			gas_assert (g_switch_value >= 4);
12323 			newname = ".lit4";
12324 			break;
12325 		      }
12326 		    new_seg = subseg_new (newname, (subsegT) 0);
12327 		    if (IS_ELF)
12328 		      bfd_set_section_flags (stdoutput, new_seg,
12329 					     (SEC_ALLOC
12330 					      | SEC_LOAD
12331 					      | SEC_READONLY
12332 					      | SEC_DATA));
12333 		    frag_align (*args == 'l' ? 2 : 3, 0, 0);
12334 		    if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
12335 		      record_alignment (new_seg, 4);
12336 		    else
12337 		      record_alignment (new_seg, *args == 'l' ? 2 : 3);
12338 		    if (seg == now_seg)
12339 		      as_bad (_("Can't use floating point insn in this section"));
12340 
12341 		    /* Set the argument to the current address in the
12342 		       section.  */
12343 		    offset_expr.X_op = O_symbol;
12344 		    offset_expr.X_add_symbol = symbol_temp_new_now ();
12345 		    offset_expr.X_add_number = 0;
12346 
12347 		    /* Put the floating point number into the section.  */
12348 		    p = frag_more ((int) length);
12349 		    memcpy (p, temp, length);
12350 
12351 		    /* Switch back to the original section.  */
12352 		    subseg_set (seg, subseg);
12353 		  }
12354 	      }
12355 	      continue;
12356 
12357 	    case 'i':		/* 16-bit unsigned immediate.  */
12358 	    case 'j':		/* 16-bit signed immediate.  */
12359 	      *imm_reloc = BFD_RELOC_LO16;
12360 	      if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12361 		{
12362 		  int more;
12363 		  offsetT minval, maxval;
12364 
12365 		  more = (insn + 1 < past
12366 			  && strcmp (insn->name, insn[1].name) == 0);
12367 
12368 		  /* If the expression was written as an unsigned number,
12369 		     only treat it as signed if there are no more
12370 		     alternatives.  */
12371 		  if (more
12372 		      && *args == 'j'
12373 		      && sizeof (imm_expr.X_add_number) <= 4
12374 		      && imm_expr.X_op == O_constant
12375 		      && imm_expr.X_add_number < 0
12376 		      && imm_expr.X_unsigned
12377 		      && HAVE_64BIT_GPRS)
12378 		    break;
12379 
12380 		  /* For compatibility with older assemblers, we accept
12381 		     0x8000-0xffff as signed 16-bit numbers when only
12382 		     signed numbers are allowed.  */
12383 		  if (*args == 'i')
12384 		    minval = 0, maxval = 0xffff;
12385 		  else if (more)
12386 		    minval = -0x8000, maxval = 0x7fff;
12387 		  else
12388 		    minval = -0x8000, maxval = 0xffff;
12389 
12390 		  if (imm_expr.X_op != O_constant
12391 		      || imm_expr.X_add_number < minval
12392 		      || imm_expr.X_add_number > maxval)
12393 		    {
12394 		      if (more)
12395 			break;
12396 		      if (imm_expr.X_op == O_constant
12397 			  || imm_expr.X_op == O_big)
12398 			as_bad (_("Expression out of range"));
12399 		    }
12400 		}
12401 	      s = expr_end;
12402 	      continue;
12403 
12404 	    case 'o':		/* 16-bit offset.  */
12405 	      offset_reloc[0] = BFD_RELOC_LO16;
12406 	      offset_reloc[1] = BFD_RELOC_UNUSED;
12407 	      offset_reloc[2] = BFD_RELOC_UNUSED;
12408 
12409 	      /* Check whether there is only a single bracketed expression
12410 		 left.  If so, it must be the base register and the
12411 		 constant must be zero.  */
12412 	      if (*s == '(' && strchr (s + 1, '(') == 0)
12413 		{
12414 		  offset_expr.X_op = O_constant;
12415 		  offset_expr.X_add_number = 0;
12416 		  continue;
12417 		}
12418 
12419 	      /* If this value won't fit into a 16 bit offset, then go
12420 		 find a macro that will generate the 32 bit offset
12421 		 code pattern.  */
12422 	      if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12423 		  && (offset_expr.X_op != O_constant
12424 		      || offset_expr.X_add_number >= 0x8000
12425 		      || offset_expr.X_add_number < -0x8000))
12426 		break;
12427 
12428 	      s = expr_end;
12429 	      continue;
12430 
12431 	    case 'p':		/* PC-relative offset.  */
12432 	      *offset_reloc = BFD_RELOC_16_PCREL_S2;
12433 	      my_getExpression (&offset_expr, s);
12434 	      s = expr_end;
12435 	      continue;
12436 
12437 	    case 'u':		/* Upper 16 bits.  */
12438 	      if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12439 		  && imm_expr.X_op == O_constant
12440 		  && (imm_expr.X_add_number < 0
12441 		      || imm_expr.X_add_number >= 0x10000))
12442 		as_bad (_("lui expression (%lu) not in range 0..65535"),
12443 			(unsigned long) imm_expr.X_add_number);
12444 	      s = expr_end;
12445 	      continue;
12446 
12447 	    case 'a':		/* 26-bit address.  */
12448 	      *offset_reloc = BFD_RELOC_MIPS_JMP;
12449 	      my_getExpression (&offset_expr, s);
12450 	      s = expr_end;
12451 	      continue;
12452 
12453 	    case 'N':		/* 3-bit branch condition code.  */
12454 	    case 'M':		/* 3-bit compare condition code.  */
12455 	      rtype = RTYPE_CCC;
12456 	      if (ip->insn_mo->pinfo & (FP_D | FP_S))
12457 		rtype |= RTYPE_FCC;
12458 	      if (!reg_lookup (&s, rtype, &regno))
12459 		break;
12460 	      if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12461 		   || strcmp (str + strlen (str) - 5, "any2f") == 0
12462 		   || strcmp (str + strlen (str) - 5, "any2t") == 0)
12463 		  && (regno & 1) != 0)
12464 		as_warn (_("Condition code register should be even for %s, "
12465 			   "was %d"),
12466 			 str, regno);
12467 	      if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12468 		   || strcmp (str + strlen (str) - 5, "any4t") == 0)
12469 		  && (regno & 3) != 0)
12470 		as_warn (_("Condition code register should be 0 or 4 for %s, "
12471 			   "was %d"),
12472 			 str, regno);
12473 	      if (*args == 'N')
12474 		INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12475 	      else
12476 		INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12477 	      continue;
12478 
12479 	    case 'H':
12480 	      if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12481 		s += 2;
12482 	      if (ISDIGIT (*s))
12483 		{
12484 		  c = 0;
12485 		  do
12486 		    {
12487 		      c *= 10;
12488 		      c += *s - '0';
12489 		      ++s;
12490 		    }
12491 		  while (ISDIGIT (*s));
12492 		}
12493 	      else
12494 		c = 8; /* Invalid sel value.  */
12495 
12496 	      if (c > 7)
12497 		as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12498 	      INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12499 	      continue;
12500 
12501 	    case 'e':
12502 	      gas_assert (!mips_opts.micromips);
12503 	      /* Must be at least one digit.  */
12504 	      my_getExpression (&imm_expr, s);
12505 	      check_absolute_expr (ip, &imm_expr);
12506 
12507 	      if ((unsigned long) imm_expr.X_add_number
12508 		  > (unsigned long) OP_MASK_VECBYTE)
12509 		{
12510 		  as_bad (_("bad byte vector index (%ld)"),
12511 			   (long) imm_expr.X_add_number);
12512 		  imm_expr.X_add_number = 0;
12513 		}
12514 
12515 	      INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12516 	      imm_expr.X_op = O_absent;
12517 	      s = expr_end;
12518 	      continue;
12519 
12520 	    case '%':
12521 	      gas_assert (!mips_opts.micromips);
12522 	      my_getExpression (&imm_expr, s);
12523 	      check_absolute_expr (ip, &imm_expr);
12524 
12525 	      if ((unsigned long) imm_expr.X_add_number
12526 		  > (unsigned long) OP_MASK_VECALIGN)
12527 		{
12528 		  as_bad (_("bad byte vector index (%ld)"),
12529 			   (long) imm_expr.X_add_number);
12530 		  imm_expr.X_add_number = 0;
12531 		}
12532 
12533 	      INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12534 	      imm_expr.X_op = O_absent;
12535 	      s = expr_end;
12536 	      continue;
12537 
12538 	    case 'm':		/* Opcode extension character.  */
12539 	      gas_assert (mips_opts.micromips);
12540 	      c = *++args;
12541 	      switch (c)
12542 		{
12543 		case 'r':
12544 		  if (strncmp (s, "$pc", 3) == 0)
12545 		    {
12546 		      s += 3;
12547 		      continue;
12548 		    }
12549 		  break;
12550 
12551 		case 'a':
12552 		case 'b':
12553 		case 'c':
12554 		case 'd':
12555 		case 'e':
12556 		case 'f':
12557 		case 'g':
12558 		case 'h':
12559 		case 'i':
12560 		case 'j':
12561 		case 'l':
12562 		case 'm':
12563 		case 'n':
12564 		case 'p':
12565 		case 'q':
12566 		case 's':
12567 		case 't':
12568 		case 'x':
12569 		case 'y':
12570 		case 'z':
12571 		  s_reset = s;
12572 		  ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12573 		  if (regno == AT && mips_opts.at)
12574 		    {
12575 		      if (mips_opts.at == ATREG)
12576 			as_warn (_("Used $at without \".set noat\""));
12577 		      else
12578 			as_warn (_("Used $%u with \".set at=$%u\""),
12579 				 regno, mips_opts.at);
12580 		    }
12581 		  if (!ok)
12582 		    {
12583 		      if (c == 'c')
12584 			{
12585 			  gas_assert (args[1] == ',');
12586 			  regno = lastregno;
12587 			  ++args;
12588 			}
12589 		      else if (c == 't')
12590 			{
12591 			  gas_assert (args[1] == ',');
12592 			  ++args;
12593 			  continue;			/* Nothing to do.  */
12594 			}
12595 		      else
12596 			break;
12597 		    }
12598 
12599 		  if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12600 		    {
12601 		      if (regno == lastregno)
12602 			{
12603 			  insn_error
12604 			    = _("Source and destination must be different");
12605 			  continue;
12606 			}
12607 		      if (regno == 31 && lastregno == 0xffffffff)
12608 			{
12609 			  insn_error
12610 			    = _("A destination register must be supplied");
12611 			  continue;
12612 			}
12613 		    }
12614 
12615 		  if (*s == ' ')
12616 		    ++s;
12617 		  if (args[1] != *s)
12618 		    {
12619 		      if (c == 'e')
12620 			{
12621 			  gas_assert (args[1] == ',');
12622 			  regno = lastregno;
12623 			  s = s_reset;
12624 			  ++args;
12625 			}
12626 		      else if (c == 't')
12627 			{
12628 			  gas_assert (args[1] == ',');
12629 			  s = s_reset;
12630 			  ++args;
12631 			  continue;			/* Nothing to do.  */
12632 			}
12633 		    }
12634 
12635 		  /* Make sure regno is the same as lastregno.  */
12636 		  if (c == 't' && regno != lastregno)
12637 		    break;
12638 
12639 		  /* Make sure regno is the same as destregno.  */
12640 		  if (c == 'x' && regno != destregno)
12641 		    break;
12642 
12643 		  /* We need to save regno, before regno maps to the
12644 		     microMIPS register encoding.  */
12645 		  lastregno = regno;
12646 
12647 		  if (c == 'f')
12648 		    destregno = regno;
12649 
12650 		  switch (c)
12651 		    {
12652 		      case 'a':
12653 			if (regno != GP)
12654 			  regno = ILLEGAL_REG;
12655 			break;
12656 
12657 		      case 'b':
12658 			regno = mips32_to_micromips_reg_b_map[regno];
12659 			break;
12660 
12661 		      case 'c':
12662 			regno = mips32_to_micromips_reg_c_map[regno];
12663 			break;
12664 
12665 		      case 'd':
12666 			regno = mips32_to_micromips_reg_d_map[regno];
12667 			break;
12668 
12669 		      case 'e':
12670 			regno = mips32_to_micromips_reg_e_map[regno];
12671 			break;
12672 
12673 		      case 'f':
12674 			regno = mips32_to_micromips_reg_f_map[regno];
12675 			break;
12676 
12677 		      case 'g':
12678 			regno = mips32_to_micromips_reg_g_map[regno];
12679 			break;
12680 
12681 		      case 'h':
12682 			regno = mips32_to_micromips_reg_h_map[regno];
12683 			break;
12684 
12685 		      case 'i':
12686 			switch (EXTRACT_OPERAND (1, MI, *ip))
12687 			  {
12688 			    case 4:
12689 			      if (regno == 21)
12690 				regno = 3;
12691 			      else if (regno == 22)
12692 				regno = 4;
12693 			      else if (regno == 5)
12694 				regno = 5;
12695 			      else if (regno == 6)
12696 				regno = 6;
12697 			      else if (regno == 7)
12698 				regno = 7;
12699 			      else
12700 				regno = ILLEGAL_REG;
12701 			      break;
12702 
12703 			    case 5:
12704 			      if (regno == 6)
12705 				regno = 0;
12706 			      else if (regno == 7)
12707 				regno = 1;
12708 			      else
12709 				regno = ILLEGAL_REG;
12710 			      break;
12711 
12712 			    case 6:
12713 			      if (regno == 7)
12714 				regno = 2;
12715 			      else
12716 				regno = ILLEGAL_REG;
12717 			      break;
12718 
12719 			    default:
12720 			      regno = ILLEGAL_REG;
12721 			      break;
12722 			  }
12723 			break;
12724 
12725 		      case 'l':
12726 			regno = mips32_to_micromips_reg_l_map[regno];
12727 			break;
12728 
12729 		      case 'm':
12730 			regno = mips32_to_micromips_reg_m_map[regno];
12731 			break;
12732 
12733 		      case 'n':
12734 			regno = mips32_to_micromips_reg_n_map[regno];
12735 			break;
12736 
12737 		      case 'q':
12738 			regno = mips32_to_micromips_reg_q_map[regno];
12739 			break;
12740 
12741 		      case 's':
12742 			if (regno != SP)
12743 			  regno = ILLEGAL_REG;
12744 			break;
12745 
12746 		      case 'y':
12747 			if (regno != 31)
12748 			  regno = ILLEGAL_REG;
12749 			break;
12750 
12751 		      case 'z':
12752 			if (regno != ZERO)
12753 			  regno = ILLEGAL_REG;
12754 			break;
12755 
12756 		      case 'j': /* Do nothing.  */
12757 		      case 'p':
12758 		      case 't':
12759 		      case 'x':
12760 			break;
12761 
12762 		      default:
12763 			internalError ();
12764 		    }
12765 
12766 		  if (regno == ILLEGAL_REG)
12767 		    break;
12768 
12769 		  switch (c)
12770 		    {
12771 		      case 'b':
12772 			INSERT_OPERAND (1, MB, *ip, regno);
12773 			break;
12774 
12775 		      case 'c':
12776 			INSERT_OPERAND (1, MC, *ip, regno);
12777 			break;
12778 
12779 		      case 'd':
12780 			INSERT_OPERAND (1, MD, *ip, regno);
12781 			break;
12782 
12783 		      case 'e':
12784 			INSERT_OPERAND (1, ME, *ip, regno);
12785 			break;
12786 
12787 		      case 'f':
12788 			INSERT_OPERAND (1, MF, *ip, regno);
12789 			break;
12790 
12791 		      case 'g':
12792 			INSERT_OPERAND (1, MG, *ip, regno);
12793 			break;
12794 
12795 		      case 'h':
12796 			INSERT_OPERAND (1, MH, *ip, regno);
12797 			break;
12798 
12799 		      case 'i':
12800 			INSERT_OPERAND (1, MI, *ip, regno);
12801 			break;
12802 
12803 		      case 'j':
12804 			INSERT_OPERAND (1, MJ, *ip, regno);
12805 			break;
12806 
12807 		      case 'l':
12808 			INSERT_OPERAND (1, ML, *ip, regno);
12809 			break;
12810 
12811 		      case 'm':
12812 			INSERT_OPERAND (1, MM, *ip, regno);
12813 			break;
12814 
12815 		      case 'n':
12816 			INSERT_OPERAND (1, MN, *ip, regno);
12817 			break;
12818 
12819 		      case 'p':
12820 			INSERT_OPERAND (1, MP, *ip, regno);
12821 			break;
12822 
12823 		      case 'q':
12824 			INSERT_OPERAND (1, MQ, *ip, regno);
12825 			break;
12826 
12827 		      case 'a':	/* Do nothing.  */
12828 		      case 's':	/* Do nothing.  */
12829 		      case 't':	/* Do nothing.  */
12830 		      case 'x':	/* Do nothing.  */
12831 		      case 'y':	/* Do nothing.  */
12832 		      case 'z':	/* Do nothing.  */
12833 			break;
12834 
12835 		      default:
12836 			internalError ();
12837 		    }
12838 		  continue;
12839 
12840 		case 'A':
12841 		  {
12842 		    bfd_reloc_code_real_type r[3];
12843 		    expressionS ep;
12844 		    int imm;
12845 
12846 		    /* Check whether there is only a single bracketed
12847 		       expression left.  If so, it must be the base register
12848 		       and the constant must be zero.  */
12849 		    if (*s == '(' && strchr (s + 1, '(') == 0)
12850 		      {
12851 			INSERT_OPERAND (1, IMMA, *ip, 0);
12852 			continue;
12853 		      }
12854 
12855 		    if (my_getSmallExpression (&ep, r, s) > 0
12856 			|| !expr_const_in_range (&ep, -64, 64, 2))
12857 		      break;
12858 
12859 		    imm = ep.X_add_number >> 2;
12860 		    INSERT_OPERAND (1, IMMA, *ip, imm);
12861 		  }
12862 		  s = expr_end;
12863 		  continue;
12864 
12865 		case 'B':
12866 		  {
12867 		    bfd_reloc_code_real_type r[3];
12868 		    expressionS ep;
12869 		    int imm;
12870 
12871 		    if (my_getSmallExpression (&ep, r, s) > 0
12872 			|| ep.X_op != O_constant)
12873 		      break;
12874 
12875 		    for (imm = 0; imm < 8; imm++)
12876 		      if (micromips_imm_b_map[imm] == ep.X_add_number)
12877 			break;
12878 		    if (imm >= 8)
12879 		      break;
12880 
12881 		    INSERT_OPERAND (1, IMMB, *ip, imm);
12882 		  }
12883 		  s = expr_end;
12884 		  continue;
12885 
12886 		case 'C':
12887 		  {
12888 		    bfd_reloc_code_real_type r[3];
12889 		    expressionS ep;
12890 		    int imm;
12891 
12892 		    if (my_getSmallExpression (&ep, r, s) > 0
12893 			|| ep.X_op != O_constant)
12894 		      break;
12895 
12896 		    for (imm = 0; imm < 16; imm++)
12897 		      if (micromips_imm_c_map[imm] == ep.X_add_number)
12898 			break;
12899 		    if (imm >= 16)
12900 		      break;
12901 
12902 		    INSERT_OPERAND (1, IMMC, *ip, imm);
12903 		  }
12904 		  s = expr_end;
12905 		  continue;
12906 
12907 		case 'D':	/* pc relative offset */
12908 		case 'E':	/* pc relative offset */
12909 		  my_getExpression (&offset_expr, s);
12910 		  if (offset_expr.X_op == O_register)
12911 		    break;
12912 
12913 		  if (!forced_insn_length)
12914 		    *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12915 		  else if (c == 'D')
12916 		    *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12917 		  else
12918 		    *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
12919 		  s = expr_end;
12920 		  continue;
12921 
12922 		case 'F':
12923 		  {
12924 		    bfd_reloc_code_real_type r[3];
12925 		    expressionS ep;
12926 		    int imm;
12927 
12928 		    if (my_getSmallExpression (&ep, r, s) > 0
12929 			|| !expr_const_in_range (&ep, 0, 16, 0))
12930 		      break;
12931 
12932 		    imm = ep.X_add_number;
12933 		    INSERT_OPERAND (1, IMMF, *ip, imm);
12934 		  }
12935 		  s = expr_end;
12936 		  continue;
12937 
12938 		case 'G':
12939 		  {
12940 		    bfd_reloc_code_real_type r[3];
12941 		    expressionS ep;
12942 		    int imm;
12943 
12944 		    /* Check whether there is only a single bracketed
12945 		       expression left.  If so, it must be the base register
12946 		       and the constant must be zero.  */
12947 		    if (*s == '(' && strchr (s + 1, '(') == 0)
12948 		      {
12949 			INSERT_OPERAND (1, IMMG, *ip, 0);
12950 			continue;
12951 		      }
12952 
12953 		    if (my_getSmallExpression (&ep, r, s) > 0
12954 			|| !expr_const_in_range (&ep, -1, 15, 0))
12955 		      break;
12956 
12957 		    imm = ep.X_add_number & 15;
12958 		    INSERT_OPERAND (1, IMMG, *ip, imm);
12959 		  }
12960 		  s = expr_end;
12961 		  continue;
12962 
12963 		case 'H':
12964 		  {
12965 		    bfd_reloc_code_real_type r[3];
12966 		    expressionS ep;
12967 		    int imm;
12968 
12969 		    /* Check whether there is only a single bracketed
12970 		       expression left.  If so, it must be the base register
12971 		       and the constant must be zero.  */
12972 		    if (*s == '(' && strchr (s + 1, '(') == 0)
12973 		      {
12974 			INSERT_OPERAND (1, IMMH, *ip, 0);
12975 			continue;
12976 		      }
12977 
12978 		    if (my_getSmallExpression (&ep, r, s) > 0
12979 			|| !expr_const_in_range (&ep, 0, 16, 1))
12980 		      break;
12981 
12982 		    imm = ep.X_add_number >> 1;
12983 		    INSERT_OPERAND (1, IMMH, *ip, imm);
12984 		  }
12985 		  s = expr_end;
12986 		  continue;
12987 
12988 		case 'I':
12989 		  {
12990 		    bfd_reloc_code_real_type r[3];
12991 		    expressionS ep;
12992 		    int imm;
12993 
12994 		    if (my_getSmallExpression (&ep, r, s) > 0
12995 			|| !expr_const_in_range (&ep, -1, 127, 0))
12996 		      break;
12997 
12998 		    imm = ep.X_add_number & 127;
12999 		    INSERT_OPERAND (1, IMMI, *ip, imm);
13000 		  }
13001 		  s = expr_end;
13002 		  continue;
13003 
13004 		case 'J':
13005 		  {
13006 		    bfd_reloc_code_real_type r[3];
13007 		    expressionS ep;
13008 		    int imm;
13009 
13010 		    /* Check whether there is only a single bracketed
13011 		       expression left.  If so, it must be the base register
13012 		       and the constant must be zero.  */
13013 		    if (*s == '(' && strchr (s + 1, '(') == 0)
13014 		      {
13015 			INSERT_OPERAND (1, IMMJ, *ip, 0);
13016 			continue;
13017 		      }
13018 
13019 		    if (my_getSmallExpression (&ep, r, s) > 0
13020 			|| !expr_const_in_range (&ep, 0, 16, 2))
13021 		      break;
13022 
13023 		    imm = ep.X_add_number >> 2;
13024 		    INSERT_OPERAND (1, IMMJ, *ip, imm);
13025 		  }
13026 		  s = expr_end;
13027 		  continue;
13028 
13029 		case 'L':
13030 		  {
13031 		    bfd_reloc_code_real_type r[3];
13032 		    expressionS ep;
13033 		    int imm;
13034 
13035 		    /* Check whether there is only a single bracketed
13036 		       expression left.  If so, it must be the base register
13037 		       and the constant must be zero.  */
13038 		    if (*s == '(' && strchr (s + 1, '(') == 0)
13039 		      {
13040 			INSERT_OPERAND (1, IMML, *ip, 0);
13041 			continue;
13042 		      }
13043 
13044 		    if (my_getSmallExpression (&ep, r, s) > 0
13045 			|| !expr_const_in_range (&ep, 0, 16, 0))
13046 		      break;
13047 
13048 		    imm = ep.X_add_number;
13049 		    INSERT_OPERAND (1, IMML, *ip, imm);
13050 		  }
13051 		  s = expr_end;
13052 		  continue;
13053 
13054 		case 'M':
13055 		  {
13056 		    bfd_reloc_code_real_type r[3];
13057 		    expressionS ep;
13058 		    int imm;
13059 
13060 		    if (my_getSmallExpression (&ep, r, s) > 0
13061 			|| !expr_const_in_range (&ep, 1, 9, 0))
13062 		      break;
13063 
13064 		    imm = ep.X_add_number & 7;
13065 		    INSERT_OPERAND (1, IMMM, *ip, imm);
13066 		  }
13067 		  s = expr_end;
13068 		  continue;
13069 
13070 		case 'N':	/* Register list for lwm and swm.  */
13071 		  {
13072 		    /* A comma-separated list of registers and/or
13073 		       dash-separated contiguous ranges including
13074 		       both ra and a set of one or more registers
13075 		       starting at s0 up to s3 which have to be
13076 		       consecutive, e.g.:
13077 
13078 		       s0, ra
13079 		       s0, s1, ra, s2, s3
13080 		       s0-s2, ra
13081 
13082 		       and any permutations of these.  */
13083 		    unsigned int reglist;
13084 		    int imm;
13085 
13086 		    if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13087 		      break;
13088 
13089 		    if ((reglist & 0xfff1ffff) != 0x80010000)
13090 		      break;
13091 
13092 		    reglist = (reglist >> 17) & 7;
13093 		    reglist += 1;
13094 		    if ((reglist & -reglist) != reglist)
13095 		      break;
13096 
13097 		    imm = ffs (reglist) - 1;
13098 		    INSERT_OPERAND (1, IMMN, *ip, imm);
13099 		  }
13100 		  continue;
13101 
13102 		case 'O':	/* sdbbp 4-bit code.  */
13103 		  {
13104 		    bfd_reloc_code_real_type r[3];
13105 		    expressionS ep;
13106 		    int imm;
13107 
13108 		    if (my_getSmallExpression (&ep, r, s) > 0
13109 			|| !expr_const_in_range (&ep, 0, 16, 0))
13110 		      break;
13111 
13112 		    imm = ep.X_add_number;
13113 		    INSERT_OPERAND (1, IMMO, *ip, imm);
13114 		  }
13115 		  s = expr_end;
13116 		  continue;
13117 
13118 		case 'P':
13119 		  {
13120 		    bfd_reloc_code_real_type r[3];
13121 		    expressionS ep;
13122 		    int imm;
13123 
13124 		    if (my_getSmallExpression (&ep, r, s) > 0
13125 			|| !expr_const_in_range (&ep, 0, 32, 2))
13126 		      break;
13127 
13128 		    imm = ep.X_add_number >> 2;
13129 		    INSERT_OPERAND (1, IMMP, *ip, imm);
13130 		  }
13131 		  s = expr_end;
13132 		  continue;
13133 
13134 		case 'Q':
13135 		  {
13136 		    bfd_reloc_code_real_type r[3];
13137 		    expressionS ep;
13138 		    int imm;
13139 
13140 		    if (my_getSmallExpression (&ep, r, s) > 0
13141 			|| !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13142 		      break;
13143 
13144 		    imm = ep.X_add_number >> 2;
13145 		    INSERT_OPERAND (1, IMMQ, *ip, imm);
13146 		  }
13147 		  s = expr_end;
13148 		  continue;
13149 
13150 		case 'U':
13151 		  {
13152 		    bfd_reloc_code_real_type r[3];
13153 		    expressionS ep;
13154 		    int imm;
13155 
13156 		    /* Check whether there is only a single bracketed
13157 		       expression left.  If so, it must be the base register
13158 		       and the constant must be zero.  */
13159 		    if (*s == '(' && strchr (s + 1, '(') == 0)
13160 		      {
13161 			INSERT_OPERAND (1, IMMU, *ip, 0);
13162 			continue;
13163 		      }
13164 
13165 		    if (my_getSmallExpression (&ep, r, s) > 0
13166 			|| !expr_const_in_range (&ep, 0, 32, 2))
13167 		      break;
13168 
13169 		    imm = ep.X_add_number >> 2;
13170 		    INSERT_OPERAND (1, IMMU, *ip, imm);
13171 		  }
13172 		  s = expr_end;
13173 		  continue;
13174 
13175 		case 'W':
13176 		  {
13177 		    bfd_reloc_code_real_type r[3];
13178 		    expressionS ep;
13179 		    int imm;
13180 
13181 		    if (my_getSmallExpression (&ep, r, s) > 0
13182 			|| !expr_const_in_range (&ep, 0, 64, 2))
13183 		      break;
13184 
13185 		    imm = ep.X_add_number >> 2;
13186 		    INSERT_OPERAND (1, IMMW, *ip, imm);
13187 		  }
13188 		  s = expr_end;
13189 		  continue;
13190 
13191 		case 'X':
13192 		  {
13193 		    bfd_reloc_code_real_type r[3];
13194 		    expressionS ep;
13195 		    int imm;
13196 
13197 		    if (my_getSmallExpression (&ep, r, s) > 0
13198 			|| !expr_const_in_range (&ep, -8, 8, 0))
13199 		      break;
13200 
13201 		    imm = ep.X_add_number;
13202 		    INSERT_OPERAND (1, IMMX, *ip, imm);
13203 		  }
13204 		  s = expr_end;
13205 		  continue;
13206 
13207 		case 'Y':
13208 		  {
13209 		    bfd_reloc_code_real_type r[3];
13210 		    expressionS ep;
13211 		    int imm;
13212 
13213 		    if (my_getSmallExpression (&ep, r, s) > 0
13214 			|| expr_const_in_range (&ep, -2, 2, 2)
13215 			|| !expr_const_in_range (&ep, -258, 258, 2))
13216 		      break;
13217 
13218 		    imm = ep.X_add_number >> 2;
13219 		    imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13220 		    INSERT_OPERAND (1, IMMY, *ip, imm);
13221 		  }
13222 		  s = expr_end;
13223 		  continue;
13224 
13225 		case 'Z':
13226 		  {
13227 		    bfd_reloc_code_real_type r[3];
13228 		    expressionS ep;
13229 
13230 		    if (my_getSmallExpression (&ep, r, s) > 0
13231 			|| !expr_const_in_range (&ep, 0, 1, 0))
13232 		      break;
13233 		  }
13234 		  s = expr_end;
13235 		  continue;
13236 
13237 		default:
13238 		  as_bad (_("Internal error: bad microMIPS opcode "
13239 			    "(unknown extension operand type `m%c'): %s %s"),
13240 			  *args, insn->name, insn->args);
13241 		  /* Further processing is fruitless.  */
13242 		  return;
13243 		}
13244 	      break;
13245 
13246 	    case 'n':		/* Register list for 32-bit lwm and swm.  */
13247 	      gas_assert (mips_opts.micromips);
13248 	      {
13249 		/* A comma-separated list of registers and/or
13250 		   dash-separated contiguous ranges including
13251 		   at least one of ra and a set of one or more
13252 		   registers starting at s0 up to s7 and then
13253 		   s8 which have to be consecutive, e.g.:
13254 
13255 		   ra
13256 		   s0
13257 		   ra, s0, s1, s2
13258 		   s0-s8
13259 		   s0-s5, ra
13260 
13261 		   and any permutations of these.  */
13262 		unsigned int reglist;
13263 		int imm;
13264 		int ra;
13265 
13266 		if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13267 		  break;
13268 
13269 		if ((reglist & 0x3f00ffff) != 0)
13270 		  break;
13271 
13272 		ra = (reglist >> 27) & 0x10;
13273 		reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13274 		reglist += 1;
13275 		if ((reglist & -reglist) != reglist)
13276 		  break;
13277 
13278 		imm = (ffs (reglist) - 1) | ra;
13279 		INSERT_OPERAND (1, RT, *ip, imm);
13280 		imm_expr.X_op = O_absent;
13281 	      }
13282 	      continue;
13283 
13284 	    case '|':		/* 4-bit trap code.  */
13285 	      gas_assert (mips_opts.micromips);
13286 	      my_getExpression (&imm_expr, s);
13287 	      check_absolute_expr (ip, &imm_expr);
13288 	      if ((unsigned long) imm_expr.X_add_number
13289 		  > MICROMIPSOP_MASK_TRAP)
13290 		as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13291 			(unsigned long) imm_expr.X_add_number,
13292 			ip->insn_mo->name);
13293 	      INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13294 	      imm_expr.X_op = O_absent;
13295 	      s = expr_end;
13296 	      continue;
13297 
13298 	    default:
13299 	      as_bad (_("Bad char = '%c'\n"), *args);
13300 	      internalError ();
13301 	    }
13302 	  break;
13303 	}
13304       /* Args don't match.  */
13305       s = argsStart;
13306       insn_error = _("Illegal operands");
13307       if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13308 	{
13309 	  ++insn;
13310 	  continue;
13311 	}
13312       else if (wrong_delay_slot_insns && need_delay_slot_ok)
13313 	{
13314 	  gas_assert (firstinsn);
13315 	  need_delay_slot_ok = FALSE;
13316 	  past = insn + 1;
13317 	  insn = firstinsn;
13318 	  continue;
13319 	}
13320       return;
13321     }
13322 }
13323 
13324 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13325 
13326 /* This routine assembles an instruction into its binary format when
13327    assembling for the mips16.  As a side effect, it sets one of the
13328    global variables imm_reloc or offset_reloc to the type of relocation
13329    to do if one of the operands is an address expression.  It also sets
13330    forced_insn_length to the resulting instruction size in bytes if the
13331    user explicitly requested a small or extended instruction.  */
13332 
13333 static void
13334 mips16_ip (char *str, struct mips_cl_insn *ip)
13335 {
13336   char *s;
13337   const char *args;
13338   struct mips_opcode *insn;
13339   char *argsstart;
13340   unsigned int regno;
13341   unsigned int lastregno = 0;
13342   char *s_reset;
13343   size_t i;
13344 
13345   insn_error = NULL;
13346 
13347   forced_insn_length = 0;
13348 
13349   for (s = str; ISLOWER (*s); ++s)
13350     ;
13351   switch (*s)
13352     {
13353     case '\0':
13354       break;
13355 
13356     case ' ':
13357       *s++ = '\0';
13358       break;
13359 
13360     case '.':
13361       if (s[1] == 't' && s[2] == ' ')
13362 	{
13363 	  *s = '\0';
13364 	  forced_insn_length = 2;
13365 	  s += 3;
13366 	  break;
13367 	}
13368       else if (s[1] == 'e' && s[2] == ' ')
13369 	{
13370 	  *s = '\0';
13371 	  forced_insn_length = 4;
13372 	  s += 3;
13373 	  break;
13374 	}
13375       /* Fall through.  */
13376     default:
13377       insn_error = _("unknown opcode");
13378       return;
13379     }
13380 
13381   if (mips_opts.noautoextend && !forced_insn_length)
13382     forced_insn_length = 2;
13383 
13384   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13385     {
13386       insn_error = _("unrecognized opcode");
13387       return;
13388     }
13389 
13390   argsstart = s;
13391   for (;;)
13392     {
13393       bfd_boolean ok;
13394 
13395       gas_assert (strcmp (insn->name, str) == 0);
13396 
13397       ok = is_opcode_valid_16 (insn);
13398       if (! ok)
13399 	{
13400 	  if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13401 	      && strcmp (insn->name, insn[1].name) == 0)
13402 	    {
13403 	      ++insn;
13404 	      continue;
13405 	    }
13406 	  else
13407 	    {
13408 	      if (!insn_error)
13409 		{
13410 		  static char buf[100];
13411 		  sprintf (buf,
13412 			   _("Opcode not supported on this processor: %s (%s)"),
13413 			   mips_cpu_info_from_arch (mips_opts.arch)->name,
13414 			   mips_cpu_info_from_isa (mips_opts.isa)->name);
13415 		  insn_error = buf;
13416 		}
13417 	      return;
13418 	    }
13419 	}
13420 
13421       create_insn (ip, insn);
13422       imm_expr.X_op = O_absent;
13423       imm_reloc[0] = BFD_RELOC_UNUSED;
13424       imm_reloc[1] = BFD_RELOC_UNUSED;
13425       imm_reloc[2] = BFD_RELOC_UNUSED;
13426       imm2_expr.X_op = O_absent;
13427       offset_expr.X_op = O_absent;
13428       offset_reloc[0] = BFD_RELOC_UNUSED;
13429       offset_reloc[1] = BFD_RELOC_UNUSED;
13430       offset_reloc[2] = BFD_RELOC_UNUSED;
13431       for (args = insn->args; 1; ++args)
13432 	{
13433 	  int c;
13434 
13435 	  if (*s == ' ')
13436 	    ++s;
13437 
13438 	  /* In this switch statement we call break if we did not find
13439              a match, continue if we did find a match, or return if we
13440              are done.  */
13441 
13442 	  c = *args;
13443 	  switch (c)
13444 	    {
13445 	    case '\0':
13446 	      if (*s == '\0')
13447 		{
13448 		  /* Stuff the immediate value in now, if we can.  */
13449 		  if (imm_expr.X_op == O_constant
13450 		      && *imm_reloc > BFD_RELOC_UNUSED
13451 		      && *imm_reloc != BFD_RELOC_MIPS16_GOT16
13452 		      && *imm_reloc != BFD_RELOC_MIPS16_CALL16
13453 		      && insn->pinfo != INSN_MACRO)
13454 		    {
13455 		      valueT tmp;
13456 
13457 		      switch (*offset_reloc)
13458 			{
13459 			  case BFD_RELOC_MIPS16_HI16_S:
13460 			    tmp = (imm_expr.X_add_number + 0x8000) >> 16;
13461 			    break;
13462 
13463 			  case BFD_RELOC_MIPS16_HI16:
13464 			    tmp = imm_expr.X_add_number >> 16;
13465 			    break;
13466 
13467 			  case BFD_RELOC_MIPS16_LO16:
13468 			    tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
13469 				  - 0x8000;
13470 			    break;
13471 
13472 			  case BFD_RELOC_UNUSED:
13473 			    tmp = imm_expr.X_add_number;
13474 			    break;
13475 
13476 			  default:
13477 			    internalError ();
13478 			}
13479 		      *offset_reloc = BFD_RELOC_UNUSED;
13480 
13481 		      mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13482 				    tmp, TRUE, forced_insn_length == 2,
13483 				    forced_insn_length == 4, &ip->insn_opcode,
13484 				    &ip->use_extend, &ip->extend);
13485 		      imm_expr.X_op = O_absent;
13486 		      *imm_reloc = BFD_RELOC_UNUSED;
13487 		    }
13488 
13489 		  return;
13490 		}
13491 	      break;
13492 
13493 	    case ',':
13494 	      if (*s++ == c)
13495 		continue;
13496 	      s--;
13497 	      switch (*++args)
13498 		{
13499 		case 'v':
13500 		  MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13501 		  continue;
13502 		case 'w':
13503 		  MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13504 		  continue;
13505 		}
13506 	      break;
13507 
13508 	    case '(':
13509 	    case ')':
13510 	      if (*s++ == c)
13511 		continue;
13512 	      break;
13513 
13514 	    case 'v':
13515 	    case 'w':
13516 	      if (s[0] != '$')
13517 		{
13518 		  if (c == 'v')
13519 		    MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13520 		  else
13521 		    MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13522 		  ++args;
13523 		  continue;
13524 		}
13525 	      /* Fall through.  */
13526 	    case 'x':
13527 	    case 'y':
13528 	    case 'z':
13529 	    case 'Z':
13530 	    case '0':
13531 	    case 'S':
13532 	    case 'R':
13533 	    case 'X':
13534 	    case 'Y':
13535   	      s_reset = s;
13536 	      if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
13537 		{
13538 		  if (c == 'v' || c == 'w')
13539 		    {
13540 		      if (c == 'v')
13541 			MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13542 		      else
13543 			MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13544 		      ++args;
13545 		      continue;
13546 		    }
13547 		  break;
13548 		}
13549 
13550 	      if (*s == ' ')
13551 		++s;
13552 	      if (args[1] != *s)
13553 		{
13554 		  if (c == 'v' || c == 'w')
13555 		    {
13556 		      regno = mips16_to_32_reg_map[lastregno];
13557 		      s = s_reset;
13558 		      ++args;
13559 		    }
13560 		}
13561 
13562 	      switch (c)
13563 		{
13564 		case 'x':
13565 		case 'y':
13566 		case 'z':
13567 		case 'v':
13568 		case 'w':
13569 		case 'Z':
13570 		  regno = mips32_to_16_reg_map[regno];
13571 		  break;
13572 
13573 		case '0':
13574 		  if (regno != 0)
13575 		    regno = ILLEGAL_REG;
13576 		  break;
13577 
13578 		case 'S':
13579 		  if (regno != SP)
13580 		    regno = ILLEGAL_REG;
13581 		  break;
13582 
13583 		case 'R':
13584 		  if (regno != RA)
13585 		    regno = ILLEGAL_REG;
13586 		  break;
13587 
13588 		case 'X':
13589 		case 'Y':
13590 		  if (regno == AT && mips_opts.at)
13591 		    {
13592 		      if (mips_opts.at == ATREG)
13593 			as_warn (_("used $at without \".set noat\""));
13594 		      else
13595 			as_warn (_("used $%u with \".set at=$%u\""),
13596 				 regno, mips_opts.at);
13597 		    }
13598 		  break;
13599 
13600 		default:
13601 		  internalError ();
13602 		}
13603 
13604 	      if (regno == ILLEGAL_REG)
13605 		break;
13606 
13607 	      switch (c)
13608 		{
13609 		case 'x':
13610 		case 'v':
13611 		  MIPS16_INSERT_OPERAND (RX, *ip, regno);
13612 		  break;
13613 		case 'y':
13614 		case 'w':
13615 		  MIPS16_INSERT_OPERAND (RY, *ip, regno);
13616 		  break;
13617 		case 'z':
13618 		  MIPS16_INSERT_OPERAND (RZ, *ip, regno);
13619 		  break;
13620 		case 'Z':
13621 		  MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
13622 		case '0':
13623 		case 'S':
13624 		case 'R':
13625 		  break;
13626 		case 'X':
13627 		  MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
13628 		  break;
13629 		case 'Y':
13630 		  regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
13631 		  MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
13632 		  break;
13633 		default:
13634 		  internalError ();
13635 		}
13636 
13637 	      lastregno = regno;
13638 	      continue;
13639 
13640 	    case 'P':
13641 	      if (strncmp (s, "$pc", 3) == 0)
13642 		{
13643 		  s += 3;
13644 		  continue;
13645 		}
13646 	      break;
13647 
13648 	    case '5':
13649 	    case 'H':
13650 	    case 'W':
13651 	    case 'D':
13652 	    case 'j':
13653 	    case 'V':
13654 	    case 'C':
13655 	    case 'U':
13656 	    case 'k':
13657 	    case 'K':
13658 	      i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13659 	      if (i > 0)
13660 		{
13661 		  if (imm_expr.X_op != O_constant)
13662 		    {
13663 		      forced_insn_length = 4;
13664 		      ip->use_extend = TRUE;
13665 		      ip->extend = 0;
13666 		    }
13667 		  else
13668 		    {
13669 		      /* We need to relax this instruction.  */
13670 		      *offset_reloc = *imm_reloc;
13671 		      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13672 		    }
13673 		  s = expr_end;
13674 		  continue;
13675 		}
13676 	      *imm_reloc = BFD_RELOC_UNUSED;
13677 	      /* Fall through.  */
13678 	    case '<':
13679 	    case '>':
13680 	    case '[':
13681 	    case ']':
13682 	    case '4':
13683 	    case '8':
13684 	      my_getExpression (&imm_expr, s);
13685 	      if (imm_expr.X_op == O_register)
13686 		{
13687 		  /* What we thought was an expression turned out to
13688                      be a register.  */
13689 
13690 		  if (s[0] == '(' && args[1] == '(')
13691 		    {
13692 		      /* It looks like the expression was omitted
13693 			 before a register indirection, which means
13694 			 that the expression is implicitly zero.  We
13695 			 still set up imm_expr, so that we handle
13696 			 explicit extensions correctly.  */
13697 		      imm_expr.X_op = O_constant;
13698 		      imm_expr.X_add_number = 0;
13699 		      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13700 		      continue;
13701 		    }
13702 
13703 		  break;
13704 		}
13705 
13706 	      /* We need to relax this instruction.  */
13707 	      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13708 	      s = expr_end;
13709 	      continue;
13710 
13711 	    case 'p':
13712 	    case 'q':
13713 	    case 'A':
13714 	    case 'B':
13715 	    case 'E':
13716 	      /* We use offset_reloc rather than imm_reloc for the PC
13717                  relative operands.  This lets macros with both
13718                  immediate and address operands work correctly.  */
13719 	      my_getExpression (&offset_expr, s);
13720 
13721 	      if (offset_expr.X_op == O_register)
13722 		break;
13723 
13724 	      /* We need to relax this instruction.  */
13725 	      *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13726 	      s = expr_end;
13727 	      continue;
13728 
13729 	    case '6':		/* break code */
13730 	      my_getExpression (&imm_expr, s);
13731 	      check_absolute_expr (ip, &imm_expr);
13732 	      if ((unsigned long) imm_expr.X_add_number > 63)
13733 		as_warn (_("Invalid value for `%s' (%lu)"),
13734 			 ip->insn_mo->name,
13735 			 (unsigned long) imm_expr.X_add_number);
13736 	      MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
13737 	      imm_expr.X_op = O_absent;
13738 	      s = expr_end;
13739 	      continue;
13740 
13741 	    case 'a':		/* 26 bit address */
13742 	      my_getExpression (&offset_expr, s);
13743 	      s = expr_end;
13744 	      *offset_reloc = BFD_RELOC_MIPS16_JMP;
13745 	      ip->insn_opcode <<= 16;
13746 	      continue;
13747 
13748 	    case 'l':		/* register list for entry macro */
13749 	    case 'L':		/* register list for exit macro */
13750 	      {
13751 		int mask;
13752 
13753 		if (c == 'l')
13754 		  mask = 0;
13755 		else
13756 		  mask = 7 << 3;
13757 		while (*s != '\0')
13758 		  {
13759 		    unsigned int freg, reg1, reg2;
13760 
13761 		    while (*s == ' ' || *s == ',')
13762 		      ++s;
13763 		    if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13764 		      freg = 0;
13765 		    else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13766 		      freg = 1;
13767 		    else
13768 		      {
13769 			as_bad (_("can't parse register list"));
13770 			break;
13771 		      }
13772 		    if (*s == ' ')
13773 		      ++s;
13774 		    if (*s != '-')
13775 		      reg2 = reg1;
13776 		    else
13777 		      {
13778 			++s;
13779 			if (!reg_lookup (&s, freg ? RTYPE_FPU
13780 					 : (RTYPE_GP | RTYPE_NUM), &reg2))
13781 			  {
13782 			    as_bad (_("invalid register list"));
13783 			    break;
13784 			  }
13785 		      }
13786 		    if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13787 		      {
13788 			mask &= ~ (7 << 3);
13789 			mask |= 5 << 3;
13790 		      }
13791 		    else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13792 		      {
13793 			mask &= ~ (7 << 3);
13794 			mask |= 6 << 3;
13795 		      }
13796 		    else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13797 		      mask |= (reg2 - 3) << 3;
13798 		    else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13799 		      mask |= (reg2 - 15) << 1;
13800 		    else if (reg1 == RA && reg2 == RA)
13801 		      mask |= 1;
13802 		    else
13803 		      {
13804 			as_bad (_("invalid register list"));
13805 			break;
13806 		      }
13807 		  }
13808 		/* The mask is filled in in the opcode table for the
13809                    benefit of the disassembler.  We remove it before
13810                    applying the actual mask.  */
13811 		ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13812 		ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13813 	      }
13814 	    continue;
13815 
13816 	    case 'm':		/* Register list for save insn.  */
13817 	    case 'M':		/* Register list for restore insn.  */
13818 	      {
13819 		int opcode = 0;
13820 		int framesz = 0, seen_framesz = 0;
13821 		int nargs = 0, statics = 0, sregs = 0;
13822 
13823 		while (*s != '\0')
13824 		  {
13825 		    unsigned int reg1, reg2;
13826 
13827 		    SKIP_SPACE_TABS (s);
13828 		    while (*s == ',')
13829 		      ++s;
13830 		    SKIP_SPACE_TABS (s);
13831 
13832 		    my_getExpression (&imm_expr, s);
13833 		    if (imm_expr.X_op == O_constant)
13834 		      {
13835 			/* Handle the frame size.  */
13836 			if (seen_framesz)
13837 			  {
13838 			    as_bad (_("more than one frame size in list"));
13839 			    break;
13840 			  }
13841 			seen_framesz = 1;
13842 			framesz = imm_expr.X_add_number;
13843 			imm_expr.X_op = O_absent;
13844 			s = expr_end;
13845 			continue;
13846 		      }
13847 
13848 		    if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13849 		      {
13850 			as_bad (_("can't parse register list"));
13851 			break;
13852 		      }
13853 
13854 		    while (*s == ' ')
13855 		      ++s;
13856 
13857 		    if (*s != '-')
13858 		      reg2 = reg1;
13859 		    else
13860 		      {
13861 			++s;
13862 			if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13863 			    || reg2 < reg1)
13864 			  {
13865 			    as_bad (_("can't parse register list"));
13866 			    break;
13867 			  }
13868 		      }
13869 
13870 		    while (reg1 <= reg2)
13871 		      {
13872 			if (reg1 >= 4 && reg1 <= 7)
13873 			  {
13874 			    if (!seen_framesz)
13875 				/* args $a0-$a3 */
13876 				nargs |= 1 << (reg1 - 4);
13877 			    else
13878 				/* statics $a0-$a3 */
13879 				statics |= 1 << (reg1 - 4);
13880 			  }
13881 			else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13882 			  {
13883 			    /* $s0-$s8 */
13884 			    sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13885 			  }
13886 			else if (reg1 == 31)
13887 			  {
13888 			    /* Add $ra to insn.  */
13889 			    opcode |= 0x40;
13890 			  }
13891 			else
13892 			  {
13893 			    as_bad (_("unexpected register in list"));
13894 			    break;
13895 			  }
13896 			if (++reg1 == 24)
13897 			  reg1 = 30;
13898 		      }
13899 		  }
13900 
13901 		/* Encode args/statics combination.  */
13902 		if (nargs & statics)
13903 		  as_bad (_("arg/static registers overlap"));
13904 		else if (nargs == 0xf)
13905 		  /* All $a0-$a3 are args.  */
13906 		  opcode |= MIPS16_ALL_ARGS << 16;
13907 		else if (statics == 0xf)
13908 		  /* All $a0-$a3 are statics.  */
13909 		  opcode |= MIPS16_ALL_STATICS << 16;
13910 		else
13911 		  {
13912 		    int narg = 0, nstat = 0;
13913 
13914 		    /* Count arg registers.  */
13915 		    while (nargs & 0x1)
13916 		      {
13917 			nargs >>= 1;
13918 			narg++;
13919 		      }
13920 		    if (nargs != 0)
13921 		      as_bad (_("invalid arg register list"));
13922 
13923 		    /* Count static registers.  */
13924 		    while (statics & 0x8)
13925 		      {
13926 			statics = (statics << 1) & 0xf;
13927 			nstat++;
13928 		      }
13929 		    if (statics != 0)
13930 		      as_bad (_("invalid static register list"));
13931 
13932 		    /* Encode args/statics.  */
13933 		    opcode |= ((narg << 2) | nstat) << 16;
13934 		  }
13935 
13936 		/* Encode $s0/$s1.  */
13937 		if (sregs & (1 << 0))		/* $s0 */
13938 		  opcode |= 0x20;
13939 		if (sregs & (1 << 1))		/* $s1 */
13940 		  opcode |= 0x10;
13941 		sregs >>= 2;
13942 
13943 		if (sregs != 0)
13944 		  {
13945 		    /* Count regs $s2-$s8.  */
13946 		    int nsreg = 0;
13947 		    while (sregs & 1)
13948 		      {
13949 			sregs >>= 1;
13950 			nsreg++;
13951 		      }
13952 		    if (sregs != 0)
13953 		      as_bad (_("invalid static register list"));
13954 		    /* Encode $s2-$s8. */
13955 		    opcode |= nsreg << 24;
13956 		  }
13957 
13958 		/* Encode frame size.  */
13959 		if (!seen_framesz)
13960 		  as_bad (_("missing frame size"));
13961 		else if ((framesz & 7) != 0 || framesz < 0
13962 			 || framesz > 0xff * 8)
13963 		  as_bad (_("invalid frame size"));
13964 		else if (framesz != 128 || (opcode >> 16) != 0)
13965 		  {
13966 		    framesz /= 8;
13967 		    opcode |= (((framesz & 0xf0) << 16)
13968 			     | (framesz & 0x0f));
13969 		  }
13970 
13971 		/* Finally build the instruction.  */
13972 		if ((opcode >> 16) != 0 || framesz == 0)
13973 		  {
13974 		    ip->use_extend = TRUE;
13975 		    ip->extend = opcode >> 16;
13976 		  }
13977 		ip->insn_opcode |= opcode & 0x7f;
13978 	      }
13979 	    continue;
13980 
13981 	    case 'e':		/* extend code */
13982 	      my_getExpression (&imm_expr, s);
13983 	      check_absolute_expr (ip, &imm_expr);
13984 	      if ((unsigned long) imm_expr.X_add_number > 0x7ff)
13985 		{
13986 		  as_warn (_("Invalid value for `%s' (%lu)"),
13987 			   ip->insn_mo->name,
13988 			   (unsigned long) imm_expr.X_add_number);
13989 		  imm_expr.X_add_number &= 0x7ff;
13990 		}
13991 	      ip->insn_opcode |= imm_expr.X_add_number;
13992 	      imm_expr.X_op = O_absent;
13993 	      s = expr_end;
13994 	      continue;
13995 
13996 	    default:
13997 	      internalError ();
13998 	    }
13999 	  break;
14000 	}
14001 
14002       /* Args don't match.  */
14003       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14004 	  strcmp (insn->name, insn[1].name) == 0)
14005 	{
14006 	  ++insn;
14007 	  s = argsstart;
14008 	  continue;
14009 	}
14010 
14011       insn_error = _("illegal operands");
14012 
14013       return;
14014     }
14015 }
14016 
14017 /* This structure holds information we know about a mips16 immediate
14018    argument type.  */
14019 
14020 struct mips16_immed_operand
14021 {
14022   /* The type code used in the argument string in the opcode table.  */
14023   int type;
14024   /* The number of bits in the short form of the opcode.  */
14025   int nbits;
14026   /* The number of bits in the extended form of the opcode.  */
14027   int extbits;
14028   /* The amount by which the short form is shifted when it is used;
14029      for example, the sw instruction has a shift count of 2.  */
14030   int shift;
14031   /* The amount by which the short form is shifted when it is stored
14032      into the instruction code.  */
14033   int op_shift;
14034   /* Non-zero if the short form is unsigned.  */
14035   int unsp;
14036   /* Non-zero if the extended form is unsigned.  */
14037   int extu;
14038   /* Non-zero if the value is PC relative.  */
14039   int pcrel;
14040 };
14041 
14042 /* The mips16 immediate operand types.  */
14043 
14044 static const struct mips16_immed_operand mips16_immed_operands[] =
14045 {
14046   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14047   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14048   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
14049   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
14050   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14051   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14052   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14053   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14054   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14055   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14056   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14057   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14058   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14059   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14060   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14061   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14062   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14063   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14064   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14065   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14066   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14067 };
14068 
14069 #define MIPS16_NUM_IMMED \
14070   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14071 
14072 /* Handle a mips16 instruction with an immediate value.  This or's the
14073    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
14074    whether an extended value is needed; if one is needed, it sets
14075    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
14076    If SMALL is true, an unextended opcode was explicitly requested.
14077    If EXT is true, an extended opcode was explicitly requested.  If
14078    WARN is true, warn if EXT does not match reality.  */
14079 
14080 static void
14081 mips16_immed (char *file, unsigned int line, int type, offsetT val,
14082 	      bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
14083 	      unsigned long *insn, bfd_boolean *use_extend,
14084 	      unsigned short *extend)
14085 {
14086   const struct mips16_immed_operand *op;
14087   int mintiny, maxtiny;
14088   bfd_boolean needext;
14089 
14090   op = mips16_immed_operands;
14091   while (op->type != type)
14092     {
14093       ++op;
14094       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
14095     }
14096 
14097   if (op->unsp)
14098     {
14099       if (type == '<' || type == '>' || type == '[' || type == ']')
14100 	{
14101 	  mintiny = 1;
14102 	  maxtiny = 1 << op->nbits;
14103 	}
14104       else
14105 	{
14106 	  mintiny = 0;
14107 	  maxtiny = (1 << op->nbits) - 1;
14108 	}
14109     }
14110   else
14111     {
14112       mintiny = - (1 << (op->nbits - 1));
14113       maxtiny = (1 << (op->nbits - 1)) - 1;
14114     }
14115 
14116   /* Branch offsets have an implicit 0 in the lowest bit.  */
14117   if (type == 'p' || type == 'q')
14118     val /= 2;
14119 
14120   if ((val & ((1 << op->shift) - 1)) != 0
14121       || val < (mintiny << op->shift)
14122       || val > (maxtiny << op->shift))
14123     needext = TRUE;
14124   else
14125     needext = FALSE;
14126 
14127   if (warn && ext && ! needext)
14128     as_warn_where (file, line,
14129 		   _("extended operand requested but not required"));
14130   if (small && needext)
14131     as_bad_where (file, line, _("invalid unextended operand value"));
14132 
14133   if (small || (! ext && ! needext))
14134     {
14135       int insnval;
14136 
14137       *use_extend = FALSE;
14138       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14139       insnval <<= op->op_shift;
14140       *insn |= insnval;
14141     }
14142   else
14143     {
14144       long minext, maxext;
14145       int extval;
14146 
14147       if (op->extu)
14148 	{
14149 	  minext = 0;
14150 	  maxext = (1 << op->extbits) - 1;
14151 	}
14152       else
14153 	{
14154 	  minext = - (1 << (op->extbits - 1));
14155 	  maxext = (1 << (op->extbits - 1)) - 1;
14156 	}
14157       if (val < minext || val > maxext)
14158 	as_bad_where (file, line,
14159 		      _("operand value out of range for instruction"));
14160 
14161       *use_extend = TRUE;
14162       if (op->extbits == 16)
14163 	{
14164 	  extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14165 	  val &= 0x1f;
14166 	}
14167       else if (op->extbits == 15)
14168 	{
14169 	  extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14170 	  val &= 0xf;
14171 	}
14172       else
14173 	{
14174 	  extval = ((val & 0x1f) << 6) | (val & 0x20);
14175 	  val = 0;
14176 	}
14177 
14178       *extend = (unsigned short) extval;
14179       *insn |= val;
14180     }
14181 }
14182 
14183 struct percent_op_match
14184 {
14185   const char *str;
14186   bfd_reloc_code_real_type reloc;
14187 };
14188 
14189 static const struct percent_op_match mips_percent_op[] =
14190 {
14191   {"%lo", BFD_RELOC_LO16},
14192 #ifdef OBJ_ELF
14193   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14194   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14195   {"%call16", BFD_RELOC_MIPS_CALL16},
14196   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14197   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14198   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14199   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14200   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14201   {"%got", BFD_RELOC_MIPS_GOT16},
14202   {"%gp_rel", BFD_RELOC_GPREL16},
14203   {"%half", BFD_RELOC_16},
14204   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14205   {"%higher", BFD_RELOC_MIPS_HIGHER},
14206   {"%neg", BFD_RELOC_MIPS_SUB},
14207   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14208   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14209   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14210   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14211   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14212   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14213   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14214 #endif
14215   {"%hi", BFD_RELOC_HI16_S}
14216 };
14217 
14218 static const struct percent_op_match mips16_percent_op[] =
14219 {
14220   {"%lo", BFD_RELOC_MIPS16_LO16},
14221   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14222   {"%got", BFD_RELOC_MIPS16_GOT16},
14223   {"%call16", BFD_RELOC_MIPS16_CALL16},
14224   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14225   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14226   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14227   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14228   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14229   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14230   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14231   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14232 };
14233 
14234 
14235 /* Return true if *STR points to a relocation operator.  When returning true,
14236    move *STR over the operator and store its relocation code in *RELOC.
14237    Leave both *STR and *RELOC alone when returning false.  */
14238 
14239 static bfd_boolean
14240 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14241 {
14242   const struct percent_op_match *percent_op;
14243   size_t limit, i;
14244 
14245   if (mips_opts.mips16)
14246     {
14247       percent_op = mips16_percent_op;
14248       limit = ARRAY_SIZE (mips16_percent_op);
14249     }
14250   else
14251     {
14252       percent_op = mips_percent_op;
14253       limit = ARRAY_SIZE (mips_percent_op);
14254     }
14255 
14256   for (i = 0; i < limit; i++)
14257     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14258       {
14259 	int len = strlen (percent_op[i].str);
14260 
14261 	if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14262 	  continue;
14263 
14264 	*str += strlen (percent_op[i].str);
14265 	*reloc = percent_op[i].reloc;
14266 
14267 	/* Check whether the output BFD supports this relocation.
14268 	   If not, issue an error and fall back on something safe.  */
14269 	if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14270 	  {
14271 	    as_bad (_("relocation %s isn't supported by the current ABI"),
14272 		    percent_op[i].str);
14273 	    *reloc = BFD_RELOC_UNUSED;
14274 	  }
14275 	return TRUE;
14276       }
14277   return FALSE;
14278 }
14279 
14280 
14281 /* Parse string STR as a 16-bit relocatable operand.  Store the
14282    expression in *EP and the relocations in the array starting
14283    at RELOC.  Return the number of relocation operators used.
14284 
14285    On exit, EXPR_END points to the first character after the expression.  */
14286 
14287 static size_t
14288 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14289 		       char *str)
14290 {
14291   bfd_reloc_code_real_type reversed_reloc[3];
14292   size_t reloc_index, i;
14293   int crux_depth, str_depth;
14294   char *crux;
14295 
14296   /* Search for the start of the main expression, recoding relocations
14297      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14298      of the main expression and with CRUX_DEPTH containing the number
14299      of open brackets at that point.  */
14300   reloc_index = -1;
14301   str_depth = 0;
14302   do
14303     {
14304       reloc_index++;
14305       crux = str;
14306       crux_depth = str_depth;
14307 
14308       /* Skip over whitespace and brackets, keeping count of the number
14309 	 of brackets.  */
14310       while (*str == ' ' || *str == '\t' || *str == '(')
14311 	if (*str++ == '(')
14312 	  str_depth++;
14313     }
14314   while (*str == '%'
14315 	 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14316 	 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14317 
14318   my_getExpression (ep, crux);
14319   str = expr_end;
14320 
14321   /* Match every open bracket.  */
14322   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14323     if (*str++ == ')')
14324       crux_depth--;
14325 
14326   if (crux_depth > 0)
14327     as_bad (_("unclosed '('"));
14328 
14329   expr_end = str;
14330 
14331   if (reloc_index != 0)
14332     {
14333       prev_reloc_op_frag = frag_now;
14334       for (i = 0; i < reloc_index; i++)
14335 	reloc[i] = reversed_reloc[reloc_index - 1 - i];
14336     }
14337 
14338   return reloc_index;
14339 }
14340 
14341 static void
14342 my_getExpression (expressionS *ep, char *str)
14343 {
14344   char *save_in;
14345 
14346   save_in = input_line_pointer;
14347   input_line_pointer = str;
14348   expression (ep);
14349   expr_end = input_line_pointer;
14350   input_line_pointer = save_in;
14351 }
14352 
14353 char *
14354 md_atof (int type, char *litP, int *sizeP)
14355 {
14356   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14357 }
14358 
14359 void
14360 md_number_to_chars (char *buf, valueT val, int n)
14361 {
14362   if (target_big_endian)
14363     number_to_chars_bigendian (buf, val, n);
14364   else
14365     number_to_chars_littleendian (buf, val, n);
14366 }
14367 
14368 #ifdef OBJ_ELF
14369 static int support_64bit_objects(void)
14370 {
14371   const char **list, **l;
14372   int yes;
14373 
14374   list = bfd_target_list ();
14375   for (l = list; *l != NULL; l++)
14376     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14377 	|| strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14378       break;
14379   yes = (*l != NULL);
14380   free (list);
14381   return yes;
14382 }
14383 #endif /* OBJ_ELF */
14384 
14385 const char *md_shortopts = "O::g::G:";
14386 
14387 enum options
14388   {
14389     OPTION_MARCH = OPTION_MD_BASE,
14390     OPTION_MTUNE,
14391     OPTION_MIPS1,
14392     OPTION_MIPS2,
14393     OPTION_MIPS3,
14394     OPTION_MIPS4,
14395     OPTION_MIPS5,
14396     OPTION_MIPS32,
14397     OPTION_MIPS64,
14398     OPTION_MIPS32R2,
14399     OPTION_MIPS64R2,
14400     OPTION_MIPS16,
14401     OPTION_NO_MIPS16,
14402     OPTION_MIPS3D,
14403     OPTION_NO_MIPS3D,
14404     OPTION_MDMX,
14405     OPTION_NO_MDMX,
14406     OPTION_DSP,
14407     OPTION_NO_DSP,
14408     OPTION_MT,
14409     OPTION_NO_MT,
14410     OPTION_SMARTMIPS,
14411     OPTION_NO_SMARTMIPS,
14412     OPTION_DSPR2,
14413     OPTION_NO_DSPR2,
14414     OPTION_MICROMIPS,
14415     OPTION_NO_MICROMIPS,
14416     OPTION_MCU,
14417     OPTION_NO_MCU,
14418     OPTION_COMPAT_ARCH_BASE,
14419     OPTION_M4650,
14420     OPTION_NO_M4650,
14421     OPTION_M4010,
14422     OPTION_NO_M4010,
14423     OPTION_M4100,
14424     OPTION_NO_M4100,
14425     OPTION_M3900,
14426     OPTION_NO_M3900,
14427     OPTION_M7000_HILO_FIX,
14428     OPTION_MNO_7000_HILO_FIX,
14429     OPTION_FIX_24K,
14430     OPTION_NO_FIX_24K,
14431     OPTION_FIX_LOONGSON2F_JUMP,
14432     OPTION_NO_FIX_LOONGSON2F_JUMP,
14433     OPTION_FIX_LOONGSON2F_NOP,
14434     OPTION_NO_FIX_LOONGSON2F_NOP,
14435     OPTION_FIX_VR4120,
14436     OPTION_NO_FIX_VR4120,
14437     OPTION_FIX_VR4130,
14438     OPTION_NO_FIX_VR4130,
14439     OPTION_FIX_CN63XXP1,
14440     OPTION_NO_FIX_CN63XXP1,
14441     OPTION_TRAP,
14442     OPTION_BREAK,
14443     OPTION_EB,
14444     OPTION_EL,
14445     OPTION_FP32,
14446     OPTION_GP32,
14447     OPTION_CONSTRUCT_FLOATS,
14448     OPTION_NO_CONSTRUCT_FLOATS,
14449     OPTION_FP64,
14450     OPTION_GP64,
14451     OPTION_RELAX_BRANCH,
14452     OPTION_NO_RELAX_BRANCH,
14453     OPTION_MSHARED,
14454     OPTION_MNO_SHARED,
14455     OPTION_MSYM32,
14456     OPTION_MNO_SYM32,
14457     OPTION_SOFT_FLOAT,
14458     OPTION_HARD_FLOAT,
14459     OPTION_SINGLE_FLOAT,
14460     OPTION_DOUBLE_FLOAT,
14461     OPTION_32,
14462     OPTION_TRAP_ZERO_JUMP,
14463     OPTION_NO_TRAP_ZERO_JUMP,
14464 #ifdef OBJ_ELF
14465     OPTION_CALL_SHARED,
14466     OPTION_CALL_NONPIC,
14467     OPTION_NON_SHARED,
14468     OPTION_XGOT,
14469     OPTION_MABI,
14470     OPTION_N32,
14471     OPTION_64,
14472     OPTION_MDEBUG,
14473     OPTION_NO_MDEBUG,
14474     OPTION_PDR,
14475     OPTION_NO_PDR,
14476     OPTION_MVXWORKS_PIC,
14477 #endif /* OBJ_ELF */
14478     OPTION_FIX_LOONGSON2F_BTB,
14479     OPTION_NO_FIX_LOONGSON2F_BTB,
14480     OPTION_END_OF_ENUM
14481   };
14482 
14483 struct option md_longopts[] =
14484 {
14485   /* Options which specify architecture.  */
14486   {"march", required_argument, NULL, OPTION_MARCH},
14487   {"mtune", required_argument, NULL, OPTION_MTUNE},
14488   {"mips0", no_argument, NULL, OPTION_MIPS1},
14489   {"mips1", no_argument, NULL, OPTION_MIPS1},
14490   {"mips2", no_argument, NULL, OPTION_MIPS2},
14491   {"mips3", no_argument, NULL, OPTION_MIPS3},
14492   {"mips4", no_argument, NULL, OPTION_MIPS4},
14493   {"mips5", no_argument, NULL, OPTION_MIPS5},
14494   {"mips32", no_argument, NULL, OPTION_MIPS32},
14495   {"mips64", no_argument, NULL, OPTION_MIPS64},
14496   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
14497   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
14498 
14499   /* Options which specify Application Specific Extensions (ASEs).  */
14500   {"mips16", no_argument, NULL, OPTION_MIPS16},
14501   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
14502   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
14503   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
14504   {"mdmx", no_argument, NULL, OPTION_MDMX},
14505   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
14506   {"mdsp", no_argument, NULL, OPTION_DSP},
14507   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
14508   {"mmt", no_argument, NULL, OPTION_MT},
14509   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
14510   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
14511   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
14512   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
14513   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
14514   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14515   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
14516   {"mmcu", no_argument, NULL, OPTION_MCU},
14517   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
14518 
14519   /* Old-style architecture options.  Don't add more of these.  */
14520   {"m4650", no_argument, NULL, OPTION_M4650},
14521   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
14522   {"m4010", no_argument, NULL, OPTION_M4010},
14523   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
14524   {"m4100", no_argument, NULL, OPTION_M4100},
14525   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
14526   {"m3900", no_argument, NULL, OPTION_M3900},
14527   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14528 
14529   /* Options which enable bug fixes.  */
14530   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
14531   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14532   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14533   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14534   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14535   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14536   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
14537   {"mfix-loongson2f-btb", no_argument, NULL, OPTION_FIX_LOONGSON2F_BTB},
14538   {"mno-fix-loongson2f-btb", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_BTB},
14539   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
14540   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
14541   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
14542   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
14543   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
14544   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
14545   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14546   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
14547 
14548   /* Miscellaneous options.  */
14549   {"trap", no_argument, NULL, OPTION_TRAP},
14550   {"no-break", no_argument, NULL, OPTION_TRAP},
14551   {"break", no_argument, NULL, OPTION_BREAK},
14552   {"no-trap", no_argument, NULL, OPTION_BREAK},
14553   {"EB", no_argument, NULL, OPTION_EB},
14554   {"EL", no_argument, NULL, OPTION_EL},
14555   {"mfp32", no_argument, NULL, OPTION_FP32},
14556   {"mgp32", no_argument, NULL, OPTION_GP32},
14557   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
14558   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
14559   {"mfp64", no_argument, NULL, OPTION_FP64},
14560   {"mgp64", no_argument, NULL, OPTION_GP64},
14561   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14562   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
14563   {"mshared", no_argument, NULL, OPTION_MSHARED},
14564   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
14565   {"msym32", no_argument, NULL, OPTION_MSYM32},
14566   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
14567   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14568   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
14569   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14570   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
14571 
14572   /* Strictly speaking this next option is ELF specific,
14573      but we allow it for other ports as well in order to
14574      make testing easier.  */
14575   {"32",          no_argument, NULL, OPTION_32},
14576 
14577   {"mtrap-zero-jump", no_argument, NULL, OPTION_TRAP_ZERO_JUMP},
14578   {"mno-trap-zero-jump", no_argument, NULL, OPTION_NO_TRAP_ZERO_JUMP},
14579 
14580   /* ELF-specific options.  */
14581 #ifdef OBJ_ELF
14582   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
14583   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
14584   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
14585   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
14586   {"xgot",        no_argument, NULL, OPTION_XGOT},
14587   {"mabi", required_argument, NULL, OPTION_MABI},
14588   {"n32",         no_argument, NULL, OPTION_N32},
14589   {"64",          no_argument, NULL, OPTION_64},
14590   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
14591   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
14592   {"mpdr", no_argument, NULL, OPTION_PDR},
14593   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
14594   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
14595 #endif /* OBJ_ELF */
14596 
14597   {NULL, no_argument, NULL, 0}
14598 };
14599 size_t md_longopts_size = sizeof (md_longopts);
14600 
14601 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14602    NEW_VALUE.  Warn if another value was already specified.  Note:
14603    we have to defer parsing the -march and -mtune arguments in order
14604    to handle 'from-abi' correctly, since the ABI might be specified
14605    in a later argument.  */
14606 
14607 static void
14608 mips_set_option_string (const char **string_ptr, const char *new_value)
14609 {
14610   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14611     as_warn (_("A different %s was already specified, is now %s"),
14612 	     string_ptr == &mips_arch_string ? "-march" : "-mtune",
14613 	     new_value);
14614 
14615   *string_ptr = new_value;
14616 }
14617 
14618 int
14619 md_parse_option (int c, char *arg)
14620 {
14621   switch (c)
14622     {
14623     case OPTION_CONSTRUCT_FLOATS:
14624       mips_disable_float_construction = 0;
14625       break;
14626 
14627     case OPTION_NO_CONSTRUCT_FLOATS:
14628       mips_disable_float_construction = 1;
14629       break;
14630 
14631     case OPTION_TRAP:
14632       mips_trap = 1;
14633       break;
14634 
14635     case OPTION_BREAK:
14636       mips_trap = 0;
14637       break;
14638 
14639     case OPTION_EB:
14640       target_big_endian = 1;
14641       break;
14642 
14643     case OPTION_EL:
14644       target_big_endian = 0;
14645       break;
14646 
14647     case 'O':
14648       if (arg == NULL)
14649 	mips_optimize = 1;
14650       else if (arg[0] == '0')
14651 	mips_optimize = 0;
14652       else if (arg[0] == '1')
14653 	mips_optimize = 1;
14654       else
14655 	mips_optimize = 2;
14656       break;
14657 
14658     case 'g':
14659       if (arg == NULL)
14660 	mips_debug = 2;
14661       else
14662 	mips_debug = atoi (arg);
14663       break;
14664 
14665     case OPTION_MIPS1:
14666       file_mips_isa = ISA_MIPS1;
14667       break;
14668 
14669     case OPTION_MIPS2:
14670       file_mips_isa = ISA_MIPS2;
14671       break;
14672 
14673     case OPTION_MIPS3:
14674       file_mips_isa = ISA_MIPS3;
14675       break;
14676 
14677     case OPTION_MIPS4:
14678       file_mips_isa = ISA_MIPS4;
14679       break;
14680 
14681     case OPTION_MIPS5:
14682       file_mips_isa = ISA_MIPS5;
14683       break;
14684 
14685     case OPTION_MIPS32:
14686       file_mips_isa = ISA_MIPS32;
14687       break;
14688 
14689     case OPTION_MIPS32R2:
14690       file_mips_isa = ISA_MIPS32R2;
14691       break;
14692 
14693     case OPTION_MIPS64R2:
14694       file_mips_isa = ISA_MIPS64R2;
14695       break;
14696 
14697     case OPTION_MIPS64:
14698       file_mips_isa = ISA_MIPS64;
14699       break;
14700 
14701     case OPTION_MTUNE:
14702       mips_set_option_string (&mips_tune_string, arg);
14703       break;
14704 
14705     case OPTION_MARCH:
14706       mips_set_option_string (&mips_arch_string, arg);
14707       break;
14708 
14709     case OPTION_M4650:
14710       mips_set_option_string (&mips_arch_string, "4650");
14711       mips_set_option_string (&mips_tune_string, "4650");
14712       break;
14713 
14714     case OPTION_NO_M4650:
14715       break;
14716 
14717     case OPTION_M4010:
14718       mips_set_option_string (&mips_arch_string, "4010");
14719       mips_set_option_string (&mips_tune_string, "4010");
14720       break;
14721 
14722     case OPTION_NO_M4010:
14723       break;
14724 
14725     case OPTION_M4100:
14726       mips_set_option_string (&mips_arch_string, "4100");
14727       mips_set_option_string (&mips_tune_string, "4100");
14728       break;
14729 
14730     case OPTION_NO_M4100:
14731       break;
14732 
14733     case OPTION_M3900:
14734       mips_set_option_string (&mips_arch_string, "3900");
14735       mips_set_option_string (&mips_tune_string, "3900");
14736       break;
14737 
14738     case OPTION_NO_M3900:
14739       break;
14740 
14741     case OPTION_MDMX:
14742       mips_opts.ase_mdmx = 1;
14743       break;
14744 
14745     case OPTION_NO_MDMX:
14746       mips_opts.ase_mdmx = 0;
14747       break;
14748 
14749     case OPTION_DSP:
14750       mips_opts.ase_dsp = 1;
14751       mips_opts.ase_dspr2 = 0;
14752       break;
14753 
14754     case OPTION_NO_DSP:
14755       mips_opts.ase_dsp = 0;
14756       mips_opts.ase_dspr2 = 0;
14757       break;
14758 
14759     case OPTION_DSPR2:
14760       mips_opts.ase_dspr2 = 1;
14761       mips_opts.ase_dsp = 1;
14762       break;
14763 
14764     case OPTION_NO_DSPR2:
14765       mips_opts.ase_dspr2 = 0;
14766       mips_opts.ase_dsp = 0;
14767       break;
14768 
14769     case OPTION_MT:
14770       mips_opts.ase_mt = 1;
14771       break;
14772 
14773     case OPTION_NO_MT:
14774       mips_opts.ase_mt = 0;
14775       break;
14776 
14777     case OPTION_MCU:
14778       mips_opts.ase_mcu = 1;
14779       break;
14780 
14781     case OPTION_NO_MCU:
14782       mips_opts.ase_mcu = 0;
14783       break;
14784 
14785     case OPTION_MICROMIPS:
14786       if (mips_opts.mips16 == 1)
14787 	{
14788 	  as_bad (_("-mmicromips cannot be used with -mips16"));
14789 	  return 0;
14790 	}
14791       mips_opts.micromips = 1;
14792       mips_no_prev_insn ();
14793       break;
14794 
14795     case OPTION_NO_MICROMIPS:
14796       mips_opts.micromips = 0;
14797       mips_no_prev_insn ();
14798       break;
14799 
14800     case OPTION_MIPS16:
14801       if (mips_opts.micromips == 1)
14802 	{
14803 	  as_bad (_("-mips16 cannot be used with -micromips"));
14804 	  return 0;
14805 	}
14806       mips_opts.mips16 = 1;
14807       mips_no_prev_insn ();
14808       break;
14809 
14810     case OPTION_NO_MIPS16:
14811       mips_opts.mips16 = 0;
14812       mips_no_prev_insn ();
14813       break;
14814 
14815     case OPTION_MIPS3D:
14816       mips_opts.ase_mips3d = 1;
14817       break;
14818 
14819     case OPTION_NO_MIPS3D:
14820       mips_opts.ase_mips3d = 0;
14821       break;
14822 
14823     case OPTION_SMARTMIPS:
14824       mips_opts.ase_smartmips = 1;
14825       break;
14826 
14827     case OPTION_NO_SMARTMIPS:
14828       mips_opts.ase_smartmips = 0;
14829       break;
14830 
14831     case OPTION_FIX_24K:
14832       mips_fix_24k = 1;
14833       break;
14834 
14835     case OPTION_NO_FIX_24K:
14836       mips_fix_24k = 0;
14837       break;
14838 
14839     case OPTION_FIX_LOONGSON2F_JUMP:
14840       mips_fix_loongson2f_jump = TRUE;
14841       break;
14842 
14843     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14844       mips_fix_loongson2f_jump = FALSE;
14845       break;
14846 
14847     case OPTION_FIX_LOONGSON2F_NOP:
14848       mips_fix_loongson2f_nop = TRUE;
14849       break;
14850 
14851     case OPTION_NO_FIX_LOONGSON2F_NOP:
14852       mips_fix_loongson2f_nop = FALSE;
14853       break;
14854 
14855     case OPTION_FIX_VR4120:
14856       mips_fix_vr4120 = 1;
14857       break;
14858 
14859     case OPTION_NO_FIX_VR4120:
14860       mips_fix_vr4120 = 0;
14861       break;
14862 
14863     case OPTION_FIX_VR4130:
14864       mips_fix_vr4130 = 1;
14865       break;
14866 
14867     case OPTION_NO_FIX_VR4130:
14868       mips_fix_vr4130 = 0;
14869       break;
14870 
14871     case OPTION_FIX_LOONGSON2F_BTB:
14872       mips_fix_loongson2f_btb = 1;
14873       break;
14874 
14875     case OPTION_NO_FIX_LOONGSON2F_BTB:
14876       mips_fix_loongson2f_btb = 0;
14877       break;
14878 
14879     case OPTION_FIX_CN63XXP1:
14880       mips_fix_cn63xxp1 = TRUE;
14881       break;
14882 
14883     case OPTION_NO_FIX_CN63XXP1:
14884       mips_fix_cn63xxp1 = FALSE;
14885       break;
14886 
14887     case OPTION_RELAX_BRANCH:
14888       mips_relax_branch = 1;
14889       break;
14890 
14891     case OPTION_NO_RELAX_BRANCH:
14892       mips_relax_branch = 0;
14893       break;
14894 
14895     case OPTION_MSHARED:
14896       mips_in_shared = TRUE;
14897       break;
14898 
14899     case OPTION_MNO_SHARED:
14900       mips_in_shared = FALSE;
14901       break;
14902 
14903     case OPTION_MSYM32:
14904       mips_opts.sym32 = TRUE;
14905       break;
14906 
14907     case OPTION_MNO_SYM32:
14908       mips_opts.sym32 = FALSE;
14909       break;
14910 
14911     case OPTION_TRAP_ZERO_JUMP:
14912       mips_trap_zero_jump = TRUE;
14913       break;
14914 
14915     case OPTION_NO_TRAP_ZERO_JUMP:
14916       mips_trap_zero_jump = FALSE;
14917       break;
14918 
14919 #ifdef OBJ_ELF
14920       /* When generating ELF code, we permit -KPIC and -call_shared to
14921 	 select SVR4_PIC, and -non_shared to select no PIC.  This is
14922 	 intended to be compatible with Irix 5.  */
14923     case OPTION_CALL_SHARED:
14924       if (!IS_ELF)
14925 	{
14926 	  as_bad (_("-call_shared is supported only for ELF format"));
14927 	  return 0;
14928 	}
14929       mips_pic = SVR4_PIC;
14930       mips_abicalls = TRUE;
14931       break;
14932 
14933     case OPTION_CALL_NONPIC:
14934       if (!IS_ELF)
14935 	{
14936 	  as_bad (_("-call_nonpic is supported only for ELF format"));
14937 	  return 0;
14938 	}
14939       mips_pic = NO_PIC;
14940       mips_abicalls = TRUE;
14941       break;
14942 
14943     case OPTION_NON_SHARED:
14944       if (!IS_ELF)
14945 	{
14946 	  as_bad (_("-non_shared is supported only for ELF format"));
14947 	  return 0;
14948 	}
14949       mips_pic = NO_PIC;
14950       mips_abicalls = FALSE;
14951       break;
14952 
14953       /* The -xgot option tells the assembler to use 32 bit offsets
14954          when accessing the got in SVR4_PIC mode.  It is for Irix
14955          compatibility.  */
14956     case OPTION_XGOT:
14957       mips_big_got = 1;
14958       break;
14959 #endif /* OBJ_ELF */
14960 
14961     case 'G':
14962       g_switch_value = atoi (arg);
14963       g_switch_seen = 1;
14964       break;
14965 
14966       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14967 	 and -mabi=64.  */
14968     case OPTION_32:
14969       if (IS_ELF)
14970 	mips_abi = O32_ABI;
14971       /* We silently ignore -32 for non-ELF targets.  This greatly
14972 	 simplifies the construction of the MIPS GAS test cases.  */
14973       break;
14974 
14975 #ifdef OBJ_ELF
14976     case OPTION_N32:
14977       if (!IS_ELF)
14978 	{
14979 	  as_bad (_("-n32 is supported for ELF format only"));
14980 	  return 0;
14981 	}
14982       mips_abi = N32_ABI;
14983       break;
14984 
14985     case OPTION_64:
14986       if (!IS_ELF)
14987 	{
14988 	  as_bad (_("-64 is supported for ELF format only"));
14989 	  return 0;
14990 	}
14991       mips_abi = N64_ABI;
14992       if (!support_64bit_objects())
14993 	as_fatal (_("No compiled in support for 64 bit object file format"));
14994       break;
14995 #endif /* OBJ_ELF */
14996 
14997     case OPTION_GP32:
14998       file_mips_gp32 = 1;
14999       break;
15000 
15001     case OPTION_GP64:
15002       file_mips_gp32 = 0;
15003       break;
15004 
15005     case OPTION_FP32:
15006       file_mips_fp32 = 1;
15007       break;
15008 
15009     case OPTION_FP64:
15010       file_mips_fp32 = 0;
15011       break;
15012 
15013     case OPTION_SINGLE_FLOAT:
15014       file_mips_single_float = 1;
15015       break;
15016 
15017     case OPTION_DOUBLE_FLOAT:
15018       file_mips_single_float = 0;
15019       break;
15020 
15021     case OPTION_SOFT_FLOAT:
15022       file_mips_soft_float = 1;
15023       break;
15024 
15025     case OPTION_HARD_FLOAT:
15026       file_mips_soft_float = 0;
15027       break;
15028 
15029 #ifdef OBJ_ELF
15030     case OPTION_MABI:
15031       if (!IS_ELF)
15032 	{
15033 	  as_bad (_("-mabi is supported for ELF format only"));
15034 	  return 0;
15035 	}
15036       if (strcmp (arg, "32") == 0)
15037 	mips_abi = O32_ABI;
15038       else if (strcmp (arg, "o64") == 0)
15039 	mips_abi = O64_ABI;
15040       else if (strcmp (arg, "n32") == 0)
15041 	mips_abi = N32_ABI;
15042       else if (strcmp (arg, "64") == 0)
15043 	{
15044 	  mips_abi = N64_ABI;
15045 	  if (! support_64bit_objects())
15046 	    as_fatal (_("No compiled in support for 64 bit object file "
15047 			"format"));
15048 	}
15049       else if (strcmp (arg, "eabi") == 0)
15050 	mips_abi = EABI_ABI;
15051       else
15052 	{
15053 	  as_fatal (_("invalid abi -mabi=%s"), arg);
15054 	  return 0;
15055 	}
15056       break;
15057 #endif /* OBJ_ELF */
15058 
15059     case OPTION_M7000_HILO_FIX:
15060       mips_7000_hilo_fix = TRUE;
15061       break;
15062 
15063     case OPTION_MNO_7000_HILO_FIX:
15064       mips_7000_hilo_fix = FALSE;
15065       break;
15066 
15067 #ifdef OBJ_ELF
15068     case OPTION_MDEBUG:
15069       mips_flag_mdebug = TRUE;
15070       break;
15071 
15072     case OPTION_NO_MDEBUG:
15073       mips_flag_mdebug = FALSE;
15074       break;
15075 
15076     case OPTION_PDR:
15077       mips_flag_pdr = TRUE;
15078       break;
15079 
15080     case OPTION_NO_PDR:
15081       mips_flag_pdr = FALSE;
15082       break;
15083 
15084     case OPTION_MVXWORKS_PIC:
15085       mips_pic = VXWORKS_PIC;
15086       break;
15087 #endif /* OBJ_ELF */
15088 
15089     default:
15090       return 0;
15091     }
15092 
15093     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15094 
15095   return 1;
15096 }
15097 
15098 /* Set up globals to generate code for the ISA or processor
15099    described by INFO.  */
15100 
15101 static void
15102 mips_set_architecture (const struct mips_cpu_info *info)
15103 {
15104   if (info != 0)
15105     {
15106       file_mips_arch = info->cpu;
15107       mips_opts.arch = info->cpu;
15108       mips_opts.isa = info->isa;
15109     }
15110 }
15111 
15112 
15113 /* Likewise for tuning.  */
15114 
15115 static void
15116 mips_set_tune (const struct mips_cpu_info *info)
15117 {
15118   if (info != 0)
15119     mips_tune = info->cpu;
15120 }
15121 
15122 
15123 void
15124 mips_after_parse_args (void)
15125 {
15126   const struct mips_cpu_info *arch_info = 0;
15127   const struct mips_cpu_info *tune_info = 0;
15128 
15129   /* GP relative stuff not working for PE */
15130   if (strncmp (TARGET_OS, "pe", 2) == 0)
15131     {
15132       if (g_switch_seen && g_switch_value != 0)
15133 	as_bad (_("-G not supported in this configuration."));
15134       g_switch_value = 0;
15135     }
15136 
15137   if (mips_abi == NO_ABI)
15138     mips_abi = MIPS_DEFAULT_ABI;
15139 
15140   /* The following code determines the architecture and register size.
15141      Similar code was added to GCC 3.3 (see override_options() in
15142      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15143      as much as possible.  */
15144 
15145   if (mips_arch_string != 0)
15146     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15147 
15148   if (file_mips_isa != ISA_UNKNOWN)
15149     {
15150       /* Handle -mipsN.  At this point, file_mips_isa contains the
15151 	 ISA level specified by -mipsN, while arch_info->isa contains
15152 	 the -march selection (if any).  */
15153       if (arch_info != 0)
15154 	{
15155 	  /* -march takes precedence over -mipsN, since it is more descriptive.
15156 	     There's no harm in specifying both as long as the ISA levels
15157 	     are the same.  */
15158 	  if (file_mips_isa != arch_info->isa)
15159 	    as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15160 		    mips_cpu_info_from_isa (file_mips_isa)->name,
15161 		    mips_cpu_info_from_isa (arch_info->isa)->name);
15162 	}
15163       else
15164 	arch_info = mips_cpu_info_from_isa (file_mips_isa);
15165     }
15166 
15167   if (arch_info == 0)
15168     {
15169       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15170       gas_assert (arch_info);
15171     }
15172 
15173   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15174     as_bad (_("-march=%s is not compatible with the selected ABI"),
15175 	    arch_info->name);
15176 
15177   mips_set_architecture (arch_info);
15178 
15179   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
15180   if (mips_tune_string != 0)
15181     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15182 
15183   if (tune_info == 0)
15184     mips_set_tune (arch_info);
15185   else
15186     mips_set_tune (tune_info);
15187 
15188   if (file_mips_gp32 >= 0)
15189     {
15190       /* The user specified the size of the integer registers.  Make sure
15191 	 it agrees with the ABI and ISA.  */
15192       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15193 	as_bad (_("-mgp64 used with a 32-bit processor"));
15194       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15195 	as_bad (_("-mgp32 used with a 64-bit ABI"));
15196       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15197 	as_bad (_("-mgp64 used with a 32-bit ABI"));
15198     }
15199   else
15200     {
15201       /* Infer the integer register size from the ABI and processor.
15202 	 Restrict ourselves to 32-bit registers if that's all the
15203 	 processor has, or if the ABI cannot handle 64-bit registers.  */
15204       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15205 			|| !ISA_HAS_64BIT_REGS (mips_opts.isa));
15206     }
15207 
15208   switch (file_mips_fp32)
15209     {
15210     default:
15211     case -1:
15212       /* No user specified float register size.
15213 	 ??? GAS treats single-float processors as though they had 64-bit
15214 	 float registers (although it complains when double-precision
15215 	 instructions are used).  As things stand, saying they have 32-bit
15216 	 registers would lead to spurious "register must be even" messages.
15217 	 So here we assume float registers are never smaller than the
15218 	 integer ones.  */
15219       if (file_mips_gp32 == 0)
15220 	/* 64-bit integer registers implies 64-bit float registers.  */
15221 	file_mips_fp32 = 0;
15222       else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15223 	       && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15224 	/* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
15225 	file_mips_fp32 = 0;
15226       else
15227 	/* 32-bit float registers.  */
15228 	file_mips_fp32 = 1;
15229       break;
15230 
15231     /* The user specified the size of the float registers.  Check if it
15232        agrees with the ABI and ISA.  */
15233     case 0:
15234       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15235 	as_bad (_("-mfp64 used with a 32-bit fpu"));
15236       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15237 	       && !ISA_HAS_MXHC1 (mips_opts.isa))
15238 	as_warn (_("-mfp64 used with a 32-bit ABI"));
15239       break;
15240     case 1:
15241       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15242 	as_warn (_("-mfp32 used with a 64-bit ABI"));
15243       break;
15244     }
15245 
15246   /* End of GCC-shared inference code.  */
15247 
15248   /* This flag is set when we have a 64-bit capable CPU but use only
15249      32-bit wide registers.  Note that EABI does not use it.  */
15250   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15251       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15252 	  || mips_abi == O32_ABI))
15253     mips_32bitmode = 1;
15254 
15255   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15256     as_bad (_("trap exception not supported at ISA 1"));
15257 
15258   /* If the selected architecture includes support for ASEs, enable
15259      generation of code for them.  */
15260   if (mips_opts.mips16 == -1)
15261     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15262   if (mips_opts.micromips == -1)
15263     mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15264   if (mips_opts.ase_mips3d == -1)
15265     mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
15266 			    && file_mips_fp32 == 0) ? 1 : 0;
15267   if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15268     as_bad (_("-mfp32 used with -mips3d"));
15269 
15270   if (mips_opts.ase_mdmx == -1)
15271     mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
15272 			  && file_mips_fp32 == 0) ? 1 : 0;
15273   if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15274     as_bad (_("-mfp32 used with -mdmx"));
15275 
15276   if (mips_opts.ase_smartmips == -1)
15277     mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15278   if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
15279     as_warn (_("%s ISA does not support SmartMIPS"),
15280 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15281 
15282   if (mips_opts.ase_dsp == -1)
15283     mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15284   if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
15285     as_warn (_("%s ISA does not support DSP ASE"),
15286 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15287 
15288   if (mips_opts.ase_dspr2 == -1)
15289     {
15290       mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15291       mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15292     }
15293   if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
15294     as_warn (_("%s ISA does not support DSP R2 ASE"),
15295 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15296 
15297   if (mips_opts.ase_mt == -1)
15298     mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15299   if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
15300     as_warn (_("%s ISA does not support MT ASE"),
15301 	     mips_cpu_info_from_isa (mips_opts.isa)->name);
15302 
15303   if (mips_opts.ase_mcu == -1)
15304     mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15305   if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15306       as_warn (_("%s ISA does not support MCU ASE"),
15307 	       mips_cpu_info_from_isa (mips_opts.isa)->name);
15308 
15309   file_mips_isa = mips_opts.isa;
15310   file_ase_mips3d = mips_opts.ase_mips3d;
15311   file_ase_mdmx = mips_opts.ase_mdmx;
15312   file_ase_smartmips = mips_opts.ase_smartmips;
15313   file_ase_dsp = mips_opts.ase_dsp;
15314   file_ase_dspr2 = mips_opts.ase_dspr2;
15315   file_ase_mt = mips_opts.ase_mt;
15316   mips_opts.gp32 = file_mips_gp32;
15317   mips_opts.fp32 = file_mips_fp32;
15318   mips_opts.soft_float = file_mips_soft_float;
15319   mips_opts.single_float = file_mips_single_float;
15320 
15321   if (mips_flag_mdebug < 0)
15322     {
15323 #ifdef OBJ_MAYBE_ECOFF
15324       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15325 	mips_flag_mdebug = 1;
15326       else
15327 #endif /* OBJ_MAYBE_ECOFF */
15328 	mips_flag_mdebug = 0;
15329     }
15330 }
15331 
15332 void
15333 mips_init_after_args (void)
15334 {
15335   /* initialize opcodes */
15336   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15337   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15338 }
15339 
15340 long
15341 md_pcrel_from (fixS *fixP)
15342 {
15343   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15344   switch (fixP->fx_r_type)
15345     {
15346     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15347     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15348       /* Return the address of the delay slot.  */
15349       return addr + 2;
15350 
15351     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15352     case BFD_RELOC_MICROMIPS_JMP:
15353     case BFD_RELOC_16_PCREL_S2:
15354     case BFD_RELOC_MIPS_JMP:
15355       /* Return the address of the delay slot.  */
15356       return addr + 4;
15357 
15358     default:
15359       /* We have no relocation type for PC relative MIPS16 instructions.  */
15360       if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15361 	as_bad_where (fixP->fx_file, fixP->fx_line,
15362 		      _("PC relative MIPS16 instruction references a different section"));
15363       return addr;
15364     }
15365 }
15366 
15367 /* This is called before the symbol table is processed.  In order to
15368    work with gcc when using mips-tfile, we must keep all local labels.
15369    However, in other cases, we want to discard them.  If we were
15370    called with -g, but we didn't see any debugging information, it may
15371    mean that gcc is smuggling debugging information through to
15372    mips-tfile, in which case we must generate all local labels.  */
15373 
15374 void
15375 mips_frob_file_before_adjust (void)
15376 {
15377 #ifndef NO_ECOFF_DEBUGGING
15378   if (ECOFF_DEBUGGING
15379       && mips_debug != 0
15380       && ! ecoff_debugging_seen)
15381     flag_keep_locals = 1;
15382 #endif
15383 }
15384 
15385 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15386    the corresponding LO16 reloc.  This is called before md_apply_fix and
15387    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15388    relocation operators.
15389 
15390    For our purposes, a %lo() expression matches a %got() or %hi()
15391    expression if:
15392 
15393       (a) it refers to the same symbol; and
15394       (b) the offset applied in the %lo() expression is no lower than
15395 	  the offset applied in the %got() or %hi().
15396 
15397    (b) allows us to cope with code like:
15398 
15399 	lui	$4,%hi(foo)
15400 	lh	$4,%lo(foo+2)($4)
15401 
15402    ...which is legal on RELA targets, and has a well-defined behaviour
15403    if the user knows that adding 2 to "foo" will not induce a carry to
15404    the high 16 bits.
15405 
15406    When several %lo()s match a particular %got() or %hi(), we use the
15407    following rules to distinguish them:
15408 
15409      (1) %lo()s with smaller offsets are a better match than %lo()s with
15410          higher offsets.
15411 
15412      (2) %lo()s with no matching %got() or %hi() are better than those
15413          that already have a matching %got() or %hi().
15414 
15415      (3) later %lo()s are better than earlier %lo()s.
15416 
15417    These rules are applied in order.
15418 
15419    (1) means, among other things, that %lo()s with identical offsets are
15420    chosen if they exist.
15421 
15422    (2) means that we won't associate several high-part relocations with
15423    the same low-part relocation unless there's no alternative.  Having
15424    several high parts for the same low part is a GNU extension; this rule
15425    allows careful users to avoid it.
15426 
15427    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15428    with the last high-part relocation being at the front of the list.
15429    It therefore makes sense to choose the last matching low-part
15430    relocation, all other things being equal.  It's also easier
15431    to code that way.  */
15432 
15433 void
15434 mips_frob_file (void)
15435 {
15436   struct mips_hi_fixup *l;
15437   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15438 
15439   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15440     {
15441       segment_info_type *seginfo;
15442       bfd_boolean matched_lo_p;
15443       fixS **hi_pos, **lo_pos, **pos;
15444 
15445       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15446 
15447       /* If a GOT16 relocation turns out to be against a global symbol,
15448 	 there isn't supposed to be a matching LO.  */
15449       if (got16_reloc_p (l->fixp->fx_r_type)
15450 	  && !pic_need_relax (l->fixp->fx_addsy, l->seg))
15451 	continue;
15452 
15453       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15454       if (fixup_has_matching_lo_p (l->fixp))
15455 	continue;
15456 
15457       seginfo = seg_info (l->seg);
15458 
15459       /* Set HI_POS to the position of this relocation in the chain.
15460 	 Set LO_POS to the position of the chosen low-part relocation.
15461 	 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15462 	 relocation that matches an immediately-preceding high-part
15463 	 relocation.  */
15464       hi_pos = NULL;
15465       lo_pos = NULL;
15466       matched_lo_p = FALSE;
15467       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15468 
15469       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15470 	{
15471 	  if (*pos == l->fixp)
15472 	    hi_pos = pos;
15473 
15474 	  if ((*pos)->fx_r_type == looking_for_rtype
15475 	      && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15476 	      && (*pos)->fx_offset >= l->fixp->fx_offset
15477 	      && (lo_pos == NULL
15478 		  || (*pos)->fx_offset < (*lo_pos)->fx_offset
15479 		  || (!matched_lo_p
15480 		      && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15481 	    lo_pos = pos;
15482 
15483 	  matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15484 			  && fixup_has_matching_lo_p (*pos));
15485 	}
15486 
15487       /* If we found a match, remove the high-part relocation from its
15488 	 current position and insert it before the low-part relocation.
15489 	 Make the offsets match so that fixup_has_matching_lo_p()
15490 	 will return true.
15491 
15492 	 We don't warn about unmatched high-part relocations since some
15493 	 versions of gcc have been known to emit dead "lui ...%hi(...)"
15494 	 instructions.  */
15495       if (lo_pos != NULL)
15496 	{
15497 	  l->fixp->fx_offset = (*lo_pos)->fx_offset;
15498 	  if (l->fixp->fx_next != *lo_pos)
15499 	    {
15500 	      *hi_pos = l->fixp->fx_next;
15501 	      l->fixp->fx_next = *lo_pos;
15502 	      *lo_pos = l->fixp;
15503 	    }
15504 	}
15505     }
15506 }
15507 
15508 /* We may have combined relocations without symbols in the N32/N64 ABI.
15509    We have to prevent gas from dropping them.  */
15510 
15511 int
15512 mips_force_relocation (fixS *fixp)
15513 {
15514   if (generic_force_reloc (fixp))
15515     return 1;
15516 
15517   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15518      so that the linker relaxation can update targets.  */
15519   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15520       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15521       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15522     return 1;
15523 
15524   if (HAVE_NEWABI
15525       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
15526       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
15527 	  || hi16_reloc_p (fixp->fx_r_type)
15528 	  || lo16_reloc_p (fixp->fx_r_type)))
15529     return 1;
15530 
15531   return 0;
15532 }
15533 
15534 /* Apply a fixup to the object file.  */
15535 
15536 void
15537 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15538 {
15539   bfd_byte *buf;
15540   long insn;
15541   reloc_howto_type *howto;
15542 
15543   /* We ignore generic BFD relocations we don't know about.  */
15544   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15545   if (! howto)
15546     return;
15547 
15548   gas_assert (fixP->fx_size == 2
15549 	      || fixP->fx_size == 4
15550 	      || fixP->fx_r_type == BFD_RELOC_16
15551 	      || fixP->fx_r_type == BFD_RELOC_64
15552 	      || fixP->fx_r_type == BFD_RELOC_CTOR
15553 	      || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15554 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15555 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15556 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15557 	      || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15558 
15559   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
15560 
15561   gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15562 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15563 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15564 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
15565 
15566   /* Don't treat parts of a composite relocation as done.  There are two
15567      reasons for this:
15568 
15569      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15570 	 should nevertheless be emitted if the first part is.
15571 
15572      (2) In normal usage, composite relocations are never assembly-time
15573 	 constants.  The easiest way of dealing with the pathological
15574 	 exceptions is to generate a relocation against STN_UNDEF and
15575 	 leave everything up to the linker.  */
15576   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15577     fixP->fx_done = 1;
15578 
15579   switch (fixP->fx_r_type)
15580     {
15581     case BFD_RELOC_MIPS_TLS_GD:
15582     case BFD_RELOC_MIPS_TLS_LDM:
15583     case BFD_RELOC_MIPS_TLS_DTPREL32:
15584     case BFD_RELOC_MIPS_TLS_DTPREL64:
15585     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15586     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15587     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15588     case BFD_RELOC_MIPS_TLS_TPREL32:
15589     case BFD_RELOC_MIPS_TLS_TPREL64:
15590     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15591     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15592     case BFD_RELOC_MICROMIPS_TLS_GD:
15593     case BFD_RELOC_MICROMIPS_TLS_LDM:
15594     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15595     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15596     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15597     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15598     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15599     case BFD_RELOC_MIPS16_TLS_GD:
15600     case BFD_RELOC_MIPS16_TLS_LDM:
15601     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15602     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15603     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15604     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15605     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15606       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15607       /* fall through */
15608 
15609     case BFD_RELOC_MIPS_JMP:
15610     case BFD_RELOC_MIPS_SHIFT5:
15611     case BFD_RELOC_MIPS_SHIFT6:
15612     case BFD_RELOC_MIPS_GOT_DISP:
15613     case BFD_RELOC_MIPS_GOT_PAGE:
15614     case BFD_RELOC_MIPS_GOT_OFST:
15615     case BFD_RELOC_MIPS_SUB:
15616     case BFD_RELOC_MIPS_INSERT_A:
15617     case BFD_RELOC_MIPS_INSERT_B:
15618     case BFD_RELOC_MIPS_DELETE:
15619     case BFD_RELOC_MIPS_HIGHEST:
15620     case BFD_RELOC_MIPS_HIGHER:
15621     case BFD_RELOC_MIPS_SCN_DISP:
15622     case BFD_RELOC_MIPS_REL16:
15623     case BFD_RELOC_MIPS_RELGOT:
15624     case BFD_RELOC_MIPS_JALR:
15625     case BFD_RELOC_HI16:
15626     case BFD_RELOC_HI16_S:
15627     case BFD_RELOC_GPREL16:
15628     case BFD_RELOC_MIPS_LITERAL:
15629     case BFD_RELOC_MIPS_CALL16:
15630     case BFD_RELOC_MIPS_GOT16:
15631     case BFD_RELOC_GPREL32:
15632     case BFD_RELOC_MIPS_GOT_HI16:
15633     case BFD_RELOC_MIPS_GOT_LO16:
15634     case BFD_RELOC_MIPS_CALL_HI16:
15635     case BFD_RELOC_MIPS_CALL_LO16:
15636     case BFD_RELOC_MIPS16_GPREL:
15637     case BFD_RELOC_MIPS16_GOT16:
15638     case BFD_RELOC_MIPS16_CALL16:
15639     case BFD_RELOC_MIPS16_HI16:
15640     case BFD_RELOC_MIPS16_HI16_S:
15641     case BFD_RELOC_MIPS16_JMP:
15642     case BFD_RELOC_MICROMIPS_JMP:
15643     case BFD_RELOC_MICROMIPS_GOT_DISP:
15644     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15645     case BFD_RELOC_MICROMIPS_GOT_OFST:
15646     case BFD_RELOC_MICROMIPS_SUB:
15647     case BFD_RELOC_MICROMIPS_HIGHEST:
15648     case BFD_RELOC_MICROMIPS_HIGHER:
15649     case BFD_RELOC_MICROMIPS_SCN_DISP:
15650     case BFD_RELOC_MICROMIPS_JALR:
15651     case BFD_RELOC_MICROMIPS_HI16:
15652     case BFD_RELOC_MICROMIPS_HI16_S:
15653     case BFD_RELOC_MICROMIPS_GPREL16:
15654     case BFD_RELOC_MICROMIPS_LITERAL:
15655     case BFD_RELOC_MICROMIPS_CALL16:
15656     case BFD_RELOC_MICROMIPS_GOT16:
15657     case BFD_RELOC_MICROMIPS_GOT_HI16:
15658     case BFD_RELOC_MICROMIPS_GOT_LO16:
15659     case BFD_RELOC_MICROMIPS_CALL_HI16:
15660     case BFD_RELOC_MICROMIPS_CALL_LO16:
15661       /* Nothing needed to do.  The value comes from the reloc entry.  */
15662       break;
15663 
15664     case BFD_RELOC_64:
15665       /* This is handled like BFD_RELOC_32, but we output a sign
15666          extended value if we are only 32 bits.  */
15667       if (fixP->fx_done)
15668 	{
15669 	  if (8 <= sizeof (valueT))
15670 	    md_number_to_chars ((char *) buf, *valP, 8);
15671 	  else
15672 	    {
15673 	      valueT hiv;
15674 
15675 	      if ((*valP & 0x80000000) != 0)
15676 		hiv = 0xffffffff;
15677 	      else
15678 		hiv = 0;
15679 	      md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
15680 				  *valP, 4);
15681 	      md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
15682 				  hiv, 4);
15683 	    }
15684 	}
15685       break;
15686 
15687     case BFD_RELOC_RVA:
15688     case BFD_RELOC_32:
15689     case BFD_RELOC_16:
15690       /* If we are deleting this reloc entry, we must fill in the
15691 	 value now.  This can happen if we have a .word which is not
15692 	 resolved when it appears but is later defined.  */
15693       if (fixP->fx_done)
15694 	md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
15695       break;
15696 
15697     case BFD_RELOC_LO16:
15698     case BFD_RELOC_MIPS16_LO16:
15699     case BFD_RELOC_MICROMIPS_LO16:
15700       /* FIXME: Now that embedded-PIC is gone, some of this code/comment
15701 	 may be safe to remove, but if so it's not obvious.  */
15702       /* When handling an embedded PIC switch statement, we can wind
15703 	 up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
15704       if (fixP->fx_done)
15705 	{
15706 	  if (*valP + 0x8000 > 0xffff)
15707 	    as_bad_where (fixP->fx_file, fixP->fx_line,
15708 			  _("relocation overflow"));
15709 	  /* 32-bit microMIPS instructions are divided into two halfwords.
15710 	     Relocations always refer to the second halfword, regardless
15711 	     of endianness.  */
15712 	  if (target_big_endian || fixP->fx_r_type == BFD_RELOC_MICROMIPS_LO16)
15713 	    buf += 2;
15714 	  md_number_to_chars ((char *) buf, *valP, 2);
15715 	}
15716       break;
15717 
15718     case BFD_RELOC_16_PCREL_S2:
15719       if ((*valP & 0x3) != 0)
15720 	as_bad_where (fixP->fx_file, fixP->fx_line,
15721 		      _("Branch to misaligned address (%lx)"), (long) *valP);
15722 
15723       /* We need to save the bits in the instruction since fixup_segment()
15724 	 might be deleting the relocation entry (i.e., a branch within
15725 	 the current segment).  */
15726       if (! fixP->fx_done)
15727 	break;
15728 
15729       /* Update old instruction data.  */
15730       if (target_big_endian)
15731 	insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
15732       else
15733 	insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
15734 
15735       if (*valP + 0x20000 <= 0x3ffff)
15736 	{
15737 	  insn |= (*valP >> 2) & 0xffff;
15738 	  md_number_to_chars ((char *) buf, insn, 4);
15739 	}
15740       else if (mips_pic == NO_PIC
15741 	       && fixP->fx_done
15742 	       && fixP->fx_frag->fr_address >= text_section->vma
15743 	       && (fixP->fx_frag->fr_address
15744 		   < text_section->vma + bfd_get_section_size (text_section))
15745 	       && ((insn & 0xffff0000) == 0x10000000	 /* beq $0,$0 */
15746 		   || (insn & 0xffff0000) == 0x04010000	 /* bgez $0 */
15747 		   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15748 	{
15749 	  /* The branch offset is too large.  If this is an
15750              unconditional branch, and we are not generating PIC code,
15751              we can convert it to an absolute jump instruction.  */
15752 	  if ((insn & 0xffff0000) == 0x04110000)	 /* bgezal $0 */
15753 	    insn = 0x0c000000;	/* jal */
15754 	  else
15755 	    insn = 0x08000000;	/* j */
15756 	  fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15757 	  fixP->fx_done = 0;
15758 	  fixP->fx_addsy = section_symbol (text_section);
15759 	  *valP += md_pcrel_from (fixP);
15760 	  md_number_to_chars ((char *) buf, insn, 4);
15761 	}
15762       else
15763 	{
15764 	  /* If we got here, we have branch-relaxation disabled,
15765 	     and there's nothing we can do to fix this instruction
15766 	     without turning it into a longer sequence.  */
15767 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15768 			_("Branch out of range"));
15769 	}
15770       break;
15771 
15772     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15773     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15774     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15775       /* We adjust the offset back to even.  */
15776       if ((*valP & 0x1) != 0)
15777 	--(*valP);
15778 
15779       if (! fixP->fx_done)
15780 	break;
15781 
15782       /* Should never visit here, because we keep the relocation.  */
15783       abort ();
15784       break;
15785 
15786     case BFD_RELOC_VTABLE_INHERIT:
15787       fixP->fx_done = 0;
15788       if (fixP->fx_addsy
15789           && !S_IS_DEFINED (fixP->fx_addsy)
15790           && !S_IS_WEAK (fixP->fx_addsy))
15791         S_SET_WEAK (fixP->fx_addsy);
15792       break;
15793 
15794     case BFD_RELOC_VTABLE_ENTRY:
15795       fixP->fx_done = 0;
15796       break;
15797 
15798     default:
15799       internalError ();
15800     }
15801 
15802   /* Remember value for tc_gen_reloc.  */
15803   fixP->fx_addnumber = *valP;
15804 }
15805 
15806 static symbolS *
15807 get_symbol (void)
15808 {
15809   int c;
15810   char *name;
15811   symbolS *p;
15812 
15813   name = input_line_pointer;
15814   c = get_symbol_end ();
15815   p = (symbolS *) symbol_find_or_make (name);
15816   *input_line_pointer = c;
15817   return p;
15818 }
15819 
15820 /* Align the current frag to a given power of two.  If a particular
15821    fill byte should be used, FILL points to an integer that contains
15822    that byte, otherwise FILL is null.
15823 
15824    This function used to have the comment:
15825 
15826       The MIPS assembler also automatically adjusts any preceding label.
15827 
15828    The implementation therefore applied the adjustment to a maximum of
15829    one label.  However, other label adjustments are applied to batches
15830    of labels, and adjusting just one caused problems when new labels
15831    were added for the sake of debugging or unwind information.
15832    We therefore adjust all preceding labels (given as LABELS) instead.  */
15833 
15834 static void
15835 mips_align (int to, int *fill, struct insn_label_list *labels)
15836 {
15837   mips_emit_delays ();
15838   mips_record_compressed_mode ();
15839   if (fill == NULL && subseg_text_p (now_seg))
15840     frag_align_code (to, 0);
15841   else
15842     frag_align (to, fill ? *fill : 0, 0);
15843   record_alignment (now_seg, to);
15844   mips_move_labels (labels, FALSE);
15845 }
15846 
15847 /* Align to a given power of two.  .align 0 turns off the automatic
15848    alignment used by the data creating pseudo-ops.  */
15849 
15850 static void
15851 s_align (int x ATTRIBUTE_UNUSED)
15852 {
15853   int temp, fill_value, *fill_ptr;
15854   long max_alignment = 28;
15855 
15856   /* o Note that the assembler pulls down any immediately preceding label
15857        to the aligned address.
15858      o It's not documented but auto alignment is reinstated by
15859        a .align pseudo instruction.
15860      o Note also that after auto alignment is turned off the mips assembler
15861        issues an error on attempt to assemble an improperly aligned data item.
15862        We don't.  */
15863 
15864   temp = get_absolute_expression ();
15865   if (temp > max_alignment)
15866     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15867   else if (temp < 0)
15868     {
15869       as_warn (_("Alignment negative: 0 assumed."));
15870       temp = 0;
15871     }
15872   if (*input_line_pointer == ',')
15873     {
15874       ++input_line_pointer;
15875       fill_value = get_absolute_expression ();
15876       fill_ptr = &fill_value;
15877     }
15878   else
15879     fill_ptr = 0;
15880   if (temp)
15881     {
15882       segment_info_type *si = seg_info (now_seg);
15883       struct insn_label_list *l = si->label_list;
15884       /* Auto alignment should be switched on by next section change.  */
15885       auto_align = 1;
15886       mips_align (temp, fill_ptr, l);
15887     }
15888   else
15889     {
15890       auto_align = 0;
15891     }
15892 
15893   demand_empty_rest_of_line ();
15894 }
15895 
15896 static void
15897 s_change_sec (int sec)
15898 {
15899   segT seg;
15900 
15901 #ifdef OBJ_ELF
15902   /* The ELF backend needs to know that we are changing sections, so
15903      that .previous works correctly.  We could do something like check
15904      for an obj_section_change_hook macro, but that might be confusing
15905      as it would not be appropriate to use it in the section changing
15906      functions in read.c, since obj-elf.c intercepts those.  FIXME:
15907      This should be cleaner, somehow.  */
15908   if (IS_ELF)
15909     obj_elf_section_change_hook ();
15910 #endif
15911 
15912   mips_emit_delays ();
15913 
15914   switch (sec)
15915     {
15916     case 't':
15917       s_text (0);
15918       break;
15919     case 'd':
15920       s_data (0);
15921       break;
15922     case 'b':
15923       subseg_set (bss_section, (subsegT) get_absolute_expression ());
15924       demand_empty_rest_of_line ();
15925       break;
15926 
15927     case 'r':
15928       seg = subseg_new (RDATA_SECTION_NAME,
15929 			(subsegT) get_absolute_expression ());
15930       if (IS_ELF)
15931 	{
15932 	  bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15933 						  | SEC_READONLY | SEC_RELOC
15934 						  | SEC_DATA));
15935 	  if (strncmp (TARGET_OS, "elf", 3) != 0)
15936 	    record_alignment (seg, 4);
15937 	}
15938       demand_empty_rest_of_line ();
15939       break;
15940 
15941     case 's':
15942       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15943       if (IS_ELF)
15944 	{
15945 	  bfd_set_section_flags (stdoutput, seg,
15946 				 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15947 	  if (strncmp (TARGET_OS, "elf", 3) != 0)
15948 	    record_alignment (seg, 4);
15949 	}
15950       demand_empty_rest_of_line ();
15951       break;
15952 
15953     case 'B':
15954       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15955       if (IS_ELF)
15956 	{
15957 	  bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15958 	  if (strncmp (TARGET_OS, "elf", 3) != 0)
15959 	    record_alignment (seg, 4);
15960 	}
15961       demand_empty_rest_of_line ();
15962       break;
15963     }
15964 
15965   auto_align = 1;
15966 }
15967 
15968 void
15969 s_change_section (int ignore ATTRIBUTE_UNUSED)
15970 {
15971 #ifdef OBJ_ELF
15972   char *section_name;
15973   char c;
15974   char next_c = 0;
15975   int section_type;
15976   int section_flag;
15977   int section_entry_size;
15978   int section_alignment;
15979 
15980   if (!IS_ELF)
15981     return;
15982 
15983   section_name = input_line_pointer;
15984   c = get_symbol_end ();
15985   if (c)
15986     next_c = *(input_line_pointer + 1);
15987 
15988   /* Do we have .section Name<,"flags">?  */
15989   if (c != ',' || (c == ',' && next_c == '"'))
15990     {
15991       /* just after name is now '\0'.  */
15992       *input_line_pointer = c;
15993       input_line_pointer = section_name;
15994       obj_elf_section (ignore);
15995       return;
15996     }
15997   input_line_pointer++;
15998 
15999   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16000   if (c == ',')
16001     section_type = get_absolute_expression ();
16002   else
16003     section_type = 0;
16004   if (*input_line_pointer++ == ',')
16005     section_flag = get_absolute_expression ();
16006   else
16007     section_flag = 0;
16008   if (*input_line_pointer++ == ',')
16009     section_entry_size = get_absolute_expression ();
16010   else
16011     section_entry_size = 0;
16012   if (*input_line_pointer++ == ',')
16013     section_alignment = get_absolute_expression ();
16014   else
16015     section_alignment = 0;
16016   /* FIXME: really ignore?  */
16017   (void) section_alignment;
16018 
16019   section_name = xstrdup (section_name);
16020 
16021   /* When using the generic form of .section (as implemented by obj-elf.c),
16022      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16023      traditionally had to fall back on the more common @progbits instead.
16024 
16025      There's nothing really harmful in this, since bfd will correct
16026      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16027      means that, for backwards compatibility, the special_section entries
16028      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16029 
16030      Even so, we shouldn't force users of the MIPS .section syntax to
16031      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16032      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16033      generic type-checking code.  */
16034   if (section_type == SHT_MIPS_DWARF)
16035     section_type = SHT_PROGBITS;
16036 
16037   obj_elf_change_section (section_name, section_type, section_flag,
16038 			  section_entry_size, 0, 0, 0);
16039 
16040   if (now_seg->name != section_name)
16041     free (section_name);
16042 #endif /* OBJ_ELF */
16043 }
16044 
16045 void
16046 mips_enable_auto_align (void)
16047 {
16048   auto_align = 1;
16049 }
16050 
16051 static void
16052 s_cons (int log_size)
16053 {
16054   segment_info_type *si = seg_info (now_seg);
16055   struct insn_label_list *l = si->label_list;
16056 
16057   mips_emit_delays ();
16058   if (log_size > 0 && auto_align)
16059     mips_align (log_size, 0, l);
16060   cons (1 << log_size);
16061   mips_clear_insn_labels ();
16062 }
16063 
16064 static void
16065 s_float_cons (int type)
16066 {
16067   segment_info_type *si = seg_info (now_seg);
16068   struct insn_label_list *l = si->label_list;
16069 
16070   mips_emit_delays ();
16071 
16072   if (auto_align)
16073     {
16074       if (type == 'd')
16075 	mips_align (3, 0, l);
16076       else
16077 	mips_align (2, 0, l);
16078     }
16079 
16080   float_cons (type);
16081   mips_clear_insn_labels ();
16082 }
16083 
16084 /* Handle .globl.  We need to override it because on Irix 5 you are
16085    permitted to say
16086        .globl foo .text
16087    where foo is an undefined symbol, to mean that foo should be
16088    considered to be the address of a function.  */
16089 
16090 static void
16091 s_mips_globl (int x ATTRIBUTE_UNUSED)
16092 {
16093   char *name;
16094   int c;
16095   symbolS *symbolP;
16096   flagword flag;
16097 
16098   do
16099     {
16100       name = input_line_pointer;
16101       c = get_symbol_end ();
16102       symbolP = symbol_find_or_make (name);
16103       S_SET_EXTERNAL (symbolP);
16104 
16105       *input_line_pointer = c;
16106       SKIP_WHITESPACE ();
16107 
16108       /* On Irix 5, every global symbol that is not explicitly labelled as
16109          being a function is apparently labelled as being an object.  */
16110       flag = BSF_OBJECT;
16111 
16112       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16113 	  && (*input_line_pointer != ','))
16114 	{
16115 	  char *secname;
16116 	  asection *sec;
16117 
16118 	  secname = input_line_pointer;
16119 	  c = get_symbol_end ();
16120 	  sec = bfd_get_section_by_name (stdoutput, secname);
16121 	  if (sec == NULL)
16122 	    as_bad (_("%s: no such section"), secname);
16123 	  *input_line_pointer = c;
16124 
16125 	  if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16126 	    flag = BSF_FUNCTION;
16127 	}
16128 
16129       symbol_get_bfdsym (symbolP)->flags |= flag;
16130 
16131       c = *input_line_pointer;
16132       if (c == ',')
16133 	{
16134 	  input_line_pointer++;
16135 	  SKIP_WHITESPACE ();
16136 	  if (is_end_of_line[(unsigned char) *input_line_pointer])
16137 	    c = '\n';
16138 	}
16139     }
16140   while (c == ',');
16141 
16142   demand_empty_rest_of_line ();
16143 }
16144 
16145 static void
16146 s_option (int x ATTRIBUTE_UNUSED)
16147 {
16148   char *opt;
16149   char c;
16150 
16151   opt = input_line_pointer;
16152   c = get_symbol_end ();
16153 
16154   if (*opt == 'O')
16155     {
16156       /* FIXME: What does this mean?  */
16157     }
16158   else if (strncmp (opt, "pic", 3) == 0)
16159     {
16160       int i;
16161 
16162       i = atoi (opt + 3);
16163       if (i == 0)
16164 	mips_pic = NO_PIC;
16165       else if (i == 2)
16166 	{
16167 	  mips_pic = SVR4_PIC;
16168 	  mips_abicalls = TRUE;
16169 	}
16170       else
16171 	as_bad (_(".option pic%d not supported"), i);
16172 
16173       if (mips_pic == SVR4_PIC)
16174 	{
16175 	  if (g_switch_seen && g_switch_value != 0)
16176 	    as_warn (_("-G may not be used with SVR4 PIC code"));
16177 	  g_switch_value = 0;
16178 	  bfd_set_gp_size (stdoutput, 0);
16179 	}
16180     }
16181   else
16182     as_warn (_("Unrecognized option \"%s\""), opt);
16183 
16184   *input_line_pointer = c;
16185   demand_empty_rest_of_line ();
16186 }
16187 
16188 /* This structure is used to hold a stack of .set values.  */
16189 
16190 struct mips_option_stack
16191 {
16192   struct mips_option_stack *next;
16193   struct mips_set_options options;
16194 };
16195 
16196 static struct mips_option_stack *mips_opts_stack;
16197 
16198 /* Handle the .set pseudo-op.  */
16199 
16200 static void
16201 s_mipsset (int x ATTRIBUTE_UNUSED)
16202 {
16203   char *name = input_line_pointer, ch;
16204 
16205   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16206     ++input_line_pointer;
16207   ch = *input_line_pointer;
16208   *input_line_pointer = '\0';
16209 
16210   if (strcmp (name, "reorder") == 0)
16211     {
16212       if (mips_opts.noreorder)
16213 	end_noreorder ();
16214     }
16215   else if (strcmp (name, "noreorder") == 0)
16216     {
16217       if (!mips_opts.noreorder)
16218 	start_noreorder ();
16219     }
16220   else if (strncmp (name, "at=", 3) == 0)
16221     {
16222       char *s = name + 3;
16223 
16224       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16225 	as_bad (_("Unrecognized register name `%s'"), s);
16226     }
16227   else if (strcmp (name, "at") == 0)
16228     {
16229       mips_opts.at = ATREG;
16230     }
16231   else if (strcmp (name, "noat") == 0)
16232     {
16233       mips_opts.at = ZERO;
16234     }
16235   else if (strcmp (name, "macro") == 0)
16236     {
16237       mips_opts.warn_about_macros = 0;
16238     }
16239   else if (strcmp (name, "nomacro") == 0)
16240     {
16241       if (mips_opts.noreorder == 0)
16242 	as_bad (_("`noreorder' must be set before `nomacro'"));
16243       mips_opts.warn_about_macros = 1;
16244     }
16245   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16246     {
16247       mips_opts.nomove = 0;
16248     }
16249   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16250     {
16251       mips_opts.nomove = 1;
16252     }
16253   else if (strcmp (name, "bopt") == 0)
16254     {
16255       mips_opts.nobopt = 0;
16256     }
16257   else if (strcmp (name, "nobopt") == 0)
16258     {
16259       mips_opts.nobopt = 1;
16260     }
16261   else if (strcmp (name, "gp=default") == 0)
16262     mips_opts.gp32 = file_mips_gp32;
16263   else if (strcmp (name, "gp=32") == 0)
16264     mips_opts.gp32 = 1;
16265   else if (strcmp (name, "gp=64") == 0)
16266     {
16267       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16268 	as_warn (_("%s isa does not support 64-bit registers"),
16269 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16270       mips_opts.gp32 = 0;
16271     }
16272   else if (strcmp (name, "fp=default") == 0)
16273     mips_opts.fp32 = file_mips_fp32;
16274   else if (strcmp (name, "fp=32") == 0)
16275     mips_opts.fp32 = 1;
16276   else if (strcmp (name, "fp=64") == 0)
16277     {
16278       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16279 	as_warn (_("%s isa does not support 64-bit floating point registers"),
16280 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16281       mips_opts.fp32 = 0;
16282     }
16283   else if (strcmp (name, "softfloat") == 0)
16284     mips_opts.soft_float = 1;
16285   else if (strcmp (name, "hardfloat") == 0)
16286     mips_opts.soft_float = 0;
16287   else if (strcmp (name, "singlefloat") == 0)
16288     mips_opts.single_float = 1;
16289   else if (strcmp (name, "doublefloat") == 0)
16290     mips_opts.single_float = 0;
16291   else if (strcmp (name, "mips16") == 0
16292 	   || strcmp (name, "MIPS-16") == 0)
16293     {
16294       if (mips_opts.micromips == 1)
16295 	as_fatal (_("`mips16' cannot be used with `micromips'"));
16296       mips_opts.mips16 = 1;
16297     }
16298   else if (strcmp (name, "nomips16") == 0
16299 	   || strcmp (name, "noMIPS-16") == 0)
16300     mips_opts.mips16 = 0;
16301   else if (strcmp (name, "micromips") == 0)
16302     {
16303       if (mips_opts.mips16 == 1)
16304 	as_fatal (_("`micromips' cannot be used with `mips16'"));
16305       mips_opts.micromips = 1;
16306     }
16307   else if (strcmp (name, "nomicromips") == 0)
16308     mips_opts.micromips = 0;
16309   else if (strcmp (name, "smartmips") == 0)
16310     {
16311       if (!ISA_SUPPORTS_SMARTMIPS)
16312 	as_warn (_("%s ISA does not support SmartMIPS ASE"),
16313 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16314       mips_opts.ase_smartmips = 1;
16315     }
16316   else if (strcmp (name, "nosmartmips") == 0)
16317     mips_opts.ase_smartmips = 0;
16318   else if (strcmp (name, "mips3d") == 0)
16319     mips_opts.ase_mips3d = 1;
16320   else if (strcmp (name, "nomips3d") == 0)
16321     mips_opts.ase_mips3d = 0;
16322   else if (strcmp (name, "mdmx") == 0)
16323     mips_opts.ase_mdmx = 1;
16324   else if (strcmp (name, "nomdmx") == 0)
16325     mips_opts.ase_mdmx = 0;
16326   else if (strcmp (name, "dsp") == 0)
16327     {
16328       if (!ISA_SUPPORTS_DSP_ASE)
16329 	as_warn (_("%s ISA does not support DSP ASE"),
16330 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16331       mips_opts.ase_dsp = 1;
16332       mips_opts.ase_dspr2 = 0;
16333     }
16334   else if (strcmp (name, "nodsp") == 0)
16335     {
16336       mips_opts.ase_dsp = 0;
16337       mips_opts.ase_dspr2 = 0;
16338     }
16339   else if (strcmp (name, "dspr2") == 0)
16340     {
16341       if (!ISA_SUPPORTS_DSPR2_ASE)
16342 	as_warn (_("%s ISA does not support DSP R2 ASE"),
16343 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16344       mips_opts.ase_dspr2 = 1;
16345       mips_opts.ase_dsp = 1;
16346     }
16347   else if (strcmp (name, "nodspr2") == 0)
16348     {
16349       mips_opts.ase_dspr2 = 0;
16350       mips_opts.ase_dsp = 0;
16351     }
16352   else if (strcmp (name, "mt") == 0)
16353     {
16354       if (!ISA_SUPPORTS_MT_ASE)
16355 	as_warn (_("%s ISA does not support MT ASE"),
16356 		 mips_cpu_info_from_isa (mips_opts.isa)->name);
16357       mips_opts.ase_mt = 1;
16358     }
16359   else if (strcmp (name, "nomt") == 0)
16360     mips_opts.ase_mt = 0;
16361   else if (strcmp (name, "mcu") == 0)
16362     mips_opts.ase_mcu = 1;
16363   else if (strcmp (name, "nomcu") == 0)
16364     mips_opts.ase_mcu = 0;
16365   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16366     {
16367       int reset = 0;
16368 
16369       /* Permit the user to change the ISA and architecture on the fly.
16370 	 Needless to say, misuse can cause serious problems.  */
16371       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16372 	{
16373 	  reset = 1;
16374 	  mips_opts.isa = file_mips_isa;
16375 	  mips_opts.arch = file_mips_arch;
16376 	}
16377       else if (strncmp (name, "arch=", 5) == 0)
16378 	{
16379 	  const struct mips_cpu_info *p;
16380 
16381 	  p = mips_parse_cpu("internal use", name + 5);
16382 	  if (!p)
16383 	    as_bad (_("unknown architecture %s"), name + 5);
16384 	  else
16385 	    {
16386 	      mips_opts.arch = p->cpu;
16387 	      mips_opts.isa = p->isa;
16388 	    }
16389 	}
16390       else if (strncmp (name, "mips", 4) == 0)
16391 	{
16392 	  const struct mips_cpu_info *p;
16393 
16394 	  p = mips_parse_cpu("internal use", name);
16395 	  if (!p)
16396 	    as_bad (_("unknown ISA level %s"), name + 4);
16397 	  else
16398 	    {
16399 	      mips_opts.arch = p->cpu;
16400 	      mips_opts.isa = p->isa;
16401 	    }
16402 	}
16403       else
16404 	as_bad (_("unknown ISA or architecture %s"), name);
16405 
16406       switch (mips_opts.isa)
16407 	{
16408 	case  0:
16409 	  break;
16410 	case ISA_MIPS1:
16411 	case ISA_MIPS2:
16412 	case ISA_MIPS32:
16413 	case ISA_MIPS32R2:
16414 	  mips_opts.gp32 = 1;
16415 	  mips_opts.fp32 = 1;
16416 	  break;
16417 	case ISA_MIPS3:
16418 	case ISA_MIPS4:
16419 	case ISA_MIPS5:
16420 	case ISA_MIPS64:
16421 	case ISA_MIPS64R2:
16422 	  mips_opts.gp32 = 0;
16423 	  mips_opts.fp32 = 0;
16424 	  break;
16425 	default:
16426 	  as_bad (_("unknown ISA level %s"), name + 4);
16427 	  break;
16428 	}
16429       if (reset)
16430 	{
16431 	  mips_opts.gp32 = file_mips_gp32;
16432 	  mips_opts.fp32 = file_mips_fp32;
16433 	}
16434     }
16435   else if (strcmp (name, "autoextend") == 0)
16436     mips_opts.noautoextend = 0;
16437   else if (strcmp (name, "noautoextend") == 0)
16438     mips_opts.noautoextend = 1;
16439   else if (strcmp (name, "push") == 0)
16440     {
16441       struct mips_option_stack *s;
16442 
16443       s = (struct mips_option_stack *) xmalloc (sizeof *s);
16444       s->next = mips_opts_stack;
16445       s->options = mips_opts;
16446       mips_opts_stack = s;
16447     }
16448   else if (strcmp (name, "pop") == 0)
16449     {
16450       struct mips_option_stack *s;
16451 
16452       s = mips_opts_stack;
16453       if (s == NULL)
16454 	as_bad (_(".set pop with no .set push"));
16455       else
16456 	{
16457 	  /* If we're changing the reorder mode we need to handle
16458              delay slots correctly.  */
16459 	  if (s->options.noreorder && ! mips_opts.noreorder)
16460 	    start_noreorder ();
16461 	  else if (! s->options.noreorder && mips_opts.noreorder)
16462 	    end_noreorder ();
16463 
16464 	  mips_opts = s->options;
16465 	  mips_opts_stack = s->next;
16466 	  free (s);
16467 	}
16468     }
16469   else if (strcmp (name, "sym32") == 0)
16470     mips_opts.sym32 = TRUE;
16471   else if (strcmp (name, "nosym32") == 0)
16472     mips_opts.sym32 = FALSE;
16473   else if (strchr (name, ','))
16474     {
16475       /* Generic ".set" directive; use the generic handler.  */
16476       *input_line_pointer = ch;
16477       input_line_pointer = name;
16478       s_set (0);
16479       return;
16480     }
16481   else
16482     {
16483       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16484     }
16485   *input_line_pointer = ch;
16486   demand_empty_rest_of_line ();
16487 }
16488 
16489 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16490    .option pic2.  It means to generate SVR4 PIC calls.  */
16491 
16492 static void
16493 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16494 {
16495   mips_pic = SVR4_PIC;
16496   mips_abicalls = TRUE;
16497 
16498   if (g_switch_seen && g_switch_value != 0)
16499     as_warn (_("-G may not be used with SVR4 PIC code"));
16500   g_switch_value = 0;
16501 
16502   bfd_set_gp_size (stdoutput, 0);
16503   demand_empty_rest_of_line ();
16504 }
16505 
16506 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16507    PIC code.  It sets the $gp register for the function based on the
16508    function address, which is in the register named in the argument.
16509    This uses a relocation against _gp_disp, which is handled specially
16510    by the linker.  The result is:
16511 	lui	$gp,%hi(_gp_disp)
16512 	addiu	$gp,$gp,%lo(_gp_disp)
16513 	addu	$gp,$gp,.cpload argument
16514    The .cpload argument is normally $25 == $t9.
16515 
16516    The -mno-shared option changes this to:
16517 	lui	$gp,%hi(__gnu_local_gp)
16518 	addiu	$gp,$gp,%lo(__gnu_local_gp)
16519    and the argument is ignored.  This saves an instruction, but the
16520    resulting code is not position independent; it uses an absolute
16521    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16522    can go into an ordinary executable, but not into a shared library.  */
16523 
16524 static void
16525 s_cpload (int ignore ATTRIBUTE_UNUSED)
16526 {
16527   expressionS ex;
16528   int reg;
16529   int in_shared;
16530 
16531   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16532      .cpload is ignored.  */
16533   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16534     {
16535       s_ignore (0);
16536       return;
16537     }
16538 
16539   if (mips_opts.mips16)
16540     {
16541       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16542       ignore_rest_of_line ();
16543       return;
16544     }
16545 
16546   /* .cpload should be in a .set noreorder section.  */
16547   if (mips_opts.noreorder == 0)
16548     as_warn (_(".cpload not in noreorder section"));
16549 
16550   reg = tc_get_register (0);
16551 
16552   /* If we need to produce a 64-bit address, we are better off using
16553      the default instruction sequence.  */
16554   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16555 
16556   ex.X_op = O_symbol;
16557   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16558                                          "__gnu_local_gp");
16559   ex.X_op_symbol = NULL;
16560   ex.X_add_number = 0;
16561 
16562   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16563   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16564 
16565   macro_start ();
16566   macro_build_lui (&ex, mips_gp_register);
16567   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16568 	       mips_gp_register, BFD_RELOC_LO16);
16569   if (in_shared)
16570     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16571 		 mips_gp_register, reg);
16572   macro_end ();
16573 
16574   demand_empty_rest_of_line ();
16575 }
16576 
16577 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16578      .cpsetup $reg1, offset|$reg2, label
16579 
16580    If offset is given, this results in:
16581      sd		$gp, offset($sp)
16582      lui	$gp, %hi(%neg(%gp_rel(label)))
16583      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16584      daddu	$gp, $gp, $reg1
16585 
16586    If $reg2 is given, this results in:
16587      daddu	$reg2, $gp, $0
16588      lui	$gp, %hi(%neg(%gp_rel(label)))
16589      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16590      daddu	$gp, $gp, $reg1
16591    $reg1 is normally $25 == $t9.
16592 
16593    The -mno-shared option replaces the last three instructions with
16594 	lui	$gp,%hi(_gp)
16595 	addiu	$gp,$gp,%lo(_gp)  */
16596 
16597 static void
16598 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16599 {
16600   expressionS ex_off;
16601   expressionS ex_sym;
16602   int reg1;
16603 
16604   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16605      We also need NewABI support.  */
16606   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16607     {
16608       s_ignore (0);
16609       return;
16610     }
16611 
16612   if (mips_opts.mips16)
16613     {
16614       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16615       ignore_rest_of_line ();
16616       return;
16617     }
16618 
16619   reg1 = tc_get_register (0);
16620   SKIP_WHITESPACE ();
16621   if (*input_line_pointer != ',')
16622     {
16623       as_bad (_("missing argument separator ',' for .cpsetup"));
16624       return;
16625     }
16626   else
16627     ++input_line_pointer;
16628   SKIP_WHITESPACE ();
16629   if (*input_line_pointer == '$')
16630     {
16631       mips_cpreturn_register = tc_get_register (0);
16632       mips_cpreturn_offset = -1;
16633     }
16634   else
16635     {
16636       mips_cpreturn_offset = get_absolute_expression ();
16637       mips_cpreturn_register = -1;
16638     }
16639   SKIP_WHITESPACE ();
16640   if (*input_line_pointer != ',')
16641     {
16642       as_bad (_("missing argument separator ',' for .cpsetup"));
16643       return;
16644     }
16645   else
16646     ++input_line_pointer;
16647   SKIP_WHITESPACE ();
16648   expression (&ex_sym);
16649 
16650   macro_start ();
16651   if (mips_cpreturn_register == -1)
16652     {
16653       ex_off.X_op = O_constant;
16654       ex_off.X_add_symbol = NULL;
16655       ex_off.X_op_symbol = NULL;
16656       ex_off.X_add_number = mips_cpreturn_offset;
16657 
16658       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16659 		   BFD_RELOC_LO16, SP);
16660     }
16661   else
16662     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16663 		 mips_gp_register, 0);
16664 
16665   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16666     {
16667       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16668 		   -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16669 		   BFD_RELOC_HI16_S);
16670 
16671       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16672 		   mips_gp_register, -1, BFD_RELOC_GPREL16,
16673 		   BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16674 
16675       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16676 		   mips_gp_register, reg1);
16677     }
16678   else
16679     {
16680       expressionS ex;
16681 
16682       ex.X_op = O_symbol;
16683       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16684       ex.X_op_symbol = NULL;
16685       ex.X_add_number = 0;
16686 
16687       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16688       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16689 
16690       macro_build_lui (&ex, mips_gp_register);
16691       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16692 		   mips_gp_register, BFD_RELOC_LO16);
16693     }
16694 
16695   macro_end ();
16696 
16697   demand_empty_rest_of_line ();
16698 }
16699 
16700 static void
16701 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16702 {
16703   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16704      .cplocal is ignored.  */
16705   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16706     {
16707       s_ignore (0);
16708       return;
16709     }
16710 
16711   if (mips_opts.mips16)
16712     {
16713       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16714       ignore_rest_of_line ();
16715       return;
16716     }
16717 
16718   mips_gp_register = tc_get_register (0);
16719   demand_empty_rest_of_line ();
16720 }
16721 
16722 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16723    offset from $sp.  The offset is remembered, and after making a PIC
16724    call $gp is restored from that location.  */
16725 
16726 static void
16727 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16728 {
16729   expressionS ex;
16730 
16731   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16732      .cprestore is ignored.  */
16733   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16734     {
16735       s_ignore (0);
16736       return;
16737     }
16738 
16739   if (mips_opts.mips16)
16740     {
16741       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16742       ignore_rest_of_line ();
16743       return;
16744     }
16745 
16746   mips_cprestore_offset = get_absolute_expression ();
16747   mips_cprestore_valid = 1;
16748 
16749   ex.X_op = O_constant;
16750   ex.X_add_symbol = NULL;
16751   ex.X_op_symbol = NULL;
16752   ex.X_add_number = mips_cprestore_offset;
16753 
16754   macro_start ();
16755   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16756 				SP, HAVE_64BIT_ADDRESSES);
16757   macro_end ();
16758 
16759   demand_empty_rest_of_line ();
16760 }
16761 
16762 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16763    was given in the preceding .cpsetup, it results in:
16764      ld		$gp, offset($sp)
16765 
16766    If a register $reg2 was given there, it results in:
16767      daddu	$gp, $reg2, $0  */
16768 
16769 static void
16770 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16771 {
16772   expressionS ex;
16773 
16774   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16775      We also need NewABI support.  */
16776   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16777     {
16778       s_ignore (0);
16779       return;
16780     }
16781 
16782   if (mips_opts.mips16)
16783     {
16784       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16785       ignore_rest_of_line ();
16786       return;
16787     }
16788 
16789   macro_start ();
16790   if (mips_cpreturn_register == -1)
16791     {
16792       ex.X_op = O_constant;
16793       ex.X_add_symbol = NULL;
16794       ex.X_op_symbol = NULL;
16795       ex.X_add_number = mips_cpreturn_offset;
16796 
16797       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16798     }
16799   else
16800     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16801 		 mips_cpreturn_register, 0);
16802   macro_end ();
16803 
16804   demand_empty_rest_of_line ();
16805 }
16806 
16807 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16808    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16809    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16810    debug information or MIPS16 TLS.  */
16811 
16812 static void
16813 s_tls_rel_directive (const size_t bytes, const char *dirstr,
16814 		     bfd_reloc_code_real_type rtype)
16815 {
16816   expressionS ex;
16817   char *p;
16818 
16819   expression (&ex);
16820 
16821   if (ex.X_op != O_symbol)
16822     {
16823       as_bad (_("Unsupported use of %s"), dirstr);
16824       ignore_rest_of_line ();
16825     }
16826 
16827   p = frag_more (bytes);
16828   md_number_to_chars (p, 0, bytes);
16829   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
16830   demand_empty_rest_of_line ();
16831   mips_clear_insn_labels ();
16832 }
16833 
16834 /* Handle .dtprelword.  */
16835 
16836 static void
16837 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16838 {
16839   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
16840 }
16841 
16842 /* Handle .dtpreldword.  */
16843 
16844 static void
16845 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16846 {
16847   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16848 }
16849 
16850 /* Handle .tprelword.  */
16851 
16852 static void
16853 s_tprelword (int ignore ATTRIBUTE_UNUSED)
16854 {
16855   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16856 }
16857 
16858 /* Handle .tpreldword.  */
16859 
16860 static void
16861 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16862 {
16863   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
16864 }
16865 
16866 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
16867    code.  It sets the offset to use in gp_rel relocations.  */
16868 
16869 static void
16870 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16871 {
16872   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16873      We also need NewABI support.  */
16874   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16875     {
16876       s_ignore (0);
16877       return;
16878     }
16879 
16880   mips_gprel_offset = get_absolute_expression ();
16881 
16882   demand_empty_rest_of_line ();
16883 }
16884 
16885 /* Handle the .gpword pseudo-op.  This is used when generating PIC
16886    code.  It generates a 32 bit GP relative reloc.  */
16887 
16888 static void
16889 s_gpword (int ignore ATTRIBUTE_UNUSED)
16890 {
16891   segment_info_type *si;
16892   struct insn_label_list *l;
16893   expressionS ex;
16894   char *p;
16895 
16896   /* When not generating PIC code, this is treated as .word.  */
16897   if (mips_pic != SVR4_PIC)
16898     {
16899       s_cons (2);
16900       return;
16901     }
16902 
16903   si = seg_info (now_seg);
16904   l = si->label_list;
16905   mips_emit_delays ();
16906   if (auto_align)
16907     mips_align (2, 0, l);
16908 
16909   expression (&ex);
16910   mips_clear_insn_labels ();
16911 
16912   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16913     {
16914       as_bad (_("Unsupported use of .gpword"));
16915       ignore_rest_of_line ();
16916     }
16917 
16918   p = frag_more (4);
16919   md_number_to_chars (p, 0, 4);
16920   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16921 	       BFD_RELOC_GPREL32);
16922 
16923   demand_empty_rest_of_line ();
16924 }
16925 
16926 static void
16927 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16928 {
16929   segment_info_type *si;
16930   struct insn_label_list *l;
16931   expressionS ex;
16932   char *p;
16933 
16934   /* When not generating PIC code, this is treated as .dword.  */
16935   if (mips_pic != SVR4_PIC)
16936     {
16937       s_cons (3);
16938       return;
16939     }
16940 
16941   si = seg_info (now_seg);
16942   l = si->label_list;
16943   mips_emit_delays ();
16944   if (auto_align)
16945     mips_align (3, 0, l);
16946 
16947   expression (&ex);
16948   mips_clear_insn_labels ();
16949 
16950   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16951     {
16952       as_bad (_("Unsupported use of .gpdword"));
16953       ignore_rest_of_line ();
16954     }
16955 
16956   p = frag_more (8);
16957   md_number_to_chars (p, 0, 8);
16958   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16959 	       BFD_RELOC_GPREL32)->fx_tcbit = 1;
16960 
16961   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
16962   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16963 	   FALSE, BFD_RELOC_64)->fx_tcbit = 1;
16964 
16965   demand_empty_rest_of_line ();
16966 }
16967 
16968 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
16969    tables in SVR4 PIC code.  */
16970 
16971 static void
16972 s_cpadd (int ignore ATTRIBUTE_UNUSED)
16973 {
16974   int reg;
16975 
16976   /* This is ignored when not generating SVR4 PIC code.  */
16977   if (mips_pic != SVR4_PIC)
16978     {
16979       s_ignore (0);
16980       return;
16981     }
16982 
16983   /* Add $gp to the register named as an argument.  */
16984   macro_start ();
16985   reg = tc_get_register (0);
16986   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
16987   macro_end ();
16988 
16989   demand_empty_rest_of_line ();
16990 }
16991 
16992 /* Handle the .insn pseudo-op.  This marks instruction labels in
16993    mips16/micromips mode.  This permits the linker to handle them specially,
16994    such as generating jalx instructions when needed.  We also make
16995    them odd for the duration of the assembly, in order to generate the
16996    right sort of code.  We will make them even in the adjust_symtab
16997    routine, while leaving them marked.  This is convenient for the
16998    debugger and the disassembler.  The linker knows to make them odd
16999    again.  */
17000 
17001 static void
17002 s_insn (int ignore ATTRIBUTE_UNUSED)
17003 {
17004   mips_mark_labels ();
17005 
17006   demand_empty_rest_of_line ();
17007 }
17008 
17009 /* Handle a .stabn directive.  We need these in order to mark a label
17010    as being a mips16 text label correctly.  Sometimes the compiler
17011    will emit a label, followed by a .stabn, and then switch sections.
17012    If the label and .stabn are in mips16 mode, then the label is
17013    really a mips16 text label.  */
17014 
17015 static void
17016 s_mips_stab (int type)
17017 {
17018   if (type == 'n')
17019     mips_mark_labels ();
17020 
17021   s_stab (type);
17022 }
17023 
17024 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17025 
17026 static void
17027 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17028 {
17029   char *name;
17030   int c;
17031   symbolS *symbolP;
17032   expressionS exp;
17033 
17034   name = input_line_pointer;
17035   c = get_symbol_end ();
17036   symbolP = symbol_find_or_make (name);
17037   S_SET_WEAK (symbolP);
17038   *input_line_pointer = c;
17039 
17040   SKIP_WHITESPACE ();
17041 
17042   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17043     {
17044       if (S_IS_DEFINED (symbolP))
17045 	{
17046 	  as_bad (_("ignoring attempt to redefine symbol %s"),
17047 		  S_GET_NAME (symbolP));
17048 	  ignore_rest_of_line ();
17049 	  return;
17050 	}
17051 
17052       if (*input_line_pointer == ',')
17053 	{
17054 	  ++input_line_pointer;
17055 	  SKIP_WHITESPACE ();
17056 	}
17057 
17058       expression (&exp);
17059       if (exp.X_op != O_symbol)
17060 	{
17061 	  as_bad (_("bad .weakext directive"));
17062 	  ignore_rest_of_line ();
17063 	  return;
17064 	}
17065       symbol_set_value_expression (symbolP, &exp);
17066     }
17067 
17068   demand_empty_rest_of_line ();
17069 }
17070 
17071 /* Parse a register string into a number.  Called from the ECOFF code
17072    to parse .frame.  The argument is non-zero if this is the frame
17073    register, so that we can record it in mips_frame_reg.  */
17074 
17075 int
17076 tc_get_register (int frame)
17077 {
17078   unsigned int reg;
17079 
17080   SKIP_WHITESPACE ();
17081   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17082     reg = 0;
17083   if (frame)
17084     {
17085       mips_frame_reg = reg != 0 ? reg : SP;
17086       mips_frame_reg_valid = 1;
17087       mips_cprestore_valid = 0;
17088     }
17089   return reg;
17090 }
17091 
17092 valueT
17093 md_section_align (asection *seg, valueT addr)
17094 {
17095   int align = bfd_get_section_alignment (stdoutput, seg);
17096 
17097   if (IS_ELF)
17098     {
17099       /* We don't need to align ELF sections to the full alignment.
17100 	 However, Irix 5 may prefer that we align them at least to a 16
17101 	 byte boundary.  We don't bother to align the sections if we
17102 	 are targeted for an embedded system.  */
17103       if (strncmp (TARGET_OS, "elf", 3) == 0)
17104         return addr;
17105       if (align > 4)
17106         align = 4;
17107     }
17108 
17109   return ((addr + (1 << align) - 1) & (-1 << align));
17110 }
17111 
17112 /* Utility routine, called from above as well.  If called while the
17113    input file is still being read, it's only an approximation.  (For
17114    example, a symbol may later become defined which appeared to be
17115    undefined earlier.)  */
17116 
17117 static int
17118 nopic_need_relax (symbolS *sym, int before_relaxing)
17119 {
17120   if (sym == 0)
17121     return 0;
17122 
17123   if (g_switch_value > 0)
17124     {
17125       const char *symname;
17126       int change;
17127 
17128       /* Find out whether this symbol can be referenced off the $gp
17129 	 register.  It can be if it is smaller than the -G size or if
17130 	 it is in the .sdata or .sbss section.  Certain symbols can
17131 	 not be referenced off the $gp, although it appears as though
17132 	 they can.  */
17133       symname = S_GET_NAME (sym);
17134       if (symname != (const char *) NULL
17135 	  && (strcmp (symname, "eprol") == 0
17136 	      || strcmp (symname, "etext") == 0
17137 	      || strcmp (symname, "_gp") == 0
17138 	      || strcmp (symname, "edata") == 0
17139 	      || strcmp (symname, "_fbss") == 0
17140 	      || strcmp (symname, "_fdata") == 0
17141 	      || strcmp (symname, "_ftext") == 0
17142 	      || strcmp (symname, "end") == 0
17143 	      || strcmp (symname, "_gp_disp") == 0))
17144 	change = 1;
17145       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17146 	       && (0
17147 #ifndef NO_ECOFF_DEBUGGING
17148 		   || (symbol_get_obj (sym)->ecoff_extern_size != 0
17149 		       && (symbol_get_obj (sym)->ecoff_extern_size
17150 			   <= g_switch_value))
17151 #endif
17152 		   /* We must defer this decision until after the whole
17153 		      file has been read, since there might be a .extern
17154 		      after the first use of this symbol.  */
17155 		   || (before_relaxing
17156 #ifndef NO_ECOFF_DEBUGGING
17157 		       && symbol_get_obj (sym)->ecoff_extern_size == 0
17158 #endif
17159 		       && S_GET_VALUE (sym) == 0)
17160 		   || (S_GET_VALUE (sym) != 0
17161 		       && S_GET_VALUE (sym) <= g_switch_value)))
17162 	change = 0;
17163       else
17164 	{
17165 	  const char *segname;
17166 
17167 	  segname = segment_name (S_GET_SEGMENT (sym));
17168 	  gas_assert (strcmp (segname, ".lit8") != 0
17169 		  && strcmp (segname, ".lit4") != 0);
17170 	  change = (strcmp (segname, ".sdata") != 0
17171 		    && strcmp (segname, ".sbss") != 0
17172 		    && strncmp (segname, ".sdata.", 7) != 0
17173 		    && strncmp (segname, ".sbss.", 6) != 0
17174 		    && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17175 		    && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17176 	}
17177       return change;
17178     }
17179   else
17180     /* We are not optimizing for the $gp register.  */
17181     return 1;
17182 }
17183 
17184 
17185 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17186 
17187 static bfd_boolean
17188 pic_need_relax (symbolS *sym, asection *segtype)
17189 {
17190   asection *symsec;
17191 
17192   /* Handle the case of a symbol equated to another symbol.  */
17193   while (symbol_equated_reloc_p (sym))
17194     {
17195       symbolS *n;
17196 
17197       /* It's possible to get a loop here in a badly written program.  */
17198       n = symbol_get_value_expression (sym)->X_add_symbol;
17199       if (n == sym)
17200 	break;
17201       sym = n;
17202     }
17203 
17204   if (symbol_section_p (sym))
17205     return TRUE;
17206 
17207   symsec = S_GET_SEGMENT (sym);
17208 
17209   /* This must duplicate the test in adjust_reloc_syms.  */
17210   return (!bfd_is_und_section (symsec)
17211 	  && !bfd_is_abs_section (symsec)
17212 	  && !bfd_is_com_section (symsec)
17213 	  && !s_is_linkonce (sym, segtype)
17214 #ifdef OBJ_ELF
17215 	  /* A global or weak symbol is treated as external.  */
17216 	  && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
17217 #endif
17218 	  );
17219 }
17220 
17221 
17222 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17223    extended opcode.  SEC is the section the frag is in.  */
17224 
17225 static int
17226 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17227 {
17228   int type;
17229   const struct mips16_immed_operand *op;
17230   offsetT val;
17231   int mintiny, maxtiny;
17232   segT symsec;
17233   fragS *sym_frag;
17234 
17235   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17236     return 0;
17237   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17238     return 1;
17239 
17240   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17241   op = mips16_immed_operands;
17242   while (op->type != type)
17243     {
17244       ++op;
17245       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17246     }
17247 
17248   if (op->unsp)
17249     {
17250       if (type == '<' || type == '>' || type == '[' || type == ']')
17251 	{
17252 	  mintiny = 1;
17253 	  maxtiny = 1 << op->nbits;
17254 	}
17255       else
17256 	{
17257 	  mintiny = 0;
17258 	  maxtiny = (1 << op->nbits) - 1;
17259 	}
17260     }
17261   else
17262     {
17263       mintiny = - (1 << (op->nbits - 1));
17264       maxtiny = (1 << (op->nbits - 1)) - 1;
17265     }
17266 
17267   sym_frag = symbol_get_frag (fragp->fr_symbol);
17268   val = S_GET_VALUE (fragp->fr_symbol);
17269   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17270 
17271   if (op->pcrel)
17272     {
17273       addressT addr;
17274 
17275       /* We won't have the section when we are called from
17276          mips_relax_frag.  However, we will always have been called
17277          from md_estimate_size_before_relax first.  If this is a
17278          branch to a different section, we mark it as such.  If SEC is
17279          NULL, and the frag is not marked, then it must be a branch to
17280          the same section.  */
17281       if (sec == NULL)
17282 	{
17283 	  if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17284 	    return 1;
17285 	}
17286       else
17287 	{
17288 	  /* Must have been called from md_estimate_size_before_relax.  */
17289 	  if (symsec != sec)
17290 	    {
17291 	      fragp->fr_subtype =
17292 		RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17293 
17294 	      /* FIXME: We should support this, and let the linker
17295                  catch branches and loads that are out of range.  */
17296 	      as_bad_where (fragp->fr_file, fragp->fr_line,
17297 			    _("unsupported PC relative reference to different section"));
17298 
17299 	      return 1;
17300 	    }
17301 	  if (fragp != sym_frag && sym_frag->fr_address == 0)
17302 	    /* Assume non-extended on the first relaxation pass.
17303 	       The address we have calculated will be bogus if this is
17304 	       a forward branch to another frag, as the forward frag
17305 	       will have fr_address == 0.  */
17306 	    return 0;
17307 	}
17308 
17309       /* In this case, we know for sure that the symbol fragment is in
17310 	 the same section.  If the relax_marker of the symbol fragment
17311 	 differs from the relax_marker of this fragment, we have not
17312 	 yet adjusted the symbol fragment fr_address.  We want to add
17313 	 in STRETCH in order to get a better estimate of the address.
17314 	 This particularly matters because of the shift bits.  */
17315       if (stretch != 0
17316 	  && sym_frag->relax_marker != fragp->relax_marker)
17317 	{
17318 	  fragS *f;
17319 
17320 	  /* Adjust stretch for any alignment frag.  Note that if have
17321              been expanding the earlier code, the symbol may be
17322              defined in what appears to be an earlier frag.  FIXME:
17323              This doesn't handle the fr_subtype field, which specifies
17324              a maximum number of bytes to skip when doing an
17325              alignment.  */
17326 	  for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17327 	    {
17328 	      if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17329 		{
17330 		  if (stretch < 0)
17331 		    stretch = - ((- stretch)
17332 				 & ~ ((1 << (int) f->fr_offset) - 1));
17333 		  else
17334 		    stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17335 		  if (stretch == 0)
17336 		    break;
17337 		}
17338 	    }
17339 	  if (f != NULL)
17340 	    val += stretch;
17341 	}
17342 
17343       addr = fragp->fr_address + fragp->fr_fix;
17344 
17345       /* The base address rules are complicated.  The base address of
17346          a branch is the following instruction.  The base address of a
17347          PC relative load or add is the instruction itself, but if it
17348          is in a delay slot (in which case it can not be extended) use
17349          the address of the instruction whose delay slot it is in.  */
17350       if (type == 'p' || type == 'q')
17351 	{
17352 	  addr += 2;
17353 
17354 	  /* If we are currently assuming that this frag should be
17355 	     extended, then, the current address is two bytes
17356 	     higher.  */
17357 	  if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17358 	    addr += 2;
17359 
17360 	  /* Ignore the low bit in the target, since it will be set
17361              for a text label.  */
17362 	  if ((val & 1) != 0)
17363 	    --val;
17364 	}
17365       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17366 	addr -= 4;
17367       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17368 	addr -= 2;
17369 
17370       val -= addr & ~ ((1 << op->shift) - 1);
17371 
17372       /* Branch offsets have an implicit 0 in the lowest bit.  */
17373       if (type == 'p' || type == 'q')
17374 	val /= 2;
17375 
17376       /* If any of the shifted bits are set, we must use an extended
17377          opcode.  If the address depends on the size of this
17378          instruction, this can lead to a loop, so we arrange to always
17379          use an extended opcode.  We only check this when we are in
17380          the main relaxation loop, when SEC is NULL.  */
17381       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17382 	{
17383 	  fragp->fr_subtype =
17384 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17385 	  return 1;
17386 	}
17387 
17388       /* If we are about to mark a frag as extended because the value
17389          is precisely maxtiny + 1, then there is a chance of an
17390          infinite loop as in the following code:
17391 	     la	$4,foo
17392 	     .skip	1020
17393 	     .align	2
17394 	   foo:
17395 	 In this case when the la is extended, foo is 0x3fc bytes
17396 	 away, so the la can be shrunk, but then foo is 0x400 away, so
17397 	 the la must be extended.  To avoid this loop, we mark the
17398 	 frag as extended if it was small, and is about to become
17399 	 extended with a value of maxtiny + 1.  */
17400       if (val == ((maxtiny + 1) << op->shift)
17401 	  && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17402 	  && sec == NULL)
17403 	{
17404 	  fragp->fr_subtype =
17405 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17406 	  return 1;
17407 	}
17408     }
17409   else if (symsec != absolute_section && sec != NULL)
17410     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17411 
17412   if ((val & ((1 << op->shift) - 1)) != 0
17413       || val < (mintiny << op->shift)
17414       || val > (maxtiny << op->shift))
17415     return 1;
17416   else
17417     return 0;
17418 }
17419 
17420 /* Compute the length of a branch sequence, and adjust the
17421    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17422    worst-case length is computed, with UPDATE being used to indicate
17423    whether an unconditional (-1), branch-likely (+1) or regular (0)
17424    branch is to be computed.  */
17425 static int
17426 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17427 {
17428   bfd_boolean toofar;
17429   int length;
17430 
17431   if (fragp
17432       && S_IS_DEFINED (fragp->fr_symbol)
17433       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17434     {
17435       addressT addr;
17436       offsetT val;
17437 
17438       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17439 
17440       addr = fragp->fr_address + fragp->fr_fix + 4;
17441 
17442       val -= addr;
17443 
17444       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17445     }
17446   else if (fragp)
17447     /* If the symbol is not defined or it's in a different segment,
17448        assume the user knows what's going on and emit a short
17449        branch.  */
17450     toofar = FALSE;
17451   else
17452     toofar = TRUE;
17453 
17454   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17455     fragp->fr_subtype
17456       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17457 			     RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17458 			     RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17459 			     RELAX_BRANCH_LINK (fragp->fr_subtype),
17460 			     toofar);
17461 
17462   length = 4;
17463   if (toofar)
17464     {
17465       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17466 	length += 8;
17467 
17468       if (mips_pic != NO_PIC)
17469 	{
17470 	  /* Additional space for PIC loading of target address.  */
17471 	  length += 8;
17472 	  if (mips_opts.isa == ISA_MIPS1)
17473 	    /* Additional space for $at-stabilizing nop.  */
17474 	    length += 4;
17475 	}
17476 
17477       /* If branch is conditional.  */
17478       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17479 	length += 8;
17480     }
17481 
17482   return length;
17483 }
17484 
17485 /* Compute the length of a branch sequence, and adjust the
17486    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
17487    worst-case length is computed, with UPDATE being used to indicate
17488    whether an unconditional (-1), or regular (0) branch is to be
17489    computed.  */
17490 
17491 static int
17492 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17493 {
17494   bfd_boolean toofar;
17495   int length;
17496 
17497   if (fragp
17498       && S_IS_DEFINED (fragp->fr_symbol)
17499       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17500     {
17501       addressT addr;
17502       offsetT val;
17503 
17504       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17505       /* Ignore the low bit in the target, since it will be set
17506 	 for a text label.  */
17507       if ((val & 1) != 0)
17508 	--val;
17509 
17510       addr = fragp->fr_address + fragp->fr_fix + 4;
17511 
17512       val -= addr;
17513 
17514       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17515     }
17516   else if (fragp)
17517     /* If the symbol is not defined or it's in a different segment,
17518        assume the user knows what's going on and emit a short
17519        branch.  */
17520     toofar = FALSE;
17521   else
17522     toofar = TRUE;
17523 
17524   if (fragp && update
17525       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17526     fragp->fr_subtype = (toofar
17527 			 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17528 			 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17529 
17530   length = 4;
17531   if (toofar)
17532     {
17533       bfd_boolean compact_known = fragp != NULL;
17534       bfd_boolean compact = FALSE;
17535       bfd_boolean uncond;
17536 
17537       if (compact_known)
17538 	compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17539       if (fragp)
17540 	uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17541       else
17542 	uncond = update < 0;
17543 
17544       /* If label is out of range, we turn branch <br>:
17545 
17546 		<br>	label			# 4 bytes
17547 	    0:
17548 
17549          into:
17550 
17551 		j	label			# 4 bytes
17552 		nop				# 2 bytes if compact && !PIC
17553 	    0:
17554        */
17555       if (mips_pic == NO_PIC && (!compact_known || compact))
17556 	length += 2;
17557 
17558       /* If assembling PIC code, we further turn:
17559 
17560 			j	label			# 4 bytes
17561 
17562          into:
17563 
17564 			lw/ld	at, %got(label)(gp)	# 4 bytes
17565 			d/addiu	at, %lo(label)		# 4 bytes
17566 			jr/c	at			# 2 bytes
17567        */
17568       if (mips_pic != NO_PIC)
17569 	length += 6;
17570 
17571       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17572 
17573 			<brneg>	0f			# 4 bytes
17574 			nop				# 2 bytes if !compact
17575        */
17576       if (!uncond)
17577 	length += (compact_known && compact) ? 4 : 6;
17578     }
17579 
17580   return length;
17581 }
17582 
17583 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17584    bit accordingly.  */
17585 
17586 static int
17587 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17588 {
17589   bfd_boolean toofar;
17590 
17591   if (fragp
17592       && S_IS_DEFINED (fragp->fr_symbol)
17593       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17594     {
17595       addressT addr;
17596       offsetT val;
17597       int type;
17598 
17599       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17600       /* Ignore the low bit in the target, since it will be set
17601 	 for a text label.  */
17602       if ((val & 1) != 0)
17603 	--val;
17604 
17605       /* Assume this is a 2-byte branch.  */
17606       addr = fragp->fr_address + fragp->fr_fix + 2;
17607 
17608       /* We try to avoid the infinite loop by not adding 2 more bytes for
17609 	 long branches.  */
17610 
17611       val -= addr;
17612 
17613       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17614       if (type == 'D')
17615 	toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17616       else if (type == 'E')
17617 	toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17618       else
17619 	abort ();
17620     }
17621   else
17622     /* If the symbol is not defined or it's in a different segment,
17623        we emit a normal 32-bit branch.  */
17624     toofar = TRUE;
17625 
17626   if (fragp && update
17627       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17628     fragp->fr_subtype
17629       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17630 	       : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17631 
17632   if (toofar)
17633     return 4;
17634 
17635   return 2;
17636 }
17637 
17638 /* Estimate the size of a frag before relaxing.  Unless this is the
17639    mips16, we are not really relaxing here, and the final size is
17640    encoded in the subtype information.  For the mips16, we have to
17641    decide whether we are using an extended opcode or not.  */
17642 
17643 int
17644 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17645 {
17646   int change;
17647 
17648   if (RELAX_BRANCH_P (fragp->fr_subtype))
17649     {
17650 
17651       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17652 
17653       return fragp->fr_var;
17654     }
17655 
17656   if (RELAX_MIPS16_P (fragp->fr_subtype))
17657     /* We don't want to modify the EXTENDED bit here; it might get us
17658        into infinite loops.  We change it only in mips_relax_frag().  */
17659     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17660 
17661   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17662     {
17663       int length = 4;
17664 
17665       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17666 	length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17667       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17668 	length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17669       fragp->fr_var = length;
17670 
17671       return length;
17672     }
17673 
17674   if (mips_pic == NO_PIC)
17675     change = nopic_need_relax (fragp->fr_symbol, 0);
17676   else if (mips_pic == SVR4_PIC)
17677     change = pic_need_relax (fragp->fr_symbol, segtype);
17678   else if (mips_pic == VXWORKS_PIC)
17679     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17680     change = 0;
17681   else
17682     abort ();
17683 
17684   if (change)
17685     {
17686       fragp->fr_subtype |= RELAX_USE_SECOND;
17687       return -RELAX_FIRST (fragp->fr_subtype);
17688     }
17689   else
17690     return -RELAX_SECOND (fragp->fr_subtype);
17691 }
17692 
17693 /* This is called to see whether a reloc against a defined symbol
17694    should be converted into a reloc against a section.  */
17695 
17696 int
17697 mips_fix_adjustable (fixS *fixp)
17698 {
17699   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17700       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17701     return 0;
17702 
17703   if (fixp->fx_addsy == NULL)
17704     return 1;
17705 
17706   /* If symbol SYM is in a mergeable section, relocations of the form
17707      SYM + 0 can usually be made section-relative.  The mergeable data
17708      is then identified by the section offset rather than by the symbol.
17709 
17710      However, if we're generating REL LO16 relocations, the offset is split
17711      between the LO16 and parterning high part relocation.  The linker will
17712      need to recalculate the complete offset in order to correctly identify
17713      the merge data.
17714 
17715      The linker has traditionally not looked for the parterning high part
17716      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17717      placed anywhere.  Rather than break backwards compatibility by changing
17718      this, it seems better not to force the issue, and instead keep the
17719      original symbol.  This will work with either linker behavior.  */
17720   if ((lo16_reloc_p (fixp->fx_r_type)
17721        || reloc_needs_lo_p (fixp->fx_r_type))
17722       && HAVE_IN_PLACE_ADDENDS
17723       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17724     return 0;
17725 
17726   /* There is no place to store an in-place offset for JALR relocations.
17727      Likewise an in-range offset of PC-relative relocations may overflow
17728      the in-place relocatable field if recalculated against the start
17729      address of the symbol's containing section.  */
17730   if (HAVE_IN_PLACE_ADDENDS
17731       && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
17732     return 0;
17733 
17734 #ifdef OBJ_ELF
17735   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17736      to a floating-point stub.  The same is true for non-R_MIPS16_26
17737      relocations against MIPS16 functions; in this case, the stub becomes
17738      the function's canonical address.
17739 
17740      Floating-point stubs are stored in unique .mips16.call.* or
17741      .mips16.fn.* sections.  If a stub T for function F is in section S,
17742      the first relocation in section S must be against F; this is how the
17743      linker determines the target function.  All relocations that might
17744      resolve to T must also be against F.  We therefore have the following
17745      restrictions, which are given in an intentionally-redundant way:
17746 
17747        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17748 	  symbols.
17749 
17750        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17751 	  if that stub might be used.
17752 
17753        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17754 	  symbols.
17755 
17756        4. We cannot reduce a stub's relocations against MIPS16 symbols if
17757 	  that stub might be used.
17758 
17759      There is a further restriction:
17760 
17761        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17762 	  R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17763 	  targets with in-place addends; the relocation field cannot
17764 	  encode the low bit.
17765 
17766      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17767      against a MIPS16 symbol.  We deal with (5) by by not reducing any
17768      such relocations on REL targets.
17769 
17770      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17771      relocation against some symbol R, no relocation against R may be
17772      reduced.  (Note that this deals with (2) as well as (1) because
17773      relocations against global symbols will never be reduced on ELF
17774      targets.)  This approach is a little simpler than trying to detect
17775      stub sections, and gives the "all or nothing" per-symbol consistency
17776      that we have for MIPS16 symbols.  */
17777   if (IS_ELF
17778       && fixp->fx_subsy == NULL
17779       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17780 	  || *symbol_get_tc (fixp->fx_addsy)
17781 	  || (HAVE_IN_PLACE_ADDENDS
17782 	      && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17783 	      && jmp_reloc_p (fixp->fx_r_type))))
17784     return 0;
17785 #endif
17786 
17787   return 1;
17788 }
17789 
17790 /* Translate internal representation of relocation info to BFD target
17791    format.  */
17792 
17793 arelent **
17794 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17795 {
17796   static arelent *retval[4];
17797   arelent *reloc;
17798   bfd_reloc_code_real_type code;
17799 
17800   memset (retval, 0, sizeof(retval));
17801   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17802   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17803   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17804   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17805 
17806   if (fixp->fx_pcrel)
17807     {
17808       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17809 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17810 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17811 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
17812 
17813       /* At this point, fx_addnumber is "symbol offset - pcrel address".
17814 	 Relocations want only the symbol offset.  */
17815       reloc->addend = fixp->fx_addnumber + reloc->address;
17816       if (!IS_ELF)
17817 	{
17818 	  /* A gruesome hack which is a result of the gruesome gas
17819 	     reloc handling.  What's worse, for COFF (as opposed to
17820 	     ECOFF), we might need yet another copy of reloc->address.
17821 	     See bfd_install_relocation.  */
17822 	  reloc->addend += reloc->address;
17823 	}
17824     }
17825   else
17826     reloc->addend = fixp->fx_addnumber;
17827 
17828   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17829      entry to be used in the relocation's section offset.  */
17830   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17831     {
17832       reloc->address = reloc->addend;
17833       reloc->addend = 0;
17834     }
17835 
17836   code = fixp->fx_r_type;
17837 
17838   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17839   if (reloc->howto == NULL)
17840     {
17841       as_bad_where (fixp->fx_file, fixp->fx_line,
17842 		    _("Can not represent %s relocation in this object file format"),
17843 		    bfd_get_reloc_code_name (code));
17844       retval[0] = NULL;
17845     }
17846 
17847   return retval;
17848 }
17849 
17850 /* Relax a machine dependent frag.  This returns the amount by which
17851    the current size of the frag should change.  */
17852 
17853 int
17854 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17855 {
17856   if (RELAX_BRANCH_P (fragp->fr_subtype))
17857     {
17858       offsetT old_var = fragp->fr_var;
17859 
17860       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17861 
17862       return fragp->fr_var - old_var;
17863     }
17864 
17865   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17866     {
17867       offsetT old_var = fragp->fr_var;
17868       offsetT new_var = 4;
17869 
17870       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17871 	new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17872       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17873 	new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17874       fragp->fr_var = new_var;
17875 
17876       return new_var - old_var;
17877     }
17878 
17879   if (! RELAX_MIPS16_P (fragp->fr_subtype))
17880     return 0;
17881 
17882   if (mips16_extended_frag (fragp, NULL, stretch))
17883     {
17884       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17885 	return 0;
17886       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17887       return 2;
17888     }
17889   else
17890     {
17891       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17892 	return 0;
17893       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17894       return -2;
17895     }
17896 
17897   return 0;
17898 }
17899 
17900 /* Convert a machine dependent frag.  */
17901 
17902 void
17903 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17904 {
17905   if (RELAX_BRANCH_P (fragp->fr_subtype))
17906     {
17907       bfd_byte *buf;
17908       unsigned long insn;
17909       expressionS exp;
17910       fixS *fixp;
17911 
17912       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
17913 
17914       if (target_big_endian)
17915 	insn = bfd_getb32 (buf);
17916       else
17917 	insn = bfd_getl32 (buf);
17918 
17919       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17920 	{
17921 	  /* We generate a fixup instead of applying it right now
17922 	     because, if there are linker relaxations, we're going to
17923 	     need the relocations.  */
17924 	  exp.X_op = O_symbol;
17925 	  exp.X_add_symbol = fragp->fr_symbol;
17926 	  exp.X_add_number = fragp->fr_offset;
17927 
17928 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
17929 			      4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
17930 	  fixp->fx_file = fragp->fr_file;
17931 	  fixp->fx_line = fragp->fr_line;
17932 
17933 	  md_number_to_chars ((char *) buf, insn, 4);
17934 	  buf += 4;
17935 	}
17936       else
17937 	{
17938 	  int i;
17939 
17940 	  as_warn_where (fragp->fr_file, fragp->fr_line,
17941 			 _("Relaxed out-of-range branch into a jump"));
17942 
17943 	  if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17944 	    goto uncond;
17945 
17946 	  if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17947 	    {
17948 	      /* Reverse the branch.  */
17949 	      switch ((insn >> 28) & 0xf)
17950 		{
17951 		case 4:
17952 		  /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
17953 		     have the condition reversed by tweaking a single
17954 		     bit, and their opcodes all have 0x4???????.  */
17955 		  gas_assert ((insn & 0xf1000000) == 0x41000000);
17956 		  insn ^= 0x00010000;
17957 		  break;
17958 
17959 		case 0:
17960 		  /* bltz	0x04000000	bgez	0x04010000
17961 		     bltzal	0x04100000	bgezal	0x04110000  */
17962 		  gas_assert ((insn & 0xfc0e0000) == 0x04000000);
17963 		  insn ^= 0x00010000;
17964 		  break;
17965 
17966 		case 1:
17967 		  /* beq	0x10000000	bne	0x14000000
17968 		     blez	0x18000000	bgtz	0x1c000000  */
17969 		  insn ^= 0x04000000;
17970 		  break;
17971 
17972 		default:
17973 		  abort ();
17974 		}
17975 	    }
17976 
17977 	  if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17978 	    {
17979 	      /* Clear the and-link bit.  */
17980 	      gas_assert ((insn & 0xfc1c0000) == 0x04100000);
17981 
17982 	      /* bltzal		0x04100000	bgezal	0x04110000
17983 		 bltzall	0x04120000	bgezall	0x04130000  */
17984 	      insn &= ~0x00100000;
17985 	    }
17986 
17987 	  /* Branch over the branch (if the branch was likely) or the
17988 	     full jump (not likely case).  Compute the offset from the
17989 	     current instruction to branch to.  */
17990 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17991 	    i = 16;
17992 	  else
17993 	    {
17994 	      /* How many bytes in instructions we've already emitted?  */
17995 	      i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
17996 	      /* How many bytes in instructions from here to the end?  */
17997 	      i = fragp->fr_var - i;
17998 	    }
17999 	  /* Convert to instruction count.  */
18000 	  i >>= 2;
18001 	  /* Branch counts from the next instruction.  */
18002 	  i--;
18003 	  insn |= i;
18004 	  /* Branch over the jump.  */
18005 	  md_number_to_chars ((char *) buf, insn, 4);
18006 	  buf += 4;
18007 
18008 	  /* nop */
18009 	  md_number_to_chars ((char *) buf, 0, 4);
18010 	  buf += 4;
18011 
18012 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18013 	    {
18014 	      /* beql $0, $0, 2f */
18015 	      insn = 0x50000000;
18016 	      /* Compute the PC offset from the current instruction to
18017 		 the end of the variable frag.  */
18018 	      /* How many bytes in instructions we've already emitted?  */
18019 	      i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
18020 	      /* How many bytes in instructions from here to the end?  */
18021 	      i = fragp->fr_var - i;
18022 	      /* Convert to instruction count.  */
18023 	      i >>= 2;
18024 	      /* Don't decrement i, because we want to branch over the
18025 		 delay slot.  */
18026 
18027 	      insn |= i;
18028 	      md_number_to_chars ((char *) buf, insn, 4);
18029 	      buf += 4;
18030 
18031 	      md_number_to_chars ((char *) buf, 0, 4);
18032 	      buf += 4;
18033 	    }
18034 
18035 	uncond:
18036 	  if (mips_pic == NO_PIC)
18037 	    {
18038 	      /* j or jal.  */
18039 	      insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18040 		      ? 0x0c000000 : 0x08000000);
18041 	      exp.X_op = O_symbol;
18042 	      exp.X_add_symbol = fragp->fr_symbol;
18043 	      exp.X_add_number = fragp->fr_offset;
18044 
18045 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
18046 				  4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
18047 	      fixp->fx_file = fragp->fr_file;
18048 	      fixp->fx_line = fragp->fr_line;
18049 
18050 	      md_number_to_chars ((char *) buf, insn, 4);
18051 	      buf += 4;
18052 	    }
18053 	  else
18054 	    {
18055 	      unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18056 
18057 	      /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18058 	      insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18059 	      insn |= at << OP_SH_RT;
18060 	      exp.X_op = O_symbol;
18061 	      exp.X_add_symbol = fragp->fr_symbol;
18062 	      exp.X_add_number = fragp->fr_offset;
18063 
18064 	      if (fragp->fr_offset)
18065 		{
18066 		  exp.X_add_symbol = make_expr_symbol (&exp);
18067 		  exp.X_add_number = 0;
18068 		}
18069 
18070 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
18071 				  4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
18072 	      fixp->fx_file = fragp->fr_file;
18073 	      fixp->fx_line = fragp->fr_line;
18074 
18075 	      md_number_to_chars ((char *) buf, insn, 4);
18076 	      buf += 4;
18077 
18078 	      if (mips_opts.isa == ISA_MIPS1)
18079 		{
18080 		  /* nop */
18081 		  md_number_to_chars ((char *) buf, 0, 4);
18082 		  buf += 4;
18083 		}
18084 
18085 	      /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18086 	      insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18087 	      insn |= at << OP_SH_RS | at << OP_SH_RT;
18088 
18089 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
18090 				  4, &exp, FALSE, BFD_RELOC_LO16);
18091 	      fixp->fx_file = fragp->fr_file;
18092 	      fixp->fx_line = fragp->fr_line;
18093 
18094 	      md_number_to_chars ((char *) buf, insn, 4);
18095 	      buf += 4;
18096 
18097 	      /* j(al)r $at.  */
18098 	      if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18099 		insn = 0x0000f809;
18100 	      else
18101 		insn = 0x00000008;
18102 	      insn |= at << OP_SH_RS;
18103 
18104 	      md_number_to_chars ((char *) buf, insn, 4);
18105 	      buf += 4;
18106 	    }
18107 	}
18108 
18109       gas_assert (buf == (bfd_byte *)fragp->fr_literal
18110 	      + fragp->fr_fix + fragp->fr_var);
18111 
18112       fragp->fr_fix += fragp->fr_var;
18113 
18114       return;
18115     }
18116 
18117   /* Relax microMIPS branches.  */
18118   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18119     {
18120       bfd_byte *buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
18121       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18122       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18123       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18124       bfd_boolean short_ds;
18125       unsigned long insn;
18126       expressionS exp;
18127       fixS *fixp;
18128 
18129       exp.X_op = O_symbol;
18130       exp.X_add_symbol = fragp->fr_symbol;
18131       exp.X_add_number = fragp->fr_offset;
18132 
18133       fragp->fr_fix += fragp->fr_var;
18134 
18135       /* Handle 16-bit branches that fit or are forced to fit.  */
18136       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18137 	{
18138 	  /* We generate a fixup instead of applying it right now,
18139 	     because if there is linker relaxation, we're going to
18140 	     need the relocations.  */
18141 	  if (type == 'D')
18142 	    fixp = fix_new_exp (fragp,
18143 				buf - (bfd_byte *) fragp->fr_literal,
18144 				2, &exp, TRUE,
18145 				BFD_RELOC_MICROMIPS_10_PCREL_S1);
18146 	  else if (type == 'E')
18147 	    fixp = fix_new_exp (fragp,
18148 				buf - (bfd_byte *) fragp->fr_literal,
18149 				2, &exp, TRUE,
18150 				BFD_RELOC_MICROMIPS_7_PCREL_S1);
18151 	  else
18152 	    abort ();
18153 
18154 	  fixp->fx_file = fragp->fr_file;
18155 	  fixp->fx_line = fragp->fr_line;
18156 
18157 	  /* These relocations can have an addend that won't fit in
18158 	     2 octets.  */
18159 	  fixp->fx_no_overflow = 1;
18160 
18161 	  return;
18162 	}
18163 
18164       /* Handle 32-bit branches that fit or are forced to fit.  */
18165       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18166 	  || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18167 	{
18168 	  /* We generate a fixup instead of applying it right now,
18169 	     because if there is linker relaxation, we're going to
18170 	     need the relocations.  */
18171 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18172 			      4, &exp, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18173 	  fixp->fx_file = fragp->fr_file;
18174 	  fixp->fx_line = fragp->fr_line;
18175 
18176 	  if (type == 0)
18177 	    return;
18178 	}
18179 
18180       /* Relax 16-bit branches to 32-bit branches.  */
18181       if (type != 0)
18182 	{
18183 	  if (target_big_endian)
18184 	    insn = bfd_getb16 (buf);
18185 	  else
18186 	    insn = bfd_getl16 (buf);
18187 
18188 	  if ((insn & 0xfc00) == 0xcc00)		/* b16  */
18189 	    insn = 0x94000000;				/* beq  */
18190 	  else if ((insn & 0xdc00) == 0x8c00)		/* beqz16/bnez16  */
18191 	    {
18192 	      unsigned long regno;
18193 
18194 	      regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18195 	      regno = micromips_to_32_reg_d_map [regno];
18196 	      insn = ((insn & 0x2000) << 16) | 0x94000000;	/* beq/bne  */
18197 	      insn |= regno << MICROMIPSOP_SH_RS;
18198 	    }
18199 	  else
18200 	    abort ();
18201 
18202 	  /* Nothing else to do, just write it out.  */
18203 	  if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18204 	      || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18205 	    {
18206 	      md_number_to_chars ((char *) buf, insn >> 16, 2);
18207 	      buf += 2;
18208 	      md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18209 	      buf += 2;
18210 
18211 	      gas_assert (buf == ((bfd_byte *) fragp->fr_literal
18212 				  + fragp->fr_fix));
18213 	      return;
18214 	    }
18215 	}
18216       else
18217 	{
18218 	  unsigned long next;
18219 
18220 	  if (target_big_endian)
18221 	    {
18222 	      insn = bfd_getb16 (buf);
18223 	      next = bfd_getb16 (buf + 2);
18224 	    }
18225 	  else
18226 	    {
18227 	      insn = bfd_getl16 (buf);
18228 	      next = bfd_getl16 (buf + 2);
18229 	    }
18230 	  insn = (insn << 16) | next;
18231 	}
18232 
18233       /* Relax 32-bit branches to a sequence of instructions.  */
18234       as_warn_where (fragp->fr_file, fragp->fr_line,
18235 		     _("Relaxed out-of-range branch into a jump"));
18236 
18237       /* Set the short-delay-slot bit.  */
18238       short_ds = al && (insn & 0x02000000) != 0;
18239 
18240       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18241 	{
18242 	  symbolS *l;
18243 
18244 	  /* Reverse the branch.  */
18245 	  if ((insn & 0xfc000000) == 0x94000000			/* beq  */
18246 	      || (insn & 0xfc000000) == 0xb4000000)		/* bne  */
18247 	    insn ^= 0x20000000;
18248 	  else if ((insn & 0xffe00000) == 0x40000000		/* bltz  */
18249 		   || (insn & 0xffe00000) == 0x40400000		/* bgez  */
18250 		   || (insn & 0xffe00000) == 0x40800000		/* blez  */
18251 		   || (insn & 0xffe00000) == 0x40c00000		/* bgtz  */
18252 		   || (insn & 0xffe00000) == 0x40a00000		/* bnezc  */
18253 		   || (insn & 0xffe00000) == 0x40e00000		/* beqzc  */
18254 		   || (insn & 0xffe00000) == 0x40200000		/* bltzal  */
18255 		   || (insn & 0xffe00000) == 0x40600000		/* bgezal  */
18256 		   || (insn & 0xffe00000) == 0x42200000		/* bltzals  */
18257 		   || (insn & 0xffe00000) == 0x42600000)	/* bgezals  */
18258 	    insn ^= 0x00400000;
18259 	  else if ((insn & 0xffe30000) == 0x43800000		/* bc1f  */
18260 		   || (insn & 0xffe30000) == 0x43a00000		/* bc1t  */
18261 		   || (insn & 0xffe30000) == 0x42800000		/* bc2f  */
18262 		   || (insn & 0xffe30000) == 0x42a00000)	/* bc2t  */
18263 	    insn ^= 0x00200000;
18264 	  else
18265 	    abort ();
18266 
18267 	  if (al)
18268 	    {
18269 	      /* Clear the and-link and short-delay-slot bits.  */
18270 	      gas_assert ((insn & 0xfda00000) == 0x40200000);
18271 
18272 	      /* bltzal  0x40200000	bgezal  0x40600000  */
18273 	      /* bltzals 0x42200000	bgezals 0x42600000  */
18274 	      insn &= ~0x02200000;
18275 	    }
18276 
18277 	  /* Make a label at the end for use with the branch.  */
18278 	  l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18279 	  micromips_label_inc ();
18280 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18281 	  if (IS_ELF)
18282 	    S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18283 #endif
18284 
18285 	  /* Refer to it.  */
18286 	  fixp = fix_new (fragp, buf - (bfd_byte *) fragp->fr_literal,
18287 			  4, l, 0, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18288 	  fixp->fx_file = fragp->fr_file;
18289 	  fixp->fx_line = fragp->fr_line;
18290 
18291 	  /* Branch over the jump.  */
18292 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18293 	  buf += 2;
18294 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18295 	  buf += 2;
18296 
18297 	  if (!compact)
18298 	    {
18299 	      /* nop  */
18300 	      insn = 0x0c00;
18301 	      md_number_to_chars ((char *) buf, insn, 2);
18302 	      buf += 2;
18303 	    }
18304 	}
18305 
18306       if (mips_pic == NO_PIC)
18307 	{
18308 	  unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
18309 
18310 	  /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18311 	  insn = al ? jal : 0xd4000000;
18312 
18313 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18314 			      4, &exp, FALSE, BFD_RELOC_MICROMIPS_JMP);
18315 	  fixp->fx_file = fragp->fr_file;
18316 	  fixp->fx_line = fragp->fr_line;
18317 
18318 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18319 	  buf += 2;
18320 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18321 	  buf += 2;
18322 
18323 	  if (compact)
18324 	    {
18325 	      /* nop  */
18326 	      insn = 0x0c00;
18327 	      md_number_to_chars ((char *) buf, insn, 2);
18328 	      buf += 2;
18329 	    }
18330 	}
18331       else
18332 	{
18333 	  unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18334 	  unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;	/* jalr/s  */
18335 	  unsigned long jr = compact ? 0x45a0 : 0x4580;		/* jr/c  */
18336 
18337 	  /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18338 	  insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18339 	  insn |= at << MICROMIPSOP_SH_RT;
18340 
18341 	  if (exp.X_add_number)
18342 	    {
18343 	      exp.X_add_symbol = make_expr_symbol (&exp);
18344 	      exp.X_add_number = 0;
18345 	    }
18346 
18347 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18348 			      4, &exp, FALSE, BFD_RELOC_MICROMIPS_GOT16);
18349 	  fixp->fx_file = fragp->fr_file;
18350 	  fixp->fx_line = fragp->fr_line;
18351 
18352 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18353 	  buf += 2;
18354 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18355 	  buf += 2;
18356 
18357 	  /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18358 	  insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18359 	  insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18360 
18361 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18362 			      4, &exp, FALSE, BFD_RELOC_MICROMIPS_LO16);
18363 	  fixp->fx_file = fragp->fr_file;
18364 	  fixp->fx_line = fragp->fr_line;
18365 
18366 	  md_number_to_chars ((char *) buf, insn >> 16, 2);
18367 	  buf += 2;
18368 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18369 	  buf += 2;
18370 
18371 	  /* jr/jrc/jalr/jalrs $at  */
18372 	  insn = al ? jalr : jr;
18373 	  insn |= at << MICROMIPSOP_SH_MJ;
18374 
18375 	  md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18376 	  buf += 2;
18377 	}
18378 
18379       gas_assert (buf == (bfd_byte *) fragp->fr_literal + fragp->fr_fix);
18380       return;
18381     }
18382 
18383   if (RELAX_MIPS16_P (fragp->fr_subtype))
18384     {
18385       int type;
18386       const struct mips16_immed_operand *op;
18387       bfd_boolean small, ext;
18388       offsetT val;
18389       bfd_byte *buf;
18390       unsigned long insn;
18391       bfd_boolean use_extend;
18392       unsigned short extend;
18393 
18394       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18395       op = mips16_immed_operands;
18396       while (op->type != type)
18397 	++op;
18398 
18399       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18400 	{
18401 	  small = FALSE;
18402 	  ext = TRUE;
18403 	}
18404       else
18405 	{
18406 	  small = TRUE;
18407 	  ext = FALSE;
18408 	}
18409 
18410       val = resolve_symbol_value (fragp->fr_symbol);
18411       if (op->pcrel)
18412 	{
18413 	  addressT addr;
18414 
18415 	  addr = fragp->fr_address + fragp->fr_fix;
18416 
18417 	  /* The rules for the base address of a PC relative reloc are
18418              complicated; see mips16_extended_frag.  */
18419 	  if (type == 'p' || type == 'q')
18420 	    {
18421 	      addr += 2;
18422 	      if (ext)
18423 		addr += 2;
18424 	      /* Ignore the low bit in the target, since it will be
18425                  set for a text label.  */
18426 	      if ((val & 1) != 0)
18427 		--val;
18428 	    }
18429 	  else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18430 	    addr -= 4;
18431 	  else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18432 	    addr -= 2;
18433 
18434 	  addr &= ~ (addressT) ((1 << op->shift) - 1);
18435 	  val -= addr;
18436 
18437 	  /* Make sure the section winds up with the alignment we have
18438              assumed.  */
18439 	  if (op->shift > 0)
18440 	    record_alignment (asec, op->shift);
18441 	}
18442 
18443       if (ext
18444 	  && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18445 	      || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18446 	as_warn_where (fragp->fr_file, fragp->fr_line,
18447 		       _("extended instruction in delay slot"));
18448 
18449       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
18450 
18451       if (target_big_endian)
18452 	insn = bfd_getb16 (buf);
18453       else
18454 	insn = bfd_getl16 (buf);
18455 
18456       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
18457 		    RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
18458 		    small, ext, &insn, &use_extend, &extend);
18459 
18460       if (use_extend)
18461 	{
18462 	  md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
18463 	  fragp->fr_fix += 2;
18464 	  buf += 2;
18465 	}
18466 
18467       md_number_to_chars ((char *) buf, insn, 2);
18468       fragp->fr_fix += 2;
18469       buf += 2;
18470     }
18471   else
18472     {
18473       relax_substateT subtype = fragp->fr_subtype;
18474       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18475       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18476       int first, second;
18477       fixS *fixp;
18478 
18479       first = RELAX_FIRST (subtype);
18480       second = RELAX_SECOND (subtype);
18481       fixp = (fixS *) fragp->fr_opcode;
18482 
18483       /* If the delay slot chosen does not match the size of the instruction,
18484          then emit a warning.  */
18485       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18486 	   || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18487 	{
18488 	  relax_substateT s;
18489 	  const char *msg;
18490 
18491 	  s = subtype & (RELAX_DELAY_SLOT_16BIT
18492 			 | RELAX_DELAY_SLOT_SIZE_FIRST
18493 			 | RELAX_DELAY_SLOT_SIZE_SECOND);
18494 	  msg = macro_warning (s);
18495 	  if (msg != NULL)
18496 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18497 	  subtype &= ~s;
18498 	}
18499 
18500       /* Possibly emit a warning if we've chosen the longer option.  */
18501       if (use_second == second_longer)
18502 	{
18503 	  relax_substateT s;
18504 	  const char *msg;
18505 
18506 	  s = (subtype
18507 	       & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18508 	  msg = macro_warning (s);
18509 	  if (msg != NULL)
18510 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18511 	  subtype &= ~s;
18512 	}
18513 
18514       /* Go through all the fixups for the first sequence.  Disable them
18515 	 (by marking them as done) if we're going to use the second
18516 	 sequence instead.  */
18517       while (fixp
18518 	     && fixp->fx_frag == fragp
18519 	     && fixp->fx_where < fragp->fr_fix - second)
18520 	{
18521 	  if (subtype & RELAX_USE_SECOND)
18522 	    fixp->fx_done = 1;
18523 	  fixp = fixp->fx_next;
18524 	}
18525 
18526       /* Go through the fixups for the second sequence.  Disable them if
18527 	 we're going to use the first sequence, otherwise adjust their
18528 	 addresses to account for the relaxation.  */
18529       while (fixp && fixp->fx_frag == fragp)
18530 	{
18531 	  if (subtype & RELAX_USE_SECOND)
18532 	    fixp->fx_where -= first;
18533 	  else
18534 	    fixp->fx_done = 1;
18535 	  fixp = fixp->fx_next;
18536 	}
18537 
18538       /* Now modify the frag contents.  */
18539       if (subtype & RELAX_USE_SECOND)
18540 	{
18541 	  char *start;
18542 
18543 	  start = fragp->fr_literal + fragp->fr_fix - first - second;
18544 	  memmove (start, start + first, second);
18545 	  fragp->fr_fix -= first;
18546 	}
18547       else
18548 	fragp->fr_fix -= second;
18549     }
18550 }
18551 
18552 #ifdef OBJ_ELF
18553 
18554 /* This function is called after the relocs have been generated.
18555    We've been storing mips16 text labels as odd.  Here we convert them
18556    back to even for the convenience of the debugger.  */
18557 
18558 void
18559 mips_frob_file_after_relocs (void)
18560 {
18561   asymbol **syms;
18562   unsigned int count, i;
18563 
18564   if (!IS_ELF)
18565     return;
18566 
18567   syms = bfd_get_outsymbols (stdoutput);
18568   count = bfd_get_symcount (stdoutput);
18569   for (i = 0; i < count; i++, syms++)
18570     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18571 	&& ((*syms)->value & 1) != 0)
18572       {
18573 	(*syms)->value &= ~1;
18574 	/* If the symbol has an odd size, it was probably computed
18575 	   incorrectly, so adjust that as well.  */
18576 	if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18577 	  ++elf_symbol (*syms)->internal_elf_sym.st_size;
18578       }
18579 }
18580 
18581 #endif
18582 
18583 /* This function is called whenever a label is defined, including fake
18584    labels instantiated off the dot special symbol.  It is used when
18585    handling branch delays; if a branch has a label, we assume we cannot
18586    move it.  This also bumps the value of the symbol by 1 in compressed
18587    code.  */
18588 
18589 void
18590 mips_record_label (symbolS *sym)
18591 {
18592   segment_info_type *si = seg_info (now_seg);
18593   struct insn_label_list *l;
18594 
18595   if (free_insn_labels == NULL)
18596     l = (struct insn_label_list *) xmalloc (sizeof *l);
18597   else
18598     {
18599       l = free_insn_labels;
18600       free_insn_labels = l->next;
18601     }
18602 
18603   l->label = sym;
18604   l->next = si->label_list;
18605   si->label_list = l;
18606 }
18607 
18608 /* This function is called as tc_frob_label() whenever a label is defined
18609    and adds a DWARF-2 record we only want for true labels.  */
18610 
18611 void
18612 mips_define_label (symbolS *sym)
18613 {
18614   mips_record_label (sym);
18615 #ifdef OBJ_ELF
18616   dwarf2_emit_label (sym);
18617 #endif
18618 }
18619 
18620 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18621 
18622 /* Some special processing for a MIPS ELF file.  */
18623 
18624 void
18625 mips_elf_final_processing (void)
18626 {
18627   /* Write out the register information.  */
18628   if (mips_abi != N64_ABI)
18629     {
18630       Elf32_RegInfo s;
18631 
18632       s.ri_gprmask = mips_gprmask;
18633       s.ri_cprmask[0] = mips_cprmask[0];
18634       s.ri_cprmask[1] = mips_cprmask[1];
18635       s.ri_cprmask[2] = mips_cprmask[2];
18636       s.ri_cprmask[3] = mips_cprmask[3];
18637       /* The gp_value field is set by the MIPS ELF backend.  */
18638 
18639       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18640 				       ((Elf32_External_RegInfo *)
18641 					mips_regmask_frag));
18642     }
18643   else
18644     {
18645       Elf64_Internal_RegInfo s;
18646 
18647       s.ri_gprmask = mips_gprmask;
18648       s.ri_pad = 0;
18649       s.ri_cprmask[0] = mips_cprmask[0];
18650       s.ri_cprmask[1] = mips_cprmask[1];
18651       s.ri_cprmask[2] = mips_cprmask[2];
18652       s.ri_cprmask[3] = mips_cprmask[3];
18653       /* The gp_value field is set by the MIPS ELF backend.  */
18654 
18655       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18656 				       ((Elf64_External_RegInfo *)
18657 					mips_regmask_frag));
18658     }
18659 
18660   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
18661      sort of BFD interface for this.  */
18662   if (mips_any_noreorder)
18663     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18664   if (mips_pic != NO_PIC)
18665     {
18666       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18667       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18668     }
18669   if (mips_abicalls)
18670     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18671 
18672   /* Set MIPS ELF flags for ASEs.  */
18673   /* We may need to define a new flag for DSP ASE, and set this flag when
18674      file_ase_dsp is true.  */
18675   /* Same for DSP R2.  */
18676   /* We may need to define a new flag for MT ASE, and set this flag when
18677      file_ase_mt is true.  */
18678   if (file_ase_mips16)
18679     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18680   if (file_ase_micromips)
18681     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18682 #if 0 /* XXX FIXME */
18683   if (file_ase_mips3d)
18684     elf_elfheader (stdoutput)->e_flags |= ???;
18685 #endif
18686   if (file_ase_mdmx)
18687     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18688 
18689   /* Set the MIPS ELF ABI flags.  */
18690   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18691     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18692   else if (mips_abi == O64_ABI)
18693     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18694   else if (mips_abi == EABI_ABI)
18695     {
18696       if (!file_mips_gp32)
18697 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18698       else
18699 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18700     }
18701   else if (mips_abi == N32_ABI)
18702     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18703 
18704   /* Nothing to do for N64_ABI.  */
18705 
18706   if (mips_32bitmode)
18707     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18708 
18709 #if 0 /* XXX FIXME */
18710   /* 32 bit code with 64 bit FP registers.  */
18711   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18712     elf_elfheader (stdoutput)->e_flags |= ???;
18713 #endif
18714 }
18715 
18716 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18717 
18718 typedef struct proc {
18719   symbolS *func_sym;
18720   symbolS *func_end_sym;
18721   unsigned long reg_mask;
18722   unsigned long reg_offset;
18723   unsigned long fpreg_mask;
18724   unsigned long fpreg_offset;
18725   unsigned long frame_offset;
18726   unsigned long frame_reg;
18727   unsigned long pc_reg;
18728 } procS;
18729 
18730 static procS cur_proc;
18731 static procS *cur_proc_ptr;
18732 static int numprocs;
18733 
18734 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
18735    as "2", and a normal nop as "0".  */
18736 
18737 #define NOP_OPCODE_MIPS		0
18738 #define NOP_OPCODE_MIPS16	1
18739 #define NOP_OPCODE_MICROMIPS	2
18740 
18741 char
18742 mips_nop_opcode (void)
18743 {
18744   if (seg_info (now_seg)->tc_segment_info_data.micromips)
18745     return NOP_OPCODE_MICROMIPS;
18746   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18747     return NOP_OPCODE_MIPS16;
18748   else
18749     return NOP_OPCODE_MIPS;
18750 }
18751 
18752 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
18753    32-bit microMIPS NOPs here (if applicable).  */
18754 
18755 void
18756 mips_handle_align (fragS *fragp)
18757 {
18758   char nop_opcode;
18759   char *p;
18760   int bytes, size, excess;
18761   valueT opcode;
18762 
18763   if (fragp->fr_type != rs_align_code)
18764     return;
18765 
18766   p = fragp->fr_literal + fragp->fr_fix;
18767   nop_opcode = *p;
18768   switch (nop_opcode)
18769     {
18770     case NOP_OPCODE_MICROMIPS:
18771       opcode = micromips_nop32_insn.insn_opcode;
18772       size = 4;
18773       break;
18774     case NOP_OPCODE_MIPS16:
18775       opcode = mips16_nop_insn.insn_opcode;
18776       size = 2;
18777       break;
18778     case NOP_OPCODE_MIPS:
18779     default:
18780       opcode = nop_insn.insn_opcode;
18781       size = 4;
18782       break;
18783     }
18784 
18785   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18786   excess = bytes % size;
18787 
18788   /* Handle the leading part if we're not inserting a whole number of
18789      instructions, and make it the end of the fixed part of the frag.
18790      Try to fit in a short microMIPS NOP if applicable and possible,
18791      and use zeroes otherwise.  */
18792   gas_assert (excess < 4);
18793   fragp->fr_fix += excess;
18794   switch (excess)
18795     {
18796     case 3:
18797       *p++ = '\0';
18798       /* Fall through.  */
18799     case 2:
18800       if (nop_opcode == NOP_OPCODE_MICROMIPS)
18801 	{
18802 	  md_number_to_chars (p, micromips_nop16_insn.insn_opcode, 2);
18803 	  p += 2;
18804 	  break;
18805 	}
18806       *p++ = '\0';
18807       /* Fall through.  */
18808     case 1:
18809       *p++ = '\0';
18810       /* Fall through.  */
18811     case 0:
18812       break;
18813     }
18814 
18815   md_number_to_chars (p, opcode, size);
18816   fragp->fr_var = size;
18817 }
18818 
18819 static void
18820 md_obj_begin (void)
18821 {
18822 }
18823 
18824 static void
18825 md_obj_end (void)
18826 {
18827   /* Check for premature end, nesting errors, etc.  */
18828   if (cur_proc_ptr)
18829     as_warn (_("missing .end at end of assembly"));
18830 }
18831 
18832 static long
18833 get_number (void)
18834 {
18835   int negative = 0;
18836   long val = 0;
18837 
18838   if (*input_line_pointer == '-')
18839     {
18840       ++input_line_pointer;
18841       negative = 1;
18842     }
18843   if (!ISDIGIT (*input_line_pointer))
18844     as_bad (_("expected simple number"));
18845   if (input_line_pointer[0] == '0')
18846     {
18847       if (input_line_pointer[1] == 'x')
18848 	{
18849 	  input_line_pointer += 2;
18850 	  while (ISXDIGIT (*input_line_pointer))
18851 	    {
18852 	      val <<= 4;
18853 	      val |= hex_value (*input_line_pointer++);
18854 	    }
18855 	  return negative ? -val : val;
18856 	}
18857       else
18858 	{
18859 	  ++input_line_pointer;
18860 	  while (ISDIGIT (*input_line_pointer))
18861 	    {
18862 	      val <<= 3;
18863 	      val |= *input_line_pointer++ - '0';
18864 	    }
18865 	  return negative ? -val : val;
18866 	}
18867     }
18868   if (!ISDIGIT (*input_line_pointer))
18869     {
18870       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18871 	      *input_line_pointer, *input_line_pointer);
18872       as_warn (_("invalid number"));
18873       return -1;
18874     }
18875   while (ISDIGIT (*input_line_pointer))
18876     {
18877       val *= 10;
18878       val += *input_line_pointer++ - '0';
18879     }
18880   return negative ? -val : val;
18881 }
18882 
18883 /* The .file directive; just like the usual .file directive, but there
18884    is an initial number which is the ECOFF file index.  In the non-ECOFF
18885    case .file implies DWARF-2.  */
18886 
18887 static void
18888 s_mips_file (int x ATTRIBUTE_UNUSED)
18889 {
18890   static int first_file_directive = 0;
18891 
18892   if (ECOFF_DEBUGGING)
18893     {
18894       get_number ();
18895       s_app_file (0);
18896     }
18897   else
18898     {
18899       char *filename;
18900 
18901       filename = dwarf2_directive_file (0);
18902 
18903       /* Versions of GCC up to 3.1 start files with a ".file"
18904 	 directive even for stabs output.  Make sure that this
18905 	 ".file" is handled.  Note that you need a version of GCC
18906          after 3.1 in order to support DWARF-2 on MIPS.  */
18907       if (filename != NULL && ! first_file_directive)
18908 	{
18909 	  (void) new_logical_line (filename, -1);
18910 	  s_app_file_string (filename, 0);
18911 	}
18912       first_file_directive = 1;
18913     }
18914 }
18915 
18916 /* The .loc directive, implying DWARF-2.  */
18917 
18918 static void
18919 s_mips_loc (int x ATTRIBUTE_UNUSED)
18920 {
18921   if (!ECOFF_DEBUGGING)
18922     dwarf2_directive_loc (0);
18923 }
18924 
18925 /* The .end directive.  */
18926 
18927 static void
18928 s_mips_end (int x ATTRIBUTE_UNUSED)
18929 {
18930   symbolS *p;
18931 
18932   /* Following functions need their own .frame and .cprestore directives.  */
18933   mips_frame_reg_valid = 0;
18934   mips_cprestore_valid = 0;
18935 
18936   if (!is_end_of_line[(unsigned char) *input_line_pointer])
18937     {
18938       p = get_symbol ();
18939       demand_empty_rest_of_line ();
18940     }
18941   else
18942     p = NULL;
18943 
18944   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18945     as_warn (_(".end not in text section"));
18946 
18947   if (!cur_proc_ptr)
18948     {
18949       as_warn (_(".end directive without a preceding .ent directive."));
18950       demand_empty_rest_of_line ();
18951       return;
18952     }
18953 
18954   if (p != NULL)
18955     {
18956       gas_assert (S_GET_NAME (p));
18957       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18958 	as_warn (_(".end symbol does not match .ent symbol."));
18959 
18960       if (debug_type == DEBUG_STABS)
18961 	stabs_generate_asm_endfunc (S_GET_NAME (p),
18962 				    S_GET_NAME (p));
18963     }
18964   else
18965     as_warn (_(".end directive missing or unknown symbol"));
18966 
18967 #ifdef OBJ_ELF
18968   /* Create an expression to calculate the size of the function.  */
18969   if (p && cur_proc_ptr)
18970     {
18971       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18972       expressionS *exp = xmalloc (sizeof (expressionS));
18973 
18974       obj->size = exp;
18975       exp->X_op = O_subtract;
18976       exp->X_add_symbol = symbol_temp_new_now ();
18977       exp->X_op_symbol = p;
18978       exp->X_add_number = 0;
18979 
18980       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18981     }
18982 
18983   /* Generate a .pdr section.  */
18984   if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
18985     {
18986       segT saved_seg = now_seg;
18987       subsegT saved_subseg = now_subseg;
18988       expressionS exp;
18989       char *fragp;
18990 
18991 #ifdef md_flush_pending_output
18992       md_flush_pending_output ();
18993 #endif
18994 
18995       gas_assert (pdr_seg);
18996       subseg_set (pdr_seg, 0);
18997 
18998       /* Write the symbol.  */
18999       exp.X_op = O_symbol;
19000       exp.X_add_symbol = p;
19001       exp.X_add_number = 0;
19002       emit_expr (&exp, 4);
19003 
19004       fragp = frag_more (7 * 4);
19005 
19006       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19007       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19008       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19009       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19010       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19011       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19012       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19013 
19014       subseg_set (saved_seg, saved_subseg);
19015     }
19016 #endif /* OBJ_ELF */
19017 
19018   cur_proc_ptr = NULL;
19019 }
19020 
19021 /* The .aent and .ent directives.  */
19022 
19023 static void
19024 s_mips_ent (int aent)
19025 {
19026   symbolS *symbolP;
19027 
19028   symbolP = get_symbol ();
19029   if (*input_line_pointer == ',')
19030     ++input_line_pointer;
19031   SKIP_WHITESPACE ();
19032   if (ISDIGIT (*input_line_pointer)
19033       || *input_line_pointer == '-')
19034     get_number ();
19035 
19036   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19037     as_warn (_(".ent or .aent not in text section."));
19038 
19039   if (!aent && cur_proc_ptr)
19040     as_warn (_("missing .end"));
19041 
19042   if (!aent)
19043     {
19044       /* This function needs its own .frame and .cprestore directives.  */
19045       mips_frame_reg_valid = 0;
19046       mips_cprestore_valid = 0;
19047 
19048       cur_proc_ptr = &cur_proc;
19049       memset (cur_proc_ptr, '\0', sizeof (procS));
19050 
19051       cur_proc_ptr->func_sym = symbolP;
19052 
19053       ++numprocs;
19054 
19055       if (debug_type == DEBUG_STABS)
19056         stabs_generate_asm_func (S_GET_NAME (symbolP),
19057 				 S_GET_NAME (symbolP));
19058     }
19059 
19060   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19061 
19062   demand_empty_rest_of_line ();
19063 }
19064 
19065 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19066    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19067    s_mips_frame is used so that we can set the PDR information correctly.
19068    We can't use the ecoff routines because they make reference to the ecoff
19069    symbol table (in the mdebug section).  */
19070 
19071 static void
19072 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19073 {
19074 #ifdef OBJ_ELF
19075   if (IS_ELF && !ECOFF_DEBUGGING)
19076     {
19077       long val;
19078 
19079       if (cur_proc_ptr == (procS *) NULL)
19080 	{
19081 	  as_warn (_(".frame outside of .ent"));
19082 	  demand_empty_rest_of_line ();
19083 	  return;
19084 	}
19085 
19086       cur_proc_ptr->frame_reg = tc_get_register (1);
19087 
19088       SKIP_WHITESPACE ();
19089       if (*input_line_pointer++ != ','
19090 	  || get_absolute_expression_and_terminator (&val) != ',')
19091 	{
19092 	  as_warn (_("Bad .frame directive"));
19093 	  --input_line_pointer;
19094 	  demand_empty_rest_of_line ();
19095 	  return;
19096 	}
19097 
19098       cur_proc_ptr->frame_offset = val;
19099       cur_proc_ptr->pc_reg = tc_get_register (0);
19100 
19101       demand_empty_rest_of_line ();
19102     }
19103   else
19104 #endif /* OBJ_ELF */
19105     s_ignore (ignore);
19106 }
19107 
19108 /* The .fmask and .mask directives. If the mdebug section is present
19109    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19110    embedded targets, s_mips_mask is used so that we can set the PDR
19111    information correctly. We can't use the ecoff routines because they
19112    make reference to the ecoff symbol table (in the mdebug section).  */
19113 
19114 static void
19115 s_mips_mask (int reg_type)
19116 {
19117 #ifdef OBJ_ELF
19118   if (IS_ELF && !ECOFF_DEBUGGING)
19119     {
19120       long mask, off;
19121 
19122       if (cur_proc_ptr == (procS *) NULL)
19123 	{
19124 	  as_warn (_(".mask/.fmask outside of .ent"));
19125 	  demand_empty_rest_of_line ();
19126 	  return;
19127 	}
19128 
19129       if (get_absolute_expression_and_terminator (&mask) != ',')
19130 	{
19131 	  as_warn (_("Bad .mask/.fmask directive"));
19132 	  --input_line_pointer;
19133 	  demand_empty_rest_of_line ();
19134 	  return;
19135 	}
19136 
19137       off = get_absolute_expression ();
19138 
19139       if (reg_type == 'F')
19140 	{
19141 	  cur_proc_ptr->fpreg_mask = mask;
19142 	  cur_proc_ptr->fpreg_offset = off;
19143 	}
19144       else
19145 	{
19146 	  cur_proc_ptr->reg_mask = mask;
19147 	  cur_proc_ptr->reg_offset = off;
19148 	}
19149 
19150       demand_empty_rest_of_line ();
19151     }
19152   else
19153 #endif /* OBJ_ELF */
19154     s_ignore (reg_type);
19155 }
19156 
19157 /* A table describing all the processors gas knows about.  Names are
19158    matched in the order listed.
19159 
19160    To ease comparison, please keep this table in the same order as
19161    gcc's mips_cpu_info_table[].  */
19162 static const struct mips_cpu_info mips_cpu_info_table[] =
19163 {
19164   /* Entries for generic ISAs */
19165   { "mips1",          MIPS_CPU_IS_ISA,		ISA_MIPS1,      CPU_R3000 },
19166   { "mips2",          MIPS_CPU_IS_ISA,		ISA_MIPS2,      CPU_R6000 },
19167   { "mips3",          MIPS_CPU_IS_ISA,		ISA_MIPS3,      CPU_R4000 },
19168   { "mips4",          MIPS_CPU_IS_ISA,		ISA_MIPS4,      CPU_R8000 },
19169   { "mips5",          MIPS_CPU_IS_ISA,		ISA_MIPS5,      CPU_MIPS5 },
19170   { "mips32",         MIPS_CPU_IS_ISA,		ISA_MIPS32,     CPU_MIPS32 },
19171   { "mips32r2",       MIPS_CPU_IS_ISA,		ISA_MIPS32R2,   CPU_MIPS32R2 },
19172   { "mips64",         MIPS_CPU_IS_ISA,		ISA_MIPS64,     CPU_MIPS64 },
19173   { "mips64r2",       MIPS_CPU_IS_ISA,		ISA_MIPS64R2,   CPU_MIPS64R2 },
19174 
19175   /* MIPS I */
19176   { "r3000",          0,			ISA_MIPS1,      CPU_R3000 },
19177   { "r2000",          0,			ISA_MIPS1,      CPU_R3000 },
19178   { "r3900",          0,			ISA_MIPS1,      CPU_R3900 },
19179 
19180   /* MIPS II */
19181   { "r6000",          0,			ISA_MIPS2,      CPU_R6000 },
19182 
19183   /* MIPS III */
19184   { "r4000",          0,			ISA_MIPS3,      CPU_R4000 },
19185   { "r4010",          0,			ISA_MIPS2,      CPU_R4010 },
19186   { "vr4100",         0,			ISA_MIPS3,      CPU_VR4100 },
19187   { "vr4111",         0,			ISA_MIPS3,      CPU_R4111 },
19188   { "vr4120",         0,			ISA_MIPS3,      CPU_VR4120 },
19189   { "vr4130",         0,			ISA_MIPS3,      CPU_VR4120 },
19190   { "vr4181",         0,			ISA_MIPS3,      CPU_R4111 },
19191   { "vr4300",         0,			ISA_MIPS3,      CPU_R4300 },
19192   { "r4400",          0,			ISA_MIPS3,      CPU_R4400 },
19193   { "r4600",          0,			ISA_MIPS3,      CPU_R4600 },
19194   { "orion",          0,			ISA_MIPS3,      CPU_R4600 },
19195   { "r4650",          0,			ISA_MIPS3,      CPU_R4650 },
19196   /* ST Microelectronics Loongson 2E and 2F cores */
19197   { "loongson2e",     0,			ISA_MIPS3,   CPU_LOONGSON_2E },
19198   { "loongson2f",     0,			ISA_MIPS3,   CPU_LOONGSON_2F },
19199 
19200   /* MIPS IV */
19201   { "r8000",          0,			ISA_MIPS4,      CPU_R8000 },
19202   { "r10000",         0,			ISA_MIPS4,      CPU_R10000 },
19203   { "r12000",         0,			ISA_MIPS4,      CPU_R12000 },
19204   { "r14000",         0,			ISA_MIPS4,      CPU_R14000 },
19205   { "r16000",         0,			ISA_MIPS4,      CPU_R16000 },
19206   { "vr5000",         0,			ISA_MIPS4,      CPU_R5000 },
19207   { "vr5400",         0,			ISA_MIPS4,      CPU_VR5400 },
19208   { "vr5500",         0,			ISA_MIPS4,      CPU_VR5500 },
19209   { "rm5200",         0,			ISA_MIPS4,      CPU_R5000 },
19210   { "rm5230",         0,			ISA_MIPS4,      CPU_R5000 },
19211   { "rm5231",         0,			ISA_MIPS4,      CPU_R5000 },
19212   { "rm5261",         0,			ISA_MIPS4,      CPU_R5000 },
19213   { "rm5721",         0,			ISA_MIPS4,      CPU_R5000 },
19214   { "rm7000",         0,			ISA_MIPS4,      CPU_RM7000 },
19215   { "rm9000",         0,			ISA_MIPS4,      CPU_RM9000 },
19216 
19217   /* MIPS 32 */
19218   { "4kc",            0,			ISA_MIPS32,	CPU_MIPS32 },
19219   { "4km",            0,			ISA_MIPS32,	CPU_MIPS32 },
19220   { "4kp",            0,			ISA_MIPS32,	CPU_MIPS32 },
19221   { "4ksc",           MIPS_CPU_ASE_SMARTMIPS,	ISA_MIPS32,	CPU_MIPS32 },
19222 
19223   /* MIPS 32 Release 2 */
19224   { "4kec",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19225   { "4kem",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19226   { "4kep",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19227   { "4ksd",           MIPS_CPU_ASE_SMARTMIPS,	ISA_MIPS32R2,   CPU_MIPS32R2 },
19228   { "m4k",            0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19229   { "m4kp",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19230   { "m14k",           MIPS_CPU_ASE_MCU,		ISA_MIPS32R2,   CPU_MIPS32R2 },
19231   { "m14kc",          MIPS_CPU_ASE_MCU,		ISA_MIPS32R2,   CPU_MIPS32R2 },
19232   { "m14ke",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19233 						ISA_MIPS32R2,   CPU_MIPS32R2 },
19234   { "m14kec",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19235 						ISA_MIPS32R2,   CPU_MIPS32R2 },
19236   { "24kc",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19237   { "24kf2_1",        0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19238   { "24kf",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19239   { "24kf1_1",        0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19240   /* Deprecated forms of the above.  */
19241   { "24kfx",          0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19242   { "24kx",           0,			ISA_MIPS32R2,   CPU_MIPS32R2 },
19243   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
19244   { "24kec",          MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19245   { "24kef2_1",       MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19246   { "24kef",          MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19247   { "24kef1_1",       MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19248   /* Deprecated forms of the above.  */
19249   { "24kefx",         MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19250   { "24kex",          MIPS_CPU_ASE_DSP,		ISA_MIPS32R2,	CPU_MIPS32R2 },
19251   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
19252   { "34kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19253 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19254   { "34kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19255 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19256   { "34kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19257 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19258   { "34kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19259 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19260   /* Deprecated forms of the above.  */
19261   { "34kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19262 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19263   { "34kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19264 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19265   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
19266   { "74kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19267 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19268   { "74kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19269 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19270   { "74kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19271 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19272   { "74kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19273 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19274   { "74kf3_2",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19275 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19276   /* Deprecated forms of the above.  */
19277   { "74kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19278 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19279   { "74kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19280 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19281   /* 1004K cores are multiprocessor versions of the 34K.  */
19282   { "1004kc",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19283 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19284   { "1004kf2_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19285 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19286   { "1004kf",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19287 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19288   { "1004kf1_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19289 						ISA_MIPS32R2,	CPU_MIPS32R2 },
19290 
19291   /* MIPS 64 */
19292   { "5kc",            0,			ISA_MIPS64,	CPU_MIPS64 },
19293   { "5kf",            0,			ISA_MIPS64,	CPU_MIPS64 },
19294   { "20kc",           MIPS_CPU_ASE_MIPS3D,	ISA_MIPS64,	CPU_MIPS64 },
19295   { "25kf",           MIPS_CPU_ASE_MIPS3D,	ISA_MIPS64,     CPU_MIPS64 },
19296 
19297   /* Broadcom SB-1 CPU core */
19298   { "sb1",            MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19299 						ISA_MIPS64,	CPU_SB1 },
19300   /* Broadcom SB-1A CPU core */
19301   { "sb1a",           MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19302 						ISA_MIPS64,	CPU_SB1 },
19303 
19304   { "loongson3a",     0,			ISA_MIPS64,	CPU_LOONGSON_3A },
19305 
19306   /* MIPS 64 Release 2 */
19307 
19308   /* Cavium Networks Octeon CPU core */
19309   { "octeon",	      0,      ISA_MIPS64R2,   CPU_OCTEON },
19310   { "octeon+",	      0,      ISA_MIPS64R2,   CPU_OCTEONP },
19311   { "octeon2",	      0,      ISA_MIPS64R2,   CPU_OCTEON2 },
19312 
19313   /* RMI Xlr */
19314   { "xlr",	      0,      ISA_MIPS64,     CPU_XLR },
19315 
19316   /* Broadcom XLP.
19317      XLP is mostly like XLR, with the prominent exception that it is
19318      MIPS64R2 rather than MIPS64.  */
19319   { "xlp",	      0,      ISA_MIPS64R2,     CPU_XLR },
19320 
19321   /* End marker */
19322   { NULL, 0, 0, 0 }
19323 };
19324 
19325 
19326 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19327    with a final "000" replaced by "k".  Ignore case.
19328 
19329    Note: this function is shared between GCC and GAS.  */
19330 
19331 static bfd_boolean
19332 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19333 {
19334   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19335     given++, canonical++;
19336 
19337   return ((*given == 0 && *canonical == 0)
19338 	  || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19339 }
19340 
19341 
19342 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19343    CPU name.  We've traditionally allowed a lot of variation here.
19344 
19345    Note: this function is shared between GCC and GAS.  */
19346 
19347 static bfd_boolean
19348 mips_matching_cpu_name_p (const char *canonical, const char *given)
19349 {
19350   /* First see if the name matches exactly, or with a final "000"
19351      turned into "k".  */
19352   if (mips_strict_matching_cpu_name_p (canonical, given))
19353     return TRUE;
19354 
19355   /* If not, try comparing based on numerical designation alone.
19356      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
19357   if (TOLOWER (*given) == 'r')
19358     given++;
19359   if (!ISDIGIT (*given))
19360     return FALSE;
19361 
19362   /* Skip over some well-known prefixes in the canonical name,
19363      hoping to find a number there too.  */
19364   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19365     canonical += 2;
19366   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19367     canonical += 2;
19368   else if (TOLOWER (canonical[0]) == 'r')
19369     canonical += 1;
19370 
19371   return mips_strict_matching_cpu_name_p (canonical, given);
19372 }
19373 
19374 
19375 /* Parse an option that takes the name of a processor as its argument.
19376    OPTION is the name of the option and CPU_STRING is the argument.
19377    Return the corresponding processor enumeration if the CPU_STRING is
19378    recognized, otherwise report an error and return null.
19379 
19380    A similar function exists in GCC.  */
19381 
19382 static const struct mips_cpu_info *
19383 mips_parse_cpu (const char *option, const char *cpu_string)
19384 {
19385   const struct mips_cpu_info *p;
19386 
19387   /* 'from-abi' selects the most compatible architecture for the given
19388      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
19389      EABIs, we have to decide whether we're using the 32-bit or 64-bit
19390      version.  Look first at the -mgp options, if given, otherwise base
19391      the choice on MIPS_DEFAULT_64BIT.
19392 
19393      Treat NO_ABI like the EABIs.  One reason to do this is that the
19394      plain 'mips' and 'mips64' configs have 'from-abi' as their default
19395      architecture.  This code picks MIPS I for 'mips' and MIPS III for
19396      'mips64', just as we did in the days before 'from-abi'.  */
19397   if (strcasecmp (cpu_string, "from-abi") == 0)
19398     {
19399       if (ABI_NEEDS_32BIT_REGS (mips_abi))
19400 	return mips_cpu_info_from_isa (ISA_MIPS1);
19401 
19402       if (ABI_NEEDS_64BIT_REGS (mips_abi))
19403 	return mips_cpu_info_from_isa (ISA_MIPS3);
19404 
19405       if (file_mips_gp32 >= 0)
19406 	return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19407 
19408       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19409 				     ? ISA_MIPS3
19410 				     : ISA_MIPS1);
19411     }
19412 
19413   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
19414   if (strcasecmp (cpu_string, "default") == 0)
19415     return 0;
19416 
19417   for (p = mips_cpu_info_table; p->name != 0; p++)
19418     if (mips_matching_cpu_name_p (p->name, cpu_string))
19419       return p;
19420 
19421   as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19422   return 0;
19423 }
19424 
19425 /* Return the canonical processor information for ISA (a member of the
19426    ISA_MIPS* enumeration).  */
19427 
19428 static const struct mips_cpu_info *
19429 mips_cpu_info_from_isa (int isa)
19430 {
19431   int i;
19432 
19433   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19434     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19435 	&& isa == mips_cpu_info_table[i].isa)
19436       return (&mips_cpu_info_table[i]);
19437 
19438   return NULL;
19439 }
19440 
19441 static const struct mips_cpu_info *
19442 mips_cpu_info_from_arch (int arch)
19443 {
19444   int i;
19445 
19446   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19447     if (arch == mips_cpu_info_table[i].cpu)
19448       return (&mips_cpu_info_table[i]);
19449 
19450   return NULL;
19451 }
19452 
19453 static void
19454 show (FILE *stream, const char *string, int *col_p, int *first_p)
19455 {
19456   if (*first_p)
19457     {
19458       fprintf (stream, "%24s", "");
19459       *col_p = 24;
19460     }
19461   else
19462     {
19463       fprintf (stream, ", ");
19464       *col_p += 2;
19465     }
19466 
19467   if (*col_p + strlen (string) > 72)
19468     {
19469       fprintf (stream, "\n%24s", "");
19470       *col_p = 24;
19471     }
19472 
19473   fprintf (stream, "%s", string);
19474   *col_p += strlen (string);
19475 
19476   *first_p = 0;
19477 }
19478 
19479 void
19480 md_show_usage (FILE *stream)
19481 {
19482   int column, first;
19483   size_t i;
19484 
19485   fprintf (stream, _("\
19486 MIPS options:\n\
19487 -EB			generate big endian output\n\
19488 -EL			generate little endian output\n\
19489 -g, -g2			do not remove unneeded NOPs or swap branches\n\
19490 -G NUM			allow referencing objects up to NUM bytes\n\
19491 			implicitly with the gp register [default 8]\n"));
19492   fprintf (stream, _("\
19493 -mips1			generate MIPS ISA I instructions\n\
19494 -mips2			generate MIPS ISA II instructions\n\
19495 -mips3			generate MIPS ISA III instructions\n\
19496 -mips4			generate MIPS ISA IV instructions\n\
19497 -mips5                  generate MIPS ISA V instructions\n\
19498 -mips32                 generate MIPS32 ISA instructions\n\
19499 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
19500 -mips64                 generate MIPS64 ISA instructions\n\
19501 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
19502 -march=CPU/-mtune=CPU	generate code/schedule for CPU, where CPU is one of:\n"));
19503 
19504   first = 1;
19505 
19506   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19507     show (stream, mips_cpu_info_table[i].name, &column, &first);
19508   show (stream, "from-abi", &column, &first);
19509   fputc ('\n', stream);
19510 
19511   fprintf (stream, _("\
19512 -mCPU			equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19513 -no-mCPU		don't generate code specific to CPU.\n\
19514 			For -mCPU and -no-mCPU, CPU must be one of:\n"));
19515 
19516   first = 1;
19517 
19518   show (stream, "3900", &column, &first);
19519   show (stream, "4010", &column, &first);
19520   show (stream, "4100", &column, &first);
19521   show (stream, "4650", &column, &first);
19522   fputc ('\n', stream);
19523 
19524   fprintf (stream, _("\
19525 -mips16			generate mips16 instructions\n\
19526 -no-mips16		do not generate mips16 instructions\n"));
19527   fprintf (stream, _("\
19528 -mmicromips		generate microMIPS instructions\n\
19529 -mno-micromips		do not generate microMIPS instructions\n"));
19530   fprintf (stream, _("\
19531 -msmartmips		generate smartmips instructions\n\
19532 -mno-smartmips		do not generate smartmips instructions\n"));
19533   fprintf (stream, _("\
19534 -mdsp			generate DSP instructions\n\
19535 -mno-dsp		do not generate DSP instructions\n"));
19536   fprintf (stream, _("\
19537 -mdspr2			generate DSP R2 instructions\n\
19538 -mno-dspr2		do not generate DSP R2 instructions\n"));
19539   fprintf (stream, _("\
19540 -mmt			generate MT instructions\n\
19541 -mno-mt			do not generate MT instructions\n"));
19542   fprintf (stream, _("\
19543 -mmcu			generate MCU instructions\n\
19544 -mno-mcu		do not generate MCU instructions\n"));
19545   fprintf (stream, _("\
19546 -mfix-loongson2f-jump	work around Loongson2F JUMP instructions\n\
19547 -mfix-loongson2f-nop	work around Loongson2F NOP errata\n\
19548 -mfix-loongson2f-btb	work around Loongson2F BTB errata\n\
19549 -mfix-vr4120		work around certain VR4120 errata\n\
19550 -mfix-vr4130		work around VR4130 mflo/mfhi errata\n\
19551 -mfix-24k		insert a nop after ERET and DERET instructions\n\
19552 -mfix-cn63xxp1		work around CN63XXP1 PREF errata\n\
19553 -mgp32			use 32-bit GPRs, regardless of the chosen ISA\n\
19554 -mfp32			use 32-bit FPRs, regardless of the chosen ISA\n\
19555 -msym32			assume all symbols have 32-bit values\n\
19556 -O0			remove unneeded NOPs, do not swap branches\n\
19557 -O			remove unneeded NOPs and swap branches\n\
19558 --trap, --no-break	trap exception on div by 0 and mult overflow\n\
19559 --break, --no-trap	break exception on div by 0 and mult overflow\n"));
19560   fprintf (stream, _("\
19561 -mhard-float		allow floating-point instructions\n\
19562 -msoft-float		do not allow floating-point instructions\n\
19563 -msingle-float		only allow 32-bit floating-point operations\n\
19564 -mdouble-float		allow 32-bit and 64-bit floating-point operations\n\
19565 --[no-]construct-floats [dis]allow floating point values to be constructed\n"
19566 		     ));
19567 #ifdef OBJ_ELF
19568   fprintf (stream, _("\
19569 -KPIC, -call_shared	generate SVR4 position independent code\n\
19570 -call_nonpic		generate non-PIC code that can operate with DSOs\n\
19571 -mvxworks-pic		generate VxWorks position independent code\n\
19572 -non_shared		do not generate code that can operate with DSOs\n\
19573 -xgot			assume a 32 bit GOT\n\
19574 -mpdr, -mno-pdr		enable/disable creation of .pdr sections\n\
19575 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
19576                         position dependent (non shared) code\n\
19577 -mabi=ABI		create ABI conformant object file for:\n"));
19578 
19579   first = 1;
19580 
19581   show (stream, "32", &column, &first);
19582   show (stream, "o64", &column, &first);
19583   show (stream, "n32", &column, &first);
19584   show (stream, "64", &column, &first);
19585   show (stream, "eabi", &column, &first);
19586 
19587   fputc ('\n', stream);
19588 
19589   fprintf (stream, _("\
19590 -32			create o32 ABI object file (default)\n\
19591 -n32			create n32 ABI object file\n\
19592 -64			create 64 ABI object file\n"));
19593 #endif
19594 }
19595 
19596 #ifdef TE_IRIX
19597 enum dwarf2_format
19598 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19599 {
19600   if (HAVE_64BIT_SYMBOLS)
19601     return dwarf2_format_64bit_irix;
19602   else
19603     return dwarf2_format_32bit;
19604 }
19605 #endif
19606 
19607 int
19608 mips_dwarf2_addr_size (void)
19609 {
19610   if (HAVE_64BIT_OBJECTS)
19611     return 8;
19612   else
19613     return 4;
19614 }
19615 
19616 /* Standard calling conventions leave the CFA at SP on entry.  */
19617 void
19618 mips_cfi_frame_initial_instructions (void)
19619 {
19620   cfi_add_CFA_def_cfa_register (SP);
19621 }
19622 
19623 int
19624 tc_mips_regname_to_dw2regnum (char *regname)
19625 {
19626   unsigned int regnum = -1;
19627   unsigned int reg;
19628 
19629   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19630     regnum = reg;
19631 
19632   return regnum;
19633 }
19634