xref: /netbsd-src/external/gpl3/binutils.old/dist/gas/config/tc-mips.c (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright (C) 1993-2018 Free Software Foundation, Inc.
3    Contributed by the OSF and Ralph Campbell.
4    Written by Keith Knowles and Ralph Campbell, working independently.
5    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6    Support.
7 
8    This file is part of GAS.
9 
10    GAS is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3, or (at your option)
13    any later version.
14 
15    GAS is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with GAS; see the file COPYING.  If not, write to the Free
22    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23    02110-1301, USA.  */
24 
25 #include "as.h"
26 #include "config.h"
27 #include "subsegs.h"
28 #include "safe-ctype.h"
29 
30 #include "opcode/mips.h"
31 #include "itbl-ops.h"
32 #include "dwarf2dbg.h"
33 #include "dw2gencfi.h"
34 
35 /* Check assumptions made in this file.  */
36 typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37 typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38 
39 #ifdef DEBUG
40 #define DBG(x) printf x
41 #else
42 #define DBG(x)
43 #endif
44 
45 #define streq(a, b)           (strcmp (a, b) == 0)
46 
47 #define SKIP_SPACE_TABS(S) \
48   do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49 
50 /* Clean up namespace so we can include obj-elf.h too.  */
51 static int mips_output_flavor (void);
52 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
53 #undef OBJ_PROCESS_STAB
54 #undef OUTPUT_FLAVOR
55 #undef S_GET_ALIGN
56 #undef S_GET_SIZE
57 #undef S_SET_ALIGN
58 #undef S_SET_SIZE
59 #undef obj_frob_file
60 #undef obj_frob_file_after_relocs
61 #undef obj_frob_symbol
62 #undef obj_pop_insert
63 #undef obj_sec_sym_ok_for_reloc
64 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
65 
66 #include "obj-elf.h"
67 /* Fix any of them that we actually care about.  */
68 #undef OUTPUT_FLAVOR
69 #define OUTPUT_FLAVOR mips_output_flavor()
70 
71 #include "elf/mips.h"
72 
73 #ifndef ECOFF_DEBUGGING
74 #define NO_ECOFF_DEBUGGING
75 #define ECOFF_DEBUGGING 0
76 #endif
77 
78 int mips_flag_mdebug = -1;
79 
80 /* Control generation of .pdr sections.  Off by default on IRIX: the native
81    linker doesn't know about and discards them, but relocations against them
82    remain, leading to rld crashes.  */
83 #ifdef TE_IRIX
84 int mips_flag_pdr = FALSE;
85 #else
86 int mips_flag_pdr = TRUE;
87 #endif
88 
89 #include "ecoff.h"
90 
91 static char *mips_regmask_frag;
92 static char *mips_flags_frag;
93 
94 #define ZERO 0
95 #define ATREG 1
96 #define S0  16
97 #define S7  23
98 #define TREG 24
99 #define PIC_CALL_REG 25
100 #define KT0 26
101 #define KT1 27
102 #define GP  28
103 #define SP  29
104 #define FP  30
105 #define RA  31
106 
107 #define ILLEGAL_REG (32)
108 
109 #define AT  mips_opts.at
110 
111 extern int target_big_endian;
112 
113 /* The name of the readonly data section.  */
114 #define RDATA_SECTION_NAME ".rodata"
115 
116 /* Ways in which an instruction can be "appended" to the output.  */
117 enum append_method {
118   /* Just add it normally.  */
119   APPEND_ADD,
120 
121   /* Add it normally and then add a nop.  */
122   APPEND_ADD_WITH_NOP,
123 
124   /* Turn an instruction with a delay slot into a "compact" version.  */
125   APPEND_ADD_COMPACT,
126 
127   /* Insert the instruction before the last one.  */
128   APPEND_SWAP
129 };
130 
131 /* Information about an instruction, including its format, operands
132    and fixups.  */
133 struct mips_cl_insn
134 {
135   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
136   const struct mips_opcode *insn_mo;
137 
138   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
139      a copy of INSN_MO->match with the operands filled in.  If we have
140      decided to use an extended MIPS16 instruction, this includes the
141      extension.  */
142   unsigned long insn_opcode;
143 
144   /* The frag that contains the instruction.  */
145   struct frag *frag;
146 
147   /* The offset into FRAG of the first instruction byte.  */
148   long where;
149 
150   /* The relocs associated with the instruction, if any.  */
151   fixS *fixp[3];
152 
153   /* True if this entry cannot be moved from its current position.  */
154   unsigned int fixed_p : 1;
155 
156   /* True if this instruction occurred in a .set noreorder block.  */
157   unsigned int noreorder_p : 1;
158 
159   /* True for mips16 instructions that jump to an absolute address.  */
160   unsigned int mips16_absolute_jump_p : 1;
161 
162   /* True if this instruction is complete.  */
163   unsigned int complete_p : 1;
164 
165   /* True if this instruction is cleared from history by unconditional
166      branch.  */
167   unsigned int cleared_p : 1;
168 };
169 
170 /* The ABI to use.  */
171 enum mips_abi_level
172 {
173   NO_ABI = 0,
174   O32_ABI,
175   O64_ABI,
176   N32_ABI,
177   N64_ABI,
178   EABI_ABI
179 };
180 
181 /* MIPS ABI we are using for this output file.  */
182 static enum mips_abi_level mips_abi = NO_ABI;
183 
184 /* Whether or not we have code that can call pic code.  */
185 int mips_abicalls = FALSE;
186 
187 /* Whether or not we have code which can be put into a shared
188    library.  */
189 static bfd_boolean mips_in_shared = TRUE;
190 
191 /* This is the set of options which may be modified by the .set
192    pseudo-op.  We use a struct so that .set push and .set pop are more
193    reliable.  */
194 
195 struct mips_set_options
196 {
197   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
198      if it has not been initialized.  Changed by `.set mipsN', and the
199      -mipsN command line option, and the default CPU.  */
200   int isa;
201   /* Enabled Application Specific Extensions (ASEs).  Changed by `.set
202      <asename>', by command line options, and based on the default
203      architecture.  */
204   int ase;
205   /* Whether we are assembling for the mips16 processor.  0 if we are
206      not, 1 if we are, and -1 if the value has not been initialized.
207      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208      -nomips16 command line options, and the default CPU.  */
209   int mips16;
210   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
211      1 if we are, and -1 if the value has not been initialized.  Changed
212      by `.set micromips' and `.set nomicromips', and the -mmicromips
213      and -mno-micromips command line options, and the default CPU.  */
214   int micromips;
215   /* Non-zero if we should not reorder instructions.  Changed by `.set
216      reorder' and `.set noreorder'.  */
217   int noreorder;
218   /* Non-zero if we should not permit the register designated "assembler
219      temporary" to be used in instructions.  The value is the register
220      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
221      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
222   unsigned int at;
223   /* Non-zero if we should warn when a macro instruction expands into
224      more than one machine instruction.  Changed by `.set nomacro' and
225      `.set macro'.  */
226   int warn_about_macros;
227   /* Non-zero if we should not move instructions.  Changed by `.set
228      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
229   int nomove;
230   /* Non-zero if we should not optimize branches by moving the target
231      of the branch into the delay slot.  Actually, we don't perform
232      this optimization anyhow.  Changed by `.set bopt' and `.set
233      nobopt'.  */
234   int nobopt;
235   /* Non-zero if we should not autoextend mips16 instructions.
236      Changed by `.set autoextend' and `.set noautoextend'.  */
237   int noautoextend;
238   /* True if we should only emit 32-bit microMIPS instructions.
239      Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240      and -mno-insn32 command line options.  */
241   bfd_boolean insn32;
242   /* Restrict general purpose registers and floating point registers
243      to 32 bit.  This is initially determined when -mgp32 or -mfp32
244      is passed but can changed if the assembler code uses .set mipsN.  */
245   int gp;
246   int fp;
247   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
248      command line option, and the default CPU.  */
249   int arch;
250   /* True if ".set sym32" is in effect.  */
251   bfd_boolean sym32;
252   /* True if floating-point operations are not allowed.  Changed by .set
253      softfloat or .set hardfloat, by command line options -msoft-float or
254      -mhard-float.  The default is false.  */
255   bfd_boolean soft_float;
256 
257   /* True if only single-precision floating-point operations are allowed.
258      Changed by .set singlefloat or .set doublefloat, command-line options
259      -msingle-float or -mdouble-float.  The default is false.  */
260   bfd_boolean single_float;
261 
262   /* 1 if single-precision operations on odd-numbered registers are
263      allowed.  */
264   int oddspreg;
265 };
266 
267 /* Specifies whether module level options have been checked yet.  */
268 static bfd_boolean file_mips_opts_checked = FALSE;
269 
270 /* Do we support nan2008?  0 if we don't, 1 if we do, and -1 if the
271    value has not been initialized.  Changed by `.nan legacy' and
272    `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273    options, and the default CPU.  */
274 static int mips_nan2008 = -1;
275 
276 /* This is the struct we use to hold the module level set of options.
277    Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
278    fp fields to -1 to indicate that they have not been initialized.  */
279 
280 static struct mips_set_options file_mips_opts =
281 {
282   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
284   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
285   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
286   /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
287 };
288 
289 /* This is similar to file_mips_opts, but for the current set of options.  */
290 
291 static struct mips_set_options mips_opts =
292 {
293   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
294   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
295   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
296   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
297   /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
298 };
299 
300 /* Which bits of file_ase were explicitly set or cleared by ASE options.  */
301 static unsigned int file_ase_explicit;
302 
303 /* These variables are filled in with the masks of registers used.
304    The object format code reads them and puts them in the appropriate
305    place.  */
306 unsigned long mips_gprmask;
307 unsigned long mips_cprmask[4];
308 
309 /* True if any MIPS16 code was produced.  */
310 static int file_ase_mips16;
311 
312 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32		\
313 			      || mips_opts.isa == ISA_MIPS32R2		\
314 			      || mips_opts.isa == ISA_MIPS32R3		\
315 			      || mips_opts.isa == ISA_MIPS32R5		\
316 			      || mips_opts.isa == ISA_MIPS64		\
317 			      || mips_opts.isa == ISA_MIPS64R2		\
318 			      || mips_opts.isa == ISA_MIPS64R3		\
319 			      || mips_opts.isa == ISA_MIPS64R5)
320 
321 /* True if any microMIPS code was produced.  */
322 static int file_ase_micromips;
323 
324 /* True if we want to create R_MIPS_JALR for jalr $25.  */
325 #ifdef TE_IRIX
326 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
327 #else
328 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
329    because there's no place for any addend, the only acceptable
330    expression is a bare symbol.  */
331 #define MIPS_JALR_HINT_P(EXPR) \
332   (!HAVE_IN_PLACE_ADDENDS \
333    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
334 #endif
335 
336 /* The argument of the -march= flag.  The architecture we are assembling.  */
337 static const char *mips_arch_string;
338 
339 /* The argument of the -mtune= flag.  The architecture for which we
340    are optimizing.  */
341 static int mips_tune = CPU_UNKNOWN;
342 static const char *mips_tune_string;
343 
344 /* True when generating 32-bit code for a 64-bit processor.  */
345 static int mips_32bitmode = 0;
346 
347 /* True if the given ABI requires 32-bit registers.  */
348 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349 
350 /* Likewise 64-bit registers.  */
351 #define ABI_NEEDS_64BIT_REGS(ABI)	\
352   ((ABI) == N32_ABI			\
353    || (ABI) == N64_ABI			\
354    || (ABI) == O64_ABI)
355 
356 #define ISA_IS_R6(ISA)			\
357   ((ISA) == ISA_MIPS32R6		\
358    || (ISA) == ISA_MIPS64R6)
359 
360 /*  Return true if ISA supports 64 bit wide gp registers.  */
361 #define ISA_HAS_64BIT_REGS(ISA)		\
362   ((ISA) == ISA_MIPS3			\
363    || (ISA) == ISA_MIPS4		\
364    || (ISA) == ISA_MIPS5		\
365    || (ISA) == ISA_MIPS64		\
366    || (ISA) == ISA_MIPS64R2		\
367    || (ISA) == ISA_MIPS64R3		\
368    || (ISA) == ISA_MIPS64R5		\
369    || (ISA) == ISA_MIPS64R6)
370 
371 /*  Return true if ISA supports 64 bit wide float registers.  */
372 #define ISA_HAS_64BIT_FPRS(ISA)		\
373   ((ISA) == ISA_MIPS3			\
374    || (ISA) == ISA_MIPS4		\
375    || (ISA) == ISA_MIPS5		\
376    || (ISA) == ISA_MIPS32R2		\
377    || (ISA) == ISA_MIPS32R3		\
378    || (ISA) == ISA_MIPS32R5		\
379    || (ISA) == ISA_MIPS32R6		\
380    || (ISA) == ISA_MIPS64		\
381    || (ISA) == ISA_MIPS64R2		\
382    || (ISA) == ISA_MIPS64R3		\
383    || (ISA) == ISA_MIPS64R5		\
384    || (ISA) == ISA_MIPS64R6)
385 
386 /* Return true if ISA supports 64-bit right rotate (dror et al.)
387    instructions.  */
388 #define ISA_HAS_DROR(ISA)		\
389   ((ISA) == ISA_MIPS64R2		\
390    || (ISA) == ISA_MIPS64R3		\
391    || (ISA) == ISA_MIPS64R5		\
392    || (ISA) == ISA_MIPS64R6		\
393    || (mips_opts.micromips		\
394        && ISA_HAS_64BIT_REGS (ISA))	\
395    )
396 
397 /* Return true if ISA supports 32-bit right rotate (ror et al.)
398    instructions.  */
399 #define ISA_HAS_ROR(ISA)		\
400   ((ISA) == ISA_MIPS32R2		\
401    || (ISA) == ISA_MIPS32R3		\
402    || (ISA) == ISA_MIPS32R5		\
403    || (ISA) == ISA_MIPS32R6		\
404    || (ISA) == ISA_MIPS64R2		\
405    || (ISA) == ISA_MIPS64R3		\
406    || (ISA) == ISA_MIPS64R5		\
407    || (ISA) == ISA_MIPS64R6		\
408    || (mips_opts.ase & ASE_SMARTMIPS)	\
409    || mips_opts.micromips		\
410    )
411 
412 /* Return true if ISA supports single-precision floats in odd registers.  */
413 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414   (((ISA) == ISA_MIPS32			\
415     || (ISA) == ISA_MIPS32R2		\
416     || (ISA) == ISA_MIPS32R3		\
417     || (ISA) == ISA_MIPS32R5		\
418     || (ISA) == ISA_MIPS32R6		\
419     || (ISA) == ISA_MIPS64		\
420     || (ISA) == ISA_MIPS64R2		\
421     || (ISA) == ISA_MIPS64R3		\
422     || (ISA) == ISA_MIPS64R5		\
423     || (ISA) == ISA_MIPS64R6		\
424     || (CPU) == CPU_R5900)		\
425    && (CPU) != CPU_LOONGSON_3A)
426 
427 /* Return true if ISA supports move to/from high part of a 64-bit
428    floating-point register. */
429 #define ISA_HAS_MXHC1(ISA)		\
430   ((ISA) == ISA_MIPS32R2		\
431    || (ISA) == ISA_MIPS32R3		\
432    || (ISA) == ISA_MIPS32R5		\
433    || (ISA) == ISA_MIPS32R6		\
434    || (ISA) == ISA_MIPS64R2		\
435    || (ISA) == ISA_MIPS64R3		\
436    || (ISA) == ISA_MIPS64R5		\
437    || (ISA) == ISA_MIPS64R6)
438 
439 /*  Return true if ISA supports legacy NAN.  */
440 #define ISA_HAS_LEGACY_NAN(ISA)		\
441   ((ISA) == ISA_MIPS1			\
442    || (ISA) == ISA_MIPS2		\
443    || (ISA) == ISA_MIPS3		\
444    || (ISA) == ISA_MIPS4		\
445    || (ISA) == ISA_MIPS5		\
446    || (ISA) == ISA_MIPS32		\
447    || (ISA) == ISA_MIPS32R2		\
448    || (ISA) == ISA_MIPS32R3		\
449    || (ISA) == ISA_MIPS32R5		\
450    || (ISA) == ISA_MIPS64		\
451    || (ISA) == ISA_MIPS64R2		\
452    || (ISA) == ISA_MIPS64R3		\
453    || (ISA) == ISA_MIPS64R5)
454 
455 #define GPR_SIZE \
456     (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457      ? 32 \
458      : mips_opts.gp)
459 
460 #define FPR_SIZE \
461     (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462      ? 32 \
463      : mips_opts.fp)
464 
465 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
466 
467 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
468 
469 /* True if relocations are stored in-place.  */
470 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471 
472 /* The ABI-derived address size.  */
473 #define HAVE_64BIT_ADDRESSES \
474   (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
475 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
476 
477 /* The size of symbolic constants (i.e., expressions of the form
478    "SYMBOL" or "SYMBOL + OFFSET").  */
479 #define HAVE_32BIT_SYMBOLS \
480   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
482 
483 /* Addresses are loaded in different ways, depending on the address size
484    in use.  The n32 ABI Documentation also mandates the use of additions
485    with overflow checking, but existing implementations don't follow it.  */
486 #define ADDRESS_ADD_INSN						\
487    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
488 
489 #define ADDRESS_ADDI_INSN						\
490    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
491 
492 #define ADDRESS_LOAD_INSN						\
493    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494 
495 #define ADDRESS_STORE_INSN						\
496    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497 
498 /* Return true if the given CPU supports the MIPS16 ASE.  */
499 #define CPU_HAS_MIPS16(cpu)						\
500    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0		\
501     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
502 
503 /* Return true if the given CPU supports the microMIPS ASE.  */
504 #define CPU_HAS_MICROMIPS(cpu)	0
505 
506 /* True if CPU has a dror instruction.  */
507 #define CPU_HAS_DROR(CPU)	((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508 
509 /* True if CPU has a ror instruction.  */
510 #define CPU_HAS_ROR(CPU)	CPU_HAS_DROR (CPU)
511 
512 /* True if CPU is in the Octeon family */
513 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 			    || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
515 
516 /* True if CPU has seq/sne and seqi/snei instructions.  */
517 #define CPU_HAS_SEQ(CPU)	(CPU_IS_OCTEON (CPU))
518 
519 /* True, if CPU has support for ldc1 and sdc1. */
520 #define CPU_HAS_LDC1_SDC1(CPU)	\
521    ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522 
523 /* True if mflo and mfhi can be immediately followed by instructions
524    which write to the HI and LO registers.
525 
526    According to MIPS specifications, MIPS ISAs I, II, and III need
527    (at least) two instructions between the reads of HI/LO and
528    instructions which write them, and later ISAs do not.  Contradicting
529    the MIPS specifications, some MIPS IV processor user manuals (e.g.
530    the UM for the NEC Vr5000) document needing the instructions between
531    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
532    MIPS64 and later ISAs to have the interlocks, plus any specific
533    earlier-ISA CPUs for which CPU documentation declares that the
534    instructions are really interlocked.  */
535 #define hilo_interlocks \
536   (mips_opts.isa == ISA_MIPS32                        \
537    || mips_opts.isa == ISA_MIPS32R2                   \
538    || mips_opts.isa == ISA_MIPS32R3                   \
539    || mips_opts.isa == ISA_MIPS32R5                   \
540    || mips_opts.isa == ISA_MIPS32R6                   \
541    || mips_opts.isa == ISA_MIPS64                     \
542    || mips_opts.isa == ISA_MIPS64R2                   \
543    || mips_opts.isa == ISA_MIPS64R3                   \
544    || mips_opts.isa == ISA_MIPS64R5                   \
545    || mips_opts.isa == ISA_MIPS64R6                   \
546    || mips_opts.arch == CPU_R4010                     \
547    || mips_opts.arch == CPU_R5900                     \
548    || mips_opts.arch == CPU_R10000                    \
549    || mips_opts.arch == CPU_R12000                    \
550    || mips_opts.arch == CPU_R14000                    \
551    || mips_opts.arch == CPU_R16000                    \
552    || mips_opts.arch == CPU_RM7000                    \
553    || mips_opts.arch == CPU_VR5500                    \
554    || mips_opts.micromips                             \
555    )
556 
557 /* Whether the processor uses hardware interlocks to protect reads
558    from the GPRs after they are loaded from memory, and thus does not
559    require nops to be inserted.  This applies to instructions marked
560    INSN_LOAD_MEMORY.  These nops are only required at MIPS ISA
561    level I and microMIPS mode instructions are always interlocked.  */
562 #define gpr_interlocks                                \
563   (mips_opts.isa != ISA_MIPS1                         \
564    || mips_opts.arch == CPU_R3900                     \
565    || mips_opts.arch == CPU_R5900                     \
566    || mips_opts.micromips                             \
567    )
568 
569 /* Whether the processor uses hardware interlocks to avoid delays
570    required by coprocessor instructions, and thus does not require
571    nops to be inserted.  This applies to instructions marked
572    INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573    instructions marked INSN_WRITE_COND_CODE and ones marked
574    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
575    levels I, II, and III and microMIPS mode instructions are always
576    interlocked.  */
577 /* Itbl support may require additional care here.  */
578 #define cop_interlocks                                \
579   ((mips_opts.isa != ISA_MIPS1                        \
580     && mips_opts.isa != ISA_MIPS2                     \
581     && mips_opts.isa != ISA_MIPS3)                    \
582    || mips_opts.arch == CPU_R4300                     \
583    || mips_opts.micromips                             \
584    )
585 
586 /* Whether the processor uses hardware interlocks to protect reads
587    from coprocessor registers after they are loaded from memory, and
588    thus does not require nops to be inserted.  This applies to
589    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
590    requires at MIPS ISA level I and microMIPS mode instructions are
591    always interlocked.  */
592 #define cop_mem_interlocks                            \
593   (mips_opts.isa != ISA_MIPS1                         \
594    || mips_opts.micromips                             \
595    )
596 
597 /* Is this a mfhi or mflo instruction?  */
598 #define MF_HILO_INSN(PINFO) \
599   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600 
601 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602    has been selected.  This implies, in particular, that addresses of text
603    labels have their LSB set.  */
604 #define HAVE_CODE_COMPRESSION						\
605   ((mips_opts.mips16 | mips_opts.micromips) != 0)
606 
607 /* The minimum and maximum signed values that can be stored in a GPR.  */
608 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
609 #define GPR_SMIN (-GPR_SMAX - 1)
610 
611 /* MIPS PIC level.  */
612 
613 enum mips_pic_level mips_pic;
614 
615 /* 1 if we should generate 32 bit offsets from the $gp register in
616    SVR4_PIC mode.  Currently has no meaning in other modes.  */
617 static int mips_big_got = 0;
618 
619 /* 1 if trap instructions should used for overflow rather than break
620    instructions.  */
621 static int mips_trap = 0;
622 
623 /* 1 if double width floating point constants should not be constructed
624    by assembling two single width halves into two single width floating
625    point registers which just happen to alias the double width destination
626    register.  On some architectures this aliasing can be disabled by a bit
627    in the status register, and the setting of this bit cannot be determined
628    automatically at assemble time.  */
629 static int mips_disable_float_construction;
630 
631 /* Non-zero if any .set noreorder directives were used.  */
632 
633 static int mips_any_noreorder;
634 
635 /* Non-zero if nops should be inserted when the register referenced in
636    an mfhi/mflo instruction is read in the next two instructions.  */
637 static int mips_7000_hilo_fix;
638 
639 /* The size of objects in the small data section.  */
640 static unsigned int g_switch_value = 8;
641 /* Whether the -G option was used.  */
642 static int g_switch_seen = 0;
643 
644 #define N_RMASK 0xc4
645 #define N_VFP   0xd4
646 
647 /* If we can determine in advance that GP optimization won't be
648    possible, we can skip the relaxation stuff that tries to produce
649    GP-relative references.  This makes delay slot optimization work
650    better.
651 
652    This function can only provide a guess, but it seems to work for
653    gcc output.  It needs to guess right for gcc, otherwise gcc
654    will put what it thinks is a GP-relative instruction in a branch
655    delay slot.
656 
657    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
658    fixed it for the non-PIC mode.  KR 95/04/07  */
659 static int nopic_need_relax (symbolS *, int);
660 
661 /* handle of the OPCODE hash table */
662 static struct hash_control *op_hash = NULL;
663 
664 /* The opcode hash table we use for the mips16.  */
665 static struct hash_control *mips16_op_hash = NULL;
666 
667 /* The opcode hash table we use for the microMIPS ASE.  */
668 static struct hash_control *micromips_op_hash = NULL;
669 
670 /* This array holds the chars that always start a comment.  If the
671     pre-processor is disabled, these aren't very useful */
672 const char comment_chars[] = "#";
673 
674 /* This array holds the chars that only start a comment at the beginning of
675    a line.  If the line seems to have the form '# 123 filename'
676    .line and .file directives will appear in the pre-processed output */
677 /* Note that input_file.c hand checks for '#' at the beginning of the
678    first line of the input file.  This is because the compiler outputs
679    #NO_APP at the beginning of its output.  */
680 /* Also note that C style comments are always supported.  */
681 const char line_comment_chars[] = "#";
682 
683 /* This array holds machine specific line separator characters.  */
684 const char line_separator_chars[] = ";";
685 
686 /* Chars that can be used to separate mant from exp in floating point nums */
687 const char EXP_CHARS[] = "eE";
688 
689 /* Chars that mean this number is a floating point constant */
690 /* As in 0f12.456 */
691 /* or    0d1.2345e12 */
692 const char FLT_CHARS[] = "rRsSfFdDxXpP";
693 
694 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695    changed in read.c .  Ideally it shouldn't have to know about it at all,
696    but nothing is ideal around here.
697  */
698 
699 /* Types of printf format used for instruction-related error messages.
700    "I" means int ("%d") and "S" means string ("%s"). */
701 enum mips_insn_error_format {
702   ERR_FMT_PLAIN,
703   ERR_FMT_I,
704   ERR_FMT_SS,
705 };
706 
707 /* Information about an error that was found while assembling the current
708    instruction.  */
709 struct mips_insn_error {
710   /* We sometimes need to match an instruction against more than one
711      opcode table entry.  Errors found during this matching are reported
712      against a particular syntactic argument rather than against the
713      instruction as a whole.  We grade these messages so that errors
714      against argument N have a greater priority than an error against
715      any argument < N, since the former implies that arguments up to N
716      were acceptable and that the opcode entry was therefore a closer match.
717      If several matches report an error against the same argument,
718      we only use that error if it is the same in all cases.
719 
720      min_argnum is the minimum argument number for which an error message
721      should be accepted.  It is 0 if MSG is against the instruction as
722      a whole.  */
723   int min_argnum;
724 
725   /* The printf()-style message, including its format and arguments.  */
726   enum mips_insn_error_format format;
727   const char *msg;
728   union {
729     int i;
730     const char *ss[2];
731   } u;
732 };
733 
734 /* The error that should be reported for the current instruction.  */
735 static struct mips_insn_error insn_error;
736 
737 static int auto_align = 1;
738 
739 /* When outputting SVR4 PIC code, the assembler needs to know the
740    offset in the stack frame from which to restore the $gp register.
741    This is set by the .cprestore pseudo-op, and saved in this
742    variable.  */
743 static offsetT mips_cprestore_offset = -1;
744 
745 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
746    more optimizations, it can use a register value instead of a memory-saved
747    offset and even an other register than $gp as global pointer.  */
748 static offsetT mips_cpreturn_offset = -1;
749 static int mips_cpreturn_register = -1;
750 static int mips_gp_register = GP;
751 static int mips_gprel_offset = 0;
752 
753 /* Whether mips_cprestore_offset has been set in the current function
754    (or whether it has already been warned about, if not).  */
755 static int mips_cprestore_valid = 0;
756 
757 /* This is the register which holds the stack frame, as set by the
758    .frame pseudo-op.  This is needed to implement .cprestore.  */
759 static int mips_frame_reg = SP;
760 
761 /* Whether mips_frame_reg has been set in the current function
762    (or whether it has already been warned about, if not).  */
763 static int mips_frame_reg_valid = 0;
764 
765 /* To output NOP instructions correctly, we need to keep information
766    about the previous two instructions.  */
767 
768 /* Whether we are optimizing.  The default value of 2 means to remove
769    unneeded NOPs and swap branch instructions when possible.  A value
770    of 1 means to not swap branches.  A value of 0 means to always
771    insert NOPs.  */
772 static int mips_optimize = 2;
773 
774 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
775    equivalent to seeing no -g option at all.  */
776 static int mips_debug = 0;
777 
778 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
779 #define MAX_VR4130_NOPS 4
780 
781 /* The maximum number of NOPs needed to fill delay slots.  */
782 #define MAX_DELAY_NOPS 2
783 
784 /* The maximum number of NOPs needed for any purpose.  */
785 #define MAX_NOPS 4
786 
787 /* A list of previous instructions, with index 0 being the most recent.
788    We need to look back MAX_NOPS instructions when filling delay slots
789    or working around processor errata.  We need to look back one
790    instruction further if we're thinking about using history[0] to
791    fill a branch delay slot.  */
792 static struct mips_cl_insn history[1 + MAX_NOPS];
793 
794 /* Arrays of operands for each instruction.  */
795 #define MAX_OPERANDS 6
796 struct mips_operand_array {
797   const struct mips_operand *operand[MAX_OPERANDS];
798 };
799 static struct mips_operand_array *mips_operands;
800 static struct mips_operand_array *mips16_operands;
801 static struct mips_operand_array *micromips_operands;
802 
803 /* Nop instructions used by emit_nop.  */
804 static struct mips_cl_insn nop_insn;
805 static struct mips_cl_insn mips16_nop_insn;
806 static struct mips_cl_insn micromips_nop16_insn;
807 static struct mips_cl_insn micromips_nop32_insn;
808 
809 /* The appropriate nop for the current mode.  */
810 #define NOP_INSN (mips_opts.mips16					\
811 		  ? &mips16_nop_insn					\
812 		  : (mips_opts.micromips				\
813 		     ? (mips_opts.insn32				\
814 			? &micromips_nop32_insn				\
815 			: &micromips_nop16_insn)			\
816 		     : &nop_insn))
817 
818 /* The size of NOP_INSN in bytes.  */
819 #define NOP_INSN_SIZE ((mips_opts.mips16				\
820 			|| (mips_opts.micromips && !mips_opts.insn32))	\
821 		       ? 2 : 4)
822 
823 /* If this is set, it points to a frag holding nop instructions which
824    were inserted before the start of a noreorder section.  If those
825    nops turn out to be unnecessary, the size of the frag can be
826    decreased.  */
827 static fragS *prev_nop_frag;
828 
829 /* The number of nop instructions we created in prev_nop_frag.  */
830 static int prev_nop_frag_holds;
831 
832 /* The number of nop instructions that we know we need in
833    prev_nop_frag.  */
834 static int prev_nop_frag_required;
835 
836 /* The number of instructions we've seen since prev_nop_frag.  */
837 static int prev_nop_frag_since;
838 
839 /* Relocations against symbols are sometimes done in two parts, with a HI
840    relocation and a LO relocation.  Each relocation has only 16 bits of
841    space to store an addend.  This means that in order for the linker to
842    handle carries correctly, it must be able to locate both the HI and
843    the LO relocation.  This means that the relocations must appear in
844    order in the relocation table.
845 
846    In order to implement this, we keep track of each unmatched HI
847    relocation.  We then sort them so that they immediately precede the
848    corresponding LO relocation.  */
849 
850 struct mips_hi_fixup
851 {
852   /* Next HI fixup.  */
853   struct mips_hi_fixup *next;
854   /* This fixup.  */
855   fixS *fixp;
856   /* The section this fixup is in.  */
857   segT seg;
858 };
859 
860 /* The list of unmatched HI relocs.  */
861 
862 static struct mips_hi_fixup *mips_hi_fixup_list;
863 
864 /* The frag containing the last explicit relocation operator.
865    Null if explicit relocations have not been used.  */
866 
867 static fragS *prev_reloc_op_frag;
868 
869 /* Map mips16 register numbers to normal MIPS register numbers.  */
870 
871 static const unsigned int mips16_to_32_reg_map[] =
872 {
873   16, 17, 2, 3, 4, 5, 6, 7
874 };
875 
876 /* Map microMIPS register numbers to normal MIPS register numbers.  */
877 
878 #define micromips_to_32_reg_d_map	mips16_to_32_reg_map
879 
880 /* The microMIPS registers with type h.  */
881 static const unsigned int micromips_to_32_reg_h_map1[] =
882 {
883   5, 5, 6, 4, 4, 4, 4, 4
884 };
885 static const unsigned int micromips_to_32_reg_h_map2[] =
886 {
887   6, 7, 7, 21, 22, 5, 6, 7
888 };
889 
890 /* The microMIPS registers with type m.  */
891 static const unsigned int micromips_to_32_reg_m_map[] =
892 {
893   0, 17, 2, 3, 16, 18, 19, 20
894 };
895 
896 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
897 
898 /* Classifies the kind of instructions we're interested in when
899    implementing -mfix-vr4120.  */
900 enum fix_vr4120_class
901 {
902   FIX_VR4120_MACC,
903   FIX_VR4120_DMACC,
904   FIX_VR4120_MULT,
905   FIX_VR4120_DMULT,
906   FIX_VR4120_DIV,
907   FIX_VR4120_MTHILO,
908   NUM_FIX_VR4120_CLASSES
909 };
910 
911 /* ...likewise -mtrap-zero-jump.  */
912 static bfd_boolean mips_trap_zero_jump;
913 
914 /* ...likewise -mfix-loongson2f-jump.  */
915 static bfd_boolean mips_fix_loongson2f_jump;
916 
917 /* ...likewise -mfix-loongson2f-nop.  */
918 static bfd_boolean mips_fix_loongson2f_nop;
919 
920 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
921 static bfd_boolean mips_fix_loongson2f;
922 
923 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
924    there must be at least one other instruction between an instruction
925    of type X and an instruction of type Y.  */
926 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
927 
928 /* True if -mfix-vr4120 is in force.  */
929 static int mips_fix_vr4120;
930 
931 /* ...likewise -mfix-vr4130.  */
932 static int mips_fix_vr4130;
933 
934 /* ...likewise -mfix-24k.  */
935 static int mips_fix_24k;
936 
937 /* ...likewise -mfix-rm7000  */
938 static int mips_fix_rm7000;
939 
940 /* ...likewise -mfix-cn63xxp1 */
941 static bfd_boolean mips_fix_cn63xxp1;
942 
943 /* We don't relax branches by default, since this causes us to expand
944    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
945    fail to compute the offset before expanding the macro to the most
946    efficient expansion.  */
947 
948 static int mips_relax_branch;
949 
950 static int mips_fix_loongson2f_btb;
951 
952 /* TRUE if checks are suppressed for invalid branches between ISA modes.
953    Needed for broken assembly produced by some GCC versions and some
954    sloppy code out there, where branches to data labels are present.  */
955 static bfd_boolean mips_ignore_branch_isa;
956 
957 /* The expansion of many macros depends on the type of symbol that
958    they refer to.  For example, when generating position-dependent code,
959    a macro that refers to a symbol may have two different expansions,
960    one which uses GP-relative addresses and one which uses absolute
961    addresses.  When generating SVR4-style PIC, a macro may have
962    different expansions for local and global symbols.
963 
964    We handle these situations by generating both sequences and putting
965    them in variant frags.  In position-dependent code, the first sequence
966    will be the GP-relative one and the second sequence will be the
967    absolute one.  In SVR4 PIC, the first sequence will be for global
968    symbols and the second will be for local symbols.
969 
970    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
971    SECOND are the lengths of the two sequences in bytes.  These fields
972    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
973    the subtype has the following flags:
974 
975    RELAX_PIC
976 	Set if generating PIC code.
977 
978    RELAX_USE_SECOND
979 	Set if it has been decided that we should use the second
980 	sequence instead of the first.
981 
982    RELAX_SECOND_LONGER
983 	Set in the first variant frag if the macro's second implementation
984 	is longer than its first.  This refers to the macro as a whole,
985 	not an individual relaxation.
986 
987    RELAX_NOMACRO
988 	Set in the first variant frag if the macro appeared in a .set nomacro
989 	block and if one alternative requires a warning but the other does not.
990 
991    RELAX_DELAY_SLOT
992 	Like RELAX_NOMACRO, but indicates that the macro appears in a branch
993 	delay slot.
994 
995    RELAX_DELAY_SLOT_16BIT
996 	Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
997 	16-bit instruction.
998 
999    RELAX_DELAY_SLOT_SIZE_FIRST
1000 	Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1001 	the macro is of the wrong size for the branch delay slot.
1002 
1003    RELAX_DELAY_SLOT_SIZE_SECOND
1004 	Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1005 	the macro is of the wrong size for the branch delay slot.
1006 
1007    The frag's "opcode" points to the first fixup for relaxable code.
1008 
1009    Relaxable macros are generated using a sequence such as:
1010 
1011       relax_start (SYMBOL);
1012       ... generate first expansion ...
1013       relax_switch ();
1014       ... generate second expansion ...
1015       relax_end ();
1016 
1017    The code and fixups for the unwanted alternative are discarded
1018    by md_convert_frag.  */
1019 #define RELAX_ENCODE(FIRST, SECOND, PIC)			\
1020   (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1021 
1022 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1023 #define RELAX_SECOND(X) ((X) & 0xff)
1024 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1025 #define RELAX_USE_SECOND 0x20000
1026 #define RELAX_SECOND_LONGER 0x40000
1027 #define RELAX_NOMACRO 0x80000
1028 #define RELAX_DELAY_SLOT 0x100000
1029 #define RELAX_DELAY_SLOT_16BIT 0x200000
1030 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1031 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1032 
1033 /* Branch without likely bit.  If label is out of range, we turn:
1034 
1035 	beq reg1, reg2, label
1036 	delay slot
1037 
1038    into
1039 
1040         bne reg1, reg2, 0f
1041         nop
1042         j label
1043      0: delay slot
1044 
1045    with the following opcode replacements:
1046 
1047 	beq <-> bne
1048 	blez <-> bgtz
1049 	bltz <-> bgez
1050 	bc1f <-> bc1t
1051 
1052 	bltzal <-> bgezal  (with jal label instead of j label)
1053 
1054    Even though keeping the delay slot instruction in the delay slot of
1055    the branch would be more efficient, it would be very tricky to do
1056    correctly, because we'd have to introduce a variable frag *after*
1057    the delay slot instruction, and expand that instead.  Let's do it
1058    the easy way for now, even if the branch-not-taken case now costs
1059    one additional instruction.  Out-of-range branches are not supposed
1060    to be common, anyway.
1061 
1062    Branch likely.  If label is out of range, we turn:
1063 
1064 	beql reg1, reg2, label
1065 	delay slot (annulled if branch not taken)
1066 
1067    into
1068 
1069         beql reg1, reg2, 1f
1070         nop
1071         beql $0, $0, 2f
1072         nop
1073      1: j[al] label
1074         delay slot (executed only if branch taken)
1075      2:
1076 
1077    It would be possible to generate a shorter sequence by losing the
1078    likely bit, generating something like:
1079 
1080 	bne reg1, reg2, 0f
1081 	nop
1082 	j[al] label
1083 	delay slot (executed only if branch taken)
1084      0:
1085 
1086 	beql -> bne
1087 	bnel -> beq
1088 	blezl -> bgtz
1089 	bgtzl -> blez
1090 	bltzl -> bgez
1091 	bgezl -> bltz
1092 	bc1fl -> bc1t
1093 	bc1tl -> bc1f
1094 
1095 	bltzall -> bgezal  (with jal label instead of j label)
1096 	bgezall -> bltzal  (ditto)
1097 
1098 
1099    but it's not clear that it would actually improve performance.  */
1100 #define RELAX_BRANCH_ENCODE(at, pic,				\
1101 			    uncond, likely, link, toofar)	\
1102   ((relax_substateT)						\
1103    (0xc0000000							\
1104     | ((at) & 0x1f)						\
1105     | ((pic) ? 0x20 : 0)					\
1106     | ((toofar) ? 0x40 : 0)					\
1107     | ((link) ? 0x80 : 0)					\
1108     | ((likely) ? 0x100 : 0)					\
1109     | ((uncond) ? 0x200 : 0)))
1110 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1111 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1112 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1113 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1114 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1115 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1116 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1117 
1118 /* For mips16 code, we use an entirely different form of relaxation.
1119    mips16 supports two versions of most instructions which take
1120    immediate values: a small one which takes some small value, and a
1121    larger one which takes a 16 bit value.  Since branches also follow
1122    this pattern, relaxing these values is required.
1123 
1124    We can assemble both mips16 and normal MIPS code in a single
1125    object.  Therefore, we need to support this type of relaxation at
1126    the same time that we support the relaxation described above.  We
1127    use the high bit of the subtype field to distinguish these cases.
1128 
1129    The information we store for this type of relaxation is the
1130    argument code found in the opcode file for this relocation, whether
1131    the user explicitly requested a small or extended form, and whether
1132    the relocation is in a jump or jal delay slot.  That tells us the
1133    size of the value, and how it should be stored.  We also store
1134    whether the fragment is considered to be extended or not.  We also
1135    store whether this is known to be a branch to a different section,
1136    whether we have tried to relax this frag yet, and whether we have
1137    ever extended a PC relative fragment because of a shift count.  */
1138 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro,	\
1139 			    small, ext,				\
1140 			    dslot, jal_dslot)			\
1141   (0x80000000							\
1142    | ((type) & 0xff)						\
1143    | ((e2) ? 0x100 : 0)						\
1144    | ((pic) ? 0x200 : 0)					\
1145    | ((sym32) ? 0x400 : 0)					\
1146    | ((nomacro) ? 0x800 : 0)					\
1147    | ((small) ? 0x1000 : 0)					\
1148    | ((ext) ? 0x2000 : 0)					\
1149    | ((dslot) ? 0x4000 : 0)					\
1150    | ((jal_dslot) ? 0x8000 : 0))
1151 
1152 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1153 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1154 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1155 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1156 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1157 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1158 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1159 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1160 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1161 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1162 
1163 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1164 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1165 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1166 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1167 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1168 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1169 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1170 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1171 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1172 
1173 /* For microMIPS code, we use relaxation similar to one we use for
1174    MIPS16 code.  Some instructions that take immediate values support
1175    two encodings: a small one which takes some small value, and a
1176    larger one which takes a 16 bit value.  As some branches also follow
1177    this pattern, relaxing these values is required.
1178 
1179    We can assemble both microMIPS and normal MIPS code in a single
1180    object.  Therefore, we need to support this type of relaxation at
1181    the same time that we support the relaxation described above.  We
1182    use one of the high bits of the subtype field to distinguish these
1183    cases.
1184 
1185    The information we store for this type of relaxation is the argument
1186    code found in the opcode file for this relocation, the register
1187    selected as the assembler temporary, whether in the 32-bit
1188    instruction mode, whether the branch is unconditional, whether it is
1189    compact, whether there is no delay-slot instruction available to fill
1190    in, whether it stores the link address implicitly in $ra, whether
1191    relaxation of out-of-range 32-bit branches to a sequence of
1192    instructions is enabled, and whether the displacement of a branch is
1193    too large to fit as an immediate argument of a 16-bit and a 32-bit
1194    branch, respectively.  */
1195 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic,		\
1196 			       uncond, compact, link, nods,	\
1197 			       relax32, toofar16, toofar32)	\
1198   (0x40000000							\
1199    | ((type) & 0xff)						\
1200    | (((at) & 0x1f) << 8)					\
1201    | ((insn32) ? 0x2000 : 0)					\
1202    | ((pic) ? 0x4000 : 0)					\
1203    | ((uncond) ? 0x8000 : 0)					\
1204    | ((compact) ? 0x10000 : 0)					\
1205    | ((link) ? 0x20000 : 0)					\
1206    | ((nods) ? 0x40000 : 0)					\
1207    | ((relax32) ? 0x80000 : 0)					\
1208    | ((toofar16) ? 0x100000 : 0)				\
1209    | ((toofar32) ? 0x200000 : 0))
1210 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1211 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1212 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1213 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1214 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1215 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1216 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1217 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1218 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1219 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1220 
1221 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1222 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1223 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1224 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1225 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1226 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1227 
1228 /* Sign-extend 16-bit value X.  */
1229 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1230 
1231 /* Is the given value a sign-extended 32-bit value?  */
1232 #define IS_SEXT_32BIT_NUM(x)						\
1233   (((x) &~ (offsetT) 0x7fffffff) == 0					\
1234    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1235 
1236 /* Is the given value a sign-extended 16-bit value?  */
1237 #define IS_SEXT_16BIT_NUM(x)						\
1238   (((x) &~ (offsetT) 0x7fff) == 0					\
1239    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1240 
1241 /* Is the given value a sign-extended 12-bit value?  */
1242 #define IS_SEXT_12BIT_NUM(x)						\
1243   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1244 
1245 /* Is the given value a sign-extended 9-bit value?  */
1246 #define IS_SEXT_9BIT_NUM(x)						\
1247   (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1248 
1249 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1250 #define IS_ZEXT_32BIT_NUM(x)						\
1251   (((x) &~ (offsetT) 0xffffffff) == 0					\
1252    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1253 
1254 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1255    SHIFT places.  */
1256 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1257   (((STRUCT) >> (SHIFT)) & (MASK))
1258 
1259 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1260 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1261   (!(MICROMIPS) \
1262    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1263    : EXTRACT_BITS ((INSN).insn_opcode, \
1264 		   MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1265 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1266   EXTRACT_BITS ((INSN).insn_opcode, \
1267 		MIPS16OP_MASK_##FIELD, \
1268 		MIPS16OP_SH_##FIELD)
1269 
1270 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1271 #define MIPS16_EXTEND (0xf000U << 16)
1272 
1273 /* Whether or not we are emitting a branch-likely macro.  */
1274 static bfd_boolean emit_branch_likely_macro = FALSE;
1275 
1276 /* Global variables used when generating relaxable macros.  See the
1277    comment above RELAX_ENCODE for more details about how relaxation
1278    is used.  */
1279 static struct {
1280   /* 0 if we're not emitting a relaxable macro.
1281      1 if we're emitting the first of the two relaxation alternatives.
1282      2 if we're emitting the second alternative.  */
1283   int sequence;
1284 
1285   /* The first relaxable fixup in the current frag.  (In other words,
1286      the first fixup that refers to relaxable code.)  */
1287   fixS *first_fixup;
1288 
1289   /* sizes[0] says how many bytes of the first alternative are stored in
1290      the current frag.  Likewise sizes[1] for the second alternative.  */
1291   unsigned int sizes[2];
1292 
1293   /* The symbol on which the choice of sequence depends.  */
1294   symbolS *symbol;
1295 } mips_relax;
1296 
1297 /* Global variables used to decide whether a macro needs a warning.  */
1298 static struct {
1299   /* True if the macro is in a branch delay slot.  */
1300   bfd_boolean delay_slot_p;
1301 
1302   /* Set to the length in bytes required if the macro is in a delay slot
1303      that requires a specific length of instruction, otherwise zero.  */
1304   unsigned int delay_slot_length;
1305 
1306   /* For relaxable macros, sizes[0] is the length of the first alternative
1307      in bytes and sizes[1] is the length of the second alternative.
1308      For non-relaxable macros, both elements give the length of the
1309      macro in bytes.  */
1310   unsigned int sizes[2];
1311 
1312   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1313      instruction of the first alternative in bytes and first_insn_sizes[1]
1314      is the length of the first instruction of the second alternative.
1315      For non-relaxable macros, both elements give the length of the first
1316      instruction in bytes.
1317 
1318      Set to zero if we haven't yet seen the first instruction.  */
1319   unsigned int first_insn_sizes[2];
1320 
1321   /* For relaxable macros, insns[0] is the number of instructions for the
1322      first alternative and insns[1] is the number of instructions for the
1323      second alternative.
1324 
1325      For non-relaxable macros, both elements give the number of
1326      instructions for the macro.  */
1327   unsigned int insns[2];
1328 
1329   /* The first variant frag for this macro.  */
1330   fragS *first_frag;
1331 } mips_macro_warning;
1332 
1333 /* Prototypes for static functions.  */
1334 
1335 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1336 
1337 static void append_insn
1338   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1339    bfd_boolean expansionp);
1340 static void mips_no_prev_insn (void);
1341 static void macro_build (expressionS *, const char *, const char *, ...);
1342 static void mips16_macro_build
1343   (expressionS *, const char *, const char *, va_list *);
1344 static void load_register (int, expressionS *, int);
1345 static void macro_build (expressionS *, const char *, const char *, ...);
1346 static void macro_start (void);
1347 static void macro_end (void);
1348 static void macro (struct mips_cl_insn *ip, char *str);
1349 static void mips16_macro (struct mips_cl_insn * ip);
1350 static void mips_ip (char *str, struct mips_cl_insn * ip);
1351 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1352 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1353 static void mips16_immed
1354   (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1355    unsigned int, unsigned long *);
1356 static size_t my_getSmallExpression
1357   (expressionS *, bfd_reloc_code_real_type *, char *);
1358 static void my_getExpression (expressionS *, char *);
1359 static void s_align (int);
1360 static void s_change_sec (int);
1361 static void s_change_section (int);
1362 static void s_cons (int);
1363 static void s_float_cons (int);
1364 static void s_mips_globl (int);
1365 static void s_option (int);
1366 static void s_mipsset (int);
1367 static void s_abicalls (int);
1368 static void s_cpload (int);
1369 static void s_cpsetup (int);
1370 static void s_cplocal (int);
1371 static void s_cprestore (int);
1372 static void s_cpreturn (int);
1373 static void s_dtprelword (int);
1374 static void s_dtpreldword (int);
1375 static void s_tprelword (int);
1376 static void s_tpreldword (int);
1377 static void s_gpvalue (int);
1378 static void s_gpword (int);
1379 static void s_gpdword (int);
1380 static void s_ehword (int);
1381 static void s_cpadd (int);
1382 static void s_insn (int);
1383 static void s_nan (int);
1384 static void s_module (int);
1385 static void s_mips_ent (int);
1386 static void s_mips_end (int);
1387 static void s_mips_frame (int);
1388 static void s_mips_mask (int reg_type);
1389 static void s_mips_stab (int);
1390 static void s_mips_weakext (int);
1391 static void s_mips_file (int);
1392 static void s_mips_loc (int);
1393 static bfd_boolean pic_need_relax (symbolS *);
1394 static int relaxed_branch_length (fragS *, asection *, int);
1395 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1396 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1397 static void file_mips_check_options (void);
1398 
1399 /* Table and functions used to map between CPU/ISA names, and
1400    ISA levels, and CPU numbers.  */
1401 
1402 struct mips_cpu_info
1403 {
1404   const char *name;           /* CPU or ISA name.  */
1405   int flags;                  /* MIPS_CPU_* flags.  */
1406   int ase;                    /* Set of ASEs implemented by the CPU.  */
1407   int isa;                    /* ISA level.  */
1408   int cpu;                    /* CPU number (default CPU if ISA).  */
1409 };
1410 
1411 #define MIPS_CPU_IS_ISA		0x0001	/* Is this an ISA?  (If 0, a CPU.) */
1412 
1413 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1414 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1415 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1416 
1417 /* Command-line options.  */
1418 const char *md_shortopts = "O::g::G:";
1419 
1420 enum options
1421   {
1422     OPTION_MARCH = OPTION_MD_BASE,
1423     OPTION_MTUNE,
1424     OPTION_MIPS1,
1425     OPTION_MIPS2,
1426     OPTION_MIPS3,
1427     OPTION_MIPS4,
1428     OPTION_MIPS5,
1429     OPTION_MIPS32,
1430     OPTION_MIPS64,
1431     OPTION_MIPS32R2,
1432     OPTION_MIPS32R3,
1433     OPTION_MIPS32R5,
1434     OPTION_MIPS32R6,
1435     OPTION_MIPS64R2,
1436     OPTION_MIPS64R3,
1437     OPTION_MIPS64R5,
1438     OPTION_MIPS64R6,
1439     OPTION_MIPS16,
1440     OPTION_NO_MIPS16,
1441     OPTION_MIPS3D,
1442     OPTION_NO_MIPS3D,
1443     OPTION_MDMX,
1444     OPTION_NO_MDMX,
1445     OPTION_DSP,
1446     OPTION_NO_DSP,
1447     OPTION_MT,
1448     OPTION_NO_MT,
1449     OPTION_VIRT,
1450     OPTION_NO_VIRT,
1451     OPTION_MSA,
1452     OPTION_NO_MSA,
1453     OPTION_SMARTMIPS,
1454     OPTION_NO_SMARTMIPS,
1455     OPTION_DSPR2,
1456     OPTION_NO_DSPR2,
1457     OPTION_DSPR3,
1458     OPTION_NO_DSPR3,
1459     OPTION_EVA,
1460     OPTION_NO_EVA,
1461     OPTION_XPA,
1462     OPTION_NO_XPA,
1463     OPTION_MICROMIPS,
1464     OPTION_NO_MICROMIPS,
1465     OPTION_MCU,
1466     OPTION_NO_MCU,
1467     OPTION_MIPS16E2,
1468     OPTION_NO_MIPS16E2,
1469     OPTION_CRC,
1470     OPTION_NO_CRC,
1471     OPTION_M4650,
1472     OPTION_NO_M4650,
1473     OPTION_M4010,
1474     OPTION_NO_M4010,
1475     OPTION_M4100,
1476     OPTION_NO_M4100,
1477     OPTION_M3900,
1478     OPTION_NO_M3900,
1479     OPTION_M7000_HILO_FIX,
1480     OPTION_MNO_7000_HILO_FIX,
1481     OPTION_FIX_24K,
1482     OPTION_NO_FIX_24K,
1483     OPTION_FIX_RM7000,
1484     OPTION_NO_FIX_RM7000,
1485     OPTION_FIX_LOONGSON2F_JUMP,
1486     OPTION_NO_FIX_LOONGSON2F_JUMP,
1487     OPTION_FIX_LOONGSON2F_NOP,
1488     OPTION_NO_FIX_LOONGSON2F_NOP,
1489     OPTION_FIX_VR4120,
1490     OPTION_NO_FIX_VR4120,
1491     OPTION_FIX_VR4130,
1492     OPTION_NO_FIX_VR4130,
1493     OPTION_FIX_CN63XXP1,
1494     OPTION_NO_FIX_CN63XXP1,
1495     OPTION_TRAP,
1496     OPTION_BREAK,
1497     OPTION_EB,
1498     OPTION_EL,
1499     OPTION_FP32,
1500     OPTION_GP32,
1501     OPTION_CONSTRUCT_FLOATS,
1502     OPTION_NO_CONSTRUCT_FLOATS,
1503     OPTION_FP64,
1504     OPTION_FPXX,
1505     OPTION_GP64,
1506     OPTION_RELAX_BRANCH,
1507     OPTION_NO_RELAX_BRANCH,
1508     OPTION_IGNORE_BRANCH_ISA,
1509     OPTION_NO_IGNORE_BRANCH_ISA,
1510     OPTION_INSN32,
1511     OPTION_NO_INSN32,
1512     OPTION_MSHARED,
1513     OPTION_MNO_SHARED,
1514     OPTION_MSYM32,
1515     OPTION_MNO_SYM32,
1516     OPTION_SOFT_FLOAT,
1517     OPTION_HARD_FLOAT,
1518     OPTION_SINGLE_FLOAT,
1519     OPTION_DOUBLE_FLOAT,
1520     OPTION_32,
1521     OPTION_CALL_SHARED,
1522     OPTION_CALL_NONPIC,
1523     OPTION_NON_SHARED,
1524     OPTION_XGOT,
1525     OPTION_MABI,
1526     OPTION_N32,
1527     OPTION_64,
1528     OPTION_MDEBUG,
1529     OPTION_NO_MDEBUG,
1530     OPTION_PDR,
1531     OPTION_NO_PDR,
1532     OPTION_MVXWORKS_PIC,
1533     OPTION_NAN,
1534     OPTION_ODD_SPREG,
1535     OPTION_NO_ODD_SPREG,
1536     OPTION_GINV,
1537     OPTION_NO_GINV,
1538     OPTION_FIX_LOONGSON2F_BTB,
1539     OPTION_NO_FIX_LOONGSON2F_BTB,
1540     OPTION_END_OF_ENUM
1541   };
1542 
1543 struct option md_longopts[] =
1544 {
1545   /* Options which specify architecture.  */
1546   {"march", required_argument, NULL, OPTION_MARCH},
1547   {"mtune", required_argument, NULL, OPTION_MTUNE},
1548   {"mips0", no_argument, NULL, OPTION_MIPS1},
1549   {"mips1", no_argument, NULL, OPTION_MIPS1},
1550   {"mips2", no_argument, NULL, OPTION_MIPS2},
1551   {"mips3", no_argument, NULL, OPTION_MIPS3},
1552   {"mips4", no_argument, NULL, OPTION_MIPS4},
1553   {"mips5", no_argument, NULL, OPTION_MIPS5},
1554   {"mips32", no_argument, NULL, OPTION_MIPS32},
1555   {"mips64", no_argument, NULL, OPTION_MIPS64},
1556   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1557   {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1558   {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1559   {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1560   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1561   {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1562   {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1563   {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1564 
1565   /* Options which specify Application Specific Extensions (ASEs).  */
1566   {"mips16", no_argument, NULL, OPTION_MIPS16},
1567   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1568   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1569   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1570   {"mdmx", no_argument, NULL, OPTION_MDMX},
1571   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1572   {"mdsp", no_argument, NULL, OPTION_DSP},
1573   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1574   {"mmt", no_argument, NULL, OPTION_MT},
1575   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1576   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1577   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1578   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1579   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1580   {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1581   {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1582   {"meva", no_argument, NULL, OPTION_EVA},
1583   {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1584   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1585   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1586   {"mmcu", no_argument, NULL, OPTION_MCU},
1587   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1588   {"mvirt", no_argument, NULL, OPTION_VIRT},
1589   {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1590   {"mmsa", no_argument, NULL, OPTION_MSA},
1591   {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1592   {"mxpa", no_argument, NULL, OPTION_XPA},
1593   {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1594   {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1595   {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1596   {"mcrc", no_argument, NULL, OPTION_CRC},
1597   {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1598   {"mginv", no_argument, NULL, OPTION_GINV},
1599   {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1600 
1601   /* Old-style architecture options.  Don't add more of these.  */
1602   {"m4650", no_argument, NULL, OPTION_M4650},
1603   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1604   {"m4010", no_argument, NULL, OPTION_M4010},
1605   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1606   {"m4100", no_argument, NULL, OPTION_M4100},
1607   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1608   {"m3900", no_argument, NULL, OPTION_M3900},
1609   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1610 
1611   /* Options which enable bug fixes.  */
1612   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1613   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1614   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1615   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1616   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1617   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1618   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1619   {"mfix-loongson2f-btb", no_argument, NULL, OPTION_FIX_LOONGSON2F_BTB},
1620   {"mno-fix-loongson2f-btb", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_BTB},
1621   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
1622   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1623   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
1624   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1625   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
1626   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1627   {"mfix-rm7000",    no_argument, NULL, OPTION_FIX_RM7000},
1628   {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1629   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1630   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1631 
1632   /* Miscellaneous options.  */
1633   {"trap", no_argument, NULL, OPTION_TRAP},
1634   {"no-break", no_argument, NULL, OPTION_TRAP},
1635   {"break", no_argument, NULL, OPTION_BREAK},
1636   {"no-trap", no_argument, NULL, OPTION_BREAK},
1637   {"EB", no_argument, NULL, OPTION_EB},
1638   {"EL", no_argument, NULL, OPTION_EL},
1639   {"mfp32", no_argument, NULL, OPTION_FP32},
1640   {"mgp32", no_argument, NULL, OPTION_GP32},
1641   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1642   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1643   {"mfp64", no_argument, NULL, OPTION_FP64},
1644   {"mfpxx", no_argument, NULL, OPTION_FPXX},
1645   {"mgp64", no_argument, NULL, OPTION_GP64},
1646   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1647   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1648   {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1649   {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1650   {"minsn32", no_argument, NULL, OPTION_INSN32},
1651   {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1652   {"mshared", no_argument, NULL, OPTION_MSHARED},
1653   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1654   {"msym32", no_argument, NULL, OPTION_MSYM32},
1655   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1656   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1657   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1658   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1659   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1660   {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1661   {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1662 
1663   /* Strictly speaking this next option is ELF specific,
1664      but we allow it for other ports as well in order to
1665      make testing easier.  */
1666   {"32", no_argument, NULL, OPTION_32},
1667 
1668   /* ELF-specific options.  */
1669   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1670   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1671   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1672   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
1673   {"xgot", no_argument, NULL, OPTION_XGOT},
1674   {"mabi", required_argument, NULL, OPTION_MABI},
1675   {"n32", no_argument, NULL, OPTION_N32},
1676   {"64", no_argument, NULL, OPTION_64},
1677   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1678   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1679   {"mpdr", no_argument, NULL, OPTION_PDR},
1680   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1681   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1682   {"mnan", required_argument, NULL, OPTION_NAN},
1683 
1684   {NULL, no_argument, NULL, 0}
1685 };
1686 size_t md_longopts_size = sizeof (md_longopts);
1687 
1688 /* Information about either an Application Specific Extension or an
1689    optional architecture feature that, for simplicity, we treat in the
1690    same way as an ASE.  */
1691 struct mips_ase
1692 {
1693   /* The name of the ASE, used in both the command-line and .set options.  */
1694   const char *name;
1695 
1696   /* The associated ASE_* flags.  If the ASE is available on both 32-bit
1697      and 64-bit architectures, the flags here refer to the subset that
1698      is available on both.  */
1699   unsigned int flags;
1700 
1701   /* The ASE_* flag used for instructions that are available on 64-bit
1702      architectures but that are not included in FLAGS.  */
1703   unsigned int flags64;
1704 
1705   /* The command-line options that turn the ASE on and off.  */
1706   int option_on;
1707   int option_off;
1708 
1709   /* The minimum required architecture revisions for MIPS32, MIPS64,
1710      microMIPS32 and microMIPS64, or -1 if the extension isn't supported.  */
1711   int mips32_rev;
1712   int mips64_rev;
1713   int micromips32_rev;
1714   int micromips64_rev;
1715 
1716   /* The architecture where the ASE was removed or -1 if the extension has not
1717      been removed.  */
1718   int rem_rev;
1719 };
1720 
1721 /* A table of all supported ASEs.  */
1722 static const struct mips_ase mips_ases[] = {
1723   { "dsp", ASE_DSP, ASE_DSP64,
1724     OPTION_DSP, OPTION_NO_DSP,
1725     2, 2, 2, 2,
1726     -1 },
1727 
1728   { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1729     OPTION_DSPR2, OPTION_NO_DSPR2,
1730     2, 2, 2, 2,
1731     -1 },
1732 
1733   { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1734     OPTION_DSPR3, OPTION_NO_DSPR3,
1735     6, 6, -1, -1,
1736     -1 },
1737 
1738   { "eva", ASE_EVA, 0,
1739     OPTION_EVA, OPTION_NO_EVA,
1740      2,  2,  2,  2,
1741     -1 },
1742 
1743   { "mcu", ASE_MCU, 0,
1744     OPTION_MCU, OPTION_NO_MCU,
1745      2,  2,  2,  2,
1746     -1 },
1747 
1748   /* Deprecated in MIPS64r5, but we don't implement that yet.  */
1749   { "mdmx", ASE_MDMX, 0,
1750     OPTION_MDMX, OPTION_NO_MDMX,
1751     -1, 1, -1, -1,
1752      6 },
1753 
1754   /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2.  */
1755   { "mips3d", ASE_MIPS3D, 0,
1756     OPTION_MIPS3D, OPTION_NO_MIPS3D,
1757     2, 1, -1, -1,
1758     6 },
1759 
1760   { "mt", ASE_MT, 0,
1761     OPTION_MT, OPTION_NO_MT,
1762      2,  2, -1, -1,
1763     -1 },
1764 
1765   { "smartmips", ASE_SMARTMIPS, 0,
1766     OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1767     1, -1, -1, -1,
1768     6 },
1769 
1770   { "virt", ASE_VIRT, ASE_VIRT64,
1771     OPTION_VIRT, OPTION_NO_VIRT,
1772      2,  2,  2,  2,
1773     -1 },
1774 
1775   { "msa", ASE_MSA, ASE_MSA64,
1776     OPTION_MSA, OPTION_NO_MSA,
1777      2,  2,  2,  2,
1778     -1 },
1779 
1780   { "xpa", ASE_XPA, 0,
1781     OPTION_XPA, OPTION_NO_XPA,
1782     2, 2, 2, 2,
1783     -1 },
1784 
1785   { "mips16e2", ASE_MIPS16E2, 0,
1786     OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1787     2,  2, -1, -1,
1788     6 },
1789 
1790   { "crc", ASE_CRC, ASE_CRC64,
1791     OPTION_CRC, OPTION_NO_CRC,
1792     6,  6, -1, -1,
1793     -1 },
1794 
1795   { "ginv", ASE_GINV, 0,
1796     OPTION_GINV, OPTION_NO_GINV,
1797     6,  6, 6, 6,
1798     -1 },
1799 };
1800 
1801 /* The set of ASEs that require -mfp64.  */
1802 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1803 
1804 /* Groups of ASE_* flags that represent different revisions of an ASE.  */
1805 static const unsigned int mips_ase_groups[] = {
1806   ASE_DSP | ASE_DSPR2 | ASE_DSPR3
1807 };
1808 
1809 /* Pseudo-op table.
1810 
1811    The following pseudo-ops from the Kane and Heinrich MIPS book
1812    should be defined here, but are currently unsupported: .alias,
1813    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1814 
1815    The following pseudo-ops from the Kane and Heinrich MIPS book are
1816    specific to the type of debugging information being generated, and
1817    should be defined by the object format: .aent, .begin, .bend,
1818    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1819    .vreg.
1820 
1821    The following pseudo-ops from the Kane and Heinrich MIPS book are
1822    not MIPS CPU specific, but are also not specific to the object file
1823    format.  This file is probably the best place to define them, but
1824    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1825 
1826 static const pseudo_typeS mips_pseudo_table[] =
1827 {
1828   /* MIPS specific pseudo-ops.  */
1829   {"option", s_option, 0},
1830   {"set", s_mipsset, 0},
1831   {"rdata", s_change_sec, 'r'},
1832   {"sdata", s_change_sec, 's'},
1833   {"livereg", s_ignore, 0},
1834   {"abicalls", s_abicalls, 0},
1835   {"cpload", s_cpload, 0},
1836   {"cpsetup", s_cpsetup, 0},
1837   {"cplocal", s_cplocal, 0},
1838   {"cprestore", s_cprestore, 0},
1839   {"cpreturn", s_cpreturn, 0},
1840   {"dtprelword", s_dtprelword, 0},
1841   {"dtpreldword", s_dtpreldword, 0},
1842   {"tprelword", s_tprelword, 0},
1843   {"tpreldword", s_tpreldword, 0},
1844   {"gpvalue", s_gpvalue, 0},
1845   {"gpword", s_gpword, 0},
1846   {"gpdword", s_gpdword, 0},
1847   {"ehword", s_ehword, 0},
1848   {"cpadd", s_cpadd, 0},
1849   {"insn", s_insn, 0},
1850   {"nan", s_nan, 0},
1851   {"module", s_module, 0},
1852 
1853   /* Relatively generic pseudo-ops that happen to be used on MIPS
1854      chips.  */
1855   {"asciiz", stringer, 8 + 1},
1856   {"bss", s_change_sec, 'b'},
1857   {"err", s_err, 0},
1858   {"half", s_cons, 1},
1859   {"dword", s_cons, 3},
1860   {"weakext", s_mips_weakext, 0},
1861   {"origin", s_org, 0},
1862   {"repeat", s_rept, 0},
1863 
1864   /* For MIPS this is non-standard, but we define it for consistency.  */
1865   {"sbss", s_change_sec, 'B'},
1866 
1867   /* These pseudo-ops are defined in read.c, but must be overridden
1868      here for one reason or another.  */
1869   {"align", s_align, 0},
1870   {"byte", s_cons, 0},
1871   {"data", s_change_sec, 'd'},
1872   {"double", s_float_cons, 'd'},
1873   {"float", s_float_cons, 'f'},
1874   {"globl", s_mips_globl, 0},
1875   {"global", s_mips_globl, 0},
1876   {"hword", s_cons, 1},
1877   {"int", s_cons, 2},
1878   {"long", s_cons, 2},
1879   {"octa", s_cons, 4},
1880   {"quad", s_cons, 3},
1881   {"section", s_change_section, 0},
1882   {"short", s_cons, 1},
1883   {"single", s_float_cons, 'f'},
1884   {"stabd", s_mips_stab, 'd'},
1885   {"stabn", s_mips_stab, 'n'},
1886   {"stabs", s_mips_stab, 's'},
1887   {"text", s_change_sec, 't'},
1888   {"word", s_cons, 2},
1889 
1890   { "extern", ecoff_directive_extern, 0},
1891 
1892   { NULL, NULL, 0 },
1893 };
1894 
1895 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1896 {
1897   /* These pseudo-ops should be defined by the object file format.
1898      However, a.out doesn't support them, so we have versions here.  */
1899   {"aent", s_mips_ent, 1},
1900   {"bgnb", s_ignore, 0},
1901   {"end", s_mips_end, 0},
1902   {"endb", s_ignore, 0},
1903   {"ent", s_mips_ent, 0},
1904   {"file", s_mips_file, 0},
1905   {"fmask", s_mips_mask, 'F'},
1906   {"frame", s_mips_frame, 0},
1907   {"loc", s_mips_loc, 0},
1908   {"mask", s_mips_mask, 'R'},
1909   {"verstamp", s_ignore, 0},
1910   { NULL, NULL, 0 },
1911 };
1912 
1913 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1914    purpose of the `.dc.a' internal pseudo-op.  */
1915 
1916 int
1917 mips_address_bytes (void)
1918 {
1919   file_mips_check_options ();
1920   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1921 }
1922 
1923 extern void pop_insert (const pseudo_typeS *);
1924 
1925 void
1926 mips_pop_insert (void)
1927 {
1928   pop_insert (mips_pseudo_table);
1929   if (! ECOFF_DEBUGGING)
1930     pop_insert (mips_nonecoff_pseudo_table);
1931 }
1932 
1933 /* Symbols labelling the current insn.  */
1934 
1935 struct insn_label_list
1936 {
1937   struct insn_label_list *next;
1938   symbolS *label;
1939 };
1940 
1941 static struct insn_label_list *free_insn_labels;
1942 #define label_list tc_segment_info_data.labels
1943 
1944 static void mips_clear_insn_labels (void);
1945 static void mips_mark_labels (void);
1946 static void mips_compressed_mark_labels (void);
1947 
1948 static inline void
1949 mips_clear_insn_labels (void)
1950 {
1951   struct insn_label_list **pl;
1952   segment_info_type *si;
1953 
1954   if (now_seg)
1955     {
1956       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1957 	;
1958 
1959       si = seg_info (now_seg);
1960       *pl = si->label_list;
1961       si->label_list = NULL;
1962     }
1963 }
1964 
1965 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1966 
1967 static inline void
1968 mips_mark_labels (void)
1969 {
1970   if (HAVE_CODE_COMPRESSION)
1971     mips_compressed_mark_labels ();
1972 }
1973 
1974 static char *expr_end;
1975 
1976 /* An expression in a macro instruction.  This is set by mips_ip and
1977    mips16_ip and when populated is always an O_constant.  */
1978 
1979 static expressionS imm_expr;
1980 
1981 /* The relocatable field in an instruction and the relocs associated
1982    with it.  These variables are used for instructions like LUI and
1983    JAL as well as true offsets.  They are also used for address
1984    operands in macros.  */
1985 
1986 static expressionS offset_expr;
1987 static bfd_reloc_code_real_type offset_reloc[3]
1988   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1989 
1990 /* This is set to the resulting size of the instruction to be produced
1991    by mips16_ip if an explicit extension is used or by mips_ip if an
1992    explicit size is supplied.  */
1993 
1994 static unsigned int forced_insn_length;
1995 
1996 /* True if we are assembling an instruction.  All dot symbols defined during
1997    this time should be treated as code labels.  */
1998 
1999 static bfd_boolean mips_assembling_insn;
2000 
2001 /* The pdr segment for per procedure frame/regmask info.  Not used for
2002    ECOFF debugging.  */
2003 
2004 static segT pdr_seg;
2005 
2006 /* The default target format to use.  */
2007 
2008 #if defined (TE_FreeBSD)
2009 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2010 #elif defined (TE_TMIPS)
2011 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2012 #else
2013 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2014 #endif
2015 
2016 const char *
2017 mips_target_format (void)
2018 {
2019   switch (OUTPUT_FLAVOR)
2020     {
2021     case bfd_target_elf_flavour:
2022 #ifdef TE_VXWORKS
2023       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2024 	return (target_big_endian
2025 		? "elf32-bigmips-vxworks"
2026 		: "elf32-littlemips-vxworks");
2027 #endif
2028       return (target_big_endian
2029 	      ? (HAVE_64BIT_OBJECTS
2030 		 ? ELF_TARGET ("elf64-", "big")
2031 		 : (HAVE_NEWABI
2032 		    ? ELF_TARGET ("elf32-n", "big")
2033 		    : ELF_TARGET ("elf32-", "big")))
2034 	      : (HAVE_64BIT_OBJECTS
2035 		 ? ELF_TARGET ("elf64-", "little")
2036 		 : (HAVE_NEWABI
2037 		    ? ELF_TARGET ("elf32-n", "little")
2038 		    : ELF_TARGET ("elf32-", "little"))));
2039     default:
2040       abort ();
2041       return NULL;
2042     }
2043 }
2044 
2045 /* Return the ISA revision that is currently in use, or 0 if we are
2046    generating code for MIPS V or below.  */
2047 
2048 static int
2049 mips_isa_rev (void)
2050 {
2051   if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2052     return 2;
2053 
2054   if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2055     return 3;
2056 
2057   if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2058     return 5;
2059 
2060   if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2061     return 6;
2062 
2063   /* microMIPS implies revision 2 or above.  */
2064   if (mips_opts.micromips)
2065     return 2;
2066 
2067   if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2068     return 1;
2069 
2070   return 0;
2071 }
2072 
2073 /* Return the mask of all ASEs that are revisions of those in FLAGS.  */
2074 
2075 static unsigned int
2076 mips_ase_mask (unsigned int flags)
2077 {
2078   unsigned int i;
2079 
2080   for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2081     if (flags & mips_ase_groups[i])
2082       flags |= mips_ase_groups[i];
2083   return flags;
2084 }
2085 
2086 /* Check whether the current ISA supports ASE.  Issue a warning if
2087    appropriate.  */
2088 
2089 static void
2090 mips_check_isa_supports_ase (const struct mips_ase *ase)
2091 {
2092   const char *base;
2093   int min_rev, size;
2094   static unsigned int warned_isa;
2095   static unsigned int warned_fp32;
2096 
2097   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2098     min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2099   else
2100     min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2101   if ((min_rev < 0 || mips_isa_rev () < min_rev)
2102       && (warned_isa & ase->flags) != ase->flags)
2103     {
2104       warned_isa |= ase->flags;
2105       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2106       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2107       if (min_rev < 0)
2108 	as_warn (_("the %d-bit %s architecture does not support the"
2109 		   " `%s' extension"), size, base, ase->name);
2110       else
2111 	as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2112 		 ase->name, base, size, min_rev);
2113     }
2114   else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2115 	   && (warned_isa & ase->flags) != ase->flags)
2116     {
2117       warned_isa |= ase->flags;
2118       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2119       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2120       as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2121 	       ase->name, base, size, ase->rem_rev);
2122     }
2123 
2124   if ((ase->flags & FP64_ASES)
2125       && mips_opts.fp != 64
2126       && (warned_fp32 & ase->flags) != ase->flags)
2127     {
2128       warned_fp32 |= ase->flags;
2129       as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2130     }
2131 }
2132 
2133 /* Check all enabled ASEs to see whether they are supported by the
2134    chosen architecture.  */
2135 
2136 static void
2137 mips_check_isa_supports_ases (void)
2138 {
2139   unsigned int i, mask;
2140 
2141   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2142     {
2143       mask = mips_ase_mask (mips_ases[i].flags);
2144       if ((mips_opts.ase & mask) == mips_ases[i].flags)
2145 	mips_check_isa_supports_ase (&mips_ases[i]);
2146     }
2147 }
2148 
2149 /* Set the state of ASE to ENABLED_P.  Return the mask of ASE_* flags
2150    that were affected.  */
2151 
2152 static unsigned int
2153 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2154 	      bfd_boolean enabled_p)
2155 {
2156   unsigned int mask;
2157 
2158   mask = mips_ase_mask (ase->flags);
2159   opts->ase &= ~mask;
2160 
2161   /* Clear combination ASE flags, which need to be recalculated based on
2162      updated regular ASE settings.  */
2163   opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
2164 
2165   if (enabled_p)
2166     opts->ase |= ase->flags;
2167 
2168   /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2169      instructions which are only valid when both ASEs are enabled.
2170      This sets the ASE_XPA_VIRT flag when both ASEs are present.  */
2171   if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2172     {
2173       opts->ase |= ASE_XPA_VIRT;
2174       mask |= ASE_XPA_VIRT;
2175     }
2176   if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2177     {
2178       opts->ase |= ASE_MIPS16E2_MT;
2179       mask |= ASE_MIPS16E2_MT;
2180     }
2181 
2182   return mask;
2183 }
2184 
2185 /* Return the ASE called NAME, or null if none.  */
2186 
2187 static const struct mips_ase *
2188 mips_lookup_ase (const char *name)
2189 {
2190   unsigned int i;
2191 
2192   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2193     if (strcmp (name, mips_ases[i].name) == 0)
2194       return &mips_ases[i];
2195   return NULL;
2196 }
2197 
2198 /* Return the length of a microMIPS instruction in bytes.  If bits of
2199    the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2200    otherwise it is a 32-bit instruction.  */
2201 
2202 static inline unsigned int
2203 micromips_insn_length (const struct mips_opcode *mo)
2204 {
2205   return mips_opcode_32bit_p (mo) ? 4 : 2;
2206 }
2207 
2208 /* Return the length of MIPS16 instruction OPCODE.  */
2209 
2210 static inline unsigned int
2211 mips16_opcode_length (unsigned long opcode)
2212 {
2213   return (opcode >> 16) == 0 ? 2 : 4;
2214 }
2215 
2216 /* Return the length of instruction INSN.  */
2217 
2218 static inline unsigned int
2219 insn_length (const struct mips_cl_insn *insn)
2220 {
2221   if (mips_opts.micromips)
2222     return micromips_insn_length (insn->insn_mo);
2223   else if (mips_opts.mips16)
2224     return mips16_opcode_length (insn->insn_opcode);
2225   else
2226     return 4;
2227 }
2228 
2229 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
2230 
2231 static void
2232 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2233 {
2234   size_t i;
2235 
2236   insn->insn_mo = mo;
2237   insn->insn_opcode = mo->match;
2238   insn->frag = NULL;
2239   insn->where = 0;
2240   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2241     insn->fixp[i] = NULL;
2242   insn->fixed_p = (mips_opts.noreorder > 0);
2243   insn->noreorder_p = (mips_opts.noreorder > 0);
2244   insn->mips16_absolute_jump_p = 0;
2245   insn->complete_p = 0;
2246   insn->cleared_p = 0;
2247 }
2248 
2249 /* Get a list of all the operands in INSN.  */
2250 
2251 static const struct mips_operand_array *
2252 insn_operands (const struct mips_cl_insn *insn)
2253 {
2254   if (insn->insn_mo >= &mips_opcodes[0]
2255       && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2256     return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2257 
2258   if (insn->insn_mo >= &mips16_opcodes[0]
2259       && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2260     return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2261 
2262   if (insn->insn_mo >= &micromips_opcodes[0]
2263       && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2264     return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2265 
2266   abort ();
2267 }
2268 
2269 /* Get a description of operand OPNO of INSN.  */
2270 
2271 static const struct mips_operand *
2272 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2273 {
2274   const struct mips_operand_array *operands;
2275 
2276   operands = insn_operands (insn);
2277   if (opno >= MAX_OPERANDS || !operands->operand[opno])
2278     abort ();
2279   return operands->operand[opno];
2280 }
2281 
2282 /* Install UVAL as the value of OPERAND in INSN.  */
2283 
2284 static inline void
2285 insn_insert_operand (struct mips_cl_insn *insn,
2286 		     const struct mips_operand *operand, unsigned int uval)
2287 {
2288   if (mips_opts.mips16
2289       && operand->type == OP_INT && operand->lsb == 0
2290       && mips_opcode_32bit_p (insn->insn_mo))
2291     insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2292   else
2293     insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2294 }
2295 
2296 /* Extract the value of OPERAND from INSN.  */
2297 
2298 static inline unsigned
2299 insn_extract_operand (const struct mips_cl_insn *insn,
2300 		      const struct mips_operand *operand)
2301 {
2302   return mips_extract_operand (operand, insn->insn_opcode);
2303 }
2304 
2305 /* Record the current MIPS16/microMIPS mode in now_seg.  */
2306 
2307 static void
2308 mips_record_compressed_mode (void)
2309 {
2310   segment_info_type *si;
2311 
2312   si = seg_info (now_seg);
2313   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2314     si->tc_segment_info_data.mips16 = mips_opts.mips16;
2315   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2316     si->tc_segment_info_data.micromips = mips_opts.micromips;
2317 }
2318 
2319 /* Read a standard MIPS instruction from BUF.  */
2320 
2321 static unsigned long
2322 read_insn (char *buf)
2323 {
2324   if (target_big_endian)
2325     return bfd_getb32 ((bfd_byte *) buf);
2326   else
2327     return bfd_getl32 ((bfd_byte *) buf);
2328 }
2329 
2330 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
2331    the next byte.  */
2332 
2333 static char *
2334 write_insn (char *buf, unsigned int insn)
2335 {
2336   md_number_to_chars (buf, insn, 4);
2337   return buf + 4;
2338 }
2339 
2340 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2341    has length LENGTH.  */
2342 
2343 static unsigned long
2344 read_compressed_insn (char *buf, unsigned int length)
2345 {
2346   unsigned long insn;
2347   unsigned int i;
2348 
2349   insn = 0;
2350   for (i = 0; i < length; i += 2)
2351     {
2352       insn <<= 16;
2353       if (target_big_endian)
2354 	insn |= bfd_getb16 ((char *) buf);
2355       else
2356 	insn |= bfd_getl16 ((char *) buf);
2357       buf += 2;
2358     }
2359   return insn;
2360 }
2361 
2362 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2363    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
2364 
2365 static char *
2366 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2367 {
2368   unsigned int i;
2369 
2370   for (i = 0; i < length; i += 2)
2371     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2372   return buf + length;
2373 }
2374 
2375 /* Install INSN at the location specified by its "frag" and "where" fields.  */
2376 
2377 static void
2378 install_insn (const struct mips_cl_insn *insn)
2379 {
2380   char *f = insn->frag->fr_literal + insn->where;
2381   if (HAVE_CODE_COMPRESSION)
2382     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2383   else
2384     write_insn (f, insn->insn_opcode);
2385   mips_record_compressed_mode ();
2386 }
2387 
2388 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
2389    and install the opcode in the new location.  */
2390 
2391 static void
2392 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2393 {
2394   size_t i;
2395 
2396   insn->frag = frag;
2397   insn->where = where;
2398   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2399     if (insn->fixp[i] != NULL)
2400       {
2401 	insn->fixp[i]->fx_frag = frag;
2402 	insn->fixp[i]->fx_where = where;
2403       }
2404   install_insn (insn);
2405 }
2406 
2407 /* Add INSN to the end of the output.  */
2408 
2409 static void
2410 add_fixed_insn (struct mips_cl_insn *insn)
2411 {
2412   char *f = frag_more (insn_length (insn));
2413   move_insn (insn, frag_now, f - frag_now->fr_literal);
2414 }
2415 
2416 /* Start a variant frag and move INSN to the start of the variant part,
2417    marking it as fixed.  The other arguments are as for frag_var.  */
2418 
2419 static void
2420 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2421 		  relax_substateT subtype, symbolS *symbol, offsetT offset)
2422 {
2423   frag_grow (max_chars);
2424   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2425   insn->fixed_p = 1;
2426   frag_var (rs_machine_dependent, max_chars, var,
2427 	    subtype, symbol, offset, NULL);
2428 }
2429 
2430 /* Insert N copies of INSN into the history buffer, starting at
2431    position FIRST.  Neither FIRST nor N need to be clipped.  */
2432 
2433 static void
2434 insert_into_history (unsigned int first, unsigned int n,
2435 		     const struct mips_cl_insn *insn)
2436 {
2437   if (mips_relax.sequence != 2)
2438     {
2439       unsigned int i;
2440 
2441       for (i = ARRAY_SIZE (history); i-- > first;)
2442 	if (i >= first + n)
2443 	  history[i] = history[i - n];
2444 	else
2445 	  history[i] = *insn;
2446     }
2447 }
2448 
2449 /* Clear the error in insn_error.  */
2450 
2451 static void
2452 clear_insn_error (void)
2453 {
2454   memset (&insn_error, 0, sizeof (insn_error));
2455 }
2456 
2457 /* Possibly record error message MSG for the current instruction.
2458    If the error is about a particular argument, ARGNUM is the 1-based
2459    number of that argument, otherwise it is 0.  FORMAT is the format
2460    of MSG.  Return true if MSG was used, false if the current message
2461    was kept.  */
2462 
2463 static bfd_boolean
2464 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2465 		       const char *msg)
2466 {
2467   if (argnum == 0)
2468     {
2469       /* Give priority to errors against specific arguments, and to
2470 	 the first whole-instruction message.  */
2471       if (insn_error.msg)
2472 	return FALSE;
2473     }
2474   else
2475     {
2476       /* Keep insn_error if it is against a later argument.  */
2477       if (argnum < insn_error.min_argnum)
2478 	return FALSE;
2479 
2480       /* If both errors are against the same argument but are different,
2481 	 give up on reporting a specific error for this argument.
2482 	 See the comment about mips_insn_error for details.  */
2483       if (argnum == insn_error.min_argnum
2484 	  && insn_error.msg
2485 	  && strcmp (insn_error.msg, msg) != 0)
2486 	{
2487 	  insn_error.msg = 0;
2488 	  insn_error.min_argnum += 1;
2489 	  return FALSE;
2490 	}
2491     }
2492   insn_error.min_argnum = argnum;
2493   insn_error.format = format;
2494   insn_error.msg = msg;
2495   return TRUE;
2496 }
2497 
2498 /* Record an instruction error with no % format fields.  ARGNUM and MSG are
2499    as for set_insn_error_format.  */
2500 
2501 static void
2502 set_insn_error (int argnum, const char *msg)
2503 {
2504   set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2505 }
2506 
2507 /* Record an instruction error with one %d field I.  ARGNUM and MSG are
2508    as for set_insn_error_format.  */
2509 
2510 static void
2511 set_insn_error_i (int argnum, const char *msg, int i)
2512 {
2513   if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2514     insn_error.u.i = i;
2515 }
2516 
2517 /* Record an instruction error with two %s fields S1 and S2.  ARGNUM and MSG
2518    are as for set_insn_error_format.  */
2519 
2520 static void
2521 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2522 {
2523   if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2524     {
2525       insn_error.u.ss[0] = s1;
2526       insn_error.u.ss[1] = s2;
2527     }
2528 }
2529 
2530 /* Report the error in insn_error, which is against assembly code STR.  */
2531 
2532 static void
2533 report_insn_error (const char *str)
2534 {
2535   const char *msg = concat (insn_error.msg, " `%s'", NULL);
2536 
2537   switch (insn_error.format)
2538     {
2539     case ERR_FMT_PLAIN:
2540       as_bad (msg, str);
2541       break;
2542 
2543     case ERR_FMT_I:
2544       as_bad (msg, insn_error.u.i, str);
2545       break;
2546 
2547     case ERR_FMT_SS:
2548       as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2549       break;
2550     }
2551 
2552   free ((char *) msg);
2553 }
2554 
2555 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
2556    the idea is to make it obvious at a glance that each errata is
2557    included.  */
2558 
2559 static void
2560 init_vr4120_conflicts (void)
2561 {
2562 #define CONFLICT(FIRST, SECOND) \
2563     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2564 
2565   /* Errata 21 - [D]DIV[U] after [D]MACC */
2566   CONFLICT (MACC, DIV);
2567   CONFLICT (DMACC, DIV);
2568 
2569   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
2570   CONFLICT (DMULT, DMULT);
2571   CONFLICT (DMULT, DMACC);
2572   CONFLICT (DMACC, DMULT);
2573   CONFLICT (DMACC, DMACC);
2574 
2575   /* Errata 24 - MT{LO,HI} after [D]MACC */
2576   CONFLICT (MACC, MTHILO);
2577   CONFLICT (DMACC, MTHILO);
2578 
2579   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2580      instruction is executed immediately after a MACC or DMACC
2581      instruction, the result of [either instruction] is incorrect."  */
2582   CONFLICT (MACC, MULT);
2583   CONFLICT (MACC, DMULT);
2584   CONFLICT (DMACC, MULT);
2585   CONFLICT (DMACC, DMULT);
2586 
2587   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2588      executed immediately after a DMULT, DMULTU, DIV, DIVU,
2589      DDIV or DDIVU instruction, the result of the MACC or
2590      DMACC instruction is incorrect.".  */
2591   CONFLICT (DMULT, MACC);
2592   CONFLICT (DMULT, DMACC);
2593   CONFLICT (DIV, MACC);
2594   CONFLICT (DIV, DMACC);
2595 
2596 #undef CONFLICT
2597 }
2598 
2599 struct regname {
2600   const char *name;
2601   unsigned int num;
2602 };
2603 
2604 #define RNUM_MASK	0x00000ff
2605 #define RTYPE_MASK	0x0ffff00
2606 #define RTYPE_NUM	0x0000100
2607 #define RTYPE_FPU	0x0000200
2608 #define RTYPE_FCC	0x0000400
2609 #define RTYPE_VEC	0x0000800
2610 #define RTYPE_GP	0x0001000
2611 #define RTYPE_CP0	0x0002000
2612 #define RTYPE_PC	0x0004000
2613 #define RTYPE_ACC	0x0008000
2614 #define RTYPE_CCC	0x0010000
2615 #define RTYPE_VI	0x0020000
2616 #define RTYPE_VF	0x0040000
2617 #define RTYPE_R5900_I	0x0080000
2618 #define RTYPE_R5900_Q	0x0100000
2619 #define RTYPE_R5900_R	0x0200000
2620 #define RTYPE_R5900_ACC	0x0400000
2621 #define RTYPE_MSA	0x0800000
2622 #define RWARN		0x8000000
2623 
2624 #define GENERIC_REGISTER_NUMBERS \
2625     {"$0",	RTYPE_NUM | 0},  \
2626     {"$1",	RTYPE_NUM | 1},  \
2627     {"$2",	RTYPE_NUM | 2},  \
2628     {"$3",	RTYPE_NUM | 3},  \
2629     {"$4",	RTYPE_NUM | 4},  \
2630     {"$5",	RTYPE_NUM | 5},  \
2631     {"$6",	RTYPE_NUM | 6},  \
2632     {"$7",	RTYPE_NUM | 7},  \
2633     {"$8",	RTYPE_NUM | 8},  \
2634     {"$9",	RTYPE_NUM | 9},  \
2635     {"$10",	RTYPE_NUM | 10}, \
2636     {"$11",	RTYPE_NUM | 11}, \
2637     {"$12",	RTYPE_NUM | 12}, \
2638     {"$13",	RTYPE_NUM | 13}, \
2639     {"$14",	RTYPE_NUM | 14}, \
2640     {"$15",	RTYPE_NUM | 15}, \
2641     {"$16",	RTYPE_NUM | 16}, \
2642     {"$17",	RTYPE_NUM | 17}, \
2643     {"$18",	RTYPE_NUM | 18}, \
2644     {"$19",	RTYPE_NUM | 19}, \
2645     {"$20",	RTYPE_NUM | 20}, \
2646     {"$21",	RTYPE_NUM | 21}, \
2647     {"$22",	RTYPE_NUM | 22}, \
2648     {"$23",	RTYPE_NUM | 23}, \
2649     {"$24",	RTYPE_NUM | 24}, \
2650     {"$25",	RTYPE_NUM | 25}, \
2651     {"$26",	RTYPE_NUM | 26}, \
2652     {"$27",	RTYPE_NUM | 27}, \
2653     {"$28",	RTYPE_NUM | 28}, \
2654     {"$29",	RTYPE_NUM | 29}, \
2655     {"$30",	RTYPE_NUM | 30}, \
2656     {"$31",	RTYPE_NUM | 31}
2657 
2658 #define FPU_REGISTER_NAMES       \
2659     {"$f0",	RTYPE_FPU | 0},  \
2660     {"$f1",	RTYPE_FPU | 1},  \
2661     {"$f2",	RTYPE_FPU | 2},  \
2662     {"$f3",	RTYPE_FPU | 3},  \
2663     {"$f4",	RTYPE_FPU | 4},  \
2664     {"$f5",	RTYPE_FPU | 5},  \
2665     {"$f6",	RTYPE_FPU | 6},  \
2666     {"$f7",	RTYPE_FPU | 7},  \
2667     {"$f8",	RTYPE_FPU | 8},  \
2668     {"$f9",	RTYPE_FPU | 9},  \
2669     {"$f10",	RTYPE_FPU | 10}, \
2670     {"$f11",	RTYPE_FPU | 11}, \
2671     {"$f12",	RTYPE_FPU | 12}, \
2672     {"$f13",	RTYPE_FPU | 13}, \
2673     {"$f14",	RTYPE_FPU | 14}, \
2674     {"$f15",	RTYPE_FPU | 15}, \
2675     {"$f16",	RTYPE_FPU | 16}, \
2676     {"$f17",	RTYPE_FPU | 17}, \
2677     {"$f18",	RTYPE_FPU | 18}, \
2678     {"$f19",	RTYPE_FPU | 19}, \
2679     {"$f20",	RTYPE_FPU | 20}, \
2680     {"$f21",	RTYPE_FPU | 21}, \
2681     {"$f22",	RTYPE_FPU | 22}, \
2682     {"$f23",	RTYPE_FPU | 23}, \
2683     {"$f24",	RTYPE_FPU | 24}, \
2684     {"$f25",	RTYPE_FPU | 25}, \
2685     {"$f26",	RTYPE_FPU | 26}, \
2686     {"$f27",	RTYPE_FPU | 27}, \
2687     {"$f28",	RTYPE_FPU | 28}, \
2688     {"$f29",	RTYPE_FPU | 29}, \
2689     {"$f30",	RTYPE_FPU | 30}, \
2690     {"$f31",	RTYPE_FPU | 31}
2691 
2692 #define FPU_CONDITION_CODE_NAMES \
2693     {"$fcc0",	RTYPE_FCC | 0},  \
2694     {"$fcc1",	RTYPE_FCC | 1},  \
2695     {"$fcc2",	RTYPE_FCC | 2},  \
2696     {"$fcc3",	RTYPE_FCC | 3},  \
2697     {"$fcc4",	RTYPE_FCC | 4},  \
2698     {"$fcc5",	RTYPE_FCC | 5},  \
2699     {"$fcc6",	RTYPE_FCC | 6},  \
2700     {"$fcc7",	RTYPE_FCC | 7}
2701 
2702 #define COPROC_CONDITION_CODE_NAMES         \
2703     {"$cc0",	RTYPE_FCC | RTYPE_CCC | 0}, \
2704     {"$cc1",	RTYPE_FCC | RTYPE_CCC | 1}, \
2705     {"$cc2",	RTYPE_FCC | RTYPE_CCC | 2}, \
2706     {"$cc3",	RTYPE_FCC | RTYPE_CCC | 3}, \
2707     {"$cc4",	RTYPE_FCC | RTYPE_CCC | 4}, \
2708     {"$cc5",	RTYPE_FCC | RTYPE_CCC | 5}, \
2709     {"$cc6",	RTYPE_FCC | RTYPE_CCC | 6}, \
2710     {"$cc7",	RTYPE_FCC | RTYPE_CCC | 7}
2711 
2712 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2713     {"$a4",	RTYPE_GP | 8},  \
2714     {"$a5",	RTYPE_GP | 9},  \
2715     {"$a6",	RTYPE_GP | 10}, \
2716     {"$a7",	RTYPE_GP | 11}, \
2717     {"$ta0",	RTYPE_GP | 8},  /* alias for $a4 */ \
2718     {"$ta1",	RTYPE_GP | 9},  /* alias for $a5 */ \
2719     {"$ta2",	RTYPE_GP | 10}, /* alias for $a6 */ \
2720     {"$ta3",	RTYPE_GP | 11}, /* alias for $a7 */ \
2721     {"$t0",	RTYPE_GP | 12}, \
2722     {"$t1",	RTYPE_GP | 13}, \
2723     {"$t2",	RTYPE_GP | 14}, \
2724     {"$t3",	RTYPE_GP | 15}
2725 
2726 #define O32_SYMBOLIC_REGISTER_NAMES \
2727     {"$t0",	RTYPE_GP | 8},  \
2728     {"$t1",	RTYPE_GP | 9},  \
2729     {"$t2",	RTYPE_GP | 10}, \
2730     {"$t3",	RTYPE_GP | 11}, \
2731     {"$t4",	RTYPE_GP | 12}, \
2732     {"$t5",	RTYPE_GP | 13}, \
2733     {"$t6",	RTYPE_GP | 14}, \
2734     {"$t7",	RTYPE_GP | 15}, \
2735     {"$ta0",	RTYPE_GP | 12}, /* alias for $t4 */ \
2736     {"$ta1",	RTYPE_GP | 13}, /* alias for $t5 */ \
2737     {"$ta2",	RTYPE_GP | 14}, /* alias for $t6 */ \
2738     {"$ta3",	RTYPE_GP | 15}  /* alias for $t7 */
2739 
2740 /* Remaining symbolic register names */
2741 #define SYMBOLIC_REGISTER_NAMES \
2742     {"$zero",	RTYPE_GP | 0},  \
2743     {"$at",	RTYPE_GP | 1},  \
2744     {"$AT",	RTYPE_GP | 1},  \
2745     {"$v0",	RTYPE_GP | 2},  \
2746     {"$v1",	RTYPE_GP | 3},  \
2747     {"$a0",	RTYPE_GP | 4},  \
2748     {"$a1",	RTYPE_GP | 5},  \
2749     {"$a2",	RTYPE_GP | 6},  \
2750     {"$a3",	RTYPE_GP | 7},  \
2751     {"$s0",	RTYPE_GP | 16}, \
2752     {"$s1",	RTYPE_GP | 17}, \
2753     {"$s2",	RTYPE_GP | 18}, \
2754     {"$s3",	RTYPE_GP | 19}, \
2755     {"$s4",	RTYPE_GP | 20}, \
2756     {"$s5",	RTYPE_GP | 21}, \
2757     {"$s6",	RTYPE_GP | 22}, \
2758     {"$s7",	RTYPE_GP | 23}, \
2759     {"$t8",	RTYPE_GP | 24}, \
2760     {"$t9",	RTYPE_GP | 25}, \
2761     {"$k0",	RTYPE_GP | 26}, \
2762     {"$kt0",	RTYPE_GP | 26}, \
2763     {"$k1",	RTYPE_GP | 27}, \
2764     {"$kt1",	RTYPE_GP | 27}, \
2765     {"$gp",	RTYPE_GP | 28}, \
2766     {"$sp",	RTYPE_GP | 29}, \
2767     {"$s8",	RTYPE_GP | 30}, \
2768     {"$fp",	RTYPE_GP | 30}, \
2769     {"$ra",	RTYPE_GP | 31}
2770 
2771 #define MIPS16_SPECIAL_REGISTER_NAMES \
2772     {"$pc",	RTYPE_PC | 0}
2773 
2774 #define MDMX_VECTOR_REGISTER_NAMES \
2775     /* {"$v0",	RTYPE_VEC | 0},  clash with REG 2 above */ \
2776     /* {"$v1",	RTYPE_VEC | 1},  clash with REG 3 above */ \
2777     {"$v2",	RTYPE_VEC | 2},  \
2778     {"$v3",	RTYPE_VEC | 3},  \
2779     {"$v4",	RTYPE_VEC | 4},  \
2780     {"$v5",	RTYPE_VEC | 5},  \
2781     {"$v6",	RTYPE_VEC | 6},  \
2782     {"$v7",	RTYPE_VEC | 7},  \
2783     {"$v8",	RTYPE_VEC | 8},  \
2784     {"$v9",	RTYPE_VEC | 9},  \
2785     {"$v10",	RTYPE_VEC | 10}, \
2786     {"$v11",	RTYPE_VEC | 11}, \
2787     {"$v12",	RTYPE_VEC | 12}, \
2788     {"$v13",	RTYPE_VEC | 13}, \
2789     {"$v14",	RTYPE_VEC | 14}, \
2790     {"$v15",	RTYPE_VEC | 15}, \
2791     {"$v16",	RTYPE_VEC | 16}, \
2792     {"$v17",	RTYPE_VEC | 17}, \
2793     {"$v18",	RTYPE_VEC | 18}, \
2794     {"$v19",	RTYPE_VEC | 19}, \
2795     {"$v20",	RTYPE_VEC | 20}, \
2796     {"$v21",	RTYPE_VEC | 21}, \
2797     {"$v22",	RTYPE_VEC | 22}, \
2798     {"$v23",	RTYPE_VEC | 23}, \
2799     {"$v24",	RTYPE_VEC | 24}, \
2800     {"$v25",	RTYPE_VEC | 25}, \
2801     {"$v26",	RTYPE_VEC | 26}, \
2802     {"$v27",	RTYPE_VEC | 27}, \
2803     {"$v28",	RTYPE_VEC | 28}, \
2804     {"$v29",	RTYPE_VEC | 29}, \
2805     {"$v30",	RTYPE_VEC | 30}, \
2806     {"$v31",	RTYPE_VEC | 31}
2807 
2808 #define R5900_I_NAMES \
2809     {"$I",	RTYPE_R5900_I | 0}
2810 
2811 #define R5900_Q_NAMES \
2812     {"$Q",	RTYPE_R5900_Q | 0}
2813 
2814 #define R5900_R_NAMES \
2815     {"$R",	RTYPE_R5900_R | 0}
2816 
2817 #define R5900_ACC_NAMES \
2818     {"$ACC",	RTYPE_R5900_ACC | 0 }
2819 
2820 #define MIPS_DSP_ACCUMULATOR_NAMES \
2821     {"$ac0",	RTYPE_ACC | 0}, \
2822     {"$ac1",	RTYPE_ACC | 1}, \
2823     {"$ac2",	RTYPE_ACC | 2}, \
2824     {"$ac3",	RTYPE_ACC | 3}
2825 
2826 static const struct regname reg_names[] = {
2827   GENERIC_REGISTER_NUMBERS,
2828   FPU_REGISTER_NAMES,
2829   FPU_CONDITION_CODE_NAMES,
2830   COPROC_CONDITION_CODE_NAMES,
2831 
2832   /* The $txx registers depends on the abi,
2833      these will be added later into the symbol table from
2834      one of the tables below once mips_abi is set after
2835      parsing of arguments from the command line. */
2836   SYMBOLIC_REGISTER_NAMES,
2837 
2838   MIPS16_SPECIAL_REGISTER_NAMES,
2839   MDMX_VECTOR_REGISTER_NAMES,
2840   R5900_I_NAMES,
2841   R5900_Q_NAMES,
2842   R5900_R_NAMES,
2843   R5900_ACC_NAMES,
2844   MIPS_DSP_ACCUMULATOR_NAMES,
2845   {0, 0}
2846 };
2847 
2848 static const struct regname reg_names_o32[] = {
2849   O32_SYMBOLIC_REGISTER_NAMES,
2850   {0, 0}
2851 };
2852 
2853 static const struct regname reg_names_n32n64[] = {
2854   N32N64_SYMBOLIC_REGISTER_NAMES,
2855   {0, 0}
2856 };
2857 
2858 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2859    interpreted as vector registers 0 and 1.  If SYMVAL is the value of one
2860    of these register symbols, return the associated vector register,
2861    otherwise return SYMVAL itself.  */
2862 
2863 static unsigned int
2864 mips_prefer_vec_regno (unsigned int symval)
2865 {
2866   if ((symval & -2) == (RTYPE_GP | 2))
2867     return RTYPE_VEC | (symval & 1);
2868   return symval;
2869 }
2870 
2871 /* Return true if string [S, E) is a valid register name, storing its
2872    symbol value in *SYMVAL_PTR if so.  */
2873 
2874 static bfd_boolean
2875 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2876 {
2877   char save_c;
2878   symbolS *symbol;
2879 
2880   /* Terminate name.  */
2881   save_c = *e;
2882   *e = '\0';
2883 
2884   /* Look up the name.  */
2885   symbol = symbol_find (s);
2886   *e = save_c;
2887 
2888   if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2889     return FALSE;
2890 
2891   *symval_ptr = S_GET_VALUE (symbol);
2892   return TRUE;
2893 }
2894 
2895 /* Return true if the string at *SPTR is a valid register name.  Allow it
2896    to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2897    is nonnull.
2898 
2899    When returning true, move *SPTR past the register, store the
2900    register's symbol value in *SYMVAL_PTR and the channel mask in
2901    *CHANNELS_PTR (if nonnull).  The symbol value includes the register
2902    number (RNUM_MASK) and register type (RTYPE_MASK).  The channel mask
2903    is a 4-bit value of the form XYZW and is 0 if no suffix was given.  */
2904 
2905 static bfd_boolean
2906 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2907 		     unsigned int *channels_ptr)
2908 {
2909   char *s, *e, *m;
2910   const char *q;
2911   unsigned int channels, symval, bit;
2912 
2913   /* Find end of name.  */
2914   s = e = *sptr;
2915   if (is_name_beginner (*e))
2916     ++e;
2917   while (is_part_of_name (*e))
2918     ++e;
2919 
2920   channels = 0;
2921   if (!mips_parse_register_1 (s, e, &symval))
2922     {
2923       if (!channels_ptr)
2924 	return FALSE;
2925 
2926       /* Eat characters from the end of the string that are valid
2927 	 channel suffixes.  The preceding register must be $ACC or
2928 	 end with a digit, so there is no ambiguity.  */
2929       bit = 1;
2930       m = e;
2931       for (q = "wzyx"; *q; q++, bit <<= 1)
2932 	if (m > s && m[-1] == *q)
2933 	  {
2934 	    --m;
2935 	    channels |= bit;
2936 	  }
2937 
2938       if (channels == 0
2939 	  || !mips_parse_register_1 (s, m, &symval)
2940 	  || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2941 	return FALSE;
2942     }
2943 
2944   *sptr = e;
2945   *symval_ptr = symval;
2946   if (channels_ptr)
2947     *channels_ptr = channels;
2948   return TRUE;
2949 }
2950 
2951 /* Check if SPTR points at a valid register specifier according to TYPES.
2952    If so, then return 1, advance S to consume the specifier and store
2953    the register's number in REGNOP, otherwise return 0.  */
2954 
2955 static int
2956 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2957 {
2958   unsigned int regno;
2959 
2960   if (mips_parse_register (s, &regno, NULL))
2961     {
2962       if (types & RTYPE_VEC)
2963 	regno = mips_prefer_vec_regno (regno);
2964       if (regno & types)
2965 	regno &= RNUM_MASK;
2966       else
2967 	regno = ~0;
2968     }
2969   else
2970     {
2971       if (types & RWARN)
2972 	as_warn (_("unrecognized register name `%s'"), *s);
2973       regno = ~0;
2974     }
2975   if (regnop)
2976     *regnop = regno;
2977   return regno <= RNUM_MASK;
2978 }
2979 
2980 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2981    mask in *CHANNELS.  Return a pointer to the first unconsumed character.  */
2982 
2983 static char *
2984 mips_parse_vu0_channels (char *s, unsigned int *channels)
2985 {
2986   unsigned int i;
2987 
2988   *channels = 0;
2989   for (i = 0; i < 4; i++)
2990     if (*s == "xyzw"[i])
2991       {
2992 	*channels |= 1 << (3 - i);
2993 	++s;
2994       }
2995   return s;
2996 }
2997 
2998 /* Token types for parsed operand lists.  */
2999 enum mips_operand_token_type {
3000   /* A plain register, e.g. $f2.  */
3001   OT_REG,
3002 
3003   /* A 4-bit XYZW channel mask.  */
3004   OT_CHANNELS,
3005 
3006   /* A constant vector index, e.g. [1].  */
3007   OT_INTEGER_INDEX,
3008 
3009   /* A register vector index, e.g. [$2].  */
3010   OT_REG_INDEX,
3011 
3012   /* A continuous range of registers, e.g. $s0-$s4.  */
3013   OT_REG_RANGE,
3014 
3015   /* A (possibly relocated) expression.  */
3016   OT_INTEGER,
3017 
3018   /* A floating-point value.  */
3019   OT_FLOAT,
3020 
3021   /* A single character.  This can be '(', ')' or ',', but '(' only appears
3022      before OT_REGs.  */
3023   OT_CHAR,
3024 
3025   /* A doubled character, either "--" or "++".  */
3026   OT_DOUBLE_CHAR,
3027 
3028   /* The end of the operand list.  */
3029   OT_END
3030 };
3031 
3032 /* A parsed operand token.  */
3033 struct mips_operand_token
3034 {
3035   /* The type of token.  */
3036   enum mips_operand_token_type type;
3037   union
3038   {
3039     /* The register symbol value for an OT_REG or OT_REG_INDEX.  */
3040     unsigned int regno;
3041 
3042     /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX.  */
3043     unsigned int channels;
3044 
3045     /* The integer value of an OT_INTEGER_INDEX.  */
3046     addressT index;
3047 
3048     /* The two register symbol values involved in an OT_REG_RANGE.  */
3049     struct {
3050       unsigned int regno1;
3051       unsigned int regno2;
3052     } reg_range;
3053 
3054     /* The value of an OT_INTEGER.  The value is represented as an
3055        expression and the relocation operators that were applied to
3056        that expression.  The reloc entries are BFD_RELOC_UNUSED if no
3057        relocation operators were used.  */
3058     struct {
3059       expressionS value;
3060       bfd_reloc_code_real_type relocs[3];
3061     } integer;
3062 
3063     /* The binary data for an OT_FLOAT constant, and the number of bytes
3064        in the constant.  */
3065     struct {
3066       unsigned char data[8];
3067       int length;
3068     } flt;
3069 
3070     /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR.  */
3071     char ch;
3072   } u;
3073 };
3074 
3075 /* An obstack used to construct lists of mips_operand_tokens.  */
3076 static struct obstack mips_operand_tokens;
3077 
3078 /* Give TOKEN type TYPE and add it to mips_operand_tokens.  */
3079 
3080 static void
3081 mips_add_token (struct mips_operand_token *token,
3082 		enum mips_operand_token_type type)
3083 {
3084   token->type = type;
3085   obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3086 }
3087 
3088 /* Check whether S is '(' followed by a register name.  Add OT_CHAR
3089    and OT_REG tokens for them if so, and return a pointer to the first
3090    unconsumed character.  Return null otherwise.  */
3091 
3092 static char *
3093 mips_parse_base_start (char *s)
3094 {
3095   struct mips_operand_token token;
3096   unsigned int regno, channels;
3097   bfd_boolean decrement_p;
3098 
3099   if (*s != '(')
3100     return 0;
3101 
3102   ++s;
3103   SKIP_SPACE_TABS (s);
3104 
3105   /* Only match "--" as part of a base expression.  In other contexts "--X"
3106      is a double negative.  */
3107   decrement_p = (s[0] == '-' && s[1] == '-');
3108   if (decrement_p)
3109     {
3110       s += 2;
3111       SKIP_SPACE_TABS (s);
3112     }
3113 
3114   /* Allow a channel specifier because that leads to better error messages
3115      than treating something like "$vf0x++" as an expression.  */
3116   if (!mips_parse_register (&s, &regno, &channels))
3117     return 0;
3118 
3119   token.u.ch = '(';
3120   mips_add_token (&token, OT_CHAR);
3121 
3122   if (decrement_p)
3123     {
3124       token.u.ch = '-';
3125       mips_add_token (&token, OT_DOUBLE_CHAR);
3126     }
3127 
3128   token.u.regno = regno;
3129   mips_add_token (&token, OT_REG);
3130 
3131   if (channels)
3132     {
3133       token.u.channels = channels;
3134       mips_add_token (&token, OT_CHANNELS);
3135     }
3136 
3137   /* For consistency, only match "++" as part of base expressions too.  */
3138   SKIP_SPACE_TABS (s);
3139   if (s[0] == '+' && s[1] == '+')
3140     {
3141       s += 2;
3142       token.u.ch = '+';
3143       mips_add_token (&token, OT_DOUBLE_CHAR);
3144     }
3145 
3146   return s;
3147 }
3148 
3149 /* Parse one or more tokens from S.  Return a pointer to the first
3150    unconsumed character on success.  Return null if an error was found
3151    and store the error text in insn_error.  FLOAT_FORMAT is as for
3152    mips_parse_arguments.  */
3153 
3154 static char *
3155 mips_parse_argument_token (char *s, char float_format)
3156 {
3157   char *end, *save_in;
3158   const char *err;
3159   unsigned int regno1, regno2, channels;
3160   struct mips_operand_token token;
3161 
3162   /* First look for "($reg", since we want to treat that as an
3163      OT_CHAR and OT_REG rather than an expression.  */
3164   end = mips_parse_base_start (s);
3165   if (end)
3166     return end;
3167 
3168   /* Handle other characters that end up as OT_CHARs.  */
3169   if (*s == ')' || *s == ',')
3170     {
3171       token.u.ch = *s;
3172       mips_add_token (&token, OT_CHAR);
3173       ++s;
3174       return s;
3175     }
3176 
3177   /* Handle tokens that start with a register.  */
3178   if (mips_parse_register (&s, &regno1, &channels))
3179     {
3180       if (channels)
3181 	{
3182 	  /* A register and a VU0 channel suffix.  */
3183 	  token.u.regno = regno1;
3184 	  mips_add_token (&token, OT_REG);
3185 
3186 	  token.u.channels = channels;
3187 	  mips_add_token (&token, OT_CHANNELS);
3188 	  return s;
3189 	}
3190 
3191       SKIP_SPACE_TABS (s);
3192       if (*s == '-')
3193 	{
3194 	  /* A register range.  */
3195 	  ++s;
3196 	  SKIP_SPACE_TABS (s);
3197 	  if (!mips_parse_register (&s, &regno2, NULL))
3198 	    {
3199 	      set_insn_error (0, _("invalid register range"));
3200 	      return 0;
3201 	    }
3202 
3203 	  token.u.reg_range.regno1 = regno1;
3204 	  token.u.reg_range.regno2 = regno2;
3205 	  mips_add_token (&token, OT_REG_RANGE);
3206 	  return s;
3207 	}
3208 
3209       /* Add the register itself.  */
3210       token.u.regno = regno1;
3211       mips_add_token (&token, OT_REG);
3212 
3213       /* Check for a vector index.  */
3214       if (*s == '[')
3215 	{
3216 	  ++s;
3217 	  SKIP_SPACE_TABS (s);
3218 	  if (mips_parse_register (&s, &token.u.regno, NULL))
3219 	    mips_add_token (&token, OT_REG_INDEX);
3220 	  else
3221 	    {
3222 	      expressionS element;
3223 
3224 	      my_getExpression (&element, s);
3225 	      if (element.X_op != O_constant)
3226 		{
3227 		  set_insn_error (0, _("vector element must be constant"));
3228 		  return 0;
3229 		}
3230 	      s = expr_end;
3231 	      token.u.index = element.X_add_number;
3232 	      mips_add_token (&token, OT_INTEGER_INDEX);
3233 	    }
3234 	  SKIP_SPACE_TABS (s);
3235 	  if (*s != ']')
3236 	    {
3237 	      set_insn_error (0, _("missing `]'"));
3238 	      return 0;
3239 	    }
3240 	  ++s;
3241 	}
3242       return s;
3243     }
3244 
3245   if (float_format)
3246     {
3247       /* First try to treat expressions as floats.  */
3248       save_in = input_line_pointer;
3249       input_line_pointer = s;
3250       err = md_atof (float_format, (char *) token.u.flt.data,
3251 		     &token.u.flt.length);
3252       end = input_line_pointer;
3253       input_line_pointer = save_in;
3254       if (err && *err)
3255 	{
3256 	  set_insn_error (0, err);
3257 	  return 0;
3258 	}
3259       if (s != end)
3260 	{
3261 	  mips_add_token (&token, OT_FLOAT);
3262 	  return end;
3263 	}
3264     }
3265 
3266   /* Treat everything else as an integer expression.  */
3267   token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3268   token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3269   token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3270   my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3271   s = expr_end;
3272   mips_add_token (&token, OT_INTEGER);
3273   return s;
3274 }
3275 
3276 /* S points to the operand list for an instruction.  FLOAT_FORMAT is 'f'
3277    if expressions should be treated as 32-bit floating-point constants,
3278    'd' if they should be treated as 64-bit floating-point constants,
3279    or 0 if they should be treated as integer expressions (the usual case).
3280 
3281    Return a list of tokens on success, otherwise return 0.  The caller
3282    must obstack_free the list after use.  */
3283 
3284 static struct mips_operand_token *
3285 mips_parse_arguments (char *s, char float_format)
3286 {
3287   struct mips_operand_token token;
3288 
3289   SKIP_SPACE_TABS (s);
3290   while (*s)
3291     {
3292       s = mips_parse_argument_token (s, float_format);
3293       if (!s)
3294 	{
3295 	  obstack_free (&mips_operand_tokens,
3296 			obstack_finish (&mips_operand_tokens));
3297 	  return 0;
3298 	}
3299       SKIP_SPACE_TABS (s);
3300     }
3301   mips_add_token (&token, OT_END);
3302   return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3303 }
3304 
3305 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3306    and architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
3307 
3308 static bfd_boolean
3309 is_opcode_valid (const struct mips_opcode *mo)
3310 {
3311   int isa = mips_opts.isa;
3312   int ase = mips_opts.ase;
3313   int fp_s, fp_d;
3314   unsigned int i;
3315 
3316   if (ISA_HAS_64BIT_REGS (isa))
3317     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3318       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3319 	ase |= mips_ases[i].flags64;
3320 
3321   if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3322     return FALSE;
3323 
3324   /* Check whether the instruction or macro requires single-precision or
3325      double-precision floating-point support.  Note that this information is
3326      stored differently in the opcode table for insns and macros.  */
3327   if (mo->pinfo == INSN_MACRO)
3328     {
3329       fp_s = mo->pinfo2 & INSN2_M_FP_S;
3330       fp_d = mo->pinfo2 & INSN2_M_FP_D;
3331     }
3332   else
3333     {
3334       fp_s = mo->pinfo & FP_S;
3335       fp_d = mo->pinfo & FP_D;
3336     }
3337 
3338   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3339     return FALSE;
3340 
3341   if (fp_s && mips_opts.soft_float)
3342     return FALSE;
3343 
3344   return TRUE;
3345 }
3346 
3347 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3348    selected ISA and architecture.  */
3349 
3350 static bfd_boolean
3351 is_opcode_valid_16 (const struct mips_opcode *mo)
3352 {
3353   int isa = mips_opts.isa;
3354   int ase = mips_opts.ase;
3355   unsigned int i;
3356 
3357   if (ISA_HAS_64BIT_REGS (isa))
3358     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3359       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3360 	ase |= mips_ases[i].flags64;
3361 
3362   return opcode_is_member (mo, isa, ase, mips_opts.arch);
3363 }
3364 
3365 /* Return TRUE if the size of the microMIPS opcode MO matches one
3366    explicitly requested.  Always TRUE in the standard MIPS mode.
3367    Use is_size_valid_16 for MIPS16 opcodes.  */
3368 
3369 static bfd_boolean
3370 is_size_valid (const struct mips_opcode *mo)
3371 {
3372   if (!mips_opts.micromips)
3373     return TRUE;
3374 
3375   if (mips_opts.insn32)
3376     {
3377       if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3378 	return FALSE;
3379       if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3380 	return FALSE;
3381     }
3382   if (!forced_insn_length)
3383     return TRUE;
3384   if (mo->pinfo == INSN_MACRO)
3385     return FALSE;
3386   return forced_insn_length == micromips_insn_length (mo);
3387 }
3388 
3389 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3390    explicitly requested.  */
3391 
3392 static bfd_boolean
3393 is_size_valid_16 (const struct mips_opcode *mo)
3394 {
3395   if (!forced_insn_length)
3396     return TRUE;
3397   if (mo->pinfo == INSN_MACRO)
3398     return FALSE;
3399   if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3400     return FALSE;
3401   if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3402     return FALSE;
3403   return TRUE;
3404 }
3405 
3406 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3407    of the preceding instruction.  Always TRUE in the standard MIPS mode.
3408 
3409    We don't accept macros in 16-bit delay slots to avoid a case where
3410    a macro expansion fails because it relies on a preceding 32-bit real
3411    instruction to have matched and does not handle the operands correctly.
3412    The only macros that may expand to 16-bit instructions are JAL that
3413    cannot be placed in a delay slot anyway, and corner cases of BALIGN
3414    and BGT (that likewise cannot be placed in a delay slot) that decay to
3415    a NOP.  In all these cases the macros precede any corresponding real
3416    instruction definitions in the opcode table, so they will match in the
3417    second pass where the size of the delay slot is ignored and therefore
3418    produce correct code.  */
3419 
3420 static bfd_boolean
3421 is_delay_slot_valid (const struct mips_opcode *mo)
3422 {
3423   if (!mips_opts.micromips)
3424     return TRUE;
3425 
3426   if (mo->pinfo == INSN_MACRO)
3427     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3428   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3429       && micromips_insn_length (mo) != 4)
3430     return FALSE;
3431   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3432       && micromips_insn_length (mo) != 2)
3433     return FALSE;
3434 
3435   return TRUE;
3436 }
3437 
3438 /* For consistency checking, verify that all bits of OPCODE are specified
3439    either by the match/mask part of the instruction definition, or by the
3440    operand list.  Also build up a list of operands in OPERANDS.
3441 
3442    INSN_BITS says which bits of the instruction are significant.
3443    If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3444    provides the mips_operand description of each operand.  DECODE_OPERAND
3445    is null for MIPS16 instructions.  */
3446 
3447 static int
3448 validate_mips_insn (const struct mips_opcode *opcode,
3449 		    unsigned long insn_bits,
3450 		    const struct mips_operand *(*decode_operand) (const char *),
3451 		    struct mips_operand_array *operands)
3452 {
3453   const char *s;
3454   unsigned long used_bits, doubled, undefined, opno, mask;
3455   const struct mips_operand *operand;
3456 
3457   mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3458   if ((mask & opcode->match) != opcode->match)
3459     {
3460       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3461 	      opcode->name, opcode->args);
3462       return 0;
3463     }
3464   used_bits = 0;
3465   opno = 0;
3466   if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3467     used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3468   for (s = opcode->args; *s; ++s)
3469     switch (*s)
3470       {
3471       case ',':
3472       case '(':
3473       case ')':
3474 	break;
3475 
3476       case '#':
3477 	s++;
3478 	break;
3479 
3480       default:
3481 	if (!decode_operand)
3482 	  operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3483 	else
3484 	  operand = decode_operand (s);
3485 	if (!operand && opcode->pinfo != INSN_MACRO)
3486 	  {
3487 	    as_bad (_("internal: unknown operand type: %s %s"),
3488 		    opcode->name, opcode->args);
3489 	    return 0;
3490 	  }
3491 	gas_assert (opno < MAX_OPERANDS);
3492 	operands->operand[opno] = operand;
3493 	if (!decode_operand && operand
3494 	    && operand->type == OP_INT && operand->lsb == 0
3495 	    && mips_opcode_32bit_p (opcode))
3496 	  used_bits |= mips16_immed_extend (-1, operand->size);
3497 	else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3498 	  {
3499 	    used_bits = mips_insert_operand (operand, used_bits, -1);
3500 	    if (operand->type == OP_MDMX_IMM_REG)
3501 	      /* Bit 5 is the format selector (OB vs QH).  The opcode table
3502 		 has separate entries for each format.  */
3503 	      used_bits &= ~(1 << (operand->lsb + 5));
3504 	    if (operand->type == OP_ENTRY_EXIT_LIST)
3505 	      used_bits &= ~(mask & 0x700);
3506 	    /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3507 	       operand field that cannot be fully described with LSB/SIZE.  */
3508 	    if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3509 	      used_bits &= ~0x6000;
3510 	  }
3511 	/* Skip prefix characters.  */
3512 	if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3513 	  ++s;
3514 	opno += 1;
3515 	break;
3516       }
3517   doubled = used_bits & mask & insn_bits;
3518   if (doubled)
3519     {
3520       as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3521 		" %s %s"), doubled, opcode->name, opcode->args);
3522       return 0;
3523     }
3524   used_bits |= mask;
3525   undefined = ~used_bits & insn_bits;
3526   if (opcode->pinfo != INSN_MACRO && undefined)
3527     {
3528       as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3529 	      undefined, opcode->name, opcode->args);
3530       return 0;
3531     }
3532   used_bits &= ~insn_bits;
3533   if (used_bits)
3534     {
3535       as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3536 	      used_bits, opcode->name, opcode->args);
3537       return 0;
3538     }
3539   return 1;
3540 }
3541 
3542 /* The MIPS16 version of validate_mips_insn.  */
3543 
3544 static int
3545 validate_mips16_insn (const struct mips_opcode *opcode,
3546 		      struct mips_operand_array *operands)
3547 {
3548   unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3549 
3550   return validate_mips_insn (opcode, insn_bits, 0, operands);
3551 }
3552 
3553 /* The microMIPS version of validate_mips_insn.  */
3554 
3555 static int
3556 validate_micromips_insn (const struct mips_opcode *opc,
3557 			 struct mips_operand_array *operands)
3558 {
3559   unsigned long insn_bits;
3560   unsigned long major;
3561   unsigned int length;
3562 
3563   if (opc->pinfo == INSN_MACRO)
3564     return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3565 			       operands);
3566 
3567   length = micromips_insn_length (opc);
3568   if (length != 2 && length != 4)
3569     {
3570       as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3571 		"%s %s"), length, opc->name, opc->args);
3572       return 0;
3573     }
3574   major = opc->match >> (10 + 8 * (length - 2));
3575   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3576       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3577     {
3578       as_bad (_("internal error: bad microMIPS opcode "
3579 		"(opcode/length mismatch): %s %s"), opc->name, opc->args);
3580       return 0;
3581     }
3582 
3583   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
3584   insn_bits = 1 << 4 * length;
3585   insn_bits <<= 4 * length;
3586   insn_bits -= 1;
3587   return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3588 			     operands);
3589 }
3590 
3591 /* This function is called once, at assembler startup time.  It should set up
3592    all the tables, etc. that the MD part of the assembler will need.  */
3593 
3594 void
3595 md_begin (void)
3596 {
3597   const char *retval = NULL;
3598   int i = 0;
3599   int broken = 0;
3600 
3601   if (mips_pic != NO_PIC)
3602     {
3603       if (g_switch_seen && g_switch_value != 0)
3604 	as_bad (_("-G may not be used in position-independent code"));
3605       g_switch_value = 0;
3606     }
3607   else if (mips_abicalls)
3608     {
3609       if (g_switch_seen && g_switch_value != 0)
3610 	as_bad (_("-G may not be used with abicalls"));
3611       g_switch_value = 0;
3612     }
3613 
3614   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3615     as_warn (_("could not set architecture and machine"));
3616 
3617   op_hash = hash_new ();
3618 
3619   mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3620   for (i = 0; i < NUMOPCODES;)
3621     {
3622       const char *name = mips_opcodes[i].name;
3623 
3624       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
3625       if (retval != NULL)
3626 	{
3627 	  fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3628 		   mips_opcodes[i].name, retval);
3629 	  /* Probably a memory allocation problem?  Give up now.  */
3630 	  as_fatal (_("broken assembler, no assembly attempted"));
3631 	}
3632       do
3633 	{
3634 	  if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3635 				   decode_mips_operand, &mips_operands[i]))
3636 	    broken = 1;
3637 	  if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3638 	    {
3639 	      create_insn (&nop_insn, mips_opcodes + i);
3640 	      if (mips_fix_loongson2f_nop)
3641 		nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3642 	      nop_insn.fixed_p = 1;
3643 	    }
3644 	  ++i;
3645 	}
3646       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3647     }
3648 
3649   mips16_op_hash = hash_new ();
3650   mips16_operands = XCNEWVEC (struct mips_operand_array,
3651 			      bfd_mips16_num_opcodes);
3652 
3653   i = 0;
3654   while (i < bfd_mips16_num_opcodes)
3655     {
3656       const char *name = mips16_opcodes[i].name;
3657 
3658       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
3659       if (retval != NULL)
3660 	as_fatal (_("internal: can't hash `%s': %s"),
3661 		  mips16_opcodes[i].name, retval);
3662       do
3663 	{
3664 	  if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3665 	    broken = 1;
3666 	  if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3667 	    {
3668 	      create_insn (&mips16_nop_insn, mips16_opcodes + i);
3669 	      mips16_nop_insn.fixed_p = 1;
3670 	    }
3671 	  ++i;
3672 	}
3673       while (i < bfd_mips16_num_opcodes
3674 	     && strcmp (mips16_opcodes[i].name, name) == 0);
3675     }
3676 
3677   micromips_op_hash = hash_new ();
3678   micromips_operands = XCNEWVEC (struct mips_operand_array,
3679 				 bfd_micromips_num_opcodes);
3680 
3681   i = 0;
3682   while (i < bfd_micromips_num_opcodes)
3683     {
3684       const char *name = micromips_opcodes[i].name;
3685 
3686       retval = hash_insert (micromips_op_hash, name,
3687 			    (void *) &micromips_opcodes[i]);
3688       if (retval != NULL)
3689 	as_fatal (_("internal: can't hash `%s': %s"),
3690 		  micromips_opcodes[i].name, retval);
3691       do
3692 	{
3693 	  struct mips_cl_insn *micromips_nop_insn;
3694 
3695 	  if (!validate_micromips_insn (&micromips_opcodes[i],
3696 					&micromips_operands[i]))
3697 	    broken = 1;
3698 
3699 	  if (micromips_opcodes[i].pinfo != INSN_MACRO)
3700 	    {
3701 	      if (micromips_insn_length (micromips_opcodes + i) == 2)
3702 		micromips_nop_insn = &micromips_nop16_insn;
3703 	      else if (micromips_insn_length (micromips_opcodes + i) == 4)
3704 		micromips_nop_insn = &micromips_nop32_insn;
3705 	      else
3706 		continue;
3707 
3708 	      if (micromips_nop_insn->insn_mo == NULL
3709 		  && strcmp (name, "nop") == 0)
3710 		{
3711 		  create_insn (micromips_nop_insn, micromips_opcodes + i);
3712 		  micromips_nop_insn->fixed_p = 1;
3713 		}
3714 	    }
3715 	}
3716       while (++i < bfd_micromips_num_opcodes
3717 	     && strcmp (micromips_opcodes[i].name, name) == 0);
3718     }
3719 
3720   if (broken)
3721     as_fatal (_("broken assembler, no assembly attempted"));
3722 
3723   /* We add all the general register names to the symbol table.  This
3724      helps us detect invalid uses of them.  */
3725   for (i = 0; reg_names[i].name; i++)
3726     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3727 				     reg_names[i].num, /* & RNUM_MASK, */
3728 				     &zero_address_frag));
3729   if (HAVE_NEWABI)
3730     for (i = 0; reg_names_n32n64[i].name; i++)
3731       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3732 				       reg_names_n32n64[i].num, /* & RNUM_MASK, */
3733 				       &zero_address_frag));
3734   else
3735     for (i = 0; reg_names_o32[i].name; i++)
3736       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3737 				       reg_names_o32[i].num, /* & RNUM_MASK, */
3738 				       &zero_address_frag));
3739 
3740   for (i = 0; i < 32; i++)
3741     {
3742       char regname[6];
3743 
3744       /* R5900 VU0 floating-point register.  */
3745       sprintf (regname, "$vf%d", i);
3746       symbol_table_insert (symbol_new (regname, reg_section,
3747 				       RTYPE_VF | i, &zero_address_frag));
3748 
3749       /* R5900 VU0 integer register.  */
3750       sprintf (regname, "$vi%d", i);
3751       symbol_table_insert (symbol_new (regname, reg_section,
3752 				       RTYPE_VI | i, &zero_address_frag));
3753 
3754       /* MSA register.  */
3755       sprintf (regname, "$w%d", i);
3756       symbol_table_insert (symbol_new (regname, reg_section,
3757 				       RTYPE_MSA | i, &zero_address_frag));
3758     }
3759 
3760   obstack_init (&mips_operand_tokens);
3761 
3762   mips_no_prev_insn ();
3763 
3764   mips_gprmask = 0;
3765   mips_cprmask[0] = 0;
3766   mips_cprmask[1] = 0;
3767   mips_cprmask[2] = 0;
3768   mips_cprmask[3] = 0;
3769 
3770   /* set the default alignment for the text section (2**2) */
3771   record_alignment (text_section, 2);
3772 
3773   bfd_set_gp_size (stdoutput, g_switch_value);
3774 
3775   /* On a native system other than VxWorks, sections must be aligned
3776      to 16 byte boundaries.  When configured for an embedded ELF
3777      target, we don't bother.  */
3778   if (strncmp (TARGET_OS, "elf", 3) != 0
3779       && strncmp (TARGET_OS, "vxworks", 7) != 0)
3780     {
3781       (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3782       (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3783       (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3784     }
3785 
3786   /* Create a .reginfo section for register masks and a .mdebug
3787      section for debugging information.  */
3788   {
3789     segT seg;
3790     subsegT subseg;
3791     flagword flags;
3792     segT sec;
3793 
3794     seg = now_seg;
3795     subseg = now_subseg;
3796 
3797     /* The ABI says this section should be loaded so that the
3798        running program can access it.  However, we don't load it
3799        if we are configured for an embedded target */
3800     flags = SEC_READONLY | SEC_DATA;
3801     if (strncmp (TARGET_OS, "elf", 3) != 0)
3802       flags |= SEC_ALLOC | SEC_LOAD;
3803 
3804     if (mips_abi != N64_ABI)
3805       {
3806 	sec = subseg_new (".reginfo", (subsegT) 0);
3807 
3808 	bfd_set_section_flags (stdoutput, sec, flags);
3809 	bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
3810 
3811 	mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3812       }
3813     else
3814       {
3815 	/* The 64-bit ABI uses a .MIPS.options section rather than
3816 	   .reginfo section.  */
3817 	sec = subseg_new (".MIPS.options", (subsegT) 0);
3818 	bfd_set_section_flags (stdoutput, sec, flags);
3819 	bfd_set_section_alignment (stdoutput, sec, 3);
3820 
3821 	/* Set up the option header.  */
3822 	{
3823 	  Elf_Internal_Options opthdr;
3824 	  char *f;
3825 
3826 	  opthdr.kind = ODK_REGINFO;
3827 	  opthdr.size = (sizeof (Elf_External_Options)
3828 			 + sizeof (Elf64_External_RegInfo));
3829 	  opthdr.section = 0;
3830 	  opthdr.info = 0;
3831 	  f = frag_more (sizeof (Elf_External_Options));
3832 	  bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3833 					 (Elf_External_Options *) f);
3834 
3835 	  mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3836 	}
3837       }
3838 
3839     sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3840     bfd_set_section_flags (stdoutput, sec,
3841 			   SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3842     bfd_set_section_alignment (stdoutput, sec, 3);
3843     mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3844 
3845     if (ECOFF_DEBUGGING)
3846       {
3847 	sec = subseg_new (".mdebug", (subsegT) 0);
3848 	(void) bfd_set_section_flags (stdoutput, sec,
3849 				      SEC_HAS_CONTENTS | SEC_READONLY);
3850 	(void) bfd_set_section_alignment (stdoutput, sec, 2);
3851       }
3852     else if (mips_flag_pdr)
3853       {
3854 	pdr_seg = subseg_new (".pdr", (subsegT) 0);
3855 	(void) bfd_set_section_flags (stdoutput, pdr_seg,
3856 				      SEC_READONLY | SEC_RELOC
3857 				      | SEC_DEBUGGING);
3858 	(void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3859       }
3860 
3861     subseg_set (seg, subseg);
3862   }
3863 
3864   if (mips_fix_vr4120)
3865     init_vr4120_conflicts ();
3866 }
3867 
3868 static inline void
3869 fpabi_incompatible_with (int fpabi, const char *what)
3870 {
3871   as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3872 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3873 }
3874 
3875 static inline void
3876 fpabi_requires (int fpabi, const char *what)
3877 {
3878   as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3879 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3880 }
3881 
3882 /* Check -mabi and register sizes against the specified FP ABI.  */
3883 static void
3884 check_fpabi (int fpabi)
3885 {
3886   switch (fpabi)
3887     {
3888     case Val_GNU_MIPS_ABI_FP_DOUBLE:
3889       if (file_mips_opts.soft_float)
3890 	fpabi_incompatible_with (fpabi, "softfloat");
3891       else if (file_mips_opts.single_float)
3892 	fpabi_incompatible_with (fpabi, "singlefloat");
3893       if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3894 	fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3895       else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3896 	fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3897       break;
3898 
3899     case Val_GNU_MIPS_ABI_FP_XX:
3900       if (mips_abi != O32_ABI)
3901 	fpabi_requires (fpabi, "-mabi=32");
3902       else if (file_mips_opts.soft_float)
3903 	fpabi_incompatible_with (fpabi, "softfloat");
3904       else if (file_mips_opts.single_float)
3905 	fpabi_incompatible_with (fpabi, "singlefloat");
3906       else if (file_mips_opts.fp != 0)
3907 	fpabi_requires (fpabi, "fp=xx");
3908       break;
3909 
3910     case Val_GNU_MIPS_ABI_FP_64A:
3911     case Val_GNU_MIPS_ABI_FP_64:
3912       if (mips_abi != O32_ABI)
3913 	fpabi_requires (fpabi, "-mabi=32");
3914       else if (file_mips_opts.soft_float)
3915 	fpabi_incompatible_with (fpabi, "softfloat");
3916       else if (file_mips_opts.single_float)
3917 	fpabi_incompatible_with (fpabi, "singlefloat");
3918       else if (file_mips_opts.fp != 64)
3919 	fpabi_requires (fpabi, "fp=64");
3920       else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3921 	fpabi_incompatible_with (fpabi, "nooddspreg");
3922       else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3923 	fpabi_requires (fpabi, "nooddspreg");
3924       break;
3925 
3926     case Val_GNU_MIPS_ABI_FP_SINGLE:
3927       if (file_mips_opts.soft_float)
3928 	fpabi_incompatible_with (fpabi, "softfloat");
3929       else if (!file_mips_opts.single_float)
3930 	fpabi_requires (fpabi, "singlefloat");
3931       break;
3932 
3933     case Val_GNU_MIPS_ABI_FP_SOFT:
3934       if (!file_mips_opts.soft_float)
3935 	fpabi_requires (fpabi, "softfloat");
3936       break;
3937 
3938     case Val_GNU_MIPS_ABI_FP_OLD_64:
3939       as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3940 	       Tag_GNU_MIPS_ABI_FP, fpabi);
3941       break;
3942 
3943     case Val_GNU_MIPS_ABI_FP_NAN2008:
3944       /* Silently ignore compatibility value.  */
3945       break;
3946 
3947     default:
3948       as_warn (_(".gnu_attribute %d,%d is not a recognized"
3949 	         " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3950       break;
3951     }
3952 }
3953 
3954 /* Perform consistency checks on the current options.  */
3955 
3956 static void
3957 mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3958 {
3959   /* Check the size of integer registers agrees with the ABI and ISA.  */
3960   if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3961     as_bad (_("`gp=64' used with a 32-bit processor"));
3962   else if (abi_checks
3963 	   && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3964     as_bad (_("`gp=32' used with a 64-bit ABI"));
3965   else if (abi_checks
3966 	   && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3967     as_bad (_("`gp=64' used with a 32-bit ABI"));
3968 
3969   /* Check the size of the float registers agrees with the ABI and ISA.  */
3970   switch (opts->fp)
3971     {
3972     case 0:
3973       if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3974 	as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3975       else if (opts->single_float == 1)
3976 	as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3977       break;
3978     case 64:
3979       if (!ISA_HAS_64BIT_FPRS (opts->isa))
3980 	as_bad (_("`fp=64' used with a 32-bit fpu"));
3981       else if (abi_checks
3982 	       && ABI_NEEDS_32BIT_REGS (mips_abi)
3983 	       && !ISA_HAS_MXHC1 (opts->isa))
3984 	as_warn (_("`fp=64' used with a 32-bit ABI"));
3985       break;
3986     case 32:
3987       if (abi_checks
3988 	  && ABI_NEEDS_64BIT_REGS (mips_abi))
3989 	as_warn (_("`fp=32' used with a 64-bit ABI"));
3990       if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
3991 	as_bad (_("`fp=32' used with a MIPS R6 cpu"));
3992       break;
3993     default:
3994       as_bad (_("Unknown size of floating point registers"));
3995       break;
3996     }
3997 
3998   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3999     as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4000 
4001   if (opts->micromips == 1 && opts->mips16 == 1)
4002     as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
4003   else if (ISA_IS_R6 (opts->isa)
4004 	   && (opts->micromips == 1
4005 	       || opts->mips16 == 1))
4006     as_fatal (_("`%s' cannot be used with `%s'"),
4007 	      opts->micromips ? "micromips" : "mips16",
4008 	      mips_cpu_info_from_isa (opts->isa)->name);
4009 
4010   if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4011     as_fatal (_("branch relaxation is not supported in `%s'"),
4012 	      mips_cpu_info_from_isa (opts->isa)->name);
4013 }
4014 
4015 /* Perform consistency checks on the module level options exactly once.
4016    This is a deferred check that happens:
4017      at the first .set directive
4018      or, at the first pseudo op that generates code (inc .dc.a)
4019      or, at the first instruction
4020      or, at the end.  */
4021 
4022 static void
4023 file_mips_check_options (void)
4024 {
4025   const struct mips_cpu_info *arch_info = 0;
4026 
4027   if (file_mips_opts_checked)
4028     return;
4029 
4030   /* The following code determines the register size.
4031      Similar code was added to GCC 3.3 (see override_options() in
4032      config/mips/mips.c).  The GAS and GCC code should be kept in sync
4033      as much as possible.  */
4034 
4035   if (file_mips_opts.gp < 0)
4036     {
4037       /* Infer the integer register size from the ABI and processor.
4038 	 Restrict ourselves to 32-bit registers if that's all the
4039 	 processor has, or if the ABI cannot handle 64-bit registers.  */
4040       file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4041 			   || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4042 			  ? 32 : 64;
4043     }
4044 
4045   if (file_mips_opts.fp < 0)
4046     {
4047       /* No user specified float register size.
4048 	 ??? GAS treats single-float processors as though they had 64-bit
4049 	 float registers (although it complains when double-precision
4050 	 instructions are used).  As things stand, saying they have 32-bit
4051 	 registers would lead to spurious "register must be even" messages.
4052 	 So here we assume float registers are never smaller than the
4053 	 integer ones.  */
4054       if (file_mips_opts.gp == 64)
4055 	/* 64-bit integer registers implies 64-bit float registers.  */
4056 	file_mips_opts.fp = 64;
4057       else if ((file_mips_opts.ase & FP64_ASES)
4058 	       && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4059 	/* Handle ASEs that require 64-bit float registers, if possible.  */
4060 	file_mips_opts.fp = 64;
4061       else if (ISA_IS_R6 (mips_opts.isa))
4062 	/* R6 implies 64-bit float registers.  */
4063 	file_mips_opts.fp = 64;
4064       else
4065 	/* 32-bit float registers.  */
4066 	file_mips_opts.fp = 32;
4067     }
4068 
4069   arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4070 
4071   /* Disable operations on odd-numbered floating-point registers by default
4072      when using the FPXX ABI.  */
4073   if (file_mips_opts.oddspreg < 0)
4074     {
4075       if (file_mips_opts.fp == 0)
4076 	file_mips_opts.oddspreg = 0;
4077       else
4078 	file_mips_opts.oddspreg = 1;
4079     }
4080 
4081   /* End of GCC-shared inference code.  */
4082 
4083   /* This flag is set when we have a 64-bit capable CPU but use only
4084      32-bit wide registers.  Note that EABI does not use it.  */
4085   if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4086       && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4087 	  || mips_abi == O32_ABI))
4088     mips_32bitmode = 1;
4089 
4090   if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4091     as_bad (_("trap exception not supported at ISA 1"));
4092 
4093   /* If the selected architecture includes support for ASEs, enable
4094      generation of code for them.  */
4095   if (file_mips_opts.mips16 == -1)
4096     file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4097   if (file_mips_opts.micromips == -1)
4098     file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4099 				? 1 : 0;
4100 
4101   if (mips_nan2008 == -1)
4102     mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4103   else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4104     as_fatal (_("`%s' does not support legacy NaN"),
4105 	      mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4106 
4107   /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4108      being selected implicitly.  */
4109   if (file_mips_opts.fp != 64)
4110     file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4111 
4112   /* If the user didn't explicitly select or deselect a particular ASE,
4113      use the default setting for the CPU.  */
4114   file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4115 
4116   /* Set up the current options.  These may change throughout assembly.  */
4117   mips_opts = file_mips_opts;
4118 
4119   mips_check_isa_supports_ases ();
4120   mips_check_options (&file_mips_opts, TRUE);
4121   file_mips_opts_checked = TRUE;
4122 
4123   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4124     as_warn (_("could not set architecture and machine"));
4125 }
4126 
4127 void
4128 md_assemble (char *str)
4129 {
4130   struct mips_cl_insn insn;
4131   bfd_reloc_code_real_type unused_reloc[3]
4132     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4133 
4134   file_mips_check_options ();
4135 
4136   imm_expr.X_op = O_absent;
4137   offset_expr.X_op = O_absent;
4138   offset_reloc[0] = BFD_RELOC_UNUSED;
4139   offset_reloc[1] = BFD_RELOC_UNUSED;
4140   offset_reloc[2] = BFD_RELOC_UNUSED;
4141 
4142   mips_mark_labels ();
4143   mips_assembling_insn = TRUE;
4144   clear_insn_error ();
4145 
4146   if (mips_opts.mips16)
4147     mips16_ip (str, &insn);
4148   else
4149     {
4150       mips_ip (str, &insn);
4151       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4152 	    str, insn.insn_opcode));
4153     }
4154 
4155   if (insn_error.msg)
4156     report_insn_error (str);
4157   else if (insn.insn_mo->pinfo == INSN_MACRO)
4158     {
4159       macro_start ();
4160       if (mips_opts.mips16)
4161 	mips16_macro (&insn);
4162       else
4163 	macro (&insn, str);
4164       macro_end ();
4165     }
4166   else
4167     {
4168       if (offset_expr.X_op != O_absent)
4169 	append_insn (&insn, &offset_expr, offset_reloc, FALSE);
4170       else
4171 	append_insn (&insn, NULL, unused_reloc, FALSE);
4172     }
4173 
4174   mips_assembling_insn = FALSE;
4175 }
4176 
4177 /* Convenience functions for abstracting away the differences between
4178    MIPS16 and non-MIPS16 relocations.  */
4179 
4180 static inline bfd_boolean
4181 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4182 {
4183   switch (reloc)
4184     {
4185     case BFD_RELOC_MIPS16_JMP:
4186     case BFD_RELOC_MIPS16_GPREL:
4187     case BFD_RELOC_MIPS16_GOT16:
4188     case BFD_RELOC_MIPS16_CALL16:
4189     case BFD_RELOC_MIPS16_HI16_S:
4190     case BFD_RELOC_MIPS16_HI16:
4191     case BFD_RELOC_MIPS16_LO16:
4192     case BFD_RELOC_MIPS16_16_PCREL_S1:
4193       return TRUE;
4194 
4195     default:
4196       return FALSE;
4197     }
4198 }
4199 
4200 static inline bfd_boolean
4201 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4202 {
4203   switch (reloc)
4204     {
4205     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4206     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4207     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4208     case BFD_RELOC_MICROMIPS_GPREL16:
4209     case BFD_RELOC_MICROMIPS_JMP:
4210     case BFD_RELOC_MICROMIPS_HI16:
4211     case BFD_RELOC_MICROMIPS_HI16_S:
4212     case BFD_RELOC_MICROMIPS_LO16:
4213     case BFD_RELOC_MICROMIPS_LITERAL:
4214     case BFD_RELOC_MICROMIPS_GOT16:
4215     case BFD_RELOC_MICROMIPS_CALL16:
4216     case BFD_RELOC_MICROMIPS_GOT_HI16:
4217     case BFD_RELOC_MICROMIPS_GOT_LO16:
4218     case BFD_RELOC_MICROMIPS_CALL_HI16:
4219     case BFD_RELOC_MICROMIPS_CALL_LO16:
4220     case BFD_RELOC_MICROMIPS_SUB:
4221     case BFD_RELOC_MICROMIPS_GOT_PAGE:
4222     case BFD_RELOC_MICROMIPS_GOT_OFST:
4223     case BFD_RELOC_MICROMIPS_GOT_DISP:
4224     case BFD_RELOC_MICROMIPS_HIGHEST:
4225     case BFD_RELOC_MICROMIPS_HIGHER:
4226     case BFD_RELOC_MICROMIPS_SCN_DISP:
4227     case BFD_RELOC_MICROMIPS_JALR:
4228       return TRUE;
4229 
4230     default:
4231       return FALSE;
4232     }
4233 }
4234 
4235 static inline bfd_boolean
4236 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4237 {
4238   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4239 }
4240 
4241 static inline bfd_boolean
4242 b_reloc_p (bfd_reloc_code_real_type reloc)
4243 {
4244   return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4245 	  || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4246 	  || reloc == BFD_RELOC_16_PCREL_S2
4247 	  || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4248 	  || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4249 	  || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4250 	  || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4251 }
4252 
4253 static inline bfd_boolean
4254 got16_reloc_p (bfd_reloc_code_real_type reloc)
4255 {
4256   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4257 	  || reloc == BFD_RELOC_MICROMIPS_GOT16);
4258 }
4259 
4260 static inline bfd_boolean
4261 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4262 {
4263   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4264 	  || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4265 }
4266 
4267 static inline bfd_boolean
4268 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4269 {
4270   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4271 	  || reloc == BFD_RELOC_MICROMIPS_LO16);
4272 }
4273 
4274 static inline bfd_boolean
4275 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4276 {
4277   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4278 }
4279 
4280 static inline bfd_boolean
4281 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4282 {
4283   return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4284 	  || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4285 }
4286 
4287 /* Return true if RELOC is a PC-relative relocation that does not have
4288    full address range.  */
4289 
4290 static inline bfd_boolean
4291 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4292 {
4293   switch (reloc)
4294     {
4295     case BFD_RELOC_16_PCREL_S2:
4296     case BFD_RELOC_MIPS16_16_PCREL_S1:
4297     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4298     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4299     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4300     case BFD_RELOC_MIPS_21_PCREL_S2:
4301     case BFD_RELOC_MIPS_26_PCREL_S2:
4302     case BFD_RELOC_MIPS_18_PCREL_S3:
4303     case BFD_RELOC_MIPS_19_PCREL_S2:
4304       return TRUE;
4305 
4306     case BFD_RELOC_32_PCREL:
4307     case BFD_RELOC_HI16_S_PCREL:
4308     case BFD_RELOC_LO16_PCREL:
4309       return HAVE_64BIT_ADDRESSES;
4310 
4311     default:
4312       return FALSE;
4313     }
4314 }
4315 
4316 /* Return true if the given relocation might need a matching %lo().
4317    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4318    need a matching %lo() when applied to local symbols.  */
4319 
4320 static inline bfd_boolean
4321 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4322 {
4323   return (HAVE_IN_PLACE_ADDENDS
4324 	  && (hi16_reloc_p (reloc)
4325 	      /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4326 		 all GOT16 relocations evaluate to "G".  */
4327 	      || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4328 }
4329 
4330 /* Return the type of %lo() reloc needed by RELOC, given that
4331    reloc_needs_lo_p.  */
4332 
4333 static inline bfd_reloc_code_real_type
4334 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4335 {
4336   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4337 	  : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4338 	     : BFD_RELOC_LO16));
4339 }
4340 
4341 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4342    relocation.  */
4343 
4344 static inline bfd_boolean
4345 fixup_has_matching_lo_p (fixS *fixp)
4346 {
4347   return (fixp->fx_next != NULL
4348 	  && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4349 	  && fixp->fx_addsy == fixp->fx_next->fx_addsy
4350 	  && fixp->fx_offset == fixp->fx_next->fx_offset);
4351 }
4352 
4353 /* Move all labels in LABELS to the current insertion point.  TEXT_P
4354    says whether the labels refer to text or data.  */
4355 
4356 static void
4357 mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
4358 {
4359   struct insn_label_list *l;
4360   valueT val;
4361 
4362   for (l = labels; l != NULL; l = l->next)
4363     {
4364       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4365       symbol_set_frag (l->label, frag_now);
4366       val = (valueT) frag_now_fix ();
4367       /* MIPS16/microMIPS text labels are stored as odd.  */
4368       if (text_p && HAVE_CODE_COMPRESSION)
4369 	++val;
4370       S_SET_VALUE (l->label, val);
4371     }
4372 }
4373 
4374 /* Move all labels in insn_labels to the current insertion point
4375    and treat them as text labels.  */
4376 
4377 static void
4378 mips_move_text_labels (void)
4379 {
4380   mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4381 }
4382 
4383 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'.  */
4384 
4385 static bfd_boolean
4386 s_is_linkonce (symbolS *sym, segT from_seg)
4387 {
4388   bfd_boolean linkonce = FALSE;
4389   segT symseg = S_GET_SEGMENT (sym);
4390 
4391   if (symseg != from_seg && !S_IS_LOCAL (sym))
4392     {
4393       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4394 	linkonce = TRUE;
4395       /* The GNU toolchain uses an extension for ELF: a section
4396 	 beginning with the magic string .gnu.linkonce is a
4397 	 linkonce section.  */
4398       if (strncmp (segment_name (symseg), ".gnu.linkonce",
4399 		   sizeof ".gnu.linkonce" - 1) == 0)
4400 	linkonce = TRUE;
4401     }
4402   return linkonce;
4403 }
4404 
4405 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
4406    linker to handle them specially, such as generating jalx instructions
4407    when needed.  We also make them odd for the duration of the assembly,
4408    in order to generate the right sort of code.  We will make them even
4409    in the adjust_symtab routine, while leaving them marked.  This is
4410    convenient for the debugger and the disassembler.  The linker knows
4411    to make them odd again.  */
4412 
4413 static void
4414 mips_compressed_mark_label (symbolS *label)
4415 {
4416   gas_assert (HAVE_CODE_COMPRESSION);
4417 
4418   if (mips_opts.mips16)
4419     S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4420   else
4421     S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4422   if ((S_GET_VALUE (label) & 1) == 0
4423       /* Don't adjust the address if the label is global or weak, or
4424 	 in a link-once section, since we'll be emitting symbol reloc
4425 	 references to it which will be patched up by the linker, and
4426 	 the final value of the symbol may or may not be MIPS16/microMIPS.  */
4427       && !S_IS_WEAK (label)
4428       && !S_IS_EXTERNAL (label)
4429       && !s_is_linkonce (label, now_seg))
4430     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4431 }
4432 
4433 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
4434 
4435 static void
4436 mips_compressed_mark_labels (void)
4437 {
4438   struct insn_label_list *l;
4439 
4440   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4441     mips_compressed_mark_label (l->label);
4442 }
4443 
4444 /* End the current frag.  Make it a variant frag and record the
4445    relaxation info.  */
4446 
4447 static void
4448 relax_close_frag (void)
4449 {
4450   mips_macro_warning.first_frag = frag_now;
4451   frag_var (rs_machine_dependent, 0, 0,
4452 	    RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4453 			  mips_pic != NO_PIC),
4454 	    mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4455 
4456   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4457   mips_relax.first_fixup = 0;
4458 }
4459 
4460 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4461    See the comment above RELAX_ENCODE for more details.  */
4462 
4463 static void
4464 relax_start (symbolS *symbol)
4465 {
4466   gas_assert (mips_relax.sequence == 0);
4467   mips_relax.sequence = 1;
4468   mips_relax.symbol = symbol;
4469 }
4470 
4471 /* Start generating the second version of a relaxable sequence.
4472    See the comment above RELAX_ENCODE for more details.  */
4473 
4474 static void
4475 relax_switch (void)
4476 {
4477   gas_assert (mips_relax.sequence == 1);
4478   mips_relax.sequence = 2;
4479 }
4480 
4481 /* End the current relaxable sequence.  */
4482 
4483 static void
4484 relax_end (void)
4485 {
4486   gas_assert (mips_relax.sequence == 2);
4487   relax_close_frag ();
4488   mips_relax.sequence = 0;
4489 }
4490 
4491 /* Return true if IP is a delayed branch or jump.  */
4492 
4493 static inline bfd_boolean
4494 delayed_branch_p (const struct mips_cl_insn *ip)
4495 {
4496   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4497 				| INSN_COND_BRANCH_DELAY
4498 				| INSN_COND_BRANCH_LIKELY)) != 0;
4499 }
4500 
4501 /* Return true if IP is a compact branch or jump.  */
4502 
4503 static inline bfd_boolean
4504 compact_branch_p (const struct mips_cl_insn *ip)
4505 {
4506   return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4507 				 | INSN2_COND_BRANCH)) != 0;
4508 }
4509 
4510 /* Return true if IP is an unconditional branch or jump.  */
4511 
4512 static inline bfd_boolean
4513 uncond_branch_p (const struct mips_cl_insn *ip)
4514 {
4515   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4516 	  || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4517 }
4518 
4519 /* Return true if IP is a branch-likely instruction.  */
4520 
4521 static inline bfd_boolean
4522 branch_likely_p (const struct mips_cl_insn *ip)
4523 {
4524   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4525 }
4526 
4527 /* Return the type of nop that should be used to fill the delay slot
4528    of delayed branch IP.  */
4529 
4530 static struct mips_cl_insn *
4531 get_delay_slot_nop (const struct mips_cl_insn *ip)
4532 {
4533   if (mips_opts.micromips
4534       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4535     return &micromips_nop32_insn;
4536   return NOP_INSN;
4537 }
4538 
4539 /* Return a mask that has bit N set if OPCODE reads the register(s)
4540    in operand N.  */
4541 
4542 static unsigned int
4543 insn_read_mask (const struct mips_opcode *opcode)
4544 {
4545   return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4546 }
4547 
4548 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4549    in operand N.  */
4550 
4551 static unsigned int
4552 insn_write_mask (const struct mips_opcode *opcode)
4553 {
4554   return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4555 }
4556 
4557 /* Return a mask of the registers specified by operand OPERAND of INSN.
4558    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4559    is set.  */
4560 
4561 static unsigned int
4562 operand_reg_mask (const struct mips_cl_insn *insn,
4563 		  const struct mips_operand *operand,
4564 		  unsigned int type_mask)
4565 {
4566   unsigned int uval, vsel;
4567 
4568   switch (operand->type)
4569     {
4570     case OP_INT:
4571     case OP_MAPPED_INT:
4572     case OP_MSB:
4573     case OP_PCREL:
4574     case OP_PERF_REG:
4575     case OP_ADDIUSP_INT:
4576     case OP_ENTRY_EXIT_LIST:
4577     case OP_REPEAT_DEST_REG:
4578     case OP_REPEAT_PREV_REG:
4579     case OP_PC:
4580     case OP_VU0_SUFFIX:
4581     case OP_VU0_MATCH_SUFFIX:
4582     case OP_IMM_INDEX:
4583       abort ();
4584 
4585     case OP_REG28:
4586       return 1 << 28;
4587 
4588     case OP_REG:
4589     case OP_OPTIONAL_REG:
4590       {
4591 	const struct mips_reg_operand *reg_op;
4592 
4593 	reg_op = (const struct mips_reg_operand *) operand;
4594 	if (!(type_mask & (1 << reg_op->reg_type)))
4595 	  return 0;
4596 	uval = insn_extract_operand (insn, operand);
4597 	return 1 << mips_decode_reg_operand (reg_op, uval);
4598       }
4599 
4600     case OP_REG_PAIR:
4601       {
4602 	const struct mips_reg_pair_operand *pair_op;
4603 
4604 	pair_op = (const struct mips_reg_pair_operand *) operand;
4605 	if (!(type_mask & (1 << pair_op->reg_type)))
4606 	  return 0;
4607 	uval = insn_extract_operand (insn, operand);
4608 	return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4609       }
4610 
4611     case OP_CLO_CLZ_DEST:
4612       if (!(type_mask & (1 << OP_REG_GP)))
4613 	return 0;
4614       uval = insn_extract_operand (insn, operand);
4615       return (1 << (uval & 31)) | (1 << (uval >> 5));
4616 
4617     case OP_SAME_RS_RT:
4618       if (!(type_mask & (1 << OP_REG_GP)))
4619 	return 0;
4620       uval = insn_extract_operand (insn, operand);
4621       gas_assert ((uval & 31) == (uval >> 5));
4622       return 1 << (uval & 31);
4623 
4624     case OP_CHECK_PREV:
4625     case OP_NON_ZERO_REG:
4626       if (!(type_mask & (1 << OP_REG_GP)))
4627 	return 0;
4628       uval = insn_extract_operand (insn, operand);
4629       return 1 << (uval & 31);
4630 
4631     case OP_LWM_SWM_LIST:
4632       abort ();
4633 
4634     case OP_SAVE_RESTORE_LIST:
4635       abort ();
4636 
4637     case OP_MDMX_IMM_REG:
4638       if (!(type_mask & (1 << OP_REG_VEC)))
4639 	return 0;
4640       uval = insn_extract_operand (insn, operand);
4641       vsel = uval >> 5;
4642       if ((vsel & 0x18) == 0x18)
4643 	return 0;
4644       return 1 << (uval & 31);
4645 
4646     case OP_REG_INDEX:
4647       if (!(type_mask & (1 << OP_REG_GP)))
4648 	return 0;
4649       return 1 << insn_extract_operand (insn, operand);
4650     }
4651   abort ();
4652 }
4653 
4654 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4655    where bit N of OPNO_MASK is set if operand N should be included.
4656    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4657    is set.  */
4658 
4659 static unsigned int
4660 insn_reg_mask (const struct mips_cl_insn *insn,
4661 	       unsigned int type_mask, unsigned int opno_mask)
4662 {
4663   unsigned int opno, reg_mask;
4664 
4665   opno = 0;
4666   reg_mask = 0;
4667   while (opno_mask != 0)
4668     {
4669       if (opno_mask & 1)
4670 	reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4671       opno_mask >>= 1;
4672       opno += 1;
4673     }
4674   return reg_mask;
4675 }
4676 
4677 /* Return the mask of core registers that IP reads.  */
4678 
4679 static unsigned int
4680 gpr_read_mask (const struct mips_cl_insn *ip)
4681 {
4682   unsigned long pinfo, pinfo2;
4683   unsigned int mask;
4684 
4685   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4686   pinfo = ip->insn_mo->pinfo;
4687   pinfo2 = ip->insn_mo->pinfo2;
4688   if (pinfo & INSN_UDI)
4689     {
4690       /* UDI instructions have traditionally been assumed to read RS
4691 	 and RT.  */
4692       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4693       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4694     }
4695   if (pinfo & INSN_READ_GPR_24)
4696     mask |= 1 << 24;
4697   if (pinfo2 & INSN2_READ_GPR_16)
4698     mask |= 1 << 16;
4699   if (pinfo2 & INSN2_READ_SP)
4700     mask |= 1 << SP;
4701   if (pinfo2 & INSN2_READ_GPR_31)
4702     mask |= 1 << 31;
4703   /* Don't include register 0.  */
4704   return mask & ~1;
4705 }
4706 
4707 /* Return the mask of core registers that IP writes.  */
4708 
4709 static unsigned int
4710 gpr_write_mask (const struct mips_cl_insn *ip)
4711 {
4712   unsigned long pinfo, pinfo2;
4713   unsigned int mask;
4714 
4715   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4716   pinfo = ip->insn_mo->pinfo;
4717   pinfo2 = ip->insn_mo->pinfo2;
4718   if (pinfo & INSN_WRITE_GPR_24)
4719     mask |= 1 << 24;
4720   if (pinfo & INSN_WRITE_GPR_31)
4721     mask |= 1 << 31;
4722   if (pinfo & INSN_UDI)
4723     /* UDI instructions have traditionally been assumed to write to RD.  */
4724     mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4725   if (pinfo2 & INSN2_WRITE_SP)
4726     mask |= 1 << SP;
4727   /* Don't include register 0.  */
4728   return mask & ~1;
4729 }
4730 
4731 /* Return the mask of floating-point registers that IP reads.  */
4732 
4733 static unsigned int
4734 fpr_read_mask (const struct mips_cl_insn *ip)
4735 {
4736   unsigned long pinfo;
4737   unsigned int mask;
4738 
4739   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4740 			     | (1 << OP_REG_MSA)),
4741 			insn_read_mask (ip->insn_mo));
4742   pinfo = ip->insn_mo->pinfo;
4743   /* Conservatively treat all operands to an FP_D instruction are doubles.
4744      (This is overly pessimistic for things like cvt.d.s.)  */
4745   if (FPR_SIZE != 64 && (pinfo & FP_D))
4746     mask |= mask << 1;
4747   return mask;
4748 }
4749 
4750 /* Return the mask of floating-point registers that IP writes.  */
4751 
4752 static unsigned int
4753 fpr_write_mask (const struct mips_cl_insn *ip)
4754 {
4755   unsigned long pinfo;
4756   unsigned int mask;
4757 
4758   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4759 			     | (1 << OP_REG_MSA)),
4760 			insn_write_mask (ip->insn_mo));
4761   pinfo = ip->insn_mo->pinfo;
4762   /* Conservatively treat all operands to an FP_D instruction are doubles.
4763      (This is overly pessimistic for things like cvt.s.d.)  */
4764   if (FPR_SIZE != 64 && (pinfo & FP_D))
4765     mask |= mask << 1;
4766   return mask;
4767 }
4768 
4769 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4770    Check whether that is allowed.  */
4771 
4772 static bfd_boolean
4773 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4774 {
4775   const char *s = insn->name;
4776   bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4777 			  || FPR_SIZE == 64)
4778 			 && mips_opts.oddspreg;
4779 
4780   if (insn->pinfo == INSN_MACRO)
4781     /* Let a macro pass, we'll catch it later when it is expanded.  */
4782     return TRUE;
4783 
4784   /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4785      otherwise it depends on oddspreg.  */
4786   if ((insn->pinfo & FP_S)
4787       && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4788 			 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4789     return FPR_SIZE == 32 || oddspreg;
4790 
4791   /* Allow odd registers for single-precision ops and double-precision if the
4792      floating-point registers are 64-bit wide.  */
4793   switch (insn->pinfo & (FP_S | FP_D))
4794     {
4795     case FP_S:
4796     case 0:
4797       return oddspreg;
4798     case FP_D:
4799       return FPR_SIZE == 64;
4800     default:
4801       break;
4802     }
4803 
4804   /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
4805   s = strchr (insn->name, '.');
4806   if (s != NULL && opnum == 2)
4807     s = strchr (s + 1, '.');
4808   if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4809     return oddspreg;
4810 
4811   return FPR_SIZE == 64;
4812 }
4813 
4814 /* Information about an instruction argument that we're trying to match.  */
4815 struct mips_arg_info
4816 {
4817   /* The instruction so far.  */
4818   struct mips_cl_insn *insn;
4819 
4820   /* The first unconsumed operand token.  */
4821   struct mips_operand_token *token;
4822 
4823   /* The 1-based operand number, in terms of insn->insn_mo->args.  */
4824   int opnum;
4825 
4826   /* The 1-based argument number, for error reporting.  This does not
4827      count elided optional registers, etc..  */
4828   int argnum;
4829 
4830   /* The last OP_REG operand seen, or ILLEGAL_REG if none.  */
4831   unsigned int last_regno;
4832 
4833   /* If the first operand was an OP_REG, this is the register that it
4834      specified, otherwise it is ILLEGAL_REG.  */
4835   unsigned int dest_regno;
4836 
4837   /* The value of the last OP_INT operand.  Only used for OP_MSB,
4838      where it gives the lsb position.  */
4839   unsigned int last_op_int;
4840 
4841   /* If true, match routines should assume that no later instruction
4842      alternative matches and should therefore be as accommodating as
4843      possible.  Match routines should not report errors if something
4844      is only invalid for !LAX_MATCH.  */
4845   bfd_boolean lax_match;
4846 
4847   /* True if a reference to the current AT register was seen.  */
4848   bfd_boolean seen_at;
4849 };
4850 
4851 /* Record that the argument is out of range.  */
4852 
4853 static void
4854 match_out_of_range (struct mips_arg_info *arg)
4855 {
4856   set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4857 }
4858 
4859 /* Record that the argument isn't constant but needs to be.  */
4860 
4861 static void
4862 match_not_constant (struct mips_arg_info *arg)
4863 {
4864   set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4865 		    arg->argnum);
4866 }
4867 
4868 /* Try to match an OT_CHAR token for character CH.  Consume the token
4869    and return true on success, otherwise return false.  */
4870 
4871 static bfd_boolean
4872 match_char (struct mips_arg_info *arg, char ch)
4873 {
4874   if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4875     {
4876       ++arg->token;
4877       if (ch == ',')
4878 	arg->argnum += 1;
4879       return TRUE;
4880     }
4881   return FALSE;
4882 }
4883 
4884 /* Try to get an expression from the next tokens in ARG.  Consume the
4885    tokens and return true on success, storing the expression value in
4886    VALUE and relocation types in R.  */
4887 
4888 static bfd_boolean
4889 match_expression (struct mips_arg_info *arg, expressionS *value,
4890 		  bfd_reloc_code_real_type *r)
4891 {
4892   /* If the next token is a '(' that was parsed as being part of a base
4893      expression, assume we have an elided offset.  The later match will fail
4894      if this turns out to be wrong.  */
4895   if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4896     {
4897       value->X_op = O_constant;
4898       value->X_add_number = 0;
4899       r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4900       return TRUE;
4901     }
4902 
4903   /* Reject register-based expressions such as "0+$2" and "(($2))".
4904      For plain registers the default error seems more appropriate.  */
4905   if (arg->token->type == OT_INTEGER
4906       && arg->token->u.integer.value.X_op == O_register)
4907     {
4908       set_insn_error (arg->argnum, _("register value used as expression"));
4909       return FALSE;
4910     }
4911 
4912   if (arg->token->type == OT_INTEGER)
4913     {
4914       *value = arg->token->u.integer.value;
4915       memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4916       ++arg->token;
4917       return TRUE;
4918     }
4919 
4920   set_insn_error_i
4921     (arg->argnum, _("operand %d must be an immediate expression"),
4922      arg->argnum);
4923   return FALSE;
4924 }
4925 
4926 /* Try to get a constant expression from the next tokens in ARG.  Consume
4927    the tokens and return true on success, storing the constant value
4928    in *VALUE.  */
4929 
4930 static bfd_boolean
4931 match_const_int (struct mips_arg_info *arg, offsetT *value)
4932 {
4933   expressionS ex;
4934   bfd_reloc_code_real_type r[3];
4935 
4936   if (!match_expression (arg, &ex, r))
4937     return FALSE;
4938 
4939   if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
4940     *value = ex.X_add_number;
4941   else
4942     {
4943       if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4944 	match_out_of_range (arg);
4945       else
4946 	match_not_constant (arg);
4947       return FALSE;
4948     }
4949   return TRUE;
4950 }
4951 
4952 /* Return the RTYPE_* flags for a register operand of type TYPE that
4953    appears in instruction OPCODE.  */
4954 
4955 static unsigned int
4956 convert_reg_type (const struct mips_opcode *opcode,
4957 		  enum mips_reg_operand_type type)
4958 {
4959   switch (type)
4960     {
4961     case OP_REG_GP:
4962       return RTYPE_NUM | RTYPE_GP;
4963 
4964     case OP_REG_FP:
4965       /* Allow vector register names for MDMX if the instruction is a 64-bit
4966 	 FPR load, store or move (including moves to and from GPRs).  */
4967       if ((mips_opts.ase & ASE_MDMX)
4968 	  && (opcode->pinfo & FP_D)
4969 	  && (opcode->pinfo & (INSN_COPROC_MOVE
4970 			       | INSN_COPROC_MEMORY_DELAY
4971 			       | INSN_LOAD_COPROC
4972 			       | INSN_LOAD_MEMORY
4973 			       | INSN_STORE_MEMORY)))
4974 	return RTYPE_FPU | RTYPE_VEC;
4975       return RTYPE_FPU;
4976 
4977     case OP_REG_CCC:
4978       if (opcode->pinfo & (FP_D | FP_S))
4979 	return RTYPE_CCC | RTYPE_FCC;
4980       return RTYPE_CCC;
4981 
4982     case OP_REG_VEC:
4983       if (opcode->membership & INSN_5400)
4984 	return RTYPE_FPU;
4985       return RTYPE_FPU | RTYPE_VEC;
4986 
4987     case OP_REG_ACC:
4988       return RTYPE_ACC;
4989 
4990     case OP_REG_COPRO:
4991       if (opcode->name[strlen (opcode->name) - 1] == '0')
4992 	return RTYPE_NUM | RTYPE_CP0;
4993       return RTYPE_NUM;
4994 
4995     case OP_REG_HW:
4996       return RTYPE_NUM;
4997 
4998     case OP_REG_VI:
4999       return RTYPE_NUM | RTYPE_VI;
5000 
5001     case OP_REG_VF:
5002       return RTYPE_NUM | RTYPE_VF;
5003 
5004     case OP_REG_R5900_I:
5005       return RTYPE_R5900_I;
5006 
5007     case OP_REG_R5900_Q:
5008       return RTYPE_R5900_Q;
5009 
5010     case OP_REG_R5900_R:
5011       return RTYPE_R5900_R;
5012 
5013     case OP_REG_R5900_ACC:
5014       return RTYPE_R5900_ACC;
5015 
5016     case OP_REG_MSA:
5017       return RTYPE_MSA;
5018 
5019     case OP_REG_MSA_CTRL:
5020       return RTYPE_NUM;
5021     }
5022   abort ();
5023 }
5024 
5025 /* ARG is register REGNO, of type TYPE.  Warn about any dubious registers.  */
5026 
5027 static void
5028 check_regno (struct mips_arg_info *arg,
5029 	     enum mips_reg_operand_type type, unsigned int regno)
5030 {
5031   if (AT && type == OP_REG_GP && regno == AT)
5032     arg->seen_at = TRUE;
5033 
5034   if (type == OP_REG_FP
5035       && (regno & 1) != 0
5036       && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5037     {
5038       /* This was a warning prior to introducing O32 FPXX and FP64 support
5039 	 so maintain a warning for FP32 but raise an error for the new
5040 	 cases.  */
5041       if (FPR_SIZE == 32)
5042 	as_warn (_("float register should be even, was %d"), regno);
5043       else
5044 	as_bad (_("float register should be even, was %d"), regno);
5045     }
5046 
5047   if (type == OP_REG_CCC)
5048     {
5049       const char *name;
5050       size_t length;
5051 
5052       name = arg->insn->insn_mo->name;
5053       length = strlen (name);
5054       if ((regno & 1) != 0
5055 	  && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5056 	      || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
5057 	as_warn (_("condition code register should be even for %s, was %d"),
5058 		 name, regno);
5059 
5060       if ((regno & 3) != 0
5061 	  && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
5062 	as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5063 		 name, regno);
5064     }
5065 }
5066 
5067 /* ARG is a register with symbol value SYMVAL.  Try to interpret it as
5068    a register of type TYPE.  Return true on success, storing the register
5069    number in *REGNO and warning about any dubious uses.  */
5070 
5071 static bfd_boolean
5072 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5073 	     unsigned int symval, unsigned int *regno)
5074 {
5075   if (type == OP_REG_VEC)
5076     symval = mips_prefer_vec_regno (symval);
5077   if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5078     return FALSE;
5079 
5080   *regno = symval & RNUM_MASK;
5081   check_regno (arg, type, *regno);
5082   return TRUE;
5083 }
5084 
5085 /* Try to interpret the next token in ARG as a register of type TYPE.
5086    Consume the token and return true on success, storing the register
5087    number in *REGNO.  Return false on failure.  */
5088 
5089 static bfd_boolean
5090 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5091 	   unsigned int *regno)
5092 {
5093   if (arg->token->type == OT_REG
5094       && match_regno (arg, type, arg->token->u.regno, regno))
5095     {
5096       ++arg->token;
5097       return TRUE;
5098     }
5099   return FALSE;
5100 }
5101 
5102 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5103    Consume the token and return true on success, storing the register numbers
5104    in *REGNO1 and *REGNO2.  Return false on failure.  */
5105 
5106 static bfd_boolean
5107 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5108 		 unsigned int *regno1, unsigned int *regno2)
5109 {
5110   if (match_reg (arg, type, regno1))
5111     {
5112       *regno2 = *regno1;
5113       return TRUE;
5114     }
5115   if (arg->token->type == OT_REG_RANGE
5116       && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5117       && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5118       && *regno1 <= *regno2)
5119     {
5120       ++arg->token;
5121       return TRUE;
5122     }
5123   return FALSE;
5124 }
5125 
5126 /* OP_INT matcher.  */
5127 
5128 static bfd_boolean
5129 match_int_operand (struct mips_arg_info *arg,
5130 		   const struct mips_operand *operand_base)
5131 {
5132   const struct mips_int_operand *operand;
5133   unsigned int uval;
5134   int min_val, max_val, factor;
5135   offsetT sval;
5136 
5137   operand = (const struct mips_int_operand *) operand_base;
5138   factor = 1 << operand->shift;
5139   min_val = mips_int_operand_min (operand);
5140   max_val = mips_int_operand_max (operand);
5141 
5142   if (operand_base->lsb == 0
5143       && operand_base->size == 16
5144       && operand->shift == 0
5145       && operand->bias == 0
5146       && (operand->max_val == 32767 || operand->max_val == 65535))
5147     {
5148       /* The operand can be relocated.  */
5149       if (!match_expression (arg, &offset_expr, offset_reloc))
5150 	return FALSE;
5151 
5152       if (offset_expr.X_op == O_big)
5153 	{
5154 	  match_out_of_range (arg);
5155 	  return FALSE;
5156 	}
5157 
5158       if (offset_reloc[0] != BFD_RELOC_UNUSED)
5159 	/* Relocation operators were used.  Accept the argument and
5160 	   leave the relocation value in offset_expr and offset_relocs
5161 	   for the caller to process.  */
5162 	return TRUE;
5163 
5164       if (offset_expr.X_op != O_constant)
5165 	{
5166 	  /* Accept non-constant operands if no later alternative matches,
5167 	     leaving it for the caller to process.  */
5168 	  if (!arg->lax_match)
5169 	    {
5170 	      match_not_constant (arg);
5171 	      return FALSE;
5172 	    }
5173 	  offset_reloc[0] = BFD_RELOC_LO16;
5174 	  return TRUE;
5175 	}
5176 
5177       /* Clear the global state; we're going to install the operand
5178 	 ourselves.  */
5179       sval = offset_expr.X_add_number;
5180       offset_expr.X_op = O_absent;
5181 
5182       /* For compatibility with older assemblers, we accept
5183 	 0x8000-0xffff as signed 16-bit numbers when only
5184 	 signed numbers are allowed.  */
5185       if (sval > max_val)
5186 	{
5187 	  max_val = ((1 << operand_base->size) - 1) << operand->shift;
5188 	  if (!arg->lax_match && sval <= max_val)
5189 	    {
5190 	      match_out_of_range (arg);
5191 	      return FALSE;
5192 	    }
5193 	}
5194     }
5195   else
5196     {
5197       if (!match_const_int (arg, &sval))
5198 	return FALSE;
5199     }
5200 
5201   arg->last_op_int = sval;
5202 
5203   if (sval < min_val || sval > max_val || sval % factor)
5204     {
5205       match_out_of_range (arg);
5206       return FALSE;
5207     }
5208 
5209   uval = (unsigned int) sval >> operand->shift;
5210   uval -= operand->bias;
5211 
5212   /* Handle -mfix-cn63xxp1.  */
5213   if (arg->opnum == 1
5214       && mips_fix_cn63xxp1
5215       && !mips_opts.micromips
5216       && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5217     switch (uval)
5218       {
5219       case 5:
5220       case 25:
5221       case 26:
5222       case 27:
5223       case 28:
5224       case 29:
5225       case 30:
5226       case 31:
5227 	/* These are ok.  */
5228 	break;
5229 
5230       default:
5231 	/* The rest must be changed to 28.  */
5232 	uval = 28;
5233 	break;
5234       }
5235 
5236   insn_insert_operand (arg->insn, operand_base, uval);
5237   return TRUE;
5238 }
5239 
5240 /* OP_MAPPED_INT matcher.  */
5241 
5242 static bfd_boolean
5243 match_mapped_int_operand (struct mips_arg_info *arg,
5244 			  const struct mips_operand *operand_base)
5245 {
5246   const struct mips_mapped_int_operand *operand;
5247   unsigned int uval, num_vals;
5248   offsetT sval;
5249 
5250   operand = (const struct mips_mapped_int_operand *) operand_base;
5251   if (!match_const_int (arg, &sval))
5252     return FALSE;
5253 
5254   num_vals = 1 << operand_base->size;
5255   for (uval = 0; uval < num_vals; uval++)
5256     if (operand->int_map[uval] == sval)
5257       break;
5258   if (uval == num_vals)
5259     {
5260       match_out_of_range (arg);
5261       return FALSE;
5262     }
5263 
5264   insn_insert_operand (arg->insn, operand_base, uval);
5265   return TRUE;
5266 }
5267 
5268 /* OP_MSB matcher.  */
5269 
5270 static bfd_boolean
5271 match_msb_operand (struct mips_arg_info *arg,
5272 		   const struct mips_operand *operand_base)
5273 {
5274   const struct mips_msb_operand *operand;
5275   int min_val, max_val, max_high;
5276   offsetT size, sval, high;
5277 
5278   operand = (const struct mips_msb_operand *) operand_base;
5279   min_val = operand->bias;
5280   max_val = min_val + (1 << operand_base->size) - 1;
5281   max_high = operand->opsize;
5282 
5283   if (!match_const_int (arg, &size))
5284     return FALSE;
5285 
5286   high = size + arg->last_op_int;
5287   sval = operand->add_lsb ? high : size;
5288 
5289   if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5290     {
5291       match_out_of_range (arg);
5292       return FALSE;
5293     }
5294   insn_insert_operand (arg->insn, operand_base, sval - min_val);
5295   return TRUE;
5296 }
5297 
5298 /* OP_REG matcher.  */
5299 
5300 static bfd_boolean
5301 match_reg_operand (struct mips_arg_info *arg,
5302 		   const struct mips_operand *operand_base)
5303 {
5304   const struct mips_reg_operand *operand;
5305   unsigned int regno, uval, num_vals;
5306 
5307   operand = (const struct mips_reg_operand *) operand_base;
5308   if (!match_reg (arg, operand->reg_type, &regno))
5309     return FALSE;
5310 
5311   if (operand->reg_map)
5312     {
5313       num_vals = 1 << operand->root.size;
5314       for (uval = 0; uval < num_vals; uval++)
5315 	if (operand->reg_map[uval] == regno)
5316 	  break;
5317       if (num_vals == uval)
5318 	return FALSE;
5319     }
5320   else
5321     uval = regno;
5322 
5323   arg->last_regno = regno;
5324   if (arg->opnum == 1)
5325     arg->dest_regno = regno;
5326   insn_insert_operand (arg->insn, operand_base, uval);
5327   return TRUE;
5328 }
5329 
5330 /* OP_REG_PAIR matcher.  */
5331 
5332 static bfd_boolean
5333 match_reg_pair_operand (struct mips_arg_info *arg,
5334 			const struct mips_operand *operand_base)
5335 {
5336   const struct mips_reg_pair_operand *operand;
5337   unsigned int regno1, regno2, uval, num_vals;
5338 
5339   operand = (const struct mips_reg_pair_operand *) operand_base;
5340   if (!match_reg (arg, operand->reg_type, &regno1)
5341       || !match_char (arg, ',')
5342       || !match_reg (arg, operand->reg_type, &regno2))
5343     return FALSE;
5344 
5345   num_vals = 1 << operand_base->size;
5346   for (uval = 0; uval < num_vals; uval++)
5347     if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5348       break;
5349   if (uval == num_vals)
5350     return FALSE;
5351 
5352   insn_insert_operand (arg->insn, operand_base, uval);
5353   return TRUE;
5354 }
5355 
5356 /* OP_PCREL matcher.  The caller chooses the relocation type.  */
5357 
5358 static bfd_boolean
5359 match_pcrel_operand (struct mips_arg_info *arg)
5360 {
5361   bfd_reloc_code_real_type r[3];
5362 
5363   return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5364 }
5365 
5366 /* OP_PERF_REG matcher.  */
5367 
5368 static bfd_boolean
5369 match_perf_reg_operand (struct mips_arg_info *arg,
5370 			const struct mips_operand *operand)
5371 {
5372   offsetT sval;
5373 
5374   if (!match_const_int (arg, &sval))
5375     return FALSE;
5376 
5377   if (sval != 0
5378       && (sval != 1
5379 	  || (mips_opts.arch == CPU_R5900
5380 	      && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5381 		  || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5382     {
5383       set_insn_error (arg->argnum, _("invalid performance register"));
5384       return FALSE;
5385     }
5386 
5387   insn_insert_operand (arg->insn, operand, sval);
5388   return TRUE;
5389 }
5390 
5391 /* OP_ADDIUSP matcher.  */
5392 
5393 static bfd_boolean
5394 match_addiusp_operand (struct mips_arg_info *arg,
5395 		       const struct mips_operand *operand)
5396 {
5397   offsetT sval;
5398   unsigned int uval;
5399 
5400   if (!match_const_int (arg, &sval))
5401     return FALSE;
5402 
5403   if (sval % 4)
5404     {
5405       match_out_of_range (arg);
5406       return FALSE;
5407     }
5408 
5409   sval /= 4;
5410   if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5411     {
5412       match_out_of_range (arg);
5413       return FALSE;
5414     }
5415 
5416   uval = (unsigned int) sval;
5417   uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5418   insn_insert_operand (arg->insn, operand, uval);
5419   return TRUE;
5420 }
5421 
5422 /* OP_CLO_CLZ_DEST matcher.  */
5423 
5424 static bfd_boolean
5425 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5426 			    const struct mips_operand *operand)
5427 {
5428   unsigned int regno;
5429 
5430   if (!match_reg (arg, OP_REG_GP, &regno))
5431     return FALSE;
5432 
5433   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5434   return TRUE;
5435 }
5436 
5437 /* OP_CHECK_PREV matcher.  */
5438 
5439 static bfd_boolean
5440 match_check_prev_operand (struct mips_arg_info *arg,
5441 			  const struct mips_operand *operand_base)
5442 {
5443   const struct mips_check_prev_operand *operand;
5444   unsigned int regno;
5445 
5446   operand = (const struct mips_check_prev_operand *) operand_base;
5447 
5448   if (!match_reg (arg, OP_REG_GP, &regno))
5449     return FALSE;
5450 
5451   if (!operand->zero_ok && regno == 0)
5452     return FALSE;
5453 
5454   if ((operand->less_than_ok && regno < arg->last_regno)
5455       || (operand->greater_than_ok && regno > arg->last_regno)
5456       || (operand->equal_ok && regno == arg->last_regno))
5457     {
5458       arg->last_regno = regno;
5459       insn_insert_operand (arg->insn, operand_base, regno);
5460       return TRUE;
5461     }
5462 
5463   return FALSE;
5464 }
5465 
5466 /* OP_SAME_RS_RT matcher.  */
5467 
5468 static bfd_boolean
5469 match_same_rs_rt_operand (struct mips_arg_info *arg,
5470 			  const struct mips_operand *operand)
5471 {
5472   unsigned int regno;
5473 
5474   if (!match_reg (arg, OP_REG_GP, &regno))
5475     return FALSE;
5476 
5477   if (regno == 0)
5478     {
5479       set_insn_error (arg->argnum, _("the source register must not be $0"));
5480       return FALSE;
5481     }
5482 
5483   arg->last_regno = regno;
5484 
5485   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5486   return TRUE;
5487 }
5488 
5489 /* OP_LWM_SWM_LIST matcher.  */
5490 
5491 static bfd_boolean
5492 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5493 			    const struct mips_operand *operand)
5494 {
5495   unsigned int reglist, sregs, ra, regno1, regno2;
5496   struct mips_arg_info reset;
5497 
5498   reglist = 0;
5499   if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5500     return FALSE;
5501   do
5502     {
5503       if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5504 	{
5505 	  reglist |= 1 << FP;
5506 	  regno2 = S7;
5507 	}
5508       reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5509       reset = *arg;
5510     }
5511   while (match_char (arg, ',')
5512 	 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5513   *arg = reset;
5514 
5515   if (operand->size == 2)
5516     {
5517       /* The list must include both ra and s0-sN, for 0 <= N <= 3.  E.g.:
5518 
5519 	 s0, ra
5520 	 s0, s1, ra, s2, s3
5521 	 s0-s2, ra
5522 
5523 	 and any permutations of these.  */
5524       if ((reglist & 0xfff1ffff) != 0x80010000)
5525 	return FALSE;
5526 
5527       sregs = (reglist >> 17) & 7;
5528       ra = 0;
5529     }
5530   else
5531     {
5532       /* The list must include at least one of ra and s0-sN,
5533 	 for 0 <= N <= 8.  (Note that there is a gap between s7 and s8,
5534 	 which are $23 and $30 respectively.)  E.g.:
5535 
5536 	 ra
5537 	 s0
5538 	 ra, s0, s1, s2
5539 	 s0-s8
5540 	 s0-s5, ra
5541 
5542 	 and any permutations of these.  */
5543       if ((reglist & 0x3f00ffff) != 0)
5544 	return FALSE;
5545 
5546 
5547 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
5548       ra = (reglist >> 27) & 0x10;
5549       sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5550     }
5551   sregs += 1;
5552   if ((sregs & -sregs) != sregs)
5553     return FALSE;
5554 
5555   insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5556   return TRUE;
5557 }
5558 
5559 static void
5560 trap_zero_jump (struct mips_cl_insn * ip)
5561 {
5562   if (strcmp (ip->insn_mo->name, "j") == 0
5563       || strcmp (ip->insn_mo->name, "jr") == 0
5564       || strcmp (ip->insn_mo->name, "jalr") == 0)
5565     {
5566       int sreg;
5567 
5568       if (mips_opts.warn_about_macros)
5569         return;
5570 
5571       sreg = EXTRACT_OPERAND (0, RS, *ip);
5572       if (mips_opts.isa == ISA_MIPS32
5573           || mips_opts.isa == ISA_MIPS32R2
5574           || mips_opts.isa == ISA_MIPS64
5575           || mips_opts.isa == ISA_MIPS64R2)
5576 	{
5577 	  expressionS ep;
5578 	  ep.X_op = O_constant;
5579 	  ep.X_add_number = 4096;
5580 	  macro_build (&ep, "tltiu", "s,j", sreg, BFD_RELOC_LO16);
5581 	}
5582       else if (mips_opts.isa != ISA_UNKNOWN
5583 	       && mips_opts.isa != ISA_MIPS1)
5584 	macro_build (NULL, "teq", "s,t", sreg, 0);
5585   }
5586 }
5587 
5588 /* OP_ENTRY_EXIT_LIST matcher.  */
5589 
5590 static unsigned int
5591 match_entry_exit_operand (struct mips_arg_info *arg,
5592 			  const struct mips_operand *operand)
5593 {
5594   unsigned int mask;
5595   bfd_boolean is_exit;
5596 
5597   /* The format is the same for both ENTRY and EXIT, but the constraints
5598      are different.  */
5599   is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5600   mask = (is_exit ? 7 << 3 : 0);
5601   do
5602     {
5603       unsigned int regno1, regno2;
5604       bfd_boolean is_freg;
5605 
5606       if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5607 	is_freg = FALSE;
5608       else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5609 	is_freg = TRUE;
5610       else
5611 	return FALSE;
5612 
5613       if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5614 	{
5615 	  mask &= ~(7 << 3);
5616 	  mask |= (5 + regno2) << 3;
5617 	}
5618       else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5619 	mask |= (regno2 - 3) << 3;
5620       else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5621 	mask |= (regno2 - 15) << 1;
5622       else if (regno1 == RA && regno2 == RA)
5623 	mask |= 1;
5624       else
5625 	return FALSE;
5626     }
5627   while (match_char (arg, ','));
5628 
5629   insn_insert_operand (arg->insn, operand, mask);
5630   return TRUE;
5631 }
5632 
5633 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5634    the argument register mask AMASK, the number of static registers
5635    saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5636    respectively, and the frame size FRAME_SIZE.  */
5637 
5638 static unsigned int
5639 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5640 			  unsigned int ra, unsigned int s0, unsigned int s1,
5641 			  unsigned int frame_size)
5642 {
5643   return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5644 	  | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5645 }
5646 
5647 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5648    argument register mask AMASK, the number of static registers saved
5649    NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5650    respectively, and the frame size FRAME_SIZE.  */
5651 
5652 static unsigned int
5653 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5654 			    unsigned int ra, unsigned int s0, unsigned int s1,
5655 			    unsigned int frame_size)
5656 {
5657   unsigned int args;
5658 
5659   args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5660   if (nsreg || amask || frame_size == 0 || frame_size > 16)
5661     args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5662 	     | ((frame_size & 0xf0) << 16));
5663   return args;
5664 }
5665 
5666 /* OP_SAVE_RESTORE_LIST matcher.  */
5667 
5668 static bfd_boolean
5669 match_save_restore_list_operand (struct mips_arg_info *arg)
5670 {
5671   unsigned int opcode, args, statics, sregs;
5672   unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5673   unsigned int arg_mask, ra, s0, s1;
5674   offsetT frame_size;
5675 
5676   opcode = arg->insn->insn_opcode;
5677   frame_size = 0;
5678   num_frame_sizes = 0;
5679   args = 0;
5680   statics = 0;
5681   sregs = 0;
5682   ra = 0;
5683   s0 = 0;
5684   s1 = 0;
5685   do
5686     {
5687       unsigned int regno1, regno2;
5688 
5689       if (arg->token->type == OT_INTEGER)
5690 	{
5691 	  /* Handle the frame size.  */
5692 	  if (!match_const_int (arg, &frame_size))
5693 	    return FALSE;
5694 	  num_frame_sizes += 1;
5695 	}
5696       else
5697 	{
5698 	  if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5699 	    return FALSE;
5700 
5701 	  while (regno1 <= regno2)
5702 	    {
5703 	      if (regno1 >= 4 && regno1 <= 7)
5704 		{
5705 		  if (num_frame_sizes == 0)
5706 		    /* args $a0-$a3 */
5707 		    args |= 1 << (regno1 - 4);
5708 		  else
5709 		    /* statics $a0-$a3 */
5710 		    statics |= 1 << (regno1 - 4);
5711 		}
5712 	      else if (regno1 >= 16 && regno1 <= 23)
5713 		/* $s0-$s7 */
5714 		sregs |= 1 << (regno1 - 16);
5715 	      else if (regno1 == 30)
5716 		/* $s8 */
5717 		sregs |= 1 << 8;
5718 	      else if (regno1 == 31)
5719 		/* Add $ra to insn.  */
5720 		ra = 1;
5721 	      else
5722 		return FALSE;
5723 	      regno1 += 1;
5724 	      if (regno1 == 24)
5725 		regno1 = 30;
5726 	    }
5727 	}
5728     }
5729   while (match_char (arg, ','));
5730 
5731   /* Encode args/statics combination.  */
5732   if (args & statics)
5733     return FALSE;
5734   else if (args == 0xf)
5735     /* All $a0-$a3 are args.  */
5736     arg_mask = MIPS_SVRS_ALL_ARGS;
5737   else if (statics == 0xf)
5738     /* All $a0-$a3 are statics.  */
5739     arg_mask = MIPS_SVRS_ALL_STATICS;
5740   else
5741     {
5742       /* Count arg registers.  */
5743       num_args = 0;
5744       while (args & 0x1)
5745 	{
5746 	  args >>= 1;
5747 	  num_args += 1;
5748 	}
5749       if (args != 0)
5750 	return FALSE;
5751 
5752       /* Count static registers.  */
5753       num_statics = 0;
5754       while (statics & 0x8)
5755 	{
5756 	  statics = (statics << 1) & 0xf;
5757 	  num_statics += 1;
5758 	}
5759       if (statics != 0)
5760 	return FALSE;
5761 
5762       /* Encode args/statics.  */
5763       arg_mask = (num_args << 2) | num_statics;
5764     }
5765 
5766   /* Encode $s0/$s1.  */
5767   if (sregs & (1 << 0))		/* $s0 */
5768     s0 = 1;
5769   if (sregs & (1 << 1))		/* $s1 */
5770     s1 = 1;
5771   sregs >>= 2;
5772 
5773   /* Encode $s2-$s8. */
5774   num_sregs = 0;
5775   while (sregs & 1)
5776     {
5777       sregs >>= 1;
5778       num_sregs += 1;
5779     }
5780   if (sregs != 0)
5781     return FALSE;
5782 
5783   /* Encode frame size.  */
5784   if (num_frame_sizes == 0)
5785     {
5786       set_insn_error (arg->argnum, _("missing frame size"));
5787       return FALSE;
5788     }
5789   if (num_frame_sizes > 1)
5790     {
5791       set_insn_error (arg->argnum, _("frame size specified twice"));
5792       return FALSE;
5793     }
5794   if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5795     {
5796       set_insn_error (arg->argnum, _("invalid frame size"));
5797       return FALSE;
5798     }
5799   frame_size /= 8;
5800 
5801   /* If the branch is itself the target of a branch, we can not swap.
5802      We cheat on this; all we check for is whether there is a label on
5803      this instruction.  If there are any branches to anything other than
5804      a label, users must use .set noreorder.  */
5805   if (seg_info (now_seg)->label_list)
5806     return FALSE;
5807 
5808   /* Finally build the instruction.  */
5809   if (mips_opts.mips16)
5810     opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5811 					  frame_size);
5812   else if (!mips_opts.micromips)
5813     opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5814 					frame_size);
5815   else
5816     abort ();
5817 
5818   arg->insn->insn_opcode = opcode;
5819   return TRUE;
5820 }
5821 
5822 /* OP_MDMX_IMM_REG matcher.  */
5823 
5824 static bfd_boolean
5825 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5826 			    const struct mips_operand *operand)
5827 {
5828   unsigned int regno, uval;
5829   bfd_boolean is_qh;
5830   const struct mips_opcode *opcode;
5831 
5832   /* The mips_opcode records whether this is an octobyte or quadhalf
5833      instruction.  Start out with that bit in place.  */
5834   opcode = arg->insn->insn_mo;
5835   uval = mips_extract_operand (operand, opcode->match);
5836   is_qh = (uval != 0);
5837 
5838   if (arg->token->type == OT_REG)
5839     {
5840       if ((opcode->membership & INSN_5400)
5841 	  && strcmp (opcode->name, "rzu.ob") == 0)
5842 	{
5843 	  set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5844 			    arg->argnum);
5845 	  return FALSE;
5846 	}
5847 
5848       if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5849 	return FALSE;
5850       ++arg->token;
5851 
5852       /* Check whether this is a vector register or a broadcast of
5853 	 a single element.  */
5854       if (arg->token->type == OT_INTEGER_INDEX)
5855 	{
5856 	  if (arg->token->u.index > (is_qh ? 3 : 7))
5857 	    {
5858 	      set_insn_error (arg->argnum, _("invalid element selector"));
5859 	      return FALSE;
5860 	    }
5861 	  uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5862 	  ++arg->token;
5863 	}
5864       else
5865 	{
5866 	  /* A full vector.  */
5867 	  if ((opcode->membership & INSN_5400)
5868 	      && (strcmp (opcode->name, "sll.ob") == 0
5869 		  || strcmp (opcode->name, "srl.ob") == 0))
5870 	    {
5871 	      set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5872 				arg->argnum);
5873 	      return FALSE;
5874 	    }
5875 
5876 	  if (is_qh)
5877 	    uval |= MDMX_FMTSEL_VEC_QH << 5;
5878 	  else
5879 	    uval |= MDMX_FMTSEL_VEC_OB << 5;
5880 	}
5881       uval |= regno;
5882     }
5883   else
5884     {
5885       offsetT sval;
5886 
5887       if (!match_const_int (arg, &sval))
5888 	return FALSE;
5889       if (sval < 0 || sval > 31)
5890 	{
5891 	  match_out_of_range (arg);
5892 	  return FALSE;
5893 	}
5894       uval |= (sval & 31);
5895       if (is_qh)
5896 	uval |= MDMX_FMTSEL_IMM_QH << 5;
5897       else
5898 	uval |= MDMX_FMTSEL_IMM_OB << 5;
5899     }
5900   insn_insert_operand (arg->insn, operand, uval);
5901   return TRUE;
5902 }
5903 
5904 /* OP_IMM_INDEX matcher.  */
5905 
5906 static bfd_boolean
5907 match_imm_index_operand (struct mips_arg_info *arg,
5908 			 const struct mips_operand *operand)
5909 {
5910   unsigned int max_val;
5911 
5912   if (arg->token->type != OT_INTEGER_INDEX)
5913     return FALSE;
5914 
5915   max_val = (1 << operand->size) - 1;
5916   if (arg->token->u.index > max_val)
5917     {
5918       match_out_of_range (arg);
5919       return FALSE;
5920     }
5921   insn_insert_operand (arg->insn, operand, arg->token->u.index);
5922   ++arg->token;
5923   return TRUE;
5924 }
5925 
5926 /* OP_REG_INDEX matcher.  */
5927 
5928 static bfd_boolean
5929 match_reg_index_operand (struct mips_arg_info *arg,
5930 			 const struct mips_operand *operand)
5931 {
5932   unsigned int regno;
5933 
5934   if (arg->token->type != OT_REG_INDEX)
5935     return FALSE;
5936 
5937   if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5938     return FALSE;
5939 
5940   insn_insert_operand (arg->insn, operand, regno);
5941   ++arg->token;
5942   return TRUE;
5943 }
5944 
5945 /* OP_PC matcher.  */
5946 
5947 static bfd_boolean
5948 match_pc_operand (struct mips_arg_info *arg)
5949 {
5950   if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5951     {
5952       ++arg->token;
5953       return TRUE;
5954     }
5955   return FALSE;
5956 }
5957 
5958 /* OP_REG28 matcher.  */
5959 
5960 static bfd_boolean
5961 match_reg28_operand (struct mips_arg_info *arg)
5962 {
5963   unsigned int regno;
5964 
5965   if (arg->token->type == OT_REG
5966       && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5967       && regno == GP)
5968     {
5969       ++arg->token;
5970       return TRUE;
5971     }
5972   return FALSE;
5973 }
5974 
5975 /* OP_NON_ZERO_REG matcher.  */
5976 
5977 static bfd_boolean
5978 match_non_zero_reg_operand (struct mips_arg_info *arg,
5979 			    const struct mips_operand *operand)
5980 {
5981   unsigned int regno;
5982 
5983   if (!match_reg (arg, OP_REG_GP, &regno))
5984     return FALSE;
5985 
5986   if (regno == 0)
5987     return FALSE;
5988 
5989   arg->last_regno = regno;
5990   insn_insert_operand (arg->insn, operand, regno);
5991   return TRUE;
5992 }
5993 
5994 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher.  OTHER_REGNO is the
5995    register that we need to match.  */
5996 
5997 static bfd_boolean
5998 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
5999 {
6000   unsigned int regno;
6001 
6002   return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
6003 }
6004 
6005 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
6006    LENGTH is the length of the value in bytes (4 for float, 8 for double)
6007    and USING_GPRS says whether the destination is a GPR rather than an FPR.
6008 
6009    Return the constant in IMM and OFFSET as follows:
6010 
6011    - If the constant should be loaded via memory, set IMM to O_absent and
6012      OFFSET to the memory address.
6013 
6014    - Otherwise, if the constant should be loaded into two 32-bit registers,
6015      set IMM to the O_constant to load into the high register and OFFSET
6016      to the corresponding value for the low register.
6017 
6018    - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6019 
6020    These constants only appear as the last operand in an instruction,
6021    and every instruction that accepts them in any variant accepts them
6022    in all variants.  This means we don't have to worry about backing out
6023    any changes if the instruction does not match.  We just match
6024    unconditionally and report an error if the constant is invalid.  */
6025 
6026 static bfd_boolean
6027 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6028 		      expressionS *offset, int length, bfd_boolean using_gprs)
6029 {
6030   char *p;
6031   segT seg, new_seg;
6032   subsegT subseg;
6033   const char *newname;
6034   unsigned char *data;
6035 
6036   /* Where the constant is placed is based on how the MIPS assembler
6037      does things:
6038 
6039      length == 4 && using_gprs  -- immediate value only
6040      length == 8 && using_gprs  -- .rdata or immediate value
6041      length == 4 && !using_gprs -- .lit4 or immediate value
6042      length == 8 && !using_gprs -- .lit8 or immediate value
6043 
6044      The .lit4 and .lit8 sections are only used if permitted by the
6045      -G argument.  */
6046   if (arg->token->type != OT_FLOAT)
6047     {
6048       set_insn_error (arg->argnum, _("floating-point expression required"));
6049       return FALSE;
6050     }
6051 
6052   gas_assert (arg->token->u.flt.length == length);
6053   data = arg->token->u.flt.data;
6054   ++arg->token;
6055 
6056   /* Handle 32-bit constants for which an immediate value is best.  */
6057   if (length == 4
6058       && (using_gprs
6059 	  || g_switch_value < 4
6060 	  || (data[0] == 0 && data[1] == 0)
6061 	  || (data[2] == 0 && data[3] == 0)))
6062     {
6063       imm->X_op = O_constant;
6064       if (!target_big_endian)
6065 	imm->X_add_number = bfd_getl32 (data);
6066       else
6067 	imm->X_add_number = bfd_getb32 (data);
6068       offset->X_op = O_absent;
6069       return TRUE;
6070     }
6071 
6072   /* Handle 64-bit constants for which an immediate value is best.  */
6073   if (length == 8
6074       && !mips_disable_float_construction
6075       /* Constants can only be constructed in GPRs and copied to FPRs if the
6076 	 GPRs are at least as wide as the FPRs or MTHC1 is available.
6077 	 Unlike most tests for 32-bit floating-point registers this check
6078 	 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6079 	 permit 64-bit moves without MXHC1.
6080 	 Force the constant into memory otherwise.  */
6081       && (using_gprs
6082 	  || GPR_SIZE == 64
6083 	  || ISA_HAS_MXHC1 (mips_opts.isa)
6084 	  || FPR_SIZE == 32)
6085       && ((data[0] == 0 && data[1] == 0)
6086 	  || (data[2] == 0 && data[3] == 0))
6087       && ((data[4] == 0 && data[5] == 0)
6088 	  || (data[6] == 0 && data[7] == 0)))
6089     {
6090       /* The value is simple enough to load with a couple of instructions.
6091 	 If using 32-bit registers, set IMM to the high order 32 bits and
6092 	 OFFSET to the low order 32 bits.  Otherwise, set IMM to the entire
6093 	 64 bit constant.  */
6094       if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6095 	{
6096 	  imm->X_op = O_constant;
6097 	  offset->X_op = O_constant;
6098 	  if (!target_big_endian)
6099 	    {
6100 	      imm->X_add_number = bfd_getl32 (data + 4);
6101 	      offset->X_add_number = bfd_getl32 (data);
6102 	    }
6103 	  else
6104 	    {
6105 	      imm->X_add_number = bfd_getb32 (data);
6106 	      offset->X_add_number = bfd_getb32 (data + 4);
6107 	    }
6108 	  if (offset->X_add_number == 0)
6109 	    offset->X_op = O_absent;
6110 	}
6111       else
6112 	{
6113 	  imm->X_op = O_constant;
6114 	  if (!target_big_endian)
6115 	    imm->X_add_number = bfd_getl64 (data);
6116 	  else
6117 	    imm->X_add_number = bfd_getb64 (data);
6118 	  offset->X_op = O_absent;
6119 	}
6120       return TRUE;
6121     }
6122 
6123   /* Switch to the right section.  */
6124   seg = now_seg;
6125   subseg = now_subseg;
6126   if (length == 4)
6127     {
6128       gas_assert (!using_gprs && g_switch_value >= 4);
6129       newname = ".lit4";
6130     }
6131   else
6132     {
6133       if (using_gprs || g_switch_value < 8)
6134 	newname = RDATA_SECTION_NAME;
6135       else
6136 	newname = ".lit8";
6137     }
6138 
6139   new_seg = subseg_new (newname, (subsegT) 0);
6140   bfd_set_section_flags (stdoutput, new_seg,
6141 			 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6142   frag_align (length == 4 ? 2 : 3, 0, 0);
6143   if (strncmp (TARGET_OS, "elf", 3) != 0)
6144     record_alignment (new_seg, 4);
6145   else
6146     record_alignment (new_seg, length == 4 ? 2 : 3);
6147   if (seg == now_seg)
6148     as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6149 
6150   /* Set the argument to the current address in the section.  */
6151   imm->X_op = O_absent;
6152   offset->X_op = O_symbol;
6153   offset->X_add_symbol = symbol_temp_new_now ();
6154   offset->X_add_number = 0;
6155 
6156   /* Put the floating point number into the section.  */
6157   p = frag_more (length);
6158   memcpy (p, data, length);
6159 
6160   /* Switch back to the original section.  */
6161   subseg_set (seg, subseg);
6162   return TRUE;
6163 }
6164 
6165 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6166    them.  */
6167 
6168 static bfd_boolean
6169 match_vu0_suffix_operand (struct mips_arg_info *arg,
6170 			  const struct mips_operand *operand,
6171 			  bfd_boolean match_p)
6172 {
6173   unsigned int uval;
6174 
6175   /* The operand can be an XYZW mask or a single 2-bit channel index
6176      (with X being 0).  */
6177   gas_assert (operand->size == 2 || operand->size == 4);
6178 
6179   /* The suffix can be omitted when it is already part of the opcode.  */
6180   if (arg->token->type != OT_CHANNELS)
6181     return match_p;
6182 
6183   uval = arg->token->u.channels;
6184   if (operand->size == 2)
6185     {
6186       /* Check that a single bit is set and convert it into a 2-bit index.  */
6187       if ((uval & -uval) != uval)
6188 	return FALSE;
6189       uval = 4 - ffs (uval);
6190     }
6191 
6192   if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6193     return FALSE;
6194 
6195   ++arg->token;
6196   if (!match_p)
6197     insn_insert_operand (arg->insn, operand, uval);
6198   return TRUE;
6199 }
6200 
6201 /* Try to match a token from ARG against OPERAND.  Consume the token
6202    and return true on success, otherwise return false.  */
6203 
6204 static bfd_boolean
6205 match_operand (struct mips_arg_info *arg,
6206 	       const struct mips_operand *operand)
6207 {
6208   switch (operand->type)
6209     {
6210     case OP_INT:
6211       return match_int_operand (arg, operand);
6212 
6213     case OP_MAPPED_INT:
6214       return match_mapped_int_operand (arg, operand);
6215 
6216     case OP_MSB:
6217       return match_msb_operand (arg, operand);
6218 
6219     case OP_REG:
6220     case OP_OPTIONAL_REG:
6221       return match_reg_operand (arg, operand);
6222 
6223     case OP_REG_PAIR:
6224       return match_reg_pair_operand (arg, operand);
6225 
6226     case OP_PCREL:
6227       return match_pcrel_operand (arg);
6228 
6229     case OP_PERF_REG:
6230       return match_perf_reg_operand (arg, operand);
6231 
6232     case OP_ADDIUSP_INT:
6233       return match_addiusp_operand (arg, operand);
6234 
6235     case OP_CLO_CLZ_DEST:
6236       return match_clo_clz_dest_operand (arg, operand);
6237 
6238     case OP_LWM_SWM_LIST:
6239       return match_lwm_swm_list_operand (arg, operand);
6240 
6241     case OP_ENTRY_EXIT_LIST:
6242       return match_entry_exit_operand (arg, operand);
6243 
6244     case OP_SAVE_RESTORE_LIST:
6245       return match_save_restore_list_operand (arg);
6246 
6247     case OP_MDMX_IMM_REG:
6248       return match_mdmx_imm_reg_operand (arg, operand);
6249 
6250     case OP_REPEAT_DEST_REG:
6251       return match_tied_reg_operand (arg, arg->dest_regno);
6252 
6253     case OP_REPEAT_PREV_REG:
6254       return match_tied_reg_operand (arg, arg->last_regno);
6255 
6256     case OP_PC:
6257       return match_pc_operand (arg);
6258 
6259     case OP_REG28:
6260       return match_reg28_operand (arg);
6261 
6262     case OP_VU0_SUFFIX:
6263       return match_vu0_suffix_operand (arg, operand, FALSE);
6264 
6265     case OP_VU0_MATCH_SUFFIX:
6266       return match_vu0_suffix_operand (arg, operand, TRUE);
6267 
6268     case OP_IMM_INDEX:
6269       return match_imm_index_operand (arg, operand);
6270 
6271     case OP_REG_INDEX:
6272       return match_reg_index_operand (arg, operand);
6273 
6274     case OP_SAME_RS_RT:
6275       return match_same_rs_rt_operand (arg, operand);
6276 
6277     case OP_CHECK_PREV:
6278       return match_check_prev_operand (arg, operand);
6279 
6280     case OP_NON_ZERO_REG:
6281       return match_non_zero_reg_operand (arg, operand);
6282     }
6283   abort ();
6284 }
6285 
6286 /* ARG is the state after successfully matching an instruction.
6287    Issue any queued-up warnings.  */
6288 
6289 static void
6290 check_completed_insn (struct mips_arg_info *arg)
6291 {
6292   if (arg->seen_at)
6293     {
6294       if (AT == ATREG)
6295 	as_warn (_("used $at without \".set noat\""));
6296       else
6297 	as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6298     }
6299 }
6300 
6301 /* Return true if modifying general-purpose register REG needs a delay.  */
6302 
6303 static bfd_boolean
6304 reg_needs_delay (unsigned int reg)
6305 {
6306   unsigned long prev_pinfo;
6307 
6308   prev_pinfo = history[0].insn_mo->pinfo;
6309   if (!mips_opts.noreorder
6310       && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6311 	  || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6312       && (gpr_write_mask (&history[0]) & (1 << reg)))
6313     return TRUE;
6314 
6315   return FALSE;
6316 }
6317 
6318 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6319    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6320    by VR4120 errata.  */
6321 
6322 static unsigned int
6323 classify_vr4120_insn (const char *name)
6324 {
6325   if (strncmp (name, "macc", 4) == 0)
6326     return FIX_VR4120_MACC;
6327   if (strncmp (name, "dmacc", 5) == 0)
6328     return FIX_VR4120_DMACC;
6329   if (strncmp (name, "mult", 4) == 0)
6330     return FIX_VR4120_MULT;
6331   if (strncmp (name, "dmult", 5) == 0)
6332     return FIX_VR4120_DMULT;
6333   if (strstr (name, "div"))
6334     return FIX_VR4120_DIV;
6335   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6336     return FIX_VR4120_MTHILO;
6337   return NUM_FIX_VR4120_CLASSES;
6338 }
6339 
6340 #define INSN_ERET	0x42000018
6341 #define INSN_DERET	0x4200001f
6342 #define INSN_DMULT	0x1c
6343 #define INSN_DMULTU	0x1d
6344 
6345 /* Return the number of instructions that must separate INSN1 and INSN2,
6346    where INSN1 is the earlier instruction.  Return the worst-case value
6347    for any INSN2 if INSN2 is null.  */
6348 
6349 static unsigned int
6350 insns_between (const struct mips_cl_insn *insn1,
6351 	       const struct mips_cl_insn *insn2)
6352 {
6353   unsigned long pinfo1, pinfo2;
6354   unsigned int mask;
6355 
6356   /* If INFO2 is null, pessimistically assume that all flags are set for
6357      the second instruction.  */
6358   pinfo1 = insn1->insn_mo->pinfo;
6359   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6360 
6361   /* For most targets, write-after-read dependencies on the HI and LO
6362      registers must be separated by at least two instructions.  */
6363   if (!hilo_interlocks)
6364     {
6365       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6366 	return 2;
6367       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6368 	return 2;
6369     }
6370 
6371   /* If we're working around r7000 errata, there must be two instructions
6372      between an mfhi or mflo and any instruction that uses the result.  */
6373   if (mips_7000_hilo_fix
6374       && !mips_opts.micromips
6375       && MF_HILO_INSN (pinfo1)
6376       && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6377     return 2;
6378 
6379   /* If we're working around 24K errata, one instruction is required
6380      if an ERET or DERET is followed by a branch instruction.  */
6381   if (mips_fix_24k && !mips_opts.micromips)
6382     {
6383       if (insn1->insn_opcode == INSN_ERET
6384 	  || insn1->insn_opcode == INSN_DERET)
6385 	{
6386 	  if (insn2 == NULL
6387 	      || insn2->insn_opcode == INSN_ERET
6388 	      || insn2->insn_opcode == INSN_DERET
6389 	      || delayed_branch_p (insn2))
6390 	    return 1;
6391 	}
6392     }
6393 
6394   /* If we're working around PMC RM7000 errata, there must be three
6395      nops between a dmult and a load instruction.  */
6396   if (mips_fix_rm7000 && !mips_opts.micromips)
6397     {
6398       if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6399 	  || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6400 	{
6401 	  if (pinfo2 & INSN_LOAD_MEMORY)
6402 	   return 3;
6403 	}
6404     }
6405 
6406   /* If working around VR4120 errata, check for combinations that need
6407      a single intervening instruction.  */
6408   if (mips_fix_vr4120 && !mips_opts.micromips)
6409     {
6410       unsigned int class1, class2;
6411 
6412       class1 = classify_vr4120_insn (insn1->insn_mo->name);
6413       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6414 	{
6415 	  if (insn2 == NULL)
6416 	    return 1;
6417 	  class2 = classify_vr4120_insn (insn2->insn_mo->name);
6418 	  if (vr4120_conflicts[class1] & (1 << class2))
6419 	    return 1;
6420 	}
6421     }
6422 
6423   if (!HAVE_CODE_COMPRESSION)
6424     {
6425       /* Check for GPR or coprocessor load delays.  All such delays
6426 	 are on the RT register.  */
6427       /* Itbl support may require additional care here.  */
6428       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6429 	  || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6430 	{
6431 	  if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6432 	    return 1;
6433 	}
6434 
6435       /* Check for generic coprocessor hazards.
6436 
6437 	 This case is not handled very well.  There is no special
6438 	 knowledge of CP0 handling, and the coprocessors other than
6439 	 the floating point unit are not distinguished at all.  */
6440       /* Itbl support may require additional care here. FIXME!
6441 	 Need to modify this to include knowledge about
6442 	 user specified delays!  */
6443       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6444 	       || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6445 	{
6446 	  /* Handle cases where INSN1 writes to a known general coprocessor
6447 	     register.  There must be a one instruction delay before INSN2
6448 	     if INSN2 reads that register, otherwise no delay is needed.  */
6449 	  mask = fpr_write_mask (insn1);
6450 	  if (mask != 0)
6451 	    {
6452 	      if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6453 		return 1;
6454 	    }
6455 	  else
6456 	    {
6457 	      /* Read-after-write dependencies on the control registers
6458 		 require a two-instruction gap.  */
6459 	      if ((pinfo1 & INSN_WRITE_COND_CODE)
6460 		  && (pinfo2 & INSN_READ_COND_CODE))
6461 		return 2;
6462 
6463 	      /* We don't know exactly what INSN1 does.  If INSN2 is
6464 		 also a coprocessor instruction, assume there must be
6465 		 a one instruction gap.  */
6466 	      if (pinfo2 & INSN_COP)
6467 		return 1;
6468 	    }
6469 	}
6470 
6471       /* Check for read-after-write dependencies on the coprocessor
6472 	 control registers in cases where INSN1 does not need a general
6473 	 coprocessor delay.  This means that INSN1 is a floating point
6474 	 comparison instruction.  */
6475       /* Itbl support may require additional care here.  */
6476       else if (!cop_interlocks
6477 	       && (pinfo1 & INSN_WRITE_COND_CODE)
6478 	       && (pinfo2 & INSN_READ_COND_CODE))
6479 	return 1;
6480     }
6481 
6482   /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6483      CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6484      and pause.  */
6485   if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6486       && ((pinfo2 & INSN_NO_DELAY_SLOT)
6487 	  || (insn2 && delayed_branch_p (insn2))))
6488     return 1;
6489 
6490   return 0;
6491 }
6492 
6493 /* Return the number of nops that would be needed to work around the
6494    VR4130 mflo/mfhi errata if instruction INSN immediately followed
6495    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
6496    that are contained within the first IGNORE instructions of HIST.  */
6497 
6498 static int
6499 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6500 		 const struct mips_cl_insn *insn)
6501 {
6502   int i, j;
6503   unsigned int mask;
6504 
6505   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
6506      are not affected by the errata.  */
6507   if (insn != 0
6508       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6509 	  || strcmp (insn->insn_mo->name, "mtlo") == 0
6510 	  || strcmp (insn->insn_mo->name, "mthi") == 0))
6511     return 0;
6512 
6513   /* Search for the first MFLO or MFHI.  */
6514   for (i = 0; i < MAX_VR4130_NOPS; i++)
6515     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6516       {
6517 	/* Extract the destination register.  */
6518 	mask = gpr_write_mask (&hist[i]);
6519 
6520 	/* No nops are needed if INSN reads that register.  */
6521 	if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6522 	  return 0;
6523 
6524 	/* ...or if any of the intervening instructions do.  */
6525 	for (j = 0; j < i; j++)
6526 	  if (gpr_read_mask (&hist[j]) & mask)
6527 	    return 0;
6528 
6529 	if (i >= ignore)
6530 	  return MAX_VR4130_NOPS - i;
6531       }
6532   return 0;
6533 }
6534 
6535 #define BASE_REG_EQ(INSN1, INSN2)	\
6536   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS)	\
6537       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6538 
6539 /* Return the minimum alignment for this store instruction.  */
6540 
6541 static int
6542 fix_24k_align_to (const struct mips_opcode *mo)
6543 {
6544   if (strcmp (mo->name, "sh") == 0)
6545     return 2;
6546 
6547   if (strcmp (mo->name, "swc1") == 0
6548       || strcmp (mo->name, "swc2") == 0
6549       || strcmp (mo->name, "sw") == 0
6550       || strcmp (mo->name, "sc") == 0
6551       || strcmp (mo->name, "s.s") == 0)
6552     return 4;
6553 
6554   if (strcmp (mo->name, "sdc1") == 0
6555       || strcmp (mo->name, "sdc2") == 0
6556       || strcmp (mo->name, "s.d") == 0)
6557     return 8;
6558 
6559   /* sb, swl, swr */
6560   return 1;
6561 }
6562 
6563 struct fix_24k_store_info
6564   {
6565     /* Immediate offset, if any, for this store instruction.  */
6566     short off;
6567     /* Alignment required by this store instruction.  */
6568     int align_to;
6569     /* True for register offsets.  */
6570     int register_offset;
6571   };
6572 
6573 /* Comparison function used by qsort.  */
6574 
6575 static int
6576 fix_24k_sort (const void *a, const void *b)
6577 {
6578   const struct fix_24k_store_info *pos1 = a;
6579   const struct fix_24k_store_info *pos2 = b;
6580 
6581   return (pos1->off - pos2->off);
6582 }
6583 
6584 /* INSN is a store instruction.  Try to record the store information
6585    in STINFO.  Return false if the information isn't known.  */
6586 
6587 static bfd_boolean
6588 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6589 			   const struct mips_cl_insn *insn)
6590 {
6591   /* The instruction must have a known offset.  */
6592   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6593     return FALSE;
6594 
6595   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6596   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6597   return TRUE;
6598 }
6599 
6600 /* Return the number of nops that would be needed to work around the 24k
6601    "lost data on stores during refill" errata if instruction INSN
6602    immediately followed the 2 instructions described by HIST.
6603    Ignore hazards that are contained within the first IGNORE
6604    instructions of HIST.
6605 
6606    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6607    for the data cache refills and store data. The following describes
6608    the scenario where the store data could be lost.
6609 
6610    * A data cache miss, due to either a load or a store, causing fill
6611      data to be supplied by the memory subsystem
6612    * The first three doublewords of fill data are returned and written
6613      into the cache
6614    * A sequence of four stores occurs in consecutive cycles around the
6615      final doubleword of the fill:
6616    * Store A
6617    * Store B
6618    * Store C
6619    * Zero, One or more instructions
6620    * Store D
6621 
6622    The four stores A-D must be to different doublewords of the line that
6623    is being filled. The fourth instruction in the sequence above permits
6624    the fill of the final doubleword to be transferred from the FSB into
6625    the cache. In the sequence above, the stores may be either integer
6626    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6627    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6628    different doublewords on the line. If the floating point unit is
6629    running in 1:2 mode, it is not possible to create the sequence above
6630    using only floating point store instructions.
6631 
6632    In this case, the cache line being filled is incorrectly marked
6633    invalid, thereby losing the data from any store to the line that
6634    occurs between the original miss and the completion of the five
6635    cycle sequence shown above.
6636 
6637    The workarounds are:
6638 
6639    * Run the data cache in write-through mode.
6640    * Insert a non-store instruction between
6641      Store A and Store B or Store B and Store C.  */
6642 
6643 static int
6644 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6645 	      const struct mips_cl_insn *insn)
6646 {
6647   struct fix_24k_store_info pos[3];
6648   int align, i, base_offset;
6649 
6650   if (ignore >= 2)
6651     return 0;
6652 
6653   /* If the previous instruction wasn't a store, there's nothing to
6654      worry about.  */
6655   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6656     return 0;
6657 
6658   /* If the instructions after the previous one are unknown, we have
6659      to assume the worst.  */
6660   if (!insn)
6661     return 1;
6662 
6663   /* Check whether we are dealing with three consecutive stores.  */
6664   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6665       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6666     return 0;
6667 
6668   /* If we don't know the relationship between the store addresses,
6669      assume the worst.  */
6670   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6671       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6672     return 1;
6673 
6674   if (!fix_24k_record_store_info (&pos[0], insn)
6675       || !fix_24k_record_store_info (&pos[1], &hist[0])
6676       || !fix_24k_record_store_info (&pos[2], &hist[1]))
6677     return 1;
6678 
6679   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6680 
6681   /* Pick a value of ALIGN and X such that all offsets are adjusted by
6682      X bytes and such that the base register + X is known to be aligned
6683      to align bytes.  */
6684 
6685   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6686     align = 8;
6687   else
6688     {
6689       align = pos[0].align_to;
6690       base_offset = pos[0].off;
6691       for (i = 1; i < 3; i++)
6692 	if (align < pos[i].align_to)
6693 	  {
6694 	    align = pos[i].align_to;
6695 	    base_offset = pos[i].off;
6696 	  }
6697       for (i = 0; i < 3; i++)
6698 	pos[i].off -= base_offset;
6699     }
6700 
6701   pos[0].off &= ~align + 1;
6702   pos[1].off &= ~align + 1;
6703   pos[2].off &= ~align + 1;
6704 
6705   /* If any two stores write to the same chunk, they also write to the
6706      same doubleword.  The offsets are still sorted at this point.  */
6707   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6708     return 0;
6709 
6710   /* A range of at least 9 bytes is needed for the stores to be in
6711      non-overlapping doublewords.  */
6712   if (pos[2].off - pos[0].off <= 8)
6713     return 0;
6714 
6715   if (pos[2].off - pos[1].off >= 24
6716       || pos[1].off - pos[0].off >= 24
6717       || pos[2].off - pos[0].off >= 32)
6718     return 0;
6719 
6720   return 1;
6721 }
6722 
6723 /* Return the number of nops that would be needed if instruction INSN
6724    immediately followed the MAX_NOPS instructions given by HIST,
6725    where HIST[0] is the most recent instruction.  Ignore hazards
6726    between INSN and the first IGNORE instructions in HIST.
6727 
6728    If INSN is null, return the worse-case number of nops for any
6729    instruction.  */
6730 
6731 static int
6732 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6733 	       const struct mips_cl_insn *insn)
6734 {
6735   int i, nops, tmp_nops;
6736 
6737   nops = 0;
6738   for (i = ignore; i < MAX_DELAY_NOPS; i++)
6739     {
6740       tmp_nops = insns_between (hist + i, insn) - i;
6741       if (tmp_nops > nops)
6742 	nops = tmp_nops;
6743     }
6744 
6745   if (mips_fix_vr4130 && !mips_opts.micromips)
6746     {
6747       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6748       if (tmp_nops > nops)
6749 	nops = tmp_nops;
6750     }
6751 
6752   if (mips_fix_24k && !mips_opts.micromips)
6753     {
6754       tmp_nops = nops_for_24k (ignore, hist, insn);
6755       if (tmp_nops > nops)
6756 	nops = tmp_nops;
6757     }
6758 
6759   return nops;
6760 }
6761 
6762 /* The variable arguments provide NUM_INSNS extra instructions that
6763    might be added to HIST.  Return the largest number of nops that
6764    would be needed after the extended sequence, ignoring hazards
6765    in the first IGNORE instructions.  */
6766 
6767 static int
6768 nops_for_sequence (int num_insns, int ignore,
6769 		   const struct mips_cl_insn *hist, ...)
6770 {
6771   va_list args;
6772   struct mips_cl_insn buffer[MAX_NOPS];
6773   struct mips_cl_insn *cursor;
6774   int nops;
6775 
6776   va_start (args, hist);
6777   cursor = buffer + num_insns;
6778   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6779   while (cursor > buffer)
6780     *--cursor = *va_arg (args, const struct mips_cl_insn *);
6781 
6782   nops = nops_for_insn (ignore, buffer, NULL);
6783   va_end (args);
6784   return nops;
6785 }
6786 
6787 /* Like nops_for_insn, but if INSN is a branch, take into account the
6788    worst-case delay for the branch target.  */
6789 
6790 static int
6791 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6792 			 const struct mips_cl_insn *insn)
6793 {
6794   int nops, tmp_nops;
6795 
6796   nops = nops_for_insn (ignore, hist, insn);
6797   if (delayed_branch_p (insn))
6798     {
6799       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6800 				    hist, insn, get_delay_slot_nop (insn));
6801       if (tmp_nops > nops)
6802 	nops = tmp_nops;
6803     }
6804   else if (compact_branch_p (insn))
6805     {
6806       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6807       if (tmp_nops > nops)
6808 	nops = tmp_nops;
6809     }
6810   return nops;
6811 }
6812 
6813 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
6814 
6815 static void
6816 fix_loongson2f_nop (struct mips_cl_insn * ip)
6817 {
6818   gas_assert (!HAVE_CODE_COMPRESSION);
6819   if (strcmp (ip->insn_mo->name, "nop") == 0)
6820     ip->insn_opcode = LOONGSON2F_NOP_INSN;
6821 }
6822 
6823 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6824                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
6825 
6826 static void
6827 fix_loongson2f_jump (struct mips_cl_insn * ip)
6828 {
6829   gas_assert (!HAVE_CODE_COMPRESSION);
6830   if (strcmp (ip->insn_mo->name, "j") == 0
6831       || strcmp (ip->insn_mo->name, "jr") == 0
6832       || strcmp (ip->insn_mo->name, "jalr") == 0)
6833     {
6834       int sreg;
6835       expressionS ep;
6836 
6837       if (! mips_opts.at)
6838         return;
6839 
6840       sreg = EXTRACT_OPERAND (0, RS, *ip);
6841       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6842         return;
6843 
6844       ep.X_op = O_constant;
6845       ep.X_add_number = 0xcfff0000;
6846       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6847       ep.X_add_number = 0xffff;
6848       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6849       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6850     }
6851 }
6852 
6853 static void
6854 fix_loongson2f (struct mips_cl_insn * ip)
6855 {
6856   if (mips_fix_loongson2f_nop)
6857     fix_loongson2f_nop (ip);
6858 
6859   if (mips_fix_loongson2f_jump)
6860     fix_loongson2f_jump (ip);
6861 }
6862 
6863 /* IP is a branch that has a delay slot, and we need to fill it
6864    automatically.   Return true if we can do that by swapping IP
6865    with the previous instruction.
6866    ADDRESS_EXPR is an operand of the instruction to be used with
6867    RELOC_TYPE.  */
6868 
6869 static bfd_boolean
6870 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
6871 		   bfd_reloc_code_real_type *reloc_type)
6872 {
6873   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
6874   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
6875   unsigned int fpr_read, prev_fpr_write;
6876 
6877   /* -O2 and above is required for this optimization.  */
6878   if (mips_optimize < 2)
6879     return FALSE;
6880 
6881   /* If we have seen .set volatile or .set nomove, don't optimize.  */
6882   if (mips_opts.nomove)
6883     return FALSE;
6884 
6885   /* We can't swap if the previous instruction's position is fixed.  */
6886   if (history[0].fixed_p)
6887     return FALSE;
6888 
6889   /* If the previous previous insn was in a .set noreorder, we can't
6890      swap.  Actually, the MIPS assembler will swap in this situation.
6891      However, gcc configured -with-gnu-as will generate code like
6892 
6893 	.set	noreorder
6894 	lw	$4,XXX
6895 	.set	reorder
6896 	INSN
6897 	bne	$4,$0,foo
6898 
6899      in which we can not swap the bne and INSN.  If gcc is not configured
6900      -with-gnu-as, it does not output the .set pseudo-ops.  */
6901   if (history[1].noreorder_p)
6902     return FALSE;
6903 
6904   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6905      This means that the previous instruction was a 4-byte one anyhow.  */
6906   if (mips_opts.mips16 && history[0].fixp[0])
6907     return FALSE;
6908 
6909   if (mips_fix_loongson2f)
6910     fix_loongson2f (ip);
6911   if (mips_trap_zero_jump)
6912     trap_zero_jump (ip);
6913 
6914   /* If the branch is itself the target of a branch, we can not swap.
6915      We cheat on this; all we check for is whether there is a label on
6916      this instruction.  If there are any branches to anything other than
6917      a label, users must use .set noreorder.  */
6918   if (seg_info (now_seg)->label_list)
6919     return FALSE;
6920 
6921   /* If the previous instruction is in a variant frag other than this
6922      branch's one, we cannot do the swap.  This does not apply to
6923      MIPS16 code, which uses variant frags for different purposes.  */
6924   if (!mips_opts.mips16
6925       && history[0].frag
6926       && history[0].frag->fr_type == rs_machine_dependent)
6927     return FALSE;
6928 
6929   /* We do not swap with instructions that cannot architecturally
6930      be placed in a branch delay slot, such as SYNC or ERET.  We
6931      also refrain from swapping with a trap instruction, since it
6932      complicates trap handlers to have the trap instruction be in
6933      a delay slot.  */
6934   prev_pinfo = history[0].insn_mo->pinfo;
6935   if (prev_pinfo & INSN_NO_DELAY_SLOT)
6936     return FALSE;
6937 
6938   /* Check for conflicts between the branch and the instructions
6939      before the candidate delay slot.  */
6940   if (nops_for_insn (0, history + 1, ip) > 0)
6941     return FALSE;
6942 
6943   /* Check for conflicts between the swapped sequence and the
6944      target of the branch.  */
6945   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6946     return FALSE;
6947 
6948   /* If the branch reads a register that the previous
6949      instruction sets, we can not swap.  */
6950   gpr_read = gpr_read_mask (ip);
6951   prev_gpr_write = gpr_write_mask (&history[0]);
6952   if (gpr_read & prev_gpr_write)
6953     return FALSE;
6954 
6955   fpr_read = fpr_read_mask (ip);
6956   prev_fpr_write = fpr_write_mask (&history[0]);
6957   if (fpr_read & prev_fpr_write)
6958     return FALSE;
6959 
6960   /* If the branch writes a register that the previous
6961      instruction sets, we can not swap.  */
6962   gpr_write = gpr_write_mask (ip);
6963   if (gpr_write & prev_gpr_write)
6964     return FALSE;
6965 
6966   /* If the branch writes a register that the previous
6967      instruction reads, we can not swap.  */
6968   prev_gpr_read = gpr_read_mask (&history[0]);
6969   if (gpr_write & prev_gpr_read)
6970     return FALSE;
6971 
6972   /* If one instruction sets a condition code and the
6973      other one uses a condition code, we can not swap.  */
6974   pinfo = ip->insn_mo->pinfo;
6975   if ((pinfo & INSN_READ_COND_CODE)
6976       && (prev_pinfo & INSN_WRITE_COND_CODE))
6977     return FALSE;
6978   if ((pinfo & INSN_WRITE_COND_CODE)
6979       && (prev_pinfo & INSN_READ_COND_CODE))
6980     return FALSE;
6981 
6982   /* If the previous instruction uses the PC, we can not swap.  */
6983   prev_pinfo2 = history[0].insn_mo->pinfo2;
6984   if (prev_pinfo2 & INSN2_READ_PC)
6985     return FALSE;
6986 
6987   /* If the previous instruction has an incorrect size for a fixed
6988      branch delay slot in microMIPS mode, we cannot swap.  */
6989   pinfo2 = ip->insn_mo->pinfo2;
6990   if (mips_opts.micromips
6991       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6992       && insn_length (history) != 2)
6993     return FALSE;
6994   if (mips_opts.micromips
6995       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6996       && insn_length (history) != 4)
6997     return FALSE;
6998 
6999   /* On R5900 short loops need to be fixed by inserting a nop in
7000      the branch delay slots.
7001      A short loop can be terminated too early.  */
7002   if (mips_opts.arch == CPU_R5900
7003       /* Check if instruction has a parameter, ignore "j $31". */
7004       && (address_expr != NULL)
7005       /* Parameter must be 16 bit. */
7006       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7007       /* Branch to same segment. */
7008       && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
7009       /* Branch to same code fragment. */
7010       && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
7011       /* Can only calculate branch offset if value is known. */
7012       && symbol_constant_p (address_expr->X_add_symbol)
7013       /* Check if branch is really conditional. */
7014       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
7015 	|| (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
7016 	|| (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7017     {
7018       int distance;
7019       /* Check if loop is shorter than 6 instructions including
7020          branch and delay slot.  */
7021       distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
7022       if (distance <= 20)
7023         {
7024           int i;
7025           int rv;
7026 
7027           rv = FALSE;
7028           /* When the loop includes branches or jumps,
7029              it is not a short loop. */
7030           for (i = 0; i < (distance / 4); i++)
7031             {
7032               if ((history[i].cleared_p)
7033                   || delayed_branch_p (&history[i]))
7034                 {
7035                   rv = TRUE;
7036                   break;
7037                 }
7038             }
7039           if (!rv)
7040             {
7041               /* Insert nop after branch to fix short loop. */
7042               return FALSE;
7043             }
7044         }
7045     }
7046 
7047   return TRUE;
7048 }
7049 
7050 /* Fix jump through register issue on loongson2f processor for kernel code:
7051    force a BTB clear before the jump to prevent it from being incorrectly
7052    prefetched by the branch prediction engine. */
7053 
7054 static void
7055 macro_build_jrpatch (expressionS *ep, unsigned int sreg)
7056 {
7057   if (!mips_fix_loongson2f_btb)
7058     return;
7059 
7060   if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == AT)
7061     return;
7062 
7063   if (!mips_opts.at)
7064     {
7065       as_warn (_("unable to apply loongson2f BTB workaround when .set noat"));
7066       return;
7067     }
7068 
7069   /* li $at, COP_0_BTB_CLEAR | COP_0_RAS_DISABLE */
7070   ep->X_op = O_constant;
7071   ep->X_add_number = 3;
7072   macro_build (ep, "ori", "t,r,i", AT, ZERO, BFD_RELOC_LO16);
7073 
7074   /* dmtc0 $at, COP_0_DIAG */
7075   macro_build (NULL, "dmtc0", "t,G", AT, 22);
7076 
7077   /* Hide these two instructions to avoid getting a ``macro expanded into
7078      multiple instructions'' warning. */
7079   if (mips_relax.sequence != 2) {
7080     mips_macro_warning.sizes[0] -= 2 * 4;
7081     mips_macro_warning.insns[0] -= 2;
7082   }
7083   if (mips_relax.sequence != 1) {
7084     mips_macro_warning.sizes[1] -= 2 * 4;
7085     mips_macro_warning.insns[1] -= 2;
7086   }
7087 }
7088 
7089 /* Decide how we should add IP to the instruction stream.
7090    ADDRESS_EXPR is an operand of the instruction to be used with
7091    RELOC_TYPE.  */
7092 
7093 static enum append_method
7094 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7095 		   bfd_reloc_code_real_type *reloc_type)
7096 {
7097   /* The relaxed version of a macro sequence must be inherently
7098      hazard-free.  */
7099   if (mips_relax.sequence == 2)
7100     return APPEND_ADD;
7101 
7102   /* We must not dabble with instructions in a ".set noreorder" block.  */
7103   if (mips_opts.noreorder)
7104     return APPEND_ADD;
7105 
7106   /* Otherwise, it's our responsibility to fill branch delay slots.  */
7107   if (delayed_branch_p (ip))
7108     {
7109       if (!branch_likely_p (ip)
7110 	  && can_swap_branch_p (ip, address_expr, reloc_type))
7111 	return APPEND_SWAP;
7112 
7113       if (mips_opts.mips16
7114 	  && ISA_SUPPORTS_MIPS16E
7115 	  && gpr_read_mask (ip) != 0)
7116 	return APPEND_ADD_COMPACT;
7117 
7118       if (mips_opts.micromips
7119 	  && ((ip->insn_opcode & 0xffe0) == 0x4580
7120 	      || (!forced_insn_length
7121 		  && ((ip->insn_opcode & 0xfc00) == 0xcc00
7122 		      || (ip->insn_opcode & 0xdc00) == 0x8c00))
7123 	      || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7124 	      || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7125 	return APPEND_ADD_COMPACT;
7126 
7127       return APPEND_ADD_WITH_NOP;
7128     }
7129 
7130   return APPEND_ADD;
7131 }
7132 
7133 /* IP is an instruction whose opcode we have just changed, END points
7134    to the end of the opcode table processed.  Point IP->insn_mo to the
7135    new opcode's definition.  */
7136 
7137 static void
7138 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7139 {
7140   const struct mips_opcode *mo;
7141 
7142   for (mo = ip->insn_mo; mo < end; mo++)
7143     if (mo->pinfo != INSN_MACRO
7144 	&& (ip->insn_opcode & mo->mask) == mo->match)
7145       {
7146 	ip->insn_mo = mo;
7147 	return;
7148       }
7149   abort ();
7150 }
7151 
7152 /* IP is a MIPS16 instruction whose opcode we have just changed.
7153    Point IP->insn_mo to the new opcode's definition.  */
7154 
7155 static void
7156 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7157 {
7158   find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7159 }
7160 
7161 /* IP is a microMIPS instruction whose opcode we have just changed.
7162    Point IP->insn_mo to the new opcode's definition.  */
7163 
7164 static void
7165 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7166 {
7167   find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7168 }
7169 
7170 /* For microMIPS macros, we need to generate a local number label
7171    as the target of branches.  */
7172 #define MICROMIPS_LABEL_CHAR		'\037'
7173 static unsigned long micromips_target_label;
7174 static char micromips_target_name[32];
7175 
7176 static char *
7177 micromips_label_name (void)
7178 {
7179   char *p = micromips_target_name;
7180   char symbol_name_temporary[24];
7181   unsigned long l;
7182   int i;
7183 
7184   if (*p)
7185     return p;
7186 
7187   i = 0;
7188   l = micromips_target_label;
7189 #ifdef LOCAL_LABEL_PREFIX
7190   *p++ = LOCAL_LABEL_PREFIX;
7191 #endif
7192   *p++ = 'L';
7193   *p++ = MICROMIPS_LABEL_CHAR;
7194   do
7195     {
7196       symbol_name_temporary[i++] = l % 10 + '0';
7197       l /= 10;
7198     }
7199   while (l != 0);
7200   while (i > 0)
7201     *p++ = symbol_name_temporary[--i];
7202   *p = '\0';
7203 
7204   return micromips_target_name;
7205 }
7206 
7207 static void
7208 micromips_label_expr (expressionS *label_expr)
7209 {
7210   label_expr->X_op = O_symbol;
7211   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7212   label_expr->X_add_number = 0;
7213 }
7214 
7215 static void
7216 micromips_label_inc (void)
7217 {
7218   micromips_target_label++;
7219   *micromips_target_name = '\0';
7220 }
7221 
7222 static void
7223 micromips_add_label (void)
7224 {
7225   symbolS *s;
7226 
7227   s = colon (micromips_label_name ());
7228   micromips_label_inc ();
7229   S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7230 }
7231 
7232 /* If assembling microMIPS code, then return the microMIPS reloc
7233    corresponding to the requested one if any.  Otherwise return
7234    the reloc unchanged.  */
7235 
7236 static bfd_reloc_code_real_type
7237 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7238 {
7239   static const bfd_reloc_code_real_type relocs[][2] =
7240     {
7241       /* Keep sorted incrementally by the left-hand key.  */
7242       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7243       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7244       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7245       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7246       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7247       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7248       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7249       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7250       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7251       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7252       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7253       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7254       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7255       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7256       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7257       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7258       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7259       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7260       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7261       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7262       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7263       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7264       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7265       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7266       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7267       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7268       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7269     };
7270   bfd_reloc_code_real_type r;
7271   size_t i;
7272 
7273   if (!mips_opts.micromips)
7274     return reloc;
7275   for (i = 0; i < ARRAY_SIZE (relocs); i++)
7276     {
7277       r = relocs[i][0];
7278       if (r > reloc)
7279 	return reloc;
7280       if (r == reloc)
7281 	return relocs[i][1];
7282     }
7283   return reloc;
7284 }
7285 
7286 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7287    Return true on success, storing the resolved value in RESULT.  */
7288 
7289 static bfd_boolean
7290 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7291 		 offsetT *result)
7292 {
7293   switch (reloc)
7294     {
7295     case BFD_RELOC_MIPS_HIGHEST:
7296     case BFD_RELOC_MICROMIPS_HIGHEST:
7297       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7298       return TRUE;
7299 
7300     case BFD_RELOC_MIPS_HIGHER:
7301     case BFD_RELOC_MICROMIPS_HIGHER:
7302       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7303       return TRUE;
7304 
7305     case BFD_RELOC_HI16_S:
7306     case BFD_RELOC_HI16_S_PCREL:
7307     case BFD_RELOC_MICROMIPS_HI16_S:
7308     case BFD_RELOC_MIPS16_HI16_S:
7309       *result = ((operand + 0x8000) >> 16) & 0xffff;
7310       return TRUE;
7311 
7312     case BFD_RELOC_HI16:
7313     case BFD_RELOC_MICROMIPS_HI16:
7314     case BFD_RELOC_MIPS16_HI16:
7315       *result = (operand >> 16) & 0xffff;
7316       return TRUE;
7317 
7318     case BFD_RELOC_LO16:
7319     case BFD_RELOC_LO16_PCREL:
7320     case BFD_RELOC_MICROMIPS_LO16:
7321     case BFD_RELOC_MIPS16_LO16:
7322       *result = operand & 0xffff;
7323       return TRUE;
7324 
7325     case BFD_RELOC_UNUSED:
7326       *result = operand;
7327       return TRUE;
7328 
7329     default:
7330       return FALSE;
7331     }
7332 }
7333 
7334 /* Output an instruction.  IP is the instruction information.
7335    ADDRESS_EXPR is an operand of the instruction to be used with
7336    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
7337    a macro expansion.  */
7338 
7339 static void
7340 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7341 	     bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
7342 {
7343   unsigned long prev_pinfo2, pinfo;
7344   bfd_boolean relaxed_branch = FALSE;
7345   enum append_method method;
7346   bfd_boolean relax32;
7347   int branch_disp;
7348 
7349   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7350     fix_loongson2f (ip);
7351 
7352   file_ase_mips16 |= mips_opts.mips16;
7353   file_ase_micromips |= mips_opts.micromips;
7354 
7355   prev_pinfo2 = history[0].insn_mo->pinfo2;
7356   pinfo = ip->insn_mo->pinfo;
7357 
7358   /* Don't raise alarm about `nods' frags as they'll fill in the right
7359      kind of nop in relaxation if required.  */
7360   if (mips_opts.micromips
7361       && !expansionp
7362       && !(history[0].frag
7363 	   && history[0].frag->fr_type == rs_machine_dependent
7364 	   && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7365 	   && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7366       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7367 	   && micromips_insn_length (ip->insn_mo) != 2)
7368 	  || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7369 	      && micromips_insn_length (ip->insn_mo) != 4)))
7370     as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7371 	     (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7372 
7373   if (address_expr == NULL)
7374     ip->complete_p = 1;
7375   else if (reloc_type[0] <= BFD_RELOC_UNUSED
7376 	   && reloc_type[1] == BFD_RELOC_UNUSED
7377 	   && reloc_type[2] == BFD_RELOC_UNUSED
7378 	   && address_expr->X_op == O_constant)
7379     {
7380       switch (*reloc_type)
7381 	{
7382 	case BFD_RELOC_MIPS_JMP:
7383 	  {
7384 	    int shift;
7385 
7386 	    /* Shift is 2, unusually, for microMIPS JALX.  */
7387 	    shift = (mips_opts.micromips
7388 		     && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7389 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7390 	      as_bad (_("jump to misaligned address (0x%lx)"),
7391 		      (unsigned long) address_expr->X_add_number);
7392 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7393 				& 0x3ffffff);
7394 	    ip->complete_p = 1;
7395 	  }
7396 	  break;
7397 
7398 	case BFD_RELOC_MIPS16_JMP:
7399 	  if ((address_expr->X_add_number & 3) != 0)
7400 	    as_bad (_("jump to misaligned address (0x%lx)"),
7401 	            (unsigned long) address_expr->X_add_number);
7402 	  ip->insn_opcode |=
7403 	    (((address_expr->X_add_number & 0x7c0000) << 3)
7404 	       | ((address_expr->X_add_number & 0xf800000) >> 7)
7405 	       | ((address_expr->X_add_number & 0x3fffc) >> 2));
7406 	  ip->complete_p = 1;
7407 	  break;
7408 
7409 	case BFD_RELOC_16_PCREL_S2:
7410 	  {
7411 	    int shift;
7412 
7413 	    shift = mips_opts.micromips ? 1 : 2;
7414 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7415 	      as_bad (_("branch to misaligned address (0x%lx)"),
7416 		      (unsigned long) address_expr->X_add_number);
7417 	    if (!mips_relax_branch)
7418 	      {
7419 		if ((address_expr->X_add_number + (1 << (shift + 15)))
7420 		    & ~((1 << (shift + 16)) - 1))
7421 		  as_bad (_("branch address range overflow (0x%lx)"),
7422 			  (unsigned long) address_expr->X_add_number);
7423 		ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7424 				    & 0xffff);
7425 	      }
7426 	  }
7427 	  break;
7428 
7429 	case BFD_RELOC_MIPS_21_PCREL_S2:
7430 	  {
7431 	    int shift;
7432 
7433 	    shift = 2;
7434 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7435 	      as_bad (_("branch to misaligned address (0x%lx)"),
7436 		      (unsigned long) address_expr->X_add_number);
7437 	    if ((address_expr->X_add_number + (1 << (shift + 20)))
7438 		& ~((1 << (shift + 21)) - 1))
7439 	      as_bad (_("branch address range overflow (0x%lx)"),
7440 		      (unsigned long) address_expr->X_add_number);
7441 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7442 				& 0x1fffff);
7443 	  }
7444 	  break;
7445 
7446 	case BFD_RELOC_MIPS_26_PCREL_S2:
7447 	  {
7448 	    int shift;
7449 
7450 	    shift = 2;
7451 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7452 	      as_bad (_("branch to misaligned address (0x%lx)"),
7453 		      (unsigned long) address_expr->X_add_number);
7454 	    if ((address_expr->X_add_number + (1 << (shift + 25)))
7455 		& ~((1 << (shift + 26)) - 1))
7456 	      as_bad (_("branch address range overflow (0x%lx)"),
7457 		      (unsigned long) address_expr->X_add_number);
7458 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7459 				& 0x3ffffff);
7460 	  }
7461 	  break;
7462 
7463 	default:
7464 	  {
7465 	    offsetT value;
7466 
7467 	    if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7468 				 &value))
7469 	      {
7470 		ip->insn_opcode |= value & 0xffff;
7471 		ip->complete_p = 1;
7472 	      }
7473 	  }
7474 	  break;
7475 	}
7476     }
7477 
7478   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7479     {
7480       /* There are a lot of optimizations we could do that we don't.
7481 	 In particular, we do not, in general, reorder instructions.
7482 	 If you use gcc with optimization, it will reorder
7483 	 instructions and generally do much more optimization then we
7484 	 do here; repeating all that work in the assembler would only
7485 	 benefit hand written assembly code, and does not seem worth
7486 	 it.  */
7487       int nops = (mips_optimize == 0
7488 		  ? nops_for_insn (0, history, NULL)
7489 		  : nops_for_insn_or_target (0, history, ip));
7490       if (nops > 0)
7491 	{
7492 	  fragS *old_frag;
7493 	  unsigned long old_frag_offset;
7494 	  int i;
7495 
7496 	  old_frag = frag_now;
7497 	  old_frag_offset = frag_now_fix ();
7498 
7499 	  for (i = 0; i < nops; i++)
7500 	    add_fixed_insn (NOP_INSN);
7501 	  insert_into_history (0, nops, NOP_INSN);
7502 
7503 	  if (listing)
7504 	    {
7505 	      listing_prev_line ();
7506 	      /* We may be at the start of a variant frag.  In case we
7507                  are, make sure there is enough space for the frag
7508                  after the frags created by listing_prev_line.  The
7509                  argument to frag_grow here must be at least as large
7510                  as the argument to all other calls to frag_grow in
7511                  this file.  We don't have to worry about being in the
7512                  middle of a variant frag, because the variants insert
7513                  all needed nop instructions themselves.  */
7514 	      frag_grow (40);
7515 	    }
7516 
7517 	  mips_move_text_labels ();
7518 
7519 #ifndef NO_ECOFF_DEBUGGING
7520 	  if (ECOFF_DEBUGGING)
7521 	    ecoff_fix_loc (old_frag, old_frag_offset);
7522 #endif
7523 	}
7524     }
7525   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7526     {
7527       int nops;
7528 
7529       /* Work out how many nops in prev_nop_frag are needed by IP,
7530 	 ignoring hazards generated by the first prev_nop_frag_since
7531 	 instructions.  */
7532       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7533       gas_assert (nops <= prev_nop_frag_holds);
7534 
7535       /* Enforce NOPS as a minimum.  */
7536       if (nops > prev_nop_frag_required)
7537 	prev_nop_frag_required = nops;
7538 
7539       if (prev_nop_frag_holds == prev_nop_frag_required)
7540 	{
7541 	  /* Settle for the current number of nops.  Update the history
7542 	     accordingly (for the benefit of any future .set reorder code).  */
7543 	  prev_nop_frag = NULL;
7544 	  insert_into_history (prev_nop_frag_since,
7545 			       prev_nop_frag_holds, NOP_INSN);
7546 	}
7547       else
7548 	{
7549 	  /* Allow this instruction to replace one of the nops that was
7550 	     tentatively added to prev_nop_frag.  */
7551 	  prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7552 	  prev_nop_frag_holds--;
7553 	  prev_nop_frag_since++;
7554 	}
7555     }
7556 
7557   method = get_append_method (ip, address_expr, reloc_type);
7558   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7559 
7560   dwarf2_emit_insn (0);
7561   /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7562      so "move" the instruction address accordingly.
7563 
7564      Also, it doesn't seem appropriate for the assembler to reorder .loc
7565      entries.  If this instruction is a branch that we are going to swap
7566      with the previous instruction, the two instructions should be
7567      treated as a unit, and the debug information for both instructions
7568      should refer to the start of the branch sequence.  Using the
7569      current position is certainly wrong when swapping a 32-bit branch
7570      and a 16-bit delay slot, since the current position would then be
7571      in the middle of a branch.  */
7572   dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7573 
7574   relax32 = (mips_relax_branch
7575 	     /* Don't try branch relaxation within .set nomacro, or within
7576 	        .set noat if we use $at for PIC computations.  If it turns
7577 	        out that the branch was out-of-range, we'll get an error.  */
7578 	     && !mips_opts.warn_about_macros
7579 	     && (mips_opts.at || mips_pic == NO_PIC)
7580 	     /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7581 	        as they have no complementing branches.  */
7582 	     && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7583 
7584   if (!HAVE_CODE_COMPRESSION
7585       && address_expr
7586       && relax32
7587       && *reloc_type == BFD_RELOC_16_PCREL_S2
7588       && delayed_branch_p (ip))
7589     {
7590       relaxed_branch = TRUE;
7591       add_relaxed_insn (ip, (relaxed_branch_length
7592 			     (NULL, NULL,
7593 			      uncond_branch_p (ip) ? -1
7594 			      : branch_likely_p (ip) ? 1
7595 			      : 0)), 4,
7596 			RELAX_BRANCH_ENCODE
7597 			(AT, mips_pic != NO_PIC,
7598 			 uncond_branch_p (ip),
7599 			 branch_likely_p (ip),
7600 			 pinfo & INSN_WRITE_GPR_31,
7601 			 0),
7602 			address_expr->X_add_symbol,
7603 			address_expr->X_add_number);
7604       *reloc_type = BFD_RELOC_UNUSED;
7605     }
7606   else if (mips_opts.micromips
7607 	   && address_expr
7608 	   && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7609 	       || *reloc_type > BFD_RELOC_UNUSED)
7610 	   && (delayed_branch_p (ip) || compact_branch_p (ip))
7611 	   /* Don't try branch relaxation when users specify
7612 	      16-bit/32-bit instructions.  */
7613 	   && !forced_insn_length)
7614     {
7615       bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7616 			     && *reloc_type > BFD_RELOC_UNUSED);
7617       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7618       int uncond = uncond_branch_p (ip) ? -1 : 0;
7619       int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7620       int nods = method == APPEND_ADD_WITH_NOP;
7621       int al = pinfo & INSN_WRITE_GPR_31;
7622       int length32 = nods ? 8 : 4;
7623 
7624       gas_assert (address_expr != NULL);
7625       gas_assert (!mips_relax.sequence);
7626 
7627       relaxed_branch = TRUE;
7628       if (nods)
7629 	method = APPEND_ADD;
7630       if (relax32)
7631 	length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7632       add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7633 			RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7634 						mips_pic != NO_PIC,
7635 						uncond, compact, al, nods,
7636 						relax32, 0, 0),
7637 			address_expr->X_add_symbol,
7638 			address_expr->X_add_number);
7639       *reloc_type = BFD_RELOC_UNUSED;
7640     }
7641   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7642     {
7643       bfd_boolean require_unextended;
7644       bfd_boolean require_extended;
7645       symbolS *symbol;
7646       offsetT offset;
7647 
7648       if (forced_insn_length != 0)
7649 	{
7650 	  require_unextended = forced_insn_length == 2;
7651 	  require_extended = forced_insn_length == 4;
7652 	}
7653       else
7654 	{
7655 	  require_unextended = (mips_opts.noautoextend
7656 				&& !mips_opcode_32bit_p (ip->insn_mo));
7657 	  require_extended = 0;
7658 	}
7659 
7660       /* We need to set up a variant frag.  */
7661       gas_assert (address_expr != NULL);
7662       /* Pass any `O_symbol' expression unchanged as an `expr_section'
7663          symbol created by `make_expr_symbol' may not get a necessary
7664          external relocation produced.  */
7665       if (address_expr->X_op == O_symbol)
7666 	{
7667 	  symbol = address_expr->X_add_symbol;
7668 	  offset = address_expr->X_add_number;
7669 	}
7670       else
7671 	{
7672 	  symbol = make_expr_symbol (address_expr);
7673 	  symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7674 	  offset = 0;
7675 	}
7676       add_relaxed_insn (ip, 12, 0,
7677 			RELAX_MIPS16_ENCODE
7678 			(*reloc_type - BFD_RELOC_UNUSED,
7679 			 mips_opts.ase & ASE_MIPS16E2,
7680 			 mips_pic != NO_PIC,
7681 			 HAVE_32BIT_SYMBOLS,
7682 			 mips_opts.warn_about_macros,
7683 			 require_unextended, require_extended,
7684 			 delayed_branch_p (&history[0]),
7685 			 history[0].mips16_absolute_jump_p),
7686 			symbol, offset);
7687     }
7688   else if (mips_opts.mips16 && insn_length (ip) == 2)
7689     {
7690       if (!delayed_branch_p (ip))
7691 	/* Make sure there is enough room to swap this instruction with
7692 	   a following jump instruction.  */
7693 	frag_grow (6);
7694       add_fixed_insn (ip);
7695     }
7696   else
7697     {
7698       if (mips_opts.mips16
7699 	  && mips_opts.noreorder
7700 	  && delayed_branch_p (&history[0]))
7701 	as_warn (_("extended instruction in delay slot"));
7702 
7703       if (mips_relax.sequence)
7704 	{
7705 	  /* If we've reached the end of this frag, turn it into a variant
7706 	     frag and record the information for the instructions we've
7707 	     written so far.  */
7708 	  if (frag_room () < 4)
7709 	    relax_close_frag ();
7710 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7711 	}
7712 
7713       if (mips_relax.sequence != 2)
7714 	{
7715 	  if (mips_macro_warning.first_insn_sizes[0] == 0)
7716 	    mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7717 	  mips_macro_warning.sizes[0] += insn_length (ip);
7718 	  mips_macro_warning.insns[0]++;
7719 	}
7720       if (mips_relax.sequence != 1)
7721 	{
7722 	  if (mips_macro_warning.first_insn_sizes[1] == 0)
7723 	    mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7724 	  mips_macro_warning.sizes[1] += insn_length (ip);
7725 	  mips_macro_warning.insns[1]++;
7726 	}
7727 
7728       if (mips_opts.mips16)
7729 	{
7730 	  ip->fixed_p = 1;
7731 	  ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7732 	}
7733       add_fixed_insn (ip);
7734     }
7735 
7736   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7737     {
7738       bfd_reloc_code_real_type final_type[3];
7739       reloc_howto_type *howto0;
7740       reloc_howto_type *howto;
7741       int i;
7742 
7743       /* Perform any necessary conversion to microMIPS relocations
7744 	 and find out how many relocations there actually are.  */
7745       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7746 	final_type[i] = micromips_map_reloc (reloc_type[i]);
7747 
7748       /* In a compound relocation, it is the final (outermost)
7749 	 operator that determines the relocated field.  */
7750       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7751       if (!howto)
7752 	abort ();
7753 
7754       if (i > 1)
7755 	howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7756       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7757 				 bfd_get_reloc_size (howto),
7758 				 address_expr,
7759 				 howto0 && howto0->pc_relative,
7760 				 final_type[0]);
7761       /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'.  */
7762       ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7763 
7764       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
7765       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7766 	*symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7767 
7768       /* These relocations can have an addend that won't fit in
7769 	 4 octets for 64bit assembly.  */
7770       if (GPR_SIZE == 64
7771 	  && ! howto->partial_inplace
7772 	  && (reloc_type[0] == BFD_RELOC_16
7773 	      || reloc_type[0] == BFD_RELOC_32
7774 	      || reloc_type[0] == BFD_RELOC_MIPS_JMP
7775 	      || reloc_type[0] == BFD_RELOC_GPREL16
7776 	      || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7777 	      || reloc_type[0] == BFD_RELOC_GPREL32
7778 	      || reloc_type[0] == BFD_RELOC_64
7779 	      || reloc_type[0] == BFD_RELOC_CTOR
7780 	      || reloc_type[0] == BFD_RELOC_MIPS_SUB
7781 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7782 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7783 	      || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7784 	      || reloc_type[0] == BFD_RELOC_MIPS_REL16
7785 	      || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7786 	      || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7787 	      || hi16_reloc_p (reloc_type[0])
7788 	      || lo16_reloc_p (reloc_type[0])))
7789 	ip->fixp[0]->fx_no_overflow = 1;
7790 
7791       /* These relocations can have an addend that won't fit in 2 octets.  */
7792       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7793 	  || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7794 	ip->fixp[0]->fx_no_overflow = 1;
7795 
7796       if (mips_relax.sequence)
7797 	{
7798 	  if (mips_relax.first_fixup == 0)
7799 	    mips_relax.first_fixup = ip->fixp[0];
7800 	}
7801       else if (reloc_needs_lo_p (*reloc_type))
7802 	{
7803 	  struct mips_hi_fixup *hi_fixup;
7804 
7805 	  /* Reuse the last entry if it already has a matching %lo.  */
7806 	  hi_fixup = mips_hi_fixup_list;
7807 	  if (hi_fixup == 0
7808 	      || !fixup_has_matching_lo_p (hi_fixup->fixp))
7809 	    {
7810 	      hi_fixup = XNEW (struct mips_hi_fixup);
7811 	      hi_fixup->next = mips_hi_fixup_list;
7812 	      mips_hi_fixup_list = hi_fixup;
7813 	    }
7814 	  hi_fixup->fixp = ip->fixp[0];
7815 	  hi_fixup->seg = now_seg;
7816 	}
7817 
7818       /* Add fixups for the second and third relocations, if given.
7819 	 Note that the ABI allows the second relocation to be
7820 	 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
7821 	 moment we only use RSS_UNDEF, but we could add support
7822 	 for the others if it ever becomes necessary.  */
7823       for (i = 1; i < 3; i++)
7824 	if (reloc_type[i] != BFD_RELOC_UNUSED)
7825 	  {
7826 	    ip->fixp[i] = fix_new (ip->frag, ip->where,
7827 				   ip->fixp[0]->fx_size, NULL, 0,
7828 				   FALSE, final_type[i]);
7829 
7830 	    /* Use fx_tcbit to mark compound relocs.  */
7831 	    ip->fixp[0]->fx_tcbit = 1;
7832 	    ip->fixp[i]->fx_tcbit = 1;
7833 	  }
7834     }
7835 
7836   /* Update the register mask information.  */
7837   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7838   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
7839 
7840   switch (method)
7841     {
7842     case APPEND_ADD:
7843       insert_into_history (0, 1, ip);
7844       break;
7845 
7846     case APPEND_ADD_WITH_NOP:
7847       {
7848 	struct mips_cl_insn *nop;
7849 
7850 	insert_into_history (0, 1, ip);
7851 	nop = get_delay_slot_nop (ip);
7852 	add_fixed_insn (nop);
7853 	insert_into_history (0, 1, nop);
7854 	if (mips_relax.sequence)
7855 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7856       }
7857       break;
7858 
7859     case APPEND_ADD_COMPACT:
7860       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
7861       if (mips_opts.mips16)
7862 	{
7863 	  ip->insn_opcode |= 0x0080;
7864 	  find_altered_mips16_opcode (ip);
7865 	}
7866       /* Convert microMIPS instructions.  */
7867       else if (mips_opts.micromips)
7868 	{
7869 	  /* jr16->jrc */
7870 	  if ((ip->insn_opcode & 0xffe0) == 0x4580)
7871 	    ip->insn_opcode |= 0x0020;
7872 	  /* b16->bc */
7873 	  else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7874 	    ip->insn_opcode = 0x40e00000;
7875 	  /* beqz16->beqzc, bnez16->bnezc */
7876 	  else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7877 	    {
7878 	      unsigned long regno;
7879 
7880 	      regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7881 	      regno &= MICROMIPSOP_MASK_MD;
7882 	      regno = micromips_to_32_reg_d_map[regno];
7883 	      ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7884 				 | (regno << MICROMIPSOP_SH_RS)
7885 				 | 0x40a00000) ^ 0x00400000;
7886 	    }
7887 	  /* beqz->beqzc, bnez->bnezc */
7888 	  else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7889 	    ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7890 			       | ((ip->insn_opcode >> 7) & 0x00400000)
7891 			       | 0x40a00000) ^ 0x00400000;
7892 	  /* beq $0->beqzc, bne $0->bnezc */
7893 	  else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7894 	    ip->insn_opcode = (((ip->insn_opcode >>
7895 				 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7896 				& (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7897 			       | ((ip->insn_opcode >> 7) & 0x00400000)
7898 			       | 0x40a00000) ^ 0x00400000;
7899 	  else
7900 	    abort ();
7901 	  find_altered_micromips_opcode (ip);
7902 	}
7903       else
7904 	abort ();
7905       install_insn (ip);
7906       insert_into_history (0, 1, ip);
7907       break;
7908 
7909     case APPEND_SWAP:
7910       {
7911 	struct mips_cl_insn delay = history[0];
7912 
7913 	if (relaxed_branch || delay.frag != ip->frag)
7914 	  {
7915 	    /* Add the delay slot instruction to the end of the
7916 	       current frag and shrink the fixed part of the
7917 	       original frag.  If the branch occupies the tail of
7918 	       the latter, move it backwards to cover the gap.  */
7919 	    delay.frag->fr_fix -= branch_disp;
7920 	    if (delay.frag == ip->frag)
7921 	      move_insn (ip, ip->frag, ip->where - branch_disp);
7922 	    add_fixed_insn (&delay);
7923 	  }
7924 	else
7925 	  {
7926 	    /* If this is not a relaxed branch and we are in the
7927 	       same frag, then just swap the instructions.  */
7928 	    move_insn (ip, delay.frag, delay.where);
7929 	    move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7930 	  }
7931 	history[0] = *ip;
7932 	delay.fixed_p = 1;
7933 	insert_into_history (0, 1, &delay);
7934       }
7935       break;
7936     }
7937 
7938   /* If we have just completed an unconditional branch, clear the history.  */
7939   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7940       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
7941     {
7942       unsigned int i;
7943 
7944       mips_no_prev_insn ();
7945 
7946       for (i = 0; i < ARRAY_SIZE (history); i++)
7947 	history[i].cleared_p = 1;
7948     }
7949 
7950   /* We need to emit a label at the end of branch-likely macros.  */
7951   if (emit_branch_likely_macro)
7952     {
7953       emit_branch_likely_macro = FALSE;
7954       micromips_add_label ();
7955     }
7956 
7957   /* We just output an insn, so the next one doesn't have a label.  */
7958   mips_clear_insn_labels ();
7959 }
7960 
7961 /* Forget that there was any previous instruction or label.
7962    When BRANCH is true, the branch history is also flushed.  */
7963 
7964 static void
7965 mips_no_prev_insn (void)
7966 {
7967   prev_nop_frag = NULL;
7968   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
7969   mips_clear_insn_labels ();
7970 }
7971 
7972 /* This function must be called before we emit something other than
7973    instructions.  It is like mips_no_prev_insn except that it inserts
7974    any NOPS that might be needed by previous instructions.  */
7975 
7976 void
7977 mips_emit_delays (void)
7978 {
7979   if (! mips_opts.noreorder)
7980     {
7981       int nops = nops_for_insn (0, history, NULL);
7982       if (nops > 0)
7983 	{
7984 	  while (nops-- > 0)
7985 	    add_fixed_insn (NOP_INSN);
7986 	  mips_move_text_labels ();
7987 	}
7988     }
7989   mips_no_prev_insn ();
7990 }
7991 
7992 /* Start a (possibly nested) noreorder block.  */
7993 
7994 static void
7995 start_noreorder (void)
7996 {
7997   if (mips_opts.noreorder == 0)
7998     {
7999       unsigned int i;
8000       int nops;
8001 
8002       /* None of the instructions before the .set noreorder can be moved.  */
8003       for (i = 0; i < ARRAY_SIZE (history); i++)
8004 	history[i].fixed_p = 1;
8005 
8006       /* Insert any nops that might be needed between the .set noreorder
8007 	 block and the previous instructions.  We will later remove any
8008 	 nops that turn out not to be needed.  */
8009       nops = nops_for_insn (0, history, NULL);
8010       if (nops > 0)
8011 	{
8012 	  if (mips_optimize != 0)
8013 	    {
8014 	      /* Record the frag which holds the nop instructions, so
8015                  that we can remove them if we don't need them.  */
8016 	      frag_grow (nops * NOP_INSN_SIZE);
8017 	      prev_nop_frag = frag_now;
8018 	      prev_nop_frag_holds = nops;
8019 	      prev_nop_frag_required = 0;
8020 	      prev_nop_frag_since = 0;
8021 	    }
8022 
8023 	  for (; nops > 0; --nops)
8024 	    add_fixed_insn (NOP_INSN);
8025 
8026 	  /* Move on to a new frag, so that it is safe to simply
8027 	     decrease the size of prev_nop_frag.  */
8028 	  frag_wane (frag_now);
8029 	  frag_new (0);
8030 	  mips_move_text_labels ();
8031 	}
8032       mips_mark_labels ();
8033       mips_clear_insn_labels ();
8034     }
8035   mips_opts.noreorder++;
8036   mips_any_noreorder = 1;
8037 }
8038 
8039 /* End a nested noreorder block.  */
8040 
8041 static void
8042 end_noreorder (void)
8043 {
8044   mips_opts.noreorder--;
8045   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8046     {
8047       /* Commit to inserting prev_nop_frag_required nops and go back to
8048 	 handling nop insertion the .set reorder way.  */
8049       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
8050 				* NOP_INSN_SIZE);
8051       insert_into_history (prev_nop_frag_since,
8052 			   prev_nop_frag_required, NOP_INSN);
8053       prev_nop_frag = NULL;
8054     }
8055 }
8056 
8057 /* Sign-extend 32-bit mode constants that have bit 31 set and all
8058    higher bits unset.  */
8059 
8060 static void
8061 normalize_constant_expr (expressionS *ex)
8062 {
8063   if (ex->X_op == O_constant
8064       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8065     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8066 			- 0x80000000);
8067 }
8068 
8069 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
8070    all higher bits unset.  */
8071 
8072 static void
8073 normalize_address_expr (expressionS *ex)
8074 {
8075   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8076 	|| (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8077       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8078     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8079 			- 0x80000000);
8080 }
8081 
8082 /* Try to match TOKENS against OPCODE, storing the result in INSN.
8083    Return true if the match was successful.
8084 
8085    OPCODE_EXTRA is a value that should be ORed into the opcode
8086    (used for VU0 channel suffixes, etc.).  MORE_ALTS is true if
8087    there are more alternatives after OPCODE and SOFT_MATCH is
8088    as for mips_arg_info.  */
8089 
8090 static bfd_boolean
8091 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8092 	    struct mips_operand_token *tokens, unsigned int opcode_extra,
8093 	    bfd_boolean lax_match, bfd_boolean complete_p)
8094 {
8095   const char *args;
8096   struct mips_arg_info arg;
8097   const struct mips_operand *operand;
8098   char c;
8099 
8100   imm_expr.X_op = O_absent;
8101   offset_expr.X_op = O_absent;
8102   offset_reloc[0] = BFD_RELOC_UNUSED;
8103   offset_reloc[1] = BFD_RELOC_UNUSED;
8104   offset_reloc[2] = BFD_RELOC_UNUSED;
8105 
8106   create_insn (insn, opcode);
8107   /* When no opcode suffix is specified, assume ".xyzw". */
8108   if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8109     insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8110   else
8111     insn->insn_opcode |= opcode_extra;
8112   memset (&arg, 0, sizeof (arg));
8113   arg.insn = insn;
8114   arg.token = tokens;
8115   arg.argnum = 1;
8116   arg.last_regno = ILLEGAL_REG;
8117   arg.dest_regno = ILLEGAL_REG;
8118   arg.lax_match = lax_match;
8119   for (args = opcode->args;; ++args)
8120     {
8121       if (arg.token->type == OT_END)
8122 	{
8123 	  /* Handle unary instructions in which only one operand is given.
8124 	     The source is then the same as the destination.  */
8125 	  if (arg.opnum == 1 && *args == ',')
8126 	    {
8127 	      operand = (mips_opts.micromips
8128 			 ? decode_micromips_operand (args + 1)
8129 			 : decode_mips_operand (args + 1));
8130 	      if (operand && mips_optional_operand_p (operand))
8131 		{
8132 		  arg.token = tokens;
8133 		  arg.argnum = 1;
8134 		  continue;
8135 		}
8136 	    }
8137 
8138 	  /* Treat elided base registers as $0.  */
8139 	  if (strcmp (args, "(b)") == 0)
8140 	    args += 3;
8141 
8142 	  if (args[0] == '+')
8143 	    switch (args[1])
8144 	      {
8145 	      case 'K':
8146 	      case 'N':
8147 		/* The register suffix is optional. */
8148 		args += 2;
8149 		break;
8150 	      }
8151 
8152 	  /* Fail the match if there were too few operands.  */
8153 	  if (*args)
8154 	    return FALSE;
8155 
8156 	  /* Successful match.  */
8157 	  if (!complete_p)
8158 	    return TRUE;
8159 	  clear_insn_error ();
8160 	  if (arg.dest_regno == arg.last_regno
8161 	      && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8162 	    {
8163 	      if (arg.opnum == 2)
8164 		set_insn_error
8165 		  (0, _("source and destination must be different"));
8166 	      else if (arg.last_regno == 31)
8167 		set_insn_error
8168 		  (0, _("a destination register must be supplied"));
8169 	    }
8170 	  else if (arg.last_regno == 31
8171 		   && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8172 		       || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8173 	    set_insn_error (0, _("the source register must not be $31"));
8174 	  check_completed_insn (&arg);
8175 	  return TRUE;
8176 	}
8177 
8178       /* Fail the match if the line has too many operands.   */
8179       if (*args == 0)
8180 	return FALSE;
8181 
8182       /* Handle characters that need to match exactly.  */
8183       if (*args == '(' || *args == ')' || *args == ',')
8184 	{
8185 	  if (match_char (&arg, *args))
8186 	    continue;
8187 	  return FALSE;
8188 	}
8189       if (*args == '#')
8190 	{
8191 	  ++args;
8192 	  if (arg.token->type == OT_DOUBLE_CHAR
8193 	      && arg.token->u.ch == *args)
8194 	    {
8195 	      ++arg.token;
8196 	      continue;
8197 	    }
8198 	  return FALSE;
8199 	}
8200 
8201       /* Handle special macro operands.  Work out the properties of
8202 	 other operands.  */
8203       arg.opnum += 1;
8204       switch (*args)
8205 	{
8206 	case '-':
8207 	  switch (args[1])
8208 	    {
8209 	    case 'A':
8210 	      *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8211 	      break;
8212 
8213 	    case 'B':
8214 	      *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8215 	      break;
8216 	    }
8217 	  break;
8218 
8219 	case '+':
8220 	  switch (args[1])
8221 	    {
8222 	    case 'i':
8223 	      *offset_reloc = BFD_RELOC_MIPS_JMP;
8224 	      break;
8225 
8226 	    case '\'':
8227 	      *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8228 	      break;
8229 
8230 	    case '\"':
8231 	      *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8232 	      break;
8233 	    }
8234 	  break;
8235 
8236 	case 'I':
8237 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
8238 	    return FALSE;
8239 	  imm_expr.X_op = O_constant;
8240 	  if (GPR_SIZE == 32)
8241 	    normalize_constant_expr (&imm_expr);
8242 	  continue;
8243 
8244 	case 'A':
8245 	  if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8246 	    {
8247 	      /* Assume that the offset has been elided and that what
8248 		 we saw was a base register.  The match will fail later
8249 		 if that assumption turns out to be wrong.  */
8250 	      offset_expr.X_op = O_constant;
8251 	      offset_expr.X_add_number = 0;
8252 	    }
8253 	  else
8254 	    {
8255 	      if (!match_expression (&arg, &offset_expr, offset_reloc))
8256 		return FALSE;
8257 	      normalize_address_expr (&offset_expr);
8258 	    }
8259 	  continue;
8260 
8261 	case 'F':
8262 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8263 				     8, TRUE))
8264 	    return FALSE;
8265 	  continue;
8266 
8267 	case 'L':
8268 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8269 				     8, FALSE))
8270 	    return FALSE;
8271 	  continue;
8272 
8273 	case 'f':
8274 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8275 				     4, TRUE))
8276 	    return FALSE;
8277 	  continue;
8278 
8279 	case 'l':
8280 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8281 				     4, FALSE))
8282 	    return FALSE;
8283 	  continue;
8284 
8285 	case 'p':
8286 	  *offset_reloc = BFD_RELOC_16_PCREL_S2;
8287 	  break;
8288 
8289 	case 'a':
8290 	  *offset_reloc = BFD_RELOC_MIPS_JMP;
8291 	  break;
8292 
8293 	case 'm':
8294 	  gas_assert (mips_opts.micromips);
8295 	  c = args[1];
8296 	  switch (c)
8297 	    {
8298 	    case 'D':
8299 	    case 'E':
8300 	      if (!forced_insn_length)
8301 		*offset_reloc = (int) BFD_RELOC_UNUSED + c;
8302 	      else if (c == 'D')
8303 		*offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8304 	      else
8305 		*offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8306 	      break;
8307 	    }
8308 	  break;
8309 	}
8310 
8311       operand = (mips_opts.micromips
8312 		 ? decode_micromips_operand (args)
8313 		 : decode_mips_operand (args));
8314       if (!operand)
8315 	abort ();
8316 
8317       /* Skip prefixes.  */
8318       if (*args == '+' || *args == 'm' || *args == '-')
8319 	args++;
8320 
8321       if (mips_optional_operand_p (operand)
8322 	  && args[1] == ','
8323 	  && (arg.token[0].type != OT_REG
8324 	      || arg.token[1].type == OT_END))
8325 	{
8326 	  /* Assume that the register has been elided and is the
8327 	     same as the first operand.  */
8328 	  arg.token = tokens;
8329 	  arg.argnum = 1;
8330 	}
8331 
8332       if (!match_operand (&arg, operand))
8333 	return FALSE;
8334     }
8335 }
8336 
8337 /* Like match_insn, but for MIPS16.  */
8338 
8339 static bfd_boolean
8340 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8341 		   struct mips_operand_token *tokens)
8342 {
8343   const char *args;
8344   const struct mips_operand *operand;
8345   const struct mips_operand *ext_operand;
8346   bfd_boolean pcrel = FALSE;
8347   int required_insn_length;
8348   struct mips_arg_info arg;
8349   int relax_char;
8350 
8351   if (forced_insn_length)
8352     required_insn_length = forced_insn_length;
8353   else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8354     required_insn_length = 2;
8355   else
8356     required_insn_length = 0;
8357 
8358   create_insn (insn, opcode);
8359   imm_expr.X_op = O_absent;
8360   offset_expr.X_op = O_absent;
8361   offset_reloc[0] = BFD_RELOC_UNUSED;
8362   offset_reloc[1] = BFD_RELOC_UNUSED;
8363   offset_reloc[2] = BFD_RELOC_UNUSED;
8364   relax_char = 0;
8365 
8366   memset (&arg, 0, sizeof (arg));
8367   arg.insn = insn;
8368   arg.token = tokens;
8369   arg.argnum = 1;
8370   arg.last_regno = ILLEGAL_REG;
8371   arg.dest_regno = ILLEGAL_REG;
8372   relax_char = 0;
8373   for (args = opcode->args;; ++args)
8374     {
8375       int c;
8376 
8377       if (arg.token->type == OT_END)
8378 	{
8379 	  offsetT value;
8380 
8381 	  /* Handle unary instructions in which only one operand is given.
8382 	     The source is then the same as the destination.  */
8383 	  if (arg.opnum == 1 && *args == ',')
8384 	    {
8385 	      operand = decode_mips16_operand (args[1], FALSE);
8386 	      if (operand && mips_optional_operand_p (operand))
8387 		{
8388 		  arg.token = tokens;
8389 		  arg.argnum = 1;
8390 		  continue;
8391 		}
8392 	    }
8393 
8394 	  /* Fail the match if there were too few operands.  */
8395 	  if (*args)
8396 	    return FALSE;
8397 
8398 	  /* Successful match.  Stuff the immediate value in now, if
8399 	     we can.  */
8400 	  clear_insn_error ();
8401 	  if (opcode->pinfo == INSN_MACRO)
8402 	    {
8403 	      gas_assert (relax_char == 0 || relax_char == 'p');
8404 	      gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8405 	    }
8406 	  else if (relax_char
8407 		   && offset_expr.X_op == O_constant
8408 		   && !pcrel
8409 		   && calculate_reloc (*offset_reloc,
8410 				       offset_expr.X_add_number,
8411 				       &value))
8412 	    {
8413 	      mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8414 			    required_insn_length, &insn->insn_opcode);
8415 	      offset_expr.X_op = O_absent;
8416 	      *offset_reloc = BFD_RELOC_UNUSED;
8417 	    }
8418 	  else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8419 	    {
8420 	      if (required_insn_length == 2)
8421 		set_insn_error (0, _("invalid unextended operand value"));
8422 	      else if (!mips_opcode_32bit_p (opcode))
8423 		{
8424 		  forced_insn_length = 4;
8425 		  insn->insn_opcode |= MIPS16_EXTEND;
8426 		}
8427 	    }
8428 	  else if (relax_char)
8429 	    *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8430 
8431 	  check_completed_insn (&arg);
8432 	  return TRUE;
8433 	}
8434 
8435       /* Fail the match if the line has too many operands.   */
8436       if (*args == 0)
8437 	return FALSE;
8438 
8439       /* Handle characters that need to match exactly.  */
8440       if (*args == '(' || *args == ')' || *args == ',')
8441 	{
8442 	  if (match_char (&arg, *args))
8443 	    continue;
8444 	  return FALSE;
8445 	}
8446 
8447       arg.opnum += 1;
8448       c = *args;
8449       switch (c)
8450 	{
8451 	case 'p':
8452 	case 'q':
8453 	case 'A':
8454 	case 'B':
8455 	case 'E':
8456 	case 'V':
8457 	case 'u':
8458 	  relax_char = c;
8459 	  break;
8460 
8461 	case 'I':
8462 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
8463 	    return FALSE;
8464 	  imm_expr.X_op = O_constant;
8465 	  if (GPR_SIZE == 32)
8466 	    normalize_constant_expr (&imm_expr);
8467 	  continue;
8468 
8469 	case 'a':
8470 	case 'i':
8471 	  *offset_reloc = BFD_RELOC_MIPS16_JMP;
8472 	  break;
8473 	}
8474 
8475       operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8476       if (!operand)
8477 	abort ();
8478 
8479       if (operand->type == OP_PCREL)
8480 	pcrel = TRUE;
8481       else
8482 	{
8483 	  ext_operand = decode_mips16_operand (c, TRUE);
8484 	  if (operand != ext_operand)
8485 	    {
8486 	      if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8487 		{
8488 		  offset_expr.X_op = O_constant;
8489 		  offset_expr.X_add_number = 0;
8490 		  relax_char = c;
8491 		  continue;
8492 		}
8493 
8494 	      if (!match_expression (&arg, &offset_expr, offset_reloc))
8495 		return FALSE;
8496 
8497 	      /* '8' is used for SLTI(U) and has traditionally not
8498 		 been allowed to take relocation operators.  */
8499 	      if (offset_reloc[0] != BFD_RELOC_UNUSED
8500 		  && (ext_operand->size != 16 || c == '8'))
8501 		{
8502 		  match_not_constant (&arg);
8503 		  return FALSE;
8504 		}
8505 
8506 	      if (offset_expr.X_op == O_big)
8507 		{
8508 		  match_out_of_range (&arg);
8509 		  return FALSE;
8510 		}
8511 
8512 	      relax_char = c;
8513 	      continue;
8514 	    }
8515 	}
8516 
8517       if (mips_optional_operand_p (operand)
8518 	  && args[1] == ','
8519 	  && (arg.token[0].type != OT_REG
8520 	      || arg.token[1].type == OT_END))
8521 	{
8522 	  /* Assume that the register has been elided and is the
8523 	     same as the first operand.  */
8524 	  arg.token = tokens;
8525 	  arg.argnum = 1;
8526 	}
8527 
8528       if (!match_operand (&arg, operand))
8529 	return FALSE;
8530     }
8531 }
8532 
8533 /* Record that the current instruction is invalid for the current ISA.  */
8534 
8535 static void
8536 match_invalid_for_isa (void)
8537 {
8538   set_insn_error_ss
8539     (0, _("opcode not supported on this processor: %s (%s)"),
8540      mips_cpu_info_from_arch (mips_opts.arch)->name,
8541      mips_cpu_info_from_isa (mips_opts.isa)->name);
8542 }
8543 
8544 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8545    Return true if a definite match or failure was found, storing any match
8546    in INSN.  OPCODE_EXTRA is a value that should be ORed into the opcode
8547    (to handle things like VU0 suffixes).  LAX_MATCH is true if we have already
8548    tried and failed to match under normal conditions and now want to try a
8549    more relaxed match.  */
8550 
8551 static bfd_boolean
8552 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8553 	     const struct mips_opcode *past, struct mips_operand_token *tokens,
8554 	     int opcode_extra, bfd_boolean lax_match)
8555 {
8556   const struct mips_opcode *opcode;
8557   const struct mips_opcode *invalid_delay_slot;
8558   bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8559 
8560   /* Search for a match, ignoring alternatives that don't satisfy the
8561      current ISA or forced_length.  */
8562   invalid_delay_slot = 0;
8563   seen_valid_for_isa = FALSE;
8564   seen_valid_for_size = FALSE;
8565   opcode = first;
8566   do
8567     {
8568       gas_assert (strcmp (opcode->name, first->name) == 0);
8569       if (is_opcode_valid (opcode))
8570 	{
8571 	  seen_valid_for_isa = TRUE;
8572 	  if (is_size_valid (opcode))
8573 	    {
8574 	      bfd_boolean delay_slot_ok;
8575 
8576 	      seen_valid_for_size = TRUE;
8577 	      delay_slot_ok = is_delay_slot_valid (opcode);
8578 	      if (match_insn (insn, opcode, tokens, opcode_extra,
8579 			      lax_match, delay_slot_ok))
8580 		{
8581 		  if (!delay_slot_ok)
8582 		    {
8583 		      if (!invalid_delay_slot)
8584 			invalid_delay_slot = opcode;
8585 		    }
8586 		  else
8587 		    return TRUE;
8588 		}
8589 	    }
8590 	}
8591       ++opcode;
8592     }
8593   while (opcode < past && strcmp (opcode->name, first->name) == 0);
8594 
8595   /* If the only matches we found had the wrong length for the delay slot,
8596      pick the first such match.  We'll issue an appropriate warning later.  */
8597   if (invalid_delay_slot)
8598     {
8599       if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8600 		      lax_match, TRUE))
8601 	return TRUE;
8602       abort ();
8603     }
8604 
8605   /* Handle the case where we didn't try to match an instruction because
8606      all the alternatives were incompatible with the current ISA.  */
8607   if (!seen_valid_for_isa)
8608     {
8609       match_invalid_for_isa ();
8610       return TRUE;
8611     }
8612 
8613   /* Handle the case where we didn't try to match an instruction because
8614      all the alternatives were of the wrong size.  */
8615   if (!seen_valid_for_size)
8616     {
8617       if (mips_opts.insn32)
8618 	set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8619       else
8620 	set_insn_error_i
8621 	  (0, _("unrecognized %d-bit version of microMIPS opcode"),
8622 	   8 * forced_insn_length);
8623       return TRUE;
8624     }
8625 
8626   return FALSE;
8627 }
8628 
8629 /* Like match_insns, but for MIPS16.  */
8630 
8631 static bfd_boolean
8632 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8633 		    struct mips_operand_token *tokens)
8634 {
8635   const struct mips_opcode *opcode;
8636   bfd_boolean seen_valid_for_isa;
8637   bfd_boolean seen_valid_for_size;
8638 
8639   /* Search for a match, ignoring alternatives that don't satisfy the
8640      current ISA.  There are no separate entries for extended forms so
8641      we deal with forced_length later.  */
8642   seen_valid_for_isa = FALSE;
8643   seen_valid_for_size = FALSE;
8644   opcode = first;
8645   do
8646     {
8647       gas_assert (strcmp (opcode->name, first->name) == 0);
8648       if (is_opcode_valid_16 (opcode))
8649 	{
8650 	  seen_valid_for_isa = TRUE;
8651 	  if (is_size_valid_16 (opcode))
8652 	    {
8653 	      seen_valid_for_size = TRUE;
8654 	      if (match_mips16_insn (insn, opcode, tokens))
8655 		return TRUE;
8656 	    }
8657 	}
8658       ++opcode;
8659     }
8660   while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8661 	 && strcmp (opcode->name, first->name) == 0);
8662 
8663   /* Handle the case where we didn't try to match an instruction because
8664      all the alternatives were incompatible with the current ISA.  */
8665   if (!seen_valid_for_isa)
8666     {
8667       match_invalid_for_isa ();
8668       return TRUE;
8669     }
8670 
8671   /* Handle the case where we didn't try to match an instruction because
8672      all the alternatives were of the wrong size.  */
8673   if (!seen_valid_for_size)
8674     {
8675       if (forced_insn_length == 2)
8676 	set_insn_error
8677 	  (0, _("unrecognized unextended version of MIPS16 opcode"));
8678       else
8679 	set_insn_error
8680 	  (0, _("unrecognized extended version of MIPS16 opcode"));
8681       return TRUE;
8682     }
8683 
8684   return FALSE;
8685 }
8686 
8687 /* Set up global variables for the start of a new macro.  */
8688 
8689 static void
8690 macro_start (void)
8691 {
8692   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8693   memset (&mips_macro_warning.first_insn_sizes, 0,
8694 	  sizeof (mips_macro_warning.first_insn_sizes));
8695   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8696   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8697 				     && delayed_branch_p (&history[0]));
8698   if (history[0].frag
8699       && history[0].frag->fr_type == rs_machine_dependent
8700       && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8701       && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8702     mips_macro_warning.delay_slot_length = 0;
8703   else
8704     switch (history[0].insn_mo->pinfo2
8705 	    & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8706       {
8707       case INSN2_BRANCH_DELAY_32BIT:
8708 	mips_macro_warning.delay_slot_length = 4;
8709 	break;
8710       case INSN2_BRANCH_DELAY_16BIT:
8711 	mips_macro_warning.delay_slot_length = 2;
8712 	break;
8713       default:
8714 	mips_macro_warning.delay_slot_length = 0;
8715 	break;
8716       }
8717   mips_macro_warning.first_frag = NULL;
8718 }
8719 
8720 /* Given that a macro is longer than one instruction or of the wrong size,
8721    return the appropriate warning for it.  Return null if no warning is
8722    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8723    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8724    and RELAX_NOMACRO.  */
8725 
8726 static const char *
8727 macro_warning (relax_substateT subtype)
8728 {
8729   if (subtype & RELAX_DELAY_SLOT)
8730     return _("macro instruction expanded into multiple instructions"
8731 	     " in a branch delay slot");
8732   else if (subtype & RELAX_NOMACRO)
8733     return _("macro instruction expanded into multiple instructions");
8734   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8735 		      | RELAX_DELAY_SLOT_SIZE_SECOND))
8736     return ((subtype & RELAX_DELAY_SLOT_16BIT)
8737 	    ? _("macro instruction expanded into a wrong size instruction"
8738 		" in a 16-bit branch delay slot")
8739 	    : _("macro instruction expanded into a wrong size instruction"
8740 		" in a 32-bit branch delay slot"));
8741   else
8742     return 0;
8743 }
8744 
8745 /* Finish up a macro.  Emit warnings as appropriate.  */
8746 
8747 static void
8748 macro_end (void)
8749 {
8750   /* Relaxation warning flags.  */
8751   relax_substateT subtype = 0;
8752 
8753   /* Check delay slot size requirements.  */
8754   if (mips_macro_warning.delay_slot_length == 2)
8755     subtype |= RELAX_DELAY_SLOT_16BIT;
8756   if (mips_macro_warning.delay_slot_length != 0)
8757     {
8758       if (mips_macro_warning.delay_slot_length
8759 	  != mips_macro_warning.first_insn_sizes[0])
8760 	subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8761       if (mips_macro_warning.delay_slot_length
8762 	  != mips_macro_warning.first_insn_sizes[1])
8763 	subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8764     }
8765 
8766   /* Check instruction count requirements.  */
8767   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8768     {
8769       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
8770 	subtype |= RELAX_SECOND_LONGER;
8771       if (mips_opts.warn_about_macros)
8772 	subtype |= RELAX_NOMACRO;
8773       if (mips_macro_warning.delay_slot_p)
8774 	subtype |= RELAX_DELAY_SLOT;
8775     }
8776 
8777   /* If both alternatives fail to fill a delay slot correctly,
8778      emit the warning now.  */
8779   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8780       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8781     {
8782       relax_substateT s;
8783       const char *msg;
8784 
8785       s = subtype & (RELAX_DELAY_SLOT_16BIT
8786 		     | RELAX_DELAY_SLOT_SIZE_FIRST
8787 		     | RELAX_DELAY_SLOT_SIZE_SECOND);
8788       msg = macro_warning (s);
8789       if (msg != NULL)
8790 	as_warn ("%s", msg);
8791       subtype &= ~s;
8792     }
8793 
8794   /* If both implementations are longer than 1 instruction, then emit the
8795      warning now.  */
8796   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8797     {
8798       relax_substateT s;
8799       const char *msg;
8800 
8801       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8802       msg = macro_warning (s);
8803       if (msg != NULL)
8804 	as_warn ("%s", msg);
8805       subtype &= ~s;
8806     }
8807 
8808   /* If any flags still set, then one implementation might need a warning
8809      and the other either will need one of a different kind or none at all.
8810      Pass any remaining flags over to relaxation.  */
8811   if (mips_macro_warning.first_frag != NULL)
8812     mips_macro_warning.first_frag->fr_subtype |= subtype;
8813 }
8814 
8815 /* Instruction operand formats used in macros that vary between
8816    standard MIPS and microMIPS code.  */
8817 
8818 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
8819 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8820 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8821 static const char * const lui_fmt[2] = { "t,u", "s,u" };
8822 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
8823 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
8824 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8825 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8826 
8827 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
8828 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8829 					     : cop12_fmt[mips_opts.micromips])
8830 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
8831 #define LUI_FMT (lui_fmt[mips_opts.micromips])
8832 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
8833 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8834 					     : mem12_fmt[mips_opts.micromips])
8835 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
8836 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
8837 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
8838 
8839 /* Read a macro's relocation codes from *ARGS and store them in *R.
8840    The first argument in *ARGS will be either the code for a single
8841    relocation or -1 followed by the three codes that make up a
8842    composite relocation.  */
8843 
8844 static void
8845 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8846 {
8847   int i, next;
8848 
8849   next = va_arg (*args, int);
8850   if (next >= 0)
8851     r[0] = (bfd_reloc_code_real_type) next;
8852   else
8853     {
8854       for (i = 0; i < 3; i++)
8855 	r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8856       /* This function is only used for 16-bit relocation fields.
8857 	 To make the macro code simpler, treat an unrelocated value
8858 	 in the same way as BFD_RELOC_LO16.  */
8859       if (r[0] == BFD_RELOC_UNUSED)
8860 	r[0] = BFD_RELOC_LO16;
8861     }
8862 }
8863 
8864 /* Build an instruction created by a macro expansion.  This is passed
8865    a pointer to the count of instructions created so far, an
8866    expression, the name of the instruction to build, an operand format
8867    string, and corresponding arguments.  */
8868 
8869 static void
8870 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
8871 {
8872   const struct mips_opcode *mo = NULL;
8873   bfd_reloc_code_real_type r[3];
8874   const struct mips_opcode *amo;
8875   const struct mips_operand *operand;
8876   struct hash_control *hash;
8877   struct mips_cl_insn insn;
8878   va_list args;
8879   unsigned int uval;
8880 
8881   va_start (args, fmt);
8882 
8883   if (mips_opts.mips16)
8884     {
8885       mips16_macro_build (ep, name, fmt, &args);
8886       va_end (args);
8887       return;
8888     }
8889 
8890   r[0] = BFD_RELOC_UNUSED;
8891   r[1] = BFD_RELOC_UNUSED;
8892   r[2] = BFD_RELOC_UNUSED;
8893   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8894   amo = (struct mips_opcode *) hash_find (hash, name);
8895   gas_assert (amo);
8896   gas_assert (strcmp (name, amo->name) == 0);
8897 
8898   do
8899     {
8900       /* Search until we get a match for NAME.  It is assumed here that
8901 	 macros will never generate MDMX, MIPS-3D, or MT instructions.
8902 	 We try to match an instruction that fulfills the branch delay
8903 	 slot instruction length requirement (if any) of the previous
8904 	 instruction.  While doing this we record the first instruction
8905 	 seen that matches all the other conditions and use it anyway
8906 	 if the requirement cannot be met; we will issue an appropriate
8907 	 warning later on.  */
8908       if (strcmp (fmt, amo->args) == 0
8909 	  && amo->pinfo != INSN_MACRO
8910 	  && is_opcode_valid (amo)
8911 	  && is_size_valid (amo))
8912 	{
8913 	  if (is_delay_slot_valid (amo))
8914 	    {
8915 	      mo = amo;
8916 	      break;
8917 	    }
8918 	  else if (!mo)
8919 	    mo = amo;
8920 	}
8921 
8922       ++amo;
8923       gas_assert (amo->name);
8924     }
8925   while (strcmp (name, amo->name) == 0);
8926 
8927   gas_assert (mo);
8928   create_insn (&insn, mo);
8929   for (; *fmt; ++fmt)
8930     {
8931       switch (*fmt)
8932 	{
8933 	case ',':
8934 	case '(':
8935 	case ')':
8936 	case 'z':
8937 	  break;
8938 
8939 	case 'i':
8940 	case 'j':
8941 	  macro_read_relocs (&args, r);
8942 	  gas_assert (*r == BFD_RELOC_GPREL16
8943 		      || *r == BFD_RELOC_MIPS_HIGHER
8944 		      || *r == BFD_RELOC_HI16_S
8945 		      || *r == BFD_RELOC_LO16
8946 		      || *r == BFD_RELOC_MIPS_GOT_OFST);
8947 	  break;
8948 
8949 	case 'o':
8950 	  macro_read_relocs (&args, r);
8951 	  break;
8952 
8953 	case 'u':
8954 	  macro_read_relocs (&args, r);
8955 	  gas_assert (ep != NULL
8956 		      && (ep->X_op == O_constant
8957 			  || (ep->X_op == O_symbol
8958 			      && (*r == BFD_RELOC_MIPS_HIGHEST
8959 				  || *r == BFD_RELOC_HI16_S
8960 				  || *r == BFD_RELOC_HI16
8961 				  || *r == BFD_RELOC_GPREL16
8962 				  || *r == BFD_RELOC_MIPS_GOT_HI16
8963 				  || *r == BFD_RELOC_MIPS_CALL_HI16))));
8964 	  break;
8965 
8966 	case 'p':
8967 	  gas_assert (ep != NULL);
8968 
8969 	  /*
8970 	   * This allows macro() to pass an immediate expression for
8971 	   * creating short branches without creating a symbol.
8972 	   *
8973 	   * We don't allow branch relaxation for these branches, as
8974 	   * they should only appear in ".set nomacro" anyway.
8975 	   */
8976 	  if (ep->X_op == O_constant)
8977 	    {
8978 	      /* For microMIPS we always use relocations for branches.
8979 	         So we should not resolve immediate values.  */
8980 	      gas_assert (!mips_opts.micromips);
8981 
8982 	      if ((ep->X_add_number & 3) != 0)
8983 		as_bad (_("branch to misaligned address (0x%lx)"),
8984 			(unsigned long) ep->X_add_number);
8985 	      if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8986 		as_bad (_("branch address range overflow (0x%lx)"),
8987 			(unsigned long) ep->X_add_number);
8988 	      insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8989 	      ep = NULL;
8990 	    }
8991 	  else
8992 	    *r = BFD_RELOC_16_PCREL_S2;
8993 	  break;
8994 
8995 	case 'a':
8996 	  gas_assert (ep != NULL);
8997 	  *r = BFD_RELOC_MIPS_JMP;
8998 	  break;
8999 
9000 	default:
9001 	  operand = (mips_opts.micromips
9002 		     ? decode_micromips_operand (fmt)
9003 		     : decode_mips_operand (fmt));
9004 	  if (!operand)
9005 	    abort ();
9006 
9007 	  uval = va_arg (args, int);
9008 	  if (operand->type == OP_CLO_CLZ_DEST)
9009 	    uval |= (uval << 5);
9010 	  insn_insert_operand (&insn, operand, uval);
9011 
9012 	  if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
9013 	    ++fmt;
9014 	  break;
9015 	}
9016     }
9017   va_end (args);
9018   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9019 
9020   append_insn (&insn, ep, r, TRUE);
9021 }
9022 
9023 static void
9024 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
9025 		    va_list *args)
9026 {
9027   struct mips_opcode *mo;
9028   struct mips_cl_insn insn;
9029   const struct mips_operand *operand;
9030   bfd_reloc_code_real_type r[3]
9031     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
9032 
9033   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9034   gas_assert (mo);
9035   gas_assert (strcmp (name, mo->name) == 0);
9036 
9037   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
9038     {
9039       ++mo;
9040       gas_assert (mo->name);
9041       gas_assert (strcmp (name, mo->name) == 0);
9042     }
9043 
9044   create_insn (&insn, mo);
9045   for (; *fmt; ++fmt)
9046     {
9047       int c;
9048 
9049       c = *fmt;
9050       switch (c)
9051 	{
9052 	case ',':
9053 	case '(':
9054 	case ')':
9055 	  break;
9056 
9057 	case '.':
9058 	case 'S':
9059 	case 'P':
9060 	case 'R':
9061 	  break;
9062 
9063 	case '<':
9064 	case '5':
9065 	case 'F':
9066 	case 'H':
9067 	case 'W':
9068 	case 'D':
9069 	case 'j':
9070 	case '8':
9071 	case 'V':
9072 	case 'C':
9073 	case 'U':
9074 	case 'k':
9075 	case 'K':
9076 	case 'p':
9077 	case 'q':
9078 	  {
9079 	    offsetT value;
9080 
9081 	    gas_assert (ep != NULL);
9082 
9083 	    if (ep->X_op != O_constant)
9084 	      *r = (int) BFD_RELOC_UNUSED + c;
9085 	    else if (calculate_reloc (*r, ep->X_add_number, &value))
9086 	      {
9087 		mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
9088 		ep = NULL;
9089 		*r = BFD_RELOC_UNUSED;
9090 	      }
9091 	  }
9092 	  break;
9093 
9094 	default:
9095 	  operand = decode_mips16_operand (c, FALSE);
9096 	  if (!operand)
9097 	    abort ();
9098 
9099 	  insn_insert_operand (&insn, operand, va_arg (*args, int));
9100 	  break;
9101 	}
9102     }
9103 
9104   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9105 
9106   append_insn (&insn, ep, r, TRUE);
9107 }
9108 
9109 /*
9110  * Generate a "jalr" instruction with a relocation hint to the called
9111  * function.  This occurs in NewABI PIC code.
9112  */
9113 static void
9114 macro_build_jalr (expressionS *ep, int cprestore)
9115 {
9116   static const bfd_reloc_code_real_type jalr_relocs[2]
9117     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9118   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9119   const char *jalr;
9120   char *f = NULL;
9121 
9122   if (MIPS_JALR_HINT_P (ep))
9123     {
9124       frag_grow (8);
9125       f = frag_more (0);
9126     }
9127   if (mips_opts.micromips)
9128     {
9129       jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9130 	      ? "jalr" : "jalrs");
9131       if (MIPS_JALR_HINT_P (ep)
9132 	  || mips_opts.insn32
9133 	  || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9134 	macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9135       else
9136 	macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9137     }
9138   else
9139     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9140   if (MIPS_JALR_HINT_P (ep))
9141     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
9142 }
9143 
9144 /*
9145  * Generate a "lui" instruction.
9146  */
9147 static void
9148 macro_build_lui (expressionS *ep, int regnum)
9149 {
9150   gas_assert (! mips_opts.mips16);
9151 
9152   if (ep->X_op != O_constant)
9153     {
9154       gas_assert (ep->X_op == O_symbol);
9155       /* _gp_disp is a special case, used from s_cpload.
9156 	 __gnu_local_gp is used if mips_no_shared.  */
9157       gas_assert (mips_pic == NO_PIC
9158 	      || (! HAVE_NEWABI
9159 		  && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9160 	      || (! mips_in_shared
9161 		  && strcmp (S_GET_NAME (ep->X_add_symbol),
9162                              "__gnu_local_gp") == 0));
9163     }
9164 
9165   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9166 }
9167 
9168 /* Generate a sequence of instructions to do a load or store from a constant
9169    offset off of a base register (breg) into/from a target register (treg),
9170    using AT if necessary.  */
9171 static void
9172 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9173 			      int treg, int breg, int dbl)
9174 {
9175   gas_assert (ep->X_op == O_constant);
9176 
9177   /* Sign-extending 32-bit constants makes their handling easier.  */
9178   if (!dbl)
9179     normalize_constant_expr (ep);
9180 
9181   /* Right now, this routine can only handle signed 32-bit constants.  */
9182   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9183     as_warn (_("operand overflow"));
9184 
9185   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9186     {
9187       /* Signed 16-bit offset will fit in the op.  Easy!  */
9188       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9189     }
9190   else
9191     {
9192       /* 32-bit offset, need multiple instructions and AT, like:
9193 	   lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
9194 	   addu     $tempreg,$tempreg,$breg
9195            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
9196          to handle the complete offset.  */
9197       macro_build_lui (ep, AT);
9198       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9199       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9200 
9201       if (!mips_opts.at)
9202 	as_bad (_("macro used $at after \".set noat\""));
9203     }
9204 }
9205 
9206 /*			set_at()
9207  * Generates code to set the $at register to true (one)
9208  * if reg is less than the immediate expression.
9209  */
9210 static void
9211 set_at (int reg, int unsignedp)
9212 {
9213   if (imm_expr.X_add_number >= -0x8000
9214       && imm_expr.X_add_number < 0x8000)
9215     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9216 		 AT, reg, BFD_RELOC_LO16);
9217   else
9218     {
9219       load_register (AT, &imm_expr, GPR_SIZE == 64);
9220       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9221     }
9222 }
9223 
9224 /* Count the leading zeroes by performing a binary chop. This is a
9225    bulky bit of source, but performance is a LOT better for the
9226    majority of values than a simple loop to count the bits:
9227        for (lcnt = 0; (lcnt < 32); lcnt++)
9228          if ((v) & (1 << (31 - lcnt)))
9229            break;
9230   However it is not code size friendly, and the gain will drop a bit
9231   on certain cached systems.
9232 */
9233 #define COUNT_TOP_ZEROES(v)             \
9234   (((v) & ~0xffff) == 0                 \
9235    ? ((v) & ~0xff) == 0                 \
9236      ? ((v) & ~0xf) == 0                \
9237        ? ((v) & ~0x3) == 0              \
9238          ? ((v) & ~0x1) == 0            \
9239            ? !(v)                       \
9240              ? 32                       \
9241              : 31                       \
9242            : 30                         \
9243          : ((v) & ~0x7) == 0            \
9244            ? 29                         \
9245            : 28                         \
9246        : ((v) & ~0x3f) == 0             \
9247          ? ((v) & ~0x1f) == 0           \
9248            ? 27                         \
9249            : 26                         \
9250          : ((v) & ~0x7f) == 0           \
9251            ? 25                         \
9252            : 24                         \
9253      : ((v) & ~0xfff) == 0              \
9254        ? ((v) & ~0x3ff) == 0            \
9255          ? ((v) & ~0x1ff) == 0          \
9256            ? 23                         \
9257            : 22                         \
9258          : ((v) & ~0x7ff) == 0          \
9259            ? 21                         \
9260            : 20                         \
9261        : ((v) & ~0x3fff) == 0           \
9262          ? ((v) & ~0x1fff) == 0         \
9263            ? 19                         \
9264            : 18                         \
9265          : ((v) & ~0x7fff) == 0         \
9266            ? 17                         \
9267            : 16                         \
9268    : ((v) & ~0xffffff) == 0             \
9269      ? ((v) & ~0xfffff) == 0            \
9270        ? ((v) & ~0x3ffff) == 0          \
9271          ? ((v) & ~0x1ffff) == 0        \
9272            ? 15                         \
9273            : 14                         \
9274          : ((v) & ~0x7ffff) == 0        \
9275            ? 13                         \
9276            : 12                         \
9277        : ((v) & ~0x3fffff) == 0         \
9278          ? ((v) & ~0x1fffff) == 0       \
9279            ? 11                         \
9280            : 10                         \
9281          : ((v) & ~0x7fffff) == 0       \
9282            ? 9                          \
9283            : 8                          \
9284      : ((v) & ~0xfffffff) == 0          \
9285        ? ((v) & ~0x3ffffff) == 0        \
9286          ? ((v) & ~0x1ffffff) == 0      \
9287            ? 7                          \
9288            : 6                          \
9289          : ((v) & ~0x7ffffff) == 0      \
9290            ? 5                          \
9291            : 4                          \
9292        : ((v) & ~0x3fffffff) == 0       \
9293          ? ((v) & ~0x1fffffff) == 0     \
9294            ? 3                          \
9295            : 2                          \
9296          : ((v) & ~0x7fffffff) == 0     \
9297            ? 1                          \
9298            : 0)
9299 
9300 /*			load_register()
9301  *  This routine generates the least number of instructions necessary to load
9302  *  an absolute expression value into a register.
9303  */
9304 static void
9305 load_register (int reg, expressionS *ep, int dbl)
9306 {
9307   int freg;
9308   expressionS hi32, lo32;
9309 
9310   if (ep->X_op != O_big)
9311     {
9312       gas_assert (ep->X_op == O_constant);
9313 
9314       /* Sign-extending 32-bit constants makes their handling easier.  */
9315       if (!dbl)
9316 	normalize_constant_expr (ep);
9317 
9318       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9319 	{
9320 	  /* We can handle 16 bit signed values with an addiu to
9321 	     $zero.  No need to ever use daddiu here, since $zero and
9322 	     the result are always correct in 32 bit mode.  */
9323 	  macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9324 	  return;
9325 	}
9326       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9327 	{
9328 	  /* We can handle 16 bit unsigned values with an ori to
9329              $zero.  */
9330 	  macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9331 	  return;
9332 	}
9333       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9334 	{
9335 	  /* 32 bit values require an lui.  */
9336 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9337 	  if ((ep->X_add_number & 0xffff) != 0)
9338 	    macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9339 	  return;
9340 	}
9341     }
9342 
9343   /* The value is larger than 32 bits.  */
9344 
9345   if (!dbl || GPR_SIZE == 32)
9346     {
9347       char value[32];
9348 
9349       sprintf_vma (value, ep->X_add_number);
9350       as_bad (_("number (0x%s) larger than 32 bits"), value);
9351       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9352       return;
9353     }
9354 
9355   if (ep->X_op != O_big)
9356     {
9357       hi32 = *ep;
9358       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9359       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9360       hi32.X_add_number &= 0xffffffff;
9361       lo32 = *ep;
9362       lo32.X_add_number &= 0xffffffff;
9363     }
9364   else
9365     {
9366       gas_assert (ep->X_add_number > 2);
9367       if (ep->X_add_number == 3)
9368 	generic_bignum[3] = 0;
9369       else if (ep->X_add_number > 4)
9370 	as_bad (_("number larger than 64 bits"));
9371       lo32.X_op = O_constant;
9372       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9373       hi32.X_op = O_constant;
9374       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9375     }
9376 
9377   if (hi32.X_add_number == 0)
9378     freg = 0;
9379   else
9380     {
9381       int shift, bit;
9382       unsigned long hi, lo;
9383 
9384       if (hi32.X_add_number == (offsetT) 0xffffffff)
9385 	{
9386 	  if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9387 	    {
9388 	      macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9389 	      return;
9390 	    }
9391 	  if (lo32.X_add_number & 0x80000000)
9392 	    {
9393 	      macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9394 	      if (lo32.X_add_number & 0xffff)
9395 		macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9396 	      return;
9397 	    }
9398 	}
9399 
9400       /* Check for 16bit shifted constant.  We know that hi32 is
9401          non-zero, so start the mask on the first bit of the hi32
9402          value.  */
9403       shift = 17;
9404       do
9405 	{
9406 	  unsigned long himask, lomask;
9407 
9408 	  if (shift < 32)
9409 	    {
9410 	      himask = 0xffff >> (32 - shift);
9411 	      lomask = (0xffff << shift) & 0xffffffff;
9412 	    }
9413 	  else
9414 	    {
9415 	      himask = 0xffff << (shift - 32);
9416 	      lomask = 0;
9417 	    }
9418 	  if ((hi32.X_add_number & ~(offsetT) himask) == 0
9419 	      && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9420 	    {
9421 	      expressionS tmp;
9422 
9423 	      tmp.X_op = O_constant;
9424 	      if (shift < 32)
9425 		tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9426 				    | (lo32.X_add_number >> shift));
9427 	      else
9428 		tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9429 	      macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9430 	      macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9431 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9432 	      return;
9433 	    }
9434 	  ++shift;
9435 	}
9436       while (shift <= (64 - 16));
9437 
9438       /* Find the bit number of the lowest one bit, and store the
9439          shifted value in hi/lo.  */
9440       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9441       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9442       if (lo != 0)
9443 	{
9444 	  bit = 0;
9445 	  while ((lo & 1) == 0)
9446 	    {
9447 	      lo >>= 1;
9448 	      ++bit;
9449 	    }
9450 	  lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9451 	  hi >>= bit;
9452 	}
9453       else
9454 	{
9455 	  bit = 32;
9456 	  while ((hi & 1) == 0)
9457 	    {
9458 	      hi >>= 1;
9459 	      ++bit;
9460 	    }
9461 	  lo = hi;
9462 	  hi = 0;
9463 	}
9464 
9465       /* Optimize if the shifted value is a (power of 2) - 1.  */
9466       if ((hi == 0 && ((lo + 1) & lo) == 0)
9467 	  || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9468 	{
9469 	  shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9470 	  if (shift != 0)
9471 	    {
9472 	      expressionS tmp;
9473 
9474 	      /* This instruction will set the register to be all
9475                  ones.  */
9476 	      tmp.X_op = O_constant;
9477 	      tmp.X_add_number = (offsetT) -1;
9478 	      macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9479 	      if (bit != 0)
9480 		{
9481 		  bit += shift;
9482 		  macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9483 			       reg, reg, (bit >= 32) ? bit - 32 : bit);
9484 		}
9485 	      macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9486 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9487 	      return;
9488 	    }
9489 	}
9490 
9491       /* Sign extend hi32 before calling load_register, because we can
9492          generally get better code when we load a sign extended value.  */
9493       if ((hi32.X_add_number & 0x80000000) != 0)
9494 	hi32.X_add_number |= ~(offsetT) 0xffffffff;
9495       load_register (reg, &hi32, 0);
9496       freg = reg;
9497     }
9498   if ((lo32.X_add_number & 0xffff0000) == 0)
9499     {
9500       if (freg != 0)
9501 	{
9502 	  macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9503 	  freg = reg;
9504 	}
9505     }
9506   else
9507     {
9508       expressionS mid16;
9509 
9510       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9511 	{
9512 	  macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9513 	  macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9514 	  return;
9515 	}
9516 
9517       if (freg != 0)
9518 	{
9519 	  macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9520 	  freg = reg;
9521 	}
9522       mid16 = lo32;
9523       mid16.X_add_number >>= 16;
9524       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9525       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9526       freg = reg;
9527     }
9528   if ((lo32.X_add_number & 0xffff) != 0)
9529     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9530 }
9531 
9532 static inline void
9533 load_delay_nop (void)
9534 {
9535   if (!gpr_interlocks)
9536     macro_build (NULL, "nop", "");
9537 }
9538 
9539 /* Load an address into a register.  */
9540 
9541 static void
9542 load_address (int reg, expressionS *ep, int *used_at)
9543 {
9544   if (ep->X_op != O_constant
9545       && ep->X_op != O_symbol)
9546     {
9547       as_bad (_("expression too complex"));
9548       ep->X_op = O_constant;
9549     }
9550 
9551   if (ep->X_op == O_constant)
9552     {
9553       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9554       return;
9555     }
9556 
9557   if (mips_pic == NO_PIC)
9558     {
9559       /* If this is a reference to a GP relative symbol, we want
9560 	   addiu	$reg,$gp,<sym>		(BFD_RELOC_GPREL16)
9561 	 Otherwise we want
9562 	   lui		$reg,<sym>		(BFD_RELOC_HI16_S)
9563 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9564 	 If we have an addend, we always use the latter form.
9565 
9566 	 With 64bit address space and a usable $at we want
9567 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9568 	   lui		$at,<sym>		(BFD_RELOC_HI16_S)
9569 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9570 	   daddiu	$at,<sym>		(BFD_RELOC_LO16)
9571 	   dsll32	$reg,0
9572 	   daddu	$reg,$reg,$at
9573 
9574 	 If $at is already in use, we use a path which is suboptimal
9575 	 on superscalar processors.
9576 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9577 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9578 	   dsll		$reg,16
9579 	   daddiu	$reg,<sym>		(BFD_RELOC_HI16_S)
9580 	   dsll		$reg,16
9581 	   daddiu	$reg,<sym>		(BFD_RELOC_LO16)
9582 
9583 	 For GP relative symbols in 64bit address space we can use
9584 	 the same sequence as in 32bit address space.  */
9585       if (HAVE_64BIT_SYMBOLS)
9586 	{
9587 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9588 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9589 	    {
9590 	      relax_start (ep->X_add_symbol);
9591 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9592 			   mips_gp_register, BFD_RELOC_GPREL16);
9593 	      relax_switch ();
9594 	    }
9595 
9596 	  if (*used_at == 0 && mips_opts.at)
9597 	    {
9598 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9599 	      macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9600 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9601 			   BFD_RELOC_MIPS_HIGHER);
9602 	      macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9603 	      macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9604 	      macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9605 	      *used_at = 1;
9606 	    }
9607 	  else
9608 	    {
9609 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9610 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9611 			   BFD_RELOC_MIPS_HIGHER);
9612 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9613 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9614 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9615 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9616 	    }
9617 
9618 	  if (mips_relax.sequence)
9619 	    relax_end ();
9620 	}
9621       else
9622 	{
9623 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9624 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9625 	    {
9626 	      relax_start (ep->X_add_symbol);
9627 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9628 			   mips_gp_register, BFD_RELOC_GPREL16);
9629 	      relax_switch ();
9630 	    }
9631 	  macro_build_lui (ep, reg);
9632 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9633 		       reg, reg, BFD_RELOC_LO16);
9634 	  if (mips_relax.sequence)
9635 	    relax_end ();
9636 	}
9637     }
9638   else if (!mips_big_got)
9639     {
9640       expressionS ex;
9641 
9642       /* If this is a reference to an external symbol, we want
9643 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9644 	 Otherwise we want
9645 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9646 	   nop
9647 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9648 	 If there is a constant, it must be added in after.
9649 
9650 	 If we have NewABI, we want
9651 	   lw		$reg,<sym+cst>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
9652          unless we're referencing a global symbol with a non-zero
9653          offset, in which case cst must be added separately.  */
9654       if (HAVE_NEWABI)
9655 	{
9656 	  if (ep->X_add_number)
9657 	    {
9658 	      ex.X_add_number = ep->X_add_number;
9659 	      ep->X_add_number = 0;
9660 	      relax_start (ep->X_add_symbol);
9661 	      macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9662 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9663 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9664 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9665 	      ex.X_op = O_constant;
9666 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9667 			   reg, reg, BFD_RELOC_LO16);
9668 	      ep->X_add_number = ex.X_add_number;
9669 	      relax_switch ();
9670 	    }
9671 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9672 		       BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9673 	  if (mips_relax.sequence)
9674 	    relax_end ();
9675 	}
9676       else
9677 	{
9678 	  ex.X_add_number = ep->X_add_number;
9679 	  ep->X_add_number = 0;
9680 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9681 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9682 	  load_delay_nop ();
9683 	  relax_start (ep->X_add_symbol);
9684 	  relax_switch ();
9685 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9686 		       BFD_RELOC_LO16);
9687 	  relax_end ();
9688 
9689 	  if (ex.X_add_number != 0)
9690 	    {
9691 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9692 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9693 	      ex.X_op = O_constant;
9694 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9695 			   reg, reg, BFD_RELOC_LO16);
9696 	    }
9697 	}
9698     }
9699   else if (mips_big_got)
9700     {
9701       expressionS ex;
9702 
9703       /* This is the large GOT case.  If this is a reference to an
9704 	 external symbol, we want
9705 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
9706 	   addu		$reg,$reg,$gp
9707 	   lw		$reg,<sym>($reg)	(BFD_RELOC_MIPS_GOT_LO16)
9708 
9709 	 Otherwise, for a reference to a local symbol in old ABI, we want
9710 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9711 	   nop
9712 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9713 	 If there is a constant, it must be added in after.
9714 
9715 	 In the NewABI, for local symbols, with or without offsets, we want:
9716 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
9717 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
9718       */
9719       if (HAVE_NEWABI)
9720 	{
9721 	  ex.X_add_number = ep->X_add_number;
9722 	  ep->X_add_number = 0;
9723 	  relax_start (ep->X_add_symbol);
9724 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9725 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9726 		       reg, reg, mips_gp_register);
9727 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9728 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9729 	  if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9730 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9731 	  else if (ex.X_add_number)
9732 	    {
9733 	      ex.X_op = O_constant;
9734 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9735 			   BFD_RELOC_LO16);
9736 	    }
9737 
9738 	  ep->X_add_number = ex.X_add_number;
9739 	  relax_switch ();
9740 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9741 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9742 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9743 		       BFD_RELOC_MIPS_GOT_OFST);
9744 	  relax_end ();
9745 	}
9746       else
9747 	{
9748 	  ex.X_add_number = ep->X_add_number;
9749 	  ep->X_add_number = 0;
9750 	  relax_start (ep->X_add_symbol);
9751 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9752 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9753 		       reg, reg, mips_gp_register);
9754 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9755 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9756 	  relax_switch ();
9757 	  if (reg_needs_delay (mips_gp_register))
9758 	    {
9759 	      /* We need a nop before loading from $gp.  This special
9760 		 check is required because the lui which starts the main
9761 		 instruction stream does not refer to $gp, and so will not
9762 		 insert the nop which may be required.  */
9763 	      macro_build (NULL, "nop", "");
9764 	    }
9765 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9766 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9767 	  load_delay_nop ();
9768 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9769 		       BFD_RELOC_LO16);
9770 	  relax_end ();
9771 
9772 	  if (ex.X_add_number != 0)
9773 	    {
9774 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9775 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9776 	      ex.X_op = O_constant;
9777 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9778 			   BFD_RELOC_LO16);
9779 	    }
9780 	}
9781     }
9782   else
9783     abort ();
9784 
9785   if (!mips_opts.at && *used_at == 1)
9786     as_bad (_("macro used $at after \".set noat\""));
9787 }
9788 
9789 /* Move the contents of register SOURCE into register DEST.  */
9790 
9791 static void
9792 move_register (int dest, int source)
9793 {
9794   /* Prefer to use a 16-bit microMIPS instruction unless the previous
9795      instruction specifically requires a 32-bit one.  */
9796   if (mips_opts.micromips
9797       && !mips_opts.insn32
9798       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9799     macro_build (NULL, "move", "mp,mj", dest, source);
9800   else
9801     macro_build (NULL, "or", "d,v,t", dest, source, 0);
9802 }
9803 
9804 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
9805    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9806    The two alternatives are:
9807 
9808    Global symbol		Local symbol
9809    -------------		------------
9810    lw DEST,%got(SYMBOL)		lw DEST,%got(SYMBOL + OFFSET)
9811    ...				...
9812    addiu DEST,DEST,OFFSET	addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9813 
9814    load_got_offset emits the first instruction and add_got_offset
9815    emits the second for a 16-bit offset or add_got_offset_hilo emits
9816    a sequence to add a 32-bit offset using a scratch register.  */
9817 
9818 static void
9819 load_got_offset (int dest, expressionS *local)
9820 {
9821   expressionS global;
9822 
9823   global = *local;
9824   global.X_add_number = 0;
9825 
9826   relax_start (local->X_add_symbol);
9827   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9828 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9829   relax_switch ();
9830   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9831 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9832   relax_end ();
9833 }
9834 
9835 static void
9836 add_got_offset (int dest, expressionS *local)
9837 {
9838   expressionS global;
9839 
9840   global.X_op = O_constant;
9841   global.X_op_symbol = NULL;
9842   global.X_add_symbol = NULL;
9843   global.X_add_number = local->X_add_number;
9844 
9845   relax_start (local->X_add_symbol);
9846   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
9847 	       dest, dest, BFD_RELOC_LO16);
9848   relax_switch ();
9849   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
9850   relax_end ();
9851 }
9852 
9853 static void
9854 add_got_offset_hilo (int dest, expressionS *local, int tmp)
9855 {
9856   expressionS global;
9857   int hold_mips_optimize;
9858 
9859   global.X_op = O_constant;
9860   global.X_op_symbol = NULL;
9861   global.X_add_symbol = NULL;
9862   global.X_add_number = local->X_add_number;
9863 
9864   relax_start (local->X_add_symbol);
9865   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9866   relax_switch ();
9867   /* Set mips_optimize around the lui instruction to avoid
9868      inserting an unnecessary nop after the lw.  */
9869   hold_mips_optimize = mips_optimize;
9870   mips_optimize = 2;
9871   macro_build_lui (&global, tmp);
9872   mips_optimize = hold_mips_optimize;
9873   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9874   relax_end ();
9875 
9876   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9877 }
9878 
9879 /* Emit a sequence of instructions to emulate a branch likely operation.
9880    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
9881    is its complementing branch with the original condition negated.
9882    CALL is set if the original branch specified the link operation.
9883    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9884 
9885    Code like this is produced in the noreorder mode:
9886 
9887 	BRNEG	<args>, 1f
9888 	 nop
9889 	b	<sym>
9890 	 delay slot (executed only if branch taken)
9891     1:
9892 
9893    or, if CALL is set:
9894 
9895 	BRNEG	<args>, 1f
9896 	 nop
9897 	bal	<sym>
9898 	 delay slot (executed only if branch taken)
9899     1:
9900 
9901    In the reorder mode the delay slot would be filled with a nop anyway,
9902    so code produced is simply:
9903 
9904 	BR	<args>, <sym>
9905 	 nop
9906 
9907    This function is used when producing code for the microMIPS ASE that
9908    does not implement branch likely instructions in hardware.  */
9909 
9910 static void
9911 macro_build_branch_likely (const char *br, const char *brneg,
9912 			   int call, expressionS *ep, const char *fmt,
9913 			   unsigned int sreg, unsigned int treg)
9914 {
9915   int noreorder = mips_opts.noreorder;
9916   expressionS expr1;
9917 
9918   gas_assert (mips_opts.micromips);
9919   start_noreorder ();
9920   if (noreorder)
9921     {
9922       micromips_label_expr (&expr1);
9923       macro_build (&expr1, brneg, fmt, sreg, treg);
9924       macro_build (NULL, "nop", "");
9925       macro_build (ep, call ? "bal" : "b", "p");
9926 
9927       /* Set to true so that append_insn adds a label.  */
9928       emit_branch_likely_macro = TRUE;
9929     }
9930   else
9931     {
9932       macro_build (ep, br, fmt, sreg, treg);
9933       macro_build (NULL, "nop", "");
9934     }
9935   end_noreorder ();
9936 }
9937 
9938 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9939    the condition code tested.  EP specifies the branch target.  */
9940 
9941 static void
9942 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9943 {
9944   const int call = 0;
9945   const char *brneg;
9946   const char *br;
9947 
9948   switch (type)
9949     {
9950     case M_BC1FL:
9951       br = "bc1f";
9952       brneg = "bc1t";
9953       break;
9954     case M_BC1TL:
9955       br = "bc1t";
9956       brneg = "bc1f";
9957       break;
9958     case M_BC2FL:
9959       br = "bc2f";
9960       brneg = "bc2t";
9961       break;
9962     case M_BC2TL:
9963       br = "bc2t";
9964       brneg = "bc2f";
9965       break;
9966     default:
9967       abort ();
9968     }
9969   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9970 }
9971 
9972 /* Emit a two-argument branch macro specified by TYPE, using SREG as
9973    the register tested.  EP specifies the branch target.  */
9974 
9975 static void
9976 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9977 {
9978   const char *brneg = NULL;
9979   const char *br;
9980   int call = 0;
9981 
9982   switch (type)
9983     {
9984     case M_BGEZ:
9985       br = "bgez";
9986       break;
9987     case M_BGEZL:
9988       br = mips_opts.micromips ? "bgez" : "bgezl";
9989       brneg = "bltz";
9990       break;
9991     case M_BGEZALL:
9992       gas_assert (mips_opts.micromips);
9993       br = mips_opts.insn32 ? "bgezal" : "bgezals";
9994       brneg = "bltz";
9995       call = 1;
9996       break;
9997     case M_BGTZ:
9998       br = "bgtz";
9999       break;
10000     case M_BGTZL:
10001       br = mips_opts.micromips ? "bgtz" : "bgtzl";
10002       brneg = "blez";
10003       break;
10004     case M_BLEZ:
10005       br = "blez";
10006       break;
10007     case M_BLEZL:
10008       br = mips_opts.micromips ? "blez" : "blezl";
10009       brneg = "bgtz";
10010       break;
10011     case M_BLTZ:
10012       br = "bltz";
10013       break;
10014     case M_BLTZL:
10015       br = mips_opts.micromips ? "bltz" : "bltzl";
10016       brneg = "bgez";
10017       break;
10018     case M_BLTZALL:
10019       gas_assert (mips_opts.micromips);
10020       br = mips_opts.insn32 ? "bltzal" : "bltzals";
10021       brneg = "bgez";
10022       call = 1;
10023       break;
10024     default:
10025       abort ();
10026     }
10027   if (mips_opts.micromips && brneg)
10028     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10029   else
10030     macro_build (ep, br, "s,p", sreg);
10031 }
10032 
10033 /* Emit a three-argument branch macro specified by TYPE, using SREG and
10034    TREG as the registers tested.  EP specifies the branch target.  */
10035 
10036 static void
10037 macro_build_branch_rsrt (int type, expressionS *ep,
10038 			 unsigned int sreg, unsigned int treg)
10039 {
10040   const char *brneg = NULL;
10041   const int call = 0;
10042   const char *br;
10043 
10044   switch (type)
10045     {
10046     case M_BEQ:
10047     case M_BEQ_I:
10048       br = "beq";
10049       break;
10050     case M_BEQL:
10051     case M_BEQL_I:
10052       br = mips_opts.micromips ? "beq" : "beql";
10053       brneg = "bne";
10054       break;
10055     case M_BNE:
10056     case M_BNE_I:
10057       br = "bne";
10058       break;
10059     case M_BNEL:
10060     case M_BNEL_I:
10061       br = mips_opts.micromips ? "bne" : "bnel";
10062       brneg = "beq";
10063       break;
10064     default:
10065       abort ();
10066     }
10067   if (mips_opts.micromips && brneg)
10068     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10069   else
10070     macro_build (ep, br, "s,t,p", sreg, treg);
10071 }
10072 
10073 /* Return the high part that should be loaded in order to make the low
10074    part of VALUE accessible using an offset of OFFBITS bits.  */
10075 
10076 static offsetT
10077 offset_high_part (offsetT value, unsigned int offbits)
10078 {
10079   offsetT bias;
10080   addressT low_mask;
10081 
10082   if (offbits == 0)
10083     return value;
10084   bias = 1 << (offbits - 1);
10085   low_mask = bias * 2 - 1;
10086   return (value + bias) & ~low_mask;
10087 }
10088 
10089 /* Return true if the value stored in offset_expr and offset_reloc
10090    fits into a signed offset of OFFBITS bits.  RANGE is the maximum
10091    amount that the caller wants to add without inducing overflow
10092    and ALIGN is the known alignment of the value in bytes.  */
10093 
10094 static bfd_boolean
10095 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10096 {
10097   if (offbits == 16)
10098     {
10099       /* Accept any relocation operator if overflow isn't a concern.  */
10100       if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10101 	return TRUE;
10102 
10103       /* These relocations are guaranteed not to overflow in correct links.  */
10104       if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10105 	  || gprel16_reloc_p (*offset_reloc))
10106 	return TRUE;
10107     }
10108   if (offset_expr.X_op == O_constant
10109       && offset_high_part (offset_expr.X_add_number, offbits) == 0
10110       && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10111     return TRUE;
10112   return FALSE;
10113 }
10114 
10115 /*
10116  *			Build macros
10117  *   This routine implements the seemingly endless macro or synthesized
10118  * instructions and addressing modes in the mips assembly language. Many
10119  * of these macros are simple and are similar to each other. These could
10120  * probably be handled by some kind of table or grammar approach instead of
10121  * this verbose method. Others are not simple macros but are more like
10122  * optimizing code generation.
10123  *   One interesting optimization is when several store macros appear
10124  * consecutively that would load AT with the upper half of the same address.
10125  * The ensuing load upper instructions are omitted. This implies some kind
10126  * of global optimization. We currently only optimize within a single macro.
10127  *   For many of the load and store macros if the address is specified as a
10128  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10129  * first load register 'at' with zero and use it as the base register. The
10130  * mips assembler simply uses register $zero. Just one tiny optimization
10131  * we're missing.
10132  */
10133 static void
10134 macro (struct mips_cl_insn *ip, char *str)
10135 {
10136   const struct mips_operand_array *operands;
10137   unsigned int breg, i;
10138   unsigned int tempreg;
10139   int mask;
10140   int used_at = 0;
10141   expressionS label_expr;
10142   expressionS expr1;
10143   expressionS *ep;
10144   const char *s;
10145   const char *s2;
10146   const char *fmt;
10147   int likely = 0;
10148   int coproc = 0;
10149   int offbits = 16;
10150   int call = 0;
10151   int jals = 0;
10152   int dbl = 0;
10153   int imm = 0;
10154   int ust = 0;
10155   int lp = 0;
10156   bfd_boolean large_offset;
10157   int off;
10158   int hold_mips_optimize;
10159   unsigned int align;
10160   unsigned int op[MAX_OPERANDS];
10161 
10162   gas_assert (! mips_opts.mips16);
10163 
10164   operands = insn_operands (ip);
10165   for (i = 0; i < MAX_OPERANDS; i++)
10166     if (operands->operand[i])
10167       op[i] = insn_extract_operand (ip, operands->operand[i]);
10168     else
10169       op[i] = -1;
10170 
10171   mask = ip->insn_mo->mask;
10172 
10173   label_expr.X_op = O_constant;
10174   label_expr.X_op_symbol = NULL;
10175   label_expr.X_add_symbol = NULL;
10176   label_expr.X_add_number = 0;
10177 
10178   expr1.X_op = O_constant;
10179   expr1.X_op_symbol = NULL;
10180   expr1.X_add_symbol = NULL;
10181   expr1.X_add_number = 1;
10182   align = 1;
10183 
10184   switch (mask)
10185     {
10186     case M_DABS:
10187       dbl = 1;
10188       /* Fall through.  */
10189     case M_ABS:
10190       /*    bgez    $a0,1f
10191 	    move    v0,$a0
10192 	    sub     v0,$zero,$a0
10193 	 1:
10194        */
10195 
10196       start_noreorder ();
10197 
10198       if (mips_opts.micromips)
10199 	micromips_label_expr (&label_expr);
10200       else
10201 	label_expr.X_add_number = 8;
10202       macro_build (&label_expr, "bgez", "s,p", op[1]);
10203       if (op[0] == op[1])
10204 	macro_build (NULL, "nop", "");
10205       else
10206 	move_register (op[0], op[1]);
10207       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10208       if (mips_opts.micromips)
10209 	micromips_add_label ();
10210 
10211       end_noreorder ();
10212       break;
10213 
10214     case M_ADD_I:
10215       s = "addi";
10216       s2 = "add";
10217       goto do_addi;
10218     case M_ADDU_I:
10219       s = "addiu";
10220       s2 = "addu";
10221       goto do_addi;
10222     case M_DADD_I:
10223       dbl = 1;
10224       s = "daddi";
10225       s2 = "dadd";
10226       if (!mips_opts.micromips)
10227 	goto do_addi;
10228       if (imm_expr.X_add_number >= -0x200
10229 	  && imm_expr.X_add_number < 0x200)
10230 	{
10231 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
10232 		       (int) imm_expr.X_add_number);
10233 	  break;
10234 	}
10235       goto do_addi_i;
10236     case M_DADDU_I:
10237       dbl = 1;
10238       s = "daddiu";
10239       s2 = "daddu";
10240     do_addi:
10241       if (imm_expr.X_add_number >= -0x8000
10242 	  && imm_expr.X_add_number < 0x8000)
10243 	{
10244 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10245 	  break;
10246 	}
10247     do_addi_i:
10248       used_at = 1;
10249       load_register (AT, &imm_expr, dbl);
10250       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10251       break;
10252 
10253     case M_AND_I:
10254       s = "andi";
10255       s2 = "and";
10256       goto do_bit;
10257     case M_OR_I:
10258       s = "ori";
10259       s2 = "or";
10260       goto do_bit;
10261     case M_NOR_I:
10262       s = "";
10263       s2 = "nor";
10264       goto do_bit;
10265     case M_XOR_I:
10266       s = "xori";
10267       s2 = "xor";
10268     do_bit:
10269       if (imm_expr.X_add_number >= 0
10270 	  && imm_expr.X_add_number < 0x10000)
10271 	{
10272 	  if (mask != M_NOR_I)
10273 	    macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10274 	  else
10275 	    {
10276 	      macro_build (&imm_expr, "ori", "t,r,i",
10277 			   op[0], op[1], BFD_RELOC_LO16);
10278 	      macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10279 	    }
10280 	  break;
10281 	}
10282 
10283       used_at = 1;
10284       load_register (AT, &imm_expr, GPR_SIZE == 64);
10285       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10286       break;
10287 
10288     case M_BALIGN:
10289       switch (imm_expr.X_add_number)
10290 	{
10291 	case 0:
10292 	  macro_build (NULL, "nop", "");
10293 	  break;
10294 	case 2:
10295 	  macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10296 	  break;
10297 	case 1:
10298 	case 3:
10299 	  macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10300 		       (int) imm_expr.X_add_number);
10301 	  break;
10302 	default:
10303 	  as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10304 		  (unsigned long) imm_expr.X_add_number);
10305 	  break;
10306 	}
10307       break;
10308 
10309     case M_BC1FL:
10310     case M_BC1TL:
10311     case M_BC2FL:
10312     case M_BC2TL:
10313       gas_assert (mips_opts.micromips);
10314       macro_build_branch_ccl (mask, &offset_expr,
10315 			      EXTRACT_OPERAND (1, BCC, *ip));
10316       break;
10317 
10318     case M_BEQ_I:
10319     case M_BEQL_I:
10320     case M_BNE_I:
10321     case M_BNEL_I:
10322       if (imm_expr.X_add_number == 0)
10323 	op[1] = 0;
10324       else
10325 	{
10326 	  op[1] = AT;
10327 	  used_at = 1;
10328 	  load_register (op[1], &imm_expr, GPR_SIZE == 64);
10329 	}
10330       /* Fall through.  */
10331     case M_BEQL:
10332     case M_BNEL:
10333       macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10334       break;
10335 
10336     case M_BGEL:
10337       likely = 1;
10338       /* Fall through.  */
10339     case M_BGE:
10340       if (op[1] == 0)
10341 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10342       else if (op[0] == 0)
10343 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10344       else
10345 	{
10346 	  used_at = 1;
10347 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10348 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10349 				   &offset_expr, AT, ZERO);
10350 	}
10351       break;
10352 
10353     case M_BGEZL:
10354     case M_BGEZALL:
10355     case M_BGTZL:
10356     case M_BLEZL:
10357     case M_BLTZL:
10358     case M_BLTZALL:
10359       macro_build_branch_rs (mask, &offset_expr, op[0]);
10360       break;
10361 
10362     case M_BGTL_I:
10363       likely = 1;
10364       /* Fall through.  */
10365     case M_BGT_I:
10366       /* Check for > max integer.  */
10367       if (imm_expr.X_add_number >= GPR_SMAX)
10368 	{
10369 	do_false:
10370 	  /* Result is always false.  */
10371 	  if (! likely)
10372 	    macro_build (NULL, "nop", "");
10373 	  else
10374 	    macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10375 	  break;
10376 	}
10377       ++imm_expr.X_add_number;
10378       /* FALLTHROUGH */
10379     case M_BGE_I:
10380     case M_BGEL_I:
10381       if (mask == M_BGEL_I)
10382 	likely = 1;
10383       if (imm_expr.X_add_number == 0)
10384 	{
10385 	  macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10386 				 &offset_expr, op[0]);
10387 	  break;
10388 	}
10389       if (imm_expr.X_add_number == 1)
10390 	{
10391 	  macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10392 				 &offset_expr, op[0]);
10393 	  break;
10394 	}
10395       if (imm_expr.X_add_number <= GPR_SMIN)
10396 	{
10397 	do_true:
10398 	  /* result is always true */
10399 	  as_warn (_("branch %s is always true"), ip->insn_mo->name);
10400 	  macro_build (&offset_expr, "b", "p");
10401 	  break;
10402 	}
10403       used_at = 1;
10404       set_at (op[0], 0);
10405       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10406 			       &offset_expr, AT, ZERO);
10407       break;
10408 
10409     case M_BGEUL:
10410       likely = 1;
10411       /* Fall through.  */
10412     case M_BGEU:
10413       if (op[1] == 0)
10414 	goto do_true;
10415       else if (op[0] == 0)
10416 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10417 				 &offset_expr, ZERO, op[1]);
10418       else
10419 	{
10420 	  used_at = 1;
10421 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10422 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10423 				   &offset_expr, AT, ZERO);
10424 	}
10425       break;
10426 
10427     case M_BGTUL_I:
10428       likely = 1;
10429       /* Fall through.  */
10430     case M_BGTU_I:
10431       if (op[0] == 0
10432 	  || (GPR_SIZE == 32
10433 	      && imm_expr.X_add_number == -1))
10434 	goto do_false;
10435       ++imm_expr.X_add_number;
10436       /* FALLTHROUGH */
10437     case M_BGEU_I:
10438     case M_BGEUL_I:
10439       if (mask == M_BGEUL_I)
10440 	likely = 1;
10441       if (imm_expr.X_add_number == 0)
10442 	goto do_true;
10443       else if (imm_expr.X_add_number == 1)
10444 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10445 				 &offset_expr, op[0], ZERO);
10446       else
10447 	{
10448 	  used_at = 1;
10449 	  set_at (op[0], 1);
10450 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10451 				   &offset_expr, AT, ZERO);
10452 	}
10453       break;
10454 
10455     case M_BGTL:
10456       likely = 1;
10457       /* Fall through.  */
10458     case M_BGT:
10459       if (op[1] == 0)
10460 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10461       else if (op[0] == 0)
10462 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10463       else
10464 	{
10465 	  used_at = 1;
10466 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10467 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10468 				   &offset_expr, AT, ZERO);
10469 	}
10470       break;
10471 
10472     case M_BGTUL:
10473       likely = 1;
10474       /* Fall through.  */
10475     case M_BGTU:
10476       if (op[1] == 0)
10477 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10478 				 &offset_expr, op[0], ZERO);
10479       else if (op[0] == 0)
10480 	goto do_false;
10481       else
10482 	{
10483 	  used_at = 1;
10484 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10485 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10486 				   &offset_expr, AT, ZERO);
10487 	}
10488       break;
10489 
10490     case M_BLEL:
10491       likely = 1;
10492       /* Fall through.  */
10493     case M_BLE:
10494       if (op[1] == 0)
10495 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10496       else if (op[0] == 0)
10497 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10498       else
10499 	{
10500 	  used_at = 1;
10501 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10502 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10503 				   &offset_expr, AT, ZERO);
10504 	}
10505       break;
10506 
10507     case M_BLEL_I:
10508       likely = 1;
10509       /* Fall through.  */
10510     case M_BLE_I:
10511       if (imm_expr.X_add_number >= GPR_SMAX)
10512 	goto do_true;
10513       ++imm_expr.X_add_number;
10514       /* FALLTHROUGH */
10515     case M_BLT_I:
10516     case M_BLTL_I:
10517       if (mask == M_BLTL_I)
10518 	likely = 1;
10519       if (imm_expr.X_add_number == 0)
10520 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10521       else if (imm_expr.X_add_number == 1)
10522 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10523       else
10524 	{
10525 	  used_at = 1;
10526 	  set_at (op[0], 0);
10527 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10528 				   &offset_expr, AT, ZERO);
10529 	}
10530       break;
10531 
10532     case M_BLEUL:
10533       likely = 1;
10534       /* Fall through.  */
10535     case M_BLEU:
10536       if (op[1] == 0)
10537 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10538 				 &offset_expr, op[0], ZERO);
10539       else if (op[0] == 0)
10540 	goto do_true;
10541       else
10542 	{
10543 	  used_at = 1;
10544 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10545 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10546 				   &offset_expr, AT, ZERO);
10547 	}
10548       break;
10549 
10550     case M_BLEUL_I:
10551       likely = 1;
10552       /* Fall through.  */
10553     case M_BLEU_I:
10554       if (op[0] == 0
10555 	  || (GPR_SIZE == 32
10556 	      && imm_expr.X_add_number == -1))
10557 	goto do_true;
10558       ++imm_expr.X_add_number;
10559       /* FALLTHROUGH */
10560     case M_BLTU_I:
10561     case M_BLTUL_I:
10562       if (mask == M_BLTUL_I)
10563 	likely = 1;
10564       if (imm_expr.X_add_number == 0)
10565 	goto do_false;
10566       else if (imm_expr.X_add_number == 1)
10567 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10568 				 &offset_expr, op[0], ZERO);
10569       else
10570 	{
10571 	  used_at = 1;
10572 	  set_at (op[0], 1);
10573 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10574 				   &offset_expr, AT, ZERO);
10575 	}
10576       break;
10577 
10578     case M_BLTL:
10579       likely = 1;
10580       /* Fall through.  */
10581     case M_BLT:
10582       if (op[1] == 0)
10583 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10584       else if (op[0] == 0)
10585 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10586       else
10587 	{
10588 	  used_at = 1;
10589 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10590 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10591 				   &offset_expr, AT, ZERO);
10592 	}
10593       break;
10594 
10595     case M_BLTUL:
10596       likely = 1;
10597       /* Fall through.  */
10598     case M_BLTU:
10599       if (op[1] == 0)
10600 	goto do_false;
10601       else if (op[0] == 0)
10602 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10603 				 &offset_expr, ZERO, op[1]);
10604       else
10605 	{
10606 	  used_at = 1;
10607 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10608 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10609 				   &offset_expr, AT, ZERO);
10610 	}
10611       break;
10612 
10613     case M_DDIV_3:
10614       dbl = 1;
10615       /* Fall through.  */
10616     case M_DIV_3:
10617       s = "mflo";
10618       goto do_div3;
10619     case M_DREM_3:
10620       dbl = 1;
10621       /* Fall through.  */
10622     case M_REM_3:
10623       s = "mfhi";
10624     do_div3:
10625       if (op[2] == 0)
10626 	{
10627 	  as_warn (_("divide by zero"));
10628 	  if (mips_trap)
10629 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10630 	  else
10631 	    macro_build (NULL, "break", BRK_FMT, 7);
10632 	  break;
10633 	}
10634 
10635       start_noreorder ();
10636       if (mips_trap)
10637 	{
10638 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10639 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10640 	}
10641       else
10642 	{
10643 	  if (mips_opts.micromips)
10644 	    micromips_label_expr (&label_expr);
10645 	  else
10646 	    label_expr.X_add_number = 8;
10647 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10648 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10649 	  macro_build (NULL, "break", BRK_FMT, 7);
10650 	  if (mips_opts.micromips)
10651 	    micromips_add_label ();
10652 	}
10653       expr1.X_add_number = -1;
10654       used_at = 1;
10655       load_register (AT, &expr1, dbl);
10656       if (mips_opts.micromips)
10657 	micromips_label_expr (&label_expr);
10658       else
10659 	label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10660       macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10661       if (dbl)
10662 	{
10663 	  expr1.X_add_number = 1;
10664 	  load_register (AT, &expr1, dbl);
10665 	  macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10666 	}
10667       else
10668 	{
10669 	  expr1.X_add_number = 0x80000000;
10670 	  macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10671 	}
10672       if (mips_trap)
10673 	{
10674 	  macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10675 	  /* We want to close the noreorder block as soon as possible, so
10676 	     that later insns are available for delay slot filling.  */
10677 	  end_noreorder ();
10678 	}
10679       else
10680 	{
10681 	  if (mips_opts.micromips)
10682 	    micromips_label_expr (&label_expr);
10683 	  else
10684 	    label_expr.X_add_number = 8;
10685 	  macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10686 	  macro_build (NULL, "nop", "");
10687 
10688 	  /* We want to close the noreorder block as soon as possible, so
10689 	     that later insns are available for delay slot filling.  */
10690 	  end_noreorder ();
10691 
10692 	  macro_build (NULL, "break", BRK_FMT, 6);
10693 	}
10694       if (mips_opts.micromips)
10695 	micromips_add_label ();
10696       macro_build (NULL, s, MFHL_FMT, op[0]);
10697       break;
10698 
10699     case M_DIV_3I:
10700       s = "div";
10701       s2 = "mflo";
10702       goto do_divi;
10703     case M_DIVU_3I:
10704       s = "divu";
10705       s2 = "mflo";
10706       goto do_divi;
10707     case M_REM_3I:
10708       s = "div";
10709       s2 = "mfhi";
10710       goto do_divi;
10711     case M_REMU_3I:
10712       s = "divu";
10713       s2 = "mfhi";
10714       goto do_divi;
10715     case M_DDIV_3I:
10716       dbl = 1;
10717       s = "ddiv";
10718       s2 = "mflo";
10719       goto do_divi;
10720     case M_DDIVU_3I:
10721       dbl = 1;
10722       s = "ddivu";
10723       s2 = "mflo";
10724       goto do_divi;
10725     case M_DREM_3I:
10726       dbl = 1;
10727       s = "ddiv";
10728       s2 = "mfhi";
10729       goto do_divi;
10730     case M_DREMU_3I:
10731       dbl = 1;
10732       s = "ddivu";
10733       s2 = "mfhi";
10734     do_divi:
10735       if (imm_expr.X_add_number == 0)
10736 	{
10737 	  as_warn (_("divide by zero"));
10738 	  if (mips_trap)
10739 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10740 	  else
10741 	    macro_build (NULL, "break", BRK_FMT, 7);
10742 	  break;
10743 	}
10744       if (imm_expr.X_add_number == 1)
10745 	{
10746 	  if (strcmp (s2, "mflo") == 0)
10747 	    move_register (op[0], op[1]);
10748 	  else
10749 	    move_register (op[0], ZERO);
10750 	  break;
10751 	}
10752       if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
10753 	{
10754 	  if (strcmp (s2, "mflo") == 0)
10755 	    macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
10756 	  else
10757 	    move_register (op[0], ZERO);
10758 	  break;
10759 	}
10760 
10761       used_at = 1;
10762       load_register (AT, &imm_expr, dbl);
10763       macro_build (NULL, s, "z,s,t", op[1], AT);
10764       macro_build (NULL, s2, MFHL_FMT, op[0]);
10765       break;
10766 
10767     case M_DIVU_3:
10768       s = "divu";
10769       s2 = "mflo";
10770       goto do_divu3;
10771     case M_REMU_3:
10772       s = "divu";
10773       s2 = "mfhi";
10774       goto do_divu3;
10775     case M_DDIVU_3:
10776       s = "ddivu";
10777       s2 = "mflo";
10778       goto do_divu3;
10779     case M_DREMU_3:
10780       s = "ddivu";
10781       s2 = "mfhi";
10782     do_divu3:
10783       start_noreorder ();
10784       if (mips_trap)
10785 	{
10786 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10787 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
10788 	  /* We want to close the noreorder block as soon as possible, so
10789 	     that later insns are available for delay slot filling.  */
10790 	  end_noreorder ();
10791 	}
10792       else
10793 	{
10794 	  if (mips_opts.micromips)
10795 	    micromips_label_expr (&label_expr);
10796 	  else
10797 	    label_expr.X_add_number = 8;
10798 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10799 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
10800 
10801 	  /* We want to close the noreorder block as soon as possible, so
10802 	     that later insns are available for delay slot filling.  */
10803 	  end_noreorder ();
10804 	  macro_build (NULL, "break", BRK_FMT, 7);
10805 	  if (mips_opts.micromips)
10806 	    micromips_add_label ();
10807 	}
10808       macro_build (NULL, s2, MFHL_FMT, op[0]);
10809       break;
10810 
10811     case M_DLCA_AB:
10812       dbl = 1;
10813       /* Fall through.  */
10814     case M_LCA_AB:
10815       call = 1;
10816       goto do_la;
10817     case M_DLA_AB:
10818       dbl = 1;
10819       /* Fall through.  */
10820     case M_LA_AB:
10821     do_la:
10822       /* Load the address of a symbol into a register.  If breg is not
10823 	 zero, we then add a base register to it.  */
10824 
10825       breg = op[2];
10826       if (dbl && GPR_SIZE == 32)
10827 	as_warn (_("dla used to load 32-bit register; recommend using la "
10828 		   "instead"));
10829 
10830       if (!dbl && HAVE_64BIT_OBJECTS)
10831 	as_warn (_("la used to load 64-bit address; recommend using dla "
10832 		   "instead"));
10833 
10834       if (small_offset_p (0, align, 16))
10835 	{
10836 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
10837 		       -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10838 	  break;
10839 	}
10840 
10841       if (mips_opts.at && (op[0] == breg))
10842 	{
10843 	  tempreg = AT;
10844 	  used_at = 1;
10845 	}
10846       else
10847 	tempreg = op[0];
10848 
10849       if (offset_expr.X_op != O_symbol
10850 	  && offset_expr.X_op != O_constant)
10851 	{
10852 	  as_bad (_("expression too complex"));
10853 	  offset_expr.X_op = O_constant;
10854 	}
10855 
10856       if (offset_expr.X_op == O_constant)
10857 	load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
10858       else if (mips_pic == NO_PIC)
10859 	{
10860 	  /* If this is a reference to a GP relative symbol, we want
10861 	       addiu	$tempreg,$gp,<sym>	(BFD_RELOC_GPREL16)
10862 	     Otherwise we want
10863 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
10864 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
10865 	     If we have a constant, we need two instructions anyhow,
10866 	     so we may as well always use the latter form.
10867 
10868 	     With 64bit address space and a usable $at we want
10869 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
10870 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
10871 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
10872 	       daddiu	$at,<sym>		(BFD_RELOC_LO16)
10873 	       dsll32	$tempreg,0
10874 	       daddu	$tempreg,$tempreg,$at
10875 
10876 	     If $at is already in use, we use a path which is suboptimal
10877 	     on superscalar processors.
10878 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
10879 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
10880 	       dsll	$tempreg,16
10881 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
10882 	       dsll	$tempreg,16
10883 	       daddiu	$tempreg,<sym>		(BFD_RELOC_LO16)
10884 
10885 	     For GP relative symbols in 64bit address space we can use
10886 	     the same sequence as in 32bit address space.  */
10887 	  if (HAVE_64BIT_SYMBOLS)
10888 	    {
10889 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10890 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10891 		{
10892 		  relax_start (offset_expr.X_add_symbol);
10893 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10894 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10895 		  relax_switch ();
10896 		}
10897 
10898 	      if (used_at == 0 && mips_opts.at)
10899 		{
10900 		  macro_build (&offset_expr, "lui", LUI_FMT,
10901 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
10902 		  macro_build (&offset_expr, "lui", LUI_FMT,
10903 			       AT, BFD_RELOC_HI16_S);
10904 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10905 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10906 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10907 			       AT, AT, BFD_RELOC_LO16);
10908 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
10909 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
10910 		  used_at = 1;
10911 		}
10912 	      else
10913 		{
10914 		  macro_build (&offset_expr, "lui", LUI_FMT,
10915 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
10916 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10917 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
10918 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10919 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10920 			       tempreg, tempreg, BFD_RELOC_HI16_S);
10921 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
10922 		  macro_build (&offset_expr, "daddiu", "t,r,j",
10923 			       tempreg, tempreg, BFD_RELOC_LO16);
10924 		}
10925 
10926 	      if (mips_relax.sequence)
10927 		relax_end ();
10928 	    }
10929 	  else
10930 	    {
10931 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10932 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10933 		{
10934 		  relax_start (offset_expr.X_add_symbol);
10935 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10936 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10937 		  relax_switch ();
10938 		}
10939 	      if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
10940 		as_bad (_("offset too large"));
10941 	      macro_build_lui (&offset_expr, tempreg);
10942 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10943 			   tempreg, tempreg, BFD_RELOC_LO16);
10944 	      if (mips_relax.sequence)
10945 		relax_end ();
10946 	    }
10947 	}
10948       else if (!mips_big_got && !HAVE_NEWABI)
10949 	{
10950 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10951 
10952 	  /* If this is a reference to an external symbol, and there
10953 	     is no constant, we want
10954 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10955 	     or for lca or if tempreg is PIC_CALL_REG
10956 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
10957 	     For a local symbol, we want
10958 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10959 	       nop
10960 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
10961 
10962 	     If we have a small constant, and this is a reference to
10963 	     an external symbol, we want
10964 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10965 	       nop
10966 	       addiu	$tempreg,$tempreg,<constant>
10967 	     For a local symbol, we want the same instruction
10968 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
10969 	     addiu instruction.
10970 
10971 	     If we have a large constant, and this is a reference to
10972 	     an external symbol, we want
10973 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
10974 	       lui	$at,<hiconstant>
10975 	       addiu	$at,$at,<loconstant>
10976 	       addu	$tempreg,$tempreg,$at
10977 	     For a local symbol, we want the same instruction
10978 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
10979 	     addiu instruction.
10980 	   */
10981 
10982 	  if (offset_expr.X_add_number == 0)
10983 	    {
10984 	      if (mips_pic == SVR4_PIC
10985 		  && breg == 0
10986 		  && (call || tempreg == PIC_CALL_REG))
10987 		lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10988 
10989 	      relax_start (offset_expr.X_add_symbol);
10990 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10991 			   lw_reloc_type, mips_gp_register);
10992 	      if (breg != 0)
10993 		{
10994 		  /* We're going to put in an addu instruction using
10995 		     tempreg, so we may as well insert the nop right
10996 		     now.  */
10997 		  load_delay_nop ();
10998 		}
10999 	      relax_switch ();
11000 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11001 			   tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
11002 	      load_delay_nop ();
11003 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11004 			   tempreg, tempreg, BFD_RELOC_LO16);
11005 	      relax_end ();
11006 	      /* FIXME: If breg == 0, and the next instruction uses
11007 		 $tempreg, then if this variant case is used an extra
11008 		 nop will be generated.  */
11009 	    }
11010 	  else if (offset_expr.X_add_number >= -0x8000
11011 		   && offset_expr.X_add_number < 0x8000)
11012 	    {
11013 	      load_got_offset (tempreg, &offset_expr);
11014 	      load_delay_nop ();
11015 	      add_got_offset (tempreg, &offset_expr);
11016 	    }
11017 	  else
11018 	    {
11019 	      expr1.X_add_number = offset_expr.X_add_number;
11020 	      offset_expr.X_add_number =
11021 		SEXT_16BIT (offset_expr.X_add_number);
11022 	      load_got_offset (tempreg, &offset_expr);
11023 	      offset_expr.X_add_number = expr1.X_add_number;
11024 	      /* If we are going to add in a base register, and the
11025 		 target register and the base register are the same,
11026 		 then we are using AT as a temporary register.  Since
11027 		 we want to load the constant into AT, we add our
11028 		 current AT (from the global offset table) and the
11029 		 register into the register now, and pretend we were
11030 		 not using a base register.  */
11031 	      if (breg == op[0])
11032 		{
11033 		  load_delay_nop ();
11034 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11035 			       op[0], AT, breg);
11036 		  breg = 0;
11037 		  tempreg = op[0];
11038 		}
11039 	      add_got_offset_hilo (tempreg, &offset_expr, AT);
11040 	      used_at = 1;
11041 	    }
11042 	}
11043       else if (!mips_big_got && HAVE_NEWABI)
11044 	{
11045 	  int add_breg_early = 0;
11046 
11047 	  /* If this is a reference to an external, and there is no
11048 	     constant, or local symbol (*), with or without a
11049 	     constant, we want
11050 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11051 	     or for lca or if tempreg is PIC_CALL_REG
11052 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
11053 
11054 	     If we have a small constant, and this is a reference to
11055 	     an external symbol, we want
11056 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11057 	       addiu	$tempreg,$tempreg,<constant>
11058 
11059 	     If we have a large constant, and this is a reference to
11060 	     an external symbol, we want
11061 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11062 	       lui	$at,<hiconstant>
11063 	       addiu	$at,$at,<loconstant>
11064 	       addu	$tempreg,$tempreg,$at
11065 
11066 	     (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11067 	     local symbols, even though it introduces an additional
11068 	     instruction.  */
11069 
11070 	  if (offset_expr.X_add_number)
11071 	    {
11072 	      expr1.X_add_number = offset_expr.X_add_number;
11073 	      offset_expr.X_add_number = 0;
11074 
11075 	      relax_start (offset_expr.X_add_symbol);
11076 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11077 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11078 
11079 	      if (expr1.X_add_number >= -0x8000
11080 		  && expr1.X_add_number < 0x8000)
11081 		{
11082 		  macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11083 			       tempreg, tempreg, BFD_RELOC_LO16);
11084 		}
11085 	      else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11086 		{
11087 		  unsigned int dreg;
11088 
11089 		  /* If we are going to add in a base register, and the
11090 		     target register and the base register are the same,
11091 		     then we are using AT as a temporary register.  Since
11092 		     we want to load the constant into AT, we add our
11093 		     current AT (from the global offset table) and the
11094 		     register into the register now, and pretend we were
11095 		     not using a base register.  */
11096 		  if (breg != op[0])
11097 		    dreg = tempreg;
11098 		  else
11099 		    {
11100 		      gas_assert (tempreg == AT);
11101 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11102 				   op[0], AT, breg);
11103 		      dreg = op[0];
11104 		      add_breg_early = 1;
11105 		    }
11106 
11107 		  load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11108 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11109 			       dreg, dreg, AT);
11110 
11111 		  used_at = 1;
11112 		}
11113 	      else
11114 		as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11115 
11116 	      relax_switch ();
11117 	      offset_expr.X_add_number = expr1.X_add_number;
11118 
11119 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11120 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11121 	      if (add_breg_early)
11122 		{
11123 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11124 			       op[0], tempreg, breg);
11125 		  breg = 0;
11126 		  tempreg = op[0];
11127 		}
11128 	      relax_end ();
11129 	    }
11130 	  else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11131 	    {
11132 	      relax_start (offset_expr.X_add_symbol);
11133 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11134 			   BFD_RELOC_MIPS_CALL16, mips_gp_register);
11135 	      relax_switch ();
11136 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11137 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11138 	      relax_end ();
11139 	    }
11140 	  else
11141 	    {
11142 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11143 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11144 	    }
11145 	}
11146       else if (mips_big_got && !HAVE_NEWABI)
11147 	{
11148 	  int gpdelay;
11149 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11150 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11151 	  int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11152 
11153 	  /* This is the large GOT case.  If this is a reference to an
11154 	     external symbol, and there is no constant, we want
11155 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11156 	       addu	$tempreg,$tempreg,$gp
11157 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11158 	     or for lca or if tempreg is PIC_CALL_REG
11159 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11160 	       addu	$tempreg,$tempreg,$gp
11161 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11162 	     For a local symbol, we want
11163 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11164 	       nop
11165 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11166 
11167 	     If we have a small constant, and this is a reference to
11168 	     an external symbol, we want
11169 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11170 	       addu	$tempreg,$tempreg,$gp
11171 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11172 	       nop
11173 	       addiu	$tempreg,$tempreg,<constant>
11174 	     For a local symbol, we want
11175 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11176 	       nop
11177 	       addiu	$tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11178 
11179 	     If we have a large constant, and this is a reference to
11180 	     an external symbol, we want
11181 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11182 	       addu	$tempreg,$tempreg,$gp
11183 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11184 	       lui	$at,<hiconstant>
11185 	       addiu	$at,$at,<loconstant>
11186 	       addu	$tempreg,$tempreg,$at
11187 	     For a local symbol, we want
11188 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11189 	       lui	$at,<hiconstant>
11190 	       addiu	$at,$at,<loconstant>	(BFD_RELOC_LO16)
11191 	       addu	$tempreg,$tempreg,$at
11192 	  */
11193 
11194 	  expr1.X_add_number = offset_expr.X_add_number;
11195 	  offset_expr.X_add_number = 0;
11196 	  relax_start (offset_expr.X_add_symbol);
11197 	  gpdelay = reg_needs_delay (mips_gp_register);
11198 	  if (expr1.X_add_number == 0 && breg == 0
11199 	      && (call || tempreg == PIC_CALL_REG))
11200 	    {
11201 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11202 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11203 	    }
11204 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11205 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11206 		       tempreg, tempreg, mips_gp_register);
11207 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11208 		       tempreg, lw_reloc_type, tempreg);
11209 	  if (expr1.X_add_number == 0)
11210 	    {
11211 	      if (breg != 0)
11212 		{
11213 		  /* We're going to put in an addu instruction using
11214 		     tempreg, so we may as well insert the nop right
11215 		     now.  */
11216 		  load_delay_nop ();
11217 		}
11218 	    }
11219 	  else if (expr1.X_add_number >= -0x8000
11220 		   && expr1.X_add_number < 0x8000)
11221 	    {
11222 	      load_delay_nop ();
11223 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11224 			   tempreg, tempreg, BFD_RELOC_LO16);
11225 	    }
11226 	  else
11227 	    {
11228 	      unsigned int dreg;
11229 
11230 	      /* If we are going to add in a base register, and the
11231 		 target register and the base register are the same,
11232 		 then we are using AT as a temporary register.  Since
11233 		 we want to load the constant into AT, we add our
11234 		 current AT (from the global offset table) and the
11235 		 register into the register now, and pretend we were
11236 		 not using a base register.  */
11237 	      if (breg != op[0])
11238 		dreg = tempreg;
11239 	      else
11240 		{
11241 		  gas_assert (tempreg == AT);
11242 		  load_delay_nop ();
11243 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11244 			       op[0], AT, breg);
11245 		  dreg = op[0];
11246 		}
11247 
11248 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11249 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11250 
11251 	      used_at = 1;
11252 	    }
11253 	  offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11254 	  relax_switch ();
11255 
11256 	  if (gpdelay)
11257 	    {
11258 	      /* This is needed because this instruction uses $gp, but
11259 		 the first instruction on the main stream does not.  */
11260 	      macro_build (NULL, "nop", "");
11261 	    }
11262 
11263 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11264 		       local_reloc_type, mips_gp_register);
11265 	  if (expr1.X_add_number >= -0x8000
11266 	      && expr1.X_add_number < 0x8000)
11267 	    {
11268 	      load_delay_nop ();
11269 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11270 			   tempreg, tempreg, BFD_RELOC_LO16);
11271 	      /* FIXME: If add_number is 0, and there was no base
11272 		 register, the external symbol case ended with a load,
11273 		 so if the symbol turns out to not be external, and
11274 		 the next instruction uses tempreg, an unnecessary nop
11275 		 will be inserted.  */
11276 	    }
11277 	  else
11278 	    {
11279 	      if (breg == op[0])
11280 		{
11281 		  /* We must add in the base register now, as in the
11282 		     external symbol case.  */
11283 		  gas_assert (tempreg == AT);
11284 		  load_delay_nop ();
11285 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11286 			       op[0], AT, breg);
11287 		  tempreg = op[0];
11288 		  /* We set breg to 0 because we have arranged to add
11289 		     it in in both cases.  */
11290 		  breg = 0;
11291 		}
11292 
11293 	      macro_build_lui (&expr1, AT);
11294 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11295 			   AT, AT, BFD_RELOC_LO16);
11296 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11297 			   tempreg, tempreg, AT);
11298 	      used_at = 1;
11299 	    }
11300 	  relax_end ();
11301 	}
11302       else if (mips_big_got && HAVE_NEWABI)
11303 	{
11304 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11305 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11306 	  int add_breg_early = 0;
11307 
11308 	  /* This is the large GOT case.  If this is a reference to an
11309 	     external symbol, and there is no constant, we want
11310 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11311 	       add	$tempreg,$tempreg,$gp
11312 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11313 	     or for lca or if tempreg is PIC_CALL_REG
11314 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11315 	       add	$tempreg,$tempreg,$gp
11316 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11317 
11318 	     If we have a small constant, and this is a reference to
11319 	     an external symbol, we want
11320 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11321 	       add	$tempreg,$tempreg,$gp
11322 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11323 	       addi	$tempreg,$tempreg,<constant>
11324 
11325 	     If we have a large constant, and this is a reference to
11326 	     an external symbol, we want
11327 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11328 	       addu	$tempreg,$tempreg,$gp
11329 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11330 	       lui	$at,<hiconstant>
11331 	       addi	$at,$at,<loconstant>
11332 	       add	$tempreg,$tempreg,$at
11333 
11334 	     If we have NewABI, and we know it's a local symbol, we want
11335 	       lw	$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
11336 	       addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
11337 	     otherwise we have to resort to GOT_HI16/GOT_LO16.  */
11338 
11339 	  relax_start (offset_expr.X_add_symbol);
11340 
11341 	  expr1.X_add_number = offset_expr.X_add_number;
11342 	  offset_expr.X_add_number = 0;
11343 
11344 	  if (expr1.X_add_number == 0 && breg == 0
11345 	      && (call || tempreg == PIC_CALL_REG))
11346 	    {
11347 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11348 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11349 	    }
11350 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11351 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11352 		       tempreg, tempreg, mips_gp_register);
11353 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11354 		       tempreg, lw_reloc_type, tempreg);
11355 
11356 	  if (expr1.X_add_number == 0)
11357 	    ;
11358 	  else if (expr1.X_add_number >= -0x8000
11359 		   && expr1.X_add_number < 0x8000)
11360 	    {
11361 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11362 			   tempreg, tempreg, BFD_RELOC_LO16);
11363 	    }
11364 	  else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11365 	    {
11366 	      unsigned int dreg;
11367 
11368 	      /* If we are going to add in a base register, and the
11369 		 target register and the base register are the same,
11370 		 then we are using AT as a temporary register.  Since
11371 		 we want to load the constant into AT, we add our
11372 		 current AT (from the global offset table) and the
11373 		 register into the register now, and pretend we were
11374 		 not using a base register.  */
11375 	      if (breg != op[0])
11376 		dreg = tempreg;
11377 	      else
11378 		{
11379 		  gas_assert (tempreg == AT);
11380 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11381 			       op[0], AT, breg);
11382 		  dreg = op[0];
11383 		  add_breg_early = 1;
11384 		}
11385 
11386 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11387 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11388 
11389 	      used_at = 1;
11390 	    }
11391 	  else
11392 	    as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11393 
11394 	  relax_switch ();
11395 	  offset_expr.X_add_number = expr1.X_add_number;
11396 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11397 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11398 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11399 		       tempreg, BFD_RELOC_MIPS_GOT_OFST);
11400 	  if (add_breg_early)
11401 	    {
11402 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11403 			   op[0], tempreg, breg);
11404 	      breg = 0;
11405 	      tempreg = op[0];
11406 	    }
11407 	  relax_end ();
11408 	}
11409       else
11410 	abort ();
11411 
11412       if (breg != 0)
11413 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11414       break;
11415 
11416     case M_JR_S:
11417       macro_build_jrpatch (&expr1, op[2]);
11418       macro_build (NULL, "jr", "s", op[2]);
11419       return;	/* didn't modify $at */
11420 
11421     case M_J_S:
11422       macro_build_jrpatch (&expr1, op[2]);
11423       macro_build (NULL, "j", "s", op[2]);
11424       return;	/* didn't modify $at */
11425 
11426     case M_JALR_S:
11427       macro_build_jrpatch (&expr1, op[2]);
11428       macro_build (NULL, "jalr", "s", op[2]);
11429       return;	/* didn't modify $at */
11430 
11431     case M_JALR_DS:
11432       macro_build_jrpatch (&expr1, op[2]);
11433       macro_build (NULL, "jalr", "d,s", op[0], op[2]);
11434       return;	/* didn't modify $at */
11435 
11436     case M_MSGSND:
11437       gas_assert (!mips_opts.micromips);
11438       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11439       break;
11440 
11441     case M_MSGLD:
11442       gas_assert (!mips_opts.micromips);
11443       macro_build (NULL, "c2", "C", 0x02);
11444       break;
11445 
11446     case M_MSGLD_T:
11447       gas_assert (!mips_opts.micromips);
11448       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11449       break;
11450 
11451     case M_MSGWAIT:
11452       gas_assert (!mips_opts.micromips);
11453       macro_build (NULL, "c2", "C", 3);
11454       break;
11455 
11456     case M_MSGWAIT_T:
11457       gas_assert (!mips_opts.micromips);
11458       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11459       break;
11460 
11461     case M_J_A:
11462       /* The j instruction may not be used in PIC code, since it
11463 	 requires an absolute address.  We convert it to a b
11464 	 instruction.  */
11465       if (mips_pic == NO_PIC)
11466 	macro_build (&offset_expr, "j", "a");
11467       else
11468 	macro_build (&offset_expr, "b", "p");
11469       break;
11470 
11471       /* The jal instructions must be handled as macros because when
11472 	 generating PIC code they expand to multi-instruction
11473 	 sequences.  Normally they are simple instructions.  */
11474     case M_JALS_1:
11475       op[1] = op[0];
11476       op[0] = RA;
11477       /* Fall through.  */
11478     case M_JALS_2:
11479       gas_assert (mips_opts.micromips);
11480       if (mips_opts.insn32)
11481 	{
11482 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11483 	  break;
11484 	}
11485       jals = 1;
11486       goto jal;
11487     case M_JAL_1:
11488       op[1] = op[0];
11489       op[0] = RA;
11490       /* Fall through.  */
11491     case M_JAL_2:
11492     jal:
11493       if (mips_pic == NO_PIC)
11494 	{
11495 	  s = jals ? "jalrs" : "jalr";
11496 	  if (mips_opts.micromips
11497 	      && !mips_opts.insn32
11498 	      && op[0] == RA
11499 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11500 	    macro_build (NULL, s, "mj", op[1]);
11501 	  else
11502 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11503 	}
11504       else
11505 	{
11506 	  int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11507 			   && mips_cprestore_offset >= 0);
11508 
11509 	  if (op[1] != PIC_CALL_REG)
11510 	    as_warn (_("MIPS PIC call to register other than $25"));
11511 
11512 	  s = ((mips_opts.micromips
11513 		&& !mips_opts.insn32
11514 		&& (!mips_opts.noreorder || cprestore))
11515 	       ? "jalrs" : "jalr");
11516 	  if (mips_opts.micromips
11517 	      && !mips_opts.insn32
11518 	      && op[0] == RA
11519 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11520 	    macro_build (NULL, s, "mj", op[1]);
11521 	  else
11522 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11523 	  if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11524 	    {
11525 	      if (mips_cprestore_offset < 0)
11526 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11527 	      else
11528 		{
11529 		  if (!mips_frame_reg_valid)
11530 		    {
11531 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11532 		      /* Quiet this warning.  */
11533 		      mips_frame_reg_valid = 1;
11534 		    }
11535 		  if (!mips_cprestore_valid)
11536 		    {
11537 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11538 		      /* Quiet this warning.  */
11539 		      mips_cprestore_valid = 1;
11540 		    }
11541 		  if (mips_opts.noreorder)
11542 		    macro_build (NULL, "nop", "");
11543 		  expr1.X_add_number = mips_cprestore_offset;
11544 		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11545 						mips_gp_register,
11546 						mips_frame_reg,
11547 						HAVE_64BIT_ADDRESSES);
11548 		}
11549 	    }
11550 	}
11551 
11552       break;
11553 
11554     case M_JALS_A:
11555       gas_assert (mips_opts.micromips);
11556       if (mips_opts.insn32)
11557 	{
11558 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11559 	  break;
11560 	}
11561       jals = 1;
11562       /* Fall through.  */
11563     case M_JAL_A:
11564       if (mips_pic == NO_PIC)
11565 	macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11566       else if (mips_pic == SVR4_PIC)
11567 	{
11568 	  /* If this is a reference to an external symbol, and we are
11569 	     using a small GOT, we want
11570 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_CALL16)
11571 	       nop
11572 	       jalr	$ra,$25
11573 	       nop
11574 	       lw	$gp,cprestore($sp)
11575 	     The cprestore value is set using the .cprestore
11576 	     pseudo-op.  If we are using a big GOT, we want
11577 	       lui	$25,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11578 	       addu	$25,$25,$gp
11579 	       lw	$25,<sym>($25)		(BFD_RELOC_MIPS_CALL_LO16)
11580 	       nop
11581 	       jalr	$ra,$25
11582 	       nop
11583 	       lw	$gp,cprestore($sp)
11584 	     If the symbol is not external, we want
11585 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
11586 	       nop
11587 	       addiu	$25,$25,<sym>		(BFD_RELOC_LO16)
11588 	       jalr	$ra,$25
11589 	       nop
11590 	       lw $gp,cprestore($sp)
11591 
11592 	     For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11593 	     sequences above, minus nops, unless the symbol is local,
11594 	     which enables us to use GOT_PAGE/GOT_OFST (big got) or
11595 	     GOT_DISP.  */
11596 	  if (HAVE_NEWABI)
11597 	    {
11598 	      if (!mips_big_got)
11599 		{
11600 		  relax_start (offset_expr.X_add_symbol);
11601 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11602 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11603 			       mips_gp_register);
11604 		  relax_switch ();
11605 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11606 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11607 			       mips_gp_register);
11608 		  relax_end ();
11609 		}
11610 	      else
11611 		{
11612 		  relax_start (offset_expr.X_add_symbol);
11613 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11614 			       BFD_RELOC_MIPS_CALL_HI16);
11615 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11616 			       PIC_CALL_REG, mips_gp_register);
11617 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11618 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11619 			       PIC_CALL_REG);
11620 		  relax_switch ();
11621 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11622 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11623 			       mips_gp_register);
11624 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11625 			       PIC_CALL_REG, PIC_CALL_REG,
11626 			       BFD_RELOC_MIPS_GOT_OFST);
11627 		  relax_end ();
11628 		}
11629 
11630 	      macro_build_jalr (&offset_expr, 0);
11631 	    }
11632 	  else
11633 	    {
11634 	      relax_start (offset_expr.X_add_symbol);
11635 	      if (!mips_big_got)
11636 		{
11637 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11638 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11639 			       mips_gp_register);
11640 		  load_delay_nop ();
11641 		  relax_switch ();
11642 		}
11643 	      else
11644 		{
11645 		  int gpdelay;
11646 
11647 		  gpdelay = reg_needs_delay (mips_gp_register);
11648 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11649 			       BFD_RELOC_MIPS_CALL_HI16);
11650 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11651 			       PIC_CALL_REG, mips_gp_register);
11652 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11653 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11654 			       PIC_CALL_REG);
11655 		  load_delay_nop ();
11656 		  relax_switch ();
11657 		  if (gpdelay)
11658 		    macro_build (NULL, "nop", "");
11659 		}
11660 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11661 			   PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11662 			   mips_gp_register);
11663 	      load_delay_nop ();
11664 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11665 			   PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11666 	      relax_end ();
11667 	      macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11668 
11669 	      if (mips_cprestore_offset < 0)
11670 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11671 	      else
11672 		{
11673 		  if (!mips_frame_reg_valid)
11674 		    {
11675 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11676 		      /* Quiet this warning.  */
11677 		      mips_frame_reg_valid = 1;
11678 		    }
11679 		  if (!mips_cprestore_valid)
11680 		    {
11681 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11682 		      /* Quiet this warning.  */
11683 		      mips_cprestore_valid = 1;
11684 		    }
11685 		  if (mips_opts.noreorder)
11686 		    macro_build (NULL, "nop", "");
11687 		  expr1.X_add_number = mips_cprestore_offset;
11688 		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11689 						mips_gp_register,
11690 						mips_frame_reg,
11691 						HAVE_64BIT_ADDRESSES);
11692 		}
11693 	    }
11694 	}
11695       else if (mips_pic == VXWORKS_PIC)
11696 	as_bad (_("non-PIC jump used in PIC library"));
11697       else
11698 	abort ();
11699 
11700       break;
11701 
11702     case M_LBUE_AB:
11703       s = "lbue";
11704       fmt = "t,+j(b)";
11705       offbits = 9;
11706       goto ld_st;
11707     case M_LHUE_AB:
11708       s = "lhue";
11709       fmt = "t,+j(b)";
11710       offbits = 9;
11711       goto ld_st;
11712     case M_LBE_AB:
11713       s = "lbe";
11714       fmt = "t,+j(b)";
11715       offbits = 9;
11716       goto ld_st;
11717     case M_LHE_AB:
11718       s = "lhe";
11719       fmt = "t,+j(b)";
11720       offbits = 9;
11721       goto ld_st;
11722     case M_LLE_AB:
11723       s = "lle";
11724       fmt = "t,+j(b)";
11725       offbits = 9;
11726       goto ld_st;
11727     case M_LWE_AB:
11728       s = "lwe";
11729       fmt = "t,+j(b)";
11730       offbits = 9;
11731       goto ld_st;
11732     case M_LWLE_AB:
11733       s = "lwle";
11734       fmt = "t,+j(b)";
11735       offbits = 9;
11736       goto ld_st;
11737     case M_LWRE_AB:
11738       s = "lwre";
11739       fmt = "t,+j(b)";
11740       offbits = 9;
11741       goto ld_st;
11742     case M_SBE_AB:
11743       s = "sbe";
11744       fmt = "t,+j(b)";
11745       offbits = 9;
11746       goto ld_st;
11747     case M_SCE_AB:
11748       s = "sce";
11749       fmt = "t,+j(b)";
11750       offbits = 9;
11751       goto ld_st;
11752     case M_SHE_AB:
11753       s = "she";
11754       fmt = "t,+j(b)";
11755       offbits = 9;
11756       goto ld_st;
11757     case M_SWE_AB:
11758       s = "swe";
11759       fmt = "t,+j(b)";
11760       offbits = 9;
11761       goto ld_st;
11762     case M_SWLE_AB:
11763       s = "swle";
11764       fmt = "t,+j(b)";
11765       offbits = 9;
11766       goto ld_st;
11767     case M_SWRE_AB:
11768       s = "swre";
11769       fmt = "t,+j(b)";
11770       offbits = 9;
11771       goto ld_st;
11772     case M_ACLR_AB:
11773       s = "aclr";
11774       fmt = "\\,~(b)";
11775       offbits = 12;
11776       goto ld_st;
11777     case M_ASET_AB:
11778       s = "aset";
11779       fmt = "\\,~(b)";
11780       offbits = 12;
11781       goto ld_st;
11782     case M_LB_AB:
11783       s = "lb";
11784       fmt = "t,o(b)";
11785       goto ld;
11786     case M_LBU_AB:
11787       s = "lbu";
11788       fmt = "t,o(b)";
11789       goto ld;
11790     case M_LH_AB:
11791       s = "lh";
11792       fmt = "t,o(b)";
11793       goto ld;
11794     case M_LHU_AB:
11795       s = "lhu";
11796       fmt = "t,o(b)";
11797       goto ld;
11798     case M_LW_AB:
11799       s = "lw";
11800       fmt = "t,o(b)";
11801       goto ld;
11802     case M_LWC0_AB:
11803       gas_assert (!mips_opts.micromips);
11804       s = "lwc0";
11805       fmt = "E,o(b)";
11806       /* Itbl support may require additional care here.  */
11807       coproc = 1;
11808       goto ld_st;
11809     case M_LWC1_AB:
11810       s = "lwc1";
11811       fmt = "T,o(b)";
11812       /* Itbl support may require additional care here.  */
11813       coproc = 1;
11814       goto ld_st;
11815     case M_LWC2_AB:
11816       s = "lwc2";
11817       fmt = COP12_FMT;
11818       offbits = (mips_opts.micromips ? 12
11819 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11820 		 : 16);
11821       /* Itbl support may require additional care here.  */
11822       coproc = 1;
11823       goto ld_st;
11824     case M_LWC3_AB:
11825       gas_assert (!mips_opts.micromips);
11826       s = "lwc3";
11827       fmt = "E,o(b)";
11828       /* Itbl support may require additional care here.  */
11829       coproc = 1;
11830       goto ld_st;
11831     case M_LWL_AB:
11832       s = "lwl";
11833       fmt = MEM12_FMT;
11834       offbits = (mips_opts.micromips ? 12 : 16);
11835       goto ld_st;
11836     case M_LWR_AB:
11837       s = "lwr";
11838       fmt = MEM12_FMT;
11839       offbits = (mips_opts.micromips ? 12 : 16);
11840       goto ld_st;
11841     case M_LDC1_AB:
11842       s = "ldc1";
11843       fmt = "T,o(b)";
11844       /* Itbl support may require additional care here.  */
11845       coproc = 1;
11846       goto ld_st;
11847     case M_LDC2_AB:
11848       s = "ldc2";
11849       fmt = COP12_FMT;
11850       offbits = (mips_opts.micromips ? 12
11851 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11852 		 : 16);
11853       /* Itbl support may require additional care here.  */
11854       coproc = 1;
11855       goto ld_st;
11856     case M_LQC2_AB:
11857       s = "lqc2";
11858       fmt = "+7,o(b)";
11859       /* Itbl support may require additional care here.  */
11860       coproc = 1;
11861       goto ld_st;
11862     case M_LDC3_AB:
11863       s = "ldc3";
11864       fmt = "E,o(b)";
11865       /* Itbl support may require additional care here.  */
11866       coproc = 1;
11867       goto ld_st;
11868     case M_LDL_AB:
11869       s = "ldl";
11870       fmt = MEM12_FMT;
11871       offbits = (mips_opts.micromips ? 12 : 16);
11872       goto ld_st;
11873     case M_LDR_AB:
11874       s = "ldr";
11875       fmt = MEM12_FMT;
11876       offbits = (mips_opts.micromips ? 12 : 16);
11877       goto ld_st;
11878     case M_LL_AB:
11879       s = "ll";
11880       fmt = LL_SC_FMT;
11881       offbits = (mips_opts.micromips ? 12
11882 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11883 		 : 16);
11884       goto ld;
11885     case M_LLD_AB:
11886       s = "lld";
11887       fmt = LL_SC_FMT;
11888       offbits = (mips_opts.micromips ? 12
11889 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11890 		 : 16);
11891       goto ld;
11892     case M_LWU_AB:
11893       s = "lwu";
11894       fmt = MEM12_FMT;
11895       offbits = (mips_opts.micromips ? 12 : 16);
11896       goto ld;
11897     case M_LWP_AB:
11898       gas_assert (mips_opts.micromips);
11899       s = "lwp";
11900       fmt = "t,~(b)";
11901       offbits = 12;
11902       lp = 1;
11903       goto ld;
11904     case M_LDP_AB:
11905       gas_assert (mips_opts.micromips);
11906       s = "ldp";
11907       fmt = "t,~(b)";
11908       offbits = 12;
11909       lp = 1;
11910       goto ld;
11911     case M_LWM_AB:
11912       gas_assert (mips_opts.micromips);
11913       s = "lwm";
11914       fmt = "n,~(b)";
11915       offbits = 12;
11916       goto ld_st;
11917     case M_LDM_AB:
11918       gas_assert (mips_opts.micromips);
11919       s = "ldm";
11920       fmt = "n,~(b)";
11921       offbits = 12;
11922       goto ld_st;
11923 
11924     ld:
11925       /* We don't want to use $0 as tempreg.  */
11926       if (op[2] == op[0] + lp || op[0] + lp == ZERO)
11927 	goto ld_st;
11928       else
11929 	tempreg = op[0] + lp;
11930       goto ld_noat;
11931 
11932     case M_SB_AB:
11933       s = "sb";
11934       fmt = "t,o(b)";
11935       goto ld_st;
11936     case M_SH_AB:
11937       s = "sh";
11938       fmt = "t,o(b)";
11939       goto ld_st;
11940     case M_SW_AB:
11941       s = "sw";
11942       fmt = "t,o(b)";
11943       goto ld_st;
11944     case M_SWC0_AB:
11945       gas_assert (!mips_opts.micromips);
11946       s = "swc0";
11947       fmt = "E,o(b)";
11948       /* Itbl support may require additional care here.  */
11949       coproc = 1;
11950       goto ld_st;
11951     case M_SWC1_AB:
11952       s = "swc1";
11953       fmt = "T,o(b)";
11954       /* Itbl support may require additional care here.  */
11955       coproc = 1;
11956       goto ld_st;
11957     case M_SWC2_AB:
11958       s = "swc2";
11959       fmt = COP12_FMT;
11960       offbits = (mips_opts.micromips ? 12
11961 		 : ISA_IS_R6 (mips_opts.isa) ? 11
11962 		 : 16);
11963       /* Itbl support may require additional care here.  */
11964       coproc = 1;
11965       goto ld_st;
11966     case M_SWC3_AB:
11967       gas_assert (!mips_opts.micromips);
11968       s = "swc3";
11969       fmt = "E,o(b)";
11970       /* Itbl support may require additional care here.  */
11971       coproc = 1;
11972       goto ld_st;
11973     case M_SWL_AB:
11974       s = "swl";
11975       fmt = MEM12_FMT;
11976       offbits = (mips_opts.micromips ? 12 : 16);
11977       goto ld_st;
11978     case M_SWR_AB:
11979       s = "swr";
11980       fmt = MEM12_FMT;
11981       offbits = (mips_opts.micromips ? 12 : 16);
11982       goto ld_st;
11983     case M_SC_AB:
11984       s = "sc";
11985       fmt = LL_SC_FMT;
11986       offbits = (mips_opts.micromips ? 12
11987 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11988 		 : 16);
11989       goto ld_st;
11990     case M_SCD_AB:
11991       s = "scd";
11992       fmt = LL_SC_FMT;
11993       offbits = (mips_opts.micromips ? 12
11994 		 : ISA_IS_R6 (mips_opts.isa) ? 9
11995 		 : 16);
11996       goto ld_st;
11997     case M_CACHE_AB:
11998       s = "cache";
11999       fmt = (mips_opts.micromips ? "k,~(b)"
12000 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12001 	     : "k,o(b)");
12002       offbits = (mips_opts.micromips ? 12
12003 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12004 		 : 16);
12005       goto ld_st;
12006     case M_CACHEE_AB:
12007       s = "cachee";
12008       fmt = "k,+j(b)";
12009       offbits = 9;
12010       goto ld_st;
12011     case M_PREF_AB:
12012       s = "pref";
12013       fmt = (mips_opts.micromips ? "k,~(b)"
12014 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12015 	     : "k,o(b)");
12016       offbits = (mips_opts.micromips ? 12
12017 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12018 		 : 16);
12019       goto ld_st;
12020     case M_PREFE_AB:
12021       s = "prefe";
12022       fmt = "k,+j(b)";
12023       offbits = 9;
12024       goto ld_st;
12025     case M_SDC1_AB:
12026       s = "sdc1";
12027       fmt = "T,o(b)";
12028       coproc = 1;
12029       /* Itbl support may require additional care here.  */
12030       goto ld_st;
12031     case M_SDC2_AB:
12032       s = "sdc2";
12033       fmt = COP12_FMT;
12034       offbits = (mips_opts.micromips ? 12
12035 		 : ISA_IS_R6 (mips_opts.isa) ? 11
12036 		 : 16);
12037       /* Itbl support may require additional care here.  */
12038       coproc = 1;
12039       goto ld_st;
12040     case M_SQC2_AB:
12041       s = "sqc2";
12042       fmt = "+7,o(b)";
12043       /* Itbl support may require additional care here.  */
12044       coproc = 1;
12045       goto ld_st;
12046     case M_SDC3_AB:
12047       gas_assert (!mips_opts.micromips);
12048       s = "sdc3";
12049       fmt = "E,o(b)";
12050       /* Itbl support may require additional care here.  */
12051       coproc = 1;
12052       goto ld_st;
12053     case M_SDL_AB:
12054       s = "sdl";
12055       fmt = MEM12_FMT;
12056       offbits = (mips_opts.micromips ? 12 : 16);
12057       goto ld_st;
12058     case M_SDR_AB:
12059       s = "sdr";
12060       fmt = MEM12_FMT;
12061       offbits = (mips_opts.micromips ? 12 : 16);
12062       goto ld_st;
12063     case M_SWP_AB:
12064       gas_assert (mips_opts.micromips);
12065       s = "swp";
12066       fmt = "t,~(b)";
12067       offbits = 12;
12068       goto ld_st;
12069     case M_SDP_AB:
12070       gas_assert (mips_opts.micromips);
12071       s = "sdp";
12072       fmt = "t,~(b)";
12073       offbits = 12;
12074       goto ld_st;
12075     case M_SWM_AB:
12076       gas_assert (mips_opts.micromips);
12077       s = "swm";
12078       fmt = "n,~(b)";
12079       offbits = 12;
12080       goto ld_st;
12081     case M_SDM_AB:
12082       gas_assert (mips_opts.micromips);
12083       s = "sdm";
12084       fmt = "n,~(b)";
12085       offbits = 12;
12086 
12087     ld_st:
12088       tempreg = AT;
12089     ld_noat:
12090       breg = op[2];
12091       if (small_offset_p (0, align, 16))
12092 	{
12093 	  /* The first case exists for M_LD_AB and M_SD_AB, which are
12094 	     macros for o32 but which should act like normal instructions
12095 	     otherwise.  */
12096 	  if (offbits == 16)
12097 	    macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
12098 			 offset_reloc[1], offset_reloc[2], breg);
12099 	  else if (small_offset_p (0, align, offbits))
12100 	    {
12101 	      if (offbits == 0)
12102 		macro_build (NULL, s, fmt, op[0], breg);
12103 	      else
12104 		macro_build (NULL, s, fmt, op[0],
12105 			     (int) offset_expr.X_add_number, breg);
12106 	    }
12107 	  else
12108 	    {
12109 	      if (tempreg == AT)
12110 		used_at = 1;
12111 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12112 			   tempreg, breg, -1, offset_reloc[0],
12113 			   offset_reloc[1], offset_reloc[2]);
12114 	      if (offbits == 0)
12115 		macro_build (NULL, s, fmt, op[0], tempreg);
12116 	      else
12117 		macro_build (NULL, s, fmt, op[0], 0, tempreg);
12118 	    }
12119 	  break;
12120 	}
12121 
12122       if (tempreg == AT)
12123 	used_at = 1;
12124 
12125       if (offset_expr.X_op != O_constant
12126 	  && offset_expr.X_op != O_symbol)
12127 	{
12128 	  as_bad (_("expression too complex"));
12129 	  offset_expr.X_op = O_constant;
12130 	}
12131 
12132       if (HAVE_32BIT_ADDRESSES
12133 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12134 	{
12135 	  char value [32];
12136 
12137 	  sprintf_vma (value, offset_expr.X_add_number);
12138 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
12139 	}
12140 
12141       /* A constant expression in PIC code can be handled just as it
12142 	 is in non PIC code.  */
12143       if (offset_expr.X_op == O_constant)
12144 	{
12145 	  expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12146 						 offbits == 0 ? 16 : offbits);
12147 	  offset_expr.X_add_number -= expr1.X_add_number;
12148 
12149 	  load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12150 	  if (breg != 0)
12151 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12152 			 tempreg, tempreg, breg);
12153 	  if (offbits == 0)
12154 	    {
12155 	      if (offset_expr.X_add_number != 0)
12156 		macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12157 			     "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12158 	      macro_build (NULL, s, fmt, op[0], tempreg);
12159 	    }
12160 	  else if (offbits == 16)
12161 	    macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12162 	  else
12163 	    macro_build (NULL, s, fmt, op[0],
12164 			 (int) offset_expr.X_add_number, tempreg);
12165 	}
12166       else if (offbits != 16)
12167 	{
12168 	  /* The offset field is too narrow to be used for a low-part
12169 	     relocation, so load the whole address into the auxiliary
12170 	     register.  */
12171 	  load_address (tempreg, &offset_expr, &used_at);
12172 	  if (breg != 0)
12173 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12174 			 tempreg, tempreg, breg);
12175 	  if (offbits == 0)
12176 	    macro_build (NULL, s, fmt, op[0], tempreg);
12177 	  else
12178 	    macro_build (NULL, s, fmt, op[0], 0, tempreg);
12179 	}
12180       else if (mips_pic == NO_PIC)
12181 	{
12182 	  /* If this is a reference to a GP relative symbol, and there
12183 	     is no base register, we want
12184 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
12185 	     Otherwise, if there is no base register, we want
12186 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12187 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12188 	     If we have a constant, we need two instructions anyhow,
12189 	     so we always use the latter form.
12190 
12191 	     If we have a base register, and this is a reference to a
12192 	     GP relative symbol, we want
12193 	       addu	$tempreg,$breg,$gp
12194 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_GPREL16)
12195 	     Otherwise we want
12196 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12197 	       addu	$tempreg,$tempreg,$breg
12198 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12199 	     With a constant we always use the latter case.
12200 
12201 	     With 64bit address space and no base register and $at usable,
12202 	     we want
12203 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12204 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12205 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12206 	       dsll32	$tempreg,0
12207 	       daddu	$tempreg,$at
12208 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12209 	     If we have a base register, we want
12210 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12211 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12212 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12213 	       daddu	$at,$breg
12214 	       dsll32	$tempreg,0
12215 	       daddu	$tempreg,$at
12216 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12217 
12218 	     Without $at we can't generate the optimal path for superscalar
12219 	     processors here since this would require two temporary registers.
12220 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12221 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12222 	       dsll	$tempreg,16
12223 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12224 	       dsll	$tempreg,16
12225 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12226 	     If we have a base register, we want
12227 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12228 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12229 	       dsll	$tempreg,16
12230 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12231 	       dsll	$tempreg,16
12232 	       daddu	$tempreg,$tempreg,$breg
12233 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12234 
12235 	     For GP relative symbols in 64bit address space we can use
12236 	     the same sequence as in 32bit address space.  */
12237 	  if (HAVE_64BIT_SYMBOLS)
12238 	    {
12239 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12240 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12241 		{
12242 		  relax_start (offset_expr.X_add_symbol);
12243 		  if (breg == 0)
12244 		    {
12245 		      macro_build (&offset_expr, s, fmt, op[0],
12246 				   BFD_RELOC_GPREL16, mips_gp_register);
12247 		    }
12248 		  else
12249 		    {
12250 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12251 				   tempreg, breg, mips_gp_register);
12252 		      macro_build (&offset_expr, s, fmt, op[0],
12253 				   BFD_RELOC_GPREL16, tempreg);
12254 		    }
12255 		  relax_switch ();
12256 		}
12257 
12258 	      if (used_at == 0 && mips_opts.at)
12259 		{
12260 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12261 			       BFD_RELOC_MIPS_HIGHEST);
12262 		  macro_build (&offset_expr, "lui", LUI_FMT, AT,
12263 			       BFD_RELOC_HI16_S);
12264 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12265 			       tempreg, BFD_RELOC_MIPS_HIGHER);
12266 		  if (breg != 0)
12267 		    macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12268 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12269 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12270 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12271 			       tempreg);
12272 		  used_at = 1;
12273 		}
12274 	      else
12275 		{
12276 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12277 			       BFD_RELOC_MIPS_HIGHEST);
12278 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12279 			       tempreg, BFD_RELOC_MIPS_HIGHER);
12280 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12281 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12282 			       tempreg, BFD_RELOC_HI16_S);
12283 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12284 		  if (breg != 0)
12285 		    macro_build (NULL, "daddu", "d,v,t",
12286 				 tempreg, tempreg, breg);
12287 		  macro_build (&offset_expr, s, fmt, op[0],
12288 			       BFD_RELOC_LO16, tempreg);
12289 		}
12290 
12291 	      if (mips_relax.sequence)
12292 		relax_end ();
12293 	      break;
12294 	    }
12295 
12296 	  if (breg == 0)
12297 	    {
12298 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12299 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12300 		{
12301 		  relax_start (offset_expr.X_add_symbol);
12302 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12303 			       mips_gp_register);
12304 		  relax_switch ();
12305 		}
12306 	      macro_build_lui (&offset_expr, tempreg);
12307 	      macro_build (&offset_expr, s, fmt, op[0],
12308 			   BFD_RELOC_LO16, tempreg);
12309 	      if (mips_relax.sequence)
12310 		relax_end ();
12311 	    }
12312 	  else
12313 	    {
12314 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12315 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12316 		{
12317 		  relax_start (offset_expr.X_add_symbol);
12318 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12319 			       tempreg, breg, mips_gp_register);
12320 		  macro_build (&offset_expr, s, fmt, op[0],
12321 			       BFD_RELOC_GPREL16, tempreg);
12322 		  relax_switch ();
12323 		}
12324 	      macro_build_lui (&offset_expr, tempreg);
12325 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12326 			   tempreg, tempreg, breg);
12327 	      macro_build (&offset_expr, s, fmt, op[0],
12328 			   BFD_RELOC_LO16, tempreg);
12329 	      if (mips_relax.sequence)
12330 		relax_end ();
12331 	    }
12332 	}
12333       else if (!mips_big_got)
12334 	{
12335 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12336 
12337 	  /* If this is a reference to an external symbol, we want
12338 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12339 	       nop
12340 	       <op>	op[0],0($tempreg)
12341 	     Otherwise we want
12342 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12343 	       nop
12344 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
12345 	       <op>	op[0],0($tempreg)
12346 
12347 	     For NewABI, we want
12348 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
12349 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
12350 
12351 	     If there is a base register, we add it to $tempreg before
12352 	     the <op>.  If there is a constant, we stick it in the
12353 	     <op> instruction.  We don't handle constants larger than
12354 	     16 bits, because we have no way to load the upper 16 bits
12355 	     (actually, we could handle them for the subset of cases
12356 	     in which we are not using $at).  */
12357 	  gas_assert (offset_expr.X_op == O_symbol);
12358 	  if (HAVE_NEWABI)
12359 	    {
12360 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12361 			   BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12362 	      if (breg != 0)
12363 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12364 			     tempreg, tempreg, breg);
12365 	      macro_build (&offset_expr, s, fmt, op[0],
12366 			   BFD_RELOC_MIPS_GOT_OFST, tempreg);
12367 	      break;
12368 	    }
12369 	  expr1.X_add_number = offset_expr.X_add_number;
12370 	  offset_expr.X_add_number = 0;
12371 	  if (expr1.X_add_number < -0x8000
12372 	      || expr1.X_add_number >= 0x8000)
12373 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12374 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12375 		       lw_reloc_type, mips_gp_register);
12376 	  load_delay_nop ();
12377 	  relax_start (offset_expr.X_add_symbol);
12378 	  relax_switch ();
12379 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12380 		       tempreg, BFD_RELOC_LO16);
12381 	  relax_end ();
12382 	  if (breg != 0)
12383 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12384 			 tempreg, tempreg, breg);
12385 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12386 	}
12387       else if (mips_big_got && !HAVE_NEWABI)
12388 	{
12389 	  int gpdelay;
12390 
12391 	  /* If this is a reference to an external symbol, we want
12392 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12393 	       addu	$tempreg,$tempreg,$gp
12394 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12395 	       <op>	op[0],0($tempreg)
12396 	     Otherwise we want
12397 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12398 	       nop
12399 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
12400 	       <op>	op[0],0($tempreg)
12401 	     If there is a base register, we add it to $tempreg before
12402 	     the <op>.  If there is a constant, we stick it in the
12403 	     <op> instruction.  We don't handle constants larger than
12404 	     16 bits, because we have no way to load the upper 16 bits
12405 	     (actually, we could handle them for the subset of cases
12406 	     in which we are not using $at).  */
12407 	  gas_assert (offset_expr.X_op == O_symbol);
12408 	  expr1.X_add_number = offset_expr.X_add_number;
12409 	  offset_expr.X_add_number = 0;
12410 	  if (expr1.X_add_number < -0x8000
12411 	      || expr1.X_add_number >= 0x8000)
12412 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12413 	  gpdelay = reg_needs_delay (mips_gp_register);
12414 	  relax_start (offset_expr.X_add_symbol);
12415 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12416 		       BFD_RELOC_MIPS_GOT_HI16);
12417 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12418 		       mips_gp_register);
12419 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12420 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12421 	  relax_switch ();
12422 	  if (gpdelay)
12423 	    macro_build (NULL, "nop", "");
12424 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12425 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12426 	  load_delay_nop ();
12427 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12428 		       tempreg, BFD_RELOC_LO16);
12429 	  relax_end ();
12430 
12431 	  if (breg != 0)
12432 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12433 			 tempreg, tempreg, breg);
12434 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12435 	}
12436       else if (mips_big_got && HAVE_NEWABI)
12437 	{
12438 	  /* If this is a reference to an external symbol, we want
12439 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12440 	       add	$tempreg,$tempreg,$gp
12441 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12442 	       <op>	op[0],<ofst>($tempreg)
12443 	     Otherwise, for local symbols, we want:
12444 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
12445 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
12446 	  gas_assert (offset_expr.X_op == O_symbol);
12447 	  expr1.X_add_number = offset_expr.X_add_number;
12448 	  offset_expr.X_add_number = 0;
12449 	  if (expr1.X_add_number < -0x8000
12450 	      || expr1.X_add_number >= 0x8000)
12451 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12452 	  relax_start (offset_expr.X_add_symbol);
12453 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12454 		       BFD_RELOC_MIPS_GOT_HI16);
12455 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12456 		       mips_gp_register);
12457 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12458 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12459 	  if (breg != 0)
12460 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12461 			 tempreg, tempreg, breg);
12462 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12463 
12464 	  relax_switch ();
12465 	  offset_expr.X_add_number = expr1.X_add_number;
12466 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12467 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12468 	  if (breg != 0)
12469 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12470 			 tempreg, tempreg, breg);
12471 	  macro_build (&offset_expr, s, fmt, op[0],
12472 		       BFD_RELOC_MIPS_GOT_OFST, tempreg);
12473 	  relax_end ();
12474 	}
12475       else
12476 	abort ();
12477 
12478       break;
12479 
12480     case M_JRADDIUSP:
12481       gas_assert (mips_opts.micromips);
12482       gas_assert (mips_opts.insn32);
12483       start_noreorder ();
12484       macro_build (NULL, "jr", "s", RA);
12485       expr1.X_add_number = op[0] << 2;
12486       macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12487       end_noreorder ();
12488       break;
12489 
12490     case M_JRC:
12491       gas_assert (mips_opts.micromips);
12492       gas_assert (mips_opts.insn32);
12493       macro_build (NULL, "jr", "s", op[0]);
12494       if (mips_opts.noreorder)
12495 	macro_build (NULL, "nop", "");
12496       break;
12497 
12498     case M_LI:
12499     case M_LI_S:
12500       load_register (op[0], &imm_expr, 0);
12501       break;
12502 
12503     case M_DLI:
12504       load_register (op[0], &imm_expr, 1);
12505       break;
12506 
12507     case M_LI_SS:
12508       if (imm_expr.X_op == O_constant)
12509 	{
12510 	  used_at = 1;
12511 	  load_register (AT, &imm_expr, 0);
12512 	  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12513 	  break;
12514 	}
12515       else
12516 	{
12517 	  gas_assert (imm_expr.X_op == O_absent
12518 		      && offset_expr.X_op == O_symbol
12519 		      && strcmp (segment_name (S_GET_SEGMENT
12520 					       (offset_expr.X_add_symbol)),
12521 				 ".lit4") == 0
12522 		      && offset_expr.X_add_number == 0);
12523 	  macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12524 		       BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12525 	  break;
12526 	}
12527 
12528     case M_LI_D:
12529       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
12530          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
12531          order 32 bits of the value and the low order 32 bits are either
12532          zero or in OFFSET_EXPR.  */
12533       if (imm_expr.X_op == O_constant)
12534 	{
12535 	  if (GPR_SIZE == 64)
12536 	    load_register (op[0], &imm_expr, 1);
12537 	  else
12538 	    {
12539 	      int hreg, lreg;
12540 
12541 	      if (target_big_endian)
12542 		{
12543 		  hreg = op[0];
12544 		  lreg = op[0] + 1;
12545 		}
12546 	      else
12547 		{
12548 		  hreg = op[0] + 1;
12549 		  lreg = op[0];
12550 		}
12551 
12552 	      if (hreg <= 31)
12553 		load_register (hreg, &imm_expr, 0);
12554 	      if (lreg <= 31)
12555 		{
12556 		  if (offset_expr.X_op == O_absent)
12557 		    move_register (lreg, 0);
12558 		  else
12559 		    {
12560 		      gas_assert (offset_expr.X_op == O_constant);
12561 		      load_register (lreg, &offset_expr, 0);
12562 		    }
12563 		}
12564 	    }
12565 	  break;
12566 	}
12567       gas_assert (imm_expr.X_op == O_absent);
12568 
12569       /* We know that sym is in the .rdata section.  First we get the
12570 	 upper 16 bits of the address.  */
12571       if (mips_pic == NO_PIC)
12572 	{
12573 	  macro_build_lui (&offset_expr, AT);
12574 	  used_at = 1;
12575 	}
12576       else
12577 	{
12578 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12579 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12580 	  used_at = 1;
12581 	}
12582 
12583       /* Now we load the register(s).  */
12584       if (GPR_SIZE == 64)
12585 	{
12586 	  used_at = 1;
12587 	  macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12588 		       BFD_RELOC_LO16, AT);
12589 	}
12590       else
12591 	{
12592 	  used_at = 1;
12593 	  macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12594 		       BFD_RELOC_LO16, AT);
12595 	  if (op[0] != RA)
12596 	    {
12597 	      /* FIXME: How in the world do we deal with the possible
12598 		 overflow here?  */
12599 	      offset_expr.X_add_number += 4;
12600 	      macro_build (&offset_expr, "lw", "t,o(b)",
12601 			   op[0] + 1, BFD_RELOC_LO16, AT);
12602 	    }
12603 	}
12604       break;
12605 
12606     case M_LI_DD:
12607       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
12608          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12609          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
12610          the value and the low order 32 bits are either zero or in
12611          OFFSET_EXPR.  */
12612       if (imm_expr.X_op == O_constant)
12613 	{
12614 	  used_at = 1;
12615 	  load_register (AT, &imm_expr, FPR_SIZE == 64);
12616 	  if (FPR_SIZE == 64 && GPR_SIZE == 64)
12617 	    macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
12618 	  else
12619 	    {
12620 	      if (ISA_HAS_MXHC1 (mips_opts.isa))
12621 	        macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12622 	      else if (FPR_SIZE != 32)
12623 		as_bad (_("Unable to generate `%s' compliant code "
12624 			  "without mthc1"),
12625 			(FPR_SIZE == 64) ? "fp64" : "fpxx");
12626 	      else
12627 		macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
12628 	      if (offset_expr.X_op == O_absent)
12629 		macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12630 	      else
12631 		{
12632 		  gas_assert (offset_expr.X_op == O_constant);
12633 		  load_register (AT, &offset_expr, 0);
12634 		  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12635 		}
12636 	    }
12637 	  break;
12638 	}
12639 
12640       gas_assert (imm_expr.X_op == O_absent
12641 		  && offset_expr.X_op == O_symbol
12642 		  && offset_expr.X_add_number == 0);
12643       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12644       if (strcmp (s, ".lit8") == 0)
12645 	{
12646 	  op[2] = mips_gp_register;
12647 	  offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12648 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12649 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12650 	}
12651       else
12652 	{
12653 	  gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12654 	  used_at = 1;
12655 	  if (mips_pic != NO_PIC)
12656 	    macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12657 			 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12658 	  else
12659 	    {
12660 	      /* FIXME: This won't work for a 64 bit address.  */
12661 	      macro_build_lui (&offset_expr, AT);
12662 	    }
12663 
12664 	  op[2] = AT;
12665 	  offset_reloc[0] = BFD_RELOC_LO16;
12666 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12667 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12668 	}
12669       align = 8;
12670       /* Fall through */
12671 
12672     case M_L_DAB:
12673       /*
12674        * The MIPS assembler seems to check for X_add_number not
12675        * being double aligned and generating:
12676        *	lui	at,%hi(foo+1)
12677        *	addu	at,at,v1
12678        *	addiu	at,at,%lo(foo+1)
12679        *	lwc1	f2,0(at)
12680        *	lwc1	f3,4(at)
12681        * But, the resulting address is the same after relocation so why
12682        * generate the extra instruction?
12683        */
12684       /* Itbl support may require additional care here.  */
12685       coproc = 1;
12686       fmt = "T,o(b)";
12687       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12688 	{
12689 	  s = "ldc1";
12690 	  goto ld_st;
12691 	}
12692       s = "lwc1";
12693       goto ldd_std;
12694 
12695     case M_S_DAB:
12696       gas_assert (!mips_opts.micromips);
12697       /* Itbl support may require additional care here.  */
12698       coproc = 1;
12699       fmt = "T,o(b)";
12700       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
12701 	{
12702 	  s = "sdc1";
12703 	  goto ld_st;
12704 	}
12705       s = "swc1";
12706       goto ldd_std;
12707 
12708     case M_LQ_AB:
12709       fmt = "t,o(b)";
12710       s = "lq";
12711       goto ld;
12712 
12713     case M_SQ_AB:
12714       fmt = "t,o(b)";
12715       s = "sq";
12716       goto ld_st;
12717 
12718     case M_LD_AB:
12719       fmt = "t,o(b)";
12720       if (GPR_SIZE == 64)
12721 	{
12722 	  s = "ld";
12723 	  goto ld;
12724 	}
12725       s = "lw";
12726       goto ldd_std;
12727 
12728     case M_SD_AB:
12729       fmt = "t,o(b)";
12730       if (GPR_SIZE == 64)
12731 	{
12732 	  s = "sd";
12733 	  goto ld_st;
12734 	}
12735       s = "sw";
12736 
12737     ldd_std:
12738       /* Even on a big endian machine $fn comes before $fn+1.  We have
12739 	 to adjust when loading from memory.  We set coproc if we must
12740 	 load $fn+1 first.  */
12741       /* Itbl support may require additional care here.  */
12742       if (!target_big_endian)
12743 	coproc = 0;
12744 
12745       breg = op[2];
12746       if (small_offset_p (0, align, 16))
12747 	{
12748 	  ep = &offset_expr;
12749 	  if (!small_offset_p (4, align, 16))
12750 	    {
12751 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12752 			   -1, offset_reloc[0], offset_reloc[1],
12753 			   offset_reloc[2]);
12754 	      expr1.X_add_number = 0;
12755 	      ep = &expr1;
12756 	      breg = AT;
12757 	      used_at = 1;
12758 	      offset_reloc[0] = BFD_RELOC_LO16;
12759 	      offset_reloc[1] = BFD_RELOC_UNUSED;
12760 	      offset_reloc[2] = BFD_RELOC_UNUSED;
12761 	    }
12762 	  if (strcmp (s, "lw") == 0 && op[0] == breg)
12763 	    {
12764 	      ep->X_add_number += 4;
12765 	      macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
12766 			   offset_reloc[1], offset_reloc[2], breg);
12767 	      ep->X_add_number -= 4;
12768 	      macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
12769 			   offset_reloc[1], offset_reloc[2], breg);
12770 	    }
12771 	  else
12772 	    {
12773 	      macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
12774 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
12775 			   breg);
12776 	      ep->X_add_number += 4;
12777 	      macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
12778 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
12779 			   breg);
12780 	    }
12781 	  break;
12782 	}
12783 
12784       if (offset_expr.X_op != O_symbol
12785 	  && offset_expr.X_op != O_constant)
12786 	{
12787 	  as_bad (_("expression too complex"));
12788 	  offset_expr.X_op = O_constant;
12789 	}
12790 
12791       if (HAVE_32BIT_ADDRESSES
12792 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12793 	{
12794 	  char value [32];
12795 
12796 	  sprintf_vma (value, offset_expr.X_add_number);
12797 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
12798 	}
12799 
12800       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
12801 	{
12802 	  /* If this is a reference to a GP relative symbol, we want
12803 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
12804 	       <op>	op[0]+1,<sym>+4($gp)	(BFD_RELOC_GPREL16)
12805 	     If we have a base register, we use this
12806 	       addu	$at,$breg,$gp
12807 	       <op>	op[0],<sym>($at)	(BFD_RELOC_GPREL16)
12808 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_GPREL16)
12809 	     If this is not a GP relative symbol, we want
12810 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12811 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
12812 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
12813 	     If there is a base register, we add it to $at after the
12814 	     lui instruction.  If there is a constant, we always use
12815 	     the last case.  */
12816 	  if (offset_expr.X_op == O_symbol
12817 	      && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12818 	      && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12819 	    {
12820 	      relax_start (offset_expr.X_add_symbol);
12821 	      if (breg == 0)
12822 		{
12823 		  tempreg = mips_gp_register;
12824 		}
12825 	      else
12826 		{
12827 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12828 			       AT, breg, mips_gp_register);
12829 		  tempreg = AT;
12830 		  used_at = 1;
12831 		}
12832 
12833 	      /* Itbl support may require additional care here.  */
12834 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12835 			   BFD_RELOC_GPREL16, tempreg);
12836 	      offset_expr.X_add_number += 4;
12837 
12838 	      /* Set mips_optimize to 2 to avoid inserting an
12839                  undesired nop.  */
12840 	      hold_mips_optimize = mips_optimize;
12841 	      mips_optimize = 2;
12842 	      /* Itbl support may require additional care here.  */
12843 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12844 			   BFD_RELOC_GPREL16, tempreg);
12845 	      mips_optimize = hold_mips_optimize;
12846 
12847 	      relax_switch ();
12848 
12849 	      offset_expr.X_add_number -= 4;
12850 	    }
12851 	  used_at = 1;
12852 	  if (offset_high_part (offset_expr.X_add_number, 16)
12853 	      != offset_high_part (offset_expr.X_add_number + 4, 16))
12854 	    {
12855 	      load_address (AT, &offset_expr, &used_at);
12856 	      offset_expr.X_op = O_constant;
12857 	      offset_expr.X_add_number = 0;
12858 	    }
12859 	  else
12860 	    macro_build_lui (&offset_expr, AT);
12861 	  if (breg != 0)
12862 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12863 	  /* Itbl support may require additional care here.  */
12864 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12865 		       BFD_RELOC_LO16, AT);
12866 	  /* FIXME: How do we handle overflow here?  */
12867 	  offset_expr.X_add_number += 4;
12868 	  /* Itbl support may require additional care here.  */
12869 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12870 		       BFD_RELOC_LO16, AT);
12871 	  if (mips_relax.sequence)
12872 	    relax_end ();
12873 	}
12874       else if (!mips_big_got)
12875 	{
12876 	  /* If this is a reference to an external symbol, we want
12877 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
12878 	       nop
12879 	       <op>	op[0],0($at)
12880 	       <op>	op[0]+1,4($at)
12881 	     Otherwise we want
12882 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
12883 	       nop
12884 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
12885 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
12886 	     If there is a base register we add it to $at before the
12887 	     lwc1 instructions.  If there is a constant we include it
12888 	     in the lwc1 instructions.  */
12889 	  used_at = 1;
12890 	  expr1.X_add_number = offset_expr.X_add_number;
12891 	  if (expr1.X_add_number < -0x8000
12892 	      || expr1.X_add_number >= 0x8000 - 4)
12893 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12894 	  load_got_offset (AT, &offset_expr);
12895 	  load_delay_nop ();
12896 	  if (breg != 0)
12897 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12898 
12899 	  /* Set mips_optimize to 2 to avoid inserting an undesired
12900              nop.  */
12901 	  hold_mips_optimize = mips_optimize;
12902 	  mips_optimize = 2;
12903 
12904 	  /* Itbl support may require additional care here.  */
12905 	  relax_start (offset_expr.X_add_symbol);
12906 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12907 		       BFD_RELOC_LO16, AT);
12908 	  expr1.X_add_number += 4;
12909 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12910 		       BFD_RELOC_LO16, AT);
12911 	  relax_switch ();
12912 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12913 		       BFD_RELOC_LO16, AT);
12914 	  offset_expr.X_add_number += 4;
12915 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12916 		       BFD_RELOC_LO16, AT);
12917 	  relax_end ();
12918 
12919 	  mips_optimize = hold_mips_optimize;
12920 	}
12921       else if (mips_big_got)
12922 	{
12923 	  int gpdelay;
12924 
12925 	  /* If this is a reference to an external symbol, we want
12926 	       lui	$at,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12927 	       addu	$at,$at,$gp
12928 	       lw	$at,<sym>($at)		(BFD_RELOC_MIPS_GOT_LO16)
12929 	       nop
12930 	       <op>	op[0],0($at)
12931 	       <op>	op[0]+1,4($at)
12932 	     Otherwise we want
12933 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
12934 	       nop
12935 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
12936 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
12937 	     If there is a base register we add it to $at before the
12938 	     lwc1 instructions.  If there is a constant we include it
12939 	     in the lwc1 instructions.  */
12940 	  used_at = 1;
12941 	  expr1.X_add_number = offset_expr.X_add_number;
12942 	  offset_expr.X_add_number = 0;
12943 	  if (expr1.X_add_number < -0x8000
12944 	      || expr1.X_add_number >= 0x8000 - 4)
12945 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12946 	  gpdelay = reg_needs_delay (mips_gp_register);
12947 	  relax_start (offset_expr.X_add_symbol);
12948 	  macro_build (&offset_expr, "lui", LUI_FMT,
12949 		       AT, BFD_RELOC_MIPS_GOT_HI16);
12950 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12951 		       AT, AT, mips_gp_register);
12952 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
12953 		       AT, BFD_RELOC_MIPS_GOT_LO16, AT);
12954 	  load_delay_nop ();
12955 	  if (breg != 0)
12956 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12957 	  /* Itbl support may require additional care here.  */
12958 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
12959 		       BFD_RELOC_LO16, AT);
12960 	  expr1.X_add_number += 4;
12961 
12962 	  /* Set mips_optimize to 2 to avoid inserting an undesired
12963              nop.  */
12964 	  hold_mips_optimize = mips_optimize;
12965 	  mips_optimize = 2;
12966 	  /* Itbl support may require additional care here.  */
12967 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
12968 		       BFD_RELOC_LO16, AT);
12969 	  mips_optimize = hold_mips_optimize;
12970 	  expr1.X_add_number -= 4;
12971 
12972 	  relax_switch ();
12973 	  offset_expr.X_add_number = expr1.X_add_number;
12974 	  if (gpdelay)
12975 	    macro_build (NULL, "nop", "");
12976 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12977 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12978 	  load_delay_nop ();
12979 	  if (breg != 0)
12980 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
12981 	  /* Itbl support may require additional care here.  */
12982 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
12983 		       BFD_RELOC_LO16, AT);
12984 	  offset_expr.X_add_number += 4;
12985 
12986 	  /* Set mips_optimize to 2 to avoid inserting an undesired
12987              nop.  */
12988 	  hold_mips_optimize = mips_optimize;
12989 	  mips_optimize = 2;
12990 	  /* Itbl support may require additional care here.  */
12991 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
12992 		       BFD_RELOC_LO16, AT);
12993 	  mips_optimize = hold_mips_optimize;
12994 	  relax_end ();
12995 	}
12996       else
12997 	abort ();
12998 
12999       break;
13000 
13001     case M_SAA_AB:
13002       s = "saa";
13003       goto saa_saad;
13004     case M_SAAD_AB:
13005       s = "saad";
13006     saa_saad:
13007       gas_assert (!mips_opts.micromips);
13008       offbits = 0;
13009       fmt = "t,(b)";
13010       goto ld_st;
13011 
13012    /* New code added to support COPZ instructions.
13013       This code builds table entries out of the macros in mip_opcodes.
13014       R4000 uses interlocks to handle coproc delays.
13015       Other chips (like the R3000) require nops to be inserted for delays.
13016 
13017       FIXME: Currently, we require that the user handle delays.
13018       In order to fill delay slots for non-interlocked chips,
13019       we must have a way to specify delays based on the coprocessor.
13020       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13021       What are the side-effects of the cop instruction?
13022       What cache support might we have and what are its effects?
13023       Both coprocessor & memory require delays. how long???
13024       What registers are read/set/modified?
13025 
13026       If an itbl is provided to interpret cop instructions,
13027       this knowledge can be encoded in the itbl spec.  */
13028 
13029     case M_COP0:
13030       s = "c0";
13031       goto copz;
13032     case M_COP1:
13033       s = "c1";
13034       goto copz;
13035     case M_COP2:
13036       s = "c2";
13037       goto copz;
13038     case M_COP3:
13039       s = "c3";
13040     copz:
13041       gas_assert (!mips_opts.micromips);
13042       /* For now we just do C (same as Cz).  The parameter will be
13043          stored in insn_opcode by mips_ip.  */
13044       macro_build (NULL, s, "C", (int) ip->insn_opcode);
13045       break;
13046 
13047     case M_MOVE:
13048       move_register (op[0], op[1]);
13049       break;
13050 
13051     case M_MOVEP:
13052       gas_assert (mips_opts.micromips);
13053       gas_assert (mips_opts.insn32);
13054       move_register (micromips_to_32_reg_h_map1[op[0]],
13055 		     micromips_to_32_reg_m_map[op[1]]);
13056       move_register (micromips_to_32_reg_h_map2[op[0]],
13057 		     micromips_to_32_reg_n_map[op[2]]);
13058       break;
13059 
13060     case M_DMUL:
13061       dbl = 1;
13062       /* Fall through.  */
13063     case M_MUL:
13064       if (mips_opts.arch == CPU_R5900)
13065 	macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13066 		     op[2]);
13067       else
13068         {
13069 	  macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13070 	  macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13071         }
13072       break;
13073 
13074     case M_DMUL_I:
13075       dbl = 1;
13076       /* Fall through.  */
13077     case M_MUL_I:
13078       /* The MIPS assembler some times generates shifts and adds.  I'm
13079 	 not trying to be that fancy. GCC should do this for us
13080 	 anyway.  */
13081       used_at = 1;
13082       load_register (AT, &imm_expr, dbl);
13083       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13084       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13085       break;
13086 
13087     case M_DMULO_I:
13088       dbl = 1;
13089       /* Fall through.  */
13090     case M_MULO_I:
13091       imm = 1;
13092       goto do_mulo;
13093 
13094     case M_DMULO:
13095       dbl = 1;
13096       /* Fall through.  */
13097     case M_MULO:
13098     do_mulo:
13099       start_noreorder ();
13100       used_at = 1;
13101       if (imm)
13102 	load_register (AT, &imm_expr, dbl);
13103       macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13104 		   op[1], imm ? AT : op[2]);
13105       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13106       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
13107       macro_build (NULL, "mfhi", MFHL_FMT, AT);
13108       if (mips_trap)
13109 	macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
13110       else
13111 	{
13112 	  if (mips_opts.micromips)
13113 	    micromips_label_expr (&label_expr);
13114 	  else
13115 	    label_expr.X_add_number = 8;
13116 	  macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13117 	  macro_build (NULL, "nop", "");
13118 	  macro_build (NULL, "break", BRK_FMT, 6);
13119 	  if (mips_opts.micromips)
13120 	    micromips_add_label ();
13121 	}
13122       end_noreorder ();
13123       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13124       break;
13125 
13126     case M_DMULOU_I:
13127       dbl = 1;
13128       /* Fall through.  */
13129     case M_MULOU_I:
13130       imm = 1;
13131       goto do_mulou;
13132 
13133     case M_DMULOU:
13134       dbl = 1;
13135       /* Fall through.  */
13136     case M_MULOU:
13137     do_mulou:
13138       start_noreorder ();
13139       used_at = 1;
13140       if (imm)
13141 	load_register (AT, &imm_expr, dbl);
13142       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13143 		   op[1], imm ? AT : op[2]);
13144       macro_build (NULL, "mfhi", MFHL_FMT, AT);
13145       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13146       if (mips_trap)
13147 	macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13148       else
13149 	{
13150 	  if (mips_opts.micromips)
13151 	    micromips_label_expr (&label_expr);
13152 	  else
13153 	    label_expr.X_add_number = 8;
13154 	  macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13155 	  macro_build (NULL, "nop", "");
13156 	  macro_build (NULL, "break", BRK_FMT, 6);
13157 	  if (mips_opts.micromips)
13158 	    micromips_add_label ();
13159 	}
13160       end_noreorder ();
13161       break;
13162 
13163     case M_DROL:
13164       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13165 	{
13166 	  if (op[0] == op[1])
13167 	    {
13168 	      tempreg = AT;
13169 	      used_at = 1;
13170 	    }
13171 	  else
13172 	    tempreg = op[0];
13173 	  macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13174 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13175 	  break;
13176 	}
13177       used_at = 1;
13178       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13179       macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13180       macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13181       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13182       break;
13183 
13184     case M_ROL:
13185       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13186 	{
13187 	  if (op[0] == op[1])
13188 	    {
13189 	      tempreg = AT;
13190 	      used_at = 1;
13191 	    }
13192 	  else
13193 	    tempreg = op[0];
13194 	  macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13195 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13196 	  break;
13197 	}
13198       used_at = 1;
13199       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13200       macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13201       macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13202       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13203       break;
13204 
13205     case M_DROL_I:
13206       {
13207 	unsigned int rot;
13208 	const char *l;
13209 	const char *rr;
13210 
13211 	rot = imm_expr.X_add_number & 0x3f;
13212 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13213 	  {
13214 	    rot = (64 - rot) & 0x3f;
13215 	    if (rot >= 32)
13216 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13217 	    else
13218 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13219 	    break;
13220 	  }
13221 	if (rot == 0)
13222 	  {
13223 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13224 	    break;
13225 	  }
13226 	l = (rot < 0x20) ? "dsll" : "dsll32";
13227 	rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13228 	rot &= 0x1f;
13229 	used_at = 1;
13230 	macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13231 	macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13232 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13233       }
13234       break;
13235 
13236     case M_ROL_I:
13237       {
13238 	unsigned int rot;
13239 
13240 	rot = imm_expr.X_add_number & 0x1f;
13241 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13242 	  {
13243 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13244 			 (32 - rot) & 0x1f);
13245 	    break;
13246 	  }
13247 	if (rot == 0)
13248 	  {
13249 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13250 	    break;
13251 	  }
13252 	used_at = 1;
13253 	macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13254 	macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13255 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13256       }
13257       break;
13258 
13259     case M_DROR:
13260       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13261 	{
13262 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13263 	  break;
13264 	}
13265       used_at = 1;
13266       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13267       macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13268       macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13269       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13270       break;
13271 
13272     case M_ROR:
13273       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13274 	{
13275 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13276 	  break;
13277 	}
13278       used_at = 1;
13279       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13280       macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13281       macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13282       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13283       break;
13284 
13285     case M_DROR_I:
13286       {
13287 	unsigned int rot;
13288 	const char *l;
13289 	const char *rr;
13290 
13291 	rot = imm_expr.X_add_number & 0x3f;
13292 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13293 	  {
13294 	    if (rot >= 32)
13295 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13296 	    else
13297 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13298 	    break;
13299 	  }
13300 	if (rot == 0)
13301 	  {
13302 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13303 	    break;
13304 	  }
13305 	rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13306 	l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13307 	rot &= 0x1f;
13308 	used_at = 1;
13309 	macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13310 	macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13311 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13312       }
13313       break;
13314 
13315     case M_ROR_I:
13316       {
13317 	unsigned int rot;
13318 
13319 	rot = imm_expr.X_add_number & 0x1f;
13320 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13321 	  {
13322 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13323 	    break;
13324 	  }
13325 	if (rot == 0)
13326 	  {
13327 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13328 	    break;
13329 	  }
13330 	used_at = 1;
13331 	macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13332 	macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13333 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13334       }
13335       break;
13336 
13337     case M_SEQ:
13338       if (op[1] == 0)
13339 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13340       else if (op[2] == 0)
13341 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13342       else
13343 	{
13344 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13345 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13346 	}
13347       break;
13348 
13349     case M_SEQ_I:
13350       if (imm_expr.X_add_number == 0)
13351 	{
13352 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13353 	  break;
13354 	}
13355       if (op[1] == 0)
13356 	{
13357 	  as_warn (_("instruction %s: result is always false"),
13358 		   ip->insn_mo->name);
13359 	  move_register (op[0], 0);
13360 	  break;
13361 	}
13362       if (CPU_HAS_SEQ (mips_opts.arch)
13363 	  && -512 <= imm_expr.X_add_number
13364 	  && imm_expr.X_add_number < 512)
13365 	{
13366 	  macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13367 		       (int) imm_expr.X_add_number);
13368 	  break;
13369 	}
13370       if (imm_expr.X_add_number >= 0
13371 	  && imm_expr.X_add_number < 0x10000)
13372 	macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13373       else if (imm_expr.X_add_number > -0x8000
13374 	       && imm_expr.X_add_number < 0)
13375 	{
13376 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13377 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13378 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13379 	}
13380       else if (CPU_HAS_SEQ (mips_opts.arch))
13381 	{
13382 	  used_at = 1;
13383 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13384 	  macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13385 	  break;
13386 	}
13387       else
13388 	{
13389 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13390 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13391 	  used_at = 1;
13392 	}
13393       macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13394       break;
13395 
13396     case M_SGE:		/* X >= Y  <==>  not (X < Y) */
13397       s = "slt";
13398       goto sge;
13399     case M_SGEU:
13400       s = "sltu";
13401     sge:
13402       macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13403       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13404       break;
13405 
13406     case M_SGE_I:	/* X >= I  <==>  not (X < I) */
13407     case M_SGEU_I:
13408       if (imm_expr.X_add_number >= -0x8000
13409 	  && imm_expr.X_add_number < 0x8000)
13410 	macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13411 		     op[0], op[1], BFD_RELOC_LO16);
13412       else
13413 	{
13414 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13415 	  macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13416 		       op[0], op[1], AT);
13417 	  used_at = 1;
13418 	}
13419       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13420       break;
13421 
13422     case M_SGT:		/* X > Y  <==>  Y < X */
13423       s = "slt";
13424       goto sgt;
13425     case M_SGTU:
13426       s = "sltu";
13427     sgt:
13428       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13429       break;
13430 
13431     case M_SGT_I:	/* X > I  <==>  I < X */
13432       s = "slt";
13433       goto sgti;
13434     case M_SGTU_I:
13435       s = "sltu";
13436     sgti:
13437       used_at = 1;
13438       load_register (AT, &imm_expr, GPR_SIZE == 64);
13439       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13440       break;
13441 
13442     case M_SLE:		/* X <= Y  <==>  Y >= X  <==>  not (Y < X) */
13443       s = "slt";
13444       goto sle;
13445     case M_SLEU:
13446       s = "sltu";
13447     sle:
13448       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13449       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13450       break;
13451 
13452     case M_SLE_I:	/* X <= I  <==>  I >= X  <==>  not (I < X) */
13453       s = "slt";
13454       goto slei;
13455     case M_SLEU_I:
13456       s = "sltu";
13457     slei:
13458       used_at = 1;
13459       load_register (AT, &imm_expr, GPR_SIZE == 64);
13460       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13461       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13462       break;
13463 
13464     case M_SLT_I:
13465       if (imm_expr.X_add_number >= -0x8000
13466 	  && imm_expr.X_add_number < 0x8000)
13467 	{
13468 	  macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13469 		       BFD_RELOC_LO16);
13470 	  break;
13471 	}
13472       used_at = 1;
13473       load_register (AT, &imm_expr, GPR_SIZE == 64);
13474       macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13475       break;
13476 
13477     case M_SLTU_I:
13478       if (imm_expr.X_add_number >= -0x8000
13479 	  && imm_expr.X_add_number < 0x8000)
13480 	{
13481 	  macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13482 		       BFD_RELOC_LO16);
13483 	  break;
13484 	}
13485       used_at = 1;
13486       load_register (AT, &imm_expr, GPR_SIZE == 64);
13487       macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13488       break;
13489 
13490     case M_SNE:
13491       if (op[1] == 0)
13492 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13493       else if (op[2] == 0)
13494 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13495       else
13496 	{
13497 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13498 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13499 	}
13500       break;
13501 
13502     case M_SNE_I:
13503       if (imm_expr.X_add_number == 0)
13504 	{
13505 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13506 	  break;
13507 	}
13508       if (op[1] == 0)
13509 	{
13510 	  as_warn (_("instruction %s: result is always true"),
13511 		   ip->insn_mo->name);
13512 	  macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13513 		       op[0], 0, BFD_RELOC_LO16);
13514 	  break;
13515 	}
13516       if (CPU_HAS_SEQ (mips_opts.arch)
13517 	  && -512 <= imm_expr.X_add_number
13518 	  && imm_expr.X_add_number < 512)
13519 	{
13520 	  macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13521 		       (int) imm_expr.X_add_number);
13522 	  break;
13523 	}
13524       if (imm_expr.X_add_number >= 0
13525 	  && imm_expr.X_add_number < 0x10000)
13526 	{
13527 	  macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13528 		       BFD_RELOC_LO16);
13529 	}
13530       else if (imm_expr.X_add_number > -0x8000
13531 	       && imm_expr.X_add_number < 0)
13532 	{
13533 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13534 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13535 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13536 	}
13537       else if (CPU_HAS_SEQ (mips_opts.arch))
13538 	{
13539 	  used_at = 1;
13540 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13541 	  macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13542 	  break;
13543 	}
13544       else
13545 	{
13546 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13547 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13548 	  used_at = 1;
13549 	}
13550       macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13551       break;
13552 
13553     case M_SUB_I:
13554       s = "addi";
13555       s2 = "sub";
13556       goto do_subi;
13557     case M_SUBU_I:
13558       s = "addiu";
13559       s2 = "subu";
13560       goto do_subi;
13561     case M_DSUB_I:
13562       dbl = 1;
13563       s = "daddi";
13564       s2 = "dsub";
13565       if (!mips_opts.micromips)
13566 	goto do_subi;
13567       if (imm_expr.X_add_number > -0x200
13568 	  && imm_expr.X_add_number <= 0x200)
13569 	{
13570 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
13571 		       (int) -imm_expr.X_add_number);
13572 	  break;
13573 	}
13574       goto do_subi_i;
13575     case M_DSUBU_I:
13576       dbl = 1;
13577       s = "daddiu";
13578       s2 = "dsubu";
13579     do_subi:
13580       if (imm_expr.X_add_number > -0x8000
13581 	  && imm_expr.X_add_number <= 0x8000)
13582 	{
13583 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13584 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13585 	  break;
13586 	}
13587     do_subi_i:
13588       used_at = 1;
13589       load_register (AT, &imm_expr, dbl);
13590       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13591       break;
13592 
13593     case M_TEQ_I:
13594       s = "teq";
13595       goto trap;
13596     case M_TGE_I:
13597       s = "tge";
13598       goto trap;
13599     case M_TGEU_I:
13600       s = "tgeu";
13601       goto trap;
13602     case M_TLT_I:
13603       s = "tlt";
13604       goto trap;
13605     case M_TLTU_I:
13606       s = "tltu";
13607       goto trap;
13608     case M_TNE_I:
13609       s = "tne";
13610     trap:
13611       used_at = 1;
13612       load_register (AT, &imm_expr, GPR_SIZE == 64);
13613       macro_build (NULL, s, "s,t", op[0], AT);
13614       break;
13615 
13616     case M_TRUNCWS:
13617     case M_TRUNCWD:
13618       gas_assert (!mips_opts.micromips);
13619       gas_assert (mips_opts.isa == ISA_MIPS1);
13620       used_at = 1;
13621 
13622       /*
13623        * Is the double cfc1 instruction a bug in the mips assembler;
13624        * or is there a reason for it?
13625        */
13626       start_noreorder ();
13627       macro_build (NULL, "cfc1", "t,G", op[2], RA);
13628       macro_build (NULL, "cfc1", "t,G", op[2], RA);
13629       macro_build (NULL, "nop", "");
13630       expr1.X_add_number = 3;
13631       macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13632       expr1.X_add_number = 2;
13633       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13634       macro_build (NULL, "ctc1", "t,G", AT, RA);
13635       macro_build (NULL, "nop", "");
13636       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13637 		   op[0], op[1]);
13638       macro_build (NULL, "ctc1", "t,G", op[2], RA);
13639       macro_build (NULL, "nop", "");
13640       end_noreorder ();
13641       break;
13642 
13643     case M_ULH_AB:
13644       s = "lb";
13645       s2 = "lbu";
13646       off = 1;
13647       goto uld_st;
13648     case M_ULHU_AB:
13649       s = "lbu";
13650       s2 = "lbu";
13651       off = 1;
13652       goto uld_st;
13653     case M_ULW_AB:
13654       s = "lwl";
13655       s2 = "lwr";
13656       offbits = (mips_opts.micromips ? 12 : 16);
13657       off = 3;
13658       goto uld_st;
13659     case M_ULD_AB:
13660       s = "ldl";
13661       s2 = "ldr";
13662       offbits = (mips_opts.micromips ? 12 : 16);
13663       off = 7;
13664       goto uld_st;
13665     case M_USH_AB:
13666       s = "sb";
13667       s2 = "sb";
13668       off = 1;
13669       ust = 1;
13670       goto uld_st;
13671     case M_USW_AB:
13672       s = "swl";
13673       s2 = "swr";
13674       offbits = (mips_opts.micromips ? 12 : 16);
13675       off = 3;
13676       ust = 1;
13677       goto uld_st;
13678     case M_USD_AB:
13679       s = "sdl";
13680       s2 = "sdr";
13681       offbits = (mips_opts.micromips ? 12 : 16);
13682       off = 7;
13683       ust = 1;
13684 
13685     uld_st:
13686       breg = op[2];
13687       large_offset = !small_offset_p (off, align, offbits);
13688       ep = &offset_expr;
13689       expr1.X_add_number = 0;
13690       if (large_offset)
13691 	{
13692 	  used_at = 1;
13693 	  tempreg = AT;
13694 	  if (small_offset_p (0, align, 16))
13695 	    macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13696 			 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13697 	  else
13698 	    {
13699 	      load_address (tempreg, ep, &used_at);
13700 	      if (breg != 0)
13701 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13702 			     tempreg, tempreg, breg);
13703 	    }
13704 	  offset_reloc[0] = BFD_RELOC_LO16;
13705 	  offset_reloc[1] = BFD_RELOC_UNUSED;
13706 	  offset_reloc[2] = BFD_RELOC_UNUSED;
13707 	  breg = tempreg;
13708 	  tempreg = op[0];
13709 	  ep = &expr1;
13710 	}
13711       else if (!ust && op[0] == breg)
13712 	{
13713 	  used_at = 1;
13714 	  tempreg = AT;
13715 	}
13716       else
13717 	tempreg = op[0];
13718 
13719       if (off == 1)
13720 	goto ulh_sh;
13721 
13722       if (!target_big_endian)
13723 	ep->X_add_number += off;
13724       if (offbits == 12)
13725 	macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
13726       else
13727 	macro_build (ep, s, "t,o(b)", tempreg, -1,
13728 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13729 
13730       if (!target_big_endian)
13731 	ep->X_add_number -= off;
13732       else
13733 	ep->X_add_number += off;
13734       if (offbits == 12)
13735 	macro_build (NULL, s2, "t,~(b)",
13736 		     tempreg, (int) ep->X_add_number, breg);
13737       else
13738 	macro_build (ep, s2, "t,o(b)", tempreg, -1,
13739 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13740 
13741       /* If necessary, move the result in tempreg to the final destination.  */
13742       if (!ust && op[0] != tempreg)
13743         {
13744 	  /* Protect second load's delay slot.  */
13745 	  load_delay_nop ();
13746 	  move_register (op[0], tempreg);
13747 	}
13748       break;
13749 
13750     ulh_sh:
13751       used_at = 1;
13752       if (target_big_endian == ust)
13753 	ep->X_add_number += off;
13754       tempreg = ust || large_offset ? op[0] : AT;
13755       macro_build (ep, s, "t,o(b)", tempreg, -1,
13756 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13757 
13758       /* For halfword transfers we need a temporary register to shuffle
13759          bytes.  Unfortunately for M_USH_A we have none available before
13760          the next store as AT holds the base address.  We deal with this
13761          case by clobbering TREG and then restoring it as with ULH.  */
13762       tempreg = ust == large_offset ? op[0] : AT;
13763       if (ust)
13764 	macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
13765 
13766       if (target_big_endian == ust)
13767 	ep->X_add_number -= off;
13768       else
13769 	ep->X_add_number += off;
13770       macro_build (ep, s2, "t,o(b)", tempreg, -1,
13771 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
13772 
13773       /* For M_USH_A re-retrieve the LSB.  */
13774       if (ust && large_offset)
13775 	{
13776 	  if (target_big_endian)
13777 	    ep->X_add_number += off;
13778 	  else
13779 	    ep->X_add_number -= off;
13780 	  macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13781 		       offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
13782 	}
13783       /* For ULH and M_USH_A OR the LSB in.  */
13784       if (!ust || large_offset)
13785 	{
13786 	  tempreg = !large_offset ? AT : op[0];
13787 	  macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
13788 	  macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13789 	}
13790       break;
13791 
13792     default:
13793       /* FIXME: Check if this is one of the itbl macros, since they
13794 	 are added dynamically.  */
13795       as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
13796       break;
13797     }
13798   if (!mips_opts.at && used_at)
13799     as_bad (_("macro used $at after \".set noat\""));
13800 }
13801 
13802 /* Implement macros in mips16 mode.  */
13803 
13804 static void
13805 mips16_macro (struct mips_cl_insn *ip)
13806 {
13807   const struct mips_operand_array *operands;
13808   int mask;
13809   int tmp;
13810   expressionS expr1;
13811   int dbl;
13812   const char *s, *s2, *s3;
13813   unsigned int op[MAX_OPERANDS];
13814   unsigned int i;
13815 
13816   mask = ip->insn_mo->mask;
13817 
13818   operands = insn_operands (ip);
13819   for (i = 0; i < MAX_OPERANDS; i++)
13820     if (operands->operand[i])
13821       op[i] = insn_extract_operand (ip, operands->operand[i]);
13822     else
13823       op[i] = -1;
13824 
13825   expr1.X_op = O_constant;
13826   expr1.X_op_symbol = NULL;
13827   expr1.X_add_symbol = NULL;
13828   expr1.X_add_number = 1;
13829 
13830   dbl = 0;
13831 
13832   switch (mask)
13833     {
13834     default:
13835       abort ();
13836 
13837     case M_DDIV_3:
13838       dbl = 1;
13839       /* Fall through.  */
13840     case M_DIV_3:
13841       s = "mflo";
13842       goto do_div3;
13843     case M_DREM_3:
13844       dbl = 1;
13845       /* Fall through.  */
13846     case M_REM_3:
13847       s = "mfhi";
13848     do_div3:
13849       start_noreorder ();
13850       macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
13851       expr1.X_add_number = 2;
13852       macro_build (&expr1, "bnez", "x,p", op[2]);
13853       macro_build (NULL, "break", "6", 7);
13854 
13855       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13856          since that causes an overflow.  We should do that as well,
13857          but I don't see how to do the comparisons without a temporary
13858          register.  */
13859       end_noreorder ();
13860       macro_build (NULL, s, "x", op[0]);
13861       break;
13862 
13863     case M_DIVU_3:
13864       s = "divu";
13865       s2 = "mflo";
13866       goto do_divu3;
13867     case M_REMU_3:
13868       s = "divu";
13869       s2 = "mfhi";
13870       goto do_divu3;
13871     case M_DDIVU_3:
13872       s = "ddivu";
13873       s2 = "mflo";
13874       goto do_divu3;
13875     case M_DREMU_3:
13876       s = "ddivu";
13877       s2 = "mfhi";
13878     do_divu3:
13879       start_noreorder ();
13880       macro_build (NULL, s, ".,x,y", op[1], op[2]);
13881       expr1.X_add_number = 2;
13882       macro_build (&expr1, "bnez", "x,p", op[2]);
13883       macro_build (NULL, "break", "6", 7);
13884       end_noreorder ();
13885       macro_build (NULL, s2, "x", op[0]);
13886       break;
13887 
13888     case M_DMUL:
13889       dbl = 1;
13890       /* Fall through.  */
13891     case M_MUL:
13892       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13893       macro_build (NULL, "mflo", "x", op[0]);
13894       break;
13895 
13896     case M_DSUBU_I:
13897       dbl = 1;
13898       goto do_subu;
13899     case M_SUBU_I:
13900     do_subu:
13901       imm_expr.X_add_number = -imm_expr.X_add_number;
13902       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
13903       break;
13904 
13905     case M_SUBU_I_2:
13906       imm_expr.X_add_number = -imm_expr.X_add_number;
13907       macro_build (&imm_expr, "addiu", "x,k", op[0]);
13908       break;
13909 
13910     case M_DSUBU_I_2:
13911       imm_expr.X_add_number = -imm_expr.X_add_number;
13912       macro_build (&imm_expr, "daddiu", "y,j", op[0]);
13913       break;
13914 
13915     case M_BEQ:
13916       s = "cmp";
13917       s2 = "bteqz";
13918       goto do_branch;
13919     case M_BNE:
13920       s = "cmp";
13921       s2 = "btnez";
13922       goto do_branch;
13923     case M_BLT:
13924       s = "slt";
13925       s2 = "btnez";
13926       goto do_branch;
13927     case M_BLTU:
13928       s = "sltu";
13929       s2 = "btnez";
13930       goto do_branch;
13931     case M_BLE:
13932       s = "slt";
13933       s2 = "bteqz";
13934       goto do_reverse_branch;
13935     case M_BLEU:
13936       s = "sltu";
13937       s2 = "bteqz";
13938       goto do_reverse_branch;
13939     case M_BGE:
13940       s = "slt";
13941       s2 = "bteqz";
13942       goto do_branch;
13943     case M_BGEU:
13944       s = "sltu";
13945       s2 = "bteqz";
13946       goto do_branch;
13947     case M_BGT:
13948       s = "slt";
13949       s2 = "btnez";
13950       goto do_reverse_branch;
13951     case M_BGTU:
13952       s = "sltu";
13953       s2 = "btnez";
13954 
13955     do_reverse_branch:
13956       tmp = op[1];
13957       op[1] = op[0];
13958       op[0] = tmp;
13959 
13960     do_branch:
13961       macro_build (NULL, s, "x,y", op[0], op[1]);
13962       macro_build (&offset_expr, s2, "p");
13963       break;
13964 
13965     case M_BEQ_I:
13966       s = "cmpi";
13967       s2 = "bteqz";
13968       s3 = "x,U";
13969       goto do_branch_i;
13970     case M_BNE_I:
13971       s = "cmpi";
13972       s2 = "btnez";
13973       s3 = "x,U";
13974       goto do_branch_i;
13975     case M_BLT_I:
13976       s = "slti";
13977       s2 = "btnez";
13978       s3 = "x,8";
13979       goto do_branch_i;
13980     case M_BLTU_I:
13981       s = "sltiu";
13982       s2 = "btnez";
13983       s3 = "x,8";
13984       goto do_branch_i;
13985     case M_BLE_I:
13986       s = "slti";
13987       s2 = "btnez";
13988       s3 = "x,8";
13989       goto do_addone_branch_i;
13990     case M_BLEU_I:
13991       s = "sltiu";
13992       s2 = "btnez";
13993       s3 = "x,8";
13994       goto do_addone_branch_i;
13995     case M_BGE_I:
13996       s = "slti";
13997       s2 = "bteqz";
13998       s3 = "x,8";
13999       goto do_branch_i;
14000     case M_BGEU_I:
14001       s = "sltiu";
14002       s2 = "bteqz";
14003       s3 = "x,8";
14004       goto do_branch_i;
14005     case M_BGT_I:
14006       s = "slti";
14007       s2 = "bteqz";
14008       s3 = "x,8";
14009       goto do_addone_branch_i;
14010     case M_BGTU_I:
14011       s = "sltiu";
14012       s2 = "bteqz";
14013       s3 = "x,8";
14014 
14015     do_addone_branch_i:
14016       ++imm_expr.X_add_number;
14017 
14018     do_branch_i:
14019       macro_build (&imm_expr, s, s3, op[0]);
14020       macro_build (&offset_expr, s2, "p");
14021       break;
14022 
14023     case M_ABS:
14024       expr1.X_add_number = 0;
14025       macro_build (&expr1, "slti", "x,8", op[1]);
14026       if (op[0] != op[1])
14027 	macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
14028       expr1.X_add_number = 2;
14029       macro_build (&expr1, "bteqz", "p");
14030       macro_build (NULL, "neg", "x,w", op[0], op[0]);
14031       break;
14032     }
14033 }
14034 
14035 /* Look up instruction [START, START + LENGTH) in HASH.  Record any extra
14036    opcode bits in *OPCODE_EXTRA.  */
14037 
14038 static struct mips_opcode *
14039 mips_lookup_insn (struct hash_control *hash, const char *start,
14040 		  ssize_t length, unsigned int *opcode_extra)
14041 {
14042   char *name, *dot, *p;
14043   unsigned int mask, suffix;
14044   ssize_t opend;
14045   struct mips_opcode *insn;
14046 
14047   /* Make a copy of the instruction so that we can fiddle with it.  */
14048   name = xstrndup (start, length);
14049 
14050   /* Look up the instruction as-is.  */
14051   insn = (struct mips_opcode *) hash_find (hash, name);
14052   if (insn)
14053     goto end;
14054 
14055   dot = strchr (name, '.');
14056   if (dot && dot[1])
14057     {
14058       /* Try to interpret the text after the dot as a VU0 channel suffix.  */
14059       p = mips_parse_vu0_channels (dot + 1, &mask);
14060       if (*p == 0 && mask != 0)
14061 	{
14062 	  *dot = 0;
14063 	  insn = (struct mips_opcode *) hash_find (hash, name);
14064 	  *dot = '.';
14065 	  if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14066 	    {
14067 	      *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
14068 	      goto end;
14069 	    }
14070 	}
14071     }
14072 
14073   if (mips_opts.micromips)
14074     {
14075       /* See if there's an instruction size override suffix,
14076 	 either `16' or `32', at the end of the mnemonic proper,
14077 	 that defines the operation, i.e. before the first `.'
14078 	 character if any.  Strip it and retry.  */
14079       opend = dot != NULL ? dot - name : length;
14080       if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14081 	suffix = 2;
14082       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
14083 	suffix = 4;
14084       else
14085 	suffix = 0;
14086       if (suffix)
14087 	{
14088 	  memmove (name + opend - 2, name + opend, length - opend + 1);
14089 	  insn = (struct mips_opcode *) hash_find (hash, name);
14090 	  if (insn)
14091 	    {
14092 	      forced_insn_length = suffix;
14093 	      goto end;
14094 	    }
14095 	}
14096     }
14097 
14098   insn = NULL;
14099  end:
14100   free (name);
14101   return insn;
14102 }
14103 
14104 /* Assemble an instruction into its binary format.  If the instruction
14105    is a macro, set imm_expr and offset_expr to the values associated
14106    with "I" and "A" operands respectively.  Otherwise store the value
14107    of the relocatable field (if any) in offset_expr.  In both cases
14108    set offset_reloc to the relocation operators applied to offset_expr.  */
14109 
14110 static void
14111 mips_ip (char *str, struct mips_cl_insn *insn)
14112 {
14113   const struct mips_opcode *first, *past;
14114   struct hash_control *hash;
14115   char format;
14116   size_t end;
14117   struct mips_operand_token *tokens;
14118   unsigned int opcode_extra;
14119 
14120   if (mips_opts.micromips)
14121     {
14122       hash = micromips_op_hash;
14123       past = &micromips_opcodes[bfd_micromips_num_opcodes];
14124     }
14125   else
14126     {
14127       hash = op_hash;
14128       past = &mips_opcodes[NUMOPCODES];
14129     }
14130   forced_insn_length = 0;
14131   opcode_extra = 0;
14132 
14133   /* We first try to match an instruction up to a space or to the end.  */
14134   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14135     continue;
14136 
14137   first = mips_lookup_insn (hash, str, end, &opcode_extra);
14138   if (first == NULL)
14139     {
14140       set_insn_error (0, _("unrecognized opcode"));
14141       return;
14142     }
14143 
14144   if (strcmp (first->name, "li.s") == 0)
14145     format = 'f';
14146   else if (strcmp (first->name, "li.d") == 0)
14147     format = 'd';
14148   else
14149     format = 0;
14150   tokens = mips_parse_arguments (str + end, format);
14151   if (!tokens)
14152     return;
14153 
14154   if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14155       && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
14156     set_insn_error (0, _("invalid operands"));
14157 
14158   obstack_free (&mips_operand_tokens, tokens);
14159 }
14160 
14161 /* As for mips_ip, but used when assembling MIPS16 code.
14162    Also set forced_insn_length to the resulting instruction size in
14163    bytes if the user explicitly requested a small or extended instruction.  */
14164 
14165 static void
14166 mips16_ip (char *str, struct mips_cl_insn *insn)
14167 {
14168   char *end, *s, c;
14169   struct mips_opcode *first;
14170   struct mips_operand_token *tokens;
14171   unsigned int l;
14172 
14173   for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14174     ;
14175   end = s;
14176   c = *end;
14177 
14178   l = 0;
14179   switch (c)
14180     {
14181     case '\0':
14182       break;
14183 
14184     case ' ':
14185       s++;
14186       break;
14187 
14188     case '.':
14189       s++;
14190       if (*s == 't')
14191 	{
14192 	  l = 2;
14193 	  s++;
14194 	}
14195       else if (*s == 'e')
14196 	{
14197 	  l = 4;
14198 	  s++;
14199 	}
14200       if (*s == '\0')
14201 	break;
14202       else if (*s++ == ' ')
14203 	break;
14204       set_insn_error (0, _("unrecognized opcode"));
14205       return;
14206     }
14207   forced_insn_length = l;
14208 
14209   *end = 0;
14210   first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
14211   *end = c;
14212 
14213   if (!first)
14214     {
14215       set_insn_error (0, _("unrecognized opcode"));
14216       return;
14217     }
14218 
14219   tokens = mips_parse_arguments (s, 0);
14220   if (!tokens)
14221     return;
14222 
14223   if (!match_mips16_insns (insn, first, tokens))
14224     set_insn_error (0, _("invalid operands"));
14225 
14226   obstack_free (&mips_operand_tokens, tokens);
14227 }
14228 
14229 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14230    NBITS is the number of significant bits in VAL.  */
14231 
14232 static unsigned long
14233 mips16_immed_extend (offsetT val, unsigned int nbits)
14234 {
14235   int extval;
14236 
14237   extval = 0;
14238   val &= (1U << nbits) - 1;
14239   if (nbits == 16 || nbits == 9)
14240     {
14241       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14242       val &= 0x1f;
14243     }
14244   else if (nbits == 15)
14245     {
14246       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14247       val &= 0xf;
14248     }
14249   else if (nbits == 6)
14250     {
14251       extval = ((val & 0x1f) << 6) | (val & 0x20);
14252       val = 0;
14253     }
14254   return (extval << 16) | val;
14255 }
14256 
14257 /* Like decode_mips16_operand, but require the operand to be defined and
14258    require it to be an integer.  */
14259 
14260 static const struct mips_int_operand *
14261 mips16_immed_operand (int type, bfd_boolean extended_p)
14262 {
14263   const struct mips_operand *operand;
14264 
14265   operand = decode_mips16_operand (type, extended_p);
14266   if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14267     abort ();
14268   return (const struct mips_int_operand *) operand;
14269 }
14270 
14271 /* Return true if SVAL fits OPERAND.  RELOC is as for mips16_immed.  */
14272 
14273 static bfd_boolean
14274 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14275 			 bfd_reloc_code_real_type reloc, offsetT sval)
14276 {
14277   int min_val, max_val;
14278 
14279   min_val = mips_int_operand_min (operand);
14280   max_val = mips_int_operand_max (operand);
14281   if (reloc != BFD_RELOC_UNUSED)
14282     {
14283       if (min_val < 0)
14284 	sval = SEXT_16BIT (sval);
14285       else
14286 	sval &= 0xffff;
14287     }
14288 
14289   return (sval >= min_val
14290 	  && sval <= max_val
14291 	  && (sval & ((1 << operand->shift) - 1)) == 0);
14292 }
14293 
14294 /* Install immediate value VAL into MIPS16 instruction *INSN,
14295    extending it if necessary.  The instruction in *INSN may
14296    already be extended.
14297 
14298    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14299    if none.  In the former case, VAL is a 16-bit number with no
14300    defined signedness.
14301 
14302    TYPE is the type of the immediate field.  USER_INSN_LENGTH
14303    is the length that the user requested, or 0 if none.  */
14304 
14305 static void
14306 mips16_immed (const char *file, unsigned int line, int type,
14307 	      bfd_reloc_code_real_type reloc, offsetT val,
14308 	      unsigned int user_insn_length, unsigned long *insn)
14309 {
14310   const struct mips_int_operand *operand;
14311   unsigned int uval, length;
14312 
14313   operand = mips16_immed_operand (type, FALSE);
14314   if (!mips16_immed_in_range_p (operand, reloc, val))
14315     {
14316       /* We need an extended instruction.  */
14317       if (user_insn_length == 2)
14318 	as_bad_where (file, line, _("invalid unextended operand value"));
14319       else
14320 	*insn |= MIPS16_EXTEND;
14321     }
14322   else if (user_insn_length == 4)
14323     {
14324       /* The operand doesn't force an unextended instruction to be extended.
14325 	 Warn if the user wanted an extended instruction anyway.  */
14326       *insn |= MIPS16_EXTEND;
14327       as_warn_where (file, line,
14328 		     _("extended operand requested but not required"));
14329     }
14330 
14331   length = mips16_opcode_length (*insn);
14332   if (length == 4)
14333     {
14334       operand = mips16_immed_operand (type, TRUE);
14335       if (!mips16_immed_in_range_p (operand, reloc, val))
14336 	as_bad_where (file, line,
14337 		      _("operand value out of range for instruction"));
14338     }
14339   uval = ((unsigned int) val >> operand->shift) - operand->bias;
14340   if (length == 2 || operand->root.lsb != 0)
14341     *insn = mips_insert_operand (&operand->root, *insn, uval);
14342   else
14343     *insn |= mips16_immed_extend (uval, operand->root.size);
14344 }
14345 
14346 struct percent_op_match
14347 {
14348   const char *str;
14349   bfd_reloc_code_real_type reloc;
14350 };
14351 
14352 static const struct percent_op_match mips_percent_op[] =
14353 {
14354   {"%lo", BFD_RELOC_LO16},
14355   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14356   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14357   {"%call16", BFD_RELOC_MIPS_CALL16},
14358   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14359   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14360   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14361   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14362   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14363   {"%got", BFD_RELOC_MIPS_GOT16},
14364   {"%gp_rel", BFD_RELOC_GPREL16},
14365   {"%gprel", BFD_RELOC_GPREL16},
14366   {"%half", BFD_RELOC_16},
14367   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14368   {"%higher", BFD_RELOC_MIPS_HIGHER},
14369   {"%neg", BFD_RELOC_MIPS_SUB},
14370   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14371   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14372   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14373   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14374   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14375   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14376   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14377   {"%hi", BFD_RELOC_HI16_S},
14378   {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14379   {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14380 };
14381 
14382 static const struct percent_op_match mips16_percent_op[] =
14383 {
14384   {"%lo", BFD_RELOC_MIPS16_LO16},
14385   {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14386   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14387   {"%got", BFD_RELOC_MIPS16_GOT16},
14388   {"%call16", BFD_RELOC_MIPS16_CALL16},
14389   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14390   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14391   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14392   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14393   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14394   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14395   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14396   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14397 };
14398 
14399 
14400 /* Return true if *STR points to a relocation operator.  When returning true,
14401    move *STR over the operator and store its relocation code in *RELOC.
14402    Leave both *STR and *RELOC alone when returning false.  */
14403 
14404 static bfd_boolean
14405 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14406 {
14407   const struct percent_op_match *percent_op;
14408   size_t limit, i;
14409 
14410   if (mips_opts.mips16)
14411     {
14412       percent_op = mips16_percent_op;
14413       limit = ARRAY_SIZE (mips16_percent_op);
14414     }
14415   else
14416     {
14417       percent_op = mips_percent_op;
14418       limit = ARRAY_SIZE (mips_percent_op);
14419     }
14420 
14421   for (i = 0; i < limit; i++)
14422     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14423       {
14424 	int len = strlen (percent_op[i].str);
14425 
14426 	if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14427 	  continue;
14428 
14429 	*str += strlen (percent_op[i].str);
14430 	*reloc = percent_op[i].reloc;
14431 
14432 	/* Check whether the output BFD supports this relocation.
14433 	   If not, issue an error and fall back on something safe.  */
14434 	if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14435 	  {
14436 	    as_bad (_("relocation %s isn't supported by the current ABI"),
14437 		    percent_op[i].str);
14438 	    *reloc = BFD_RELOC_UNUSED;
14439 	  }
14440 	return TRUE;
14441       }
14442   return FALSE;
14443 }
14444 
14445 
14446 /* Parse string STR as a 16-bit relocatable operand.  Store the
14447    expression in *EP and the relocations in the array starting
14448    at RELOC.  Return the number of relocation operators used.
14449 
14450    On exit, EXPR_END points to the first character after the expression.  */
14451 
14452 static size_t
14453 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14454 		       char *str)
14455 {
14456   bfd_reloc_code_real_type reversed_reloc[3];
14457   size_t reloc_index, i;
14458   int crux_depth, str_depth;
14459   char *crux;
14460 
14461   /* Search for the start of the main expression, recoding relocations
14462      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14463      of the main expression and with CRUX_DEPTH containing the number
14464      of open brackets at that point.  */
14465   reloc_index = -1;
14466   str_depth = 0;
14467   do
14468     {
14469       reloc_index++;
14470       crux = str;
14471       crux_depth = str_depth;
14472 
14473       /* Skip over whitespace and brackets, keeping count of the number
14474 	 of brackets.  */
14475       while (*str == ' ' || *str == '\t' || *str == '(')
14476 	if (*str++ == '(')
14477 	  str_depth++;
14478     }
14479   while (*str == '%'
14480 	 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14481 	 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14482 
14483   my_getExpression (ep, crux);
14484   str = expr_end;
14485 
14486   /* Match every open bracket.  */
14487   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14488     if (*str++ == ')')
14489       crux_depth--;
14490 
14491   if (crux_depth > 0)
14492     as_bad (_("unclosed '('"));
14493 
14494   expr_end = str;
14495 
14496   if (reloc_index != 0)
14497     {
14498       prev_reloc_op_frag = frag_now;
14499       for (i = 0; i < reloc_index; i++)
14500 	reloc[i] = reversed_reloc[reloc_index - 1 - i];
14501     }
14502 
14503   return reloc_index;
14504 }
14505 
14506 static void
14507 my_getExpression (expressionS *ep, char *str)
14508 {
14509   char *save_in;
14510 
14511   save_in = input_line_pointer;
14512   input_line_pointer = str;
14513   expression (ep);
14514   expr_end = input_line_pointer;
14515   input_line_pointer = save_in;
14516 }
14517 
14518 const char *
14519 md_atof (int type, char *litP, int *sizeP)
14520 {
14521   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14522 }
14523 
14524 void
14525 md_number_to_chars (char *buf, valueT val, int n)
14526 {
14527   if (target_big_endian)
14528     number_to_chars_bigendian (buf, val, n);
14529   else
14530     number_to_chars_littleendian (buf, val, n);
14531 }
14532 
14533 static int support_64bit_objects(void)
14534 {
14535   const char **list, **l;
14536   int yes;
14537 
14538   list = bfd_target_list ();
14539   for (l = list; *l != NULL; l++)
14540     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14541 	|| strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14542       break;
14543   yes = (*l != NULL);
14544   free (list);
14545   return yes;
14546 }
14547 
14548 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14549    NEW_VALUE.  Warn if another value was already specified.  Note:
14550    we have to defer parsing the -march and -mtune arguments in order
14551    to handle 'from-abi' correctly, since the ABI might be specified
14552    in a later argument.  */
14553 
14554 static void
14555 mips_set_option_string (const char **string_ptr, const char *new_value)
14556 {
14557   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14558     as_warn (_("a different %s was already specified, is now %s"),
14559 	     string_ptr == &mips_arch_string ? "-march" : "-mtune",
14560 	     new_value);
14561 
14562   *string_ptr = new_value;
14563 }
14564 
14565 int
14566 md_parse_option (int c, const char *arg)
14567 {
14568   unsigned int i;
14569 
14570   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14571     if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14572       {
14573 	file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14574 					   c == mips_ases[i].option_on);
14575 	return 1;
14576       }
14577 
14578   switch (c)
14579     {
14580     case OPTION_CONSTRUCT_FLOATS:
14581       mips_disable_float_construction = 0;
14582       break;
14583 
14584     case OPTION_NO_CONSTRUCT_FLOATS:
14585       mips_disable_float_construction = 1;
14586       break;
14587 
14588     case OPTION_TRAP:
14589       mips_trap = 1;
14590       break;
14591 
14592     case OPTION_BREAK:
14593       mips_trap = 0;
14594       break;
14595 
14596     case OPTION_EB:
14597       target_big_endian = 1;
14598       break;
14599 
14600     case OPTION_EL:
14601       target_big_endian = 0;
14602       break;
14603 
14604     case 'O':
14605       if (arg == NULL)
14606 	mips_optimize = 1;
14607       else if (arg[0] == '0')
14608 	mips_optimize = 0;
14609       else if (arg[0] == '1')
14610 	mips_optimize = 1;
14611       else
14612 	mips_optimize = 2;
14613       break;
14614 
14615     case 'g':
14616       if (arg == NULL)
14617 	mips_debug = 2;
14618       else
14619 	mips_debug = atoi (arg);
14620       break;
14621 
14622     case OPTION_MIPS1:
14623       file_mips_opts.isa = ISA_MIPS1;
14624       break;
14625 
14626     case OPTION_MIPS2:
14627       file_mips_opts.isa = ISA_MIPS2;
14628       break;
14629 
14630     case OPTION_MIPS3:
14631       file_mips_opts.isa = ISA_MIPS3;
14632       break;
14633 
14634     case OPTION_MIPS4:
14635       file_mips_opts.isa = ISA_MIPS4;
14636       break;
14637 
14638     case OPTION_MIPS5:
14639       file_mips_opts.isa = ISA_MIPS5;
14640       break;
14641 
14642     case OPTION_MIPS32:
14643       file_mips_opts.isa = ISA_MIPS32;
14644       break;
14645 
14646     case OPTION_MIPS32R2:
14647       file_mips_opts.isa = ISA_MIPS32R2;
14648       break;
14649 
14650     case OPTION_MIPS32R3:
14651       file_mips_opts.isa = ISA_MIPS32R3;
14652       break;
14653 
14654     case OPTION_MIPS32R5:
14655       file_mips_opts.isa = ISA_MIPS32R5;
14656       break;
14657 
14658     case OPTION_MIPS32R6:
14659       file_mips_opts.isa = ISA_MIPS32R6;
14660       break;
14661 
14662     case OPTION_MIPS64R2:
14663       file_mips_opts.isa = ISA_MIPS64R2;
14664       break;
14665 
14666     case OPTION_MIPS64R3:
14667       file_mips_opts.isa = ISA_MIPS64R3;
14668       break;
14669 
14670     case OPTION_MIPS64R5:
14671       file_mips_opts.isa = ISA_MIPS64R5;
14672       break;
14673 
14674     case OPTION_MIPS64R6:
14675       file_mips_opts.isa = ISA_MIPS64R6;
14676       break;
14677 
14678     case OPTION_MIPS64:
14679       file_mips_opts.isa = ISA_MIPS64;
14680       break;
14681 
14682     case OPTION_MTUNE:
14683       mips_set_option_string (&mips_tune_string, arg);
14684       break;
14685 
14686     case OPTION_MARCH:
14687       mips_set_option_string (&mips_arch_string, arg);
14688       break;
14689 
14690     case OPTION_M4650:
14691       mips_set_option_string (&mips_arch_string, "4650");
14692       mips_set_option_string (&mips_tune_string, "4650");
14693       break;
14694 
14695     case OPTION_NO_M4650:
14696       break;
14697 
14698     case OPTION_M4010:
14699       mips_set_option_string (&mips_arch_string, "4010");
14700       mips_set_option_string (&mips_tune_string, "4010");
14701       break;
14702 
14703     case OPTION_NO_M4010:
14704       break;
14705 
14706     case OPTION_M4100:
14707       mips_set_option_string (&mips_arch_string, "4100");
14708       mips_set_option_string (&mips_tune_string, "4100");
14709       break;
14710 
14711     case OPTION_NO_M4100:
14712       break;
14713 
14714     case OPTION_M3900:
14715       mips_set_option_string (&mips_arch_string, "3900");
14716       mips_set_option_string (&mips_tune_string, "3900");
14717       break;
14718 
14719     case OPTION_NO_M3900:
14720       break;
14721 
14722     case OPTION_MICROMIPS:
14723       if (file_mips_opts.mips16 == 1)
14724 	{
14725 	  as_bad (_("-mmicromips cannot be used with -mips16"));
14726 	  return 0;
14727 	}
14728       file_mips_opts.micromips = 1;
14729       mips_no_prev_insn ();
14730       break;
14731 
14732     case OPTION_NO_MICROMIPS:
14733       file_mips_opts.micromips = 0;
14734       mips_no_prev_insn ();
14735       break;
14736 
14737     case OPTION_MIPS16:
14738       if (file_mips_opts.micromips == 1)
14739 	{
14740 	  as_bad (_("-mips16 cannot be used with -micromips"));
14741 	  return 0;
14742 	}
14743       file_mips_opts.mips16 = 1;
14744       mips_no_prev_insn ();
14745       break;
14746 
14747     case OPTION_NO_MIPS16:
14748       file_mips_opts.mips16 = 0;
14749       mips_no_prev_insn ();
14750       break;
14751 
14752     case OPTION_FIX_24K:
14753       mips_fix_24k = 1;
14754       break;
14755 
14756     case OPTION_NO_FIX_24K:
14757       mips_fix_24k = 0;
14758       break;
14759 
14760     case OPTION_FIX_RM7000:
14761       mips_fix_rm7000 = 1;
14762       break;
14763 
14764     case OPTION_NO_FIX_RM7000:
14765       mips_fix_rm7000 = 0;
14766       break;
14767 
14768     case OPTION_FIX_LOONGSON2F_JUMP:
14769       mips_fix_loongson2f_jump = TRUE;
14770       break;
14771 
14772     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14773       mips_fix_loongson2f_jump = FALSE;
14774       break;
14775 
14776     case OPTION_FIX_LOONGSON2F_NOP:
14777       mips_fix_loongson2f_nop = TRUE;
14778       break;
14779 
14780     case OPTION_NO_FIX_LOONGSON2F_NOP:
14781       mips_fix_loongson2f_nop = FALSE;
14782       break;
14783 
14784     case OPTION_FIX_VR4120:
14785       mips_fix_vr4120 = 1;
14786       break;
14787 
14788     case OPTION_NO_FIX_VR4120:
14789       mips_fix_vr4120 = 0;
14790       break;
14791 
14792     case OPTION_FIX_VR4130:
14793       mips_fix_vr4130 = 1;
14794       break;
14795 
14796     case OPTION_NO_FIX_VR4130:
14797       mips_fix_vr4130 = 0;
14798       break;
14799 
14800     case OPTION_FIX_LOONGSON2F_BTB:
14801       mips_fix_loongson2f_btb = 1;
14802       break;
14803 
14804     case OPTION_NO_FIX_LOONGSON2F_BTB:
14805       mips_fix_loongson2f_btb = 0;
14806       break;
14807 
14808     case OPTION_FIX_CN63XXP1:
14809       mips_fix_cn63xxp1 = TRUE;
14810       break;
14811 
14812     case OPTION_NO_FIX_CN63XXP1:
14813       mips_fix_cn63xxp1 = FALSE;
14814       break;
14815 
14816     case OPTION_RELAX_BRANCH:
14817       mips_relax_branch = 1;
14818       break;
14819 
14820     case OPTION_NO_RELAX_BRANCH:
14821       mips_relax_branch = 0;
14822       break;
14823 
14824     case OPTION_IGNORE_BRANCH_ISA:
14825       mips_ignore_branch_isa = TRUE;
14826       break;
14827 
14828     case OPTION_NO_IGNORE_BRANCH_ISA:
14829       mips_ignore_branch_isa = FALSE;
14830       break;
14831 
14832     case OPTION_INSN32:
14833       file_mips_opts.insn32 = TRUE;
14834       break;
14835 
14836     case OPTION_NO_INSN32:
14837       file_mips_opts.insn32 = FALSE;
14838       break;
14839 
14840     case OPTION_MSHARED:
14841       mips_in_shared = TRUE;
14842       break;
14843 
14844     case OPTION_MNO_SHARED:
14845       mips_in_shared = FALSE;
14846       break;
14847 
14848     case OPTION_MSYM32:
14849       file_mips_opts.sym32 = TRUE;
14850       break;
14851 
14852     case OPTION_MNO_SYM32:
14853       file_mips_opts.sym32 = FALSE;
14854       break;
14855 
14856       /* When generating ELF code, we permit -KPIC and -call_shared to
14857 	 select SVR4_PIC, and -non_shared to select no PIC.  This is
14858 	 intended to be compatible with Irix 5.  */
14859     case OPTION_CALL_SHARED:
14860       mips_pic = SVR4_PIC;
14861       mips_abicalls = TRUE;
14862       break;
14863 
14864     case OPTION_CALL_NONPIC:
14865       mips_pic = NO_PIC;
14866       mips_abicalls = TRUE;
14867       break;
14868 
14869     case OPTION_NON_SHARED:
14870       mips_pic = NO_PIC;
14871       mips_abicalls = FALSE;
14872       break;
14873 
14874       /* The -xgot option tells the assembler to use 32 bit offsets
14875          when accessing the got in SVR4_PIC mode.  It is for Irix
14876          compatibility.  */
14877     case OPTION_XGOT:
14878       mips_big_got = 1;
14879       break;
14880 
14881     case 'G':
14882       g_switch_value = atoi (arg);
14883       g_switch_seen = 1;
14884       break;
14885 
14886       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14887 	 and -mabi=64.  */
14888     case OPTION_32:
14889       mips_abi = O32_ABI;
14890       break;
14891 
14892     case OPTION_N32:
14893       mips_abi = N32_ABI;
14894       break;
14895 
14896     case OPTION_64:
14897       mips_abi = N64_ABI;
14898       if (!support_64bit_objects())
14899 	as_fatal (_("no compiled in support for 64 bit object file format"));
14900       break;
14901 
14902     case OPTION_GP32:
14903       file_mips_opts.gp = 32;
14904       break;
14905 
14906     case OPTION_GP64:
14907       file_mips_opts.gp = 64;
14908       break;
14909 
14910     case OPTION_FP32:
14911       file_mips_opts.fp = 32;
14912       break;
14913 
14914     case OPTION_FPXX:
14915       file_mips_opts.fp = 0;
14916       break;
14917 
14918     case OPTION_FP64:
14919       file_mips_opts.fp = 64;
14920       break;
14921 
14922     case OPTION_ODD_SPREG:
14923       file_mips_opts.oddspreg = 1;
14924       break;
14925 
14926     case OPTION_NO_ODD_SPREG:
14927       file_mips_opts.oddspreg = 0;
14928       break;
14929 
14930     case OPTION_SINGLE_FLOAT:
14931       file_mips_opts.single_float = 1;
14932       break;
14933 
14934     case OPTION_DOUBLE_FLOAT:
14935       file_mips_opts.single_float = 0;
14936       break;
14937 
14938     case OPTION_SOFT_FLOAT:
14939       file_mips_opts.soft_float = 1;
14940       break;
14941 
14942     case OPTION_HARD_FLOAT:
14943       file_mips_opts.soft_float = 0;
14944       break;
14945 
14946     case OPTION_MABI:
14947       if (strcmp (arg, "32") == 0)
14948 	mips_abi = O32_ABI;
14949       else if (strcmp (arg, "o64") == 0)
14950 	mips_abi = O64_ABI;
14951       else if (strcmp (arg, "n32") == 0)
14952 	mips_abi = N32_ABI;
14953       else if (strcmp (arg, "64") == 0)
14954 	{
14955 	  mips_abi = N64_ABI;
14956 	  if (! support_64bit_objects())
14957 	    as_fatal (_("no compiled in support for 64 bit object file "
14958 			"format"));
14959 	}
14960       else if (strcmp (arg, "eabi") == 0)
14961 	mips_abi = EABI_ABI;
14962       else
14963 	{
14964 	  as_fatal (_("invalid abi -mabi=%s"), arg);
14965 	  return 0;
14966 	}
14967       break;
14968 
14969     case OPTION_M7000_HILO_FIX:
14970       mips_7000_hilo_fix = TRUE;
14971       break;
14972 
14973     case OPTION_MNO_7000_HILO_FIX:
14974       mips_7000_hilo_fix = FALSE;
14975       break;
14976 
14977     case OPTION_MDEBUG:
14978       mips_flag_mdebug = TRUE;
14979       break;
14980 
14981     case OPTION_NO_MDEBUG:
14982       mips_flag_mdebug = FALSE;
14983       break;
14984 
14985     case OPTION_PDR:
14986       mips_flag_pdr = TRUE;
14987       break;
14988 
14989     case OPTION_NO_PDR:
14990       mips_flag_pdr = FALSE;
14991       break;
14992 
14993     case OPTION_MVXWORKS_PIC:
14994       mips_pic = VXWORKS_PIC;
14995       break;
14996 
14997     case OPTION_NAN:
14998       if (strcmp (arg, "2008") == 0)
14999 	mips_nan2008 = 1;
15000       else if (strcmp (arg, "legacy") == 0)
15001 	mips_nan2008 = 0;
15002       else
15003 	{
15004 	  as_fatal (_("invalid NaN setting -mnan=%s"), arg);
15005 	  return 0;
15006 	}
15007       break;
15008 
15009     default:
15010       return 0;
15011     }
15012 
15013     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15014 
15015   return 1;
15016 }
15017 
15018 /* Set up globals to tune for the ISA or processor described by INFO.  */
15019 
15020 static void
15021 mips_set_tune (const struct mips_cpu_info *info)
15022 {
15023   if (info != 0)
15024     mips_tune = info->cpu;
15025 }
15026 
15027 
15028 void
15029 mips_after_parse_args (void)
15030 {
15031   const struct mips_cpu_info *arch_info = 0;
15032   const struct mips_cpu_info *tune_info = 0;
15033 
15034   /* GP relative stuff not working for PE */
15035   if (strncmp (TARGET_OS, "pe", 2) == 0)
15036     {
15037       if (g_switch_seen && g_switch_value != 0)
15038 	as_bad (_("-G not supported in this configuration"));
15039       g_switch_value = 0;
15040     }
15041 
15042   if (mips_abi == NO_ABI)
15043     mips_abi = MIPS_DEFAULT_ABI;
15044 
15045   /* The following code determines the architecture.
15046      Similar code was added to GCC 3.3 (see override_options() in
15047      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15048      as much as possible.  */
15049 
15050   if (mips_arch_string != 0)
15051     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15052 
15053   if (file_mips_opts.isa != ISA_UNKNOWN)
15054     {
15055       /* Handle -mipsN.  At this point, file_mips_opts.isa contains the
15056 	 ISA level specified by -mipsN, while arch_info->isa contains
15057 	 the -march selection (if any).  */
15058       if (arch_info != 0)
15059 	{
15060 	  /* -march takes precedence over -mipsN, since it is more descriptive.
15061 	     There's no harm in specifying both as long as the ISA levels
15062 	     are the same.  */
15063 	  if (file_mips_opts.isa != arch_info->isa)
15064 	    as_bad (_("-%s conflicts with the other architecture options,"
15065 		      " which imply -%s"),
15066 		    mips_cpu_info_from_isa (file_mips_opts.isa)->name,
15067 		    mips_cpu_info_from_isa (arch_info->isa)->name);
15068 	}
15069       else
15070 	arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
15071     }
15072 
15073   if (arch_info == 0)
15074     {
15075       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15076       gas_assert (arch_info);
15077     }
15078 
15079   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15080     as_bad (_("-march=%s is not compatible with the selected ABI"),
15081 	    arch_info->name);
15082 
15083   file_mips_opts.arch = arch_info->cpu;
15084   file_mips_opts.isa = arch_info->isa;
15085 
15086   /* Set up initial mips_opts state.  */
15087   mips_opts = file_mips_opts;
15088 
15089   /* The register size inference code is now placed in
15090      file_mips_check_options.  */
15091 
15092   /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15093      processor.  */
15094   if (mips_tune_string != 0)
15095     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15096 
15097   if (tune_info == 0)
15098     mips_set_tune (arch_info);
15099   else
15100     mips_set_tune (tune_info);
15101 
15102   if (mips_flag_mdebug < 0)
15103     mips_flag_mdebug = 0;
15104 }
15105 
15106 void
15107 mips_init_after_args (void)
15108 {
15109   /* initialize opcodes */
15110   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15111   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15112 }
15113 
15114 long
15115 md_pcrel_from (fixS *fixP)
15116 {
15117   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15118   switch (fixP->fx_r_type)
15119     {
15120     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15121     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15122       /* Return the address of the delay slot.  */
15123       return addr + 2;
15124 
15125     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15126     case BFD_RELOC_MICROMIPS_JMP:
15127     case BFD_RELOC_MIPS16_16_PCREL_S1:
15128     case BFD_RELOC_16_PCREL_S2:
15129     case BFD_RELOC_MIPS_21_PCREL_S2:
15130     case BFD_RELOC_MIPS_26_PCREL_S2:
15131     case BFD_RELOC_MIPS_JMP:
15132       /* Return the address of the delay slot.  */
15133       return addr + 4;
15134 
15135     case BFD_RELOC_MIPS_18_PCREL_S3:
15136       /* Return the aligned address of the doubleword containing
15137          the instruction.  */
15138       return addr & ~7;
15139 
15140     default:
15141       return addr;
15142     }
15143 }
15144 
15145 /* This is called before the symbol table is processed.  In order to
15146    work with gcc when using mips-tfile, we must keep all local labels.
15147    However, in other cases, we want to discard them.  If we were
15148    called with -g, but we didn't see any debugging information, it may
15149    mean that gcc is smuggling debugging information through to
15150    mips-tfile, in which case we must generate all local labels.  */
15151 
15152 void
15153 mips_frob_file_before_adjust (void)
15154 {
15155 #ifndef NO_ECOFF_DEBUGGING
15156   if (ECOFF_DEBUGGING
15157       && mips_debug != 0
15158       && ! ecoff_debugging_seen)
15159     flag_keep_locals = 1;
15160 #endif
15161 }
15162 
15163 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15164    the corresponding LO16 reloc.  This is called before md_apply_fix and
15165    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15166    relocation operators.
15167 
15168    For our purposes, a %lo() expression matches a %got() or %hi()
15169    expression if:
15170 
15171       (a) it refers to the same symbol; and
15172       (b) the offset applied in the %lo() expression is no lower than
15173 	  the offset applied in the %got() or %hi().
15174 
15175    (b) allows us to cope with code like:
15176 
15177 	lui	$4,%hi(foo)
15178 	lh	$4,%lo(foo+2)($4)
15179 
15180    ...which is legal on RELA targets, and has a well-defined behaviour
15181    if the user knows that adding 2 to "foo" will not induce a carry to
15182    the high 16 bits.
15183 
15184    When several %lo()s match a particular %got() or %hi(), we use the
15185    following rules to distinguish them:
15186 
15187      (1) %lo()s with smaller offsets are a better match than %lo()s with
15188          higher offsets.
15189 
15190      (2) %lo()s with no matching %got() or %hi() are better than those
15191          that already have a matching %got() or %hi().
15192 
15193      (3) later %lo()s are better than earlier %lo()s.
15194 
15195    These rules are applied in order.
15196 
15197    (1) means, among other things, that %lo()s with identical offsets are
15198    chosen if they exist.
15199 
15200    (2) means that we won't associate several high-part relocations with
15201    the same low-part relocation unless there's no alternative.  Having
15202    several high parts for the same low part is a GNU extension; this rule
15203    allows careful users to avoid it.
15204 
15205    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15206    with the last high-part relocation being at the front of the list.
15207    It therefore makes sense to choose the last matching low-part
15208    relocation, all other things being equal.  It's also easier
15209    to code that way.  */
15210 
15211 void
15212 mips_frob_file (void)
15213 {
15214   struct mips_hi_fixup *l;
15215   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15216 
15217   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15218     {
15219       segment_info_type *seginfo;
15220       bfd_boolean matched_lo_p;
15221       fixS **hi_pos, **lo_pos, **pos;
15222 
15223       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15224 
15225       /* If a GOT16 relocation turns out to be against a global symbol,
15226 	 there isn't supposed to be a matching LO.  Ignore %gots against
15227 	 constants; we'll report an error for those later.  */
15228       if (got16_reloc_p (l->fixp->fx_r_type)
15229 	  && !(l->fixp->fx_addsy
15230 	       && pic_need_relax (l->fixp->fx_addsy)))
15231 	continue;
15232 
15233       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15234       if (fixup_has_matching_lo_p (l->fixp))
15235 	continue;
15236 
15237       seginfo = seg_info (l->seg);
15238 
15239       /* Set HI_POS to the position of this relocation in the chain.
15240 	 Set LO_POS to the position of the chosen low-part relocation.
15241 	 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15242 	 relocation that matches an immediately-preceding high-part
15243 	 relocation.  */
15244       hi_pos = NULL;
15245       lo_pos = NULL;
15246       matched_lo_p = FALSE;
15247       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15248 
15249       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15250 	{
15251 	  if (*pos == l->fixp)
15252 	    hi_pos = pos;
15253 
15254 	  if ((*pos)->fx_r_type == looking_for_rtype
15255 	      && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15256 	      && (*pos)->fx_offset >= l->fixp->fx_offset
15257 	      && (lo_pos == NULL
15258 		  || (*pos)->fx_offset < (*lo_pos)->fx_offset
15259 		  || (!matched_lo_p
15260 		      && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15261 	    lo_pos = pos;
15262 
15263 	  matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15264 			  && fixup_has_matching_lo_p (*pos));
15265 	}
15266 
15267       /* If we found a match, remove the high-part relocation from its
15268 	 current position and insert it before the low-part relocation.
15269 	 Make the offsets match so that fixup_has_matching_lo_p()
15270 	 will return true.
15271 
15272 	 We don't warn about unmatched high-part relocations since some
15273 	 versions of gcc have been known to emit dead "lui ...%hi(...)"
15274 	 instructions.  */
15275       if (lo_pos != NULL)
15276 	{
15277 	  l->fixp->fx_offset = (*lo_pos)->fx_offset;
15278 	  if (l->fixp->fx_next != *lo_pos)
15279 	    {
15280 	      *hi_pos = l->fixp->fx_next;
15281 	      l->fixp->fx_next = *lo_pos;
15282 	      *lo_pos = l->fixp;
15283 	    }
15284 	}
15285     }
15286 }
15287 
15288 int
15289 mips_force_relocation (fixS *fixp)
15290 {
15291   if (generic_force_reloc (fixp))
15292     return 1;
15293 
15294   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15295      so that the linker relaxation can update targets.  */
15296   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15297       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15298       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15299     return 1;
15300 
15301   /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15302      and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15303      microMIPS symbols so that we can do cross-mode branch diagnostics
15304      and BAL to JALX conversion by the linker.  */
15305   if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15306        || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15307        || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15308       && fixp->fx_addsy
15309       && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15310     return 1;
15311 
15312   /* We want all PC-relative relocations to be kept for R6 relaxation.  */
15313   if (ISA_IS_R6 (file_mips_opts.isa)
15314       && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15315 	  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15316 	  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15317 	  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15318 	  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15319 	  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15320 	  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15321     return 1;
15322 
15323   return 0;
15324 }
15325 
15326 /* Implement TC_FORCE_RELOCATION_ABS.  */
15327 
15328 bfd_boolean
15329 mips_force_relocation_abs (fixS *fixp)
15330 {
15331   if (generic_force_reloc (fixp))
15332     return TRUE;
15333 
15334   /* These relocations do not have enough bits in the in-place addend
15335      to hold an arbitrary absolute section's offset.  */
15336   if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15337     return TRUE;
15338 
15339   return FALSE;
15340 }
15341 
15342 /* Read the instruction associated with RELOC from BUF.  */
15343 
15344 static unsigned int
15345 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15346 {
15347   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15348     return read_compressed_insn (buf, 4);
15349   else
15350     return read_insn (buf);
15351 }
15352 
15353 /* Write instruction INSN to BUF, given that it has been relocated
15354    by RELOC.  */
15355 
15356 static void
15357 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15358 		  unsigned long insn)
15359 {
15360   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15361     write_compressed_insn (buf, insn, 4);
15362   else
15363     write_insn (buf, insn);
15364 }
15365 
15366 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15367    to a symbol in another ISA mode, which cannot be converted to JALX.  */
15368 
15369 static bfd_boolean
15370 fix_bad_cross_mode_jump_p (fixS *fixP)
15371 {
15372   unsigned long opcode;
15373   int other;
15374   char *buf;
15375 
15376   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15377     return FALSE;
15378 
15379   other = S_GET_OTHER (fixP->fx_addsy);
15380   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15381   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15382   switch (fixP->fx_r_type)
15383     {
15384     case BFD_RELOC_MIPS_JMP:
15385       return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15386     case BFD_RELOC_MICROMIPS_JMP:
15387       return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15388     default:
15389       return FALSE;
15390     }
15391 }
15392 
15393 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15394    jump to a symbol in the same ISA mode.  */
15395 
15396 static bfd_boolean
15397 fix_bad_same_mode_jalx_p (fixS *fixP)
15398 {
15399   unsigned long opcode;
15400   int other;
15401   char *buf;
15402 
15403   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15404     return FALSE;
15405 
15406   other = S_GET_OTHER (fixP->fx_addsy);
15407   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15408   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15409   switch (fixP->fx_r_type)
15410     {
15411     case BFD_RELOC_MIPS_JMP:
15412       return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15413     case BFD_RELOC_MIPS16_JMP:
15414       return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15415     case BFD_RELOC_MICROMIPS_JMP:
15416       return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15417     default:
15418       return FALSE;
15419     }
15420 }
15421 
15422 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15423    to a symbol whose value plus addend is not aligned according to the
15424    ultimate (after linker relaxation) jump instruction's immediate field
15425    requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15426    regular MIPS code, to (1 << 2).  */
15427 
15428 static bfd_boolean
15429 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15430 {
15431   bfd_boolean micro_to_mips_p;
15432   valueT val;
15433   int other;
15434 
15435   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15436     return FALSE;
15437 
15438   other = S_GET_OTHER (fixP->fx_addsy);
15439   val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15440   val += fixP->fx_offset;
15441   micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15442 		     && !ELF_ST_IS_MICROMIPS (other));
15443   return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15444 	  != ELF_ST_IS_COMPRESSED (other));
15445 }
15446 
15447 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15448    to a symbol whose annotation indicates another ISA mode.  For absolute
15449    symbols check the ISA bit instead.
15450 
15451    We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15452    symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15453    MIPS symbols and associated with BAL instructions as these instructions
15454    may be converted to JALX by the linker.  */
15455 
15456 static bfd_boolean
15457 fix_bad_cross_mode_branch_p (fixS *fixP)
15458 {
15459   bfd_boolean absolute_p;
15460   unsigned long opcode;
15461   asection *symsec;
15462   valueT val;
15463   int other;
15464   char *buf;
15465 
15466   if (mips_ignore_branch_isa)
15467     return FALSE;
15468 
15469   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15470     return FALSE;
15471 
15472   symsec = S_GET_SEGMENT (fixP->fx_addsy);
15473   absolute_p = bfd_is_abs_section (symsec);
15474 
15475   val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15476   other = S_GET_OTHER (fixP->fx_addsy);
15477 
15478   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15479   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15480   switch (fixP->fx_r_type)
15481     {
15482     case BFD_RELOC_16_PCREL_S2:
15483       return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15484 	      && opcode != 0x0411);
15485     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15486       return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15487 	      && opcode != 0x4060);
15488     case BFD_RELOC_MIPS_21_PCREL_S2:
15489     case BFD_RELOC_MIPS_26_PCREL_S2:
15490       return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15491     case BFD_RELOC_MIPS16_16_PCREL_S1:
15492       return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15493     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15494     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15495       return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15496     default:
15497       abort ();
15498     }
15499 }
15500 
15501 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15502    branch instruction pointed to by FIXP is not aligned according to the
15503    branch instruction's immediate field requirement.  We need the addend
15504    to preserve the ISA bit and also the sum must not have bit 2 set.  We
15505    must explicitly OR in the ISA bit from symbol annotation as the bit
15506    won't be set in the symbol's value then.  */
15507 
15508 static bfd_boolean
15509 fix_bad_misaligned_branch_p (fixS *fixP)
15510 {
15511   bfd_boolean absolute_p;
15512   asection *symsec;
15513   valueT isa_bit;
15514   valueT val;
15515   valueT off;
15516   int other;
15517 
15518   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15519     return FALSE;
15520 
15521   symsec = S_GET_SEGMENT (fixP->fx_addsy);
15522   absolute_p = bfd_is_abs_section (symsec);
15523 
15524   val = S_GET_VALUE (fixP->fx_addsy);
15525   other = S_GET_OTHER (fixP->fx_addsy);
15526   off = fixP->fx_offset;
15527 
15528   isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15529   val |= ELF_ST_IS_COMPRESSED (other);
15530   val += off;
15531   return (val & 0x3) != isa_bit;
15532 }
15533 
15534 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15535    and its calculated value VAL.  */
15536 
15537 static void
15538 fix_validate_branch (fixS *fixP, valueT val)
15539 {
15540   if (fixP->fx_done && (val & 0x3) != 0)
15541     as_bad_where (fixP->fx_file, fixP->fx_line,
15542 		  _("branch to misaligned address (0x%lx)"),
15543 		  (long) (val + md_pcrel_from (fixP)));
15544   else if (fix_bad_cross_mode_branch_p (fixP))
15545     as_bad_where (fixP->fx_file, fixP->fx_line,
15546 		  _("branch to a symbol in another ISA mode"));
15547   else if (fix_bad_misaligned_branch_p (fixP))
15548     as_bad_where (fixP->fx_file, fixP->fx_line,
15549 		  _("branch to misaligned address (0x%lx)"),
15550 		  (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15551   else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15552     as_bad_where (fixP->fx_file, fixP->fx_line,
15553 		  _("cannot encode misaligned addend "
15554 		    "in the relocatable field (0x%lx)"),
15555 		  (long) fixP->fx_offset);
15556 }
15557 
15558 /* Apply a fixup to the object file.  */
15559 
15560 void
15561 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15562 {
15563   char *buf;
15564   unsigned long insn;
15565   reloc_howto_type *howto;
15566 
15567   if (fixP->fx_pcrel)
15568     switch (fixP->fx_r_type)
15569       {
15570       case BFD_RELOC_16_PCREL_S2:
15571       case BFD_RELOC_MIPS16_16_PCREL_S1:
15572       case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15573       case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15574       case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15575       case BFD_RELOC_32_PCREL:
15576       case BFD_RELOC_MIPS_21_PCREL_S2:
15577       case BFD_RELOC_MIPS_26_PCREL_S2:
15578       case BFD_RELOC_MIPS_18_PCREL_S3:
15579       case BFD_RELOC_MIPS_19_PCREL_S2:
15580       case BFD_RELOC_HI16_S_PCREL:
15581       case BFD_RELOC_LO16_PCREL:
15582 	break;
15583 
15584       case BFD_RELOC_32:
15585 	fixP->fx_r_type = BFD_RELOC_32_PCREL;
15586 	break;
15587 
15588       default:
15589 	as_bad_where (fixP->fx_file, fixP->fx_line,
15590 		      _("PC-relative reference to a different section"));
15591 	break;
15592       }
15593 
15594   /* Handle BFD_RELOC_8, since it's easy.  Punt on other bfd relocations
15595      that have no MIPS ELF equivalent.  */
15596   if (fixP->fx_r_type != BFD_RELOC_8)
15597     {
15598       howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15599       if (!howto)
15600 	return;
15601     }
15602 
15603   gas_assert (fixP->fx_size == 2
15604 	      || fixP->fx_size == 4
15605 	      || fixP->fx_r_type == BFD_RELOC_8
15606 	      || fixP->fx_r_type == BFD_RELOC_16
15607 	      || fixP->fx_r_type == BFD_RELOC_64
15608 	      || fixP->fx_r_type == BFD_RELOC_CTOR
15609 	      || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15610 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15611 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15612 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15613 	      || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15614 	      || fixP->fx_r_type == BFD_RELOC_NONE);
15615 
15616   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15617 
15618   /* Don't treat parts of a composite relocation as done.  There are two
15619      reasons for this:
15620 
15621      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15622 	 should nevertheless be emitted if the first part is.
15623 
15624      (2) In normal usage, composite relocations are never assembly-time
15625 	 constants.  The easiest way of dealing with the pathological
15626 	 exceptions is to generate a relocation against STN_UNDEF and
15627 	 leave everything up to the linker.  */
15628   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15629     fixP->fx_done = 1;
15630 
15631   switch (fixP->fx_r_type)
15632     {
15633     case BFD_RELOC_MIPS_TLS_GD:
15634     case BFD_RELOC_MIPS_TLS_LDM:
15635     case BFD_RELOC_MIPS_TLS_DTPREL32:
15636     case BFD_RELOC_MIPS_TLS_DTPREL64:
15637     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15638     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15639     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15640     case BFD_RELOC_MIPS_TLS_TPREL32:
15641     case BFD_RELOC_MIPS_TLS_TPREL64:
15642     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15643     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15644     case BFD_RELOC_MICROMIPS_TLS_GD:
15645     case BFD_RELOC_MICROMIPS_TLS_LDM:
15646     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15647     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15648     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15649     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15650     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15651     case BFD_RELOC_MIPS16_TLS_GD:
15652     case BFD_RELOC_MIPS16_TLS_LDM:
15653     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15654     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15655     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15656     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15657     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
15658       if (fixP->fx_addsy)
15659 	S_SET_THREAD_LOCAL (fixP->fx_addsy);
15660       else
15661 	as_bad_where (fixP->fx_file, fixP->fx_line,
15662 		      _("TLS relocation against a constant"));
15663       break;
15664 
15665     case BFD_RELOC_MIPS_JMP:
15666     case BFD_RELOC_MIPS16_JMP:
15667     case BFD_RELOC_MICROMIPS_JMP:
15668       {
15669 	int shift;
15670 
15671 	gas_assert (!fixP->fx_done);
15672 
15673 	/* Shift is 2, unusually, for microMIPS JALX.  */
15674 	if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15675 	    && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15676 	  shift = 1;
15677 	else
15678 	  shift = 2;
15679 
15680 	if (fix_bad_cross_mode_jump_p (fixP))
15681 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15682 			_("jump to a symbol in another ISA mode"));
15683 	else if (fix_bad_same_mode_jalx_p (fixP))
15684 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15685 			_("JALX to a symbol in the same ISA mode"));
15686 	else if (fix_bad_misaligned_jump_p (fixP, shift))
15687 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15688 			_("jump to misaligned address (0x%lx)"),
15689 			(long) (S_GET_VALUE (fixP->fx_addsy)
15690 				+ fixP->fx_offset));
15691 	else if (HAVE_IN_PLACE_ADDENDS
15692 		 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15693 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15694 			_("cannot encode misaligned addend "
15695 			  "in the relocatable field (0x%lx)"),
15696 			(long) fixP->fx_offset);
15697       }
15698       /* Fall through.  */
15699 
15700     case BFD_RELOC_MIPS_SHIFT5:
15701     case BFD_RELOC_MIPS_SHIFT6:
15702     case BFD_RELOC_MIPS_GOT_DISP:
15703     case BFD_RELOC_MIPS_GOT_PAGE:
15704     case BFD_RELOC_MIPS_GOT_OFST:
15705     case BFD_RELOC_MIPS_SUB:
15706     case BFD_RELOC_MIPS_INSERT_A:
15707     case BFD_RELOC_MIPS_INSERT_B:
15708     case BFD_RELOC_MIPS_DELETE:
15709     case BFD_RELOC_MIPS_HIGHEST:
15710     case BFD_RELOC_MIPS_HIGHER:
15711     case BFD_RELOC_MIPS_SCN_DISP:
15712     case BFD_RELOC_MIPS_REL16:
15713     case BFD_RELOC_MIPS_RELGOT:
15714     case BFD_RELOC_MIPS_JALR:
15715     case BFD_RELOC_HI16:
15716     case BFD_RELOC_HI16_S:
15717     case BFD_RELOC_LO16:
15718     case BFD_RELOC_GPREL16:
15719     case BFD_RELOC_MIPS_LITERAL:
15720     case BFD_RELOC_MIPS_CALL16:
15721     case BFD_RELOC_MIPS_GOT16:
15722     case BFD_RELOC_GPREL32:
15723     case BFD_RELOC_MIPS_GOT_HI16:
15724     case BFD_RELOC_MIPS_GOT_LO16:
15725     case BFD_RELOC_MIPS_CALL_HI16:
15726     case BFD_RELOC_MIPS_CALL_LO16:
15727     case BFD_RELOC_HI16_S_PCREL:
15728     case BFD_RELOC_LO16_PCREL:
15729     case BFD_RELOC_MIPS16_GPREL:
15730     case BFD_RELOC_MIPS16_GOT16:
15731     case BFD_RELOC_MIPS16_CALL16:
15732     case BFD_RELOC_MIPS16_HI16:
15733     case BFD_RELOC_MIPS16_HI16_S:
15734     case BFD_RELOC_MIPS16_LO16:
15735     case BFD_RELOC_MICROMIPS_GOT_DISP:
15736     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15737     case BFD_RELOC_MICROMIPS_GOT_OFST:
15738     case BFD_RELOC_MICROMIPS_SUB:
15739     case BFD_RELOC_MICROMIPS_HIGHEST:
15740     case BFD_RELOC_MICROMIPS_HIGHER:
15741     case BFD_RELOC_MICROMIPS_SCN_DISP:
15742     case BFD_RELOC_MICROMIPS_JALR:
15743     case BFD_RELOC_MICROMIPS_HI16:
15744     case BFD_RELOC_MICROMIPS_HI16_S:
15745     case BFD_RELOC_MICROMIPS_LO16:
15746     case BFD_RELOC_MICROMIPS_GPREL16:
15747     case BFD_RELOC_MICROMIPS_LITERAL:
15748     case BFD_RELOC_MICROMIPS_CALL16:
15749     case BFD_RELOC_MICROMIPS_GOT16:
15750     case BFD_RELOC_MICROMIPS_GOT_HI16:
15751     case BFD_RELOC_MICROMIPS_GOT_LO16:
15752     case BFD_RELOC_MICROMIPS_CALL_HI16:
15753     case BFD_RELOC_MICROMIPS_CALL_LO16:
15754     case BFD_RELOC_MIPS_EH:
15755       if (fixP->fx_done)
15756 	{
15757 	  offsetT value;
15758 
15759 	  if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15760 	    {
15761 	      insn = read_reloc_insn (buf, fixP->fx_r_type);
15762 	      if (mips16_reloc_p (fixP->fx_r_type))
15763 		insn |= mips16_immed_extend (value, 16);
15764 	      else
15765 		insn |= (value & 0xffff);
15766 	      write_reloc_insn (buf, fixP->fx_r_type, insn);
15767 	    }
15768 	  else
15769 	    as_bad_where (fixP->fx_file, fixP->fx_line,
15770 			  _("unsupported constant in relocation"));
15771 	}
15772       break;
15773 
15774     case BFD_RELOC_64:
15775       /* This is handled like BFD_RELOC_32, but we output a sign
15776          extended value if we are only 32 bits.  */
15777       if (fixP->fx_done)
15778 	{
15779 	  if (8 <= sizeof (valueT))
15780 	    md_number_to_chars (buf, *valP, 8);
15781 	  else
15782 	    {
15783 	      valueT hiv;
15784 
15785 	      if ((*valP & 0x80000000) != 0)
15786 		hiv = 0xffffffff;
15787 	      else
15788 		hiv = 0;
15789 	      md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15790 	      md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
15791 	    }
15792 	}
15793       break;
15794 
15795     case BFD_RELOC_RVA:
15796     case BFD_RELOC_32:
15797     case BFD_RELOC_32_PCREL:
15798     case BFD_RELOC_16:
15799     case BFD_RELOC_8:
15800       /* If we are deleting this reloc entry, we must fill in the
15801 	 value now.  This can happen if we have a .word which is not
15802 	 resolved when it appears but is later defined.  */
15803       if (fixP->fx_done)
15804 	md_number_to_chars (buf, *valP, fixP->fx_size);
15805       break;
15806 
15807     case BFD_RELOC_MIPS_21_PCREL_S2:
15808       fix_validate_branch (fixP, *valP);
15809       if (!fixP->fx_done)
15810 	break;
15811 
15812       if (*valP + 0x400000 <= 0x7fffff)
15813 	{
15814 	  insn = read_insn (buf);
15815 	  insn |= (*valP >> 2) & 0x1fffff;
15816 	  write_insn (buf, insn);
15817 	}
15818       else
15819 	as_bad_where (fixP->fx_file, fixP->fx_line,
15820 		      _("branch out of range"));
15821       break;
15822 
15823     case BFD_RELOC_MIPS_26_PCREL_S2:
15824       fix_validate_branch (fixP, *valP);
15825       if (!fixP->fx_done)
15826 	break;
15827 
15828       if (*valP + 0x8000000 <= 0xfffffff)
15829 	{
15830 	  insn = read_insn (buf);
15831 	  insn |= (*valP >> 2) & 0x3ffffff;
15832 	  write_insn (buf, insn);
15833 	}
15834       else
15835 	as_bad_where (fixP->fx_file, fixP->fx_line,
15836 		      _("branch out of range"));
15837       break;
15838 
15839     case BFD_RELOC_MIPS_18_PCREL_S3:
15840       if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
15841 	as_bad_where (fixP->fx_file, fixP->fx_line,
15842 		      _("PC-relative access using misaligned symbol (%lx)"),
15843 		      (long) S_GET_VALUE (fixP->fx_addsy));
15844       if ((fixP->fx_offset & 0x7) != 0)
15845 	as_bad_where (fixP->fx_file, fixP->fx_line,
15846 		      _("PC-relative access using misaligned offset (%lx)"),
15847 		      (long) fixP->fx_offset);
15848       if (!fixP->fx_done)
15849 	break;
15850 
15851       if (*valP + 0x100000 <= 0x1fffff)
15852 	{
15853 	  insn = read_insn (buf);
15854 	  insn |= (*valP >> 3) & 0x3ffff;
15855 	  write_insn (buf, insn);
15856 	}
15857       else
15858 	as_bad_where (fixP->fx_file, fixP->fx_line,
15859 		      _("PC-relative access out of range"));
15860       break;
15861 
15862     case BFD_RELOC_MIPS_19_PCREL_S2:
15863       if ((*valP & 0x3) != 0)
15864 	as_bad_where (fixP->fx_file, fixP->fx_line,
15865 		      _("PC-relative access to misaligned address (%lx)"),
15866 		      (long) *valP);
15867       if (!fixP->fx_done)
15868 	break;
15869 
15870       if (*valP + 0x100000 <= 0x1fffff)
15871 	{
15872 	  insn = read_insn (buf);
15873 	  insn |= (*valP >> 2) & 0x7ffff;
15874 	  write_insn (buf, insn);
15875 	}
15876       else
15877 	as_bad_where (fixP->fx_file, fixP->fx_line,
15878 		      _("PC-relative access out of range"));
15879       break;
15880 
15881     case BFD_RELOC_16_PCREL_S2:
15882       fix_validate_branch (fixP, *valP);
15883 
15884       /* We need to save the bits in the instruction since fixup_segment()
15885 	 might be deleting the relocation entry (i.e., a branch within
15886 	 the current segment).  */
15887       if (! fixP->fx_done)
15888 	break;
15889 
15890       /* Update old instruction data.  */
15891       insn = read_insn (buf);
15892 
15893       if (*valP + 0x20000 <= 0x3ffff)
15894 	{
15895 	  insn |= (*valP >> 2) & 0xffff;
15896 	  write_insn (buf, insn);
15897 	}
15898       else if (fixP->fx_tcbit2
15899 	       && fixP->fx_done
15900 	       && fixP->fx_frag->fr_address >= text_section->vma
15901 	       && (fixP->fx_frag->fr_address
15902 		   < text_section->vma + bfd_get_section_size (text_section))
15903 	       && ((insn & 0xffff0000) == 0x10000000	 /* beq $0,$0 */
15904 		   || (insn & 0xffff0000) == 0x04010000	 /* bgez $0 */
15905 		   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15906 	{
15907 	  /* The branch offset is too large.  If this is an
15908              unconditional branch, and we are not generating PIC code,
15909              we can convert it to an absolute jump instruction.  */
15910 	  if ((insn & 0xffff0000) == 0x04110000)	 /* bgezal $0 */
15911 	    insn = 0x0c000000;	/* jal */
15912 	  else
15913 	    insn = 0x08000000;	/* j */
15914 	  fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15915 	  fixP->fx_done = 0;
15916 	  fixP->fx_addsy = section_symbol (text_section);
15917 	  *valP += md_pcrel_from (fixP);
15918 	  write_insn (buf, insn);
15919 	}
15920       else
15921 	{
15922 	  /* If we got here, we have branch-relaxation disabled,
15923 	     and there's nothing we can do to fix this instruction
15924 	     without turning it into a longer sequence.  */
15925 	  as_bad_where (fixP->fx_file, fixP->fx_line,
15926 			_("branch out of range"));
15927 	}
15928       break;
15929 
15930     case BFD_RELOC_MIPS16_16_PCREL_S1:
15931     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15932     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15933     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15934       gas_assert (!fixP->fx_done);
15935       if (fix_bad_cross_mode_branch_p (fixP))
15936 	as_bad_where (fixP->fx_file, fixP->fx_line,
15937 		      _("branch to a symbol in another ISA mode"));
15938       else if (fixP->fx_addsy
15939 	       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15940 	       && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15941 	       && (fixP->fx_offset & 0x1) != 0)
15942 	as_bad_where (fixP->fx_file, fixP->fx_line,
15943 		      _("branch to misaligned address (0x%lx)"),
15944 		      (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15945       else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15946 	as_bad_where (fixP->fx_file, fixP->fx_line,
15947 		      _("cannot encode misaligned addend "
15948 			"in the relocatable field (0x%lx)"),
15949 		      (long) fixP->fx_offset);
15950       break;
15951 
15952     case BFD_RELOC_VTABLE_INHERIT:
15953       fixP->fx_done = 0;
15954       if (fixP->fx_addsy
15955           && !S_IS_DEFINED (fixP->fx_addsy)
15956           && !S_IS_WEAK (fixP->fx_addsy))
15957         S_SET_WEAK (fixP->fx_addsy);
15958       break;
15959 
15960     case BFD_RELOC_NONE:
15961     case BFD_RELOC_VTABLE_ENTRY:
15962       fixP->fx_done = 0;
15963       break;
15964 
15965     default:
15966       abort ();
15967     }
15968 
15969   /* Remember value for tc_gen_reloc.  */
15970   fixP->fx_addnumber = *valP;
15971 }
15972 
15973 static symbolS *
15974 get_symbol (void)
15975 {
15976   int c;
15977   char *name;
15978   symbolS *p;
15979 
15980   c = get_symbol_name (&name);
15981   p = (symbolS *) symbol_find_or_make (name);
15982   (void) restore_line_pointer (c);
15983   return p;
15984 }
15985 
15986 /* Align the current frag to a given power of two.  If a particular
15987    fill byte should be used, FILL points to an integer that contains
15988    that byte, otherwise FILL is null.
15989 
15990    This function used to have the comment:
15991 
15992       The MIPS assembler also automatically adjusts any preceding label.
15993 
15994    The implementation therefore applied the adjustment to a maximum of
15995    one label.  However, other label adjustments are applied to batches
15996    of labels, and adjusting just one caused problems when new labels
15997    were added for the sake of debugging or unwind information.
15998    We therefore adjust all preceding labels (given as LABELS) instead.  */
15999 
16000 static void
16001 mips_align (int to, int *fill, struct insn_label_list *labels)
16002 {
16003   mips_emit_delays ();
16004   mips_record_compressed_mode ();
16005   if (fill == NULL && subseg_text_p (now_seg))
16006     frag_align_code (to, 0);
16007   else
16008     frag_align (to, fill ? *fill : 0, 0);
16009   record_alignment (now_seg, to);
16010   mips_move_labels (labels, FALSE);
16011 }
16012 
16013 /* Align to a given power of two.  .align 0 turns off the automatic
16014    alignment used by the data creating pseudo-ops.  */
16015 
16016 static void
16017 s_align (int x ATTRIBUTE_UNUSED)
16018 {
16019   int temp, fill_value, *fill_ptr;
16020   long max_alignment = 28;
16021 
16022   /* o Note that the assembler pulls down any immediately preceding label
16023        to the aligned address.
16024      o It's not documented but auto alignment is reinstated by
16025        a .align pseudo instruction.
16026      o Note also that after auto alignment is turned off the mips assembler
16027        issues an error on attempt to assemble an improperly aligned data item.
16028        We don't.  */
16029 
16030   temp = get_absolute_expression ();
16031   if (temp > max_alignment)
16032     as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
16033   else if (temp < 0)
16034     {
16035       as_warn (_("alignment negative, 0 assumed"));
16036       temp = 0;
16037     }
16038   if (*input_line_pointer == ',')
16039     {
16040       ++input_line_pointer;
16041       fill_value = get_absolute_expression ();
16042       fill_ptr = &fill_value;
16043     }
16044   else
16045     fill_ptr = 0;
16046   if (temp)
16047     {
16048       segment_info_type *si = seg_info (now_seg);
16049       struct insn_label_list *l = si->label_list;
16050       /* Auto alignment should be switched on by next section change.  */
16051       auto_align = 1;
16052       mips_align (temp, fill_ptr, l);
16053     }
16054   else
16055     {
16056       auto_align = 0;
16057     }
16058 
16059   demand_empty_rest_of_line ();
16060 }
16061 
16062 static void
16063 s_change_sec (int sec)
16064 {
16065   segT seg;
16066 
16067   /* The ELF backend needs to know that we are changing sections, so
16068      that .previous works correctly.  We could do something like check
16069      for an obj_section_change_hook macro, but that might be confusing
16070      as it would not be appropriate to use it in the section changing
16071      functions in read.c, since obj-elf.c intercepts those.  FIXME:
16072      This should be cleaner, somehow.  */
16073   obj_elf_section_change_hook ();
16074 
16075   mips_emit_delays ();
16076 
16077   switch (sec)
16078     {
16079     case 't':
16080       s_text (0);
16081       break;
16082     case 'd':
16083       s_data (0);
16084       break;
16085     case 'b':
16086       subseg_set (bss_section, (subsegT) get_absolute_expression ());
16087       demand_empty_rest_of_line ();
16088       break;
16089 
16090     case 'r':
16091       seg = subseg_new (RDATA_SECTION_NAME,
16092 			(subsegT) get_absolute_expression ());
16093       bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16094 					      | SEC_READONLY | SEC_RELOC
16095 					      | SEC_DATA));
16096       if (strncmp (TARGET_OS, "elf", 3) != 0)
16097 	record_alignment (seg, 4);
16098       demand_empty_rest_of_line ();
16099       break;
16100 
16101     case 's':
16102       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16103       bfd_set_section_flags (stdoutput, seg,
16104 			     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16105       if (strncmp (TARGET_OS, "elf", 3) != 0)
16106 	record_alignment (seg, 4);
16107       demand_empty_rest_of_line ();
16108       break;
16109 
16110     case 'B':
16111       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16112       bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16113       if (strncmp (TARGET_OS, "elf", 3) != 0)
16114 	record_alignment (seg, 4);
16115       demand_empty_rest_of_line ();
16116       break;
16117     }
16118 
16119   auto_align = 1;
16120 }
16121 
16122 void
16123 s_change_section (int ignore ATTRIBUTE_UNUSED)
16124 {
16125   char *saved_ilp;
16126   char *section_name;
16127   char c, endc;
16128   char next_c = 0;
16129   int section_type;
16130   int section_flag;
16131   int section_entry_size;
16132   int section_alignment;
16133 
16134   saved_ilp = input_line_pointer;
16135   endc = get_symbol_name (&section_name);
16136   c = (endc == '"' ? input_line_pointer[1] : endc);
16137   if (c)
16138     next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
16139 
16140   /* Do we have .section Name<,"flags">?  */
16141   if (c != ',' || (c == ',' && next_c == '"'))
16142     {
16143       /* Just after name is now '\0'.  */
16144       (void) restore_line_pointer (endc);
16145       input_line_pointer = saved_ilp;
16146       obj_elf_section (ignore);
16147       return;
16148     }
16149 
16150   section_name = xstrdup (section_name);
16151   c = restore_line_pointer (endc);
16152 
16153   input_line_pointer++;
16154 
16155   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16156   if (c == ',')
16157     section_type = get_absolute_expression ();
16158   else
16159     section_type = 0;
16160 
16161   if (*input_line_pointer++ == ',')
16162     section_flag = get_absolute_expression ();
16163   else
16164     section_flag = 0;
16165 
16166   if (*input_line_pointer++ == ',')
16167     section_entry_size = get_absolute_expression ();
16168   else
16169     section_entry_size = 0;
16170 
16171   if (*input_line_pointer++ == ',')
16172     section_alignment = get_absolute_expression ();
16173   else
16174     section_alignment = 0;
16175 
16176   /* FIXME: really ignore?  */
16177   (void) section_alignment;
16178 
16179   /* When using the generic form of .section (as implemented by obj-elf.c),
16180      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16181      traditionally had to fall back on the more common @progbits instead.
16182 
16183      There's nothing really harmful in this, since bfd will correct
16184      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16185      means that, for backwards compatibility, the special_section entries
16186      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16187 
16188      Even so, we shouldn't force users of the MIPS .section syntax to
16189      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16190      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16191      generic type-checking code.  */
16192   if (section_type == SHT_MIPS_DWARF)
16193     section_type = SHT_PROGBITS;
16194 
16195   obj_elf_change_section (section_name, section_type, 0, section_flag,
16196 			  section_entry_size, 0, 0, 0);
16197 
16198   if (now_seg->name != section_name)
16199     free (section_name);
16200 }
16201 
16202 void
16203 mips_enable_auto_align (void)
16204 {
16205   auto_align = 1;
16206 }
16207 
16208 static void
16209 s_cons (int log_size)
16210 {
16211   segment_info_type *si = seg_info (now_seg);
16212   struct insn_label_list *l = si->label_list;
16213 
16214   mips_emit_delays ();
16215   if (log_size > 0 && auto_align)
16216     mips_align (log_size, 0, l);
16217   cons (1 << log_size);
16218   mips_clear_insn_labels ();
16219 }
16220 
16221 static void
16222 s_float_cons (int type)
16223 {
16224   segment_info_type *si = seg_info (now_seg);
16225   struct insn_label_list *l = si->label_list;
16226 
16227   mips_emit_delays ();
16228 
16229   if (auto_align)
16230     {
16231       if (type == 'd')
16232 	mips_align (3, 0, l);
16233       else
16234 	mips_align (2, 0, l);
16235     }
16236 
16237   float_cons (type);
16238   mips_clear_insn_labels ();
16239 }
16240 
16241 /* Handle .globl.  We need to override it because on Irix 5 you are
16242    permitted to say
16243        .globl foo .text
16244    where foo is an undefined symbol, to mean that foo should be
16245    considered to be the address of a function.  */
16246 
16247 static void
16248 s_mips_globl (int x ATTRIBUTE_UNUSED)
16249 {
16250   char *name;
16251   int c;
16252   symbolS *symbolP;
16253   flagword flag;
16254 
16255   do
16256     {
16257       c = get_symbol_name (&name);
16258       symbolP = symbol_find_or_make (name);
16259       S_SET_EXTERNAL (symbolP);
16260 
16261       *input_line_pointer = c;
16262       SKIP_WHITESPACE_AFTER_NAME ();
16263 
16264       /* On Irix 5, every global symbol that is not explicitly labelled as
16265          being a function is apparently labelled as being an object.  */
16266       flag = BSF_OBJECT;
16267 
16268       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16269 	  && (*input_line_pointer != ','))
16270 	{
16271 	  char *secname;
16272 	  asection *sec;
16273 
16274 	  c = get_symbol_name (&secname);
16275 	  sec = bfd_get_section_by_name (stdoutput, secname);
16276 	  if (sec == NULL)
16277 	    as_bad (_("%s: no such section"), secname);
16278 	  (void) restore_line_pointer (c);
16279 
16280 	  if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16281 	    flag = BSF_FUNCTION;
16282 	}
16283 
16284       symbol_get_bfdsym (symbolP)->flags |= flag;
16285 
16286       c = *input_line_pointer;
16287       if (c == ',')
16288 	{
16289 	  input_line_pointer++;
16290 	  SKIP_WHITESPACE ();
16291 	  if (is_end_of_line[(unsigned char) *input_line_pointer])
16292 	    c = '\n';
16293 	}
16294     }
16295   while (c == ',');
16296 
16297   demand_empty_rest_of_line ();
16298 }
16299 
16300 static void
16301 s_option (int x ATTRIBUTE_UNUSED)
16302 {
16303   char *opt;
16304   char c;
16305 
16306   c = get_symbol_name (&opt);
16307 
16308   if (*opt == 'O')
16309     {
16310       /* FIXME: What does this mean?  */
16311     }
16312   else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
16313     {
16314       int i;
16315 
16316       i = atoi (opt + 3);
16317       if (i != 0 && i != 2)
16318 	as_bad (_(".option pic%d not supported"), i);
16319       else if (mips_pic == VXWORKS_PIC)
16320 	as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16321       else if (i == 0)
16322 	mips_pic = NO_PIC;
16323       else if (i == 2)
16324 	{
16325 	  mips_pic = SVR4_PIC;
16326 	  mips_abicalls = TRUE;
16327 	}
16328 
16329       if (mips_pic == SVR4_PIC)
16330 	{
16331 	  if (g_switch_seen && g_switch_value != 0)
16332 	    as_warn (_("-G may not be used with SVR4 PIC code"));
16333 	  g_switch_value = 0;
16334 	  bfd_set_gp_size (stdoutput, 0);
16335 	}
16336     }
16337   else
16338     as_warn (_("unrecognized option \"%s\""), opt);
16339 
16340   (void) restore_line_pointer (c);
16341   demand_empty_rest_of_line ();
16342 }
16343 
16344 /* This structure is used to hold a stack of .set values.  */
16345 
16346 struct mips_option_stack
16347 {
16348   struct mips_option_stack *next;
16349   struct mips_set_options options;
16350 };
16351 
16352 static struct mips_option_stack *mips_opts_stack;
16353 
16354 /* Return status for .set/.module option handling.  */
16355 
16356 enum code_option_type
16357 {
16358   /* Unrecognized option.  */
16359   OPTION_TYPE_BAD = -1,
16360 
16361   /* Ordinary option.  */
16362   OPTION_TYPE_NORMAL,
16363 
16364   /* ISA changing option.  */
16365   OPTION_TYPE_ISA
16366 };
16367 
16368 /* Handle common .set/.module options.  Return status indicating option
16369    type.  */
16370 
16371 static enum code_option_type
16372 parse_code_option (char * name)
16373 {
16374   bfd_boolean isa_set = FALSE;
16375   const struct mips_ase *ase;
16376 
16377   if (strncmp (name, "at=", 3) == 0)
16378     {
16379       char *s = name + 3;
16380 
16381       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16382 	as_bad (_("unrecognized register name `%s'"), s);
16383     }
16384   else if (strcmp (name, "at") == 0)
16385     mips_opts.at = ATREG;
16386   else if (strcmp (name, "noat") == 0)
16387     mips_opts.at = ZERO;
16388   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16389     mips_opts.nomove = 0;
16390   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16391     mips_opts.nomove = 1;
16392   else if (strcmp (name, "bopt") == 0)
16393     mips_opts.nobopt = 0;
16394   else if (strcmp (name, "nobopt") == 0)
16395     mips_opts.nobopt = 1;
16396   else if (strcmp (name, "gp=32") == 0)
16397     mips_opts.gp = 32;
16398   else if (strcmp (name, "gp=64") == 0)
16399     mips_opts.gp = 64;
16400   else if (strcmp (name, "fp=32") == 0)
16401     mips_opts.fp = 32;
16402   else if (strcmp (name, "fp=xx") == 0)
16403     mips_opts.fp = 0;
16404   else if (strcmp (name, "fp=64") == 0)
16405     mips_opts.fp = 64;
16406   else if (strcmp (name, "softfloat") == 0)
16407     mips_opts.soft_float = 1;
16408   else if (strcmp (name, "hardfloat") == 0)
16409     mips_opts.soft_float = 0;
16410   else if (strcmp (name, "singlefloat") == 0)
16411     mips_opts.single_float = 1;
16412   else if (strcmp (name, "doublefloat") == 0)
16413     mips_opts.single_float = 0;
16414   else if (strcmp (name, "nooddspreg") == 0)
16415     mips_opts.oddspreg = 0;
16416   else if (strcmp (name, "oddspreg") == 0)
16417     mips_opts.oddspreg = 1;
16418   else if (strcmp (name, "mips16") == 0
16419 	   || strcmp (name, "MIPS-16") == 0)
16420     mips_opts.mips16 = 1;
16421   else if (strcmp (name, "nomips16") == 0
16422 	   || strcmp (name, "noMIPS-16") == 0)
16423     mips_opts.mips16 = 0;
16424   else if (strcmp (name, "micromips") == 0)
16425     mips_opts.micromips = 1;
16426   else if (strcmp (name, "nomicromips") == 0)
16427     mips_opts.micromips = 0;
16428   else if (name[0] == 'n'
16429 	   && name[1] == 'o'
16430 	   && (ase = mips_lookup_ase (name + 2)))
16431     mips_set_ase (ase, &mips_opts, FALSE);
16432   else if ((ase = mips_lookup_ase (name)))
16433     mips_set_ase (ase, &mips_opts, TRUE);
16434   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16435     {
16436       /* Permit the user to change the ISA and architecture on the fly.
16437 	 Needless to say, misuse can cause serious problems.  */
16438       if (strncmp (name, "arch=", 5) == 0)
16439 	{
16440 	  const struct mips_cpu_info *p;
16441 
16442 	  p = mips_parse_cpu ("internal use", name + 5);
16443 	  if (!p)
16444 	    as_bad (_("unknown architecture %s"), name + 5);
16445 	  else
16446 	    {
16447 	      mips_opts.arch = p->cpu;
16448 	      mips_opts.isa = p->isa;
16449 	      isa_set = TRUE;
16450 	    }
16451 	}
16452       else if (strncmp (name, "mips", 4) == 0)
16453 	{
16454 	  const struct mips_cpu_info *p;
16455 
16456 	  p = mips_parse_cpu ("internal use", name);
16457 	  if (!p)
16458 	    as_bad (_("unknown ISA level %s"), name + 4);
16459 	  else
16460 	    {
16461 	      mips_opts.arch = p->cpu;
16462 	      mips_opts.isa = p->isa;
16463 	      isa_set = TRUE;
16464 	    }
16465 	}
16466       else
16467 	as_bad (_("unknown ISA or architecture %s"), name);
16468     }
16469   else if (strcmp (name, "autoextend") == 0)
16470     mips_opts.noautoextend = 0;
16471   else if (strcmp (name, "noautoextend") == 0)
16472     mips_opts.noautoextend = 1;
16473   else if (strcmp (name, "insn32") == 0)
16474     mips_opts.insn32 = TRUE;
16475   else if (strcmp (name, "noinsn32") == 0)
16476     mips_opts.insn32 = FALSE;
16477   else if (strcmp (name, "sym32") == 0)
16478     mips_opts.sym32 = TRUE;
16479   else if (strcmp (name, "nosym32") == 0)
16480     mips_opts.sym32 = FALSE;
16481   else
16482     return OPTION_TYPE_BAD;
16483 
16484   return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16485 }
16486 
16487 /* Handle the .set pseudo-op.  */
16488 
16489 static void
16490 s_mipsset (int x ATTRIBUTE_UNUSED)
16491 {
16492   enum code_option_type type = OPTION_TYPE_NORMAL;
16493   char *name = input_line_pointer, ch;
16494 
16495   file_mips_check_options ();
16496 
16497   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16498     ++input_line_pointer;
16499   ch = *input_line_pointer;
16500   *input_line_pointer = '\0';
16501 
16502   if (strchr (name, ','))
16503     {
16504       /* Generic ".set" directive; use the generic handler.  */
16505       *input_line_pointer = ch;
16506       input_line_pointer = name;
16507       s_set (0);
16508       return;
16509     }
16510 
16511   if (strcmp (name, "reorder") == 0)
16512     {
16513       if (mips_opts.noreorder)
16514 	end_noreorder ();
16515     }
16516   else if (strcmp (name, "noreorder") == 0)
16517     {
16518       if (!mips_opts.noreorder)
16519 	start_noreorder ();
16520     }
16521   else if (strcmp (name, "macro") == 0)
16522     mips_opts.warn_about_macros = 0;
16523   else if (strcmp (name, "nomacro") == 0)
16524     {
16525       if (mips_opts.noreorder == 0)
16526 	as_bad (_("`noreorder' must be set before `nomacro'"));
16527       mips_opts.warn_about_macros = 1;
16528     }
16529   else if (strcmp (name, "gp=default") == 0)
16530     mips_opts.gp = file_mips_opts.gp;
16531   else if (strcmp (name, "fp=default") == 0)
16532     mips_opts.fp = file_mips_opts.fp;
16533   else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16534     {
16535       mips_opts.isa = file_mips_opts.isa;
16536       mips_opts.arch = file_mips_opts.arch;
16537       mips_opts.gp = file_mips_opts.gp;
16538       mips_opts.fp = file_mips_opts.fp;
16539     }
16540   else if (strcmp (name, "push") == 0)
16541     {
16542       struct mips_option_stack *s;
16543 
16544       s = XNEW (struct mips_option_stack);
16545       s->next = mips_opts_stack;
16546       s->options = mips_opts;
16547       mips_opts_stack = s;
16548     }
16549   else if (strcmp (name, "pop") == 0)
16550     {
16551       struct mips_option_stack *s;
16552 
16553       s = mips_opts_stack;
16554       if (s == NULL)
16555 	as_bad (_(".set pop with no .set push"));
16556       else
16557 	{
16558 	  /* If we're changing the reorder mode we need to handle
16559              delay slots correctly.  */
16560 	  if (s->options.noreorder && ! mips_opts.noreorder)
16561 	    start_noreorder ();
16562 	  else if (! s->options.noreorder && mips_opts.noreorder)
16563 	    end_noreorder ();
16564 
16565 	  mips_opts = s->options;
16566 	  mips_opts_stack = s->next;
16567 	  free (s);
16568 	}
16569     }
16570   else
16571     {
16572       type = parse_code_option (name);
16573       if (type == OPTION_TYPE_BAD)
16574 	as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16575     }
16576 
16577   /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16578      registers based on what is supported by the arch/cpu.  */
16579   if (type == OPTION_TYPE_ISA)
16580     {
16581       switch (mips_opts.isa)
16582 	{
16583 	case 0:
16584 	  break;
16585 	case ISA_MIPS1:
16586 	  /* MIPS I cannot support FPXX.  */
16587 	  mips_opts.fp = 32;
16588 	  /* fall-through.  */
16589 	case ISA_MIPS2:
16590 	case ISA_MIPS32:
16591 	case ISA_MIPS32R2:
16592 	case ISA_MIPS32R3:
16593 	case ISA_MIPS32R5:
16594 	  mips_opts.gp = 32;
16595 	  if (mips_opts.fp != 0)
16596 	    mips_opts.fp = 32;
16597 	  break;
16598 	case ISA_MIPS32R6:
16599 	  mips_opts.gp = 32;
16600 	  mips_opts.fp = 64;
16601 	  break;
16602 	case ISA_MIPS3:
16603 	case ISA_MIPS4:
16604 	case ISA_MIPS5:
16605 	case ISA_MIPS64:
16606 	case ISA_MIPS64R2:
16607 	case ISA_MIPS64R3:
16608 	case ISA_MIPS64R5:
16609 	case ISA_MIPS64R6:
16610 	  mips_opts.gp = 64;
16611 	  if (mips_opts.fp != 0)
16612 	    {
16613 	      if (mips_opts.arch == CPU_R5900)
16614 		mips_opts.fp = 32;
16615 	      else
16616 		mips_opts.fp = 64;
16617 	    }
16618 	  break;
16619 	default:
16620 	  as_bad (_("unknown ISA level %s"), name + 4);
16621 	  break;
16622 	}
16623     }
16624 
16625   mips_check_options (&mips_opts, FALSE);
16626 
16627   mips_check_isa_supports_ases ();
16628   *input_line_pointer = ch;
16629   demand_empty_rest_of_line ();
16630 }
16631 
16632 /* Handle the .module pseudo-op.  */
16633 
16634 static void
16635 s_module (int ignore ATTRIBUTE_UNUSED)
16636 {
16637   char *name = input_line_pointer, ch;
16638 
16639   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16640     ++input_line_pointer;
16641   ch = *input_line_pointer;
16642   *input_line_pointer = '\0';
16643 
16644   if (!file_mips_opts_checked)
16645     {
16646       if (parse_code_option (name) == OPTION_TYPE_BAD)
16647 	as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16648 
16649       /* Update module level settings from mips_opts.  */
16650       file_mips_opts = mips_opts;
16651     }
16652   else
16653     as_bad (_(".module is not permitted after generating code"));
16654 
16655   *input_line_pointer = ch;
16656   demand_empty_rest_of_line ();
16657 }
16658 
16659 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16660    .option pic2.  It means to generate SVR4 PIC calls.  */
16661 
16662 static void
16663 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16664 {
16665   mips_pic = SVR4_PIC;
16666   mips_abicalls = TRUE;
16667 
16668   if (g_switch_seen && g_switch_value != 0)
16669     as_warn (_("-G may not be used with SVR4 PIC code"));
16670   g_switch_value = 0;
16671 
16672   bfd_set_gp_size (stdoutput, 0);
16673   demand_empty_rest_of_line ();
16674 }
16675 
16676 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16677    PIC code.  It sets the $gp register for the function based on the
16678    function address, which is in the register named in the argument.
16679    This uses a relocation against _gp_disp, which is handled specially
16680    by the linker.  The result is:
16681 	lui	$gp,%hi(_gp_disp)
16682 	addiu	$gp,$gp,%lo(_gp_disp)
16683 	addu	$gp,$gp,.cpload argument
16684    The .cpload argument is normally $25 == $t9.
16685 
16686    The -mno-shared option changes this to:
16687 	lui	$gp,%hi(__gnu_local_gp)
16688 	addiu	$gp,$gp,%lo(__gnu_local_gp)
16689    and the argument is ignored.  This saves an instruction, but the
16690    resulting code is not position independent; it uses an absolute
16691    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16692    can go into an ordinary executable, but not into a shared library.  */
16693 
16694 static void
16695 s_cpload (int ignore ATTRIBUTE_UNUSED)
16696 {
16697   expressionS ex;
16698   int reg;
16699   int in_shared;
16700 
16701   file_mips_check_options ();
16702 
16703   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16704      .cpload 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"), ".cpload");
16714       ignore_rest_of_line ();
16715       return;
16716     }
16717 
16718   /* .cpload should be in a .set noreorder section.  */
16719   if (mips_opts.noreorder == 0)
16720     as_warn (_(".cpload not in noreorder section"));
16721 
16722   reg = tc_get_register (0);
16723 
16724   /* If we need to produce a 64-bit address, we are better off using
16725      the default instruction sequence.  */
16726   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16727 
16728   ex.X_op = O_symbol;
16729   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16730                                          "__gnu_local_gp");
16731   ex.X_op_symbol = NULL;
16732   ex.X_add_number = 0;
16733 
16734   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16735   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16736 
16737   mips_mark_labels ();
16738   mips_assembling_insn = TRUE;
16739 
16740   macro_start ();
16741   macro_build_lui (&ex, mips_gp_register);
16742   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16743 	       mips_gp_register, BFD_RELOC_LO16);
16744   if (in_shared)
16745     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16746 		 mips_gp_register, reg);
16747   macro_end ();
16748 
16749   mips_assembling_insn = FALSE;
16750   demand_empty_rest_of_line ();
16751 }
16752 
16753 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16754      .cpsetup $reg1, offset|$reg2, label
16755 
16756    If offset is given, this results in:
16757      sd		$gp, offset($sp)
16758      lui	$gp, %hi(%neg(%gp_rel(label)))
16759      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16760      daddu	$gp, $gp, $reg1
16761 
16762    If $reg2 is given, this results in:
16763      or		$reg2, $gp, $0
16764      lui	$gp, %hi(%neg(%gp_rel(label)))
16765      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
16766      daddu	$gp, $gp, $reg1
16767    $reg1 is normally $25 == $t9.
16768 
16769    The -mno-shared option replaces the last three instructions with
16770 	lui	$gp,%hi(_gp)
16771 	addiu	$gp,$gp,%lo(_gp)  */
16772 
16773 static void
16774 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16775 {
16776   expressionS ex_off;
16777   expressionS ex_sym;
16778   int reg1;
16779 
16780   file_mips_check_options ();
16781 
16782   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16783      We also need NewABI support.  */
16784   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16785     {
16786       s_ignore (0);
16787       return;
16788     }
16789 
16790   if (mips_opts.mips16)
16791     {
16792       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16793       ignore_rest_of_line ();
16794       return;
16795     }
16796 
16797   reg1 = tc_get_register (0);
16798   SKIP_WHITESPACE ();
16799   if (*input_line_pointer != ',')
16800     {
16801       as_bad (_("missing argument separator ',' for .cpsetup"));
16802       return;
16803     }
16804   else
16805     ++input_line_pointer;
16806   SKIP_WHITESPACE ();
16807   if (*input_line_pointer == '$')
16808     {
16809       mips_cpreturn_register = tc_get_register (0);
16810       mips_cpreturn_offset = -1;
16811     }
16812   else
16813     {
16814       mips_cpreturn_offset = get_absolute_expression ();
16815       mips_cpreturn_register = -1;
16816     }
16817   SKIP_WHITESPACE ();
16818   if (*input_line_pointer != ',')
16819     {
16820       as_bad (_("missing argument separator ',' for .cpsetup"));
16821       return;
16822     }
16823   else
16824     ++input_line_pointer;
16825   SKIP_WHITESPACE ();
16826   expression (&ex_sym);
16827 
16828   mips_mark_labels ();
16829   mips_assembling_insn = TRUE;
16830 
16831   macro_start ();
16832   if (mips_cpreturn_register == -1)
16833     {
16834       ex_off.X_op = O_constant;
16835       ex_off.X_add_symbol = NULL;
16836       ex_off.X_op_symbol = NULL;
16837       ex_off.X_add_number = mips_cpreturn_offset;
16838 
16839       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16840 		   BFD_RELOC_LO16, SP);
16841     }
16842   else
16843     move_register (mips_cpreturn_register, mips_gp_register);
16844 
16845   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16846     {
16847       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16848 		   -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16849 		   BFD_RELOC_HI16_S);
16850 
16851       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16852 		   mips_gp_register, -1, BFD_RELOC_GPREL16,
16853 		   BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16854 
16855       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16856 		   mips_gp_register, reg1);
16857     }
16858   else
16859     {
16860       expressionS ex;
16861 
16862       ex.X_op = O_symbol;
16863       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16864       ex.X_op_symbol = NULL;
16865       ex.X_add_number = 0;
16866 
16867       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16868       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16869 
16870       macro_build_lui (&ex, mips_gp_register);
16871       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16872 		   mips_gp_register, BFD_RELOC_LO16);
16873     }
16874 
16875   macro_end ();
16876 
16877   mips_assembling_insn = FALSE;
16878   demand_empty_rest_of_line ();
16879 }
16880 
16881 static void
16882 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16883 {
16884   file_mips_check_options ();
16885 
16886   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16887      .cplocal is ignored.  */
16888   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16889     {
16890       s_ignore (0);
16891       return;
16892     }
16893 
16894   if (mips_opts.mips16)
16895     {
16896       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16897       ignore_rest_of_line ();
16898       return;
16899     }
16900 
16901   mips_gp_register = tc_get_register (0);
16902   demand_empty_rest_of_line ();
16903 }
16904 
16905 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16906    offset from $sp.  The offset is remembered, and after making a PIC
16907    call $gp is restored from that location.  */
16908 
16909 static void
16910 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16911 {
16912   expressionS ex;
16913 
16914   file_mips_check_options ();
16915 
16916   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16917      .cprestore is ignored.  */
16918   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16919     {
16920       s_ignore (0);
16921       return;
16922     }
16923 
16924   if (mips_opts.mips16)
16925     {
16926       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16927       ignore_rest_of_line ();
16928       return;
16929     }
16930 
16931   mips_cprestore_offset = get_absolute_expression ();
16932   mips_cprestore_valid = 1;
16933 
16934   ex.X_op = O_constant;
16935   ex.X_add_symbol = NULL;
16936   ex.X_op_symbol = NULL;
16937   ex.X_add_number = mips_cprestore_offset;
16938 
16939   mips_mark_labels ();
16940   mips_assembling_insn = TRUE;
16941 
16942   macro_start ();
16943   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16944 				SP, HAVE_64BIT_ADDRESSES);
16945   macro_end ();
16946 
16947   mips_assembling_insn = FALSE;
16948   demand_empty_rest_of_line ();
16949 }
16950 
16951 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16952    was given in the preceding .cpsetup, it results in:
16953      ld		$gp, offset($sp)
16954 
16955    If a register $reg2 was given there, it results in:
16956      or		$gp, $reg2, $0  */
16957 
16958 static void
16959 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16960 {
16961   expressionS ex;
16962 
16963   file_mips_check_options ();
16964 
16965   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16966      We also need NewABI support.  */
16967   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16968     {
16969       s_ignore (0);
16970       return;
16971     }
16972 
16973   if (mips_opts.mips16)
16974     {
16975       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16976       ignore_rest_of_line ();
16977       return;
16978     }
16979 
16980   mips_mark_labels ();
16981   mips_assembling_insn = TRUE;
16982 
16983   macro_start ();
16984   if (mips_cpreturn_register == -1)
16985     {
16986       ex.X_op = O_constant;
16987       ex.X_add_symbol = NULL;
16988       ex.X_op_symbol = NULL;
16989       ex.X_add_number = mips_cpreturn_offset;
16990 
16991       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16992     }
16993   else
16994     move_register (mips_gp_register, mips_cpreturn_register);
16995 
16996   macro_end ();
16997 
16998   mips_assembling_insn = FALSE;
16999   demand_empty_rest_of_line ();
17000 }
17001 
17002 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17003    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17004    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17005    debug information or MIPS16 TLS.  */
17006 
17007 static void
17008 s_tls_rel_directive (const size_t bytes, const char *dirstr,
17009 		     bfd_reloc_code_real_type rtype)
17010 {
17011   expressionS ex;
17012   char *p;
17013 
17014   expression (&ex);
17015 
17016   if (ex.X_op != O_symbol)
17017     {
17018       as_bad (_("unsupported use of %s"), dirstr);
17019       ignore_rest_of_line ();
17020     }
17021 
17022   p = frag_more (bytes);
17023   md_number_to_chars (p, 0, bytes);
17024   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
17025   demand_empty_rest_of_line ();
17026   mips_clear_insn_labels ();
17027 }
17028 
17029 /* Handle .dtprelword.  */
17030 
17031 static void
17032 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17033 {
17034   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
17035 }
17036 
17037 /* Handle .dtpreldword.  */
17038 
17039 static void
17040 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17041 {
17042   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17043 }
17044 
17045 /* Handle .tprelword.  */
17046 
17047 static void
17048 s_tprelword (int ignore ATTRIBUTE_UNUSED)
17049 {
17050   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17051 }
17052 
17053 /* Handle .tpreldword.  */
17054 
17055 static void
17056 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17057 {
17058   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17059 }
17060 
17061 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
17062    code.  It sets the offset to use in gp_rel relocations.  */
17063 
17064 static void
17065 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17066 {
17067   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17068      We also need NewABI support.  */
17069   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17070     {
17071       s_ignore (0);
17072       return;
17073     }
17074 
17075   mips_gprel_offset = get_absolute_expression ();
17076 
17077   demand_empty_rest_of_line ();
17078 }
17079 
17080 /* Handle the .gpword pseudo-op.  This is used when generating PIC
17081    code.  It generates a 32 bit GP relative reloc.  */
17082 
17083 static void
17084 s_gpword (int ignore ATTRIBUTE_UNUSED)
17085 {
17086   segment_info_type *si;
17087   struct insn_label_list *l;
17088   expressionS ex;
17089   char *p;
17090 
17091   /* When not generating PIC code, this is treated as .word.  */
17092   if (mips_pic != SVR4_PIC)
17093     {
17094       s_cons (2);
17095       return;
17096     }
17097 
17098   si = seg_info (now_seg);
17099   l = si->label_list;
17100   mips_emit_delays ();
17101   if (auto_align)
17102     mips_align (2, 0, l);
17103 
17104   expression (&ex);
17105   mips_clear_insn_labels ();
17106 
17107   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17108     {
17109       as_bad (_("unsupported use of .gpword"));
17110       ignore_rest_of_line ();
17111     }
17112 
17113   p = frag_more (4);
17114   md_number_to_chars (p, 0, 4);
17115   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17116 	       BFD_RELOC_GPREL32);
17117 
17118   demand_empty_rest_of_line ();
17119 }
17120 
17121 static void
17122 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17123 {
17124   segment_info_type *si;
17125   struct insn_label_list *l;
17126   expressionS ex;
17127   char *p;
17128 
17129   /* When not generating PIC code, this is treated as .dword.  */
17130   if (mips_pic != SVR4_PIC)
17131     {
17132       s_cons (3);
17133       return;
17134     }
17135 
17136   si = seg_info (now_seg);
17137   l = si->label_list;
17138   mips_emit_delays ();
17139   if (auto_align)
17140     mips_align (3, 0, l);
17141 
17142   expression (&ex);
17143   mips_clear_insn_labels ();
17144 
17145   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17146     {
17147       as_bad (_("unsupported use of .gpdword"));
17148       ignore_rest_of_line ();
17149     }
17150 
17151   p = frag_more (8);
17152   md_number_to_chars (p, 0, 8);
17153   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17154 	       BFD_RELOC_GPREL32)->fx_tcbit = 1;
17155 
17156   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
17157   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17158 	   FALSE, BFD_RELOC_64)->fx_tcbit = 1;
17159 
17160   demand_empty_rest_of_line ();
17161 }
17162 
17163 /* Handle the .ehword pseudo-op.  This is used when generating unwinding
17164    tables.  It generates a R_MIPS_EH reloc.  */
17165 
17166 static void
17167 s_ehword (int ignore ATTRIBUTE_UNUSED)
17168 {
17169   expressionS ex;
17170   char *p;
17171 
17172   mips_emit_delays ();
17173 
17174   expression (&ex);
17175   mips_clear_insn_labels ();
17176 
17177   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17178     {
17179       as_bad (_("unsupported use of .ehword"));
17180       ignore_rest_of_line ();
17181     }
17182 
17183   p = frag_more (4);
17184   md_number_to_chars (p, 0, 4);
17185   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17186 	       BFD_RELOC_32_PCREL);
17187 
17188   demand_empty_rest_of_line ();
17189 }
17190 
17191 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
17192    tables in SVR4 PIC code.  */
17193 
17194 static void
17195 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17196 {
17197   int reg;
17198 
17199   file_mips_check_options ();
17200 
17201   /* This is ignored when not generating SVR4 PIC code.  */
17202   if (mips_pic != SVR4_PIC)
17203     {
17204       s_ignore (0);
17205       return;
17206     }
17207 
17208   mips_mark_labels ();
17209   mips_assembling_insn = TRUE;
17210 
17211   /* Add $gp to the register named as an argument.  */
17212   macro_start ();
17213   reg = tc_get_register (0);
17214   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17215   macro_end ();
17216 
17217   mips_assembling_insn = FALSE;
17218   demand_empty_rest_of_line ();
17219 }
17220 
17221 /* Handle the .insn pseudo-op.  This marks instruction labels in
17222    mips16/micromips mode.  This permits the linker to handle them specially,
17223    such as generating jalx instructions when needed.  We also make
17224    them odd for the duration of the assembly, in order to generate the
17225    right sort of code.  We will make them even in the adjust_symtab
17226    routine, while leaving them marked.  This is convenient for the
17227    debugger and the disassembler.  The linker knows to make them odd
17228    again.  */
17229 
17230 static void
17231 s_insn (int ignore ATTRIBUTE_UNUSED)
17232 {
17233   file_mips_check_options ();
17234   file_ase_mips16 |= mips_opts.mips16;
17235   file_ase_micromips |= mips_opts.micromips;
17236 
17237   mips_mark_labels ();
17238 
17239   demand_empty_rest_of_line ();
17240 }
17241 
17242 /* Handle the .nan pseudo-op.  */
17243 
17244 static void
17245 s_nan (int ignore ATTRIBUTE_UNUSED)
17246 {
17247   static const char str_legacy[] = "legacy";
17248   static const char str_2008[] = "2008";
17249   size_t i;
17250 
17251   for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17252 
17253   if (i == sizeof (str_2008) - 1
17254       && memcmp (input_line_pointer, str_2008, i) == 0)
17255     mips_nan2008 = 1;
17256   else if (i == sizeof (str_legacy) - 1
17257 	   && memcmp (input_line_pointer, str_legacy, i) == 0)
17258     {
17259       if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17260 	mips_nan2008 = 0;
17261       else
17262 	as_bad (_("`%s' does not support legacy NaN"),
17263 	          mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17264     }
17265   else
17266     as_bad (_("bad .nan directive"));
17267 
17268   input_line_pointer += i;
17269   demand_empty_rest_of_line ();
17270 }
17271 
17272 /* Handle a .stab[snd] directive.  Ideally these directives would be
17273    implemented in a transparent way, so that removing them would not
17274    have any effect on the generated instructions.  However, s_stab
17275    internally changes the section, so in practice we need to decide
17276    now whether the preceding label marks compressed code.  We do not
17277    support changing the compression mode of a label after a .stab*
17278    directive, such as in:
17279 
17280    foo:
17281 	.stabs ...
17282 	.set mips16
17283 
17284    so the current mode wins.  */
17285 
17286 static void
17287 s_mips_stab (int type)
17288 {
17289   file_mips_check_options ();
17290   mips_mark_labels ();
17291   s_stab (type);
17292 }
17293 
17294 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17295 
17296 static void
17297 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17298 {
17299   char *name;
17300   int c;
17301   symbolS *symbolP;
17302   expressionS exp;
17303 
17304   c = get_symbol_name (&name);
17305   symbolP = symbol_find_or_make (name);
17306   S_SET_WEAK (symbolP);
17307   *input_line_pointer = c;
17308 
17309   SKIP_WHITESPACE_AFTER_NAME ();
17310 
17311   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17312     {
17313       if (S_IS_DEFINED (symbolP))
17314 	{
17315 	  as_bad (_("ignoring attempt to redefine symbol %s"),
17316 		  S_GET_NAME (symbolP));
17317 	  ignore_rest_of_line ();
17318 	  return;
17319 	}
17320 
17321       if (*input_line_pointer == ',')
17322 	{
17323 	  ++input_line_pointer;
17324 	  SKIP_WHITESPACE ();
17325 	}
17326 
17327       expression (&exp);
17328       if (exp.X_op != O_symbol)
17329 	{
17330 	  as_bad (_("bad .weakext directive"));
17331 	  ignore_rest_of_line ();
17332 	  return;
17333 	}
17334       symbol_set_value_expression (symbolP, &exp);
17335     }
17336 
17337   demand_empty_rest_of_line ();
17338 }
17339 
17340 /* Parse a register string into a number.  Called from the ECOFF code
17341    to parse .frame.  The argument is non-zero if this is the frame
17342    register, so that we can record it in mips_frame_reg.  */
17343 
17344 int
17345 tc_get_register (int frame)
17346 {
17347   unsigned int reg;
17348 
17349   SKIP_WHITESPACE ();
17350   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17351     reg = 0;
17352   if (frame)
17353     {
17354       mips_frame_reg = reg != 0 ? reg : SP;
17355       mips_frame_reg_valid = 1;
17356       mips_cprestore_valid = 0;
17357     }
17358   return reg;
17359 }
17360 
17361 valueT
17362 md_section_align (asection *seg, valueT addr)
17363 {
17364   int align = bfd_get_section_alignment (stdoutput, seg);
17365 
17366   /* We don't need to align ELF sections to the full alignment.
17367      However, Irix 5 may prefer that we align them at least to a 16
17368      byte boundary.  We don't bother to align the sections if we
17369      are targeted for an embedded system.  */
17370   if (strncmp (TARGET_OS, "elf", 3) == 0)
17371     return addr;
17372   if (align > 4)
17373     align = 4;
17374 
17375   return ((addr + (1 << align) - 1) & -(1 << align));
17376 }
17377 
17378 /* Utility routine, called from above as well.  If called while the
17379    input file is still being read, it's only an approximation.  (For
17380    example, a symbol may later become defined which appeared to be
17381    undefined earlier.)  */
17382 
17383 static int
17384 nopic_need_relax (symbolS *sym, int before_relaxing)
17385 {
17386   if (sym == 0)
17387     return 0;
17388 
17389   if (g_switch_value > 0)
17390     {
17391       const char *symname;
17392       int change;
17393 
17394       /* Find out whether this symbol can be referenced off the $gp
17395 	 register.  It can be if it is smaller than the -G size or if
17396 	 it is in the .sdata or .sbss section.  Certain symbols can
17397 	 not be referenced off the $gp, although it appears as though
17398 	 they can.  */
17399       symname = S_GET_NAME (sym);
17400       if (symname != (const char *) NULL
17401 	  && (strcmp (symname, "eprol") == 0
17402 	      || strcmp (symname, "etext") == 0
17403 	      || strcmp (symname, "_gp") == 0
17404 	      || strcmp (symname, "edata") == 0
17405 	      || strcmp (symname, "_fbss") == 0
17406 	      || strcmp (symname, "_fdata") == 0
17407 	      || strcmp (symname, "_ftext") == 0
17408 	      || strcmp (symname, "end") == 0
17409 	      || strcmp (symname, "_gp_disp") == 0))
17410 	change = 1;
17411       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17412 	       && (0
17413 #ifndef NO_ECOFF_DEBUGGING
17414 		   || (symbol_get_obj (sym)->ecoff_extern_size != 0
17415 		       && (symbol_get_obj (sym)->ecoff_extern_size
17416 			   <= g_switch_value))
17417 #endif
17418 		   /* We must defer this decision until after the whole
17419 		      file has been read, since there might be a .extern
17420 		      after the first use of this symbol.  */
17421 		   || (before_relaxing
17422 #ifndef NO_ECOFF_DEBUGGING
17423 		       && symbol_get_obj (sym)->ecoff_extern_size == 0
17424 #endif
17425 		       && S_GET_VALUE (sym) == 0)
17426 		   || (S_GET_VALUE (sym) != 0
17427 		       && S_GET_VALUE (sym) <= g_switch_value)))
17428 	change = 0;
17429       else
17430 	{
17431 	  const char *segname;
17432 
17433 	  segname = segment_name (S_GET_SEGMENT (sym));
17434 	  gas_assert (strcmp (segname, ".lit8") != 0
17435 		  && strcmp (segname, ".lit4") != 0);
17436 	  change = (strcmp (segname, ".sdata") != 0
17437 		    && strcmp (segname, ".sbss") != 0
17438 		    && strncmp (segname, ".sdata.", 7) != 0
17439 		    && strncmp (segname, ".sbss.", 6) != 0
17440 		    && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
17441 		    && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
17442 	}
17443       return change;
17444     }
17445   else
17446     /* We are not optimizing for the $gp register.  */
17447     return 1;
17448 }
17449 
17450 
17451 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17452 
17453 static bfd_boolean
17454 pic_need_relax (symbolS *sym)
17455 {
17456   asection *symsec;
17457 
17458   /* Handle the case of a symbol equated to another symbol.  */
17459   while (symbol_equated_reloc_p (sym))
17460     {
17461       symbolS *n;
17462 
17463       /* It's possible to get a loop here in a badly written program.  */
17464       n = symbol_get_value_expression (sym)->X_add_symbol;
17465       if (n == sym)
17466 	break;
17467       sym = n;
17468     }
17469 
17470   if (symbol_section_p (sym))
17471     return TRUE;
17472 
17473   symsec = S_GET_SEGMENT (sym);
17474 
17475   /* This must duplicate the test in adjust_reloc_syms.  */
17476   return (!bfd_is_und_section (symsec)
17477 	  && !bfd_is_abs_section (symsec)
17478 	  && !bfd_is_com_section (symsec)
17479 	  /* A global or weak symbol is treated as external.  */
17480 	  && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17481 }
17482 
17483 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17484    convert a section-relative value VAL to the equivalent PC-relative
17485    value.  */
17486 
17487 static offsetT
17488 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17489 		  offsetT val, long stretch)
17490 {
17491   fragS *sym_frag;
17492   addressT addr;
17493 
17494   gas_assert (pcrel_op->root.root.type == OP_PCREL);
17495 
17496   sym_frag = symbol_get_frag (fragp->fr_symbol);
17497 
17498   /* If the relax_marker of the symbol fragment differs from the
17499      relax_marker of this fragment, we have not yet adjusted the
17500      symbol fragment fr_address.  We want to add in STRETCH in
17501      order to get a better estimate of the address.  This
17502      particularly matters because of the shift bits.  */
17503   if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17504     {
17505       fragS *f;
17506 
17507       /* Adjust stretch for any alignment frag.  Note that if have
17508 	 been expanding the earlier code, the symbol may be
17509 	 defined in what appears to be an earlier frag.  FIXME:
17510 	 This doesn't handle the fr_subtype field, which specifies
17511 	 a maximum number of bytes to skip when doing an
17512 	 alignment.  */
17513       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17514 	{
17515 	  if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17516 	    {
17517 	      if (stretch < 0)
17518 		stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17519 	      else
17520 		stretch &= ~((1 << (int) f->fr_offset) - 1);
17521 	      if (stretch == 0)
17522 		break;
17523 	    }
17524 	}
17525       if (f != NULL)
17526 	val += stretch;
17527     }
17528 
17529   addr = fragp->fr_address + fragp->fr_fix;
17530 
17531   /* The base address rules are complicated.  The base address of
17532      a branch is the following instruction.  The base address of a
17533      PC relative load or add is the instruction itself, but if it
17534      is in a delay slot (in which case it can not be extended) use
17535      the address of the instruction whose delay slot it is in.  */
17536   if (pcrel_op->include_isa_bit)
17537     {
17538       addr += 2;
17539 
17540       /* If we are currently assuming that this frag should be
17541 	 extended, then the current address is two bytes higher.  */
17542       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17543 	addr += 2;
17544 
17545       /* Ignore the low bit in the target, since it will be set
17546 	 for a text label.  */
17547       val &= -2;
17548     }
17549   else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17550     addr -= 4;
17551   else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17552     addr -= 2;
17553 
17554   val -= addr & -(1 << pcrel_op->align_log2);
17555 
17556   return val;
17557 }
17558 
17559 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17560    extended opcode.  SEC is the section the frag is in.  */
17561 
17562 static int
17563 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17564 {
17565   const struct mips_int_operand *operand;
17566   offsetT val;
17567   segT symsec;
17568   int type;
17569 
17570   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17571     return 0;
17572   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17573     return 1;
17574 
17575   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17576   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17577   operand = mips16_immed_operand (type, FALSE);
17578   if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17579       || (operand->root.type == OP_PCREL
17580 	  ? sec != symsec
17581 	  : !bfd_is_abs_section (symsec)))
17582     return 1;
17583 
17584   val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17585 
17586   if (operand->root.type == OP_PCREL)
17587     {
17588       const struct mips_pcrel_operand *pcrel_op;
17589       offsetT maxtiny;
17590 
17591       if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17592 	return 1;
17593 
17594       pcrel_op = (const struct mips_pcrel_operand *) operand;
17595       val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17596 
17597       /* If any of the shifted bits are set, we must use an extended
17598          opcode.  If the address depends on the size of this
17599          instruction, this can lead to a loop, so we arrange to always
17600          use an extended opcode.  */
17601       if ((val & ((1 << operand->shift) - 1)) != 0)
17602 	{
17603 	  fragp->fr_subtype =
17604 	    RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17605 	  return 1;
17606 	}
17607 
17608       /* If we are about to mark a frag as extended because the value
17609          is precisely the next value above maxtiny, then there is a
17610          chance of an infinite loop as in the following code:
17611 	     la	$4,foo
17612 	     .skip	1020
17613 	     .align	2
17614 	   foo:
17615 	 In this case when the la is extended, foo is 0x3fc bytes
17616 	 away, so the la can be shrunk, but then foo is 0x400 away, so
17617 	 the la must be extended.  To avoid this loop, we mark the
17618 	 frag as extended if it was small, and is about to become
17619 	 extended with the next value above maxtiny.  */
17620       maxtiny = mips_int_operand_max (operand);
17621       if (val == maxtiny + (1 << operand->shift)
17622 	  && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17623 	{
17624 	  fragp->fr_subtype =
17625 	    RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17626 	  return 1;
17627 	}
17628     }
17629 
17630   return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17631 }
17632 
17633 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17634    macro expansion.  SEC is the section the frag is in.  We only
17635    support PC-relative instructions (LA, DLA, LW, LD) here, in
17636    non-PIC code using 32-bit addressing.  */
17637 
17638 static int
17639 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17640 {
17641   const struct mips_pcrel_operand *pcrel_op;
17642   const struct mips_int_operand *operand;
17643   offsetT val;
17644   segT symsec;
17645   int type;
17646 
17647   gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17648 
17649   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17650     return 0;
17651   if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17652     return 0;
17653 
17654   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17655   switch (type)
17656     {
17657     case 'A':
17658     case 'B':
17659     case 'E':
17660       symsec = S_GET_SEGMENT (fragp->fr_symbol);
17661       if (bfd_is_abs_section (symsec))
17662 	return 1;
17663       if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17664 	return 0;
17665       if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17666 	return 1;
17667 
17668       operand = mips16_immed_operand (type, TRUE);
17669       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17670       pcrel_op = (const struct mips_pcrel_operand *) operand;
17671       val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17672 
17673       return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17674 
17675     default:
17676       return 0;
17677     }
17678 }
17679 
17680 /* Compute the length of a branch sequence, and adjust the
17681    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17682    worst-case length is computed, with UPDATE being used to indicate
17683    whether an unconditional (-1), branch-likely (+1) or regular (0)
17684    branch is to be computed.  */
17685 static int
17686 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17687 {
17688   bfd_boolean toofar;
17689   int length;
17690 
17691   if (fragp
17692       && S_IS_DEFINED (fragp->fr_symbol)
17693       && !S_IS_WEAK (fragp->fr_symbol)
17694       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17695     {
17696       addressT addr;
17697       offsetT val;
17698 
17699       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17700 
17701       addr = fragp->fr_address + fragp->fr_fix + 4;
17702 
17703       val -= addr;
17704 
17705       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17706     }
17707   else
17708     /* If the symbol is not defined or it's in a different segment,
17709        we emit the long sequence.  */
17710     toofar = TRUE;
17711 
17712   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17713     fragp->fr_subtype
17714       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17715 			     RELAX_BRANCH_PIC (fragp->fr_subtype),
17716 			     RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17717 			     RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17718 			     RELAX_BRANCH_LINK (fragp->fr_subtype),
17719 			     toofar);
17720 
17721   length = 4;
17722   if (toofar)
17723     {
17724       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17725 	length += 8;
17726 
17727       if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
17728 	{
17729 	  /* Additional space for PIC loading of target address.  */
17730 	  length += 8;
17731 	  if (mips_opts.isa == ISA_MIPS1)
17732 	    /* Additional space for $at-stabilizing nop.  */
17733 	    length += 4;
17734 	}
17735 
17736       /* If branch is conditional.  */
17737       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17738 	length += 8;
17739     }
17740 
17741   return length;
17742 }
17743 
17744 /* Get a FRAG's branch instruction delay slot size, either from the
17745    short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17746    or SHORT_INSN_SIZE otherwise.  */
17747 
17748 static int
17749 frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17750 {
17751   char *buf = fragp->fr_literal + fragp->fr_fix;
17752 
17753   if (al)
17754     return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17755   else
17756     return short_insn_size;
17757 }
17758 
17759 /* Compute the length of a branch sequence, and adjust the
17760    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
17761    worst-case length is computed, with UPDATE being used to indicate
17762    whether an unconditional (-1), or regular (0) branch is to be
17763    computed.  */
17764 
17765 static int
17766 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17767 {
17768   bfd_boolean insn32 = TRUE;
17769   bfd_boolean nods = TRUE;
17770   bfd_boolean pic = TRUE;
17771   bfd_boolean al = TRUE;
17772   int short_insn_size;
17773   bfd_boolean toofar;
17774   int length;
17775 
17776   if (fragp)
17777     {
17778       insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17779       nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
17780       pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
17781       al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17782     }
17783   short_insn_size = insn32 ? 4 : 2;
17784 
17785   if (fragp
17786       && S_IS_DEFINED (fragp->fr_symbol)
17787       && !S_IS_WEAK (fragp->fr_symbol)
17788       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17789     {
17790       addressT addr;
17791       offsetT val;
17792 
17793       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17794       /* Ignore the low bit in the target, since it will be set
17795 	 for a text label.  */
17796       if ((val & 1) != 0)
17797 	--val;
17798 
17799       addr = fragp->fr_address + fragp->fr_fix + 4;
17800 
17801       val -= addr;
17802 
17803       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17804     }
17805   else
17806     /* If the symbol is not defined or it's in a different segment,
17807        we emit the long sequence.  */
17808     toofar = TRUE;
17809 
17810   if (fragp && update
17811       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17812     fragp->fr_subtype = (toofar
17813 			 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17814 			 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17815 
17816   length = 4;
17817   if (toofar)
17818     {
17819       bfd_boolean compact_known = fragp != NULL;
17820       bfd_boolean compact = FALSE;
17821       bfd_boolean uncond;
17822 
17823       if (fragp)
17824 	{
17825 	  compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17826 	  uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17827 	}
17828       else
17829 	uncond = update < 0;
17830 
17831       /* If label is out of range, we turn branch <br>:
17832 
17833 		<br>	label			# 4 bytes
17834 	    0:
17835 
17836          into:
17837 
17838 		j	label			# 4 bytes
17839 		nop				# 2/4 bytes if
17840 						#  compact && (!PIC || insn32)
17841 	    0:
17842        */
17843       if ((!pic || insn32) && (!compact_known || compact))
17844 	length += short_insn_size;
17845 
17846       /* If assembling PIC code, we further turn:
17847 
17848 			j	label			# 4 bytes
17849 
17850          into:
17851 
17852 			lw/ld	at, %got(label)(gp)	# 4 bytes
17853 			d/addiu	at, %lo(label)		# 4 bytes
17854 			jr/c	at			# 2/4 bytes
17855        */
17856       if (pic)
17857 	length += 4 + short_insn_size;
17858 
17859       /* Add an extra nop if the jump has no compact form and we need
17860          to fill the delay slot.  */
17861       if ((!pic || al) && nods)
17862 	length += (fragp
17863 		   ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17864 		   : short_insn_size);
17865 
17866       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17867 
17868 			<brneg>	0f			# 4 bytes
17869 			nop				# 2/4 bytes if !compact
17870        */
17871       if (!uncond)
17872 	length += (compact_known && compact) ? 4 : 4 + short_insn_size;
17873     }
17874   else if (nods)
17875     {
17876       /* Add an extra nop to fill the delay slot.  */
17877       gas_assert (fragp);
17878       length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17879     }
17880 
17881   return length;
17882 }
17883 
17884 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17885    bit accordingly.  */
17886 
17887 static int
17888 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17889 {
17890   bfd_boolean toofar;
17891 
17892   if (fragp
17893       && S_IS_DEFINED (fragp->fr_symbol)
17894       && !S_IS_WEAK (fragp->fr_symbol)
17895       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17896     {
17897       addressT addr;
17898       offsetT val;
17899       int type;
17900 
17901       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17902       /* Ignore the low bit in the target, since it will be set
17903 	 for a text label.  */
17904       if ((val & 1) != 0)
17905 	--val;
17906 
17907       /* Assume this is a 2-byte branch.  */
17908       addr = fragp->fr_address + fragp->fr_fix + 2;
17909 
17910       /* We try to avoid the infinite loop by not adding 2 more bytes for
17911 	 long branches.  */
17912 
17913       val -= addr;
17914 
17915       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17916       if (type == 'D')
17917 	toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17918       else if (type == 'E')
17919 	toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17920       else
17921 	abort ();
17922     }
17923   else
17924     /* If the symbol is not defined or it's in a different segment,
17925        we emit a normal 32-bit branch.  */
17926     toofar = TRUE;
17927 
17928   if (fragp && update
17929       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17930     fragp->fr_subtype
17931       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17932 	       : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17933 
17934   if (toofar)
17935     return 4;
17936 
17937   return 2;
17938 }
17939 
17940 /* Estimate the size of a frag before relaxing.  Unless this is the
17941    mips16, we are not really relaxing here, and the final size is
17942    encoded in the subtype information.  For the mips16, we have to
17943    decide whether we are using an extended opcode or not.  */
17944 
17945 int
17946 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17947 {
17948   int change;
17949 
17950   if (RELAX_BRANCH_P (fragp->fr_subtype))
17951     {
17952 
17953       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17954 
17955       return fragp->fr_var;
17956     }
17957 
17958   if (RELAX_MIPS16_P (fragp->fr_subtype))
17959     {
17960       /* We don't want to modify the EXTENDED bit here; it might get us
17961 	 into infinite loops.  We change it only in mips_relax_frag().  */
17962       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17963 	return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
17964       else
17965 	return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17966     }
17967 
17968   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17969     {
17970       int length = 4;
17971 
17972       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17973 	length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17974       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17975 	length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17976       fragp->fr_var = length;
17977 
17978       return length;
17979     }
17980 
17981   if (mips_pic == VXWORKS_PIC)
17982     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17983     change = 0;
17984   else if (RELAX_PIC (fragp->fr_subtype))
17985     change = pic_need_relax (fragp->fr_symbol);
17986   else
17987     change = nopic_need_relax (fragp->fr_symbol, 0);
17988 
17989   if (change)
17990     {
17991       fragp->fr_subtype |= RELAX_USE_SECOND;
17992       return -RELAX_FIRST (fragp->fr_subtype);
17993     }
17994   else
17995     return -RELAX_SECOND (fragp->fr_subtype);
17996 }
17997 
17998 /* This is called to see whether a reloc against a defined symbol
17999    should be converted into a reloc against a section.  */
18000 
18001 int
18002 mips_fix_adjustable (fixS *fixp)
18003 {
18004   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18005       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18006     return 0;
18007 
18008   if (fixp->fx_addsy == NULL)
18009     return 1;
18010 
18011   /* Allow relocs used for EH tables.  */
18012   if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18013     return 1;
18014 
18015   /* If symbol SYM is in a mergeable section, relocations of the form
18016      SYM + 0 can usually be made section-relative.  The mergeable data
18017      is then identified by the section offset rather than by the symbol.
18018 
18019      However, if we're generating REL LO16 relocations, the offset is split
18020      between the LO16 and partnering high part relocation.  The linker will
18021      need to recalculate the complete offset in order to correctly identify
18022      the merge data.
18023 
18024      The linker has traditionally not looked for the partnering high part
18025      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18026      placed anywhere.  Rather than break backwards compatibility by changing
18027      this, it seems better not to force the issue, and instead keep the
18028      original symbol.  This will work with either linker behavior.  */
18029   if ((lo16_reloc_p (fixp->fx_r_type)
18030        || reloc_needs_lo_p (fixp->fx_r_type))
18031       && HAVE_IN_PLACE_ADDENDS
18032       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18033     return 0;
18034 
18035   /* There is no place to store an in-place offset for JALR relocations.  */
18036   if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18037     return 0;
18038 
18039   /* Likewise an in-range offset of limited PC-relative relocations may
18040      overflow the in-place relocatable field if recalculated against the
18041      start address of the symbol's containing section.
18042 
18043      Also, PC relative relocations for MIPS R6 need to be symbol rather than
18044      section relative to allow linker relaxations to be performed later on.  */
18045   if (limited_pcrel_reloc_p (fixp->fx_r_type)
18046       && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
18047     return 0;
18048 
18049   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18050      to a floating-point stub.  The same is true for non-R_MIPS16_26
18051      relocations against MIPS16 functions; in this case, the stub becomes
18052      the function's canonical address.
18053 
18054      Floating-point stubs are stored in unique .mips16.call.* or
18055      .mips16.fn.* sections.  If a stub T for function F is in section S,
18056      the first relocation in section S must be against F; this is how the
18057      linker determines the target function.  All relocations that might
18058      resolve to T must also be against F.  We therefore have the following
18059      restrictions, which are given in an intentionally-redundant way:
18060 
18061        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18062 	  symbols.
18063 
18064        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18065 	  if that stub might be used.
18066 
18067        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18068 	  symbols.
18069 
18070        4. We cannot reduce a stub's relocations against MIPS16 symbols if
18071 	  that stub might be used.
18072 
18073      There is a further restriction:
18074 
18075        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18076 	  R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
18077 	  R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18078 	  R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18079 	  against MIPS16 or microMIPS symbols because we need to keep the
18080 	  MIPS16 or microMIPS symbol for the purpose of mode mismatch
18081 	  detection and JAL or BAL to JALX instruction conversion in the
18082 	  linker.
18083 
18084      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18085      against a MIPS16 symbol.  We deal with (5) by additionally leaving
18086      alone any jump and branch relocations against a microMIPS symbol.
18087 
18088      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18089      relocation against some symbol R, no relocation against R may be
18090      reduced.  (Note that this deals with (2) as well as (1) because
18091      relocations against global symbols will never be reduced on ELF
18092      targets.)  This approach is a little simpler than trying to detect
18093      stub sections, and gives the "all or nothing" per-symbol consistency
18094      that we have for MIPS16 symbols.  */
18095   if (fixp->fx_subsy == NULL
18096       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18097 	  || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18098 	      && (jmp_reloc_p (fixp->fx_r_type)
18099 		  || b_reloc_p (fixp->fx_r_type)))
18100 	  || *symbol_get_tc (fixp->fx_addsy)))
18101     return 0;
18102 
18103   return 1;
18104 }
18105 
18106 /* Translate internal representation of relocation info to BFD target
18107    format.  */
18108 
18109 arelent **
18110 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18111 {
18112   static arelent *retval[4];
18113   arelent *reloc;
18114   bfd_reloc_code_real_type code;
18115 
18116   memset (retval, 0, sizeof(retval));
18117   reloc = retval[0] = XCNEW (arelent);
18118   reloc->sym_ptr_ptr = XNEW (asymbol *);
18119   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18120   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18121 
18122   if (fixp->fx_pcrel)
18123     {
18124       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18125 		  || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18126 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18127 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18128 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18129 		  || fixp->fx_r_type == BFD_RELOC_32_PCREL
18130 		  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18131 		  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18132 		  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18133 		  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18134 		  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18135 		  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18136 
18137       /* At this point, fx_addnumber is "symbol offset - pcrel address".
18138 	 Relocations want only the symbol offset.  */
18139       switch (fixp->fx_r_type)
18140 	{
18141 	case BFD_RELOC_MIPS_18_PCREL_S3:
18142 	  reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18143 	  break;
18144 	default:
18145 	  reloc->addend = fixp->fx_addnumber + reloc->address;
18146 	  break;
18147 	}
18148     }
18149   else if (HAVE_IN_PLACE_ADDENDS
18150 	   && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18151 	   && (read_compressed_insn (fixp->fx_frag->fr_literal
18152 				     + fixp->fx_where, 4) >> 26) == 0x3c)
18153     {
18154       /* Shift is 2, unusually, for microMIPS JALX.  Adjust the in-place
18155          addend accordingly.  */
18156       reloc->addend = fixp->fx_addnumber >> 1;
18157     }
18158   else
18159     reloc->addend = fixp->fx_addnumber;
18160 
18161   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18162      entry to be used in the relocation's section offset.  */
18163   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18164     {
18165       reloc->address = reloc->addend;
18166       reloc->addend = 0;
18167     }
18168 
18169   code = fixp->fx_r_type;
18170 
18171   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18172   if (reloc->howto == NULL)
18173     {
18174       as_bad_where (fixp->fx_file, fixp->fx_line,
18175 		    _("cannot represent %s relocation in this object file"
18176 		      " format"),
18177 		    bfd_get_reloc_code_name (code));
18178       retval[0] = NULL;
18179     }
18180 
18181   return retval;
18182 }
18183 
18184 /* Relax a machine dependent frag.  This returns the amount by which
18185    the current size of the frag should change.  */
18186 
18187 int
18188 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18189 {
18190   if (RELAX_BRANCH_P (fragp->fr_subtype))
18191     {
18192       offsetT old_var = fragp->fr_var;
18193 
18194       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
18195 
18196       return fragp->fr_var - old_var;
18197     }
18198 
18199   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18200     {
18201       offsetT old_var = fragp->fr_var;
18202       offsetT new_var = 4;
18203 
18204       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18205 	new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18206       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18207 	new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18208       fragp->fr_var = new_var;
18209 
18210       return new_var - old_var;
18211     }
18212 
18213   if (! RELAX_MIPS16_P (fragp->fr_subtype))
18214     return 0;
18215 
18216   if (!mips16_extended_frag (fragp, sec, stretch))
18217     {
18218       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18219 	{
18220 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18221 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18222 	}
18223       else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18224 	{
18225 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18226 	  return -2;
18227 	}
18228       else
18229 	return 0;
18230     }
18231   else if (!mips16_macro_frag (fragp, sec, stretch))
18232     {
18233       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18234 	{
18235 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18236 	  fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18237 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18238 	}
18239       else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18240 	{
18241 	  fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18242 	  return 2;
18243 	}
18244       else
18245 	return 0;
18246     }
18247   else
18248     {
18249       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18250 	return 0;
18251       else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18252 	{
18253 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18254 	  fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18255 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18256 	}
18257       else
18258 	{
18259 	  fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18260 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18261 	}
18262     }
18263 
18264   return 0;
18265 }
18266 
18267 /* Convert a machine dependent frag.  */
18268 
18269 void
18270 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18271 {
18272   if (RELAX_BRANCH_P (fragp->fr_subtype))
18273     {
18274       char *buf;
18275       unsigned long insn;
18276       fixS *fixp;
18277 
18278       buf = fragp->fr_literal + fragp->fr_fix;
18279       insn = read_insn (buf);
18280 
18281       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18282 	{
18283 	  /* We generate a fixup instead of applying it right now
18284 	     because, if there are linker relaxations, we're going to
18285 	     need the relocations.  */
18286 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18287 			  fragp->fr_symbol, fragp->fr_offset,
18288 			  TRUE, BFD_RELOC_16_PCREL_S2);
18289 	  fixp->fx_file = fragp->fr_file;
18290 	  fixp->fx_line = fragp->fr_line;
18291 
18292 	  buf = write_insn (buf, insn);
18293 	}
18294       else
18295 	{
18296 	  int i;
18297 
18298 	  as_warn_where (fragp->fr_file, fragp->fr_line,
18299 			 _("relaxed out-of-range branch into a jump"));
18300 
18301 	  if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18302 	    goto uncond;
18303 
18304 	  if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18305 	    {
18306 	      /* Reverse the branch.  */
18307 	      switch ((insn >> 28) & 0xf)
18308 		{
18309 		case 4:
18310 		  if ((insn & 0xff000000) == 0x47000000
18311 		      || (insn & 0xff600000) == 0x45600000)
18312 		    {
18313 		      /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18314 			 reversed by tweaking bit 23.  */
18315 		      insn ^= 0x00800000;
18316 		    }
18317 		  else
18318 		    {
18319 		      /* bc[0-3][tf]l? instructions can have the condition
18320 			 reversed by tweaking a single TF bit, and their
18321 			 opcodes all have 0x4???????.  */
18322 		      gas_assert ((insn & 0xf3e00000) == 0x41000000);
18323 		      insn ^= 0x00010000;
18324 		    }
18325 		  break;
18326 
18327 		case 0:
18328 		  /* bltz	0x04000000	bgez	0x04010000
18329 		     bltzal	0x04100000	bgezal	0x04110000  */
18330 		  gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18331 		  insn ^= 0x00010000;
18332 		  break;
18333 
18334 		case 1:
18335 		  /* beq	0x10000000	bne	0x14000000
18336 		     blez	0x18000000	bgtz	0x1c000000  */
18337 		  insn ^= 0x04000000;
18338 		  break;
18339 
18340 		default:
18341 		  abort ();
18342 		}
18343 	    }
18344 
18345 	  if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18346 	    {
18347 	      /* Clear the and-link bit.  */
18348 	      gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18349 
18350 	      /* bltzal		0x04100000	bgezal	0x04110000
18351 		 bltzall	0x04120000	bgezall	0x04130000  */
18352 	      insn &= ~0x00100000;
18353 	    }
18354 
18355 	  /* Branch over the branch (if the branch was likely) or the
18356 	     full jump (not likely case).  Compute the offset from the
18357 	     current instruction to branch to.  */
18358 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18359 	    i = 16;
18360 	  else
18361 	    {
18362 	      /* How many bytes in instructions we've already emitted?  */
18363 	      i = buf - fragp->fr_literal - fragp->fr_fix;
18364 	      /* How many bytes in instructions from here to the end?  */
18365 	      i = fragp->fr_var - i;
18366 	    }
18367 	  /* Convert to instruction count.  */
18368 	  i >>= 2;
18369 	  /* Branch counts from the next instruction.  */
18370 	  i--;
18371 	  insn |= i;
18372 	  /* Branch over the jump.  */
18373 	  buf = write_insn (buf, insn);
18374 
18375 	  /* nop */
18376 	  buf = write_insn (buf, 0);
18377 
18378 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18379 	    {
18380 	      /* beql $0, $0, 2f */
18381 	      insn = 0x50000000;
18382 	      /* Compute the PC offset from the current instruction to
18383 		 the end of the variable frag.  */
18384 	      /* How many bytes in instructions we've already emitted?  */
18385 	      i = buf - fragp->fr_literal - fragp->fr_fix;
18386 	      /* How many bytes in instructions from here to the end?  */
18387 	      i = fragp->fr_var - i;
18388 	      /* Convert to instruction count.  */
18389 	      i >>= 2;
18390 	      /* Don't decrement i, because we want to branch over the
18391 		 delay slot.  */
18392 	      insn |= i;
18393 
18394 	      buf = write_insn (buf, insn);
18395 	      buf = write_insn (buf, 0);
18396 	    }
18397 
18398 	uncond:
18399 	  if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18400 	    {
18401 	      /* j or jal.  */
18402 	      insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18403 		      ? 0x0c000000 : 0x08000000);
18404 
18405 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18406 			      fragp->fr_symbol, fragp->fr_offset,
18407 			      FALSE, BFD_RELOC_MIPS_JMP);
18408 	      fixp->fx_file = fragp->fr_file;
18409 	      fixp->fx_line = fragp->fr_line;
18410 
18411 	      buf = write_insn (buf, insn);
18412 	    }
18413 	  else
18414 	    {
18415 	      unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18416 
18417 	      /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18418 	      insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18419 	      insn |= at << OP_SH_RT;
18420 
18421 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18422 			      fragp->fr_symbol, fragp->fr_offset,
18423 			      FALSE, BFD_RELOC_MIPS_GOT16);
18424 	      fixp->fx_file = fragp->fr_file;
18425 	      fixp->fx_line = fragp->fr_line;
18426 
18427 	      buf = write_insn (buf, insn);
18428 
18429 	      if (mips_opts.isa == ISA_MIPS1)
18430 		/* nop */
18431 		buf = write_insn (buf, 0);
18432 
18433 	      /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18434 	      insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18435 	      insn |= at << OP_SH_RS | at << OP_SH_RT;
18436 
18437 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18438 			      fragp->fr_symbol, fragp->fr_offset,
18439 			      FALSE, BFD_RELOC_LO16);
18440 	      fixp->fx_file = fragp->fr_file;
18441 	      fixp->fx_line = fragp->fr_line;
18442 
18443 	      buf = write_insn (buf, insn);
18444 
18445 	      /* j(al)r $at.  */
18446 	      if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18447 		insn = 0x0000f809;
18448 	      else
18449 		insn = 0x00000008;
18450 	      insn |= at << OP_SH_RS;
18451 
18452 	      buf = write_insn (buf, insn);
18453 	    }
18454 	}
18455 
18456       fragp->fr_fix += fragp->fr_var;
18457       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18458       return;
18459     }
18460 
18461   /* Relax microMIPS branches.  */
18462   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18463     {
18464       char *buf = fragp->fr_literal + fragp->fr_fix;
18465       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18466       bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18467       bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18468       bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18469       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18470       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18471       bfd_boolean short_ds;
18472       unsigned long insn;
18473       fixS *fixp;
18474 
18475       fragp->fr_fix += fragp->fr_var;
18476 
18477       /* Handle 16-bit branches that fit or are forced to fit.  */
18478       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18479 	{
18480 	  /* We generate a fixup instead of applying it right now,
18481 	     because if there is linker relaxation, we're going to
18482 	     need the relocations.  */
18483 	  switch (type)
18484 	    {
18485 	    case 'D':
18486 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18487 			      fragp->fr_symbol, fragp->fr_offset,
18488 			      TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18489 	      break;
18490 	    case 'E':
18491 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18492 			      fragp->fr_symbol, fragp->fr_offset,
18493 			      TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18494 	      break;
18495 	    default:
18496 	      abort ();
18497 	    }
18498 
18499 	  fixp->fx_file = fragp->fr_file;
18500 	  fixp->fx_line = fragp->fr_line;
18501 
18502 	  /* These relocations can have an addend that won't fit in
18503 	     2 octets.  */
18504 	  fixp->fx_no_overflow = 1;
18505 
18506 	  return;
18507 	}
18508 
18509       /* Handle 32-bit branches that fit or are forced to fit.  */
18510       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18511 	  || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18512 	{
18513 	  /* We generate a fixup instead of applying it right now,
18514 	     because if there is linker relaxation, we're going to
18515 	     need the relocations.  */
18516 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18517 			  fragp->fr_symbol, fragp->fr_offset,
18518 			  TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18519 	  fixp->fx_file = fragp->fr_file;
18520 	  fixp->fx_line = fragp->fr_line;
18521 
18522 	  if (type == 0)
18523 	    {
18524 	      insn = read_compressed_insn (buf, 4);
18525 	      buf += 4;
18526 
18527 	      if (nods)
18528 		{
18529 		  /* Check the short-delay-slot bit.  */
18530 		  if (!al || (insn & 0x02000000) != 0)
18531 		    buf = write_compressed_insn (buf, 0x0c00, 2);
18532 		  else
18533 		    buf = write_compressed_insn (buf, 0x00000000, 4);
18534 		}
18535 
18536 	      gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18537 	      return;
18538 	    }
18539 	}
18540 
18541       /* Relax 16-bit branches to 32-bit branches.  */
18542       if (type != 0)
18543 	{
18544 	  insn = read_compressed_insn (buf, 2);
18545 
18546 	  if ((insn & 0xfc00) == 0xcc00)		/* b16  */
18547 	    insn = 0x94000000;				/* beq  */
18548 	  else if ((insn & 0xdc00) == 0x8c00)		/* beqz16/bnez16  */
18549 	    {
18550 	      unsigned long regno;
18551 
18552 	      regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18553 	      regno = micromips_to_32_reg_d_map [regno];
18554 	      insn = ((insn & 0x2000) << 16) | 0x94000000;	/* beq/bne  */
18555 	      insn |= regno << MICROMIPSOP_SH_RS;
18556 	    }
18557 	  else
18558 	    abort ();
18559 
18560 	  /* Nothing else to do, just write it out.  */
18561 	  if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18562 	      || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18563 	    {
18564 	      buf = write_compressed_insn (buf, insn, 4);
18565 	      if (nods)
18566 		buf = write_compressed_insn (buf, 0x0c00, 2);
18567 	      gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18568 	      return;
18569 	    }
18570 	}
18571       else
18572 	insn = read_compressed_insn (buf, 4);
18573 
18574       /* Relax 32-bit branches to a sequence of instructions.  */
18575       as_warn_where (fragp->fr_file, fragp->fr_line,
18576 		     _("relaxed out-of-range branch into a jump"));
18577 
18578       /* Set the short-delay-slot bit.  */
18579       short_ds = !al || (insn & 0x02000000) != 0;
18580 
18581       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18582 	{
18583 	  symbolS *l;
18584 
18585 	  /* Reverse the branch.  */
18586 	  if ((insn & 0xfc000000) == 0x94000000			/* beq  */
18587 	      || (insn & 0xfc000000) == 0xb4000000)		/* bne  */
18588 	    insn ^= 0x20000000;
18589 	  else if ((insn & 0xffe00000) == 0x40000000		/* bltz  */
18590 		   || (insn & 0xffe00000) == 0x40400000		/* bgez  */
18591 		   || (insn & 0xffe00000) == 0x40800000		/* blez  */
18592 		   || (insn & 0xffe00000) == 0x40c00000		/* bgtz  */
18593 		   || (insn & 0xffe00000) == 0x40a00000		/* bnezc  */
18594 		   || (insn & 0xffe00000) == 0x40e00000		/* beqzc  */
18595 		   || (insn & 0xffe00000) == 0x40200000		/* bltzal  */
18596 		   || (insn & 0xffe00000) == 0x40600000		/* bgezal  */
18597 		   || (insn & 0xffe00000) == 0x42200000		/* bltzals  */
18598 		   || (insn & 0xffe00000) == 0x42600000)	/* bgezals  */
18599 	    insn ^= 0x00400000;
18600 	  else if ((insn & 0xffe30000) == 0x43800000		/* bc1f  */
18601 		   || (insn & 0xffe30000) == 0x43a00000		/* bc1t  */
18602 		   || (insn & 0xffe30000) == 0x42800000		/* bc2f  */
18603 		   || (insn & 0xffe30000) == 0x42a00000)	/* bc2t  */
18604 	    insn ^= 0x00200000;
18605 	  else if ((insn & 0xff000000) == 0x83000000		/* BZ.df
18606 								   BNZ.df  */
18607 		    || (insn & 0xff600000) == 0x81600000)	/* BZ.V
18608 								   BNZ.V */
18609 	    insn ^= 0x00800000;
18610 	  else
18611 	    abort ();
18612 
18613 	  if (al)
18614 	    {
18615 	      /* Clear the and-link and short-delay-slot bits.  */
18616 	      gas_assert ((insn & 0xfda00000) == 0x40200000);
18617 
18618 	      /* bltzal  0x40200000	bgezal  0x40600000  */
18619 	      /* bltzals 0x42200000	bgezals 0x42600000  */
18620 	      insn &= ~0x02200000;
18621 	    }
18622 
18623 	  /* Make a label at the end for use with the branch.  */
18624 	  l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18625 	  micromips_label_inc ();
18626 	  S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18627 
18628 	  /* Refer to it.  */
18629 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18630 			  BFD_RELOC_MICROMIPS_16_PCREL_S1);
18631 	  fixp->fx_file = fragp->fr_file;
18632 	  fixp->fx_line = fragp->fr_line;
18633 
18634 	  /* Branch over the jump.  */
18635 	  buf = write_compressed_insn (buf, insn, 4);
18636 
18637 	  if (!compact)
18638 	    {
18639 	      /* nop  */
18640 	      if (insn32)
18641 		buf = write_compressed_insn (buf, 0x00000000, 4);
18642 	      else
18643 		buf = write_compressed_insn (buf, 0x0c00, 2);
18644 	    }
18645 	}
18646 
18647       if (!pic)
18648 	{
18649 	  unsigned long jal = (short_ds || nods
18650 			       ? 0x74000000 : 0xf4000000);	/* jal/s  */
18651 
18652 	  /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18653 	  insn = al ? jal : 0xd4000000;
18654 
18655 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18656 			  fragp->fr_symbol, fragp->fr_offset,
18657 			  FALSE, BFD_RELOC_MICROMIPS_JMP);
18658 	  fixp->fx_file = fragp->fr_file;
18659 	  fixp->fx_line = fragp->fr_line;
18660 
18661 	  buf = write_compressed_insn (buf, insn, 4);
18662 
18663 	  if (compact || nods)
18664 	    {
18665 	      /* nop  */
18666 	      if (insn32)
18667 		buf = write_compressed_insn (buf, 0x00000000, 4);
18668 	      else
18669 		buf = write_compressed_insn (buf, 0x0c00, 2);
18670 	    }
18671 	}
18672       else
18673 	{
18674 	  unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18675 
18676 	  /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18677 	  insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18678 	  insn |= at << MICROMIPSOP_SH_RT;
18679 
18680 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18681 			  fragp->fr_symbol, fragp->fr_offset,
18682 			  FALSE, BFD_RELOC_MICROMIPS_GOT16);
18683 	  fixp->fx_file = fragp->fr_file;
18684 	  fixp->fx_line = fragp->fr_line;
18685 
18686 	  buf = write_compressed_insn (buf, insn, 4);
18687 
18688 	  /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18689 	  insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18690 	  insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18691 
18692 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18693 			  fragp->fr_symbol, fragp->fr_offset,
18694 			  FALSE, BFD_RELOC_MICROMIPS_LO16);
18695 	  fixp->fx_file = fragp->fr_file;
18696 	  fixp->fx_line = fragp->fr_line;
18697 
18698 	  buf = write_compressed_insn (buf, insn, 4);
18699 
18700 	  if (insn32)
18701 	    {
18702 	      /* jr/jalr $at  */
18703 	      insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18704 	      insn |= at << MICROMIPSOP_SH_RS;
18705 
18706 	      buf = write_compressed_insn (buf, insn, 4);
18707 
18708 	      if (compact || nods)
18709 		/* nop  */
18710 		buf = write_compressed_insn (buf, 0x00000000, 4);
18711 	    }
18712 	  else
18713 	    {
18714 	      /* jr/jrc/jalr/jalrs $at  */
18715 	      unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;	/* jalr/s  */
18716 	      unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c  */
18717 
18718 	      insn = al ? jalr : jr;
18719 	      insn |= at << MICROMIPSOP_SH_MJ;
18720 
18721 	      buf = write_compressed_insn (buf, insn, 2);
18722 	      if (al && nods)
18723 		{
18724 		  /* nop  */
18725 		  if (short_ds)
18726 		    buf = write_compressed_insn (buf, 0x0c00, 2);
18727 		  else
18728 		    buf = write_compressed_insn (buf, 0x00000000, 4);
18729 		}
18730 	    }
18731 	}
18732 
18733       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18734       return;
18735     }
18736 
18737   if (RELAX_MIPS16_P (fragp->fr_subtype))
18738     {
18739       int type;
18740       const struct mips_int_operand *operand;
18741       offsetT val;
18742       char *buf;
18743       unsigned int user_length;
18744       bfd_boolean need_reloc;
18745       unsigned long insn;
18746       bfd_boolean mac;
18747       bfd_boolean ext;
18748       segT symsec;
18749 
18750       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18751       operand = mips16_immed_operand (type, FALSE);
18752 
18753       mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
18754       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
18755       val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
18756 
18757       symsec = S_GET_SEGMENT (fragp->fr_symbol);
18758       need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18759 		    || (operand->root.type == OP_PCREL && !mac
18760 			? asec != symsec
18761 			: !bfd_is_abs_section (symsec)));
18762 
18763       if (operand->root.type == OP_PCREL && !mac)
18764 	{
18765 	  const struct mips_pcrel_operand *pcrel_op;
18766 
18767 	  pcrel_op = (const struct mips_pcrel_operand *) operand;
18768 
18769 	  if (pcrel_op->include_isa_bit && !need_reloc)
18770 	    {
18771 	      if (!mips_ignore_branch_isa
18772 		  && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18773 		as_bad_where (fragp->fr_file, fragp->fr_line,
18774 			      _("branch to a symbol in another ISA mode"));
18775 	      else if ((fragp->fr_offset & 0x1) != 0)
18776 		as_bad_where (fragp->fr_file, fragp->fr_line,
18777 			      _("branch to misaligned address (0x%lx)"),
18778 			      (long) val);
18779 	    }
18780 
18781 	  val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
18782 
18783 	  /* Make sure the section winds up with the alignment we have
18784              assumed.  */
18785 	  if (operand->shift > 0)
18786 	    record_alignment (asec, operand->shift);
18787 	}
18788 
18789       if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18790 	  || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18791 	{
18792 	  if (mac)
18793 	    as_warn_where (fragp->fr_file, fragp->fr_line,
18794 			   _("macro instruction expanded into multiple "
18795 			     "instructions in a branch delay slot"));
18796 	  else if (ext)
18797 	    as_warn_where (fragp->fr_file, fragp->fr_line,
18798 			   _("extended instruction in a branch delay slot"));
18799 	}
18800       else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
18801 	as_warn_where (fragp->fr_file, fragp->fr_line,
18802 		       _("macro instruction expanded into multiple "
18803 			 "instructions"));
18804 
18805       buf = fragp->fr_literal + fragp->fr_fix;
18806 
18807       insn = read_compressed_insn (buf, 2);
18808       if (ext)
18809 	insn |= MIPS16_EXTEND;
18810 
18811       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18812 	user_length = 4;
18813       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18814 	user_length = 2;
18815       else
18816 	user_length = 0;
18817 
18818       if (mac)
18819 	{
18820 	  unsigned long reg;
18821 	  unsigned long new;
18822 	  unsigned long op;
18823 	  bfd_boolean e2;
18824 
18825 	  gas_assert (type == 'A' || type == 'B' || type == 'E');
18826 	  gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
18827 
18828 	  e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18829 
18830 	  if (need_reloc)
18831 	    {
18832 	      fixS *fixp;
18833 
18834 	      gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18835 
18836 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18837 			      fragp->fr_symbol, fragp->fr_offset,
18838 			      FALSE, BFD_RELOC_MIPS16_HI16_S);
18839 	      fixp->fx_file = fragp->fr_file;
18840 	      fixp->fx_line = fragp->fr_line;
18841 
18842 	      fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
18843 			      fragp->fr_symbol, fragp->fr_offset,
18844 			      FALSE, BFD_RELOC_MIPS16_LO16);
18845 	      fixp->fx_file = fragp->fr_file;
18846 	      fixp->fx_line = fragp->fr_line;
18847 
18848 	      val = 0;
18849 	    }
18850 
18851 	  switch (insn & 0xf800)
18852 	    {
18853 	    case 0x0800:					/* ADDIU */
18854 	      reg = (insn >> 8) & 0x7;
18855 	      op = 0xf0004800 | (reg << 8);
18856 	      break;
18857 	    case 0xb000:					/* LW */
18858 	      reg = (insn >> 8) & 0x7;
18859 	      op = 0xf0009800 | (reg << 8) | (reg << 5);
18860 	      break;
18861 	    case 0xf800:					/* I64 */
18862 	      reg = (insn >> 5) & 0x7;
18863 	      switch (insn & 0x0700)
18864 		{
18865 		case 0x0400:					/* LD */
18866 		  op = 0xf0003800 | (reg << 8) | (reg << 5);
18867 		  break;
18868 		case 0x0600:					/* DADDIU */
18869 		  op = 0xf000fd00 | (reg << 5);
18870 		  break;
18871 		default:
18872 		  abort ();
18873 		}
18874 	      break;
18875 	    default:
18876 	      abort ();
18877 	    }
18878 
18879 	  new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8);	/* LUI/LI */
18880 	  new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18881 	  buf = write_compressed_insn (buf, new, 4);
18882 	  if (!e2)
18883 	    {
18884 	      new = 0xf4003000 | (reg << 8) | (reg << 5);	/* SLL */
18885 	      buf = write_compressed_insn (buf, new, 4);
18886 	    }
18887 	  op |= mips16_immed_extend (val, 16);
18888 	  buf = write_compressed_insn (buf, op, 4);
18889 
18890 	  fragp->fr_fix += e2 ? 8 : 12;
18891 	}
18892       else
18893 	{
18894 	  unsigned int length = ext ? 4 : 2;
18895 
18896 	  if (need_reloc)
18897 	    {
18898 	      bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18899 	      fixS *fixp;
18900 
18901 	      switch (type)
18902 		{
18903 		case 'p':
18904 		case 'q':
18905 		  reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18906 		  break;
18907 		default:
18908 		  break;
18909 		}
18910 	      if (mac || reloc == BFD_RELOC_NONE)
18911 		as_bad_where (fragp->fr_file, fragp->fr_line,
18912 			      _("unsupported relocation"));
18913 	      else if (ext)
18914 		{
18915 		  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18916 				  fragp->fr_symbol, fragp->fr_offset,
18917 				  TRUE, reloc);
18918 		  fixp->fx_file = fragp->fr_file;
18919 		  fixp->fx_line = fragp->fr_line;
18920 		}
18921 	      else
18922 		as_bad_where (fragp->fr_file, fragp->fr_line,
18923 			      _("invalid unextended operand value"));
18924 	    }
18925 	  else
18926 	    mips16_immed (fragp->fr_file, fragp->fr_line, type,
18927 			  BFD_RELOC_UNUSED, val, user_length, &insn);
18928 
18929 	  gas_assert (mips16_opcode_length (insn) == length);
18930 	  write_compressed_insn (buf, insn, length);
18931 	  fragp->fr_fix += length;
18932 	}
18933     }
18934   else
18935     {
18936       relax_substateT subtype = fragp->fr_subtype;
18937       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18938       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18939       int first, second;
18940       fixS *fixp;
18941 
18942       first = RELAX_FIRST (subtype);
18943       second = RELAX_SECOND (subtype);
18944       fixp = (fixS *) fragp->fr_opcode;
18945 
18946       /* If the delay slot chosen does not match the size of the instruction,
18947          then emit a warning.  */
18948       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18949 	   || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18950 	{
18951 	  relax_substateT s;
18952 	  const char *msg;
18953 
18954 	  s = subtype & (RELAX_DELAY_SLOT_16BIT
18955 			 | RELAX_DELAY_SLOT_SIZE_FIRST
18956 			 | RELAX_DELAY_SLOT_SIZE_SECOND);
18957 	  msg = macro_warning (s);
18958 	  if (msg != NULL)
18959 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18960 	  subtype &= ~s;
18961 	}
18962 
18963       /* Possibly emit a warning if we've chosen the longer option.  */
18964       if (use_second == second_longer)
18965 	{
18966 	  relax_substateT s;
18967 	  const char *msg;
18968 
18969 	  s = (subtype
18970 	       & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18971 	  msg = macro_warning (s);
18972 	  if (msg != NULL)
18973 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18974 	  subtype &= ~s;
18975 	}
18976 
18977       /* Go through all the fixups for the first sequence.  Disable them
18978 	 (by marking them as done) if we're going to use the second
18979 	 sequence instead.  */
18980       while (fixp
18981 	     && fixp->fx_frag == fragp
18982 	     && fixp->fx_where < fragp->fr_fix - second)
18983 	{
18984 	  if (subtype & RELAX_USE_SECOND)
18985 	    fixp->fx_done = 1;
18986 	  fixp = fixp->fx_next;
18987 	}
18988 
18989       /* Go through the fixups for the second sequence.  Disable them if
18990 	 we're going to use the first sequence, otherwise adjust their
18991 	 addresses to account for the relaxation.  */
18992       while (fixp && fixp->fx_frag == fragp)
18993 	{
18994 	  if (subtype & RELAX_USE_SECOND)
18995 	    fixp->fx_where -= first;
18996 	  else
18997 	    fixp->fx_done = 1;
18998 	  fixp = fixp->fx_next;
18999 	}
19000 
19001       /* Now modify the frag contents.  */
19002       if (subtype & RELAX_USE_SECOND)
19003 	{
19004 	  char *start;
19005 
19006 	  start = fragp->fr_literal + fragp->fr_fix - first - second;
19007 	  memmove (start, start + first, second);
19008 	  fragp->fr_fix -= first;
19009 	}
19010       else
19011 	fragp->fr_fix -= second;
19012     }
19013 }
19014 
19015 /* This function is called after the relocs have been generated.
19016    We've been storing mips16 text labels as odd.  Here we convert them
19017    back to even for the convenience of the debugger.  */
19018 
19019 void
19020 mips_frob_file_after_relocs (void)
19021 {
19022   asymbol **syms;
19023   unsigned int count, i;
19024 
19025   syms = bfd_get_outsymbols (stdoutput);
19026   count = bfd_get_symcount (stdoutput);
19027   for (i = 0; i < count; i++, syms++)
19028     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19029 	&& ((*syms)->value & 1) != 0)
19030       {
19031 	(*syms)->value &= ~1;
19032 	/* If the symbol has an odd size, it was probably computed
19033 	   incorrectly, so adjust that as well.  */
19034 	if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19035 	  ++elf_symbol (*syms)->internal_elf_sym.st_size;
19036       }
19037 }
19038 
19039 /* This function is called whenever a label is defined, including fake
19040    labels instantiated off the dot special symbol.  It is used when
19041    handling branch delays; if a branch has a label, we assume we cannot
19042    move it.  This also bumps the value of the symbol by 1 in compressed
19043    code.  */
19044 
19045 static void
19046 mips_record_label (symbolS *sym)
19047 {
19048   segment_info_type *si = seg_info (now_seg);
19049   struct insn_label_list *l;
19050 
19051   if (free_insn_labels == NULL)
19052     l = XNEW (struct insn_label_list);
19053   else
19054     {
19055       l = free_insn_labels;
19056       free_insn_labels = l->next;
19057     }
19058 
19059   l->label = sym;
19060   l->next = si->label_list;
19061   si->label_list = l;
19062 }
19063 
19064 /* This function is called as tc_frob_label() whenever a label is defined
19065    and adds a DWARF-2 record we only want for true labels.  */
19066 
19067 void
19068 mips_define_label (symbolS *sym)
19069 {
19070   mips_record_label (sym);
19071   dwarf2_emit_label (sym);
19072 }
19073 
19074 /* This function is called by tc_new_dot_label whenever a new dot symbol
19075    is defined.  */
19076 
19077 void
19078 mips_add_dot_label (symbolS *sym)
19079 {
19080   mips_record_label (sym);
19081   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19082     mips_compressed_mark_label (sym);
19083 }
19084 
19085 /* Converting ASE flags from internal to .MIPS.abiflags values.  */
19086 static unsigned int
19087 mips_convert_ase_flags (int ase)
19088 {
19089   unsigned int ext_ases = 0;
19090 
19091   if (ase & ASE_DSP)
19092     ext_ases |= AFL_ASE_DSP;
19093   if (ase & ASE_DSPR2)
19094     ext_ases |= AFL_ASE_DSPR2;
19095   if (ase & ASE_DSPR3)
19096     ext_ases |= AFL_ASE_DSPR3;
19097   if (ase & ASE_EVA)
19098     ext_ases |= AFL_ASE_EVA;
19099   if (ase & ASE_MCU)
19100     ext_ases |= AFL_ASE_MCU;
19101   if (ase & ASE_MDMX)
19102     ext_ases |= AFL_ASE_MDMX;
19103   if (ase & ASE_MIPS3D)
19104     ext_ases |= AFL_ASE_MIPS3D;
19105   if (ase & ASE_MT)
19106     ext_ases |= AFL_ASE_MT;
19107   if (ase & ASE_SMARTMIPS)
19108     ext_ases |= AFL_ASE_SMARTMIPS;
19109   if (ase & ASE_VIRT)
19110     ext_ases |= AFL_ASE_VIRT;
19111   if (ase & ASE_MSA)
19112     ext_ases |= AFL_ASE_MSA;
19113   if (ase & ASE_XPA)
19114     ext_ases |= AFL_ASE_XPA;
19115   if (ase & ASE_MIPS16E2)
19116     ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
19117   if (ase & ASE_CRC)
19118     ext_ases |= AFL_ASE_CRC;
19119   if (ase & ASE_GINV)
19120     ext_ases |= AFL_ASE_GINV;
19121 
19122   return ext_ases;
19123 }
19124 /* Some special processing for a MIPS ELF file.  */
19125 
19126 void
19127 mips_elf_final_processing (void)
19128 {
19129   int fpabi;
19130   Elf_Internal_ABIFlags_v0 flags;
19131 
19132   flags.version = 0;
19133   flags.isa_rev = 0;
19134   switch (file_mips_opts.isa)
19135     {
19136     case INSN_ISA1:
19137       flags.isa_level = 1;
19138       break;
19139     case INSN_ISA2:
19140       flags.isa_level = 2;
19141       break;
19142     case INSN_ISA3:
19143       flags.isa_level = 3;
19144       break;
19145     case INSN_ISA4:
19146       flags.isa_level = 4;
19147       break;
19148     case INSN_ISA5:
19149       flags.isa_level = 5;
19150       break;
19151     case INSN_ISA32:
19152       flags.isa_level = 32;
19153       flags.isa_rev = 1;
19154       break;
19155     case INSN_ISA32R2:
19156       flags.isa_level = 32;
19157       flags.isa_rev = 2;
19158       break;
19159     case INSN_ISA32R3:
19160       flags.isa_level = 32;
19161       flags.isa_rev = 3;
19162       break;
19163     case INSN_ISA32R5:
19164       flags.isa_level = 32;
19165       flags.isa_rev = 5;
19166       break;
19167     case INSN_ISA32R6:
19168       flags.isa_level = 32;
19169       flags.isa_rev = 6;
19170       break;
19171     case INSN_ISA64:
19172       flags.isa_level = 64;
19173       flags.isa_rev = 1;
19174       break;
19175     case INSN_ISA64R2:
19176       flags.isa_level = 64;
19177       flags.isa_rev = 2;
19178       break;
19179     case INSN_ISA64R3:
19180       flags.isa_level = 64;
19181       flags.isa_rev = 3;
19182       break;
19183     case INSN_ISA64R5:
19184       flags.isa_level = 64;
19185       flags.isa_rev = 5;
19186       break;
19187     case INSN_ISA64R6:
19188       flags.isa_level = 64;
19189       flags.isa_rev = 6;
19190       break;
19191     }
19192 
19193   flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19194   flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19195 		    : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19196 		    : (file_mips_opts.fp == 64) ? AFL_REG_64
19197 		    : AFL_REG_32;
19198   flags.cpr2_size = AFL_REG_NONE;
19199   flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19200                                            Tag_GNU_MIPS_ABI_FP);
19201   flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19202   flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19203   if (file_ase_mips16)
19204     flags.ases |= AFL_ASE_MIPS16;
19205   if (file_ase_micromips)
19206     flags.ases |= AFL_ASE_MICROMIPS;
19207   flags.flags1 = 0;
19208   if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19209        || file_mips_opts.fp == 64)
19210       && file_mips_opts.oddspreg)
19211     flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19212   flags.flags2 = 0;
19213 
19214   bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19215 				     ((Elf_External_ABIFlags_v0 *)
19216 				     mips_flags_frag));
19217 
19218   /* Write out the register information.  */
19219   if (mips_abi != N64_ABI)
19220     {
19221       Elf32_RegInfo s;
19222 
19223       s.ri_gprmask = mips_gprmask;
19224       s.ri_cprmask[0] = mips_cprmask[0];
19225       s.ri_cprmask[1] = mips_cprmask[1];
19226       s.ri_cprmask[2] = mips_cprmask[2];
19227       s.ri_cprmask[3] = mips_cprmask[3];
19228       /* The gp_value field is set by the MIPS ELF backend.  */
19229 
19230       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19231 				       ((Elf32_External_RegInfo *)
19232 					mips_regmask_frag));
19233     }
19234   else
19235     {
19236       Elf64_Internal_RegInfo s;
19237 
19238       s.ri_gprmask = mips_gprmask;
19239       s.ri_pad = 0;
19240       s.ri_cprmask[0] = mips_cprmask[0];
19241       s.ri_cprmask[1] = mips_cprmask[1];
19242       s.ri_cprmask[2] = mips_cprmask[2];
19243       s.ri_cprmask[3] = mips_cprmask[3];
19244       /* The gp_value field is set by the MIPS ELF backend.  */
19245 
19246       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19247 				       ((Elf64_External_RegInfo *)
19248 					mips_regmask_frag));
19249     }
19250 
19251   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
19252      sort of BFD interface for this.  */
19253   if (mips_any_noreorder)
19254     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19255   if (mips_pic != NO_PIC)
19256     {
19257       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19258       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19259     }
19260   if (mips_abicalls)
19261     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19262 
19263   /* Set MIPS ELF flags for ASEs.  Note that not all ASEs have flags
19264      defined at present; this might need to change in future.  */
19265   if (file_ase_mips16)
19266     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19267   if (file_ase_micromips)
19268     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19269   if (file_mips_opts.ase & ASE_MDMX)
19270     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19271 
19272   /* Set the MIPS ELF ABI flags.  */
19273   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19274     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19275   else if (mips_abi == O64_ABI)
19276     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19277   else if (mips_abi == EABI_ABI)
19278     {
19279       if (file_mips_opts.gp == 64)
19280 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19281       else
19282 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19283     }
19284 
19285   /* Nothing to do for N32_ABI or N64_ABI.  */
19286 
19287   if (mips_32bitmode)
19288     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19289 
19290   if (mips_nan2008 == 1)
19291     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19292 
19293   /* 32 bit code with 64 bit FP registers.  */
19294   fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19295 				    Tag_GNU_MIPS_ABI_FP);
19296   if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19297     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19298 }
19299 
19300 typedef struct proc {
19301   symbolS *func_sym;
19302   symbolS *func_end_sym;
19303   unsigned long reg_mask;
19304   unsigned long reg_offset;
19305   unsigned long fpreg_mask;
19306   unsigned long fpreg_offset;
19307   unsigned long frame_offset;
19308   unsigned long frame_reg;
19309   unsigned long pc_reg;
19310 } procS;
19311 
19312 static procS cur_proc;
19313 static procS *cur_proc_ptr;
19314 static int numprocs;
19315 
19316 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
19317    as "2", and a normal nop as "0".  */
19318 
19319 #define NOP_OPCODE_MIPS		0
19320 #define NOP_OPCODE_MIPS16	1
19321 #define NOP_OPCODE_MICROMIPS	2
19322 
19323 char
19324 mips_nop_opcode (void)
19325 {
19326   if (seg_info (now_seg)->tc_segment_info_data.micromips)
19327     return NOP_OPCODE_MICROMIPS;
19328   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19329     return NOP_OPCODE_MIPS16;
19330   else
19331     return NOP_OPCODE_MIPS;
19332 }
19333 
19334 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
19335    32-bit microMIPS NOPs here (if applicable).  */
19336 
19337 void
19338 mips_handle_align (fragS *fragp)
19339 {
19340   char nop_opcode;
19341   char *p;
19342   int bytes, size, excess;
19343   valueT opcode;
19344 
19345   if (fragp->fr_type != rs_align_code)
19346     return;
19347 
19348   p = fragp->fr_literal + fragp->fr_fix;
19349   nop_opcode = *p;
19350   switch (nop_opcode)
19351     {
19352     case NOP_OPCODE_MICROMIPS:
19353       opcode = micromips_nop32_insn.insn_opcode;
19354       size = 4;
19355       break;
19356     case NOP_OPCODE_MIPS16:
19357       opcode = mips16_nop_insn.insn_opcode;
19358       size = 2;
19359       break;
19360     case NOP_OPCODE_MIPS:
19361     default:
19362       opcode = nop_insn.insn_opcode;
19363       size = 4;
19364       break;
19365     }
19366 
19367   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19368   excess = bytes % size;
19369 
19370   /* Handle the leading part if we're not inserting a whole number of
19371      instructions, and make it the end of the fixed part of the frag.
19372      Try to fit in a short microMIPS NOP if applicable and possible,
19373      and use zeroes otherwise.  */
19374   gas_assert (excess < 4);
19375   fragp->fr_fix += excess;
19376   switch (excess)
19377     {
19378     case 3:
19379       *p++ = '\0';
19380       /* Fall through.  */
19381     case 2:
19382       if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19383 	{
19384 	  p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19385 	  break;
19386 	}
19387       *p++ = '\0';
19388       /* Fall through.  */
19389     case 1:
19390       *p++ = '\0';
19391       /* Fall through.  */
19392     case 0:
19393       break;
19394     }
19395 
19396   md_number_to_chars (p, opcode, size);
19397   fragp->fr_var = size;
19398 }
19399 
19400 static long
19401 get_number (void)
19402 {
19403   int negative = 0;
19404   long val = 0;
19405 
19406   if (*input_line_pointer == '-')
19407     {
19408       ++input_line_pointer;
19409       negative = 1;
19410     }
19411   if (!ISDIGIT (*input_line_pointer))
19412     as_bad (_("expected simple number"));
19413   if (input_line_pointer[0] == '0')
19414     {
19415       if (input_line_pointer[1] == 'x')
19416 	{
19417 	  input_line_pointer += 2;
19418 	  while (ISXDIGIT (*input_line_pointer))
19419 	    {
19420 	      val <<= 4;
19421 	      val |= hex_value (*input_line_pointer++);
19422 	    }
19423 	  return negative ? -val : val;
19424 	}
19425       else
19426 	{
19427 	  ++input_line_pointer;
19428 	  while (ISDIGIT (*input_line_pointer))
19429 	    {
19430 	      val <<= 3;
19431 	      val |= *input_line_pointer++ - '0';
19432 	    }
19433 	  return negative ? -val : val;
19434 	}
19435     }
19436   if (!ISDIGIT (*input_line_pointer))
19437     {
19438       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19439 	      *input_line_pointer, *input_line_pointer);
19440       as_warn (_("invalid number"));
19441       return -1;
19442     }
19443   while (ISDIGIT (*input_line_pointer))
19444     {
19445       val *= 10;
19446       val += *input_line_pointer++ - '0';
19447     }
19448   return negative ? -val : val;
19449 }
19450 
19451 /* The .file directive; just like the usual .file directive, but there
19452    is an initial number which is the ECOFF file index.  In the non-ECOFF
19453    case .file implies DWARF-2.  */
19454 
19455 static void
19456 s_mips_file (int x ATTRIBUTE_UNUSED)
19457 {
19458   static int first_file_directive = 0;
19459 
19460   if (ECOFF_DEBUGGING)
19461     {
19462       get_number ();
19463       s_app_file (0);
19464     }
19465   else
19466     {
19467       char *filename;
19468 
19469       filename = dwarf2_directive_filename ();
19470 
19471       /* Versions of GCC up to 3.1 start files with a ".file"
19472 	 directive even for stabs output.  Make sure that this
19473 	 ".file" is handled.  Note that you need a version of GCC
19474          after 3.1 in order to support DWARF-2 on MIPS.  */
19475       if (filename != NULL && ! first_file_directive)
19476 	{
19477 	  (void) new_logical_line (filename, -1);
19478 	  s_app_file_string (filename, 0);
19479 	}
19480       first_file_directive = 1;
19481     }
19482 }
19483 
19484 /* The .loc directive, implying DWARF-2.  */
19485 
19486 static void
19487 s_mips_loc (int x ATTRIBUTE_UNUSED)
19488 {
19489   if (!ECOFF_DEBUGGING)
19490     dwarf2_directive_loc (0);
19491 }
19492 
19493 /* The .end directive.  */
19494 
19495 static void
19496 s_mips_end (int x ATTRIBUTE_UNUSED)
19497 {
19498   symbolS *p;
19499 
19500   /* Following functions need their own .frame and .cprestore directives.  */
19501   mips_frame_reg_valid = 0;
19502   mips_cprestore_valid = 0;
19503 
19504   if (!is_end_of_line[(unsigned char) *input_line_pointer])
19505     {
19506       p = get_symbol ();
19507       demand_empty_rest_of_line ();
19508     }
19509   else
19510     p = NULL;
19511 
19512   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19513     as_warn (_(".end not in text section"));
19514 
19515   if (!cur_proc_ptr)
19516     {
19517       as_warn (_(".end directive without a preceding .ent directive"));
19518       demand_empty_rest_of_line ();
19519       return;
19520     }
19521 
19522   if (p != NULL)
19523     {
19524       gas_assert (S_GET_NAME (p));
19525       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19526 	as_warn (_(".end symbol does not match .ent symbol"));
19527 
19528       if (debug_type == DEBUG_STABS)
19529 	stabs_generate_asm_endfunc (S_GET_NAME (p),
19530 				    S_GET_NAME (p));
19531     }
19532   else
19533     as_warn (_(".end directive missing or unknown symbol"));
19534 
19535   /* Create an expression to calculate the size of the function.  */
19536   if (p && cur_proc_ptr)
19537     {
19538       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19539       expressionS *exp = XNEW (expressionS);
19540 
19541       obj->size = exp;
19542       exp->X_op = O_subtract;
19543       exp->X_add_symbol = symbol_temp_new_now ();
19544       exp->X_op_symbol = p;
19545       exp->X_add_number = 0;
19546 
19547       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19548     }
19549 
19550 #ifdef md_flush_pending_output
19551   md_flush_pending_output ();
19552 #endif
19553 
19554   /* Generate a .pdr section.  */
19555   if (!ECOFF_DEBUGGING && mips_flag_pdr)
19556     {
19557       segT saved_seg = now_seg;
19558       subsegT saved_subseg = now_subseg;
19559       expressionS exp;
19560       char *fragp;
19561 
19562       gas_assert (pdr_seg);
19563       subseg_set (pdr_seg, 0);
19564 
19565       /* Write the symbol.  */
19566       exp.X_op = O_symbol;
19567       exp.X_add_symbol = p;
19568       exp.X_add_number = 0;
19569       emit_expr (&exp, 4);
19570 
19571       fragp = frag_more (7 * 4);
19572 
19573       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19574       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19575       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19576       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19577       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19578       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19579       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19580 
19581       subseg_set (saved_seg, saved_subseg);
19582     }
19583 
19584   cur_proc_ptr = NULL;
19585 }
19586 
19587 /* The .aent and .ent directives.  */
19588 
19589 static void
19590 s_mips_ent (int aent)
19591 {
19592   symbolS *symbolP;
19593 
19594   symbolP = get_symbol ();
19595   if (*input_line_pointer == ',')
19596     ++input_line_pointer;
19597   SKIP_WHITESPACE ();
19598   if (ISDIGIT (*input_line_pointer)
19599       || *input_line_pointer == '-')
19600     get_number ();
19601 
19602   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
19603     as_warn (_(".ent or .aent not in text section"));
19604 
19605   if (!aent && cur_proc_ptr)
19606     as_warn (_("missing .end"));
19607 
19608   if (!aent)
19609     {
19610       /* This function needs its own .frame and .cprestore directives.  */
19611       mips_frame_reg_valid = 0;
19612       mips_cprestore_valid = 0;
19613 
19614       cur_proc_ptr = &cur_proc;
19615       memset (cur_proc_ptr, '\0', sizeof (procS));
19616 
19617       cur_proc_ptr->func_sym = symbolP;
19618 
19619       ++numprocs;
19620 
19621       if (debug_type == DEBUG_STABS)
19622         stabs_generate_asm_func (S_GET_NAME (symbolP),
19623 				 S_GET_NAME (symbolP));
19624     }
19625 
19626   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19627 
19628   demand_empty_rest_of_line ();
19629 }
19630 
19631 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
19632    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
19633    s_mips_frame is used so that we can set the PDR information correctly.
19634    We can't use the ecoff routines because they make reference to the ecoff
19635    symbol table (in the mdebug section).  */
19636 
19637 static void
19638 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
19639 {
19640   if (ECOFF_DEBUGGING)
19641     s_ignore (ignore);
19642   else
19643     {
19644       long val;
19645 
19646       if (cur_proc_ptr == (procS *) NULL)
19647 	{
19648 	  as_warn (_(".frame outside of .ent"));
19649 	  demand_empty_rest_of_line ();
19650 	  return;
19651 	}
19652 
19653       cur_proc_ptr->frame_reg = tc_get_register (1);
19654 
19655       SKIP_WHITESPACE ();
19656       if (*input_line_pointer++ != ','
19657 	  || get_absolute_expression_and_terminator (&val) != ',')
19658 	{
19659 	  as_warn (_("bad .frame directive"));
19660 	  --input_line_pointer;
19661 	  demand_empty_rest_of_line ();
19662 	  return;
19663 	}
19664 
19665       cur_proc_ptr->frame_offset = val;
19666       cur_proc_ptr->pc_reg = tc_get_register (0);
19667 
19668       demand_empty_rest_of_line ();
19669     }
19670 }
19671 
19672 /* The .fmask and .mask directives. If the mdebug section is present
19673    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
19674    embedded targets, s_mips_mask is used so that we can set the PDR
19675    information correctly. We can't use the ecoff routines because they
19676    make reference to the ecoff symbol table (in the mdebug section).  */
19677 
19678 static void
19679 s_mips_mask (int reg_type)
19680 {
19681   if (ECOFF_DEBUGGING)
19682     s_ignore (reg_type);
19683   else
19684     {
19685       long mask, off;
19686 
19687       if (cur_proc_ptr == (procS *) NULL)
19688 	{
19689 	  as_warn (_(".mask/.fmask outside of .ent"));
19690 	  demand_empty_rest_of_line ();
19691 	  return;
19692 	}
19693 
19694       if (get_absolute_expression_and_terminator (&mask) != ',')
19695 	{
19696 	  as_warn (_("bad .mask/.fmask directive"));
19697 	  --input_line_pointer;
19698 	  demand_empty_rest_of_line ();
19699 	  return;
19700 	}
19701 
19702       off = get_absolute_expression ();
19703 
19704       if (reg_type == 'F')
19705 	{
19706 	  cur_proc_ptr->fpreg_mask = mask;
19707 	  cur_proc_ptr->fpreg_offset = off;
19708 	}
19709       else
19710 	{
19711 	  cur_proc_ptr->reg_mask = mask;
19712 	  cur_proc_ptr->reg_offset = off;
19713 	}
19714 
19715       demand_empty_rest_of_line ();
19716     }
19717 }
19718 
19719 /* A table describing all the processors gas knows about.  Names are
19720    matched in the order listed.
19721 
19722    To ease comparison, please keep this table in the same order as
19723    gcc's mips_cpu_info_table[].  */
19724 static const struct mips_cpu_info mips_cpu_info_table[] =
19725 {
19726   /* Entries for generic ISAs */
19727   { "mips1",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS1,    CPU_R3000 },
19728   { "mips2",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS2,    CPU_R6000 },
19729   { "mips3",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS3,    CPU_R4000 },
19730   { "mips4",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS4,    CPU_R8000 },
19731   { "mips5",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS5,    CPU_MIPS5 },
19732   { "mips32",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS32,   CPU_MIPS32 },
19733   { "mips32r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R2, CPU_MIPS32R2 },
19734   { "mips32r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R3, CPU_MIPS32R3 },
19735   { "mips32r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R5, CPU_MIPS32R5 },
19736   { "mips32r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R6, CPU_MIPS32R6 },
19737   { "mips64",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS64,   CPU_MIPS64 },
19738   { "mips64r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R2, CPU_MIPS64R2 },
19739   { "mips64r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R3, CPU_MIPS64R3 },
19740   { "mips64r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R5, CPU_MIPS64R5 },
19741   { "mips64r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R6, CPU_MIPS64R6 },
19742 
19743   /* MIPS I */
19744   { "r3000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
19745   { "r2000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
19746   { "r3900",          0, 0,			ISA_MIPS1,    CPU_R3900 },
19747 
19748   /* MIPS II */
19749   { "r6000",          0, 0,			ISA_MIPS2,    CPU_R6000 },
19750 
19751   /* MIPS III */
19752   { "r4000",          0, 0,			ISA_MIPS3,    CPU_R4000 },
19753   { "r4010",          0, 0,			ISA_MIPS2,    CPU_R4010 },
19754   { "vr4100",         0, 0,			ISA_MIPS3,    CPU_VR4100 },
19755   { "vr4111",         0, 0,			ISA_MIPS3,    CPU_R4111 },
19756   { "vr4120",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
19757   { "vr4130",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
19758   { "vr4181",         0, 0,			ISA_MIPS3,    CPU_R4111 },
19759   { "vr4300",         0, 0,			ISA_MIPS3,    CPU_R4300 },
19760   { "r4400",          0, 0,			ISA_MIPS3,    CPU_R4400 },
19761   { "r4600",          0, 0,			ISA_MIPS3,    CPU_R4600 },
19762   { "orion",          0, 0,			ISA_MIPS3,    CPU_R4600 },
19763   { "r4650",          0, 0,			ISA_MIPS3,    CPU_R4650 },
19764   { "r5900",          0, 0,			ISA_MIPS3,    CPU_R5900 },
19765   /* ST Microelectronics Loongson 2E and 2F cores */
19766   { "loongson2e",     0, 0,			ISA_MIPS3,    CPU_LOONGSON_2E },
19767   { "loongson2f",     0, 0,			ISA_MIPS3,    CPU_LOONGSON_2F },
19768 
19769   /* MIPS IV */
19770   { "r8000",          0, 0,			ISA_MIPS4,    CPU_R8000 },
19771   { "r10000",         0, 0,			ISA_MIPS4,    CPU_R10000 },
19772   { "r12000",         0, 0,			ISA_MIPS4,    CPU_R12000 },
19773   { "r14000",         0, 0,			ISA_MIPS4,    CPU_R14000 },
19774   { "r16000",         0, 0,			ISA_MIPS4,    CPU_R16000 },
19775   { "vr5000",         0, 0,			ISA_MIPS4,    CPU_R5000 },
19776   { "vr5400",         0, 0,			ISA_MIPS4,    CPU_VR5400 },
19777   { "vr5500",         0, 0,			ISA_MIPS4,    CPU_VR5500 },
19778   { "rm5200",         0, 0,			ISA_MIPS4,    CPU_R5000 },
19779   { "rm5230",         0, 0,			ISA_MIPS4,    CPU_R5000 },
19780   { "rm5231",         0, 0,			ISA_MIPS4,    CPU_R5000 },
19781   { "rm5261",         0, 0,			ISA_MIPS4,    CPU_R5000 },
19782   { "rm5721",         0, 0,			ISA_MIPS4,    CPU_R5000 },
19783   { "rm7000",         0, 0,			ISA_MIPS4,    CPU_RM7000 },
19784   { "rm9000",         0, 0,			ISA_MIPS4,    CPU_RM9000 },
19785 
19786   /* MIPS 32 */
19787   { "4kc",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
19788   { "4km",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
19789   { "4kp",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
19790   { "4ksc",           0, ASE_SMARTMIPS,		ISA_MIPS32,   CPU_MIPS32 },
19791 
19792   /* MIPS 32 Release 2 */
19793   { "4kec",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19794   { "4kem",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19795   { "4kep",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19796   { "4ksd",           0, ASE_SMARTMIPS,		ISA_MIPS32R2, CPU_MIPS32R2 },
19797   { "m4k",            0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19798   { "m4kp",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19799   { "m14k",           0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
19800   { "m14kc",          0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
19801   { "m14ke",          0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19802 						ISA_MIPS32R2, CPU_MIPS32R2 },
19803   { "m14kec",         0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19804 						ISA_MIPS32R2, CPU_MIPS32R2 },
19805   { "24kc",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19806   { "24kf2_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19807   { "24kf",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19808   { "24kf1_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19809   /* Deprecated forms of the above.  */
19810   { "24kfx",          0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19811   { "24kx",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
19812   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
19813   { "24kec",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
19814   { "24kef2_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
19815   { "24kef",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
19816   { "24kef1_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
19817   /* Deprecated forms of the above.  */
19818   { "24kefx",         0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
19819   { "24kex",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
19820   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
19821   { "34kc",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19822   { "34kf2_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19823   { "34kf",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19824   { "34kf1_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19825   /* Deprecated forms of the above.  */
19826   { "34kfx",          0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19827   { "34kx",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19828   /* 34Kn is a 34kc without DSP.  */
19829   { "34kn",           0, ASE_MT,		ISA_MIPS32R2, CPU_MIPS32R2 },
19830   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
19831   { "74kc",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
19832   { "74kf2_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
19833   { "74kf",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
19834   { "74kf1_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
19835   { "74kf3_2",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
19836   /* Deprecated forms of the above.  */
19837   { "74kfx",          0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
19838   { "74kx",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
19839   /* 1004K cores are multiprocessor versions of the 34K.  */
19840   { "1004kc",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19841   { "1004kf2_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19842   { "1004kf",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19843   { "1004kf1_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19844   /* interaptiv is the new name for 1004kf */
19845   { "interaptiv",     0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
19846   { "interaptiv-mr2", 0,
19847     ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19848     ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
19849   /* M5100 family */
19850   { "m5100",          0, ASE_MCU,		ISA_MIPS32R5, CPU_MIPS32R5 },
19851   { "m5101",          0, ASE_MCU,		ISA_MIPS32R5, CPU_MIPS32R5 },
19852   /* P5600 with EVA and Virtualization ASEs, other ASEs are optional.  */
19853   { "p5600",          0, ASE_VIRT | ASE_EVA | ASE_XPA,	ISA_MIPS32R5, CPU_MIPS32R5 },
19854 
19855   /* MIPS 64 */
19856   { "5kc",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
19857   { "5kf",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
19858   { "20kc",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
19859   { "25kf",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
19860 
19861   /* Broadcom SB-1 CPU core */
19862   { "sb1",            0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
19863   /* Broadcom SB-1A CPU core */
19864   { "sb1a",           0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
19865 
19866   { "loongson3a",     0, 0,			ISA_MIPS64R2, CPU_LOONGSON_3A },
19867 
19868   /* MIPS 64 Release 2 */
19869 
19870   /* Cavium Networks Octeon CPU core */
19871   { "octeon",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON },
19872   { "octeon+",	      0, 0,			ISA_MIPS64R2, CPU_OCTEONP },
19873   { "octeon2",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON2 },
19874   { "octeon3",	      0, ASE_VIRT | ASE_VIRT64,	ISA_MIPS64R5, CPU_OCTEON3 },
19875 
19876   /* RMI Xlr */
19877   { "xlr",	      0, 0,			ISA_MIPS64,   CPU_XLR },
19878 
19879   /* Broadcom XLP.
19880      XLP is mostly like XLR, with the prominent exception that it is
19881      MIPS64R2 rather than MIPS64.  */
19882   { "xlp",	      0, 0,			ISA_MIPS64R2, CPU_XLR },
19883 
19884   /* MIPS 64 Release 6 */
19885   { "i6400",	      0, ASE_MSA,		ISA_MIPS64R6, CPU_MIPS64R6},
19886   { "p6600",	      0, ASE_VIRT | ASE_MSA,	ISA_MIPS64R6, CPU_MIPS64R6},
19887 
19888   /* End marker */
19889   { NULL, 0, 0, 0, 0 }
19890 };
19891 
19892 
19893 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19894    with a final "000" replaced by "k".  Ignore case.
19895 
19896    Note: this function is shared between GCC and GAS.  */
19897 
19898 static bfd_boolean
19899 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19900 {
19901   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19902     given++, canonical++;
19903 
19904   return ((*given == 0 && *canonical == 0)
19905 	  || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19906 }
19907 
19908 
19909 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19910    CPU name.  We've traditionally allowed a lot of variation here.
19911 
19912    Note: this function is shared between GCC and GAS.  */
19913 
19914 static bfd_boolean
19915 mips_matching_cpu_name_p (const char *canonical, const char *given)
19916 {
19917   /* First see if the name matches exactly, or with a final "000"
19918      turned into "k".  */
19919   if (mips_strict_matching_cpu_name_p (canonical, given))
19920     return TRUE;
19921 
19922   /* If not, try comparing based on numerical designation alone.
19923      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
19924   if (TOLOWER (*given) == 'r')
19925     given++;
19926   if (!ISDIGIT (*given))
19927     return FALSE;
19928 
19929   /* Skip over some well-known prefixes in the canonical name,
19930      hoping to find a number there too.  */
19931   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19932     canonical += 2;
19933   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19934     canonical += 2;
19935   else if (TOLOWER (canonical[0]) == 'r')
19936     canonical += 1;
19937 
19938   return mips_strict_matching_cpu_name_p (canonical, given);
19939 }
19940 
19941 
19942 /* Parse an option that takes the name of a processor as its argument.
19943    OPTION is the name of the option and CPU_STRING is the argument.
19944    Return the corresponding processor enumeration if the CPU_STRING is
19945    recognized, otherwise report an error and return null.
19946 
19947    A similar function exists in GCC.  */
19948 
19949 static const struct mips_cpu_info *
19950 mips_parse_cpu (const char *option, const char *cpu_string)
19951 {
19952   const struct mips_cpu_info *p;
19953 
19954   /* 'from-abi' selects the most compatible architecture for the given
19955      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
19956      EABIs, we have to decide whether we're using the 32-bit or 64-bit
19957      version.  Look first at the -mgp options, if given, otherwise base
19958      the choice on MIPS_DEFAULT_64BIT.
19959 
19960      Treat NO_ABI like the EABIs.  One reason to do this is that the
19961      plain 'mips' and 'mips64' configs have 'from-abi' as their default
19962      architecture.  This code picks MIPS I for 'mips' and MIPS III for
19963      'mips64', just as we did in the days before 'from-abi'.  */
19964   if (strcasecmp (cpu_string, "from-abi") == 0)
19965     {
19966       if (ABI_NEEDS_32BIT_REGS (mips_abi))
19967 	return mips_cpu_info_from_isa (ISA_MIPS1);
19968 
19969       if (ABI_NEEDS_64BIT_REGS (mips_abi))
19970 	return mips_cpu_info_from_isa (ISA_MIPS3);
19971 
19972       if (file_mips_opts.gp >= 0)
19973 	return mips_cpu_info_from_isa (file_mips_opts.gp == 32
19974 				       ? ISA_MIPS1 : ISA_MIPS3);
19975 
19976       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19977 				     ? ISA_MIPS3
19978 				     : ISA_MIPS1);
19979     }
19980 
19981   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
19982   if (strcasecmp (cpu_string, "default") == 0)
19983     return 0;
19984 
19985   for (p = mips_cpu_info_table; p->name != 0; p++)
19986     if (mips_matching_cpu_name_p (p->name, cpu_string))
19987       return p;
19988 
19989   as_bad (_("bad value (%s) for %s"), cpu_string, option);
19990   return 0;
19991 }
19992 
19993 /* Return the canonical processor information for ISA (a member of the
19994    ISA_MIPS* enumeration).  */
19995 
19996 static const struct mips_cpu_info *
19997 mips_cpu_info_from_isa (int isa)
19998 {
19999   int i;
20000 
20001   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20002     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
20003 	&& isa == mips_cpu_info_table[i].isa)
20004       return (&mips_cpu_info_table[i]);
20005 
20006   return NULL;
20007 }
20008 
20009 static const struct mips_cpu_info *
20010 mips_cpu_info_from_arch (int arch)
20011 {
20012   int i;
20013 
20014   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20015     if (arch == mips_cpu_info_table[i].cpu)
20016       return (&mips_cpu_info_table[i]);
20017 
20018   return NULL;
20019 }
20020 
20021 static void
20022 show (FILE *stream, const char *string, int *col_p, int *first_p)
20023 {
20024   if (*first_p)
20025     {
20026       fprintf (stream, "%24s", "");
20027       *col_p = 24;
20028     }
20029   else
20030     {
20031       fprintf (stream, ", ");
20032       *col_p += 2;
20033     }
20034 
20035   if (*col_p + strlen (string) > 72)
20036     {
20037       fprintf (stream, "\n%24s", "");
20038       *col_p = 24;
20039     }
20040 
20041   fprintf (stream, "%s", string);
20042   *col_p += strlen (string);
20043 
20044   *first_p = 0;
20045 }
20046 
20047 void
20048 md_show_usage (FILE *stream)
20049 {
20050   int column, first;
20051   size_t i;
20052 
20053   fprintf (stream, _("\
20054 MIPS options:\n\
20055 -EB			generate big endian output\n\
20056 -EL			generate little endian output\n\
20057 -g, -g2			do not remove unneeded NOPs or swap branches\n\
20058 -G NUM			allow referencing objects up to NUM bytes\n\
20059 			implicitly with the gp register [default 8]\n"));
20060   fprintf (stream, _("\
20061 -mips1			generate MIPS ISA I instructions\n\
20062 -mips2			generate MIPS ISA II instructions\n\
20063 -mips3			generate MIPS ISA III instructions\n\
20064 -mips4			generate MIPS ISA IV instructions\n\
20065 -mips5                  generate MIPS ISA V instructions\n\
20066 -mips32                 generate MIPS32 ISA instructions\n\
20067 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
20068 -mips32r3               generate MIPS32 release 3 ISA instructions\n\
20069 -mips32r5               generate MIPS32 release 5 ISA instructions\n\
20070 -mips32r6               generate MIPS32 release 6 ISA instructions\n\
20071 -mips64                 generate MIPS64 ISA instructions\n\
20072 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
20073 -mips64r3               generate MIPS64 release 3 ISA instructions\n\
20074 -mips64r5               generate MIPS64 release 5 ISA instructions\n\
20075 -mips64r6               generate MIPS64 release 6 ISA instructions\n\
20076 -march=CPU/-mtune=CPU	generate code/schedule for CPU, where CPU is one of:\n"));
20077 
20078   first = 1;
20079 
20080   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20081     show (stream, mips_cpu_info_table[i].name, &column, &first);
20082   show (stream, "from-abi", &column, &first);
20083   fputc ('\n', stream);
20084 
20085   fprintf (stream, _("\
20086 -mCPU			equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20087 -no-mCPU		don't generate code specific to CPU.\n\
20088 			For -mCPU and -no-mCPU, CPU must be one of:\n"));
20089 
20090   first = 1;
20091 
20092   show (stream, "3900", &column, &first);
20093   show (stream, "4010", &column, &first);
20094   show (stream, "4100", &column, &first);
20095   show (stream, "4650", &column, &first);
20096   fputc ('\n', stream);
20097 
20098   fprintf (stream, _("\
20099 -mips16			generate mips16 instructions\n\
20100 -no-mips16		do not generate mips16 instructions\n"));
20101   fprintf (stream, _("\
20102 -mmips16e2		generate MIPS16e2 instructions\n\
20103 -mno-mips16e2		do not generate MIPS16e2 instructions\n"));
20104   fprintf (stream, _("\
20105 -mmicromips		generate microMIPS instructions\n\
20106 -mno-micromips		do not generate microMIPS instructions\n"));
20107   fprintf (stream, _("\
20108 -msmartmips		generate smartmips instructions\n\
20109 -mno-smartmips		do not generate smartmips instructions\n"));
20110   fprintf (stream, _("\
20111 -mdsp			generate DSP instructions\n\
20112 -mno-dsp		do not generate DSP instructions\n"));
20113   fprintf (stream, _("\
20114 -mdspr2			generate DSP R2 instructions\n\
20115 -mno-dspr2		do not generate DSP R2 instructions\n"));
20116   fprintf (stream, _("\
20117 -mdspr3			generate DSP R3 instructions\n\
20118 -mno-dspr3		do not generate DSP R3 instructions\n"));
20119   fprintf (stream, _("\
20120 -mmt			generate MT instructions\n\
20121 -mno-mt			do not generate MT instructions\n"));
20122   fprintf (stream, _("\
20123 -mmcu			generate MCU instructions\n\
20124 -mno-mcu		do not generate MCU instructions\n"));
20125   fprintf (stream, _("\
20126 -mmsa			generate MSA instructions\n\
20127 -mno-msa		do not generate MSA instructions\n"));
20128   fprintf (stream, _("\
20129 -mxpa			generate eXtended Physical Address (XPA) instructions\n\
20130 -mno-xpa		do not generate eXtended Physical Address (XPA) instructions\n"));
20131   fprintf (stream, _("\
20132 -mvirt			generate Virtualization instructions\n\
20133 -mno-virt		do not generate Virtualization instructions\n"));
20134   fprintf (stream, _("\
20135 -mcrc			generate CRC instructions\n\
20136 -mno-crc		do not generate CRC instructions\n"));
20137   fprintf (stream, _("\
20138 -mginv			generate Global INValidate (GINV) instructions\n\
20139 -mno-ginv		do not generate Global INValidate instructions\n"));
20140   fprintf (stream, _("\
20141 -minsn32		only generate 32-bit microMIPS instructions\n\
20142 -mno-insn32		generate all microMIPS instructions\n"));
20143   fprintf (stream, _("\
20144 -mfix-loongson2f-jump	work around Loongson2F JUMP instructions\n\
20145 -mfix-loongson2f-nop	work around Loongson2F NOP errata\n\
20146 -mfix-loongson2f-btb	work around Loongson2F BTB errata\n\
20147 -mfix-vr4120		work around certain VR4120 errata\n\
20148 -mfix-vr4130		work around VR4130 mflo/mfhi errata\n\
20149 -mfix-24k		insert a nop after ERET and DERET instructions\n\
20150 -mfix-cn63xxp1		work around CN63XXP1 PREF errata\n\
20151 -mgp32			use 32-bit GPRs, regardless of the chosen ISA\n\
20152 -mfp32			use 32-bit FPRs, regardless of the chosen ISA\n\
20153 -msym32			assume all symbols have 32-bit values\n\
20154 -O0			do not remove unneeded NOPs, do not swap branches\n\
20155 -O, -O1			remove unneeded NOPs, do not swap branches\n\
20156 -O2			remove unneeded NOPs and swap branches\n\
20157 --trap, --no-break	trap exception on div by 0 and mult overflow\n\
20158 --break, --no-trap	break exception on div by 0 and mult overflow\n"));
20159   fprintf (stream, _("\
20160 -mhard-float		allow floating-point instructions\n\
20161 -msoft-float		do not allow floating-point instructions\n\
20162 -msingle-float		only allow 32-bit floating-point operations\n\
20163 -mdouble-float		allow 32-bit and 64-bit floating-point operations\n\
20164 --[no-]construct-floats	[dis]allow floating point values to be constructed\n\
20165 --[no-]relax-branch	[dis]allow out-of-range branches to be relaxed\n\
20166 -mignore-branch-isa	accept invalid branches requiring an ISA mode switch\n\
20167 -mno-ignore-branch-isa	reject invalid branches requiring an ISA mode switch\n\
20168 -mnan=ENCODING		select an IEEE 754 NaN encoding convention, either of:\n"));
20169 
20170   first = 1;
20171 
20172   show (stream, "legacy", &column, &first);
20173   show (stream, "2008", &column, &first);
20174 
20175   fputc ('\n', stream);
20176 
20177   fprintf (stream, _("\
20178 -KPIC, -call_shared	generate SVR4 position independent code\n\
20179 -call_nonpic		generate non-PIC code that can operate with DSOs\n\
20180 -mvxworks-pic		generate VxWorks position independent code\n\
20181 -non_shared		do not generate code that can operate with DSOs\n\
20182 -xgot			assume a 32 bit GOT\n\
20183 -mpdr, -mno-pdr		enable/disable creation of .pdr sections\n\
20184 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
20185                         position dependent (non shared) code\n\
20186 -mabi=ABI		create ABI conformant object file for:\n"));
20187 
20188   first = 1;
20189 
20190   show (stream, "32", &column, &first);
20191   show (stream, "o64", &column, &first);
20192   show (stream, "n32", &column, &first);
20193   show (stream, "64", &column, &first);
20194   show (stream, "eabi", &column, &first);
20195 
20196   fputc ('\n', stream);
20197 
20198   fprintf (stream, _("\
20199 -32			create o32 ABI object file%s\n"),
20200 	   MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20201   fprintf (stream, _("\
20202 -n32			create n32 ABI object file%s\n"),
20203 	   MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20204   fprintf (stream, _("\
20205 -64			create 64 ABI object file%s\n"),
20206 	   MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20207 }
20208 
20209 #ifdef TE_IRIX
20210 enum dwarf2_format
20211 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20212 {
20213   if (HAVE_64BIT_SYMBOLS)
20214     return dwarf2_format_64bit_irix;
20215   else
20216     return dwarf2_format_32bit;
20217 }
20218 #endif
20219 
20220 int
20221 mips_dwarf2_addr_size (void)
20222 {
20223   if (HAVE_64BIT_OBJECTS)
20224     return 8;
20225   else
20226     return 4;
20227 }
20228 
20229 /* Standard calling conventions leave the CFA at SP on entry.  */
20230 void
20231 mips_cfi_frame_initial_instructions (void)
20232 {
20233   cfi_add_CFA_def_cfa_register (SP);
20234 }
20235 
20236 int
20237 tc_mips_regname_to_dw2regnum (char *regname)
20238 {
20239   unsigned int regnum = -1;
20240   unsigned int reg;
20241 
20242   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20243     regnum = reg;
20244 
20245   return regnum;
20246 }
20247 
20248 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20249    Given a symbolic attribute NAME, return the proper integer value.
20250    Returns -1 if the attribute is not known.  */
20251 
20252 int
20253 mips_convert_symbolic_attribute (const char *name)
20254 {
20255   static const struct
20256   {
20257     const char * name;
20258     const int    tag;
20259   }
20260   attribute_table[] =
20261     {
20262 #define T(tag) {#tag, tag}
20263       T (Tag_GNU_MIPS_ABI_FP),
20264       T (Tag_GNU_MIPS_ABI_MSA),
20265 #undef T
20266     };
20267   unsigned int i;
20268 
20269   if (name == NULL)
20270     return -1;
20271 
20272   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20273     if (streq (name, attribute_table[i].name))
20274       return attribute_table[i].tag;
20275 
20276   return -1;
20277 }
20278 
20279 void
20280 md_mips_end (void)
20281 {
20282   int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20283 
20284   mips_emit_delays ();
20285   if (cur_proc_ptr)
20286     as_warn (_("missing .end at end of assembly"));
20287 
20288   /* Just in case no code was emitted, do the consistency check.  */
20289   file_mips_check_options ();
20290 
20291   /* Set a floating-point ABI if the user did not.  */
20292   if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20293     {
20294       /* Perform consistency checks on the floating-point ABI.  */
20295       fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20296 					Tag_GNU_MIPS_ABI_FP);
20297       if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20298 	check_fpabi (fpabi);
20299     }
20300   else
20301     {
20302       /* Soft-float gets precedence over single-float, the two options should
20303          not be used together so this should not matter.  */
20304       if (file_mips_opts.soft_float == 1)
20305 	fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20306       /* Single-float gets precedence over all double_float cases.  */
20307       else if (file_mips_opts.single_float == 1)
20308 	fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20309       else
20310 	{
20311 	  switch (file_mips_opts.fp)
20312 	    {
20313 	    case 32:
20314 	      if (file_mips_opts.gp == 32)
20315 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20316 	      break;
20317 	    case 0:
20318 	      fpabi = Val_GNU_MIPS_ABI_FP_XX;
20319 	      break;
20320 	    case 64:
20321 	      if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20322 		fpabi = Val_GNU_MIPS_ABI_FP_64A;
20323 	      else if (file_mips_opts.gp == 32)
20324 		fpabi = Val_GNU_MIPS_ABI_FP_64;
20325 	      else
20326 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20327 	      break;
20328 	    }
20329 	}
20330 
20331       bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20332 				Tag_GNU_MIPS_ABI_FP, fpabi);
20333     }
20334 }
20335 
20336 /*  Returns the relocation type required for a particular CFI encoding.  */
20337 
20338 bfd_reloc_code_real_type
20339 mips_cfi_reloc_for_encoding (int encoding)
20340 {
20341   if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20342     return BFD_RELOC_32_PCREL;
20343   else return BFD_RELOC_NONE;
20344 }
20345