xref: /netbsd-src/external/gpl3/binutils/dist/gas/config/tc-mips.c (revision 4ac76180e904e771b9d522c7e57296d371f06499)
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright (C) 1993-2022 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 FCSR 31
108 
109 #define ILLEGAL_REG (32)
110 
111 #define AT  mips_opts.at
112 
113 extern int target_big_endian;
114 
115 /* The name of the readonly data section.  */
116 #define RDATA_SECTION_NAME ".rodata"
117 
118 /* Ways in which an instruction can be "appended" to the output.  */
119 enum append_method {
120   /* Just add it normally.  */
121   APPEND_ADD,
122 
123   /* Add it normally and then add a nop.  */
124   APPEND_ADD_WITH_NOP,
125 
126   /* Turn an instruction with a delay slot into a "compact" version.  */
127   APPEND_ADD_COMPACT,
128 
129   /* Insert the instruction before the last one.  */
130   APPEND_SWAP
131 };
132 
133 /* Information about an instruction, including its format, operands
134    and fixups.  */
135 struct mips_cl_insn
136 {
137   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
138   const struct mips_opcode *insn_mo;
139 
140   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
141      a copy of INSN_MO->match with the operands filled in.  If we have
142      decided to use an extended MIPS16 instruction, this includes the
143      extension.  */
144   unsigned long insn_opcode;
145 
146   /* The name if this is an label.  */
147   char label[16];
148 
149   /* The target label name if this is an branch.  */
150   char target[16];
151 
152   /* The frag that contains the instruction.  */
153   struct frag *frag;
154 
155   /* The offset into FRAG of the first instruction byte.  */
156   long where;
157 
158   /* The relocs associated with the instruction, if any.  */
159   fixS *fixp[3];
160 
161   /* True if this entry cannot be moved from its current position.  */
162   unsigned int fixed_p : 1;
163 
164   /* True if this instruction occurred in a .set noreorder block.  */
165   unsigned int noreorder_p : 1;
166 
167   /* True for mips16 instructions that jump to an absolute address.  */
168   unsigned int mips16_absolute_jump_p : 1;
169 
170   /* True if this instruction is complete.  */
171   unsigned int complete_p : 1;
172 
173   /* True if this instruction is cleared from history by unconditional
174      branch.  */
175   unsigned int cleared_p : 1;
176 };
177 
178 /* The ABI to use.  */
179 enum mips_abi_level
180 {
181   NO_ABI = 0,
182   O32_ABI,
183   O64_ABI,
184   N32_ABI,
185   N64_ABI,
186   EABI_ABI
187 };
188 
189 /* MIPS ABI we are using for this output file.  */
190 static enum mips_abi_level mips_abi = NO_ABI;
191 
192 /* Whether or not we have code that can call pic code.  */
193 int mips_abicalls = false;
194 
195 /* Whether or not we have code which can be put into a shared
196    library.  */
197 static bool mips_in_shared = true;
198 
199 /* This is the set of options which may be modified by the .set
200    pseudo-op.  We use a struct so that .set push and .set pop are more
201    reliable.  */
202 
203 struct mips_set_options
204 {
205   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
206      if it has not been initialized.  Changed by `.set mipsN', and the
207      -mipsN command line option, and the default CPU.  */
208   int isa;
209   /* Enabled Application Specific Extensions (ASEs).  Changed by `.set
210      <asename>', by command line options, and based on the default
211      architecture.  */
212   int ase;
213   /* Whether we are assembling for the mips16 processor.  0 if we are
214      not, 1 if we are, and -1 if the value has not been initialized.
215      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
216      -nomips16 command line options, and the default CPU.  */
217   int mips16;
218   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
219      1 if we are, and -1 if the value has not been initialized.  Changed
220      by `.set micromips' and `.set nomicromips', and the -mmicromips
221      and -mno-micromips command line options, and the default CPU.  */
222   int micromips;
223   /* Non-zero if we should not reorder instructions.  Changed by `.set
224      reorder' and `.set noreorder'.  */
225   int noreorder;
226   /* Non-zero if we should not permit the register designated "assembler
227      temporary" to be used in instructions.  The value is the register
228      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
229      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
230   unsigned int at;
231   /* Non-zero if we should warn when a macro instruction expands into
232      more than one machine instruction.  Changed by `.set nomacro' and
233      `.set macro'.  */
234   int warn_about_macros;
235   /* Non-zero if we should not move instructions.  Changed by `.set
236      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
237   int nomove;
238   /* Non-zero if we should not optimize branches by moving the target
239      of the branch into the delay slot.  Actually, we don't perform
240      this optimization anyhow.  Changed by `.set bopt' and `.set
241      nobopt'.  */
242   int nobopt;
243   /* Non-zero if we should not autoextend mips16 instructions.
244      Changed by `.set autoextend' and `.set noautoextend'.  */
245   int noautoextend;
246   /* True if we should only emit 32-bit microMIPS instructions.
247      Changed by `.set insn32' and `.set noinsn32', and the -minsn32
248      and -mno-insn32 command line options.  */
249   bool insn32;
250   /* Restrict general purpose registers and floating point registers
251      to 32 bit.  This is initially determined when -mgp32 or -mfp32
252      is passed but can changed if the assembler code uses .set mipsN.  */
253   int gp;
254   int fp;
255   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
256      command line option, and the default CPU.  */
257   int arch;
258   /* True if ".set sym32" is in effect.  */
259   bool sym32;
260   /* True if floating-point operations are not allowed.  Changed by .set
261      softfloat or .set hardfloat, by command line options -msoft-float or
262      -mhard-float.  The default is false.  */
263   bool soft_float;
264 
265   /* True if only single-precision floating-point operations are allowed.
266      Changed by .set singlefloat or .set doublefloat, command-line options
267      -msingle-float or -mdouble-float.  The default is false.  */
268   bool single_float;
269 
270   /* 1 if single-precision operations on odd-numbered registers are
271      allowed.  */
272   int oddspreg;
273 
274   /* The set of ASEs that should be enabled for the user specified
275      architecture.  This cannot be inferred from 'arch' for all cores
276      as processors only have a unique 'arch' if they add architecture
277      specific instructions (UDI).  */
278   int init_ase;
279 };
280 
281 /* Specifies whether module level options have been checked yet.  */
282 static bool file_mips_opts_checked = false;
283 
284 /* Do we support nan2008?  0 if we don't, 1 if we do, and -1 if the
285    value has not been initialized.  Changed by `.nan legacy' and
286    `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
287    options, and the default CPU.  */
288 static int mips_nan2008 = -1;
289 
290 /* This is the struct we use to hold the module level set of options.
291    Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
292    fp fields to -1 to indicate that they have not been initialized.  */
293 
294 static struct mips_set_options file_mips_opts =
295 {
296   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
297   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
298   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ false,
299   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ false,
300   /* soft_float */ false, /* single_float */ false, /* oddspreg */ -1,
301   /* init_ase */ 0
302 };
303 
304 /* This is similar to file_mips_opts, but for the current set of options.  */
305 
306 static struct mips_set_options mips_opts =
307 {
308   /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
309   /* noreorder */ 0,  /* at */ ATREG, /* warn_about_macros */ 0,
310   /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ false,
311   /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ false,
312   /* soft_float */ false, /* single_float */ false, /* oddspreg */ -1,
313   /* init_ase */ 0
314 };
315 
316 /* Which bits of file_ase were explicitly set or cleared by ASE options.  */
317 static unsigned int file_ase_explicit;
318 
319 /* These variables are filled in with the masks of registers used.
320    The object format code reads them and puts them in the appropriate
321    place.  */
322 unsigned long mips_gprmask;
323 unsigned long mips_cprmask[4];
324 
325 /* True if any MIPS16 code was produced.  */
326 static int file_ase_mips16;
327 
328 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32		\
329 			      || mips_opts.isa == ISA_MIPS32R2		\
330 			      || mips_opts.isa == ISA_MIPS32R3		\
331 			      || mips_opts.isa == ISA_MIPS32R5		\
332 			      || mips_opts.isa == ISA_MIPS64		\
333 			      || mips_opts.isa == ISA_MIPS64R2		\
334 			      || mips_opts.isa == ISA_MIPS64R3		\
335 			      || mips_opts.isa == ISA_MIPS64R5)
336 
337 /* True if any microMIPS code was produced.  */
338 static int file_ase_micromips;
339 
340 /* True if we want to create R_MIPS_JALR for jalr $25.  */
341 #ifdef TE_IRIX
342 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
343 #else
344 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
345    because there's no place for any addend, the only acceptable
346    expression is a bare symbol.  */
347 #define MIPS_JALR_HINT_P(EXPR) \
348   (!HAVE_IN_PLACE_ADDENDS \
349    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
350 #endif
351 
352 /* The argument of the -march= flag.  The architecture we are assembling.  */
353 static const char *mips_arch_string;
354 
355 /* The argument of the -mtune= flag.  The architecture for which we
356    are optimizing.  */
357 static int mips_tune = CPU_UNKNOWN;
358 static const char *mips_tune_string;
359 
360 /* True when generating 32-bit code for a 64-bit processor.  */
361 static int mips_32bitmode = 0;
362 
363 /* True if the given ABI requires 32-bit registers.  */
364 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
365 
366 /* Likewise 64-bit registers.  */
367 #define ABI_NEEDS_64BIT_REGS(ABI)	\
368   ((ABI) == N32_ABI			\
369    || (ABI) == N64_ABI			\
370    || (ABI) == O64_ABI)
371 
372 #define ISA_IS_R6(ISA)			\
373   ((ISA) == ISA_MIPS32R6		\
374    || (ISA) == ISA_MIPS64R6)
375 
376 /*  Return true if ISA supports 64 bit wide gp registers.  */
377 #define ISA_HAS_64BIT_REGS(ISA)		\
378   ((ISA) == ISA_MIPS3			\
379    || (ISA) == ISA_MIPS4		\
380    || (ISA) == ISA_MIPS5		\
381    || (ISA) == ISA_MIPS64		\
382    || (ISA) == ISA_MIPS64R2		\
383    || (ISA) == ISA_MIPS64R3		\
384    || (ISA) == ISA_MIPS64R5		\
385    || (ISA) == ISA_MIPS64R6)
386 
387 /*  Return true if ISA supports 64 bit wide float registers.  */
388 #define ISA_HAS_64BIT_FPRS(ISA)		\
389   ((ISA) == ISA_MIPS3			\
390    || (ISA) == ISA_MIPS4		\
391    || (ISA) == ISA_MIPS5		\
392    || (ISA) == ISA_MIPS32R2		\
393    || (ISA) == ISA_MIPS32R3		\
394    || (ISA) == ISA_MIPS32R5		\
395    || (ISA) == ISA_MIPS32R6		\
396    || (ISA) == ISA_MIPS64		\
397    || (ISA) == ISA_MIPS64R2		\
398    || (ISA) == ISA_MIPS64R3		\
399    || (ISA) == ISA_MIPS64R5		\
400    || (ISA) == ISA_MIPS64R6)
401 
402 /* Return true if ISA supports 64-bit right rotate (dror et al.)
403    instructions.  */
404 #define ISA_HAS_DROR(ISA)		\
405   ((ISA) == ISA_MIPS64R2		\
406    || (ISA) == ISA_MIPS64R3		\
407    || (ISA) == ISA_MIPS64R5		\
408    || (ISA) == ISA_MIPS64R6		\
409    || (mips_opts.micromips		\
410        && ISA_HAS_64BIT_REGS (ISA))	\
411    )
412 
413 /* Return true if ISA supports 32-bit right rotate (ror et al.)
414    instructions.  */
415 #define ISA_HAS_ROR(ISA)		\
416   ((ISA) == ISA_MIPS32R2		\
417    || (ISA) == ISA_MIPS32R3		\
418    || (ISA) == ISA_MIPS32R5		\
419    || (ISA) == ISA_MIPS32R6		\
420    || (ISA) == ISA_MIPS64R2		\
421    || (ISA) == ISA_MIPS64R3		\
422    || (ISA) == ISA_MIPS64R5		\
423    || (ISA) == ISA_MIPS64R6		\
424    || (mips_opts.ase & ASE_SMARTMIPS)	\
425    || mips_opts.micromips		\
426    )
427 
428 /* Return true if ISA supports single-precision floats in odd registers.  */
429 #define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
430   (((ISA) == ISA_MIPS32			\
431     || (ISA) == ISA_MIPS32R2		\
432     || (ISA) == ISA_MIPS32R3		\
433     || (ISA) == ISA_MIPS32R5		\
434     || (ISA) == ISA_MIPS32R6		\
435     || (ISA) == ISA_MIPS64		\
436     || (ISA) == ISA_MIPS64R2		\
437     || (ISA) == ISA_MIPS64R3		\
438     || (ISA) == ISA_MIPS64R5		\
439     || (ISA) == ISA_MIPS64R6		\
440     || (CPU) == CPU_R5900)		\
441    && ((CPU) != CPU_GS464		\
442     || (CPU) != CPU_GS464E		\
443     || (CPU) != CPU_GS264E))
444 
445 /* Return true if ISA supports move to/from high part of a 64-bit
446    floating-point register. */
447 #define ISA_HAS_MXHC1(ISA)		\
448   ((ISA) == ISA_MIPS32R2		\
449    || (ISA) == ISA_MIPS32R3		\
450    || (ISA) == ISA_MIPS32R5		\
451    || (ISA) == ISA_MIPS32R6		\
452    || (ISA) == ISA_MIPS64R2		\
453    || (ISA) == ISA_MIPS64R3		\
454    || (ISA) == ISA_MIPS64R5		\
455    || (ISA) == ISA_MIPS64R6)
456 
457 /*  Return true if ISA supports legacy NAN.  */
458 #define ISA_HAS_LEGACY_NAN(ISA)		\
459   ((ISA) == ISA_MIPS1			\
460    || (ISA) == ISA_MIPS2		\
461    || (ISA) == ISA_MIPS3		\
462    || (ISA) == ISA_MIPS4		\
463    || (ISA) == ISA_MIPS5		\
464    || (ISA) == ISA_MIPS32		\
465    || (ISA) == ISA_MIPS32R2		\
466    || (ISA) == ISA_MIPS32R3		\
467    || (ISA) == ISA_MIPS32R5		\
468    || (ISA) == ISA_MIPS64		\
469    || (ISA) == ISA_MIPS64R2		\
470    || (ISA) == ISA_MIPS64R3		\
471    || (ISA) == ISA_MIPS64R5)
472 
473 #define GPR_SIZE \
474     (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
475      ? 32 \
476      : mips_opts.gp)
477 
478 #define FPR_SIZE \
479     (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
480      ? 32 \
481      : mips_opts.fp)
482 
483 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
484 
485 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
486 
487 /* True if relocations are stored in-place.  */
488 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
489 
490 /* The ABI-derived address size.  */
491 #define HAVE_64BIT_ADDRESSES \
492   (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
493 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
494 
495 /* The size of symbolic constants (i.e., expressions of the form
496    "SYMBOL" or "SYMBOL + OFFSET").  */
497 #define HAVE_32BIT_SYMBOLS \
498   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
499 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
500 
501 /* Addresses are loaded in different ways, depending on the address size
502    in use.  The n32 ABI Documentation also mandates the use of additions
503    with overflow checking, but existing implementations don't follow it.  */
504 #define ADDRESS_ADD_INSN						\
505    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
506 
507 #define ADDRESS_ADDI_INSN						\
508    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
509 
510 #define ADDRESS_LOAD_INSN						\
511    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
512 
513 #define ADDRESS_STORE_INSN						\
514    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
515 
516 /* Return true if the given CPU supports the MIPS16 ASE.  */
517 #define CPU_HAS_MIPS16(cpu)						\
518    (startswith (TARGET_CPU, "mips16")					\
519     || startswith (TARGET_CANONICAL, "mips-lsi-elf"))
520 
521 /* Return true if the given CPU supports the microMIPS ASE.  */
522 #define CPU_HAS_MICROMIPS(cpu)	0
523 
524 /* True if CPU has a dror instruction.  */
525 #define CPU_HAS_DROR(CPU)	((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
526 
527 /* True if CPU has a ror instruction.  */
528 #define CPU_HAS_ROR(CPU)	CPU_HAS_DROR (CPU)
529 
530 /* True if CPU is in the Octeon family.  */
531 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
532 			    || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
533 
534 /* True if CPU has seq/sne and seqi/snei instructions.  */
535 #define CPU_HAS_SEQ(CPU)	(CPU_IS_OCTEON (CPU))
536 
537 /* True, if CPU has support for ldc1 and sdc1. */
538 #define CPU_HAS_LDC1_SDC1(CPU)	\
539    ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
540 
541 /* True if mflo and mfhi can be immediately followed by instructions
542    which write to the HI and LO registers.
543 
544    According to MIPS specifications, MIPS ISAs I, II, and III need
545    (at least) two instructions between the reads of HI/LO and
546    instructions which write them, and later ISAs do not.  Contradicting
547    the MIPS specifications, some MIPS IV processor user manuals (e.g.
548    the UM for the NEC Vr5000) document needing the instructions between
549    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
550    MIPS64 and later ISAs to have the interlocks, plus any specific
551    earlier-ISA CPUs for which CPU documentation declares that the
552    instructions are really interlocked.  */
553 #define hilo_interlocks \
554   (mips_opts.isa == ISA_MIPS32                        \
555    || mips_opts.isa == ISA_MIPS32R2                   \
556    || mips_opts.isa == ISA_MIPS32R3                   \
557    || mips_opts.isa == ISA_MIPS32R5                   \
558    || mips_opts.isa == ISA_MIPS32R6                   \
559    || mips_opts.isa == ISA_MIPS64                     \
560    || mips_opts.isa == ISA_MIPS64R2                   \
561    || mips_opts.isa == ISA_MIPS64R3                   \
562    || mips_opts.isa == ISA_MIPS64R5                   \
563    || mips_opts.isa == ISA_MIPS64R6                   \
564    || mips_opts.arch == CPU_R4010                     \
565    || mips_opts.arch == CPU_R5900                     \
566    || mips_opts.arch == CPU_R10000                    \
567    || mips_opts.arch == CPU_R12000                    \
568    || mips_opts.arch == CPU_R14000                    \
569    || mips_opts.arch == CPU_R16000                    \
570    || mips_opts.arch == CPU_RM7000                    \
571    || mips_opts.arch == CPU_VR5500                    \
572    || mips_opts.micromips                             \
573    )
574 
575 /* Whether the processor uses hardware interlocks to protect reads
576    from the GPRs after they are loaded from memory, and thus does not
577    require nops to be inserted.  This applies to instructions marked
578    INSN_LOAD_MEMORY.  These nops are only required at MIPS ISA
579    level I and microMIPS mode instructions are always interlocked.  */
580 #define gpr_interlocks                                \
581   (mips_opts.isa != ISA_MIPS1                         \
582    || mips_opts.arch == CPU_R3900                     \
583    || mips_opts.arch == CPU_R5900                     \
584    || mips_opts.micromips                             \
585    )
586 
587 /* Whether the processor uses hardware interlocks to avoid delays
588    required by coprocessor instructions, and thus does not require
589    nops to be inserted.  This applies to instructions marked
590    INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
591    instructions marked INSN_WRITE_COND_CODE and ones marked
592    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
593    levels I, II, and III and microMIPS mode instructions are always
594    interlocked.  */
595 /* Itbl support may require additional care here.  */
596 #define cop_interlocks                                \
597   ((mips_opts.isa != ISA_MIPS1                        \
598     && mips_opts.isa != ISA_MIPS2                     \
599     && mips_opts.isa != ISA_MIPS3)                    \
600    || mips_opts.arch == CPU_R4300                     \
601    || mips_opts.micromips                             \
602    )
603 
604 /* Whether the processor uses hardware interlocks to protect reads
605    from coprocessor registers after they are loaded from memory, and
606    thus does not require nops to be inserted.  This applies to
607    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
608    requires at MIPS ISA level I and microMIPS mode instructions are
609    always interlocked.  */
610 #define cop_mem_interlocks                            \
611   (mips_opts.isa != ISA_MIPS1                         \
612    || mips_opts.micromips                             \
613    )
614 
615 /* Is this a mfhi or mflo instruction?  */
616 #define MF_HILO_INSN(PINFO) \
617   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
618 
619 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
620    has been selected.  This implies, in particular, that addresses of text
621    labels have their LSB set.  */
622 #define HAVE_CODE_COMPRESSION						\
623   ((mips_opts.mips16 | mips_opts.micromips) != 0)
624 
625 /* The minimum and maximum signed values that can be stored in a GPR.  */
626 #define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
627 #define GPR_SMIN (-GPR_SMAX - 1)
628 
629 /* MIPS PIC level.  */
630 
631 enum mips_pic_level mips_pic;
632 
633 /* 1 if we should generate 32 bit offsets from the $gp register in
634    SVR4_PIC mode.  Currently has no meaning in other modes.  */
635 static int mips_big_got = 0;
636 
637 /* 1 if trap instructions should used for overflow rather than break
638    instructions.  */
639 static int mips_trap = 0;
640 
641 /* 1 if double width floating point constants should not be constructed
642    by assembling two single width halves into two single width floating
643    point registers which just happen to alias the double width destination
644    register.  On some architectures this aliasing can be disabled by a bit
645    in the status register, and the setting of this bit cannot be determined
646    automatically at assemble time.  */
647 static int mips_disable_float_construction;
648 
649 /* Non-zero if any .set noreorder directives were used.  */
650 
651 static int mips_any_noreorder;
652 
653 /* Non-zero if nops should be inserted when the register referenced in
654    an mfhi/mflo instruction is read in the next two instructions.  */
655 static int mips_7000_hilo_fix;
656 
657 /* The size of objects in the small data section.  */
658 static unsigned int g_switch_value = 8;
659 /* Whether the -G option was used.  */
660 static int g_switch_seen = 0;
661 
662 #define N_RMASK 0xc4
663 #define N_VFP   0xd4
664 
665 /* If we can determine in advance that GP optimization won't be
666    possible, we can skip the relaxation stuff that tries to produce
667    GP-relative references.  This makes delay slot optimization work
668    better.
669 
670    This function can only provide a guess, but it seems to work for
671    gcc output.  It needs to guess right for gcc, otherwise gcc
672    will put what it thinks is a GP-relative instruction in a branch
673    delay slot.
674 
675    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
676    fixed it for the non-PIC mode.  KR 95/04/07  */
677 static int nopic_need_relax (symbolS *, int);
678 
679 /* Handle of the OPCODE hash table.  */
680 static htab_t op_hash = NULL;
681 
682 /* The opcode hash table we use for the mips16.  */
683 static htab_t mips16_op_hash = NULL;
684 
685 /* The opcode hash table we use for the microMIPS ASE.  */
686 static htab_t micromips_op_hash = NULL;
687 
688 /* This array holds the chars that always start a comment.  If the
689     pre-processor is disabled, these aren't very useful.  */
690 const char comment_chars[] = "#";
691 
692 /* This array holds the chars that only start a comment at the beginning of
693    a line.  If the line seems to have the form '# 123 filename'
694    .line and .file directives will appear in the pre-processed output.  */
695 /* Note that input_file.c hand checks for '#' at the beginning of the
696    first line of the input file.  This is because the compiler outputs
697    #NO_APP at the beginning of its output.  */
698 /* Also note that C style comments are always supported.  */
699 const char line_comment_chars[] = "#";
700 
701 /* This array holds machine specific line separator characters.  */
702 const char line_separator_chars[] = ";";
703 
704 /* Chars that can be used to separate mant from exp in floating point nums.  */
705 const char EXP_CHARS[] = "eE";
706 
707 /* Chars that mean this number is a floating point constant.
708    As in 0f12.456
709    or    0d1.2345e12.  */
710 const char FLT_CHARS[] = "rRsSfFdDxXpP";
711 
712 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
713    changed in read.c .  Ideally it shouldn't have to know about it at all,
714    but nothing is ideal around here.  */
715 
716 /* Types of printf format used for instruction-related error messages.
717    "I" means int ("%d") and "S" means string ("%s").  */
718 enum mips_insn_error_format
719 {
720   ERR_FMT_PLAIN,
721   ERR_FMT_I,
722   ERR_FMT_SS,
723 };
724 
725 /* Information about an error that was found while assembling the current
726    instruction.  */
727 struct mips_insn_error
728 {
729   /* We sometimes need to match an instruction against more than one
730      opcode table entry.  Errors found during this matching are reported
731      against a particular syntactic argument rather than against the
732      instruction as a whole.  We grade these messages so that errors
733      against argument N have a greater priority than an error against
734      any argument < N, since the former implies that arguments up to N
735      were acceptable and that the opcode entry was therefore a closer match.
736      If several matches report an error against the same argument,
737      we only use that error if it is the same in all cases.
738 
739      min_argnum is the minimum argument number for which an error message
740      should be accepted.  It is 0 if MSG is against the instruction as
741      a whole.  */
742   int min_argnum;
743 
744   /* The printf()-style message, including its format and arguments.  */
745   enum mips_insn_error_format format;
746   const char *msg;
747   union
748   {
749     int i;
750     const char *ss[2];
751   } u;
752 };
753 
754 /* The error that should be reported for the current instruction.  */
755 static struct mips_insn_error insn_error;
756 
757 static int auto_align = 1;
758 
759 /* When outputting SVR4 PIC code, the assembler needs to know the
760    offset in the stack frame from which to restore the $gp register.
761    This is set by the .cprestore pseudo-op, and saved in this
762    variable.  */
763 static offsetT mips_cprestore_offset = -1;
764 
765 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
766    more optimizations, it can use a register value instead of a memory-saved
767    offset and even an other register than $gp as global pointer.  */
768 static offsetT mips_cpreturn_offset = -1;
769 static int mips_cpreturn_register = -1;
770 static int mips_gp_register = GP;
771 static int mips_gprel_offset = 0;
772 
773 /* Whether mips_cprestore_offset has been set in the current function
774    (or whether it has already been warned about, if not).  */
775 static int mips_cprestore_valid = 0;
776 
777 /* This is the register which holds the stack frame, as set by the
778    .frame pseudo-op.  This is needed to implement .cprestore.  */
779 static int mips_frame_reg = SP;
780 
781 /* Whether mips_frame_reg has been set in the current function
782    (or whether it has already been warned about, if not).  */
783 static int mips_frame_reg_valid = 0;
784 
785 /* To output NOP instructions correctly, we need to keep information
786    about the previous two instructions.  */
787 
788 /* Whether we are optimizing.  The default value of 2 means to remove
789    unneeded NOPs and swap branch instructions when possible.  A value
790    of 1 means to not swap branches.  A value of 0 means to always
791    insert NOPs.  */
792 static int mips_optimize = 2;
793 
794 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
795    equivalent to seeing no -g option at all.  */
796 static int mips_debug = 0;
797 
798 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
799 #define MAX_VR4130_NOPS 4
800 
801 /* The maximum number of NOPs needed to fill delay slots.  */
802 #define MAX_DELAY_NOPS 2
803 
804 /* The maximum number of NOPs needed for any purpose.  */
805 #define MAX_NOPS 4
806 
807 /* The maximum range of context length of ll/sc.  */
808 #define MAX_LLSC_RANGE 20
809 
810 /* A list of previous instructions, with index 0 being the most recent.
811    We need to look back MAX_NOPS instructions when filling delay slots
812    or working around processor errata.  We need to look back one
813    instruction further if we're thinking about using history[0] to
814    fill a branch delay slot.  */
815 static struct mips_cl_insn history[1 + MAX_NOPS + MAX_LLSC_RANGE];
816 
817 /* The maximum number of LABELS detect for the same address.  */
818 #define MAX_LABELS_SAME 10
819 
820 /* Arrays of operands for each instruction.  */
821 #define MAX_OPERANDS 6
822 struct mips_operand_array
823 {
824   const struct mips_operand *operand[MAX_OPERANDS];
825 };
826 static struct mips_operand_array *mips_operands;
827 static struct mips_operand_array *mips16_operands;
828 static struct mips_operand_array *micromips_operands;
829 
830 /* Nop instructions used by emit_nop.  */
831 static struct mips_cl_insn nop_insn;
832 static struct mips_cl_insn mips16_nop_insn;
833 static struct mips_cl_insn micromips_nop16_insn;
834 static struct mips_cl_insn micromips_nop32_insn;
835 
836 /* Sync instructions used by insert sync.  */
837 static struct mips_cl_insn sync_insn;
838 
839 /* The appropriate nop for the current mode.  */
840 #define NOP_INSN (mips_opts.mips16					\
841 		  ? &mips16_nop_insn					\
842 		  : (mips_opts.micromips				\
843 		     ? (mips_opts.insn32				\
844 			? &micromips_nop32_insn				\
845 			: &micromips_nop16_insn)			\
846 		     : &nop_insn))
847 
848 /* The size of NOP_INSN in bytes.  */
849 #define NOP_INSN_SIZE ((mips_opts.mips16				\
850 			|| (mips_opts.micromips && !mips_opts.insn32))	\
851 		       ? 2 : 4)
852 
853 /* If this is set, it points to a frag holding nop instructions which
854    were inserted before the start of a noreorder section.  If those
855    nops turn out to be unnecessary, the size of the frag can be
856    decreased.  */
857 static fragS *prev_nop_frag;
858 
859 /* The number of nop instructions we created in prev_nop_frag.  */
860 static int prev_nop_frag_holds;
861 
862 /* The number of nop instructions that we know we need in
863    prev_nop_frag.  */
864 static int prev_nop_frag_required;
865 
866 /* The number of instructions we've seen since prev_nop_frag.  */
867 static int prev_nop_frag_since;
868 
869 /* Relocations against symbols are sometimes done in two parts, with a HI
870    relocation and a LO relocation.  Each relocation has only 16 bits of
871    space to store an addend.  This means that in order for the linker to
872    handle carries correctly, it must be able to locate both the HI and
873    the LO relocation.  This means that the relocations must appear in
874    order in the relocation table.
875 
876    In order to implement this, we keep track of each unmatched HI
877    relocation.  We then sort them so that they immediately precede the
878    corresponding LO relocation.  */
879 
880 struct mips_hi_fixup
881 {
882   /* Next HI fixup.  */
883   struct mips_hi_fixup *next;
884   /* This fixup.  */
885   fixS *fixp;
886   /* The section this fixup is in.  */
887   segT seg;
888 };
889 
890 /* The list of unmatched HI relocs.  */
891 
892 static struct mips_hi_fixup *mips_hi_fixup_list;
893 
894 /* Map mips16 register numbers to normal MIPS register numbers.  */
895 
896 static const unsigned int mips16_to_32_reg_map[] =
897 {
898   16, 17, 2, 3, 4, 5, 6, 7
899 };
900 
901 /* Map microMIPS register numbers to normal MIPS register numbers.  */
902 
903 #define micromips_to_32_reg_d_map	mips16_to_32_reg_map
904 
905 /* The microMIPS registers with type h.  */
906 static const unsigned int micromips_to_32_reg_h_map1[] =
907 {
908   5, 5, 6, 4, 4, 4, 4, 4
909 };
910 static const unsigned int micromips_to_32_reg_h_map2[] =
911 {
912   6, 7, 7, 21, 22, 5, 6, 7
913 };
914 
915 /* The microMIPS registers with type m.  */
916 static const unsigned int micromips_to_32_reg_m_map[] =
917 {
918   0, 17, 2, 3, 16, 18, 19, 20
919 };
920 
921 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
922 
923 /* Classifies the kind of instructions we're interested in when
924    implementing -mfix-vr4120.  */
925 enum fix_vr4120_class
926 {
927   FIX_VR4120_MACC,
928   FIX_VR4120_DMACC,
929   FIX_VR4120_MULT,
930   FIX_VR4120_DMULT,
931   FIX_VR4120_DIV,
932   FIX_VR4120_MTHILO,
933   NUM_FIX_VR4120_CLASSES
934 };
935 
936 /* ...likewise -mtrap-zero-jump.  */
937 static bfd_boolean mips_trap_zero_jump;
938 
939 /* ...likewise -mfix-loongson2f-jump.  */
940 static bool mips_fix_loongson2f_jump;
941 
942 /* ...likewise -mfix-loongson2f-nop.  */
943 static bool mips_fix_loongson2f_nop;
944 
945 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
946 static bool mips_fix_loongson2f;
947 
948 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
949    there must be at least one other instruction between an instruction
950    of type X and an instruction of type Y.  */
951 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
952 
953 /* True if -mfix-vr4120 is in force.  */
954 static int mips_fix_vr4120;
955 
956 /* ...likewise -mfix-vr4130.  */
957 static int mips_fix_vr4130;
958 
959 /* ...likewise -mfix-24k.  */
960 static int mips_fix_24k;
961 
962 /* ...likewise -mfix-rm7000  */
963 static int mips_fix_rm7000;
964 
965 /* ...likewise -mfix-cn63xxp1 */
966 static bool mips_fix_cn63xxp1;
967 
968 /* ...likewise -mfix-r5900 */
969 static bool mips_fix_r5900;
970 static bool mips_fix_r5900_explicit;
971 
972 /* ...likewise -mfix-loongson3-llsc.  */
973 static bool mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
974 
975 /* We don't relax branches by default, since this causes us to expand
976    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
977    fail to compute the offset before expanding the macro to the most
978    efficient expansion.  */
979 
980 static int mips_relax_branch;
981 
982 static int mips_fix_loongson2f_btb;
983 
984 /* TRUE if checks are suppressed for invalid branches between ISA modes.
985    Needed for broken assembly produced by some GCC versions and some
986    sloppy code out there, where branches to data labels are present.  */
987 static bool mips_ignore_branch_isa;
988 
989 /* The expansion of many macros depends on the type of symbol that
990    they refer to.  For example, when generating position-dependent code,
991    a macro that refers to a symbol may have two different expansions,
992    one which uses GP-relative addresses and one which uses absolute
993    addresses.  When generating SVR4-style PIC, a macro may have
994    different expansions for local and global symbols.
995 
996    We handle these situations by generating both sequences and putting
997    them in variant frags.  In position-dependent code, the first sequence
998    will be the GP-relative one and the second sequence will be the
999    absolute one.  In SVR4 PIC, the first sequence will be for global
1000    symbols and the second will be for local symbols.
1001 
1002    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
1003    SECOND are the lengths of the two sequences in bytes.  These fields
1004    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
1005    the subtype has the following flags:
1006 
1007    RELAX_PIC
1008 	Set if generating PIC code.
1009 
1010    RELAX_USE_SECOND
1011 	Set if it has been decided that we should use the second
1012 	sequence instead of the first.
1013 
1014    RELAX_SECOND_LONGER
1015 	Set in the first variant frag if the macro's second implementation
1016 	is longer than its first.  This refers to the macro as a whole,
1017 	not an individual relaxation.
1018 
1019    RELAX_NOMACRO
1020 	Set in the first variant frag if the macro appeared in a .set nomacro
1021 	block and if one alternative requires a warning but the other does not.
1022 
1023    RELAX_DELAY_SLOT
1024 	Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1025 	delay slot.
1026 
1027    RELAX_DELAY_SLOT_16BIT
1028 	Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1029 	16-bit instruction.
1030 
1031    RELAX_DELAY_SLOT_SIZE_FIRST
1032 	Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1033 	the macro is of the wrong size for the branch delay slot.
1034 
1035    RELAX_DELAY_SLOT_SIZE_SECOND
1036 	Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1037 	the macro is of the wrong size for the branch delay slot.
1038 
1039    The frag's "opcode" points to the first fixup for relaxable code.
1040 
1041    Relaxable macros are generated using a sequence such as:
1042 
1043       relax_start (SYMBOL);
1044       ... generate first expansion ...
1045       relax_switch ();
1046       ... generate second expansion ...
1047       relax_end ();
1048 
1049    The code and fixups for the unwanted alternative are discarded
1050    by md_convert_frag.  */
1051 #define RELAX_ENCODE(FIRST, SECOND, PIC)			\
1052   (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
1053 
1054 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1055 #define RELAX_SECOND(X) ((X) & 0xff)
1056 #define RELAX_PIC(X) (((X) & 0x10000) != 0)
1057 #define RELAX_USE_SECOND 0x20000
1058 #define RELAX_SECOND_LONGER 0x40000
1059 #define RELAX_NOMACRO 0x80000
1060 #define RELAX_DELAY_SLOT 0x100000
1061 #define RELAX_DELAY_SLOT_16BIT 0x200000
1062 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1063 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
1064 
1065 /* Branch without likely bit.  If label is out of range, we turn:
1066 
1067 	beq reg1, reg2, label
1068 	delay slot
1069 
1070    into
1071 
1072         bne reg1, reg2, 0f
1073         nop
1074         j label
1075      0: delay slot
1076 
1077    with the following opcode replacements:
1078 
1079 	beq <-> bne
1080 	blez <-> bgtz
1081 	bltz <-> bgez
1082 	bc1f <-> bc1t
1083 
1084 	bltzal <-> bgezal  (with jal label instead of j label)
1085 
1086    Even though keeping the delay slot instruction in the delay slot of
1087    the branch would be more efficient, it would be very tricky to do
1088    correctly, because we'd have to introduce a variable frag *after*
1089    the delay slot instruction, and expand that instead.  Let's do it
1090    the easy way for now, even if the branch-not-taken case now costs
1091    one additional instruction.  Out-of-range branches are not supposed
1092    to be common, anyway.
1093 
1094    Branch likely.  If label is out of range, we turn:
1095 
1096 	beql reg1, reg2, label
1097 	delay slot (annulled if branch not taken)
1098 
1099    into
1100 
1101         beql reg1, reg2, 1f
1102         nop
1103         beql $0, $0, 2f
1104         nop
1105      1: j[al] label
1106         delay slot (executed only if branch taken)
1107      2:
1108 
1109    It would be possible to generate a shorter sequence by losing the
1110    likely bit, generating something like:
1111 
1112 	bne reg1, reg2, 0f
1113 	nop
1114 	j[al] label
1115 	delay slot (executed only if branch taken)
1116      0:
1117 
1118 	beql -> bne
1119 	bnel -> beq
1120 	blezl -> bgtz
1121 	bgtzl -> blez
1122 	bltzl -> bgez
1123 	bgezl -> bltz
1124 	bc1fl -> bc1t
1125 	bc1tl -> bc1f
1126 
1127 	bltzall -> bgezal  (with jal label instead of j label)
1128 	bgezall -> bltzal  (ditto)
1129 
1130 
1131    but it's not clear that it would actually improve performance.  */
1132 #define RELAX_BRANCH_ENCODE(at, pic,				\
1133 			    uncond, likely, link, toofar)	\
1134   ((relax_substateT)						\
1135    (0xc0000000							\
1136     | ((at) & 0x1f)						\
1137     | ((pic) ? 0x20 : 0)					\
1138     | ((toofar) ? 0x40 : 0)					\
1139     | ((link) ? 0x80 : 0)					\
1140     | ((likely) ? 0x100 : 0)					\
1141     | ((uncond) ? 0x200 : 0)))
1142 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1143 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1144 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1145 #define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1146 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1147 #define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
1148 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1149 
1150 /* For mips16 code, we use an entirely different form of relaxation.
1151    mips16 supports two versions of most instructions which take
1152    immediate values: a small one which takes some small value, and a
1153    larger one which takes a 16 bit value.  Since branches also follow
1154    this pattern, relaxing these values is required.
1155 
1156    We can assemble both mips16 and normal MIPS code in a single
1157    object.  Therefore, we need to support this type of relaxation at
1158    the same time that we support the relaxation described above.  We
1159    use the high bit of the subtype field to distinguish these cases.
1160 
1161    The information we store for this type of relaxation is the
1162    argument code found in the opcode file for this relocation, whether
1163    the user explicitly requested a small or extended form, and whether
1164    the relocation is in a jump or jal delay slot.  That tells us the
1165    size of the value, and how it should be stored.  We also store
1166    whether the fragment is considered to be extended or not.  We also
1167    store whether this is known to be a branch to a different section,
1168    whether we have tried to relax this frag yet, and whether we have
1169    ever extended a PC relative fragment because of a shift count.  */
1170 #define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro,	\
1171 			    small, ext,				\
1172 			    dslot, jal_dslot)			\
1173   (0x80000000							\
1174    | ((type) & 0xff)						\
1175    | ((e2) ? 0x100 : 0)						\
1176    | ((pic) ? 0x200 : 0)					\
1177    | ((sym32) ? 0x400 : 0)					\
1178    | ((nomacro) ? 0x800 : 0)					\
1179    | ((small) ? 0x1000 : 0)					\
1180    | ((ext) ? 0x2000 : 0)					\
1181    | ((dslot) ? 0x4000 : 0)					\
1182    | ((jal_dslot) ? 0x8000 : 0))
1183 
1184 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1185 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1186 #define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1187 #define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1188 #define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1189 #define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1190 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1191 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1192 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1193 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1194 
1195 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1196 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1197 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1198 #define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1199 #define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1200 #define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1201 #define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1202 #define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1203 #define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
1204 
1205 /* For microMIPS code, we use relaxation similar to one we use for
1206    MIPS16 code.  Some instructions that take immediate values support
1207    two encodings: a small one which takes some small value, and a
1208    larger one which takes a 16 bit value.  As some branches also follow
1209    this pattern, relaxing these values is required.
1210 
1211    We can assemble both microMIPS and normal MIPS code in a single
1212    object.  Therefore, we need to support this type of relaxation at
1213    the same time that we support the relaxation described above.  We
1214    use one of the high bits of the subtype field to distinguish these
1215    cases.
1216 
1217    The information we store for this type of relaxation is the argument
1218    code found in the opcode file for this relocation, the register
1219    selected as the assembler temporary, whether in the 32-bit
1220    instruction mode, whether the branch is unconditional, whether it is
1221    compact, whether there is no delay-slot instruction available to fill
1222    in, whether it stores the link address implicitly in $ra, whether
1223    relaxation of out-of-range 32-bit branches to a sequence of
1224    instructions is enabled, and whether the displacement of a branch is
1225    too large to fit as an immediate argument of a 16-bit and a 32-bit
1226    branch, respectively.  */
1227 #define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic,		\
1228 			       uncond, compact, link, nods,	\
1229 			       relax32, toofar16, toofar32)	\
1230   (0x40000000							\
1231    | ((type) & 0xff)						\
1232    | (((at) & 0x1f) << 8)					\
1233    | ((insn32) ? 0x2000 : 0)					\
1234    | ((pic) ? 0x4000 : 0)					\
1235    | ((uncond) ? 0x8000 : 0)					\
1236    | ((compact) ? 0x10000 : 0)					\
1237    | ((link) ? 0x20000 : 0)					\
1238    | ((nods) ? 0x40000 : 0)					\
1239    | ((relax32) ? 0x80000 : 0)					\
1240    | ((toofar16) ? 0x100000 : 0)				\
1241    | ((toofar32) ? 0x200000 : 0))
1242 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1243 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1244 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1245 #define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
1246 #define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1247 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1248 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1249 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1250 #define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1251 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1252 
1253 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1254 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1255 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1256 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1257 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1258 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
1259 
1260 /* Sign-extend 16-bit value X.  */
1261 #define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1262 
1263 /* Is the given value a sign-extended 32-bit value?  */
1264 #define IS_SEXT_32BIT_NUM(x)						\
1265   (((x) &~ (offsetT) 0x7fffffff) == 0					\
1266    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1267 
1268 /* Is the given value a sign-extended 16-bit value?  */
1269 #define IS_SEXT_16BIT_NUM(x)						\
1270   (((x) &~ (offsetT) 0x7fff) == 0					\
1271    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1272 
1273 /* Is the given value a sign-extended 12-bit value?  */
1274 #define IS_SEXT_12BIT_NUM(x)						\
1275   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1276 
1277 /* Is the given value a sign-extended 9-bit value?  */
1278 #define IS_SEXT_9BIT_NUM(x)						\
1279   (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1280 
1281 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1282 #define IS_ZEXT_32BIT_NUM(x)						\
1283   (((x) &~ (offsetT) 0xffffffff) == 0					\
1284    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1285 
1286 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1287    SHIFT places.  */
1288 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1289   (((STRUCT) >> (SHIFT)) & (MASK))
1290 
1291 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1292 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1293   (!(MICROMIPS) \
1294    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1295    : EXTRACT_BITS ((INSN).insn_opcode, \
1296 		   MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1297 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1298   EXTRACT_BITS ((INSN).insn_opcode, \
1299 		MIPS16OP_MASK_##FIELD, \
1300 		MIPS16OP_SH_##FIELD)
1301 
1302 /* The MIPS16 EXTEND opcode, shifted left 16 places.  */
1303 #define MIPS16_EXTEND (0xf000U << 16)
1304 
1305 /* Whether or not we are emitting a branch-likely macro.  */
1306 static bool emit_branch_likely_macro = false;
1307 
1308 /* Global variables used when generating relaxable macros.  See the
1309    comment above RELAX_ENCODE for more details about how relaxation
1310    is used.  */
1311 static struct {
1312   /* 0 if we're not emitting a relaxable macro.
1313      1 if we're emitting the first of the two relaxation alternatives.
1314      2 if we're emitting the second alternative.  */
1315   int sequence;
1316 
1317   /* The first relaxable fixup in the current frag.  (In other words,
1318      the first fixup that refers to relaxable code.)  */
1319   fixS *first_fixup;
1320 
1321   /* sizes[0] says how many bytes of the first alternative are stored in
1322      the current frag.  Likewise sizes[1] for the second alternative.  */
1323   unsigned int sizes[2];
1324 
1325   /* The symbol on which the choice of sequence depends.  */
1326   symbolS *symbol;
1327 } mips_relax;
1328 
1329 /* Global variables used to decide whether a macro needs a warning.  */
1330 static struct {
1331   /* True if the macro is in a branch delay slot.  */
1332   bool delay_slot_p;
1333 
1334   /* Set to the length in bytes required if the macro is in a delay slot
1335      that requires a specific length of instruction, otherwise zero.  */
1336   unsigned int delay_slot_length;
1337 
1338   /* For relaxable macros, sizes[0] is the length of the first alternative
1339      in bytes and sizes[1] is the length of the second alternative.
1340      For non-relaxable macros, both elements give the length of the
1341      macro in bytes.  */
1342   unsigned int sizes[2];
1343 
1344   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1345      instruction of the first alternative in bytes and first_insn_sizes[1]
1346      is the length of the first instruction of the second alternative.
1347      For non-relaxable macros, both elements give the length of the first
1348      instruction in bytes.
1349 
1350      Set to zero if we haven't yet seen the first instruction.  */
1351   unsigned int first_insn_sizes[2];
1352 
1353   /* For relaxable macros, insns[0] is the number of instructions for the
1354      first alternative and insns[1] is the number of instructions for the
1355      second alternative.
1356 
1357      For non-relaxable macros, both elements give the number of
1358      instructions for the macro.  */
1359   unsigned int insns[2];
1360 
1361   /* The first variant frag for this macro.  */
1362   fragS *first_frag;
1363 } mips_macro_warning;
1364 
1365 /* Prototypes for static functions.  */
1366 
1367 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1368 
1369 static void append_insn
1370   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1371    bool expansionp);
1372 static void mips_no_prev_insn (void);
1373 static void macro_build (expressionS *, const char *, const char *, ...);
1374 static void mips16_macro_build
1375   (expressionS *, const char *, const char *, va_list *);
1376 static void load_register (int, expressionS *, int);
1377 static void macro_build (expressionS *, const char *, const char *, ...);
1378 static void macro_start (void);
1379 static void macro_end (void);
1380 static void macro (struct mips_cl_insn *ip, char *str);
1381 static void mips16_macro (struct mips_cl_insn * ip);
1382 static void mips_ip (char *str, struct mips_cl_insn * ip);
1383 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1384 static unsigned long mips16_immed_extend (offsetT, unsigned int);
1385 static void mips16_immed
1386   (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1387    unsigned int, unsigned long *);
1388 static size_t my_getSmallExpression
1389   (expressionS *, bfd_reloc_code_real_type *, char *);
1390 static void my_getExpression (expressionS *, char *);
1391 static void s_align (int);
1392 static void s_change_sec (int);
1393 static void s_change_section (int);
1394 static void s_cons (int);
1395 static void s_float_cons (int);
1396 static void s_mips_globl (int);
1397 static void s_option (int);
1398 static void s_mipsset (int);
1399 static void s_abicalls (int);
1400 static void s_cpload (int);
1401 static void s_cpsetup (int);
1402 static void s_cplocal (int);
1403 static void s_cprestore (int);
1404 static void s_cpreturn (int);
1405 static void s_dtprelword (int);
1406 static void s_dtpreldword (int);
1407 static void s_tprelword (int);
1408 static void s_tpreldword (int);
1409 static void s_gpvalue (int);
1410 static void s_gpword (int);
1411 static void s_gpdword (int);
1412 static void s_ehword (int);
1413 static void s_cpadd (int);
1414 static void s_insn (int);
1415 static void s_nan (int);
1416 static void s_module (int);
1417 static void s_mips_ent (int);
1418 static void s_mips_end (int);
1419 static void s_mips_frame (int);
1420 static void s_mips_mask (int reg_type);
1421 static void s_mips_stab (int);
1422 static void s_mips_weakext (int);
1423 static void s_mips_file (int);
1424 static void s_mips_loc (int);
1425 static bool pic_need_relax (symbolS *);
1426 static int relaxed_branch_length (fragS *, asection *, int);
1427 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1428 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1429 static void file_mips_check_options (void);
1430 
1431 /* Table and functions used to map between CPU/ISA names, and
1432    ISA levels, and CPU numbers.  */
1433 
1434 struct mips_cpu_info
1435 {
1436   const char *name;           /* CPU or ISA name.  */
1437   int flags;                  /* MIPS_CPU_* flags.  */
1438   int ase;                    /* Set of ASEs implemented by the CPU.  */
1439   int isa;                    /* ISA level.  */
1440   int cpu;                    /* CPU number (default CPU if ISA).  */
1441 };
1442 
1443 #define MIPS_CPU_IS_ISA		0x0001	/* Is this an ISA?  (If 0, a CPU.) */
1444 
1445 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1446 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1447 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1448 
1449 /* Command-line options.  */
1450 const char *md_shortopts = "O::g::G:";
1451 
1452 enum options
1453   {
1454     OPTION_MARCH = OPTION_MD_BASE,
1455     OPTION_MTUNE,
1456     OPTION_MIPS1,
1457     OPTION_MIPS2,
1458     OPTION_MIPS3,
1459     OPTION_MIPS4,
1460     OPTION_MIPS5,
1461     OPTION_MIPS32,
1462     OPTION_MIPS64,
1463     OPTION_MIPS32R2,
1464     OPTION_MIPS32R3,
1465     OPTION_MIPS32R5,
1466     OPTION_MIPS32R6,
1467     OPTION_MIPS64R2,
1468     OPTION_MIPS64R3,
1469     OPTION_MIPS64R5,
1470     OPTION_MIPS64R6,
1471     OPTION_MIPS16,
1472     OPTION_NO_MIPS16,
1473     OPTION_MIPS3D,
1474     OPTION_NO_MIPS3D,
1475     OPTION_MDMX,
1476     OPTION_NO_MDMX,
1477     OPTION_DSP,
1478     OPTION_NO_DSP,
1479     OPTION_MT,
1480     OPTION_NO_MT,
1481     OPTION_VIRT,
1482     OPTION_NO_VIRT,
1483     OPTION_MSA,
1484     OPTION_NO_MSA,
1485     OPTION_SMARTMIPS,
1486     OPTION_NO_SMARTMIPS,
1487     OPTION_DSPR2,
1488     OPTION_NO_DSPR2,
1489     OPTION_DSPR3,
1490     OPTION_NO_DSPR3,
1491     OPTION_EVA,
1492     OPTION_NO_EVA,
1493     OPTION_XPA,
1494     OPTION_NO_XPA,
1495     OPTION_MICROMIPS,
1496     OPTION_NO_MICROMIPS,
1497     OPTION_MCU,
1498     OPTION_NO_MCU,
1499     OPTION_MIPS16E2,
1500     OPTION_NO_MIPS16E2,
1501     OPTION_CRC,
1502     OPTION_NO_CRC,
1503     OPTION_M4650,
1504     OPTION_NO_M4650,
1505     OPTION_M4010,
1506     OPTION_NO_M4010,
1507     OPTION_M4100,
1508     OPTION_NO_M4100,
1509     OPTION_M3900,
1510     OPTION_NO_M3900,
1511     OPTION_M7000_HILO_FIX,
1512     OPTION_MNO_7000_HILO_FIX,
1513     OPTION_FIX_24K,
1514     OPTION_NO_FIX_24K,
1515     OPTION_FIX_RM7000,
1516     OPTION_NO_FIX_RM7000,
1517     OPTION_FIX_LOONGSON3_LLSC,
1518     OPTION_NO_FIX_LOONGSON3_LLSC,
1519     OPTION_FIX_LOONGSON2F_JUMP,
1520     OPTION_NO_FIX_LOONGSON2F_JUMP,
1521     OPTION_FIX_LOONGSON2F_NOP,
1522     OPTION_NO_FIX_LOONGSON2F_NOP,
1523     OPTION_FIX_VR4120,
1524     OPTION_NO_FIX_VR4120,
1525     OPTION_FIX_VR4130,
1526     OPTION_NO_FIX_VR4130,
1527     OPTION_FIX_CN63XXP1,
1528     OPTION_NO_FIX_CN63XXP1,
1529     OPTION_FIX_R5900,
1530     OPTION_NO_FIX_R5900,
1531     OPTION_TRAP,
1532     OPTION_BREAK,
1533     OPTION_EB,
1534     OPTION_EL,
1535     OPTION_FP32,
1536     OPTION_GP32,
1537     OPTION_CONSTRUCT_FLOATS,
1538     OPTION_NO_CONSTRUCT_FLOATS,
1539     OPTION_FP64,
1540     OPTION_FPXX,
1541     OPTION_GP64,
1542     OPTION_RELAX_BRANCH,
1543     OPTION_NO_RELAX_BRANCH,
1544     OPTION_IGNORE_BRANCH_ISA,
1545     OPTION_NO_IGNORE_BRANCH_ISA,
1546     OPTION_INSN32,
1547     OPTION_NO_INSN32,
1548     OPTION_MSHARED,
1549     OPTION_MNO_SHARED,
1550     OPTION_MSYM32,
1551     OPTION_MNO_SYM32,
1552     OPTION_SOFT_FLOAT,
1553     OPTION_HARD_FLOAT,
1554     OPTION_SINGLE_FLOAT,
1555     OPTION_DOUBLE_FLOAT,
1556     OPTION_32,
1557     OPTION_CALL_SHARED,
1558     OPTION_CALL_NONPIC,
1559     OPTION_NON_SHARED,
1560     OPTION_XGOT,
1561     OPTION_MABI,
1562     OPTION_N32,
1563     OPTION_64,
1564     OPTION_MDEBUG,
1565     OPTION_NO_MDEBUG,
1566     OPTION_PDR,
1567     OPTION_NO_PDR,
1568     OPTION_MVXWORKS_PIC,
1569     OPTION_NAN,
1570     OPTION_ODD_SPREG,
1571     OPTION_NO_ODD_SPREG,
1572     OPTION_GINV,
1573     OPTION_NO_GINV,
1574     OPTION_LOONGSON_MMI,
1575     OPTION_NO_LOONGSON_MMI,
1576     OPTION_LOONGSON_CAM,
1577     OPTION_NO_LOONGSON_CAM,
1578     OPTION_LOONGSON_EXT,
1579     OPTION_NO_LOONGSON_EXT,
1580     OPTION_LOONGSON_EXT2,
1581     OPTION_NO_LOONGSON_EXT2,
1582     OPTION_FIX_LOONGSON2F_BTB,
1583     OPTION_NO_FIX_LOONGSON2F_BTB,
1584     OPTION_END_OF_ENUM
1585   };
1586 
1587 struct option md_longopts[] =
1588 {
1589   /* Options which specify architecture.  */
1590   {"march", required_argument, NULL, OPTION_MARCH},
1591   {"mtune", required_argument, NULL, OPTION_MTUNE},
1592   {"mips0", no_argument, NULL, OPTION_MIPS1},
1593   {"mips1", no_argument, NULL, OPTION_MIPS1},
1594   {"mips2", no_argument, NULL, OPTION_MIPS2},
1595   {"mips3", no_argument, NULL, OPTION_MIPS3},
1596   {"mips4", no_argument, NULL, OPTION_MIPS4},
1597   {"mips5", no_argument, NULL, OPTION_MIPS5},
1598   {"mips32", no_argument, NULL, OPTION_MIPS32},
1599   {"mips64", no_argument, NULL, OPTION_MIPS64},
1600   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1601   {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1602   {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
1603   {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
1604   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1605   {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1606   {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
1607   {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
1608 
1609   /* Options which specify Application Specific Extensions (ASEs).  */
1610   {"mips16", no_argument, NULL, OPTION_MIPS16},
1611   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1612   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1613   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1614   {"mdmx", no_argument, NULL, OPTION_MDMX},
1615   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1616   {"mdsp", no_argument, NULL, OPTION_DSP},
1617   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1618   {"mmt", no_argument, NULL, OPTION_MT},
1619   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1620   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1621   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1622   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1623   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1624   {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1625   {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
1626   {"meva", no_argument, NULL, OPTION_EVA},
1627   {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1628   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1629   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1630   {"mmcu", no_argument, NULL, OPTION_MCU},
1631   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1632   {"mvirt", no_argument, NULL, OPTION_VIRT},
1633   {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1634   {"mmsa", no_argument, NULL, OPTION_MSA},
1635   {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
1636   {"mxpa", no_argument, NULL, OPTION_XPA},
1637   {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
1638   {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1639   {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
1640   {"mcrc", no_argument, NULL, OPTION_CRC},
1641   {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
1642   {"mginv", no_argument, NULL, OPTION_GINV},
1643   {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
1644   {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1645   {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
1646   {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1647   {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
1648   {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1649   {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
1650   {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1651   {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
1652 
1653   /* Old-style architecture options.  Don't add more of these.  */
1654   {"m4650", no_argument, NULL, OPTION_M4650},
1655   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1656   {"m4010", no_argument, NULL, OPTION_M4010},
1657   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1658   {"m4100", no_argument, NULL, OPTION_M4100},
1659   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1660   {"m3900", no_argument, NULL, OPTION_M3900},
1661   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1662 
1663   /* Options which enable bug fixes.  */
1664   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1665   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1666   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1667   {"mfix-loongson3-llsc",   no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1668   {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
1669   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1670   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1671   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1672   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1673   {"mfix-loongson2f-btb", no_argument, NULL, OPTION_FIX_LOONGSON2F_BTB},
1674   {"mno-fix-loongson2f-btb", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_BTB},
1675   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
1676   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1677   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
1678   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1679   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
1680   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1681   {"mfix-rm7000",    no_argument, NULL, OPTION_FIX_RM7000},
1682   {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
1683   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1684   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1685   {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1686   {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
1687 
1688   /* Miscellaneous options.  */
1689   {"trap", no_argument, NULL, OPTION_TRAP},
1690   {"no-break", no_argument, NULL, OPTION_TRAP},
1691   {"break", no_argument, NULL, OPTION_BREAK},
1692   {"no-trap", no_argument, NULL, OPTION_BREAK},
1693   {"EB", no_argument, NULL, OPTION_EB},
1694   {"EL", no_argument, NULL, OPTION_EL},
1695   {"mfp32", no_argument, NULL, OPTION_FP32},
1696   {"mgp32", no_argument, NULL, OPTION_GP32},
1697   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1698   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1699   {"mfp64", no_argument, NULL, OPTION_FP64},
1700   {"mfpxx", no_argument, NULL, OPTION_FPXX},
1701   {"mgp64", no_argument, NULL, OPTION_GP64},
1702   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1703   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
1704   {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1705   {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
1706   {"minsn32", no_argument, NULL, OPTION_INSN32},
1707   {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
1708   {"mshared", no_argument, NULL, OPTION_MSHARED},
1709   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1710   {"msym32", no_argument, NULL, OPTION_MSYM32},
1711   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1712   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1713   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1714   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1715   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1716   {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1717   {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
1718 
1719   /* Strictly speaking this next option is ELF specific,
1720      but we allow it for other ports as well in order to
1721      make testing easier.  */
1722   {"32", no_argument, NULL, OPTION_32},
1723 
1724   /* ELF-specific options.  */
1725   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1726   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1727   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1728   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
1729   {"xgot", no_argument, NULL, OPTION_XGOT},
1730   {"mabi", required_argument, NULL, OPTION_MABI},
1731   {"n32", no_argument, NULL, OPTION_N32},
1732   {"64", no_argument, NULL, OPTION_64},
1733   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1734   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1735   {"mpdr", no_argument, NULL, OPTION_PDR},
1736   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1737   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
1738   {"mnan", required_argument, NULL, OPTION_NAN},
1739 
1740   {NULL, no_argument, NULL, 0}
1741 };
1742 size_t md_longopts_size = sizeof (md_longopts);
1743 
1744 /* Information about either an Application Specific Extension or an
1745    optional architecture feature that, for simplicity, we treat in the
1746    same way as an ASE.  */
1747 struct mips_ase
1748 {
1749   /* The name of the ASE, used in both the command-line and .set options.  */
1750   const char *name;
1751 
1752   /* The associated ASE_* flags.  If the ASE is available on both 32-bit
1753      and 64-bit architectures, the flags here refer to the subset that
1754      is available on both.  */
1755   unsigned int flags;
1756 
1757   /* The ASE_* flag used for instructions that are available on 64-bit
1758      architectures but that are not included in FLAGS.  */
1759   unsigned int flags64;
1760 
1761   /* The command-line options that turn the ASE on and off.  */
1762   int option_on;
1763   int option_off;
1764 
1765   /* The minimum required architecture revisions for MIPS32, MIPS64,
1766      microMIPS32 and microMIPS64, or -1 if the extension isn't supported.  */
1767   int mips32_rev;
1768   int mips64_rev;
1769   int micromips32_rev;
1770   int micromips64_rev;
1771 
1772   /* The architecture where the ASE was removed or -1 if the extension has not
1773      been removed.  */
1774   int rem_rev;
1775 };
1776 
1777 /* A table of all supported ASEs.  */
1778 static const struct mips_ase mips_ases[] = {
1779   { "dsp", ASE_DSP, ASE_DSP64,
1780     OPTION_DSP, OPTION_NO_DSP,
1781     2, 2, 2, 2,
1782     -1 },
1783 
1784   { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1785     OPTION_DSPR2, OPTION_NO_DSPR2,
1786     2, 2, 2, 2,
1787     -1 },
1788 
1789   { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1790     OPTION_DSPR3, OPTION_NO_DSPR3,
1791     6, 6, -1, -1,
1792     -1 },
1793 
1794   { "eva", ASE_EVA, 0,
1795     OPTION_EVA, OPTION_NO_EVA,
1796      2,  2,  2,  2,
1797     -1 },
1798 
1799   { "mcu", ASE_MCU, 0,
1800     OPTION_MCU, OPTION_NO_MCU,
1801      2,  2,  2,  2,
1802     -1 },
1803 
1804   /* Deprecated in MIPS64r5, but we don't implement that yet.  */
1805   { "mdmx", ASE_MDMX, 0,
1806     OPTION_MDMX, OPTION_NO_MDMX,
1807     -1, 1, -1, -1,
1808      6 },
1809 
1810   /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2.  */
1811   { "mips3d", ASE_MIPS3D, 0,
1812     OPTION_MIPS3D, OPTION_NO_MIPS3D,
1813     2, 1, -1, -1,
1814     6 },
1815 
1816   { "mt", ASE_MT, 0,
1817     OPTION_MT, OPTION_NO_MT,
1818      2,  2, -1, -1,
1819     -1 },
1820 
1821   { "smartmips", ASE_SMARTMIPS, 0,
1822     OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1823     1, -1, -1, -1,
1824     6 },
1825 
1826   { "virt", ASE_VIRT, ASE_VIRT64,
1827     OPTION_VIRT, OPTION_NO_VIRT,
1828      2,  2,  2,  2,
1829     -1 },
1830 
1831   { "msa", ASE_MSA, ASE_MSA64,
1832     OPTION_MSA, OPTION_NO_MSA,
1833      2,  2,  2,  2,
1834     -1 },
1835 
1836   { "xpa", ASE_XPA, 0,
1837     OPTION_XPA, OPTION_NO_XPA,
1838     2, 2, 2, 2,
1839     -1 },
1840 
1841   { "mips16e2", ASE_MIPS16E2, 0,
1842     OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1843     2,  2, -1, -1,
1844     6 },
1845 
1846   { "crc", ASE_CRC, ASE_CRC64,
1847     OPTION_CRC, OPTION_NO_CRC,
1848     6,  6, -1, -1,
1849     -1 },
1850 
1851   { "ginv", ASE_GINV, 0,
1852     OPTION_GINV, OPTION_NO_GINV,
1853     6,  6, 6, 6,
1854     -1 },
1855 
1856   { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1857     OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1858     0, 0, -1, -1,
1859     -1 },
1860 
1861   { "loongson-cam", ASE_LOONGSON_CAM, 0,
1862     OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1863     0, 0, -1, -1,
1864     -1 },
1865 
1866   { "loongson-ext", ASE_LOONGSON_EXT, 0,
1867     OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1868     0, 0, -1, -1,
1869     -1 },
1870 
1871   { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1872     OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1873     0, 0, -1, -1,
1874     -1 },
1875 };
1876 
1877 /* The set of ASEs that require -mfp64.  */
1878 #define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
1879 
1880 /* Groups of ASE_* flags that represent different revisions of an ASE.  */
1881 static const unsigned int mips_ase_groups[] = {
1882   ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1883   ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
1884 };
1885 
1886 /* Pseudo-op table.
1887 
1888    The following pseudo-ops from the Kane and Heinrich MIPS book
1889    should be defined here, but are currently unsupported: .alias,
1890    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1891 
1892    The following pseudo-ops from the Kane and Heinrich MIPS book are
1893    specific to the type of debugging information being generated, and
1894    should be defined by the object format: .aent, .begin, .bend,
1895    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1896    .vreg.
1897 
1898    The following pseudo-ops from the Kane and Heinrich MIPS book are
1899    not MIPS CPU specific, but are also not specific to the object file
1900    format.  This file is probably the best place to define them, but
1901    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1902 
1903 static const pseudo_typeS mips_pseudo_table[] =
1904 {
1905   /* MIPS specific pseudo-ops.  */
1906   {"option", s_option, 0},
1907   {"set", s_mipsset, 0},
1908   {"rdata", s_change_sec, 'r'},
1909   {"sdata", s_change_sec, 's'},
1910   {"livereg", s_ignore, 0},
1911   {"abicalls", s_abicalls, 0},
1912   {"cpload", s_cpload, 0},
1913   {"cpsetup", s_cpsetup, 0},
1914   {"cplocal", s_cplocal, 0},
1915   {"cprestore", s_cprestore, 0},
1916   {"cpreturn", s_cpreturn, 0},
1917   {"dtprelword", s_dtprelword, 0},
1918   {"dtpreldword", s_dtpreldword, 0},
1919   {"tprelword", s_tprelword, 0},
1920   {"tpreldword", s_tpreldword, 0},
1921   {"gpvalue", s_gpvalue, 0},
1922   {"gpword", s_gpword, 0},
1923   {"gpdword", s_gpdword, 0},
1924   {"ehword", s_ehword, 0},
1925   {"cpadd", s_cpadd, 0},
1926   {"insn", s_insn, 0},
1927   {"nan", s_nan, 0},
1928   {"module", s_module, 0},
1929 
1930   /* Relatively generic pseudo-ops that happen to be used on MIPS
1931      chips.  */
1932   {"asciiz", stringer, 8 + 1},
1933   {"bss", s_change_sec, 'b'},
1934   {"err", s_err, 0},
1935   {"half", s_cons, 1},
1936   {"dword", s_cons, 3},
1937   {"weakext", s_mips_weakext, 0},
1938   {"origin", s_org, 0},
1939   {"repeat", s_rept, 0},
1940 
1941   /* For MIPS this is non-standard, but we define it for consistency.  */
1942   {"sbss", s_change_sec, 'B'},
1943 
1944   /* These pseudo-ops are defined in read.c, but must be overridden
1945      here for one reason or another.  */
1946   {"align", s_align, 0},
1947   {"byte", s_cons, 0},
1948   {"data", s_change_sec, 'd'},
1949   {"double", s_float_cons, 'd'},
1950   {"float", s_float_cons, 'f'},
1951   {"globl", s_mips_globl, 0},
1952   {"global", s_mips_globl, 0},
1953   {"hword", s_cons, 1},
1954   {"int", s_cons, 2},
1955   {"long", s_cons, 2},
1956   {"octa", s_cons, 4},
1957   {"quad", s_cons, 3},
1958   {"section", s_change_section, 0},
1959   {"short", s_cons, 1},
1960   {"single", s_float_cons, 'f'},
1961   {"stabd", s_mips_stab, 'd'},
1962   {"stabn", s_mips_stab, 'n'},
1963   {"stabs", s_mips_stab, 's'},
1964   {"text", s_change_sec, 't'},
1965   {"word", s_cons, 2},
1966 
1967   { "extern", ecoff_directive_extern, 0},
1968 
1969   { NULL, NULL, 0 },
1970 };
1971 
1972 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1973 {
1974   /* These pseudo-ops should be defined by the object file format.
1975      However, a.out doesn't support them, so we have versions here.  */
1976   {"aent", s_mips_ent, 1},
1977   {"bgnb", s_ignore, 0},
1978   {"end", s_mips_end, 0},
1979   {"endb", s_ignore, 0},
1980   {"ent", s_mips_ent, 0},
1981   {"file", s_mips_file, 0},
1982   {"fmask", s_mips_mask, 'F'},
1983   {"frame", s_mips_frame, 0},
1984   {"loc", s_mips_loc, 0},
1985   {"mask", s_mips_mask, 'R'},
1986   {"verstamp", s_ignore, 0},
1987   { NULL, NULL, 0 },
1988 };
1989 
1990 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1991    purpose of the `.dc.a' internal pseudo-op.  */
1992 
1993 int
1994 mips_address_bytes (void)
1995 {
1996   file_mips_check_options ();
1997   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1998 }
1999 
2000 extern void pop_insert (const pseudo_typeS *);
2001 
2002 void
2003 mips_pop_insert (void)
2004 {
2005   pop_insert (mips_pseudo_table);
2006   if (! ECOFF_DEBUGGING)
2007     pop_insert (mips_nonecoff_pseudo_table);
2008 }
2009 
2010 /* Symbols labelling the current insn.  */
2011 
2012 struct insn_label_list
2013 {
2014   struct insn_label_list *next;
2015   symbolS *label;
2016 };
2017 
2018 static struct insn_label_list *free_insn_labels;
2019 #define label_list tc_segment_info_data.labels
2020 
2021 static void mips_clear_insn_labels (void);
2022 static void mips_mark_labels (void);
2023 static void mips_compressed_mark_labels (void);
2024 
2025 static inline void
2026 mips_clear_insn_labels (void)
2027 {
2028   struct insn_label_list **pl;
2029   segment_info_type *si;
2030 
2031   if (now_seg)
2032     {
2033       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2034 	;
2035 
2036       si = seg_info (now_seg);
2037       *pl = si->label_list;
2038       si->label_list = NULL;
2039     }
2040 }
2041 
2042 /* Mark instruction labels in MIPS16/microMIPS mode.  */
2043 
2044 static inline void
2045 mips_mark_labels (void)
2046 {
2047   if (HAVE_CODE_COMPRESSION)
2048     mips_compressed_mark_labels ();
2049 }
2050 
2051 static char *expr_end;
2052 
2053 /* An expression in a macro instruction.  This is set by mips_ip and
2054    mips16_ip and when populated is always an O_constant.  */
2055 
2056 static expressionS imm_expr;
2057 
2058 /* The relocatable field in an instruction and the relocs associated
2059    with it.  These variables are used for instructions like LUI and
2060    JAL as well as true offsets.  They are also used for address
2061    operands in macros.  */
2062 
2063 static expressionS offset_expr;
2064 static bfd_reloc_code_real_type offset_reloc[3]
2065   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2066 
2067 /* This is set to the resulting size of the instruction to be produced
2068    by mips16_ip if an explicit extension is used or by mips_ip if an
2069    explicit size is supplied.  */
2070 
2071 static unsigned int forced_insn_length;
2072 
2073 /* True if we are assembling an instruction.  All dot symbols defined during
2074    this time should be treated as code labels.  */
2075 
2076 static bool mips_assembling_insn;
2077 
2078 /* The pdr segment for per procedure frame/regmask info.  Not used for
2079    ECOFF debugging.  */
2080 
2081 static segT pdr_seg;
2082 
2083 /* The default target format to use.  */
2084 
2085 #if defined (TE_FreeBSD)
2086 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2087 #elif defined (TE_TMIPS)
2088 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2089 #else
2090 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2091 #endif
2092 
2093 const char *
2094 mips_target_format (void)
2095 {
2096   switch (OUTPUT_FLAVOR)
2097     {
2098     case bfd_target_elf_flavour:
2099 #ifdef TE_VXWORKS
2100       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2101 	return (target_big_endian
2102 		? "elf32-bigmips-vxworks"
2103 		: "elf32-littlemips-vxworks");
2104 #endif
2105       return (target_big_endian
2106 	      ? (HAVE_64BIT_OBJECTS
2107 		 ? ELF_TARGET ("elf64-", "big")
2108 		 : (HAVE_NEWABI
2109 		    ? ELF_TARGET ("elf32-n", "big")
2110 		    : ELF_TARGET ("elf32-", "big")))
2111 	      : (HAVE_64BIT_OBJECTS
2112 		 ? ELF_TARGET ("elf64-", "little")
2113 		 : (HAVE_NEWABI
2114 		    ? ELF_TARGET ("elf32-n", "little")
2115 		    : ELF_TARGET ("elf32-", "little"))));
2116     default:
2117       abort ();
2118       return NULL;
2119     }
2120 }
2121 
2122 /* Return the ISA revision that is currently in use, or 0 if we are
2123    generating code for MIPS V or below.  */
2124 
2125 static int
2126 mips_isa_rev (void)
2127 {
2128   if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2129     return 2;
2130 
2131   if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2132     return 3;
2133 
2134   if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2135     return 5;
2136 
2137   if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2138     return 6;
2139 
2140   /* microMIPS implies revision 2 or above.  */
2141   if (mips_opts.micromips)
2142     return 2;
2143 
2144   if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2145     return 1;
2146 
2147   return 0;
2148 }
2149 
2150 /* Return the mask of all ASEs that are revisions of those in FLAGS.  */
2151 
2152 static unsigned int
2153 mips_ase_mask (unsigned int flags)
2154 {
2155   unsigned int i;
2156 
2157   for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2158     if (flags & mips_ase_groups[i])
2159       flags |= mips_ase_groups[i];
2160   return flags;
2161 }
2162 
2163 /* Check whether the current ISA supports ASE.  Issue a warning if
2164    appropriate.  */
2165 
2166 static void
2167 mips_check_isa_supports_ase (const struct mips_ase *ase)
2168 {
2169   const char *base;
2170   int min_rev, size;
2171   static unsigned int warned_isa;
2172   static unsigned int warned_fp32;
2173 
2174   if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2175     min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2176   else
2177     min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2178   if ((min_rev < 0 || mips_isa_rev () < min_rev)
2179       && (warned_isa & ase->flags) != ase->flags)
2180     {
2181       warned_isa |= ase->flags;
2182       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2183       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2184       if (min_rev < 0)
2185 	as_warn (_("the %d-bit %s architecture does not support the"
2186 		   " `%s' extension"), size, base, ase->name);
2187       else
2188 	as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
2189 		 ase->name, base, size, min_rev);
2190     }
2191   else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2192 	   && (warned_isa & ase->flags) != ase->flags)
2193     {
2194       warned_isa |= ase->flags;
2195       base = mips_opts.micromips ? "microMIPS" : "MIPS";
2196       size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2197       as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2198 	       ase->name, base, size, ase->rem_rev);
2199     }
2200 
2201   if ((ase->flags & FP64_ASES)
2202       && mips_opts.fp != 64
2203       && (warned_fp32 & ase->flags) != ase->flags)
2204     {
2205       warned_fp32 |= ase->flags;
2206       as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
2207     }
2208 }
2209 
2210 /* Check all enabled ASEs to see whether they are supported by the
2211    chosen architecture.  */
2212 
2213 static void
2214 mips_check_isa_supports_ases (void)
2215 {
2216   unsigned int i, mask;
2217 
2218   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2219     {
2220       mask = mips_ase_mask (mips_ases[i].flags);
2221       if ((mips_opts.ase & mask) == mips_ases[i].flags)
2222 	mips_check_isa_supports_ase (&mips_ases[i]);
2223     }
2224 }
2225 
2226 /* Set the state of ASE to ENABLED_P.  Return the mask of ASE_* flags
2227    that were affected.  */
2228 
2229 static unsigned int
2230 mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2231 	      bool enabled_p)
2232 {
2233   unsigned int mask;
2234 
2235   mask = mips_ase_mask (ase->flags);
2236   opts->ase &= ~mask;
2237 
2238   /* Clear combination ASE flags, which need to be recalculated based on
2239      updated regular ASE settings.  */
2240   opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
2241 
2242   if (enabled_p)
2243     opts->ase |= ase->flags;
2244 
2245   /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2246      instructions which are only valid when both ASEs are enabled.
2247      This sets the ASE_XPA_VIRT flag when both ASEs are present.  */
2248   if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2249     {
2250       opts->ase |= ASE_XPA_VIRT;
2251       mask |= ASE_XPA_VIRT;
2252     }
2253   if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2254     {
2255       opts->ase |= ASE_MIPS16E2_MT;
2256       mask |= ASE_MIPS16E2_MT;
2257     }
2258 
2259   /* The EVA Extension has instructions which are only valid when the R6 ISA
2260      is enabled.  This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2261      present.  */
2262   if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2263     {
2264       opts->ase |= ASE_EVA_R6;
2265       mask |= ASE_EVA_R6;
2266     }
2267 
2268   return mask;
2269 }
2270 
2271 /* Return the ASE called NAME, or null if none.  */
2272 
2273 static const struct mips_ase *
2274 mips_lookup_ase (const char *name)
2275 {
2276   unsigned int i;
2277 
2278   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2279     if (strcmp (name, mips_ases[i].name) == 0)
2280       return &mips_ases[i];
2281   return NULL;
2282 }
2283 
2284 /* Return the length of a microMIPS instruction in bytes.  If bits of
2285    the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2286    otherwise it is a 32-bit instruction.  */
2287 
2288 static inline unsigned int
2289 micromips_insn_length (const struct mips_opcode *mo)
2290 {
2291   return mips_opcode_32bit_p (mo) ? 4 : 2;
2292 }
2293 
2294 /* Return the length of MIPS16 instruction OPCODE.  */
2295 
2296 static inline unsigned int
2297 mips16_opcode_length (unsigned long opcode)
2298 {
2299   return (opcode >> 16) == 0 ? 2 : 4;
2300 }
2301 
2302 /* Return the length of instruction INSN.  */
2303 
2304 static inline unsigned int
2305 insn_length (const struct mips_cl_insn *insn)
2306 {
2307   if (mips_opts.micromips)
2308     return micromips_insn_length (insn->insn_mo);
2309   else if (mips_opts.mips16)
2310     return mips16_opcode_length (insn->insn_opcode);
2311   else
2312     return 4;
2313 }
2314 
2315 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
2316 
2317 static void
2318 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2319 {
2320   size_t i;
2321 
2322   insn->insn_mo = mo;
2323   insn->insn_opcode = mo->match;
2324   insn->frag = NULL;
2325   insn->where = 0;
2326   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2327     insn->fixp[i] = NULL;
2328   insn->fixed_p = (mips_opts.noreorder > 0);
2329   insn->noreorder_p = (mips_opts.noreorder > 0);
2330   insn->mips16_absolute_jump_p = 0;
2331   insn->complete_p = 0;
2332   insn->cleared_p = 0;
2333 }
2334 
2335 /* Get a list of all the operands in INSN.  */
2336 
2337 static const struct mips_operand_array *
2338 insn_operands (const struct mips_cl_insn *insn)
2339 {
2340   if (insn->insn_mo >= &mips_opcodes[0]
2341       && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2342     return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2343 
2344   if (insn->insn_mo >= &mips16_opcodes[0]
2345       && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2346     return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2347 
2348   if (insn->insn_mo >= &micromips_opcodes[0]
2349       && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2350     return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2351 
2352   abort ();
2353 }
2354 
2355 /* Get a description of operand OPNO of INSN.  */
2356 
2357 static const struct mips_operand *
2358 insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2359 {
2360   const struct mips_operand_array *operands;
2361 
2362   operands = insn_operands (insn);
2363   if (opno >= MAX_OPERANDS || !operands->operand[opno])
2364     abort ();
2365   return operands->operand[opno];
2366 }
2367 
2368 /* Install UVAL as the value of OPERAND in INSN.  */
2369 
2370 static inline void
2371 insn_insert_operand (struct mips_cl_insn *insn,
2372 		     const struct mips_operand *operand, unsigned int uval)
2373 {
2374   if (mips_opts.mips16
2375       && operand->type == OP_INT && operand->lsb == 0
2376       && mips_opcode_32bit_p (insn->insn_mo))
2377     insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2378   else
2379     insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2380 }
2381 
2382 /* Extract the value of OPERAND from INSN.  */
2383 
2384 static inline unsigned
2385 insn_extract_operand (const struct mips_cl_insn *insn,
2386 		      const struct mips_operand *operand)
2387 {
2388   return mips_extract_operand (operand, insn->insn_opcode);
2389 }
2390 
2391 /* Record the current MIPS16/microMIPS mode in now_seg.  */
2392 
2393 static void
2394 mips_record_compressed_mode (void)
2395 {
2396   segment_info_type *si;
2397 
2398   si = seg_info (now_seg);
2399   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2400     si->tc_segment_info_data.mips16 = mips_opts.mips16;
2401   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2402     si->tc_segment_info_data.micromips = mips_opts.micromips;
2403 }
2404 
2405 /* Read a standard MIPS instruction from BUF.  */
2406 
2407 static unsigned long
2408 read_insn (char *buf)
2409 {
2410   if (target_big_endian)
2411     return bfd_getb32 ((bfd_byte *) buf);
2412   else
2413     return bfd_getl32 ((bfd_byte *) buf);
2414 }
2415 
2416 /* Write standard MIPS instruction INSN to BUF.  Return a pointer to
2417    the next byte.  */
2418 
2419 static char *
2420 write_insn (char *buf, unsigned int insn)
2421 {
2422   md_number_to_chars (buf, insn, 4);
2423   return buf + 4;
2424 }
2425 
2426 /* Read a microMIPS or MIPS16 opcode from BUF, given that it
2427    has length LENGTH.  */
2428 
2429 static unsigned long
2430 read_compressed_insn (char *buf, unsigned int length)
2431 {
2432   unsigned long insn;
2433   unsigned int i;
2434 
2435   insn = 0;
2436   for (i = 0; i < length; i += 2)
2437     {
2438       insn <<= 16;
2439       if (target_big_endian)
2440 	insn |= bfd_getb16 ((char *) buf);
2441       else
2442 	insn |= bfd_getl16 ((char *) buf);
2443       buf += 2;
2444     }
2445   return insn;
2446 }
2447 
2448 /* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2449    instruction is LENGTH bytes long.  Return a pointer to the next byte.  */
2450 
2451 static char *
2452 write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2453 {
2454   unsigned int i;
2455 
2456   for (i = 0; i < length; i += 2)
2457     md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2458   return buf + length;
2459 }
2460 
2461 /* Install INSN at the location specified by its "frag" and "where" fields.  */
2462 
2463 static void
2464 install_insn (const struct mips_cl_insn *insn)
2465 {
2466   char *f = insn->frag->fr_literal + insn->where;
2467   if (HAVE_CODE_COMPRESSION)
2468     write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
2469   else
2470     write_insn (f, insn->insn_opcode);
2471   mips_record_compressed_mode ();
2472 }
2473 
2474 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
2475    and install the opcode in the new location.  */
2476 
2477 static void
2478 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2479 {
2480   size_t i;
2481 
2482   insn->frag = frag;
2483   insn->where = where;
2484   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2485     if (insn->fixp[i] != NULL)
2486       {
2487 	insn->fixp[i]->fx_frag = frag;
2488 	insn->fixp[i]->fx_where = where;
2489       }
2490   install_insn (insn);
2491 }
2492 
2493 /* Add INSN to the end of the output.  */
2494 
2495 static void
2496 add_fixed_insn (struct mips_cl_insn *insn)
2497 {
2498   char *f = frag_more (insn_length (insn));
2499   move_insn (insn, frag_now, f - frag_now->fr_literal);
2500 }
2501 
2502 /* Start a variant frag and move INSN to the start of the variant part,
2503    marking it as fixed.  The other arguments are as for frag_var.  */
2504 
2505 static void
2506 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2507 		  relax_substateT subtype, symbolS *symbol, offsetT offset)
2508 {
2509   frag_grow (max_chars);
2510   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2511   insn->fixed_p = 1;
2512   frag_var (rs_machine_dependent, max_chars, var,
2513 	    subtype, symbol, offset, NULL);
2514 }
2515 
2516 /* Insert N copies of INSN into the history buffer, starting at
2517    position FIRST.  Neither FIRST nor N need to be clipped.  */
2518 
2519 static void
2520 insert_into_history (unsigned int first, unsigned int n,
2521 		     const struct mips_cl_insn *insn)
2522 {
2523   if (mips_relax.sequence != 2)
2524     {
2525       unsigned int i;
2526 
2527       for (i = ARRAY_SIZE (history); i-- > first;)
2528 	if (i >= first + n)
2529 	  history[i] = history[i - n];
2530 	else
2531 	  history[i] = *insn;
2532     }
2533 }
2534 
2535 /* Clear the error in insn_error.  */
2536 
2537 static void
2538 clear_insn_error (void)
2539 {
2540   memset (&insn_error, 0, sizeof (insn_error));
2541 }
2542 
2543 /* Possibly record error message MSG for the current instruction.
2544    If the error is about a particular argument, ARGNUM is the 1-based
2545    number of that argument, otherwise it is 0.  FORMAT is the format
2546    of MSG.  Return true if MSG was used, false if the current message
2547    was kept.  */
2548 
2549 static bool
2550 set_insn_error_format (int argnum, enum mips_insn_error_format format,
2551 		       const char *msg)
2552 {
2553   if (argnum == 0)
2554     {
2555       /* Give priority to errors against specific arguments, and to
2556 	 the first whole-instruction message.  */
2557       if (insn_error.msg)
2558 	return false;
2559     }
2560   else
2561     {
2562       /* Keep insn_error if it is against a later argument.  */
2563       if (argnum < insn_error.min_argnum)
2564 	return false;
2565 
2566       /* If both errors are against the same argument but are different,
2567 	 give up on reporting a specific error for this argument.
2568 	 See the comment about mips_insn_error for details.  */
2569       if (argnum == insn_error.min_argnum
2570 	  && insn_error.msg
2571 	  && strcmp (insn_error.msg, msg) != 0)
2572 	{
2573 	  insn_error.msg = 0;
2574 	  insn_error.min_argnum += 1;
2575 	  return false;
2576 	}
2577     }
2578   insn_error.min_argnum = argnum;
2579   insn_error.format = format;
2580   insn_error.msg = msg;
2581   return true;
2582 }
2583 
2584 /* Record an instruction error with no % format fields.  ARGNUM and MSG are
2585    as for set_insn_error_format.  */
2586 
2587 static void
2588 set_insn_error (int argnum, const char *msg)
2589 {
2590   set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2591 }
2592 
2593 /* Record an instruction error with one %d field I.  ARGNUM and MSG are
2594    as for set_insn_error_format.  */
2595 
2596 static void
2597 set_insn_error_i (int argnum, const char *msg, int i)
2598 {
2599   if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2600     insn_error.u.i = i;
2601 }
2602 
2603 /* Record an instruction error with two %s fields S1 and S2.  ARGNUM and MSG
2604    are as for set_insn_error_format.  */
2605 
2606 static void
2607 set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2608 {
2609   if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2610     {
2611       insn_error.u.ss[0] = s1;
2612       insn_error.u.ss[1] = s2;
2613     }
2614 }
2615 
2616 /* Report the error in insn_error, which is against assembly code STR.  */
2617 
2618 static void
2619 report_insn_error (const char *str)
2620 {
2621   const char *msg = concat (insn_error.msg, " `%s'", NULL);
2622 
2623   switch (insn_error.format)
2624     {
2625     case ERR_FMT_PLAIN:
2626       as_bad (msg, str);
2627       break;
2628 
2629     case ERR_FMT_I:
2630       as_bad (msg, insn_error.u.i, str);
2631       break;
2632 
2633     case ERR_FMT_SS:
2634       as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2635       break;
2636     }
2637 
2638   free ((char *) msg);
2639 }
2640 
2641 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
2642    the idea is to make it obvious at a glance that each errata is
2643    included.  */
2644 
2645 static void
2646 init_vr4120_conflicts (void)
2647 {
2648 #define CONFLICT(FIRST, SECOND) \
2649     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2650 
2651   /* Errata 21 - [D]DIV[U] after [D]MACC */
2652   CONFLICT (MACC, DIV);
2653   CONFLICT (DMACC, DIV);
2654 
2655   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
2656   CONFLICT (DMULT, DMULT);
2657   CONFLICT (DMULT, DMACC);
2658   CONFLICT (DMACC, DMULT);
2659   CONFLICT (DMACC, DMACC);
2660 
2661   /* Errata 24 - MT{LO,HI} after [D]MACC */
2662   CONFLICT (MACC, MTHILO);
2663   CONFLICT (DMACC, MTHILO);
2664 
2665   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2666      instruction is executed immediately after a MACC or DMACC
2667      instruction, the result of [either instruction] is incorrect."  */
2668   CONFLICT (MACC, MULT);
2669   CONFLICT (MACC, DMULT);
2670   CONFLICT (DMACC, MULT);
2671   CONFLICT (DMACC, DMULT);
2672 
2673   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2674      executed immediately after a DMULT, DMULTU, DIV, DIVU,
2675      DDIV or DDIVU instruction, the result of the MACC or
2676      DMACC instruction is incorrect.".  */
2677   CONFLICT (DMULT, MACC);
2678   CONFLICT (DMULT, DMACC);
2679   CONFLICT (DIV, MACC);
2680   CONFLICT (DIV, DMACC);
2681 
2682 #undef CONFLICT
2683 }
2684 
2685 struct regname {
2686   const char *name;
2687   unsigned int num;
2688 };
2689 
2690 #define RNUM_MASK	0x00000ff
2691 #define RTYPE_MASK	0x0ffff00
2692 #define RTYPE_NUM	0x0000100
2693 #define RTYPE_FPU	0x0000200
2694 #define RTYPE_FCC	0x0000400
2695 #define RTYPE_VEC	0x0000800
2696 #define RTYPE_GP	0x0001000
2697 #define RTYPE_CP0	0x0002000
2698 #define RTYPE_PC	0x0004000
2699 #define RTYPE_ACC	0x0008000
2700 #define RTYPE_CCC	0x0010000
2701 #define RTYPE_VI	0x0020000
2702 #define RTYPE_VF	0x0040000
2703 #define RTYPE_R5900_I	0x0080000
2704 #define RTYPE_R5900_Q	0x0100000
2705 #define RTYPE_R5900_R	0x0200000
2706 #define RTYPE_R5900_ACC	0x0400000
2707 #define RTYPE_MSA	0x0800000
2708 #define RWARN		0x8000000
2709 
2710 #define GENERIC_REGISTER_NUMBERS \
2711     {"$0",	RTYPE_NUM | 0},  \
2712     {"$1",	RTYPE_NUM | 1},  \
2713     {"$2",	RTYPE_NUM | 2},  \
2714     {"$3",	RTYPE_NUM | 3},  \
2715     {"$4",	RTYPE_NUM | 4},  \
2716     {"$5",	RTYPE_NUM | 5},  \
2717     {"$6",	RTYPE_NUM | 6},  \
2718     {"$7",	RTYPE_NUM | 7},  \
2719     {"$8",	RTYPE_NUM | 8},  \
2720     {"$9",	RTYPE_NUM | 9},  \
2721     {"$10",	RTYPE_NUM | 10}, \
2722     {"$11",	RTYPE_NUM | 11}, \
2723     {"$12",	RTYPE_NUM | 12}, \
2724     {"$13",	RTYPE_NUM | 13}, \
2725     {"$14",	RTYPE_NUM | 14}, \
2726     {"$15",	RTYPE_NUM | 15}, \
2727     {"$16",	RTYPE_NUM | 16}, \
2728     {"$17",	RTYPE_NUM | 17}, \
2729     {"$18",	RTYPE_NUM | 18}, \
2730     {"$19",	RTYPE_NUM | 19}, \
2731     {"$20",	RTYPE_NUM | 20}, \
2732     {"$21",	RTYPE_NUM | 21}, \
2733     {"$22",	RTYPE_NUM | 22}, \
2734     {"$23",	RTYPE_NUM | 23}, \
2735     {"$24",	RTYPE_NUM | 24}, \
2736     {"$25",	RTYPE_NUM | 25}, \
2737     {"$26",	RTYPE_NUM | 26}, \
2738     {"$27",	RTYPE_NUM | 27}, \
2739     {"$28",	RTYPE_NUM | 28}, \
2740     {"$29",	RTYPE_NUM | 29}, \
2741     {"$30",	RTYPE_NUM | 30}, \
2742     {"$31",	RTYPE_NUM | 31}
2743 
2744 #define FPU_REGISTER_NAMES       \
2745     {"$f0",	RTYPE_FPU | 0},  \
2746     {"$f1",	RTYPE_FPU | 1},  \
2747     {"$f2",	RTYPE_FPU | 2},  \
2748     {"$f3",	RTYPE_FPU | 3},  \
2749     {"$f4",	RTYPE_FPU | 4},  \
2750     {"$f5",	RTYPE_FPU | 5},  \
2751     {"$f6",	RTYPE_FPU | 6},  \
2752     {"$f7",	RTYPE_FPU | 7},  \
2753     {"$f8",	RTYPE_FPU | 8},  \
2754     {"$f9",	RTYPE_FPU | 9},  \
2755     {"$f10",	RTYPE_FPU | 10}, \
2756     {"$f11",	RTYPE_FPU | 11}, \
2757     {"$f12",	RTYPE_FPU | 12}, \
2758     {"$f13",	RTYPE_FPU | 13}, \
2759     {"$f14",	RTYPE_FPU | 14}, \
2760     {"$f15",	RTYPE_FPU | 15}, \
2761     {"$f16",	RTYPE_FPU | 16}, \
2762     {"$f17",	RTYPE_FPU | 17}, \
2763     {"$f18",	RTYPE_FPU | 18}, \
2764     {"$f19",	RTYPE_FPU | 19}, \
2765     {"$f20",	RTYPE_FPU | 20}, \
2766     {"$f21",	RTYPE_FPU | 21}, \
2767     {"$f22",	RTYPE_FPU | 22}, \
2768     {"$f23",	RTYPE_FPU | 23}, \
2769     {"$f24",	RTYPE_FPU | 24}, \
2770     {"$f25",	RTYPE_FPU | 25}, \
2771     {"$f26",	RTYPE_FPU | 26}, \
2772     {"$f27",	RTYPE_FPU | 27}, \
2773     {"$f28",	RTYPE_FPU | 28}, \
2774     {"$f29",	RTYPE_FPU | 29}, \
2775     {"$f30",	RTYPE_FPU | 30}, \
2776     {"$f31",	RTYPE_FPU | 31}
2777 
2778 #define FPU_CONDITION_CODE_NAMES \
2779     {"$fcc0",	RTYPE_FCC | 0},  \
2780     {"$fcc1",	RTYPE_FCC | 1},  \
2781     {"$fcc2",	RTYPE_FCC | 2},  \
2782     {"$fcc3",	RTYPE_FCC | 3},  \
2783     {"$fcc4",	RTYPE_FCC | 4},  \
2784     {"$fcc5",	RTYPE_FCC | 5},  \
2785     {"$fcc6",	RTYPE_FCC | 6},  \
2786     {"$fcc7",	RTYPE_FCC | 7}
2787 
2788 #define COPROC_CONDITION_CODE_NAMES         \
2789     {"$cc0",	RTYPE_FCC | RTYPE_CCC | 0}, \
2790     {"$cc1",	RTYPE_FCC | RTYPE_CCC | 1}, \
2791     {"$cc2",	RTYPE_FCC | RTYPE_CCC | 2}, \
2792     {"$cc3",	RTYPE_FCC | RTYPE_CCC | 3}, \
2793     {"$cc4",	RTYPE_FCC | RTYPE_CCC | 4}, \
2794     {"$cc5",	RTYPE_FCC | RTYPE_CCC | 5}, \
2795     {"$cc6",	RTYPE_FCC | RTYPE_CCC | 6}, \
2796     {"$cc7",	RTYPE_FCC | RTYPE_CCC | 7}
2797 
2798 #define N32N64_SYMBOLIC_REGISTER_NAMES \
2799     {"$a4",	RTYPE_GP | 8},  \
2800     {"$a5",	RTYPE_GP | 9},  \
2801     {"$a6",	RTYPE_GP | 10}, \
2802     {"$a7",	RTYPE_GP | 11}, \
2803     {"$ta0",	RTYPE_GP | 8},  /* alias for $a4 */ \
2804     {"$ta1",	RTYPE_GP | 9},  /* alias for $a5 */ \
2805     {"$ta2",	RTYPE_GP | 10}, /* alias for $a6 */ \
2806     {"$ta3",	RTYPE_GP | 11}, /* alias for $a7 */ \
2807     {"$t0",	RTYPE_GP | 12}, \
2808     {"$t1",	RTYPE_GP | 13}, \
2809     {"$t2",	RTYPE_GP | 14}, \
2810     {"$t3",	RTYPE_GP | 15}
2811 
2812 #define O32_SYMBOLIC_REGISTER_NAMES \
2813     {"$t0",	RTYPE_GP | 8},  \
2814     {"$t1",	RTYPE_GP | 9},  \
2815     {"$t2",	RTYPE_GP | 10}, \
2816     {"$t3",	RTYPE_GP | 11}, \
2817     {"$t4",	RTYPE_GP | 12}, \
2818     {"$t5",	RTYPE_GP | 13}, \
2819     {"$t6",	RTYPE_GP | 14}, \
2820     {"$t7",	RTYPE_GP | 15}, \
2821     {"$ta0",	RTYPE_GP | 12}, /* alias for $t4 */ \
2822     {"$ta1",	RTYPE_GP | 13}, /* alias for $t5 */ \
2823     {"$ta2",	RTYPE_GP | 14}, /* alias for $t6 */ \
2824     {"$ta3",	RTYPE_GP | 15}  /* alias for $t7 */
2825 
2826 /* Remaining symbolic register names.  */
2827 #define SYMBOLIC_REGISTER_NAMES \
2828     {"$zero",	RTYPE_GP | 0},  \
2829     {"$at",	RTYPE_GP | 1},  \
2830     {"$AT",	RTYPE_GP | 1},  \
2831     {"$v0",	RTYPE_GP | 2},  \
2832     {"$v1",	RTYPE_GP | 3},  \
2833     {"$a0",	RTYPE_GP | 4},  \
2834     {"$a1",	RTYPE_GP | 5},  \
2835     {"$a2",	RTYPE_GP | 6},  \
2836     {"$a3",	RTYPE_GP | 7},  \
2837     {"$s0",	RTYPE_GP | 16}, \
2838     {"$s1",	RTYPE_GP | 17}, \
2839     {"$s2",	RTYPE_GP | 18}, \
2840     {"$s3",	RTYPE_GP | 19}, \
2841     {"$s4",	RTYPE_GP | 20}, \
2842     {"$s5",	RTYPE_GP | 21}, \
2843     {"$s6",	RTYPE_GP | 22}, \
2844     {"$s7",	RTYPE_GP | 23}, \
2845     {"$t8",	RTYPE_GP | 24}, \
2846     {"$t9",	RTYPE_GP | 25}, \
2847     {"$k0",	RTYPE_GP | 26}, \
2848     {"$kt0",	RTYPE_GP | 26}, \
2849     {"$k1",	RTYPE_GP | 27}, \
2850     {"$kt1",	RTYPE_GP | 27}, \
2851     {"$gp",	RTYPE_GP | 28}, \
2852     {"$sp",	RTYPE_GP | 29}, \
2853     {"$s8",	RTYPE_GP | 30}, \
2854     {"$fp",	RTYPE_GP | 30}, \
2855     {"$ra",	RTYPE_GP | 31}
2856 
2857 #define MIPS16_SPECIAL_REGISTER_NAMES \
2858     {"$pc",	RTYPE_PC | 0}
2859 
2860 #define MDMX_VECTOR_REGISTER_NAMES \
2861     /* {"$v0",	RTYPE_VEC | 0},  Clash with REG 2 above.  */ \
2862     /* {"$v1",	RTYPE_VEC | 1},  Clash with REG 3 above.  */ \
2863     {"$v2",	RTYPE_VEC | 2},  \
2864     {"$v3",	RTYPE_VEC | 3},  \
2865     {"$v4",	RTYPE_VEC | 4},  \
2866     {"$v5",	RTYPE_VEC | 5},  \
2867     {"$v6",	RTYPE_VEC | 6},  \
2868     {"$v7",	RTYPE_VEC | 7},  \
2869     {"$v8",	RTYPE_VEC | 8},  \
2870     {"$v9",	RTYPE_VEC | 9},  \
2871     {"$v10",	RTYPE_VEC | 10}, \
2872     {"$v11",	RTYPE_VEC | 11}, \
2873     {"$v12",	RTYPE_VEC | 12}, \
2874     {"$v13",	RTYPE_VEC | 13}, \
2875     {"$v14",	RTYPE_VEC | 14}, \
2876     {"$v15",	RTYPE_VEC | 15}, \
2877     {"$v16",	RTYPE_VEC | 16}, \
2878     {"$v17",	RTYPE_VEC | 17}, \
2879     {"$v18",	RTYPE_VEC | 18}, \
2880     {"$v19",	RTYPE_VEC | 19}, \
2881     {"$v20",	RTYPE_VEC | 20}, \
2882     {"$v21",	RTYPE_VEC | 21}, \
2883     {"$v22",	RTYPE_VEC | 22}, \
2884     {"$v23",	RTYPE_VEC | 23}, \
2885     {"$v24",	RTYPE_VEC | 24}, \
2886     {"$v25",	RTYPE_VEC | 25}, \
2887     {"$v26",	RTYPE_VEC | 26}, \
2888     {"$v27",	RTYPE_VEC | 27}, \
2889     {"$v28",	RTYPE_VEC | 28}, \
2890     {"$v29",	RTYPE_VEC | 29}, \
2891     {"$v30",	RTYPE_VEC | 30}, \
2892     {"$v31",	RTYPE_VEC | 31}
2893 
2894 #define R5900_I_NAMES \
2895     {"$I",	RTYPE_R5900_I | 0}
2896 
2897 #define R5900_Q_NAMES \
2898     {"$Q",	RTYPE_R5900_Q | 0}
2899 
2900 #define R5900_R_NAMES \
2901     {"$R",	RTYPE_R5900_R | 0}
2902 
2903 #define R5900_ACC_NAMES \
2904     {"$ACC",	RTYPE_R5900_ACC | 0 }
2905 
2906 #define MIPS_DSP_ACCUMULATOR_NAMES \
2907     {"$ac0",	RTYPE_ACC | 0}, \
2908     {"$ac1",	RTYPE_ACC | 1}, \
2909     {"$ac2",	RTYPE_ACC | 2}, \
2910     {"$ac3",	RTYPE_ACC | 3}
2911 
2912 static const struct regname reg_names[] = {
2913   GENERIC_REGISTER_NUMBERS,
2914   FPU_REGISTER_NAMES,
2915   FPU_CONDITION_CODE_NAMES,
2916   COPROC_CONDITION_CODE_NAMES,
2917 
2918   /* The $txx registers depends on the abi,
2919      these will be added later into the symbol table from
2920      one of the tables below once mips_abi is set after
2921      parsing of arguments from the command line. */
2922   SYMBOLIC_REGISTER_NAMES,
2923 
2924   MIPS16_SPECIAL_REGISTER_NAMES,
2925   MDMX_VECTOR_REGISTER_NAMES,
2926   R5900_I_NAMES,
2927   R5900_Q_NAMES,
2928   R5900_R_NAMES,
2929   R5900_ACC_NAMES,
2930   MIPS_DSP_ACCUMULATOR_NAMES,
2931   {0, 0}
2932 };
2933 
2934 static const struct regname reg_names_o32[] = {
2935   O32_SYMBOLIC_REGISTER_NAMES,
2936   {0, 0}
2937 };
2938 
2939 static const struct regname reg_names_n32n64[] = {
2940   N32N64_SYMBOLIC_REGISTER_NAMES,
2941   {0, 0}
2942 };
2943 
2944 /* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2945    interpreted as vector registers 0 and 1.  If SYMVAL is the value of one
2946    of these register symbols, return the associated vector register,
2947    otherwise return SYMVAL itself.  */
2948 
2949 static unsigned int
2950 mips_prefer_vec_regno (unsigned int symval)
2951 {
2952   if ((symval & -2) == (RTYPE_GP | 2))
2953     return RTYPE_VEC | (symval & 1);
2954   return symval;
2955 }
2956 
2957 /* Return true if string [S, E) is a valid register name, storing its
2958    symbol value in *SYMVAL_PTR if so.  */
2959 
2960 static bool
2961 mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
2962 {
2963   char save_c;
2964   symbolS *symbol;
2965 
2966   /* Terminate name.  */
2967   save_c = *e;
2968   *e = '\0';
2969 
2970   /* Look up the name.  */
2971   symbol = symbol_find (s);
2972   *e = save_c;
2973 
2974   if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2975     return false;
2976 
2977   *symval_ptr = S_GET_VALUE (symbol);
2978   return true;
2979 }
2980 
2981 /* Return true if the string at *SPTR is a valid register name.  Allow it
2982    to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2983    is nonnull.
2984 
2985    When returning true, move *SPTR past the register, store the
2986    register's symbol value in *SYMVAL_PTR and the channel mask in
2987    *CHANNELS_PTR (if nonnull).  The symbol value includes the register
2988    number (RNUM_MASK) and register type (RTYPE_MASK).  The channel mask
2989    is a 4-bit value of the form XYZW and is 0 if no suffix was given.  */
2990 
2991 static bool
2992 mips_parse_register (char **sptr, unsigned int *symval_ptr,
2993 		     unsigned int *channels_ptr)
2994 {
2995   char *s, *e, *m;
2996   const char *q;
2997   unsigned int channels, symval, bit;
2998 
2999   /* Find end of name.  */
3000   s = e = *sptr;
3001   if (is_name_beginner (*e))
3002     ++e;
3003   while (is_part_of_name (*e))
3004     ++e;
3005 
3006   channels = 0;
3007   if (!mips_parse_register_1 (s, e, &symval))
3008     {
3009       if (!channels_ptr)
3010 	return false;
3011 
3012       /* Eat characters from the end of the string that are valid
3013 	 channel suffixes.  The preceding register must be $ACC or
3014 	 end with a digit, so there is no ambiguity.  */
3015       bit = 1;
3016       m = e;
3017       for (q = "wzyx"; *q; q++, bit <<= 1)
3018 	if (m > s && m[-1] == *q)
3019 	  {
3020 	    --m;
3021 	    channels |= bit;
3022 	  }
3023 
3024       if (channels == 0
3025 	  || !mips_parse_register_1 (s, m, &symval)
3026 	  || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
3027 	return false;
3028     }
3029 
3030   *sptr = e;
3031   *symval_ptr = symval;
3032   if (channels_ptr)
3033     *channels_ptr = channels;
3034   return true;
3035 }
3036 
3037 /* Check if SPTR points at a valid register specifier according to TYPES.
3038    If so, then return 1, advance S to consume the specifier and store
3039    the register's number in REGNOP, otherwise return 0.  */
3040 
3041 static int
3042 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3043 {
3044   unsigned int regno;
3045 
3046   if (mips_parse_register (s, &regno, NULL))
3047     {
3048       if (types & RTYPE_VEC)
3049 	regno = mips_prefer_vec_regno (regno);
3050       if (regno & types)
3051 	regno &= RNUM_MASK;
3052       else
3053 	regno = ~0;
3054     }
3055   else
3056     {
3057       if (types & RWARN)
3058 	as_warn (_("unrecognized register name `%s'"), *s);
3059       regno = ~0;
3060     }
3061   if (regnop)
3062     *regnop = regno;
3063   return regno <= RNUM_MASK;
3064 }
3065 
3066 /* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3067    mask in *CHANNELS.  Return a pointer to the first unconsumed character.  */
3068 
3069 static char *
3070 mips_parse_vu0_channels (char *s, unsigned int *channels)
3071 {
3072   unsigned int i;
3073 
3074   *channels = 0;
3075   for (i = 0; i < 4; i++)
3076     if (*s == "xyzw"[i])
3077       {
3078 	*channels |= 1 << (3 - i);
3079 	++s;
3080       }
3081   return s;
3082 }
3083 
3084 /* Token types for parsed operand lists.  */
3085 enum mips_operand_token_type {
3086   /* A plain register, e.g. $f2.  */
3087   OT_REG,
3088 
3089   /* A 4-bit XYZW channel mask.  */
3090   OT_CHANNELS,
3091 
3092   /* A constant vector index, e.g. [1].  */
3093   OT_INTEGER_INDEX,
3094 
3095   /* A register vector index, e.g. [$2].  */
3096   OT_REG_INDEX,
3097 
3098   /* A continuous range of registers, e.g. $s0-$s4.  */
3099   OT_REG_RANGE,
3100 
3101   /* A (possibly relocated) expression.  */
3102   OT_INTEGER,
3103 
3104   /* A floating-point value.  */
3105   OT_FLOAT,
3106 
3107   /* A single character.  This can be '(', ')' or ',', but '(' only appears
3108      before OT_REGs.  */
3109   OT_CHAR,
3110 
3111   /* A doubled character, either "--" or "++".  */
3112   OT_DOUBLE_CHAR,
3113 
3114   /* The end of the operand list.  */
3115   OT_END
3116 };
3117 
3118 /* A parsed operand token.  */
3119 struct mips_operand_token
3120 {
3121   /* The type of token.  */
3122   enum mips_operand_token_type type;
3123   union
3124   {
3125     /* The register symbol value for an OT_REG or OT_REG_INDEX.  */
3126     unsigned int regno;
3127 
3128     /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX.  */
3129     unsigned int channels;
3130 
3131     /* The integer value of an OT_INTEGER_INDEX.  */
3132     addressT index;
3133 
3134     /* The two register symbol values involved in an OT_REG_RANGE.  */
3135     struct {
3136       unsigned int regno1;
3137       unsigned int regno2;
3138     } reg_range;
3139 
3140     /* The value of an OT_INTEGER.  The value is represented as an
3141        expression and the relocation operators that were applied to
3142        that expression.  The reloc entries are BFD_RELOC_UNUSED if no
3143        relocation operators were used.  */
3144     struct {
3145       expressionS value;
3146       bfd_reloc_code_real_type relocs[3];
3147     } integer;
3148 
3149     /* The binary data for an OT_FLOAT constant, and the number of bytes
3150        in the constant.  */
3151     struct {
3152       unsigned char data[8];
3153       int length;
3154     } flt;
3155 
3156     /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR.  */
3157     char ch;
3158   } u;
3159 };
3160 
3161 /* An obstack used to construct lists of mips_operand_tokens.  */
3162 static struct obstack mips_operand_tokens;
3163 
3164 /* Give TOKEN type TYPE and add it to mips_operand_tokens.  */
3165 
3166 static void
3167 mips_add_token (struct mips_operand_token *token,
3168 		enum mips_operand_token_type type)
3169 {
3170   token->type = type;
3171   obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3172 }
3173 
3174 /* Check whether S is '(' followed by a register name.  Add OT_CHAR
3175    and OT_REG tokens for them if so, and return a pointer to the first
3176    unconsumed character.  Return null otherwise.  */
3177 
3178 static char *
3179 mips_parse_base_start (char *s)
3180 {
3181   struct mips_operand_token token;
3182   unsigned int regno, channels;
3183   bool decrement_p;
3184 
3185   if (*s != '(')
3186     return 0;
3187 
3188   ++s;
3189   SKIP_SPACE_TABS (s);
3190 
3191   /* Only match "--" as part of a base expression.  In other contexts "--X"
3192      is a double negative.  */
3193   decrement_p = (s[0] == '-' && s[1] == '-');
3194   if (decrement_p)
3195     {
3196       s += 2;
3197       SKIP_SPACE_TABS (s);
3198     }
3199 
3200   /* Allow a channel specifier because that leads to better error messages
3201      than treating something like "$vf0x++" as an expression.  */
3202   if (!mips_parse_register (&s, &regno, &channels))
3203     return 0;
3204 
3205   token.u.ch = '(';
3206   mips_add_token (&token, OT_CHAR);
3207 
3208   if (decrement_p)
3209     {
3210       token.u.ch = '-';
3211       mips_add_token (&token, OT_DOUBLE_CHAR);
3212     }
3213 
3214   token.u.regno = regno;
3215   mips_add_token (&token, OT_REG);
3216 
3217   if (channels)
3218     {
3219       token.u.channels = channels;
3220       mips_add_token (&token, OT_CHANNELS);
3221     }
3222 
3223   /* For consistency, only match "++" as part of base expressions too.  */
3224   SKIP_SPACE_TABS (s);
3225   if (s[0] == '+' && s[1] == '+')
3226     {
3227       s += 2;
3228       token.u.ch = '+';
3229       mips_add_token (&token, OT_DOUBLE_CHAR);
3230     }
3231 
3232   return s;
3233 }
3234 
3235 /* Parse one or more tokens from S.  Return a pointer to the first
3236    unconsumed character on success.  Return null if an error was found
3237    and store the error text in insn_error.  FLOAT_FORMAT is as for
3238    mips_parse_arguments.  */
3239 
3240 static char *
3241 mips_parse_argument_token (char *s, char float_format)
3242 {
3243   char *end, *save_in;
3244   const char *err;
3245   unsigned int regno1, regno2, channels;
3246   struct mips_operand_token token;
3247 
3248   /* First look for "($reg", since we want to treat that as an
3249      OT_CHAR and OT_REG rather than an expression.  */
3250   end = mips_parse_base_start (s);
3251   if (end)
3252     return end;
3253 
3254   /* Handle other characters that end up as OT_CHARs.  */
3255   if (*s == ')' || *s == ',')
3256     {
3257       token.u.ch = *s;
3258       mips_add_token (&token, OT_CHAR);
3259       ++s;
3260       return s;
3261     }
3262 
3263   /* Handle tokens that start with a register.  */
3264   if (mips_parse_register (&s, &regno1, &channels))
3265     {
3266       if (channels)
3267 	{
3268 	  /* A register and a VU0 channel suffix.  */
3269 	  token.u.regno = regno1;
3270 	  mips_add_token (&token, OT_REG);
3271 
3272 	  token.u.channels = channels;
3273 	  mips_add_token (&token, OT_CHANNELS);
3274 	  return s;
3275 	}
3276 
3277       SKIP_SPACE_TABS (s);
3278       if (*s == '-')
3279 	{
3280 	  /* A register range.  */
3281 	  ++s;
3282 	  SKIP_SPACE_TABS (s);
3283 	  if (!mips_parse_register (&s, &regno2, NULL))
3284 	    {
3285 	      set_insn_error (0, _("invalid register range"));
3286 	      return 0;
3287 	    }
3288 
3289 	  token.u.reg_range.regno1 = regno1;
3290 	  token.u.reg_range.regno2 = regno2;
3291 	  mips_add_token (&token, OT_REG_RANGE);
3292 	  return s;
3293 	}
3294 
3295       /* Add the register itself.  */
3296       token.u.regno = regno1;
3297       mips_add_token (&token, OT_REG);
3298 
3299       /* Check for a vector index.  */
3300       if (*s == '[')
3301 	{
3302 	  ++s;
3303 	  SKIP_SPACE_TABS (s);
3304 	  if (mips_parse_register (&s, &token.u.regno, NULL))
3305 	    mips_add_token (&token, OT_REG_INDEX);
3306 	  else
3307 	    {
3308 	      expressionS element;
3309 
3310 	      my_getExpression (&element, s);
3311 	      if (element.X_op != O_constant)
3312 		{
3313 		  set_insn_error (0, _("vector element must be constant"));
3314 		  return 0;
3315 		}
3316 	      s = expr_end;
3317 	      token.u.index = element.X_add_number;
3318 	      mips_add_token (&token, OT_INTEGER_INDEX);
3319 	    }
3320 	  SKIP_SPACE_TABS (s);
3321 	  if (*s != ']')
3322 	    {
3323 	      set_insn_error (0, _("missing `]'"));
3324 	      return 0;
3325 	    }
3326 	  ++s;
3327 	}
3328       return s;
3329     }
3330 
3331   if (float_format)
3332     {
3333       /* First try to treat expressions as floats.  */
3334       save_in = input_line_pointer;
3335       input_line_pointer = s;
3336       err = md_atof (float_format, (char *) token.u.flt.data,
3337 		     &token.u.flt.length);
3338       end = input_line_pointer;
3339       input_line_pointer = save_in;
3340       if (err && *err)
3341 	{
3342 	  set_insn_error (0, err);
3343 	  return 0;
3344 	}
3345       if (s != end)
3346 	{
3347 	  mips_add_token (&token, OT_FLOAT);
3348 	  return end;
3349 	}
3350     }
3351 
3352   /* Treat everything else as an integer expression.  */
3353   token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3354   token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3355   token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3356   my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3357   s = expr_end;
3358   mips_add_token (&token, OT_INTEGER);
3359   return s;
3360 }
3361 
3362 /* S points to the operand list for an instruction.  FLOAT_FORMAT is 'f'
3363    if expressions should be treated as 32-bit floating-point constants,
3364    'd' if they should be treated as 64-bit floating-point constants,
3365    or 0 if they should be treated as integer expressions (the usual case).
3366 
3367    Return a list of tokens on success, otherwise return 0.  The caller
3368    must obstack_free the list after use.  */
3369 
3370 static struct mips_operand_token *
3371 mips_parse_arguments (char *s, char float_format)
3372 {
3373   struct mips_operand_token token;
3374 
3375   SKIP_SPACE_TABS (s);
3376   while (*s)
3377     {
3378       s = mips_parse_argument_token (s, float_format);
3379       if (!s)
3380 	{
3381 	  obstack_free (&mips_operand_tokens,
3382 			obstack_finish (&mips_operand_tokens));
3383 	  return 0;
3384 	}
3385       SKIP_SPACE_TABS (s);
3386     }
3387   mips_add_token (&token, OT_END);
3388   return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
3389 }
3390 
3391 /* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3392    and architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
3393 
3394 static bool
3395 is_opcode_valid (const struct mips_opcode *mo)
3396 {
3397   int isa = mips_opts.isa;
3398   int ase = mips_opts.ase;
3399   int fp_s, fp_d;
3400   unsigned int i;
3401 
3402   if (ISA_HAS_64BIT_REGS (isa))
3403     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3404       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3405 	ase |= mips_ases[i].flags64;
3406 
3407   if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
3408     return false;
3409 
3410   /* Check whether the instruction or macro requires single-precision or
3411      double-precision floating-point support.  Note that this information is
3412      stored differently in the opcode table for insns and macros.  */
3413   if (mo->pinfo == INSN_MACRO)
3414     {
3415       fp_s = mo->pinfo2 & INSN2_M_FP_S;
3416       fp_d = mo->pinfo2 & INSN2_M_FP_D;
3417     }
3418   else
3419     {
3420       fp_s = mo->pinfo & FP_S;
3421       fp_d = mo->pinfo & FP_D;
3422     }
3423 
3424   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3425     return false;
3426 
3427   if (fp_s && mips_opts.soft_float)
3428     return false;
3429 
3430   return true;
3431 }
3432 
3433 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
3434    selected ISA and architecture.  */
3435 
3436 static bool
3437 is_opcode_valid_16 (const struct mips_opcode *mo)
3438 {
3439   int isa = mips_opts.isa;
3440   int ase = mips_opts.ase;
3441   unsigned int i;
3442 
3443   if (ISA_HAS_64BIT_REGS (isa))
3444     for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3445       if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3446 	ase |= mips_ases[i].flags64;
3447 
3448   return opcode_is_member (mo, isa, ase, mips_opts.arch);
3449 }
3450 
3451 /* Return TRUE if the size of the microMIPS opcode MO matches one
3452    explicitly requested.  Always TRUE in the standard MIPS mode.
3453    Use is_size_valid_16 for MIPS16 opcodes.  */
3454 
3455 static bool
3456 is_size_valid (const struct mips_opcode *mo)
3457 {
3458   if (!mips_opts.micromips)
3459     return true;
3460 
3461   if (mips_opts.insn32)
3462     {
3463       if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3464 	return false;
3465       if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3466 	return false;
3467     }
3468   if (!forced_insn_length)
3469     return true;
3470   if (mo->pinfo == INSN_MACRO)
3471     return false;
3472   return forced_insn_length == micromips_insn_length (mo);
3473 }
3474 
3475 /* Return TRUE if the size of the MIPS16 opcode MO matches one
3476    explicitly requested.  */
3477 
3478 static bool
3479 is_size_valid_16 (const struct mips_opcode *mo)
3480 {
3481   if (!forced_insn_length)
3482     return true;
3483   if (mo->pinfo == INSN_MACRO)
3484     return false;
3485   if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3486     return false;
3487   if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3488     return false;
3489   return true;
3490 }
3491 
3492 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
3493    of the preceding instruction.  Always TRUE in the standard MIPS mode.
3494 
3495    We don't accept macros in 16-bit delay slots to avoid a case where
3496    a macro expansion fails because it relies on a preceding 32-bit real
3497    instruction to have matched and does not handle the operands correctly.
3498    The only macros that may expand to 16-bit instructions are JAL that
3499    cannot be placed in a delay slot anyway, and corner cases of BALIGN
3500    and BGT (that likewise cannot be placed in a delay slot) that decay to
3501    a NOP.  In all these cases the macros precede any corresponding real
3502    instruction definitions in the opcode table, so they will match in the
3503    second pass where the size of the delay slot is ignored and therefore
3504    produce correct code.  */
3505 
3506 static bool
3507 is_delay_slot_valid (const struct mips_opcode *mo)
3508 {
3509   if (!mips_opts.micromips)
3510     return true;
3511 
3512   if (mo->pinfo == INSN_MACRO)
3513     return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
3514   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3515       && micromips_insn_length (mo) != 4)
3516     return false;
3517   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3518       && micromips_insn_length (mo) != 2)
3519     return false;
3520 
3521   return true;
3522 }
3523 
3524 /* For consistency checking, verify that all bits of OPCODE are specified
3525    either by the match/mask part of the instruction definition, or by the
3526    operand list.  Also build up a list of operands in OPERANDS.
3527 
3528    INSN_BITS says which bits of the instruction are significant.
3529    If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3530    provides the mips_operand description of each operand.  DECODE_OPERAND
3531    is null for MIPS16 instructions.  */
3532 
3533 static int
3534 validate_mips_insn (const struct mips_opcode *opcode,
3535 		    unsigned long insn_bits,
3536 		    const struct mips_operand *(*decode_operand) (const char *),
3537 		    struct mips_operand_array *operands)
3538 {
3539   const char *s;
3540   unsigned long used_bits, doubled, undefined, opno, mask;
3541   const struct mips_operand *operand;
3542 
3543   mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3544   if ((mask & opcode->match) != opcode->match)
3545     {
3546       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3547 	      opcode->name, opcode->args);
3548       return 0;
3549     }
3550   used_bits = 0;
3551   opno = 0;
3552   if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3553     used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
3554   for (s = opcode->args; *s; ++s)
3555     switch (*s)
3556       {
3557       case ',':
3558       case '(':
3559       case ')':
3560 	break;
3561 
3562       case '#':
3563 	s++;
3564 	break;
3565 
3566       default:
3567 	if (!decode_operand)
3568 	  operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
3569 	else
3570 	  operand = decode_operand (s);
3571 	if (!operand && opcode->pinfo != INSN_MACRO)
3572 	  {
3573 	    as_bad (_("internal: unknown operand type: %s %s"),
3574 		    opcode->name, opcode->args);
3575 	    return 0;
3576 	  }
3577 	gas_assert (opno < MAX_OPERANDS);
3578 	operands->operand[opno] = operand;
3579 	if (!decode_operand && operand
3580 	    && operand->type == OP_INT && operand->lsb == 0
3581 	    && mips_opcode_32bit_p (opcode))
3582 	  used_bits |= mips16_immed_extend (-1, operand->size);
3583 	else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
3584 	  {
3585 	    used_bits = mips_insert_operand (operand, used_bits, -1);
3586 	    if (operand->type == OP_MDMX_IMM_REG)
3587 	      /* Bit 5 is the format selector (OB vs QH).  The opcode table
3588 		 has separate entries for each format.  */
3589 	      used_bits &= ~(1 << (operand->lsb + 5));
3590 	    if (operand->type == OP_ENTRY_EXIT_LIST)
3591 	      used_bits &= ~(mask & 0x700);
3592 	    /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3593 	       operand field that cannot be fully described with LSB/SIZE.  */
3594 	    if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3595 	      used_bits &= ~0x6000;
3596 	  }
3597 	/* Skip prefix characters.  */
3598 	if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
3599 	  ++s;
3600 	opno += 1;
3601 	break;
3602       }
3603   doubled = used_bits & mask & insn_bits;
3604   if (doubled)
3605     {
3606       as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3607 		" %s %s"), doubled, opcode->name, opcode->args);
3608       return 0;
3609     }
3610   used_bits |= mask;
3611   undefined = ~used_bits & insn_bits;
3612   if (opcode->pinfo != INSN_MACRO && undefined)
3613     {
3614       as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3615 	      undefined, opcode->name, opcode->args);
3616       return 0;
3617     }
3618   used_bits &= ~insn_bits;
3619   if (used_bits)
3620     {
3621       as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3622 	      used_bits, opcode->name, opcode->args);
3623       return 0;
3624     }
3625   return 1;
3626 }
3627 
3628 /* The MIPS16 version of validate_mips_insn.  */
3629 
3630 static int
3631 validate_mips16_insn (const struct mips_opcode *opcode,
3632 		      struct mips_operand_array *operands)
3633 {
3634   unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
3635 
3636   return validate_mips_insn (opcode, insn_bits, 0, operands);
3637 }
3638 
3639 /* The microMIPS version of validate_mips_insn.  */
3640 
3641 static int
3642 validate_micromips_insn (const struct mips_opcode *opc,
3643 			 struct mips_operand_array *operands)
3644 {
3645   unsigned long insn_bits;
3646   unsigned long major;
3647   unsigned int length;
3648 
3649   if (opc->pinfo == INSN_MACRO)
3650     return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3651 			       operands);
3652 
3653   length = micromips_insn_length (opc);
3654   if (length != 2 && length != 4)
3655     {
3656       as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
3657 		"%s %s"), length, opc->name, opc->args);
3658       return 0;
3659     }
3660   major = opc->match >> (10 + 8 * (length - 2));
3661   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3662       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3663     {
3664       as_bad (_("internal error: bad microMIPS opcode "
3665 		"(opcode/length mismatch): %s %s"), opc->name, opc->args);
3666       return 0;
3667     }
3668 
3669   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
3670   insn_bits = 1 << 4 * length;
3671   insn_bits <<= 4 * length;
3672   insn_bits -= 1;
3673   return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3674 			     operands);
3675 }
3676 
3677 /* This function is called once, at assembler startup time.  It should set up
3678    all the tables, etc. that the MD part of the assembler will need.  */
3679 
3680 void
3681 md_begin (void)
3682 {
3683   int i = 0;
3684   int broken = 0;
3685 
3686   if (mips_pic != NO_PIC)
3687     {
3688       if (g_switch_seen && g_switch_value != 0)
3689 	as_bad (_("-G may not be used in position-independent code"));
3690       g_switch_value = 0;
3691     }
3692   else if (mips_abicalls)
3693     {
3694       if (g_switch_seen && g_switch_value != 0)
3695 	as_bad (_("-G may not be used with abicalls"));
3696       g_switch_value = 0;
3697     }
3698 
3699   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3700     as_warn (_("could not set architecture and machine"));
3701 
3702   op_hash = str_htab_create ();
3703 
3704   mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
3705   for (i = 0; i < NUMOPCODES;)
3706     {
3707       const char *name = mips_opcodes[i].name;
3708 
3709       if (str_hash_insert (op_hash, name, &mips_opcodes[i], 0) != NULL)
3710 	as_fatal (_("duplicate %s"), name);
3711       do
3712 	{
3713 	  if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3714 				   decode_mips_operand, &mips_operands[i]))
3715 	    broken = 1;
3716 
3717 	  if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3718 	    {
3719 	      create_insn (&nop_insn, mips_opcodes + i);
3720 	      if (mips_fix_loongson2f_nop)
3721 		nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3722 	      nop_insn.fixed_p = 1;
3723 	    }
3724 
3725           if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3726 	    create_insn (&sync_insn, mips_opcodes + i);
3727 
3728 	  ++i;
3729 	}
3730       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3731     }
3732 
3733   mips16_op_hash = str_htab_create ();
3734   mips16_operands = XCNEWVEC (struct mips_operand_array,
3735 			      bfd_mips16_num_opcodes);
3736 
3737   i = 0;
3738   while (i < bfd_mips16_num_opcodes)
3739     {
3740       const char *name = mips16_opcodes[i].name;
3741 
3742       if (str_hash_insert (mips16_op_hash, name, &mips16_opcodes[i], 0))
3743 	as_fatal (_("duplicate %s"), name);
3744       do
3745 	{
3746 	  if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3747 	    broken = 1;
3748 	  if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3749 	    {
3750 	      create_insn (&mips16_nop_insn, mips16_opcodes + i);
3751 	      mips16_nop_insn.fixed_p = 1;
3752 	    }
3753 	  ++i;
3754 	}
3755       while (i < bfd_mips16_num_opcodes
3756 	     && strcmp (mips16_opcodes[i].name, name) == 0);
3757     }
3758 
3759   micromips_op_hash = str_htab_create ();
3760   micromips_operands = XCNEWVEC (struct mips_operand_array,
3761 				 bfd_micromips_num_opcodes);
3762 
3763   i = 0;
3764   while (i < bfd_micromips_num_opcodes)
3765     {
3766       const char *name = micromips_opcodes[i].name;
3767 
3768       if (str_hash_insert (micromips_op_hash, name, &micromips_opcodes[i], 0))
3769 	as_fatal (_("duplicate %s"), name);
3770       do
3771 	{
3772 	  struct mips_cl_insn *micromips_nop_insn;
3773 
3774 	  if (!validate_micromips_insn (&micromips_opcodes[i],
3775 					&micromips_operands[i]))
3776 	    broken = 1;
3777 
3778 	  if (micromips_opcodes[i].pinfo != INSN_MACRO)
3779 	    {
3780 	      if (micromips_insn_length (micromips_opcodes + i) == 2)
3781 		micromips_nop_insn = &micromips_nop16_insn;
3782 	      else if (micromips_insn_length (micromips_opcodes + i) == 4)
3783 		micromips_nop_insn = &micromips_nop32_insn;
3784 	      else
3785 		continue;
3786 
3787 	      if (micromips_nop_insn->insn_mo == NULL
3788 		  && strcmp (name, "nop") == 0)
3789 		{
3790 		  create_insn (micromips_nop_insn, micromips_opcodes + i);
3791 		  micromips_nop_insn->fixed_p = 1;
3792 		}
3793 	    }
3794 	}
3795       while (++i < bfd_micromips_num_opcodes
3796 	     && strcmp (micromips_opcodes[i].name, name) == 0);
3797     }
3798 
3799   if (broken)
3800     as_fatal (_("broken assembler, no assembly attempted"));
3801 
3802   /* We add all the general register names to the symbol table.  This
3803      helps us detect invalid uses of them.  */
3804   for (i = 0; reg_names[i].name; i++)
3805     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
3806 				     &zero_address_frag,
3807 				     reg_names[i].num));
3808   if (HAVE_NEWABI)
3809     for (i = 0; reg_names_n32n64[i].name; i++)
3810       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
3811 				       &zero_address_frag,
3812 				       reg_names_n32n64[i].num));
3813   else
3814     for (i = 0; reg_names_o32[i].name; i++)
3815       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
3816 				       &zero_address_frag,
3817 				       reg_names_o32[i].num));
3818 
3819   for (i = 0; i < 32; i++)
3820     {
3821       char regname[16];
3822 
3823       /* R5900 VU0 floating-point register.  */
3824       sprintf (regname, "$vf%d", i);
3825       symbol_table_insert (symbol_new (regname, reg_section,
3826 				       &zero_address_frag, RTYPE_VF | i));
3827 
3828       /* R5900 VU0 integer register.  */
3829       sprintf (regname, "$vi%d", i);
3830       symbol_table_insert (symbol_new (regname, reg_section,
3831 				       &zero_address_frag, RTYPE_VI | i));
3832 
3833       /* MSA register.  */
3834       sprintf (regname, "$w%d", i);
3835       symbol_table_insert (symbol_new (regname, reg_section,
3836 				       &zero_address_frag, RTYPE_MSA | i));
3837     }
3838 
3839   obstack_init (&mips_operand_tokens);
3840 
3841   mips_no_prev_insn ();
3842 
3843   mips_gprmask = 0;
3844   mips_cprmask[0] = 0;
3845   mips_cprmask[1] = 0;
3846   mips_cprmask[2] = 0;
3847   mips_cprmask[3] = 0;
3848 
3849   /* set the default alignment for the text section (2**2) */
3850   record_alignment (text_section, 2);
3851 
3852   bfd_set_gp_size (stdoutput, g_switch_value);
3853 
3854   /* On a native system other than VxWorks, sections must be aligned
3855      to 16 byte boundaries.  When configured for an embedded ELF
3856      target, we don't bother.  */
3857   if (!startswith (TARGET_OS, "elf")
3858       && !startswith (TARGET_OS, "vxworks"))
3859     {
3860       bfd_set_section_alignment (text_section, 4);
3861       bfd_set_section_alignment (data_section, 4);
3862       bfd_set_section_alignment (bss_section, 4);
3863     }
3864 
3865   /* Create a .reginfo section for register masks and a .mdebug
3866      section for debugging information.  */
3867   {
3868     segT seg;
3869     subsegT subseg;
3870     flagword flags;
3871     segT sec;
3872 
3873     seg = now_seg;
3874     subseg = now_subseg;
3875 
3876     /* The ABI says this section should be loaded so that the
3877        running program can access it.  However, we don't load it
3878        if we are configured for an embedded target.  */
3879     flags = SEC_READONLY | SEC_DATA;
3880     if (!startswith (TARGET_OS, "elf"))
3881       flags |= SEC_ALLOC | SEC_LOAD;
3882 
3883     if (mips_abi != N64_ABI)
3884       {
3885 	sec = subseg_new (".reginfo", (subsegT) 0);
3886 
3887 	bfd_set_section_flags (sec, flags);
3888 	bfd_set_section_alignment (sec, HAVE_NEWABI ? 3 : 2);
3889 
3890 	mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3891       }
3892     else
3893       {
3894 	/* The 64-bit ABI uses a .MIPS.options section rather than
3895 	   .reginfo section.  */
3896 	sec = subseg_new (".MIPS.options", (subsegT) 0);
3897 	bfd_set_section_flags (sec, flags);
3898 	bfd_set_section_alignment (sec, 3);
3899 
3900 	/* Set up the option header.  */
3901 	{
3902 	  Elf_Internal_Options opthdr;
3903 	  char *f;
3904 
3905 	  opthdr.kind = ODK_REGINFO;
3906 	  opthdr.size = (sizeof (Elf_External_Options)
3907 			 + sizeof (Elf64_External_RegInfo));
3908 	  opthdr.section = 0;
3909 	  opthdr.info = 0;
3910 	  f = frag_more (sizeof (Elf_External_Options));
3911 	  bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3912 					 (Elf_External_Options *) f);
3913 
3914 	  mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3915 	}
3916       }
3917 
3918     sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3919     bfd_set_section_flags (sec,
3920 			   SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3921     bfd_set_section_alignment (sec, 3);
3922     mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3923 
3924     if (ECOFF_DEBUGGING)
3925       {
3926 	sec = subseg_new (".mdebug", (subsegT) 0);
3927 	bfd_set_section_flags (sec, SEC_HAS_CONTENTS | SEC_READONLY);
3928 	bfd_set_section_alignment (sec, 2);
3929       }
3930     else if (mips_flag_pdr)
3931       {
3932 	pdr_seg = subseg_new (".pdr", (subsegT) 0);
3933 	bfd_set_section_flags (pdr_seg,
3934 			       SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
3935 	bfd_set_section_alignment (pdr_seg, 2);
3936       }
3937 
3938     subseg_set (seg, subseg);
3939   }
3940 
3941   if (mips_fix_vr4120)
3942     init_vr4120_conflicts ();
3943 }
3944 
3945 static inline void
3946 fpabi_incompatible_with (int fpabi, const char *what)
3947 {
3948   as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3949 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3950 }
3951 
3952 static inline void
3953 fpabi_requires (int fpabi, const char *what)
3954 {
3955   as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3956 	   Tag_GNU_MIPS_ABI_FP, fpabi, what);
3957 }
3958 
3959 /* Check -mabi and register sizes against the specified FP ABI.  */
3960 static void
3961 check_fpabi (int fpabi)
3962 {
3963   switch (fpabi)
3964     {
3965     case Val_GNU_MIPS_ABI_FP_DOUBLE:
3966       if (file_mips_opts.soft_float)
3967 	fpabi_incompatible_with (fpabi, "softfloat");
3968       else if (file_mips_opts.single_float)
3969 	fpabi_incompatible_with (fpabi, "singlefloat");
3970       if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3971 	fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3972       else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3973 	fpabi_incompatible_with (fpabi, "gp=32 fp=64");
3974       break;
3975 
3976     case Val_GNU_MIPS_ABI_FP_XX:
3977       if (mips_abi != O32_ABI)
3978 	fpabi_requires (fpabi, "-mabi=32");
3979       else if (file_mips_opts.soft_float)
3980 	fpabi_incompatible_with (fpabi, "softfloat");
3981       else if (file_mips_opts.single_float)
3982 	fpabi_incompatible_with (fpabi, "singlefloat");
3983       else if (file_mips_opts.fp != 0)
3984 	fpabi_requires (fpabi, "fp=xx");
3985       break;
3986 
3987     case Val_GNU_MIPS_ABI_FP_64A:
3988     case Val_GNU_MIPS_ABI_FP_64:
3989       if (mips_abi != O32_ABI)
3990 	fpabi_requires (fpabi, "-mabi=32");
3991       else if (file_mips_opts.soft_float)
3992 	fpabi_incompatible_with (fpabi, "softfloat");
3993       else if (file_mips_opts.single_float)
3994 	fpabi_incompatible_with (fpabi, "singlefloat");
3995       else if (file_mips_opts.fp != 64)
3996 	fpabi_requires (fpabi, "fp=64");
3997       else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3998 	fpabi_incompatible_with (fpabi, "nooddspreg");
3999       else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
4000 	fpabi_requires (fpabi, "nooddspreg");
4001       break;
4002 
4003     case Val_GNU_MIPS_ABI_FP_SINGLE:
4004       if (file_mips_opts.soft_float)
4005 	fpabi_incompatible_with (fpabi, "softfloat");
4006       else if (!file_mips_opts.single_float)
4007 	fpabi_requires (fpabi, "singlefloat");
4008       break;
4009 
4010     case Val_GNU_MIPS_ABI_FP_SOFT:
4011       if (!file_mips_opts.soft_float)
4012 	fpabi_requires (fpabi, "softfloat");
4013       break;
4014 
4015     case Val_GNU_MIPS_ABI_FP_OLD_64:
4016       as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4017 	       Tag_GNU_MIPS_ABI_FP, fpabi);
4018       break;
4019 
4020     case Val_GNU_MIPS_ABI_FP_NAN2008:
4021       /* Silently ignore compatibility value.  */
4022       break;
4023 
4024     default:
4025       as_warn (_(".gnu_attribute %d,%d is not a recognized"
4026 	         " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4027       break;
4028     }
4029 }
4030 
4031 /* Perform consistency checks on the current options.  */
4032 
4033 static void
4034 mips_check_options (struct mips_set_options *opts, bool abi_checks)
4035 {
4036   /* Check the size of integer registers agrees with the ABI and ISA.  */
4037   if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4038     as_bad (_("`gp=64' used with a 32-bit processor"));
4039   else if (abi_checks
4040 	   && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4041     as_bad (_("`gp=32' used with a 64-bit ABI"));
4042   else if (abi_checks
4043 	   && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4044     as_bad (_("`gp=64' used with a 32-bit ABI"));
4045 
4046   /* Check the size of the float registers agrees with the ABI and ISA.  */
4047   switch (opts->fp)
4048     {
4049     case 0:
4050       if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4051 	as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4052       else if (opts->single_float == 1)
4053 	as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4054       break;
4055     case 64:
4056       if (!ISA_HAS_64BIT_FPRS (opts->isa))
4057 	as_bad (_("`fp=64' used with a 32-bit fpu"));
4058       else if (abi_checks
4059 	       && ABI_NEEDS_32BIT_REGS (mips_abi)
4060 	       && !ISA_HAS_MXHC1 (opts->isa))
4061 	as_warn (_("`fp=64' used with a 32-bit ABI"));
4062       break;
4063     case 32:
4064       if (abi_checks
4065 	  && ABI_NEEDS_64BIT_REGS (mips_abi))
4066 	as_warn (_("`fp=32' used with a 64-bit ABI"));
4067       if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
4068 	as_bad (_("`fp=32' used with a MIPS R6 cpu"));
4069       break;
4070     default:
4071       as_bad (_("Unknown size of floating point registers"));
4072       break;
4073     }
4074 
4075   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4076     as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4077 
4078   if (opts->micromips == 1 && opts->mips16 == 1)
4079     as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
4080   else if (ISA_IS_R6 (opts->isa)
4081 	   && (opts->micromips == 1
4082 	       || opts->mips16 == 1))
4083     as_fatal (_("`%s' cannot be used with `%s'"),
4084 	      opts->micromips ? "micromips" : "mips16",
4085 	      mips_cpu_info_from_isa (opts->isa)->name);
4086 
4087   if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4088     as_fatal (_("branch relaxation is not supported in `%s'"),
4089 	      mips_cpu_info_from_isa (opts->isa)->name);
4090 }
4091 
4092 /* Perform consistency checks on the module level options exactly once.
4093    This is a deferred check that happens:
4094      at the first .set directive
4095      or, at the first pseudo op that generates code (inc .dc.a)
4096      or, at the first instruction
4097      or, at the end.  */
4098 
4099 static void
4100 file_mips_check_options (void)
4101 {
4102   if (file_mips_opts_checked)
4103     return;
4104 
4105   /* The following code determines the register size.
4106      Similar code was added to GCC 3.3 (see override_options() in
4107      config/mips/mips.c).  The GAS and GCC code should be kept in sync
4108      as much as possible.  */
4109 
4110   if (file_mips_opts.gp < 0)
4111     {
4112       /* Infer the integer register size from the ABI and processor.
4113 	 Restrict ourselves to 32-bit registers if that's all the
4114 	 processor has, or if the ABI cannot handle 64-bit registers.  */
4115       file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4116 			   || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4117 			  ? 32 : 64;
4118     }
4119 
4120   if (file_mips_opts.fp < 0)
4121     {
4122       /* No user specified float register size.
4123 	 ??? GAS treats single-float processors as though they had 64-bit
4124 	 float registers (although it complains when double-precision
4125 	 instructions are used).  As things stand, saying they have 32-bit
4126 	 registers would lead to spurious "register must be even" messages.
4127 	 So here we assume float registers are never smaller than the
4128 	 integer ones.  */
4129       if (file_mips_opts.gp == 64)
4130 	/* 64-bit integer registers implies 64-bit float registers.  */
4131 	file_mips_opts.fp = 64;
4132       else if ((file_mips_opts.ase & FP64_ASES)
4133 	       && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4134 	/* Handle ASEs that require 64-bit float registers, if possible.  */
4135 	file_mips_opts.fp = 64;
4136       else if (ISA_IS_R6 (mips_opts.isa))
4137 	/* R6 implies 64-bit float registers.  */
4138 	file_mips_opts.fp = 64;
4139       else
4140 	/* 32-bit float registers.  */
4141 	file_mips_opts.fp = 32;
4142     }
4143 
4144   /* Disable operations on odd-numbered floating-point registers by default
4145      when using the FPXX ABI.  */
4146   if (file_mips_opts.oddspreg < 0)
4147     {
4148       if (file_mips_opts.fp == 0)
4149 	file_mips_opts.oddspreg = 0;
4150       else
4151 	file_mips_opts.oddspreg = 1;
4152     }
4153 
4154   /* End of GCC-shared inference code.  */
4155 
4156   /* This flag is set when we have a 64-bit capable CPU but use only
4157      32-bit wide registers.  Note that EABI does not use it.  */
4158   if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4159       && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4160 	  || mips_abi == O32_ABI))
4161     mips_32bitmode = 1;
4162 
4163   if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4164     as_bad (_("trap exception not supported at ISA 1"));
4165 
4166   /* If the selected architecture includes support for ASEs, enable
4167      generation of code for them.  */
4168   if (file_mips_opts.mips16 == -1)
4169     file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4170   if (file_mips_opts.micromips == -1)
4171     file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4172 				? 1 : 0;
4173 
4174   if (mips_nan2008 == -1)
4175     mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4176   else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4177     as_fatal (_("`%s' does not support legacy NaN"),
4178 	      mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4179 
4180   /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4181      being selected implicitly.  */
4182   if (file_mips_opts.fp != 64)
4183     file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4184 
4185   /* If the user didn't explicitly select or deselect a particular ASE,
4186      use the default setting for the CPU.  */
4187   file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
4188 
4189   /* Set up the current options.  These may change throughout assembly.  */
4190   mips_opts = file_mips_opts;
4191 
4192   mips_check_isa_supports_ases ();
4193   mips_check_options (&file_mips_opts, true);
4194   file_mips_opts_checked = true;
4195 
4196   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4197     as_warn (_("could not set architecture and machine"));
4198 }
4199 
4200 void
4201 md_assemble (char *str)
4202 {
4203   struct mips_cl_insn insn;
4204   bfd_reloc_code_real_type unused_reloc[3]
4205     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
4206 
4207   file_mips_check_options ();
4208 
4209   imm_expr.X_op = O_absent;
4210   offset_expr.X_op = O_absent;
4211   offset_reloc[0] = BFD_RELOC_UNUSED;
4212   offset_reloc[1] = BFD_RELOC_UNUSED;
4213   offset_reloc[2] = BFD_RELOC_UNUSED;
4214 
4215   mips_mark_labels ();
4216   mips_assembling_insn = true;
4217   clear_insn_error ();
4218 
4219   if (mips_opts.mips16)
4220     mips16_ip (str, &insn);
4221   else
4222     {
4223       mips_ip (str, &insn);
4224       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4225 	    str, insn.insn_opcode));
4226     }
4227 
4228   if (insn_error.msg)
4229     report_insn_error (str);
4230   else if (insn.insn_mo->pinfo == INSN_MACRO)
4231     {
4232       macro_start ();
4233       if (mips_opts.mips16)
4234 	mips16_macro (&insn);
4235       else
4236 	macro (&insn, str);
4237       macro_end ();
4238     }
4239   else
4240     {
4241       if (offset_expr.X_op != O_absent)
4242 	append_insn (&insn, &offset_expr, offset_reloc, false);
4243       else
4244 	append_insn (&insn, NULL, unused_reloc, false);
4245     }
4246 
4247   mips_assembling_insn = false;
4248 }
4249 
4250 /* Convenience functions for abstracting away the differences between
4251    MIPS16 and non-MIPS16 relocations.  */
4252 
4253 static inline bool
4254 mips16_reloc_p (bfd_reloc_code_real_type reloc)
4255 {
4256   switch (reloc)
4257     {
4258     case BFD_RELOC_MIPS16_JMP:
4259     case BFD_RELOC_MIPS16_GPREL:
4260     case BFD_RELOC_MIPS16_GOT16:
4261     case BFD_RELOC_MIPS16_CALL16:
4262     case BFD_RELOC_MIPS16_HI16_S:
4263     case BFD_RELOC_MIPS16_HI16:
4264     case BFD_RELOC_MIPS16_LO16:
4265     case BFD_RELOC_MIPS16_16_PCREL_S1:
4266       return true;
4267 
4268     default:
4269       return false;
4270     }
4271 }
4272 
4273 static inline bool
4274 micromips_reloc_p (bfd_reloc_code_real_type reloc)
4275 {
4276   switch (reloc)
4277     {
4278     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4279     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4280     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4281     case BFD_RELOC_MICROMIPS_GPREL16:
4282     case BFD_RELOC_MICROMIPS_JMP:
4283     case BFD_RELOC_MICROMIPS_HI16:
4284     case BFD_RELOC_MICROMIPS_HI16_S:
4285     case BFD_RELOC_MICROMIPS_LO16:
4286     case BFD_RELOC_MICROMIPS_LITERAL:
4287     case BFD_RELOC_MICROMIPS_GOT16:
4288     case BFD_RELOC_MICROMIPS_CALL16:
4289     case BFD_RELOC_MICROMIPS_GOT_HI16:
4290     case BFD_RELOC_MICROMIPS_GOT_LO16:
4291     case BFD_RELOC_MICROMIPS_CALL_HI16:
4292     case BFD_RELOC_MICROMIPS_CALL_LO16:
4293     case BFD_RELOC_MICROMIPS_SUB:
4294     case BFD_RELOC_MICROMIPS_GOT_PAGE:
4295     case BFD_RELOC_MICROMIPS_GOT_OFST:
4296     case BFD_RELOC_MICROMIPS_GOT_DISP:
4297     case BFD_RELOC_MICROMIPS_HIGHEST:
4298     case BFD_RELOC_MICROMIPS_HIGHER:
4299     case BFD_RELOC_MICROMIPS_SCN_DISP:
4300     case BFD_RELOC_MICROMIPS_JALR:
4301       return true;
4302 
4303     default:
4304       return false;
4305     }
4306 }
4307 
4308 static inline bool
4309 jmp_reloc_p (bfd_reloc_code_real_type reloc)
4310 {
4311   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4312 }
4313 
4314 static inline bool
4315 b_reloc_p (bfd_reloc_code_real_type reloc)
4316 {
4317   return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4318 	  || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4319 	  || reloc == BFD_RELOC_16_PCREL_S2
4320 	  || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
4321 	  || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4322 	  || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4323 	  || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4324 }
4325 
4326 static inline bool
4327 got16_reloc_p (bfd_reloc_code_real_type reloc)
4328 {
4329   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
4330 	  || reloc == BFD_RELOC_MICROMIPS_GOT16);
4331 }
4332 
4333 static inline bool
4334 hi16_reloc_p (bfd_reloc_code_real_type reloc)
4335 {
4336   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
4337 	  || reloc == BFD_RELOC_MICROMIPS_HI16_S);
4338 }
4339 
4340 static inline bool
4341 lo16_reloc_p (bfd_reloc_code_real_type reloc)
4342 {
4343   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
4344 	  || reloc == BFD_RELOC_MICROMIPS_LO16);
4345 }
4346 
4347 static inline bool
4348 jalr_reloc_p (bfd_reloc_code_real_type reloc)
4349 {
4350   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
4351 }
4352 
4353 static inline bool
4354 gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4355 {
4356   return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4357 	  || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4358 }
4359 
4360 /* Return true if RELOC is a PC-relative relocation that does not have
4361    full address range.  */
4362 
4363 static inline bool
4364 limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4365 {
4366   switch (reloc)
4367     {
4368     case BFD_RELOC_16_PCREL_S2:
4369     case BFD_RELOC_MIPS16_16_PCREL_S1:
4370     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4371     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4372     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4373     case BFD_RELOC_MIPS_21_PCREL_S2:
4374     case BFD_RELOC_MIPS_26_PCREL_S2:
4375     case BFD_RELOC_MIPS_18_PCREL_S3:
4376     case BFD_RELOC_MIPS_19_PCREL_S2:
4377       return true;
4378 
4379     case BFD_RELOC_32_PCREL:
4380     case BFD_RELOC_HI16_S_PCREL:
4381     case BFD_RELOC_LO16_PCREL:
4382       return HAVE_64BIT_ADDRESSES;
4383 
4384     default:
4385       return false;
4386     }
4387 }
4388 
4389 /* Return true if the given relocation might need a matching %lo().
4390    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4391    need a matching %lo() when applied to local symbols.  */
4392 
4393 static inline bool
4394 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
4395 {
4396   return (HAVE_IN_PLACE_ADDENDS
4397 	  && (hi16_reloc_p (reloc)
4398 	      /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4399 		 all GOT16 relocations evaluate to "G".  */
4400 	      || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4401 }
4402 
4403 /* Return the type of %lo() reloc needed by RELOC, given that
4404    reloc_needs_lo_p.  */
4405 
4406 static inline bfd_reloc_code_real_type
4407 matching_lo_reloc (bfd_reloc_code_real_type reloc)
4408 {
4409   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4410 	  : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4411 	     : BFD_RELOC_LO16));
4412 }
4413 
4414 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
4415    relocation.  */
4416 
4417 static inline bool
4418 fixup_has_matching_lo_p (fixS *fixp)
4419 {
4420   return (fixp->fx_next != NULL
4421 	  && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
4422 	  && fixp->fx_addsy == fixp->fx_next->fx_addsy
4423 	  && fixp->fx_offset == fixp->fx_next->fx_offset);
4424 }
4425 
4426 /* Move all labels in LABELS to the current insertion point.  TEXT_P
4427    says whether the labels refer to text or data.  */
4428 
4429 static void
4430 mips_move_labels (struct insn_label_list *labels, bool text_p)
4431 {
4432   struct insn_label_list *l;
4433   valueT val;
4434 
4435   for (l = labels; l != NULL; l = l->next)
4436     {
4437       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
4438       symbol_set_frag (l->label, frag_now);
4439       val = (valueT) frag_now_fix ();
4440       /* MIPS16/microMIPS text labels are stored as odd.
4441 	 We just carry the ISA mode bit forward.  */
4442       if (text_p && HAVE_CODE_COMPRESSION)
4443 	val |= (S_GET_VALUE (l->label) & 0x1);
4444       S_SET_VALUE (l->label, val);
4445     }
4446 }
4447 
4448 /* Move all labels in insn_labels to the current insertion point
4449    and treat them as text labels.  */
4450 
4451 static void
4452 mips_move_text_labels (void)
4453 {
4454   mips_move_labels (seg_info (now_seg)->label_list, true);
4455 }
4456 
4457 /* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'.  */
4458 
4459 static bool
4460 s_is_linkonce (symbolS *sym, segT from_seg)
4461 {
4462   bool linkonce = false;
4463   segT symseg = S_GET_SEGMENT (sym);
4464 
4465   if (symseg != from_seg && !S_IS_LOCAL (sym))
4466     {
4467       if ((bfd_section_flags (symseg) & SEC_LINK_ONCE))
4468 	linkonce = true;
4469       /* The GNU toolchain uses an extension for ELF: a section
4470 	 beginning with the magic string .gnu.linkonce is a
4471 	 linkonce section.  */
4472       if (startswith (segment_name (symseg), ".gnu.linkonce"))
4473 	linkonce = true;
4474     }
4475   return linkonce;
4476 }
4477 
4478 /* Mark MIPS16 or microMIPS instruction label LABEL.  This permits the
4479    linker to handle them specially, such as generating jalx instructions
4480    when needed.  We also make them odd for the duration of the assembly,
4481    in order to generate the right sort of code.  We will make them even
4482    in the adjust_symtab routine, while leaving them marked.  This is
4483    convenient for the debugger and the disassembler.  The linker knows
4484    to make them odd again.  */
4485 
4486 static void
4487 mips_compressed_mark_label (symbolS *label)
4488 {
4489   gas_assert (HAVE_CODE_COMPRESSION);
4490 
4491   if (mips_opts.mips16)
4492     S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4493   else
4494     S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
4495   if ((S_GET_VALUE (label) & 1) == 0
4496       /* Don't adjust the address if the label is global or weak, or
4497 	 in a link-once section, since we'll be emitting symbol reloc
4498 	 references to it which will be patched up by the linker, and
4499 	 the final value of the symbol may or may not be MIPS16/microMIPS.  */
4500       && !S_IS_WEAK (label)
4501       && !S_IS_EXTERNAL (label)
4502       && !s_is_linkonce (label, now_seg))
4503     S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4504 }
4505 
4506 /* Mark preceding MIPS16 or microMIPS instruction labels.  */
4507 
4508 static void
4509 mips_compressed_mark_labels (void)
4510 {
4511   struct insn_label_list *l;
4512 
4513   for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4514     mips_compressed_mark_label (l->label);
4515 }
4516 
4517 /* End the current frag.  Make it a variant frag and record the
4518    relaxation info.  */
4519 
4520 static void
4521 relax_close_frag (void)
4522 {
4523   mips_macro_warning.first_frag = frag_now;
4524   frag_var (rs_machine_dependent, 0, 0,
4525 	    RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4526 			  mips_pic != NO_PIC),
4527 	    mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4528 
4529   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4530   mips_relax.first_fixup = 0;
4531 }
4532 
4533 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
4534    See the comment above RELAX_ENCODE for more details.  */
4535 
4536 static void
4537 relax_start (symbolS *symbol)
4538 {
4539   gas_assert (mips_relax.sequence == 0);
4540   mips_relax.sequence = 1;
4541   mips_relax.symbol = symbol;
4542 }
4543 
4544 /* Start generating the second version of a relaxable sequence.
4545    See the comment above RELAX_ENCODE for more details.  */
4546 
4547 static void
4548 relax_switch (void)
4549 {
4550   gas_assert (mips_relax.sequence == 1);
4551   mips_relax.sequence = 2;
4552 }
4553 
4554 /* End the current relaxable sequence.  */
4555 
4556 static void
4557 relax_end (void)
4558 {
4559   gas_assert (mips_relax.sequence == 2);
4560   relax_close_frag ();
4561   mips_relax.sequence = 0;
4562 }
4563 
4564 /* Return true if IP is a delayed branch or jump.  */
4565 
4566 static inline bool
4567 delayed_branch_p (const struct mips_cl_insn *ip)
4568 {
4569   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4570 				| INSN_COND_BRANCH_DELAY
4571 				| INSN_COND_BRANCH_LIKELY)) != 0;
4572 }
4573 
4574 /* Return true if IP is a compact branch or jump.  */
4575 
4576 static inline bool
4577 compact_branch_p (const struct mips_cl_insn *ip)
4578 {
4579   return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4580 				 | INSN2_COND_BRANCH)) != 0;
4581 }
4582 
4583 /* Return true if IP is an unconditional branch or jump.  */
4584 
4585 static inline bool
4586 uncond_branch_p (const struct mips_cl_insn *ip)
4587 {
4588   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
4589 	  || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
4590 }
4591 
4592 /* Return true if IP is a branch-likely instruction.  */
4593 
4594 static inline bool
4595 branch_likely_p (const struct mips_cl_insn *ip)
4596 {
4597   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4598 }
4599 
4600 /* Return the type of nop that should be used to fill the delay slot
4601    of delayed branch IP.  */
4602 
4603 static struct mips_cl_insn *
4604 get_delay_slot_nop (const struct mips_cl_insn *ip)
4605 {
4606   if (mips_opts.micromips
4607       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4608     return &micromips_nop32_insn;
4609   return NOP_INSN;
4610 }
4611 
4612 /* Return a mask that has bit N set if OPCODE reads the register(s)
4613    in operand N.  */
4614 
4615 static unsigned int
4616 insn_read_mask (const struct mips_opcode *opcode)
4617 {
4618   return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4619 }
4620 
4621 /* Return a mask that has bit N set if OPCODE writes to the register(s)
4622    in operand N.  */
4623 
4624 static unsigned int
4625 insn_write_mask (const struct mips_opcode *opcode)
4626 {
4627   return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4628 }
4629 
4630 /* Return a mask of the registers specified by operand OPERAND of INSN.
4631    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4632    is set.  */
4633 
4634 static unsigned int
4635 operand_reg_mask (const struct mips_cl_insn *insn,
4636 		  const struct mips_operand *operand,
4637 		  unsigned int type_mask)
4638 {
4639   unsigned int uval, vsel;
4640 
4641   switch (operand->type)
4642     {
4643     case OP_INT:
4644     case OP_MAPPED_INT:
4645     case OP_MSB:
4646     case OP_PCREL:
4647     case OP_PERF_REG:
4648     case OP_ADDIUSP_INT:
4649     case OP_ENTRY_EXIT_LIST:
4650     case OP_REPEAT_DEST_REG:
4651     case OP_REPEAT_PREV_REG:
4652     case OP_PC:
4653     case OP_VU0_SUFFIX:
4654     case OP_VU0_MATCH_SUFFIX:
4655     case OP_IMM_INDEX:
4656       abort ();
4657 
4658     case OP_REG28:
4659       return 1 << 28;
4660 
4661     case OP_REG:
4662     case OP_OPTIONAL_REG:
4663       {
4664 	const struct mips_reg_operand *reg_op;
4665 
4666 	reg_op = (const struct mips_reg_operand *) operand;
4667 	if (!(type_mask & (1 << reg_op->reg_type)))
4668 	  return 0;
4669 	uval = insn_extract_operand (insn, operand);
4670 	return 1u << mips_decode_reg_operand (reg_op, uval);
4671       }
4672 
4673     case OP_REG_PAIR:
4674       {
4675 	const struct mips_reg_pair_operand *pair_op;
4676 
4677 	pair_op = (const struct mips_reg_pair_operand *) operand;
4678 	if (!(type_mask & (1 << pair_op->reg_type)))
4679 	  return 0;
4680 	uval = insn_extract_operand (insn, operand);
4681 	return (1u << pair_op->reg1_map[uval]) | (1u << pair_op->reg2_map[uval]);
4682       }
4683 
4684     case OP_CLO_CLZ_DEST:
4685       if (!(type_mask & (1 << OP_REG_GP)))
4686 	return 0;
4687       uval = insn_extract_operand (insn, operand);
4688       return (1u << (uval & 31)) | (1u << (uval >> 5));
4689 
4690     case OP_SAME_RS_RT:
4691       if (!(type_mask & (1 << OP_REG_GP)))
4692 	return 0;
4693       uval = insn_extract_operand (insn, operand);
4694       gas_assert ((uval & 31) == (uval >> 5));
4695       return 1u << (uval & 31);
4696 
4697     case OP_CHECK_PREV:
4698     case OP_NON_ZERO_REG:
4699       if (!(type_mask & (1 << OP_REG_GP)))
4700 	return 0;
4701       uval = insn_extract_operand (insn, operand);
4702       return 1u << (uval & 31);
4703 
4704     case OP_LWM_SWM_LIST:
4705       abort ();
4706 
4707     case OP_SAVE_RESTORE_LIST:
4708       abort ();
4709 
4710     case OP_MDMX_IMM_REG:
4711       if (!(type_mask & (1 << OP_REG_VEC)))
4712 	return 0;
4713       uval = insn_extract_operand (insn, operand);
4714       vsel = uval >> 5;
4715       if ((vsel & 0x18) == 0x18)
4716 	return 0;
4717       return 1u << (uval & 31);
4718 
4719     case OP_REG_INDEX:
4720       if (!(type_mask & (1 << OP_REG_GP)))
4721 	return 0;
4722       return 1u << insn_extract_operand (insn, operand);
4723     }
4724   abort ();
4725 }
4726 
4727 /* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4728    where bit N of OPNO_MASK is set if operand N should be included.
4729    Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4730    is set.  */
4731 
4732 static unsigned int
4733 insn_reg_mask (const struct mips_cl_insn *insn,
4734 	       unsigned int type_mask, unsigned int opno_mask)
4735 {
4736   unsigned int opno, reg_mask;
4737 
4738   opno = 0;
4739   reg_mask = 0;
4740   while (opno_mask != 0)
4741     {
4742       if (opno_mask & 1)
4743 	reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4744       opno_mask >>= 1;
4745       opno += 1;
4746     }
4747   return reg_mask;
4748 }
4749 
4750 /* Return the mask of core registers that IP reads.  */
4751 
4752 static unsigned int
4753 gpr_read_mask (const struct mips_cl_insn *ip)
4754 {
4755   unsigned long pinfo, pinfo2;
4756   unsigned int mask;
4757 
4758   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4759   pinfo = ip->insn_mo->pinfo;
4760   pinfo2 = ip->insn_mo->pinfo2;
4761   if (pinfo & INSN_UDI)
4762     {
4763       /* UDI instructions have traditionally been assumed to read RS
4764 	 and RT.  */
4765       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4766       mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4767     }
4768   if (pinfo & INSN_READ_GPR_24)
4769     mask |= 1 << 24;
4770   if (pinfo2 & INSN2_READ_GPR_16)
4771     mask |= 1 << 16;
4772   if (pinfo2 & INSN2_READ_SP)
4773     mask |= 1 << SP;
4774   if (pinfo2 & INSN2_READ_GPR_31)
4775     mask |= 1u << 31;
4776   /* Don't include register 0.  */
4777   return mask & ~1;
4778 }
4779 
4780 /* Return the mask of core registers that IP writes.  */
4781 
4782 static unsigned int
4783 gpr_write_mask (const struct mips_cl_insn *ip)
4784 {
4785   unsigned long pinfo, pinfo2;
4786   unsigned int mask;
4787 
4788   mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4789   pinfo = ip->insn_mo->pinfo;
4790   pinfo2 = ip->insn_mo->pinfo2;
4791   if (pinfo & INSN_WRITE_GPR_24)
4792     mask |= 1 << 24;
4793   if (pinfo & INSN_WRITE_GPR_31)
4794     mask |= 1u << 31;
4795   if (pinfo & INSN_UDI)
4796     /* UDI instructions have traditionally been assumed to write to RD.  */
4797     mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4798   if (pinfo2 & INSN2_WRITE_SP)
4799     mask |= 1 << SP;
4800   /* Don't include register 0.  */
4801   return mask & ~1;
4802 }
4803 
4804 /* Return the mask of floating-point registers that IP reads.  */
4805 
4806 static unsigned int
4807 fpr_read_mask (const struct mips_cl_insn *ip)
4808 {
4809   unsigned long pinfo;
4810   unsigned int mask;
4811 
4812   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4813 			     | (1 << OP_REG_MSA)),
4814 			insn_read_mask (ip->insn_mo));
4815   pinfo = ip->insn_mo->pinfo;
4816   /* Conservatively treat all operands to an FP_D instruction are doubles.
4817      (This is overly pessimistic for things like cvt.d.s.)  */
4818   if (FPR_SIZE != 64 && (pinfo & FP_D))
4819     mask |= mask << 1;
4820   return mask;
4821 }
4822 
4823 /* Return the mask of floating-point registers that IP writes.  */
4824 
4825 static unsigned int
4826 fpr_write_mask (const struct mips_cl_insn *ip)
4827 {
4828   unsigned long pinfo;
4829   unsigned int mask;
4830 
4831   mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4832 			     | (1 << OP_REG_MSA)),
4833 			insn_write_mask (ip->insn_mo));
4834   pinfo = ip->insn_mo->pinfo;
4835   /* Conservatively treat all operands to an FP_D instruction are doubles.
4836      (This is overly pessimistic for things like cvt.s.d.)  */
4837   if (FPR_SIZE != 64 && (pinfo & FP_D))
4838     mask |= mask << 1;
4839   return mask;
4840 }
4841 
4842 /* Operand OPNUM of INSN is an odd-numbered floating-point register.
4843    Check whether that is allowed.  */
4844 
4845 static bool
4846 mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4847 {
4848   const char *s = insn->name;
4849   bool oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4850 		   || FPR_SIZE == 64) && mips_opts.oddspreg;
4851 
4852   if (insn->pinfo == INSN_MACRO)
4853     /* Let a macro pass, we'll catch it later when it is expanded.  */
4854     return true;
4855 
4856   /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4857      otherwise it depends on oddspreg.  */
4858   if ((insn->pinfo & FP_S)
4859       && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
4860 			 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
4861     return FPR_SIZE == 32 || oddspreg;
4862 
4863   /* Allow odd registers for single-precision ops and double-precision if the
4864      floating-point registers are 64-bit wide.  */
4865   switch (insn->pinfo & (FP_S | FP_D))
4866     {
4867     case FP_S:
4868     case 0:
4869       return oddspreg;
4870     case FP_D:
4871       return FPR_SIZE == 64;
4872     default:
4873       break;
4874     }
4875 
4876   /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
4877   s = strchr (insn->name, '.');
4878   if (s != NULL && opnum == 2)
4879     s = strchr (s + 1, '.');
4880   if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4881     return oddspreg;
4882 
4883   return FPR_SIZE == 64;
4884 }
4885 
4886 /* Information about an instruction argument that we're trying to match.  */
4887 struct mips_arg_info
4888 {
4889   /* The instruction so far.  */
4890   struct mips_cl_insn *insn;
4891 
4892   /* The first unconsumed operand token.  */
4893   struct mips_operand_token *token;
4894 
4895   /* The 1-based operand number, in terms of insn->insn_mo->args.  */
4896   int opnum;
4897 
4898   /* The 1-based argument number, for error reporting.  This does not
4899      count elided optional registers, etc..  */
4900   int argnum;
4901 
4902   /* The last OP_REG operand seen, or ILLEGAL_REG if none.  */
4903   unsigned int last_regno;
4904 
4905   /* If the first operand was an OP_REG, this is the register that it
4906      specified, otherwise it is ILLEGAL_REG.  */
4907   unsigned int dest_regno;
4908 
4909   /* The value of the last OP_INT operand.  Only used for OP_MSB,
4910      where it gives the lsb position.  */
4911   unsigned int last_op_int;
4912 
4913   /* If true, match routines should assume that no later instruction
4914      alternative matches and should therefore be as accommodating as
4915      possible.  Match routines should not report errors if something
4916      is only invalid for !LAX_MATCH.  */
4917   bool lax_match;
4918 
4919   /* True if a reference to the current AT register was seen.  */
4920   bool seen_at;
4921 };
4922 
4923 /* Record that the argument is out of range.  */
4924 
4925 static void
4926 match_out_of_range (struct mips_arg_info *arg)
4927 {
4928   set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4929 }
4930 
4931 /* Record that the argument isn't constant but needs to be.  */
4932 
4933 static void
4934 match_not_constant (struct mips_arg_info *arg)
4935 {
4936   set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4937 		    arg->argnum);
4938 }
4939 
4940 /* Try to match an OT_CHAR token for character CH.  Consume the token
4941    and return true on success, otherwise return false.  */
4942 
4943 static bool
4944 match_char (struct mips_arg_info *arg, char ch)
4945 {
4946   if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4947     {
4948       ++arg->token;
4949       if (ch == ',')
4950 	arg->argnum += 1;
4951       return true;
4952     }
4953   return false;
4954 }
4955 
4956 /* Try to get an expression from the next tokens in ARG.  Consume the
4957    tokens and return true on success, storing the expression value in
4958    VALUE and relocation types in R.  */
4959 
4960 static bool
4961 match_expression (struct mips_arg_info *arg, expressionS *value,
4962 		  bfd_reloc_code_real_type *r)
4963 {
4964   /* If the next token is a '(' that was parsed as being part of a base
4965      expression, assume we have an elided offset.  The later match will fail
4966      if this turns out to be wrong.  */
4967   if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
4968     {
4969       value->X_op = O_constant;
4970       value->X_add_number = 0;
4971       r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
4972       return true;
4973     }
4974 
4975   /* Reject register-based expressions such as "0+$2" and "(($2))".
4976      For plain registers the default error seems more appropriate.  */
4977   if (arg->token->type == OT_INTEGER
4978       && arg->token->u.integer.value.X_op == O_register)
4979     {
4980       set_insn_error (arg->argnum, _("register value used as expression"));
4981       return false;
4982     }
4983 
4984   if (arg->token->type == OT_INTEGER)
4985     {
4986       *value = arg->token->u.integer.value;
4987       memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4988       ++arg->token;
4989       return true;
4990     }
4991 
4992   set_insn_error_i
4993     (arg->argnum, _("operand %d must be an immediate expression"),
4994      arg->argnum);
4995   return false;
4996 }
4997 
4998 /* Try to get a constant expression from the next tokens in ARG.  Consume
4999    the tokens and return true on success, storing the constant value
5000    in *VALUE.  */
5001 
5002 static bool
5003 match_const_int (struct mips_arg_info *arg, offsetT *value)
5004 {
5005   expressionS ex;
5006   bfd_reloc_code_real_type r[3];
5007 
5008   if (!match_expression (arg, &ex, r))
5009     return false;
5010 
5011   if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
5012     *value = ex.X_add_number;
5013   else
5014     {
5015       if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5016 	match_out_of_range (arg);
5017       else
5018 	match_not_constant (arg);
5019       return false;
5020     }
5021   return true;
5022 }
5023 
5024 /* Return the RTYPE_* flags for a register operand of type TYPE that
5025    appears in instruction OPCODE.  */
5026 
5027 static unsigned int
5028 convert_reg_type (const struct mips_opcode *opcode,
5029 		  enum mips_reg_operand_type type)
5030 {
5031   switch (type)
5032     {
5033     case OP_REG_GP:
5034       return RTYPE_NUM | RTYPE_GP;
5035 
5036     case OP_REG_FP:
5037       /* Allow vector register names for MDMX if the instruction is a 64-bit
5038 	 FPR load, store or move (including moves to and from GPRs).  */
5039       if ((mips_opts.ase & ASE_MDMX)
5040 	  && (opcode->pinfo & FP_D)
5041 	  && (opcode->pinfo & (INSN_COPROC_MOVE
5042 			       | INSN_COPROC_MEMORY_DELAY
5043 			       | INSN_LOAD_COPROC
5044 			       | INSN_LOAD_MEMORY
5045 			       | INSN_STORE_MEMORY)))
5046 	return RTYPE_FPU | RTYPE_VEC;
5047       return RTYPE_FPU;
5048 
5049     case OP_REG_CCC:
5050       if (opcode->pinfo & (FP_D | FP_S))
5051 	return RTYPE_CCC | RTYPE_FCC;
5052       return RTYPE_CCC;
5053 
5054     case OP_REG_VEC:
5055       if (opcode->membership & INSN_5400)
5056 	return RTYPE_FPU;
5057       return RTYPE_FPU | RTYPE_VEC;
5058 
5059     case OP_REG_ACC:
5060       return RTYPE_ACC;
5061 
5062     case OP_REG_COPRO:
5063     case OP_REG_CONTROL:
5064       if (opcode->name[strlen (opcode->name) - 1] == '0')
5065 	return RTYPE_NUM | RTYPE_CP0;
5066       return RTYPE_NUM;
5067 
5068     case OP_REG_HW:
5069       return RTYPE_NUM;
5070 
5071     case OP_REG_VI:
5072       return RTYPE_NUM | RTYPE_VI;
5073 
5074     case OP_REG_VF:
5075       return RTYPE_NUM | RTYPE_VF;
5076 
5077     case OP_REG_R5900_I:
5078       return RTYPE_R5900_I;
5079 
5080     case OP_REG_R5900_Q:
5081       return RTYPE_R5900_Q;
5082 
5083     case OP_REG_R5900_R:
5084       return RTYPE_R5900_R;
5085 
5086     case OP_REG_R5900_ACC:
5087       return RTYPE_R5900_ACC;
5088 
5089     case OP_REG_MSA:
5090       return RTYPE_MSA;
5091 
5092     case OP_REG_MSA_CTRL:
5093       return RTYPE_NUM;
5094     }
5095   abort ();
5096 }
5097 
5098 /* ARG is register REGNO, of type TYPE.  Warn about any dubious registers.  */
5099 
5100 static void
5101 check_regno (struct mips_arg_info *arg,
5102 	     enum mips_reg_operand_type type, unsigned int regno)
5103 {
5104   if (AT && type == OP_REG_GP && regno == AT)
5105     arg->seen_at = true;
5106 
5107   if (type == OP_REG_FP
5108       && (regno & 1) != 0
5109       && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
5110     {
5111       /* This was a warning prior to introducing O32 FPXX and FP64 support
5112 	 so maintain a warning for FP32 but raise an error for the new
5113 	 cases.  */
5114       if (FPR_SIZE == 32)
5115 	as_warn (_("float register should be even, was %d"), regno);
5116       else
5117 	as_bad (_("float register should be even, was %d"), regno);
5118     }
5119 
5120   if (type == OP_REG_CCC)
5121     {
5122       const char *name;
5123       size_t length;
5124 
5125       name = arg->insn->insn_mo->name;
5126       length = strlen (name);
5127       if ((regno & 1) != 0
5128 	  && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5129 	      || (length >= 5 && startswith (name + length - 5, "any2"))))
5130 	as_warn (_("condition code register should be even for %s, was %d"),
5131 		 name, regno);
5132 
5133       if ((regno & 3) != 0
5134 	  && (length >= 5 && startswith (name + length - 5, "any4")))
5135 	as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
5136 		 name, regno);
5137     }
5138 }
5139 
5140 /* ARG is a register with symbol value SYMVAL.  Try to interpret it as
5141    a register of type TYPE.  Return true on success, storing the register
5142    number in *REGNO and warning about any dubious uses.  */
5143 
5144 static bool
5145 match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5146 	     unsigned int symval, unsigned int *regno)
5147 {
5148   if (type == OP_REG_VEC)
5149     symval = mips_prefer_vec_regno (symval);
5150   if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5151     return false;
5152 
5153   *regno = symval & RNUM_MASK;
5154   check_regno (arg, type, *regno);
5155   return true;
5156 }
5157 
5158 /* Try to interpret the next token in ARG as a register of type TYPE.
5159    Consume the token and return true on success, storing the register
5160    number in *REGNO.  Return false on failure.  */
5161 
5162 static bool
5163 match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5164 	   unsigned int *regno)
5165 {
5166   if (arg->token->type == OT_REG
5167       && match_regno (arg, type, arg->token->u.regno, regno))
5168     {
5169       ++arg->token;
5170       return true;
5171     }
5172   return false;
5173 }
5174 
5175 /* Try to interpret the next token in ARG as a range of registers of type TYPE.
5176    Consume the token and return true on success, storing the register numbers
5177    in *REGNO1 and *REGNO2.  Return false on failure.  */
5178 
5179 static bool
5180 match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5181 		 unsigned int *regno1, unsigned int *regno2)
5182 {
5183   if (match_reg (arg, type, regno1))
5184     {
5185       *regno2 = *regno1;
5186       return true;
5187     }
5188   if (arg->token->type == OT_REG_RANGE
5189       && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5190       && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5191       && *regno1 <= *regno2)
5192     {
5193       ++arg->token;
5194       return true;
5195     }
5196   return false;
5197 }
5198 
5199 /* OP_INT matcher.  */
5200 
5201 static bool
5202 match_int_operand (struct mips_arg_info *arg,
5203 		   const struct mips_operand *operand_base)
5204 {
5205   const struct mips_int_operand *operand;
5206   unsigned int uval;
5207   int min_val, max_val, factor;
5208   offsetT sval;
5209 
5210   operand = (const struct mips_int_operand *) operand_base;
5211   factor = 1 << operand->shift;
5212   min_val = mips_int_operand_min (operand);
5213   max_val = mips_int_operand_max (operand);
5214 
5215   if (operand_base->lsb == 0
5216       && operand_base->size == 16
5217       && operand->shift == 0
5218       && operand->bias == 0
5219       && (operand->max_val == 32767 || operand->max_val == 65535))
5220     {
5221       /* The operand can be relocated.  */
5222       if (!match_expression (arg, &offset_expr, offset_reloc))
5223 	return false;
5224 
5225       if (offset_expr.X_op == O_big)
5226 	{
5227 	  match_out_of_range (arg);
5228 	  return false;
5229 	}
5230 
5231       if (offset_reloc[0] != BFD_RELOC_UNUSED)
5232 	/* Relocation operators were used.  Accept the argument and
5233 	   leave the relocation value in offset_expr and offset_relocs
5234 	   for the caller to process.  */
5235 	return true;
5236 
5237       if (offset_expr.X_op != O_constant)
5238 	{
5239 	  /* Accept non-constant operands if no later alternative matches,
5240 	     leaving it for the caller to process.  */
5241 	  if (!arg->lax_match)
5242 	    {
5243 	      match_not_constant (arg);
5244 	      return false;
5245 	    }
5246 	  offset_reloc[0] = BFD_RELOC_LO16;
5247 	  return true;
5248 	}
5249 
5250       /* Clear the global state; we're going to install the operand
5251 	 ourselves.  */
5252       sval = offset_expr.X_add_number;
5253       offset_expr.X_op = O_absent;
5254 
5255       /* For compatibility with older assemblers, we accept
5256 	 0x8000-0xffff as signed 16-bit numbers when only
5257 	 signed numbers are allowed.  */
5258       if (sval > max_val)
5259 	{
5260 	  max_val = ((1 << operand_base->size) - 1) << operand->shift;
5261 	  if (!arg->lax_match && sval <= max_val)
5262 	    {
5263 	      match_out_of_range (arg);
5264 	      return false;
5265 	    }
5266 	}
5267     }
5268   else
5269     {
5270       if (!match_const_int (arg, &sval))
5271 	return false;
5272     }
5273 
5274   arg->last_op_int = sval;
5275 
5276   if (sval < min_val || sval > max_val || sval % factor)
5277     {
5278       match_out_of_range (arg);
5279       return false;
5280     }
5281 
5282   uval = (unsigned int) sval >> operand->shift;
5283   uval -= operand->bias;
5284 
5285   /* Handle -mfix-cn63xxp1.  */
5286   if (arg->opnum == 1
5287       && mips_fix_cn63xxp1
5288       && !mips_opts.micromips
5289       && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5290     switch (uval)
5291       {
5292       case 5:
5293       case 25:
5294       case 26:
5295       case 27:
5296       case 28:
5297       case 29:
5298       case 30:
5299       case 31:
5300 	/* These are ok.  */
5301 	break;
5302 
5303       default:
5304 	/* The rest must be changed to 28.  */
5305 	uval = 28;
5306 	break;
5307       }
5308 
5309   insn_insert_operand (arg->insn, operand_base, uval);
5310   return true;
5311 }
5312 
5313 /* OP_MAPPED_INT matcher.  */
5314 
5315 static bool
5316 match_mapped_int_operand (struct mips_arg_info *arg,
5317 			  const struct mips_operand *operand_base)
5318 {
5319   const struct mips_mapped_int_operand *operand;
5320   unsigned int uval, num_vals;
5321   offsetT sval;
5322 
5323   operand = (const struct mips_mapped_int_operand *) operand_base;
5324   if (!match_const_int (arg, &sval))
5325     return false;
5326 
5327   num_vals = 1 << operand_base->size;
5328   for (uval = 0; uval < num_vals; uval++)
5329     if (operand->int_map[uval] == sval)
5330       break;
5331   if (uval == num_vals)
5332     {
5333       match_out_of_range (arg);
5334       return false;
5335     }
5336 
5337   insn_insert_operand (arg->insn, operand_base, uval);
5338   return true;
5339 }
5340 
5341 /* OP_MSB matcher.  */
5342 
5343 static bool
5344 match_msb_operand (struct mips_arg_info *arg,
5345 		   const struct mips_operand *operand_base)
5346 {
5347   const struct mips_msb_operand *operand;
5348   int min_val, max_val, max_high;
5349   offsetT size, sval, high;
5350 
5351   operand = (const struct mips_msb_operand *) operand_base;
5352   min_val = operand->bias;
5353   max_val = min_val + (1 << operand_base->size) - 1;
5354   max_high = operand->opsize;
5355 
5356   if (!match_const_int (arg, &size))
5357     return false;
5358 
5359   high = size + arg->last_op_int;
5360   sval = operand->add_lsb ? high : size;
5361 
5362   if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5363     {
5364       match_out_of_range (arg);
5365       return false;
5366     }
5367   insn_insert_operand (arg->insn, operand_base, sval - min_val);
5368   return true;
5369 }
5370 
5371 /* OP_REG matcher.  */
5372 
5373 static bool
5374 match_reg_operand (struct mips_arg_info *arg,
5375 		   const struct mips_operand *operand_base)
5376 {
5377   const struct mips_reg_operand *operand;
5378   unsigned int regno, uval, num_vals;
5379 
5380   operand = (const struct mips_reg_operand *) operand_base;
5381   if (!match_reg (arg, operand->reg_type, &regno))
5382     return false;
5383 
5384   if (operand->reg_map)
5385     {
5386       num_vals = 1 << operand->root.size;
5387       for (uval = 0; uval < num_vals; uval++)
5388 	if (operand->reg_map[uval] == regno)
5389 	  break;
5390       if (num_vals == uval)
5391 	return false;
5392     }
5393   else
5394     uval = regno;
5395 
5396   arg->last_regno = regno;
5397   if (arg->opnum == 1)
5398     arg->dest_regno = regno;
5399   insn_insert_operand (arg->insn, operand_base, uval);
5400   return true;
5401 }
5402 
5403 /* OP_REG_PAIR matcher.  */
5404 
5405 static bool
5406 match_reg_pair_operand (struct mips_arg_info *arg,
5407 			const struct mips_operand *operand_base)
5408 {
5409   const struct mips_reg_pair_operand *operand;
5410   unsigned int regno1, regno2, uval, num_vals;
5411 
5412   operand = (const struct mips_reg_pair_operand *) operand_base;
5413   if (!match_reg (arg, operand->reg_type, &regno1)
5414       || !match_char (arg, ',')
5415       || !match_reg (arg, operand->reg_type, &regno2))
5416     return false;
5417 
5418   num_vals = 1 << operand_base->size;
5419   for (uval = 0; uval < num_vals; uval++)
5420     if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5421       break;
5422   if (uval == num_vals)
5423     return false;
5424 
5425   insn_insert_operand (arg->insn, operand_base, uval);
5426   return true;
5427 }
5428 
5429 /* OP_PCREL matcher.  The caller chooses the relocation type.  */
5430 
5431 static bool
5432 match_pcrel_operand (struct mips_arg_info *arg)
5433 {
5434   bfd_reloc_code_real_type r[3];
5435 
5436   return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
5437 }
5438 
5439 /* OP_PERF_REG matcher.  */
5440 
5441 static bool
5442 match_perf_reg_operand (struct mips_arg_info *arg,
5443 			const struct mips_operand *operand)
5444 {
5445   offsetT sval;
5446 
5447   if (!match_const_int (arg, &sval))
5448     return false;
5449 
5450   if (sval != 0
5451       && (sval != 1
5452 	  || (mips_opts.arch == CPU_R5900
5453 	      && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5454 		  || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5455     {
5456       set_insn_error (arg->argnum, _("invalid performance register"));
5457       return false;
5458     }
5459 
5460   insn_insert_operand (arg->insn, operand, sval);
5461   return true;
5462 }
5463 
5464 /* OP_ADDIUSP matcher.  */
5465 
5466 static bool
5467 match_addiusp_operand (struct mips_arg_info *arg,
5468 		       const struct mips_operand *operand)
5469 {
5470   offsetT sval;
5471   unsigned int uval;
5472 
5473   if (!match_const_int (arg, &sval))
5474     return false;
5475 
5476   if (sval % 4)
5477     {
5478       match_out_of_range (arg);
5479       return false;
5480     }
5481 
5482   sval /= 4;
5483   if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
5484     {
5485       match_out_of_range (arg);
5486       return false;
5487     }
5488 
5489   uval = (unsigned int) sval;
5490   uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5491   insn_insert_operand (arg->insn, operand, uval);
5492   return true;
5493 }
5494 
5495 /* OP_CLO_CLZ_DEST matcher.  */
5496 
5497 static bool
5498 match_clo_clz_dest_operand (struct mips_arg_info *arg,
5499 			    const struct mips_operand *operand)
5500 {
5501   unsigned int regno;
5502 
5503   if (!match_reg (arg, OP_REG_GP, &regno))
5504     return false;
5505 
5506   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5507   return true;
5508 }
5509 
5510 /* OP_CHECK_PREV matcher.  */
5511 
5512 static bool
5513 match_check_prev_operand (struct mips_arg_info *arg,
5514 			  const struct mips_operand *operand_base)
5515 {
5516   const struct mips_check_prev_operand *operand;
5517   unsigned int regno;
5518 
5519   operand = (const struct mips_check_prev_operand *) operand_base;
5520 
5521   if (!match_reg (arg, OP_REG_GP, &regno))
5522     return false;
5523 
5524   if (!operand->zero_ok && regno == 0)
5525     return false;
5526 
5527   if ((operand->less_than_ok && regno < arg->last_regno)
5528       || (operand->greater_than_ok && regno > arg->last_regno)
5529       || (operand->equal_ok && regno == arg->last_regno))
5530     {
5531       arg->last_regno = regno;
5532       insn_insert_operand (arg->insn, operand_base, regno);
5533       return true;
5534     }
5535 
5536   return false;
5537 }
5538 
5539 /* OP_SAME_RS_RT matcher.  */
5540 
5541 static bool
5542 match_same_rs_rt_operand (struct mips_arg_info *arg,
5543 			  const struct mips_operand *operand)
5544 {
5545   unsigned int regno;
5546 
5547   if (!match_reg (arg, OP_REG_GP, &regno))
5548     return false;
5549 
5550   if (regno == 0)
5551     {
5552       set_insn_error (arg->argnum, _("the source register must not be $0"));
5553       return false;
5554     }
5555 
5556   arg->last_regno = regno;
5557 
5558   insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5559   return true;
5560 }
5561 
5562 /* OP_LWM_SWM_LIST matcher.  */
5563 
5564 static bool
5565 match_lwm_swm_list_operand (struct mips_arg_info *arg,
5566 			    const struct mips_operand *operand)
5567 {
5568   unsigned int reglist, sregs, ra, regno1, regno2;
5569   struct mips_arg_info reset;
5570 
5571   reglist = 0;
5572   if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5573     return false;
5574   do
5575     {
5576       if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5577 	{
5578 	  reglist |= 1 << FP;
5579 	  regno2 = S7;
5580 	}
5581       reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5582       reset = *arg;
5583     }
5584   while (match_char (arg, ',')
5585 	 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5586   *arg = reset;
5587 
5588   if (operand->size == 2)
5589     {
5590       /* The list must include both ra and s0-sN, for 0 <= N <= 3.  E.g.:
5591 
5592 	 s0, ra
5593 	 s0, s1, ra, s2, s3
5594 	 s0-s2, ra
5595 
5596 	 and any permutations of these.  */
5597       if ((reglist & 0xfff1ffff) != 0x80010000)
5598 	return false;
5599 
5600       sregs = (reglist >> 17) & 7;
5601       ra = 0;
5602     }
5603   else
5604     {
5605       /* The list must include at least one of ra and s0-sN,
5606 	 for 0 <= N <= 8.  (Note that there is a gap between s7 and s8,
5607 	 which are $23 and $30 respectively.)  E.g.:
5608 
5609 	 ra
5610 	 s0
5611 	 ra, s0, s1, s2
5612 	 s0-s8
5613 	 s0-s5, ra
5614 
5615 	 and any permutations of these.  */
5616       if ((reglist & 0x3f00ffff) != 0)
5617 	return false;
5618 
5619 
5620 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
5621       ra = (reglist >> 27) & 0x10;
5622       sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5623     }
5624   sregs += 1;
5625   if ((sregs & -sregs) != sregs)
5626     return false;
5627 
5628   insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5629   return true;
5630 }
5631 
5632 static void
5633 trap_zero_jump (struct mips_cl_insn * ip)
5634 {
5635   if (strcmp (ip->insn_mo->name, "j") == 0
5636       || strcmp (ip->insn_mo->name, "jr") == 0
5637       || strcmp (ip->insn_mo->name, "jalr") == 0)
5638     {
5639       int sreg;
5640 
5641       if (mips_opts.warn_about_macros)
5642         return;
5643 
5644       sreg = EXTRACT_OPERAND (0, RS, *ip);
5645       if (mips_opts.isa == ISA_MIPS32
5646           || mips_opts.isa == ISA_MIPS32R2
5647           || mips_opts.isa == ISA_MIPS64
5648           || mips_opts.isa == ISA_MIPS64R2)
5649 	{
5650 	  expressionS ep;
5651 	  ep.X_op = O_constant;
5652 	  ep.X_add_number = 4096;
5653 	  macro_build (&ep, "tltiu", "s,j", sreg, BFD_RELOC_LO16);
5654 	}
5655       else if (mips_opts.isa != ISA_UNKNOWN
5656 	       && mips_opts.isa != ISA_MIPS1)
5657 	macro_build (NULL, "teq", "s,t", sreg, 0);
5658   }
5659 }
5660 
5661 /* OP_ENTRY_EXIT_LIST matcher.  */
5662 
5663 static unsigned int
5664 match_entry_exit_operand (struct mips_arg_info *arg,
5665 			  const struct mips_operand *operand)
5666 {
5667   unsigned int mask;
5668   bool is_exit;
5669 
5670   /* The format is the same for both ENTRY and EXIT, but the constraints
5671      are different.  */
5672   is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5673   mask = (is_exit ? 7 << 3 : 0);
5674   do
5675     {
5676       unsigned int regno1, regno2;
5677       bool is_freg;
5678 
5679       if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5680 	is_freg = false;
5681       else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5682 	is_freg = true;
5683       else
5684 	return false;
5685 
5686       if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5687 	{
5688 	  mask &= ~(7 << 3);
5689 	  mask |= (5 + regno2) << 3;
5690 	}
5691       else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5692 	mask |= (regno2 - 3) << 3;
5693       else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5694 	mask |= (regno2 - 15) << 1;
5695       else if (regno1 == RA && regno2 == RA)
5696 	mask |= 1;
5697       else
5698 	return false;
5699     }
5700   while (match_char (arg, ','));
5701 
5702   insn_insert_operand (arg->insn, operand, mask);
5703   return true;
5704 }
5705 
5706 /* Encode regular MIPS SAVE/RESTORE instruction operands according to
5707    the argument register mask AMASK, the number of static registers
5708    saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5709    respectively, and the frame size FRAME_SIZE.  */
5710 
5711 static unsigned int
5712 mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5713 			  unsigned int ra, unsigned int s0, unsigned int s1,
5714 			  unsigned int frame_size)
5715 {
5716   return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5717 	  | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5718 }
5719 
5720 /* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5721    argument register mask AMASK, the number of static registers saved
5722    NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5723    respectively, and the frame size FRAME_SIZE.  */
5724 
5725 static unsigned int
5726 mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5727 			    unsigned int ra, unsigned int s0, unsigned int s1,
5728 			    unsigned int frame_size)
5729 {
5730   unsigned int args;
5731 
5732   args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5733   if (nsreg || amask || frame_size == 0 || frame_size > 16)
5734     args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5735 	     | ((frame_size & 0xf0) << 16));
5736   return args;
5737 }
5738 
5739 /* OP_SAVE_RESTORE_LIST matcher.  */
5740 
5741 static bool
5742 match_save_restore_list_operand (struct mips_arg_info *arg)
5743 {
5744   unsigned int opcode, args, statics, sregs;
5745   unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
5746   unsigned int arg_mask, ra, s0, s1;
5747   offsetT frame_size;
5748 
5749   opcode = arg->insn->insn_opcode;
5750   frame_size = 0;
5751   num_frame_sizes = 0;
5752   args = 0;
5753   statics = 0;
5754   sregs = 0;
5755   ra = 0;
5756   s0 = 0;
5757   s1 = 0;
5758   do
5759     {
5760       unsigned int regno1, regno2;
5761 
5762       if (arg->token->type == OT_INTEGER)
5763 	{
5764 	  /* Handle the frame size.  */
5765 	  if (!match_const_int (arg, &frame_size))
5766 	    return false;
5767 	  num_frame_sizes += 1;
5768 	}
5769       else
5770 	{
5771 	  if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5772 	    return false;
5773 
5774 	  while (regno1 <= regno2)
5775 	    {
5776 	      if (regno1 >= 4 && regno1 <= 7)
5777 		{
5778 		  if (num_frame_sizes == 0)
5779 		    /* args $a0-$a3 */
5780 		    args |= 1 << (regno1 - 4);
5781 		  else
5782 		    /* statics $a0-$a3 */
5783 		    statics |= 1 << (regno1 - 4);
5784 		}
5785 	      else if (regno1 >= 16 && regno1 <= 23)
5786 		/* $s0-$s7 */
5787 		sregs |= 1 << (regno1 - 16);
5788 	      else if (regno1 == 30)
5789 		/* $s8 */
5790 		sregs |= 1 << 8;
5791 	      else if (regno1 == 31)
5792 		/* Add $ra to insn.  */
5793 		ra = 1;
5794 	      else
5795 		return false;
5796 	      regno1 += 1;
5797 	      if (regno1 == 24)
5798 		regno1 = 30;
5799 	    }
5800 	}
5801     }
5802   while (match_char (arg, ','));
5803 
5804   /* Encode args/statics combination.  */
5805   if (args & statics)
5806     return false;
5807   else if (args == 0xf)
5808     /* All $a0-$a3 are args.  */
5809     arg_mask = MIPS_SVRS_ALL_ARGS;
5810   else if (statics == 0xf)
5811     /* All $a0-$a3 are statics.  */
5812     arg_mask = MIPS_SVRS_ALL_STATICS;
5813   else
5814     {
5815       /* Count arg registers.  */
5816       num_args = 0;
5817       while (args & 0x1)
5818 	{
5819 	  args >>= 1;
5820 	  num_args += 1;
5821 	}
5822       if (args != 0)
5823 	return false;
5824 
5825       /* Count static registers.  */
5826       num_statics = 0;
5827       while (statics & 0x8)
5828 	{
5829 	  statics = (statics << 1) & 0xf;
5830 	  num_statics += 1;
5831 	}
5832       if (statics != 0)
5833 	return false;
5834 
5835       /* Encode args/statics.  */
5836       arg_mask = (num_args << 2) | num_statics;
5837     }
5838 
5839   /* Encode $s0/$s1.  */
5840   if (sregs & (1 << 0))		/* $s0 */
5841     s0 = 1;
5842   if (sregs & (1 << 1))		/* $s1 */
5843     s1 = 1;
5844   sregs >>= 2;
5845 
5846   /* Encode $s2-$s8. */
5847   num_sregs = 0;
5848   while (sregs & 1)
5849     {
5850       sregs >>= 1;
5851       num_sregs += 1;
5852     }
5853   if (sregs != 0)
5854     return false;
5855 
5856   /* Encode frame size.  */
5857   if (num_frame_sizes == 0)
5858     {
5859       set_insn_error (arg->argnum, _("missing frame size"));
5860       return false;
5861     }
5862   if (num_frame_sizes > 1)
5863     {
5864       set_insn_error (arg->argnum, _("frame size specified twice"));
5865       return false;
5866     }
5867   if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5868     {
5869       set_insn_error (arg->argnum, _("invalid frame size"));
5870       return false;
5871     }
5872   frame_size /= 8;
5873 
5874   /* If the branch is itself the target of a branch, we can not swap.
5875      We cheat on this; all we check for is whether there is a label on
5876      this instruction.  If there are any branches to anything other than
5877      a label, users must use .set noreorder.  */
5878   if (seg_info (now_seg)->label_list)
5879     return FALSE;
5880 
5881   /* Finally build the instruction.  */
5882   if (mips_opts.mips16)
5883     opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5884 					  frame_size);
5885   else if (!mips_opts.micromips)
5886     opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5887 					frame_size);
5888   else
5889     abort ();
5890 
5891   arg->insn->insn_opcode = opcode;
5892   return true;
5893 }
5894 
5895 /* OP_MDMX_IMM_REG matcher.  */
5896 
5897 static bool
5898 match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
5899 			    const struct mips_operand *operand)
5900 {
5901   unsigned int regno, uval;
5902   bool is_qh;
5903   const struct mips_opcode *opcode;
5904 
5905   /* The mips_opcode records whether this is an octobyte or quadhalf
5906      instruction.  Start out with that bit in place.  */
5907   opcode = arg->insn->insn_mo;
5908   uval = mips_extract_operand (operand, opcode->match);
5909   is_qh = (uval != 0);
5910 
5911   if (arg->token->type == OT_REG)
5912     {
5913       if ((opcode->membership & INSN_5400)
5914 	  && strcmp (opcode->name, "rzu.ob") == 0)
5915 	{
5916 	  set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5917 			    arg->argnum);
5918 	  return false;
5919 	}
5920 
5921       if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5922 	return false;
5923       ++arg->token;
5924 
5925       /* Check whether this is a vector register or a broadcast of
5926 	 a single element.  */
5927       if (arg->token->type == OT_INTEGER_INDEX)
5928 	{
5929 	  if (arg->token->u.index > (is_qh ? 3 : 7))
5930 	    {
5931 	      set_insn_error (arg->argnum, _("invalid element selector"));
5932 	      return false;
5933 	    }
5934 	  uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5935 	  ++arg->token;
5936 	}
5937       else
5938 	{
5939 	  /* A full vector.  */
5940 	  if ((opcode->membership & INSN_5400)
5941 	      && (strcmp (opcode->name, "sll.ob") == 0
5942 		  || strcmp (opcode->name, "srl.ob") == 0))
5943 	    {
5944 	      set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5945 				arg->argnum);
5946 	      return false;
5947 	    }
5948 
5949 	  if (is_qh)
5950 	    uval |= MDMX_FMTSEL_VEC_QH << 5;
5951 	  else
5952 	    uval |= MDMX_FMTSEL_VEC_OB << 5;
5953 	}
5954       uval |= regno;
5955     }
5956   else
5957     {
5958       offsetT sval;
5959 
5960       if (!match_const_int (arg, &sval))
5961 	return false;
5962       if (sval < 0 || sval > 31)
5963 	{
5964 	  match_out_of_range (arg);
5965 	  return false;
5966 	}
5967       uval |= (sval & 31);
5968       if (is_qh)
5969 	uval |= MDMX_FMTSEL_IMM_QH << 5;
5970       else
5971 	uval |= MDMX_FMTSEL_IMM_OB << 5;
5972     }
5973   insn_insert_operand (arg->insn, operand, uval);
5974   return true;
5975 }
5976 
5977 /* OP_IMM_INDEX matcher.  */
5978 
5979 static bool
5980 match_imm_index_operand (struct mips_arg_info *arg,
5981 			 const struct mips_operand *operand)
5982 {
5983   unsigned int max_val;
5984 
5985   if (arg->token->type != OT_INTEGER_INDEX)
5986     return false;
5987 
5988   max_val = (1 << operand->size) - 1;
5989   if (arg->token->u.index > max_val)
5990     {
5991       match_out_of_range (arg);
5992       return false;
5993     }
5994   insn_insert_operand (arg->insn, operand, arg->token->u.index);
5995   ++arg->token;
5996   return true;
5997 }
5998 
5999 /* OP_REG_INDEX matcher.  */
6000 
6001 static bool
6002 match_reg_index_operand (struct mips_arg_info *arg,
6003 			 const struct mips_operand *operand)
6004 {
6005   unsigned int regno;
6006 
6007   if (arg->token->type != OT_REG_INDEX)
6008     return false;
6009 
6010   if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
6011     return false;
6012 
6013   insn_insert_operand (arg->insn, operand, regno);
6014   ++arg->token;
6015   return true;
6016 }
6017 
6018 /* OP_PC matcher.  */
6019 
6020 static bool
6021 match_pc_operand (struct mips_arg_info *arg)
6022 {
6023   if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
6024     {
6025       ++arg->token;
6026       return true;
6027     }
6028   return false;
6029 }
6030 
6031 /* OP_REG28 matcher.  */
6032 
6033 static bool
6034 match_reg28_operand (struct mips_arg_info *arg)
6035 {
6036   unsigned int regno;
6037 
6038   if (arg->token->type == OT_REG
6039       && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
6040       && regno == GP)
6041     {
6042       ++arg->token;
6043       return true;
6044     }
6045   return false;
6046 }
6047 
6048 /* OP_NON_ZERO_REG matcher.  */
6049 
6050 static bool
6051 match_non_zero_reg_operand (struct mips_arg_info *arg,
6052 			    const struct mips_operand *operand)
6053 {
6054   unsigned int regno;
6055 
6056   if (!match_reg (arg, OP_REG_GP, &regno))
6057     return false;
6058 
6059   if (regno == 0)
6060     {
6061       set_insn_error (arg->argnum, _("the source register must not be $0"));
6062       return false;
6063     }
6064 
6065   arg->last_regno = regno;
6066   insn_insert_operand (arg->insn, operand, regno);
6067   return true;
6068 }
6069 
6070 /* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher.  OTHER_REGNO is the
6071    register that we need to match.  */
6072 
6073 static bool
6074 match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
6075 {
6076   unsigned int regno;
6077 
6078   return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
6079 }
6080 
6081 /* Try to match a floating-point constant from ARG for LI.S or LI.D.
6082    LENGTH is the length of the value in bytes (4 for float, 8 for double)
6083    and USING_GPRS says whether the destination is a GPR rather than an FPR.
6084 
6085    Return the constant in IMM and OFFSET as follows:
6086 
6087    - If the constant should be loaded via memory, set IMM to O_absent and
6088      OFFSET to the memory address.
6089 
6090    - Otherwise, if the constant should be loaded into two 32-bit registers,
6091      set IMM to the O_constant to load into the high register and OFFSET
6092      to the corresponding value for the low register.
6093 
6094    - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6095 
6096    These constants only appear as the last operand in an instruction,
6097    and every instruction that accepts them in any variant accepts them
6098    in all variants.  This means we don't have to worry about backing out
6099    any changes if the instruction does not match.  We just match
6100    unconditionally and report an error if the constant is invalid.  */
6101 
6102 static bool
6103 match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6104 		      expressionS *offset, int length, bool using_gprs)
6105 {
6106   char *p;
6107   segT seg, new_seg;
6108   subsegT subseg;
6109   const char *newname;
6110   unsigned char *data;
6111 
6112   /* Where the constant is placed is based on how the MIPS assembler
6113      does things:
6114 
6115      length == 4 && using_gprs  -- immediate value only
6116      length == 8 && using_gprs  -- .rdata or immediate value
6117      length == 4 && !using_gprs -- .lit4 or immediate value
6118      length == 8 && !using_gprs -- .lit8 or immediate value
6119 
6120      The .lit4 and .lit8 sections are only used if permitted by the
6121      -G argument.  */
6122   if (arg->token->type != OT_FLOAT)
6123     {
6124       set_insn_error (arg->argnum, _("floating-point expression required"));
6125       return false;
6126     }
6127 
6128   gas_assert (arg->token->u.flt.length == length);
6129   data = arg->token->u.flt.data;
6130   ++arg->token;
6131 
6132   /* Handle 32-bit constants for which an immediate value is best.  */
6133   if (length == 4
6134       && (using_gprs
6135 	  || g_switch_value < 4
6136 	  || (data[0] == 0 && data[1] == 0)
6137 	  || (data[2] == 0 && data[3] == 0)))
6138     {
6139       imm->X_op = O_constant;
6140       if (!target_big_endian)
6141 	imm->X_add_number = bfd_getl32 (data);
6142       else
6143 	imm->X_add_number = bfd_getb32 (data);
6144       offset->X_op = O_absent;
6145       return true;
6146     }
6147 
6148   /* Handle 64-bit constants for which an immediate value is best.  */
6149   if (length == 8
6150       && !mips_disable_float_construction
6151       /* Constants can only be constructed in GPRs and copied to FPRs if the
6152 	 GPRs are at least as wide as the FPRs or MTHC1 is available.
6153 	 Unlike most tests for 32-bit floating-point registers this check
6154 	 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6155 	 permit 64-bit moves without MXHC1.
6156 	 Force the constant into memory otherwise.  */
6157       && (using_gprs
6158 	  || GPR_SIZE == 64
6159 	  || ISA_HAS_MXHC1 (mips_opts.isa)
6160 	  || FPR_SIZE == 32)
6161       && ((data[0] == 0 && data[1] == 0)
6162 	  || (data[2] == 0 && data[3] == 0))
6163       && ((data[4] == 0 && data[5] == 0)
6164 	  || (data[6] == 0 && data[7] == 0)))
6165     {
6166       /* The value is simple enough to load with a couple of instructions.
6167 	 If using 32-bit registers, set IMM to the high order 32 bits and
6168 	 OFFSET to the low order 32 bits.  Otherwise, set IMM to the entire
6169 	 64 bit constant.  */
6170       if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
6171 	{
6172 	  imm->X_op = O_constant;
6173 	  offset->X_op = O_constant;
6174 	  if (!target_big_endian)
6175 	    {
6176 	      imm->X_add_number = bfd_getl32 (data + 4);
6177 	      offset->X_add_number = bfd_getl32 (data);
6178 	    }
6179 	  else
6180 	    {
6181 	      imm->X_add_number = bfd_getb32 (data);
6182 	      offset->X_add_number = bfd_getb32 (data + 4);
6183 	    }
6184 	  if (offset->X_add_number == 0)
6185 	    offset->X_op = O_absent;
6186 	}
6187       else
6188 	{
6189 	  imm->X_op = O_constant;
6190 	  if (!target_big_endian)
6191 	    imm->X_add_number = bfd_getl64 (data);
6192 	  else
6193 	    imm->X_add_number = bfd_getb64 (data);
6194 	  offset->X_op = O_absent;
6195 	}
6196       return true;
6197     }
6198 
6199   /* Switch to the right section.  */
6200   seg = now_seg;
6201   subseg = now_subseg;
6202   if (length == 4)
6203     {
6204       gas_assert (!using_gprs && g_switch_value >= 4);
6205       newname = ".lit4";
6206     }
6207   else
6208     {
6209       if (using_gprs || g_switch_value < 8)
6210 	newname = RDATA_SECTION_NAME;
6211       else
6212 	newname = ".lit8";
6213     }
6214 
6215   new_seg = subseg_new (newname, (subsegT) 0);
6216   bfd_set_section_flags (new_seg,
6217 			 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6218   frag_align (length == 4 ? 2 : 3, 0, 0);
6219   if (!startswith (TARGET_OS, "elf"))
6220     record_alignment (new_seg, 4);
6221   else
6222     record_alignment (new_seg, length == 4 ? 2 : 3);
6223   if (seg == now_seg)
6224     as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
6225 
6226   /* Set the argument to the current address in the section.  */
6227   imm->X_op = O_absent;
6228   offset->X_op = O_symbol;
6229   offset->X_add_symbol = symbol_temp_new_now ();
6230   offset->X_add_number = 0;
6231 
6232   /* Put the floating point number into the section.  */
6233   p = frag_more (length);
6234   memcpy (p, data, length);
6235 
6236   /* Switch back to the original section.  */
6237   subseg_set (seg, subseg);
6238   return true;
6239 }
6240 
6241 /* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6242    them.  */
6243 
6244 static bool
6245 match_vu0_suffix_operand (struct mips_arg_info *arg,
6246 			  const struct mips_operand *operand,
6247 			  bool match_p)
6248 {
6249   unsigned int uval;
6250 
6251   /* The operand can be an XYZW mask or a single 2-bit channel index
6252      (with X being 0).  */
6253   gas_assert (operand->size == 2 || operand->size == 4);
6254 
6255   /* The suffix can be omitted when it is already part of the opcode.  */
6256   if (arg->token->type != OT_CHANNELS)
6257     return match_p;
6258 
6259   uval = arg->token->u.channels;
6260   if (operand->size == 2)
6261     {
6262       /* Check that a single bit is set and convert it into a 2-bit index.  */
6263       if ((uval & -uval) != uval)
6264 	return false;
6265       uval = 4 - ffs (uval);
6266     }
6267 
6268   if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6269     return false;
6270 
6271   ++arg->token;
6272   if (!match_p)
6273     insn_insert_operand (arg->insn, operand, uval);
6274   return true;
6275 }
6276 
6277 /* Try to match a token from ARG against OPERAND.  Consume the token
6278    and return true on success, otherwise return false.  */
6279 
6280 static bool
6281 match_operand (struct mips_arg_info *arg,
6282 	       const struct mips_operand *operand)
6283 {
6284   switch (operand->type)
6285     {
6286     case OP_INT:
6287       return match_int_operand (arg, operand);
6288 
6289     case OP_MAPPED_INT:
6290       return match_mapped_int_operand (arg, operand);
6291 
6292     case OP_MSB:
6293       return match_msb_operand (arg, operand);
6294 
6295     case OP_REG:
6296     case OP_OPTIONAL_REG:
6297       return match_reg_operand (arg, operand);
6298 
6299     case OP_REG_PAIR:
6300       return match_reg_pair_operand (arg, operand);
6301 
6302     case OP_PCREL:
6303       return match_pcrel_operand (arg);
6304 
6305     case OP_PERF_REG:
6306       return match_perf_reg_operand (arg, operand);
6307 
6308     case OP_ADDIUSP_INT:
6309       return match_addiusp_operand (arg, operand);
6310 
6311     case OP_CLO_CLZ_DEST:
6312       return match_clo_clz_dest_operand (arg, operand);
6313 
6314     case OP_LWM_SWM_LIST:
6315       return match_lwm_swm_list_operand (arg, operand);
6316 
6317     case OP_ENTRY_EXIT_LIST:
6318       return match_entry_exit_operand (arg, operand);
6319 
6320     case OP_SAVE_RESTORE_LIST:
6321       return match_save_restore_list_operand (arg);
6322 
6323     case OP_MDMX_IMM_REG:
6324       return match_mdmx_imm_reg_operand (arg, operand);
6325 
6326     case OP_REPEAT_DEST_REG:
6327       return match_tied_reg_operand (arg, arg->dest_regno);
6328 
6329     case OP_REPEAT_PREV_REG:
6330       return match_tied_reg_operand (arg, arg->last_regno);
6331 
6332     case OP_PC:
6333       return match_pc_operand (arg);
6334 
6335     case OP_REG28:
6336       return match_reg28_operand (arg);
6337 
6338     case OP_VU0_SUFFIX:
6339       return match_vu0_suffix_operand (arg, operand, false);
6340 
6341     case OP_VU0_MATCH_SUFFIX:
6342       return match_vu0_suffix_operand (arg, operand, true);
6343 
6344     case OP_IMM_INDEX:
6345       return match_imm_index_operand (arg, operand);
6346 
6347     case OP_REG_INDEX:
6348       return match_reg_index_operand (arg, operand);
6349 
6350     case OP_SAME_RS_RT:
6351       return match_same_rs_rt_operand (arg, operand);
6352 
6353     case OP_CHECK_PREV:
6354       return match_check_prev_operand (arg, operand);
6355 
6356     case OP_NON_ZERO_REG:
6357       return match_non_zero_reg_operand (arg, operand);
6358     }
6359   abort ();
6360 }
6361 
6362 /* ARG is the state after successfully matching an instruction.
6363    Issue any queued-up warnings.  */
6364 
6365 static void
6366 check_completed_insn (struct mips_arg_info *arg)
6367 {
6368   if (arg->seen_at)
6369     {
6370       if (AT == ATREG)
6371 	as_warn (_("used $at without \".set noat\""));
6372       else
6373 	as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
6374     }
6375 }
6376 
6377 /* Return true if modifying general-purpose register REG needs a delay.  */
6378 
6379 static bool
6380 reg_needs_delay (unsigned int reg)
6381 {
6382   unsigned long prev_pinfo;
6383 
6384   prev_pinfo = history[0].insn_mo->pinfo;
6385   if (!mips_opts.noreorder
6386       && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
6387 	  || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
6388       && (gpr_write_mask (&history[0]) & (1 << reg)))
6389     return true;
6390 
6391   return false;
6392 }
6393 
6394 /* Classify an instruction according to the FIX_VR4120_* enumeration.
6395    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6396    by VR4120 errata.  */
6397 
6398 static unsigned int
6399 classify_vr4120_insn (const char *name)
6400 {
6401   if (startswith (name, "macc"))
6402     return FIX_VR4120_MACC;
6403   if (startswith (name, "dmacc"))
6404     return FIX_VR4120_DMACC;
6405   if (startswith (name, "mult"))
6406     return FIX_VR4120_MULT;
6407   if (startswith (name, "dmult"))
6408     return FIX_VR4120_DMULT;
6409   if (strstr (name, "div"))
6410     return FIX_VR4120_DIV;
6411   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6412     return FIX_VR4120_MTHILO;
6413   return NUM_FIX_VR4120_CLASSES;
6414 }
6415 
6416 #define INSN_ERET	0x42000018
6417 #define INSN_DERET	0x4200001f
6418 #define INSN_DMULT	0x1c
6419 #define INSN_DMULTU	0x1d
6420 
6421 /* Return the number of instructions that must separate INSN1 and INSN2,
6422    where INSN1 is the earlier instruction.  Return the worst-case value
6423    for any INSN2 if INSN2 is null.  */
6424 
6425 static unsigned int
6426 insns_between (const struct mips_cl_insn *insn1,
6427 	       const struct mips_cl_insn *insn2)
6428 {
6429   unsigned long pinfo1, pinfo2;
6430   unsigned int mask;
6431 
6432   /* If INFO2 is null, pessimistically assume that all flags are set for
6433      the second instruction.  */
6434   pinfo1 = insn1->insn_mo->pinfo;
6435   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
6436 
6437   /* For most targets, write-after-read dependencies on the HI and LO
6438      registers must be separated by at least two instructions.  */
6439   if (!hilo_interlocks)
6440     {
6441       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6442 	return 2;
6443       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6444 	return 2;
6445     }
6446 
6447   /* If we're working around r7000 errata, there must be two instructions
6448      between an mfhi or mflo and any instruction that uses the result.  */
6449   if (mips_7000_hilo_fix
6450       && !mips_opts.micromips
6451       && MF_HILO_INSN (pinfo1)
6452       && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
6453     return 2;
6454 
6455   /* If we're working around 24K errata, one instruction is required
6456      if an ERET or DERET is followed by a branch instruction.  */
6457   if (mips_fix_24k && !mips_opts.micromips)
6458     {
6459       if (insn1->insn_opcode == INSN_ERET
6460 	  || insn1->insn_opcode == INSN_DERET)
6461 	{
6462 	  if (insn2 == NULL
6463 	      || insn2->insn_opcode == INSN_ERET
6464 	      || insn2->insn_opcode == INSN_DERET
6465 	      || delayed_branch_p (insn2))
6466 	    return 1;
6467 	}
6468     }
6469 
6470   /* If we're working around PMC RM7000 errata, there must be three
6471      nops between a dmult and a load instruction.  */
6472   if (mips_fix_rm7000 && !mips_opts.micromips)
6473     {
6474       if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6475 	  || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6476 	{
6477 	  if (pinfo2 & INSN_LOAD_MEMORY)
6478 	   return 3;
6479 	}
6480     }
6481 
6482   /* If working around VR4120 errata, check for combinations that need
6483      a single intervening instruction.  */
6484   if (mips_fix_vr4120 && !mips_opts.micromips)
6485     {
6486       unsigned int class1, class2;
6487 
6488       class1 = classify_vr4120_insn (insn1->insn_mo->name);
6489       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
6490 	{
6491 	  if (insn2 == NULL)
6492 	    return 1;
6493 	  class2 = classify_vr4120_insn (insn2->insn_mo->name);
6494 	  if (vr4120_conflicts[class1] & (1 << class2))
6495 	    return 1;
6496 	}
6497     }
6498 
6499   if (!HAVE_CODE_COMPRESSION)
6500     {
6501       /* Check for GPR or coprocessor load delays.  All such delays
6502 	 are on the RT register.  */
6503       /* Itbl support may require additional care here.  */
6504       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
6505 	  || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
6506 	{
6507 	  if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
6508 	    return 1;
6509 	}
6510 
6511       /* Check for generic coprocessor hazards.
6512 
6513 	 This case is not handled very well.  There is no special
6514 	 knowledge of CP0 handling, and the coprocessors other than
6515 	 the floating point unit are not distinguished at all.  */
6516       /* Itbl support may require additional care here. FIXME!
6517 	 Need to modify this to include knowledge about
6518 	 user specified delays!  */
6519       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
6520 	       || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6521 	{
6522 	  /* Handle cases where INSN1 writes to a known general coprocessor
6523 	     register.  There must be a one instruction delay before INSN2
6524 	     if INSN2 reads that register, otherwise no delay is needed.  */
6525 	  mask = fpr_write_mask (insn1);
6526 	  if (mask != 0)
6527 	    {
6528 	      if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
6529 		return 1;
6530 	    }
6531 	  else
6532 	    {
6533 	      /* Read-after-write dependencies on the control registers
6534 		 require a two-instruction gap.  */
6535 	      if ((pinfo1 & INSN_WRITE_COND_CODE)
6536 		  && (pinfo2 & INSN_READ_COND_CODE))
6537 		return 2;
6538 
6539 	      /* We don't know exactly what INSN1 does.  If INSN2 is
6540 		 also a coprocessor instruction, assume there must be
6541 		 a one instruction gap.  */
6542 	      if (pinfo2 & INSN_COP)
6543 		return 1;
6544 	    }
6545 	}
6546 
6547       /* Check for read-after-write dependencies on the coprocessor
6548 	 control registers in cases where INSN1 does not need a general
6549 	 coprocessor delay.  This means that INSN1 is a floating point
6550 	 comparison instruction.  */
6551       /* Itbl support may require additional care here.  */
6552       else if (!cop_interlocks
6553 	       && (pinfo1 & INSN_WRITE_COND_CODE)
6554 	       && (pinfo2 & INSN_READ_COND_CODE))
6555 	return 1;
6556     }
6557 
6558   /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6559      CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6560      and pause.  */
6561   if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6562       && ((pinfo2 & INSN_NO_DELAY_SLOT)
6563 	  || (insn2 && delayed_branch_p (insn2))))
6564     return 1;
6565 
6566   return 0;
6567 }
6568 
6569 /* Return the number of nops that would be needed to work around the
6570    VR4130 mflo/mfhi errata if instruction INSN immediately followed
6571    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
6572    that are contained within the first IGNORE instructions of HIST.  */
6573 
6574 static int
6575 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
6576 		 const struct mips_cl_insn *insn)
6577 {
6578   int i, j;
6579   unsigned int mask;
6580 
6581   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
6582      are not affected by the errata.  */
6583   if (insn != 0
6584       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6585 	  || strcmp (insn->insn_mo->name, "mtlo") == 0
6586 	  || strcmp (insn->insn_mo->name, "mthi") == 0))
6587     return 0;
6588 
6589   /* Search for the first MFLO or MFHI.  */
6590   for (i = 0; i < MAX_VR4130_NOPS; i++)
6591     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
6592       {
6593 	/* Extract the destination register.  */
6594 	mask = gpr_write_mask (&hist[i]);
6595 
6596 	/* No nops are needed if INSN reads that register.  */
6597 	if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
6598 	  return 0;
6599 
6600 	/* ...or if any of the intervening instructions do.  */
6601 	for (j = 0; j < i; j++)
6602 	  if (gpr_read_mask (&hist[j]) & mask)
6603 	    return 0;
6604 
6605 	if (i >= ignore)
6606 	  return MAX_VR4130_NOPS - i;
6607       }
6608   return 0;
6609 }
6610 
6611 #define BASE_REG_EQ(INSN1, INSN2)	\
6612   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS)	\
6613       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6614 
6615 /* Return the minimum alignment for this store instruction.  */
6616 
6617 static int
6618 fix_24k_align_to (const struct mips_opcode *mo)
6619 {
6620   if (strcmp (mo->name, "sh") == 0)
6621     return 2;
6622 
6623   if (strcmp (mo->name, "swc1") == 0
6624       || strcmp (mo->name, "swc2") == 0
6625       || strcmp (mo->name, "sw") == 0
6626       || strcmp (mo->name, "sc") == 0
6627       || strcmp (mo->name, "s.s") == 0)
6628     return 4;
6629 
6630   if (strcmp (mo->name, "sdc1") == 0
6631       || strcmp (mo->name, "sdc2") == 0
6632       || strcmp (mo->name, "s.d") == 0)
6633     return 8;
6634 
6635   /* sb, swl, swr */
6636   return 1;
6637 }
6638 
6639 struct fix_24k_store_info
6640   {
6641     /* Immediate offset, if any, for this store instruction.  */
6642     short off;
6643     /* Alignment required by this store instruction.  */
6644     int align_to;
6645     /* True for register offsets.  */
6646     int register_offset;
6647   };
6648 
6649 /* Comparison function used by qsort.  */
6650 
6651 static int
6652 fix_24k_sort (const void *a, const void *b)
6653 {
6654   const struct fix_24k_store_info *pos1 = a;
6655   const struct fix_24k_store_info *pos2 = b;
6656 
6657   return (pos1->off - pos2->off);
6658 }
6659 
6660 /* INSN is a store instruction.  Try to record the store information
6661    in STINFO.  Return false if the information isn't known.  */
6662 
6663 static bool
6664 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
6665 			   const struct mips_cl_insn *insn)
6666 {
6667   /* The instruction must have a known offset.  */
6668   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6669     return false;
6670 
6671   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6672   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6673   return true;
6674 }
6675 
6676 /* Return the number of nops that would be needed to work around the 24k
6677    "lost data on stores during refill" errata if instruction INSN
6678    immediately followed the 2 instructions described by HIST.
6679    Ignore hazards that are contained within the first IGNORE
6680    instructions of HIST.
6681 
6682    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6683    for the data cache refills and store data. The following describes
6684    the scenario where the store data could be lost.
6685 
6686    * A data cache miss, due to either a load or a store, causing fill
6687      data to be supplied by the memory subsystem
6688    * The first three doublewords of fill data are returned and written
6689      into the cache
6690    * A sequence of four stores occurs in consecutive cycles around the
6691      final doubleword of the fill:
6692    * Store A
6693    * Store B
6694    * Store C
6695    * Zero, One or more instructions
6696    * Store D
6697 
6698    The four stores A-D must be to different doublewords of the line that
6699    is being filled. The fourth instruction in the sequence above permits
6700    the fill of the final doubleword to be transferred from the FSB into
6701    the cache. In the sequence above, the stores may be either integer
6702    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6703    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6704    different doublewords on the line. If the floating point unit is
6705    running in 1:2 mode, it is not possible to create the sequence above
6706    using only floating point store instructions.
6707 
6708    In this case, the cache line being filled is incorrectly marked
6709    invalid, thereby losing the data from any store to the line that
6710    occurs between the original miss and the completion of the five
6711    cycle sequence shown above.
6712 
6713    The workarounds are:
6714 
6715    * Run the data cache in write-through mode.
6716    * Insert a non-store instruction between
6717      Store A and Store B or Store B and Store C.  */
6718 
6719 static int
6720 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
6721 	      const struct mips_cl_insn *insn)
6722 {
6723   struct fix_24k_store_info pos[3];
6724   int align, i, base_offset;
6725 
6726   if (ignore >= 2)
6727     return 0;
6728 
6729   /* If the previous instruction wasn't a store, there's nothing to
6730      worry about.  */
6731   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6732     return 0;
6733 
6734   /* If the instructions after the previous one are unknown, we have
6735      to assume the worst.  */
6736   if (!insn)
6737     return 1;
6738 
6739   /* Check whether we are dealing with three consecutive stores.  */
6740   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6741       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6742     return 0;
6743 
6744   /* If we don't know the relationship between the store addresses,
6745      assume the worst.  */
6746   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
6747       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6748     return 1;
6749 
6750   if (!fix_24k_record_store_info (&pos[0], insn)
6751       || !fix_24k_record_store_info (&pos[1], &hist[0])
6752       || !fix_24k_record_store_info (&pos[2], &hist[1]))
6753     return 1;
6754 
6755   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6756 
6757   /* Pick a value of ALIGN and X such that all offsets are adjusted by
6758      X bytes and such that the base register + X is known to be aligned
6759      to align bytes.  */
6760 
6761   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6762     align = 8;
6763   else
6764     {
6765       align = pos[0].align_to;
6766       base_offset = pos[0].off;
6767       for (i = 1; i < 3; i++)
6768 	if (align < pos[i].align_to)
6769 	  {
6770 	    align = pos[i].align_to;
6771 	    base_offset = pos[i].off;
6772 	  }
6773       for (i = 0; i < 3; i++)
6774 	pos[i].off -= base_offset;
6775     }
6776 
6777   pos[0].off &= ~align + 1;
6778   pos[1].off &= ~align + 1;
6779   pos[2].off &= ~align + 1;
6780 
6781   /* If any two stores write to the same chunk, they also write to the
6782      same doubleword.  The offsets are still sorted at this point.  */
6783   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6784     return 0;
6785 
6786   /* A range of at least 9 bytes is needed for the stores to be in
6787      non-overlapping doublewords.  */
6788   if (pos[2].off - pos[0].off <= 8)
6789     return 0;
6790 
6791   if (pos[2].off - pos[1].off >= 24
6792       || pos[1].off - pos[0].off >= 24
6793       || pos[2].off - pos[0].off >= 32)
6794     return 0;
6795 
6796   return 1;
6797 }
6798 
6799 /* Return the number of nops that would be needed if instruction INSN
6800    immediately followed the MAX_NOPS instructions given by HIST,
6801    where HIST[0] is the most recent instruction.  Ignore hazards
6802    between INSN and the first IGNORE instructions in HIST.
6803 
6804    If INSN is null, return the worse-case number of nops for any
6805    instruction.  */
6806 
6807 static int
6808 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
6809 	       const struct mips_cl_insn *insn)
6810 {
6811   int i, nops, tmp_nops;
6812 
6813   nops = 0;
6814   for (i = ignore; i < MAX_DELAY_NOPS; i++)
6815     {
6816       tmp_nops = insns_between (hist + i, insn) - i;
6817       if (tmp_nops > nops)
6818 	nops = tmp_nops;
6819     }
6820 
6821   if (mips_fix_vr4130 && !mips_opts.micromips)
6822     {
6823       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
6824       if (tmp_nops > nops)
6825 	nops = tmp_nops;
6826     }
6827 
6828   if (mips_fix_24k && !mips_opts.micromips)
6829     {
6830       tmp_nops = nops_for_24k (ignore, hist, insn);
6831       if (tmp_nops > nops)
6832 	nops = tmp_nops;
6833     }
6834 
6835   return nops;
6836 }
6837 
6838 /* The variable arguments provide NUM_INSNS extra instructions that
6839    might be added to HIST.  Return the largest number of nops that
6840    would be needed after the extended sequence, ignoring hazards
6841    in the first IGNORE instructions.  */
6842 
6843 static int
6844 nops_for_sequence (int num_insns, int ignore,
6845 		   const struct mips_cl_insn *hist, ...)
6846 {
6847   va_list args;
6848   struct mips_cl_insn buffer[MAX_NOPS];
6849   struct mips_cl_insn *cursor;
6850   int nops;
6851 
6852   va_start (args, hist);
6853   cursor = buffer + num_insns;
6854   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
6855   while (cursor > buffer)
6856     *--cursor = *va_arg (args, const struct mips_cl_insn *);
6857 
6858   nops = nops_for_insn (ignore, buffer, NULL);
6859   va_end (args);
6860   return nops;
6861 }
6862 
6863 /* Like nops_for_insn, but if INSN is a branch, take into account the
6864    worst-case delay for the branch target.  */
6865 
6866 static int
6867 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
6868 			 const struct mips_cl_insn *insn)
6869 {
6870   int nops, tmp_nops;
6871 
6872   nops = nops_for_insn (ignore, hist, insn);
6873   if (delayed_branch_p (insn))
6874     {
6875       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
6876 				    hist, insn, get_delay_slot_nop (insn));
6877       if (tmp_nops > nops)
6878 	nops = tmp_nops;
6879     }
6880   else if (compact_branch_p (insn))
6881     {
6882       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
6883       if (tmp_nops > nops)
6884 	nops = tmp_nops;
6885     }
6886   return nops;
6887 }
6888 
6889 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
6890 
6891 static void
6892 fix_loongson2f_nop (struct mips_cl_insn * ip)
6893 {
6894   gas_assert (!HAVE_CODE_COMPRESSION);
6895   if (strcmp (ip->insn_mo->name, "nop") == 0)
6896     ip->insn_opcode = LOONGSON2F_NOP_INSN;
6897 }
6898 
6899 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6900                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
6901 
6902 static void
6903 fix_loongson2f_jump (struct mips_cl_insn * ip)
6904 {
6905   gas_assert (!HAVE_CODE_COMPRESSION);
6906   if (strcmp (ip->insn_mo->name, "j") == 0
6907       || strcmp (ip->insn_mo->name, "jr") == 0
6908       || strcmp (ip->insn_mo->name, "jalr") == 0)
6909     {
6910       int sreg;
6911       expressionS ep;
6912 
6913       if (! mips_opts.at)
6914         return;
6915 
6916       sreg = EXTRACT_OPERAND (0, RS, *ip);
6917       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6918         return;
6919 
6920       ep.X_op = O_constant;
6921       ep.X_add_number = 0xcfff0000;
6922       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6923       ep.X_add_number = 0xffff;
6924       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6925       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6926     }
6927 }
6928 
6929 static void
6930 fix_loongson2f (struct mips_cl_insn * ip)
6931 {
6932   if (mips_fix_loongson2f_nop)
6933     fix_loongson2f_nop (ip);
6934 
6935   if (mips_fix_loongson2f_jump)
6936     fix_loongson2f_jump (ip);
6937 }
6938 
6939 static bool
6940 has_label_name (const char *arr[], size_t len ,const char *s)
6941 {
6942   unsigned long i;
6943   for (i = 0; i < len; i++)
6944     {
6945       if (!arr[i])
6946 	return false;
6947       if (streq (arr[i], s))
6948 	return true;
6949     }
6950   return false;
6951 }
6952 
6953 /* Fix loongson3 llsc errata: Insert sync before ll/lld.  */
6954 
6955 static void
6956 fix_loongson3_llsc (struct mips_cl_insn * ip)
6957 {
6958   gas_assert (!HAVE_CODE_COMPRESSION);
6959 
6960   /* If is an local label and the insn is not sync,
6961      look forward that whether an branch between ll/sc jump to here
6962      if so, insert a sync.  */
6963   if (seg_info (now_seg)->label_list
6964       && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6965       && (strcmp (ip->insn_mo->name, "sync") != 0))
6966     {
6967       unsigned long i;
6968       valueT label_value;
6969       const char *label_names[MAX_LABELS_SAME];
6970       const char *label_name;
6971 
6972       label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6973       label_names[0] = label_name;
6974       struct insn_label_list *llist = seg_info (now_seg)->label_list;
6975       label_value = S_GET_VALUE (llist->label);
6976 
6977       for (i = 1; i < MAX_LABELS_SAME; i++)
6978 	{
6979 	  llist = llist->next;
6980 	  if (!llist)
6981 	    break;
6982 	  if (S_GET_VALUE (llist->label) == label_value)
6983 	    label_names[i] = S_GET_NAME (llist->label);
6984 	  else
6985 	    break;
6986 	}
6987       for (; i < MAX_LABELS_SAME; i++)
6988 	label_names[i] = NULL;
6989 
6990       unsigned long lookback = ARRAY_SIZE (history);
6991       for (i = 0; i < lookback; i++)
6992 	{
6993 	  if (streq (history[i].insn_mo->name, "ll")
6994 	      || streq (history[i].insn_mo->name, "lld"))
6995 	    break;
6996 
6997 	  if (streq (history[i].insn_mo->name, "sc")
6998 	      || streq (history[i].insn_mo->name, "scd"))
6999 	    {
7000 	      unsigned long j;
7001 
7002 	      for (j = i + 1; j < lookback; j++)
7003 		{
7004 		  if (streq (history[i].insn_mo->name, "ll")
7005 		      || streq (history[i].insn_mo->name, "lld"))
7006 		    break;
7007 
7008 		  if (delayed_branch_p (&history[j]))
7009 		    {
7010 		      if (has_label_name (label_names,
7011 					  MAX_LABELS_SAME,
7012 					  history[j].target))
7013 			{
7014 			  add_fixed_insn (&sync_insn);
7015 			  insert_into_history (0, 1, &sync_insn);
7016 			  i = lookback;
7017 			  break;
7018 			}
7019 		    }
7020 		}
7021 	    }
7022 	}
7023     }
7024   /* If we find a sc, we look forward to look for an branch insn,
7025      and see whether it jump back and out of ll/sc.  */
7026   else if (streq (ip->insn_mo->name, "sc") || streq (ip->insn_mo->name, "scd"))
7027     {
7028       unsigned long lookback = ARRAY_SIZE (history) - 1;
7029       unsigned long i;
7030 
7031       for (i = 0; i < lookback; i++)
7032 	{
7033 	  if (streq (history[i].insn_mo->name, "ll")
7034 	      || streq (history[i].insn_mo->name, "lld"))
7035 	    break;
7036 
7037 	  if (delayed_branch_p (&history[i]))
7038 	    {
7039 	      unsigned long j;
7040 
7041 	      for (j = i + 1; j < lookback; j++)
7042 		{
7043 		  if (streq (history[j].insn_mo->name, "ll")
7044 		      || streq (history[i].insn_mo->name, "lld"))
7045 		    break;
7046 		}
7047 
7048 	      for (; j < lookback; j++)
7049 		{
7050 		  if (history[j].label[0] != '\0'
7051 		      && streq (history[j].label, history[i].target)
7052 		      && strcmp (history[j+1].insn_mo->name, "sync") != 0)
7053 		    {
7054 		      add_fixed_insn (&sync_insn);
7055 		      insert_into_history (++j, 1, &sync_insn);
7056 		    }
7057 		}
7058 	    }
7059 	}
7060     }
7061 
7062   /* Skip if there is a sync before ll/lld.  */
7063   if ((strcmp (ip->insn_mo->name, "ll") == 0
7064        || strcmp (ip->insn_mo->name, "lld") == 0)
7065       && (strcmp (history[0].insn_mo->name, "sync") != 0))
7066     {
7067       add_fixed_insn (&sync_insn);
7068       insert_into_history (0, 1, &sync_insn);
7069     }
7070 }
7071 
7072 /* IP is a branch that has a delay slot, and we need to fill it
7073    automatically.   Return true if we can do that by swapping IP
7074    with the previous instruction.
7075    ADDRESS_EXPR is an operand of the instruction to be used with
7076    RELOC_TYPE.  */
7077 
7078 static bool
7079 can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
7080 		   bfd_reloc_code_real_type *reloc_type)
7081 {
7082   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
7083   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
7084   unsigned int fpr_read, prev_fpr_write;
7085 
7086   /* -O2 and above is required for this optimization.  */
7087   if (mips_optimize < 2)
7088     return false;
7089 
7090   /* If we have seen .set volatile or .set nomove, don't optimize.  */
7091   if (mips_opts.nomove)
7092     return false;
7093 
7094   /* We can't swap if the previous instruction's position is fixed.  */
7095   if (history[0].fixed_p)
7096     return false;
7097 
7098   /* If the previous previous insn was in a .set noreorder, we can't
7099      swap.  Actually, the MIPS assembler will swap in this situation.
7100      However, gcc configured -with-gnu-as will generate code like
7101 
7102 	.set	noreorder
7103 	lw	$4,XXX
7104 	.set	reorder
7105 	INSN
7106 	bne	$4,$0,foo
7107 
7108      in which we can not swap the bne and INSN.  If gcc is not configured
7109      -with-gnu-as, it does not output the .set pseudo-ops.  */
7110   if (history[1].noreorder_p)
7111     return false;
7112 
7113   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7114      This means that the previous instruction was a 4-byte one anyhow.  */
7115   if (mips_opts.mips16 && history[0].fixp[0])
7116     return false;
7117 
7118   if (mips_fix_loongson2f)
7119     fix_loongson2f (ip);
7120   if (mips_trap_zero_jump)
7121     trap_zero_jump (ip);
7122 
7123   /* If the branch is itself the target of a branch, we can not swap.
7124      We cheat on this; all we check for is whether there is a label on
7125      this instruction.  If there are any branches to anything other than
7126      a label, users must use .set noreorder.  */
7127   if (seg_info (now_seg)->label_list)
7128     return false;
7129 
7130   /* If the previous instruction is in a variant frag other than this
7131      branch's one, we cannot do the swap.  This does not apply to
7132      MIPS16 code, which uses variant frags for different purposes.  */
7133   if (!mips_opts.mips16
7134       && history[0].frag
7135       && history[0].frag->fr_type == rs_machine_dependent)
7136     return false;
7137 
7138   /* We do not swap with instructions that cannot architecturally
7139      be placed in a branch delay slot, such as SYNC or ERET.  We
7140      also refrain from swapping with a trap instruction, since it
7141      complicates trap handlers to have the trap instruction be in
7142      a delay slot.  */
7143   prev_pinfo = history[0].insn_mo->pinfo;
7144   if (prev_pinfo & INSN_NO_DELAY_SLOT)
7145     return false;
7146 
7147   /* Check for conflicts between the branch and the instructions
7148      before the candidate delay slot.  */
7149   if (nops_for_insn (0, history + 1, ip) > 0)
7150     return false;
7151 
7152   /* Check for conflicts between the swapped sequence and the
7153      target of the branch.  */
7154   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
7155     return false;
7156 
7157   /* If the branch reads a register that the previous
7158      instruction sets, we can not swap.  */
7159   gpr_read = gpr_read_mask (ip);
7160   prev_gpr_write = gpr_write_mask (&history[0]);
7161   if (gpr_read & prev_gpr_write)
7162     return false;
7163 
7164   fpr_read = fpr_read_mask (ip);
7165   prev_fpr_write = fpr_write_mask (&history[0]);
7166   if (fpr_read & prev_fpr_write)
7167     return false;
7168 
7169   /* If the branch writes a register that the previous
7170      instruction sets, we can not swap.  */
7171   gpr_write = gpr_write_mask (ip);
7172   if (gpr_write & prev_gpr_write)
7173     return false;
7174 
7175   /* If the branch writes a register that the previous
7176      instruction reads, we can not swap.  */
7177   prev_gpr_read = gpr_read_mask (&history[0]);
7178   if (gpr_write & prev_gpr_read)
7179     return false;
7180 
7181   /* If one instruction sets a condition code and the
7182      other one uses a condition code, we can not swap.  */
7183   pinfo = ip->insn_mo->pinfo;
7184   if ((pinfo & INSN_READ_COND_CODE)
7185       && (prev_pinfo & INSN_WRITE_COND_CODE))
7186     return false;
7187   if ((pinfo & INSN_WRITE_COND_CODE)
7188       && (prev_pinfo & INSN_READ_COND_CODE))
7189     return false;
7190 
7191   /* If the previous instruction uses the PC, we can not swap.  */
7192   prev_pinfo2 = history[0].insn_mo->pinfo2;
7193   if (prev_pinfo2 & INSN2_READ_PC)
7194     return false;
7195 
7196   /* If the previous instruction has an incorrect size for a fixed
7197      branch delay slot in microMIPS mode, we cannot swap.  */
7198   pinfo2 = ip->insn_mo->pinfo2;
7199   if (mips_opts.micromips
7200       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7201       && insn_length (history) != 2)
7202     return false;
7203   if (mips_opts.micromips
7204       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7205       && insn_length (history) != 4)
7206     return false;
7207 
7208   /* On the R5900 short loops need to be fixed by inserting a NOP in the
7209      branch delay slot.
7210 
7211      The short loop bug under certain conditions causes loops to execute
7212      only once or twice.  We must ensure that the assembler never
7213      generates loops that satisfy all of the following conditions:
7214 
7215      - a loop consists of less than or equal to six instructions
7216        (including the branch delay slot);
7217      - a loop contains only one conditional branch instruction at the end
7218        of the loop;
7219      - a loop does not contain any other branch or jump instructions;
7220      - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7221 
7222      We need to do this because of a hardware bug in the R5900 chip.  */
7223   if (mips_fix_r5900
7224       /* Check if instruction has a parameter, ignore "j $31". */
7225       && (address_expr != NULL)
7226       /* Parameter must be 16 bit. */
7227       && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7228       /* Branch to same segment. */
7229       && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
7230       /* Branch to same code fragment. */
7231       && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
7232       /* Can only calculate branch offset if value is known. */
7233       && symbol_constant_p (address_expr->X_add_symbol)
7234       /* Check if branch is really conditional. */
7235       && !((ip->insn_opcode & 0xffff0000) == 0x10000000   /* beq $0,$0 */
7236 	|| (ip->insn_opcode & 0xffff0000) == 0x04010000   /* bgez $0 */
7237 	|| (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7238     {
7239       int distance;
7240       /* Check if loop is shorter than or equal to 6 instructions
7241          including branch and delay slot.  */
7242       distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
7243       if (distance <= 20)
7244         {
7245           int i;
7246           int rv;
7247 
7248           rv = false;
7249           /* When the loop includes branches or jumps,
7250              it is not a short loop. */
7251           for (i = 0; i < (distance / 4); i++)
7252             {
7253               if ((history[i].cleared_p)
7254                   || delayed_branch_p (&history[i]))
7255                 {
7256                   rv = true;
7257                   break;
7258                 }
7259             }
7260           if (!rv)
7261             {
7262               /* Insert nop after branch to fix short loop. */
7263               return false;
7264             }
7265         }
7266     }
7267 
7268   return true;
7269 }
7270 
7271 /* Fix jump through register issue on loongson2f processor for kernel code:
7272    force a BTB clear before the jump to prevent it from being incorrectly
7273    prefetched by the branch prediction engine. */
7274 
7275 static void
7276 macro_build_jrpatch (expressionS *ep, unsigned int sreg)
7277 {
7278   if (!mips_fix_loongson2f_btb)
7279     return;
7280 
7281   if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == AT)
7282     return;
7283 
7284   if (!mips_opts.at)
7285     {
7286       as_warn (_("unable to apply loongson2f BTB workaround when .set noat"));
7287       return;
7288     }
7289 
7290   /* li $at, COP_0_BTB_CLEAR | COP_0_RAS_DISABLE */
7291   ep->X_op = O_constant;
7292   ep->X_add_number = 3;
7293   macro_build (ep, "ori", "t,r,i", AT, ZERO, BFD_RELOC_LO16);
7294 
7295   /* dmtc0 $at, COP_0_DIAG */
7296   macro_build (NULL, "dmtc0", "t,G", AT, 22);
7297 
7298   /* Hide these two instructions to avoid getting a ``macro expanded into
7299      multiple instructions'' warning. */
7300   if (mips_relax.sequence != 2) {
7301     mips_macro_warning.sizes[0] -= 2 * 4;
7302     mips_macro_warning.insns[0] -= 2;
7303   }
7304   if (mips_relax.sequence != 1) {
7305     mips_macro_warning.sizes[1] -= 2 * 4;
7306     mips_macro_warning.insns[1] -= 2;
7307   }
7308 }
7309 
7310 /* Decide how we should add IP to the instruction stream.
7311    ADDRESS_EXPR is an operand of the instruction to be used with
7312    RELOC_TYPE.  */
7313 
7314 static enum append_method
7315 get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
7316 		   bfd_reloc_code_real_type *reloc_type)
7317 {
7318   /* The relaxed version of a macro sequence must be inherently
7319      hazard-free.  */
7320   if (mips_relax.sequence == 2)
7321     return APPEND_ADD;
7322 
7323   /* We must not dabble with instructions in a ".set noreorder" block.  */
7324   if (mips_opts.noreorder)
7325     return APPEND_ADD;
7326 
7327   /* Otherwise, it's our responsibility to fill branch delay slots.  */
7328   if (delayed_branch_p (ip))
7329     {
7330       if (!branch_likely_p (ip)
7331 	  && can_swap_branch_p (ip, address_expr, reloc_type))
7332 	return APPEND_SWAP;
7333 
7334       if (mips_opts.mips16
7335 	  && ISA_SUPPORTS_MIPS16E
7336 	  && gpr_read_mask (ip) != 0)
7337 	return APPEND_ADD_COMPACT;
7338 
7339       if (mips_opts.micromips
7340 	  && ((ip->insn_opcode & 0xffe0) == 0x4580
7341 	      || (!forced_insn_length
7342 		  && ((ip->insn_opcode & 0xfc00) == 0xcc00
7343 		      || (ip->insn_opcode & 0xdc00) == 0x8c00))
7344 	      || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7345 	      || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7346 	return APPEND_ADD_COMPACT;
7347 
7348       return APPEND_ADD_WITH_NOP;
7349     }
7350 
7351   return APPEND_ADD;
7352 }
7353 
7354 /* IP is an instruction whose opcode we have just changed, END points
7355    to the end of the opcode table processed.  Point IP->insn_mo to the
7356    new opcode's definition.  */
7357 
7358 static void
7359 find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
7360 {
7361   const struct mips_opcode *mo;
7362 
7363   for (mo = ip->insn_mo; mo < end; mo++)
7364     if (mo->pinfo != INSN_MACRO
7365 	&& (ip->insn_opcode & mo->mask) == mo->match)
7366       {
7367 	ip->insn_mo = mo;
7368 	return;
7369       }
7370   abort ();
7371 }
7372 
7373 /* IP is a MIPS16 instruction whose opcode we have just changed.
7374    Point IP->insn_mo to the new opcode's definition.  */
7375 
7376 static void
7377 find_altered_mips16_opcode (struct mips_cl_insn *ip)
7378 {
7379   find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7380 }
7381 
7382 /* IP is a microMIPS instruction whose opcode we have just changed.
7383    Point IP->insn_mo to the new opcode's definition.  */
7384 
7385 static void
7386 find_altered_micromips_opcode (struct mips_cl_insn *ip)
7387 {
7388   find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7389 }
7390 
7391 /* For microMIPS macros, we need to generate a local number label
7392    as the target of branches.  */
7393 #define MICROMIPS_LABEL_CHAR		'\037'
7394 static unsigned long micromips_target_label;
7395 static char micromips_target_name[32];
7396 
7397 static char *
7398 micromips_label_name (void)
7399 {
7400   char *p = micromips_target_name;
7401   char symbol_name_temporary[24];
7402   unsigned long l;
7403   int i;
7404 
7405   if (*p)
7406     return p;
7407 
7408   i = 0;
7409   l = micromips_target_label;
7410 #ifdef LOCAL_LABEL_PREFIX
7411   *p++ = LOCAL_LABEL_PREFIX;
7412 #endif
7413   *p++ = 'L';
7414   *p++ = MICROMIPS_LABEL_CHAR;
7415   do
7416     {
7417       symbol_name_temporary[i++] = l % 10 + '0';
7418       l /= 10;
7419     }
7420   while (l != 0);
7421   while (i > 0)
7422     *p++ = symbol_name_temporary[--i];
7423   *p = '\0';
7424 
7425   return micromips_target_name;
7426 }
7427 
7428 static void
7429 micromips_label_expr (expressionS *label_expr)
7430 {
7431   label_expr->X_op = O_symbol;
7432   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7433   label_expr->X_add_number = 0;
7434 }
7435 
7436 static void
7437 micromips_label_inc (void)
7438 {
7439   micromips_target_label++;
7440   *micromips_target_name = '\0';
7441 }
7442 
7443 static void
7444 micromips_add_label (void)
7445 {
7446   symbolS *s;
7447 
7448   s = colon (micromips_label_name ());
7449   micromips_label_inc ();
7450   S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
7451 }
7452 
7453 /* If assembling microMIPS code, then return the microMIPS reloc
7454    corresponding to the requested one if any.  Otherwise return
7455    the reloc unchanged.  */
7456 
7457 static bfd_reloc_code_real_type
7458 micromips_map_reloc (bfd_reloc_code_real_type reloc)
7459 {
7460   static const bfd_reloc_code_real_type relocs[][2] =
7461     {
7462       /* Keep sorted incrementally by the left-hand key.  */
7463       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7464       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7465       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7466       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7467       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7468       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7469       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7470       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7471       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7472       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7473       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7474       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7475       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7476       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7477       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7478       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7479       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7480       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7481       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7482       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7483       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7484       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7485       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7486       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7487       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7488       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7489       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7490     };
7491   bfd_reloc_code_real_type r;
7492   size_t i;
7493 
7494   if (!mips_opts.micromips)
7495     return reloc;
7496   for (i = 0; i < ARRAY_SIZE (relocs); i++)
7497     {
7498       r = relocs[i][0];
7499       if (r > reloc)
7500 	return reloc;
7501       if (r == reloc)
7502 	return relocs[i][1];
7503     }
7504   return reloc;
7505 }
7506 
7507 /* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7508    Return true on success, storing the resolved value in RESULT.  */
7509 
7510 static bool
7511 calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7512 		 offsetT *result)
7513 {
7514   switch (reloc)
7515     {
7516     case BFD_RELOC_MIPS_HIGHEST:
7517     case BFD_RELOC_MICROMIPS_HIGHEST:
7518       *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7519       return true;
7520 
7521     case BFD_RELOC_MIPS_HIGHER:
7522     case BFD_RELOC_MICROMIPS_HIGHER:
7523       *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7524       return true;
7525 
7526     case BFD_RELOC_HI16_S:
7527     case BFD_RELOC_HI16_S_PCREL:
7528     case BFD_RELOC_MICROMIPS_HI16_S:
7529     case BFD_RELOC_MIPS16_HI16_S:
7530       *result = ((operand + 0x8000) >> 16) & 0xffff;
7531       return true;
7532 
7533     case BFD_RELOC_HI16:
7534     case BFD_RELOC_MICROMIPS_HI16:
7535     case BFD_RELOC_MIPS16_HI16:
7536       *result = (operand >> 16) & 0xffff;
7537       return true;
7538 
7539     case BFD_RELOC_LO16:
7540     case BFD_RELOC_LO16_PCREL:
7541     case BFD_RELOC_MICROMIPS_LO16:
7542     case BFD_RELOC_MIPS16_LO16:
7543       *result = operand & 0xffff;
7544       return true;
7545 
7546     case BFD_RELOC_UNUSED:
7547       *result = operand;
7548       return true;
7549 
7550     default:
7551       return false;
7552     }
7553 }
7554 
7555 /* Output an instruction.  IP is the instruction information.
7556    ADDRESS_EXPR is an operand of the instruction to be used with
7557    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
7558    a macro expansion.  */
7559 
7560 static void
7561 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
7562 	     bfd_reloc_code_real_type *reloc_type, bool expansionp)
7563 {
7564   unsigned long prev_pinfo2, pinfo;
7565   bool relaxed_branch = false;
7566   enum append_method method;
7567   bool relax32;
7568   int branch_disp;
7569 
7570   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
7571     fix_loongson2f (ip);
7572 
7573   ip->target[0] = '\0';
7574   if (offset_expr.X_op == O_symbol)
7575     strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7576   ip->label[0] = '\0';
7577   if (seg_info (now_seg)->label_list)
7578     strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7579   if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7580     fix_loongson3_llsc (ip);
7581 
7582   file_ase_mips16 |= mips_opts.mips16;
7583   file_ase_micromips |= mips_opts.micromips;
7584 
7585   prev_pinfo2 = history[0].insn_mo->pinfo2;
7586   pinfo = ip->insn_mo->pinfo;
7587 
7588   /* Don't raise alarm about `nods' frags as they'll fill in the right
7589      kind of nop in relaxation if required.  */
7590   if (mips_opts.micromips
7591       && !expansionp
7592       && !(history[0].frag
7593 	   && history[0].frag->fr_type == rs_machine_dependent
7594 	   && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7595 	   && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
7596       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7597 	   && micromips_insn_length (ip->insn_mo) != 2)
7598 	  || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7599 	      && micromips_insn_length (ip->insn_mo) != 4)))
7600     as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
7601 	     (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
7602 
7603   if (address_expr == NULL)
7604     ip->complete_p = 1;
7605   else if (reloc_type[0] <= BFD_RELOC_UNUSED
7606 	   && reloc_type[1] == BFD_RELOC_UNUSED
7607 	   && reloc_type[2] == BFD_RELOC_UNUSED
7608 	   && address_expr->X_op == O_constant)
7609     {
7610       switch (*reloc_type)
7611 	{
7612 	case BFD_RELOC_MIPS_JMP:
7613 	  {
7614 	    int shift;
7615 
7616 	    /* Shift is 2, unusually, for microMIPS JALX.  */
7617 	    shift = (mips_opts.micromips
7618 		     && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
7619 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7620 	      as_bad (_("jump to misaligned address (0x%lx)"),
7621 		      (unsigned long) address_expr->X_add_number);
7622 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7623 				& 0x3ffffff);
7624 	    ip->complete_p = 1;
7625 	  }
7626 	  break;
7627 
7628 	case BFD_RELOC_MIPS16_JMP:
7629 	  if ((address_expr->X_add_number & 3) != 0)
7630 	    as_bad (_("jump to misaligned address (0x%lx)"),
7631 	            (unsigned long) address_expr->X_add_number);
7632 	  ip->insn_opcode |=
7633 	    (((address_expr->X_add_number & 0x7c0000) << 3)
7634 	       | ((address_expr->X_add_number & 0xf800000) >> 7)
7635 	       | ((address_expr->X_add_number & 0x3fffc) >> 2));
7636 	  ip->complete_p = 1;
7637 	  break;
7638 
7639 	case BFD_RELOC_16_PCREL_S2:
7640 	  {
7641 	    int shift;
7642 
7643 	    shift = mips_opts.micromips ? 1 : 2;
7644 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7645 	      as_bad (_("branch to misaligned address (0x%lx)"),
7646 		      (unsigned long) address_expr->X_add_number);
7647 	    if (!mips_relax_branch)
7648 	      {
7649 		if ((address_expr->X_add_number + (1 << (shift + 15)))
7650 		    & ~((1 << (shift + 16)) - 1))
7651 		  as_bad (_("branch address range overflow (0x%lx)"),
7652 			  (unsigned long) address_expr->X_add_number);
7653 		ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7654 				    & 0xffff);
7655 	      }
7656 	  }
7657 	  break;
7658 
7659 	case BFD_RELOC_MIPS_21_PCREL_S2:
7660 	  {
7661 	    int shift;
7662 
7663 	    shift = 2;
7664 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7665 	      as_bad (_("branch to misaligned address (0x%lx)"),
7666 		      (unsigned long) address_expr->X_add_number);
7667 	    if ((address_expr->X_add_number + (1 << (shift + 20)))
7668 		& ~((1 << (shift + 21)) - 1))
7669 	      as_bad (_("branch address range overflow (0x%lx)"),
7670 		      (unsigned long) address_expr->X_add_number);
7671 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7672 				& 0x1fffff);
7673 	  }
7674 	  break;
7675 
7676 	case BFD_RELOC_MIPS_26_PCREL_S2:
7677 	  {
7678 	    int shift;
7679 
7680 	    shift = 2;
7681 	    if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7682 	      as_bad (_("branch to misaligned address (0x%lx)"),
7683 		      (unsigned long) address_expr->X_add_number);
7684 	    if ((address_expr->X_add_number + (1 << (shift + 25)))
7685 		& ~((1 << (shift + 26)) - 1))
7686 	      as_bad (_("branch address range overflow (0x%lx)"),
7687 		      (unsigned long) address_expr->X_add_number);
7688 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7689 				& 0x3ffffff);
7690 	  }
7691 	  break;
7692 
7693 	default:
7694 	  {
7695 	    offsetT value;
7696 
7697 	    if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7698 				 &value))
7699 	      {
7700 		ip->insn_opcode |= value & 0xffff;
7701 		ip->complete_p = 1;
7702 	      }
7703 	  }
7704 	  break;
7705 	}
7706     }
7707 
7708   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7709     {
7710       /* There are a lot of optimizations we could do that we don't.
7711 	 In particular, we do not, in general, reorder instructions.
7712 	 If you use gcc with optimization, it will reorder
7713 	 instructions and generally do much more optimization then we
7714 	 do here; repeating all that work in the assembler would only
7715 	 benefit hand written assembly code, and does not seem worth
7716 	 it.  */
7717       int nops = (mips_optimize == 0
7718 		  ? nops_for_insn (0, history, NULL)
7719 		  : nops_for_insn_or_target (0, history, ip));
7720       if (nops > 0)
7721 	{
7722 	  fragS *old_frag;
7723 	  unsigned long old_frag_offset;
7724 	  int i;
7725 
7726 	  old_frag = frag_now;
7727 	  old_frag_offset = frag_now_fix ();
7728 
7729 	  for (i = 0; i < nops; i++)
7730 	    add_fixed_insn (NOP_INSN);
7731 	  insert_into_history (0, nops, NOP_INSN);
7732 
7733 	  if (listing)
7734 	    {
7735 	      listing_prev_line ();
7736 	      /* We may be at the start of a variant frag.  In case we
7737                  are, make sure there is enough space for the frag
7738                  after the frags created by listing_prev_line.  The
7739                  argument to frag_grow here must be at least as large
7740                  as the argument to all other calls to frag_grow in
7741                  this file.  We don't have to worry about being in the
7742                  middle of a variant frag, because the variants insert
7743                  all needed nop instructions themselves.  */
7744 	      frag_grow (40);
7745 	    }
7746 
7747 	  mips_move_text_labels ();
7748 
7749 #ifndef NO_ECOFF_DEBUGGING
7750 	  if (ECOFF_DEBUGGING)
7751 	    ecoff_fix_loc (old_frag, old_frag_offset);
7752 #endif
7753 	}
7754     }
7755   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7756     {
7757       int nops;
7758 
7759       /* Work out how many nops in prev_nop_frag are needed by IP,
7760 	 ignoring hazards generated by the first prev_nop_frag_since
7761 	 instructions.  */
7762       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
7763       gas_assert (nops <= prev_nop_frag_holds);
7764 
7765       /* Enforce NOPS as a minimum.  */
7766       if (nops > prev_nop_frag_required)
7767 	prev_nop_frag_required = nops;
7768 
7769       if (prev_nop_frag_holds == prev_nop_frag_required)
7770 	{
7771 	  /* Settle for the current number of nops.  Update the history
7772 	     accordingly (for the benefit of any future .set reorder code).  */
7773 	  prev_nop_frag = NULL;
7774 	  insert_into_history (prev_nop_frag_since,
7775 			       prev_nop_frag_holds, NOP_INSN);
7776 	}
7777       else
7778 	{
7779 	  /* Allow this instruction to replace one of the nops that was
7780 	     tentatively added to prev_nop_frag.  */
7781 	  prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
7782 	  prev_nop_frag_holds--;
7783 	  prev_nop_frag_since++;
7784 	}
7785     }
7786 
7787   method = get_append_method (ip, address_expr, reloc_type);
7788   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
7789 
7790   dwarf2_emit_insn (0);
7791   /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7792      so "move" the instruction address accordingly.
7793 
7794      Also, it doesn't seem appropriate for the assembler to reorder .loc
7795      entries.  If this instruction is a branch that we are going to swap
7796      with the previous instruction, the two instructions should be
7797      treated as a unit, and the debug information for both instructions
7798      should refer to the start of the branch sequence.  Using the
7799      current position is certainly wrong when swapping a 32-bit branch
7800      and a 16-bit delay slot, since the current position would then be
7801      in the middle of a branch.  */
7802   dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
7803 
7804   relax32 = (mips_relax_branch
7805 	     /* Don't try branch relaxation within .set nomacro, or within
7806 	        .set noat if we use $at for PIC computations.  If it turns
7807 	        out that the branch was out-of-range, we'll get an error.  */
7808 	     && !mips_opts.warn_about_macros
7809 	     && (mips_opts.at || mips_pic == NO_PIC)
7810 	     /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7811 	        as they have no complementing branches.  */
7812 	     && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
7813 
7814   if (!HAVE_CODE_COMPRESSION
7815       && address_expr
7816       && relax32
7817       && *reloc_type == BFD_RELOC_16_PCREL_S2
7818       && delayed_branch_p (ip))
7819     {
7820       relaxed_branch = true;
7821       add_relaxed_insn (ip, (relaxed_branch_length
7822 			     (NULL, NULL,
7823 			      uncond_branch_p (ip) ? -1
7824 			      : branch_likely_p (ip) ? 1
7825 			      : 0)), 4,
7826 			RELAX_BRANCH_ENCODE
7827 			(AT, mips_pic != NO_PIC,
7828 			 uncond_branch_p (ip),
7829 			 branch_likely_p (ip),
7830 			 pinfo & INSN_WRITE_GPR_31,
7831 			 0),
7832 			address_expr->X_add_symbol,
7833 			address_expr->X_add_number);
7834       *reloc_type = BFD_RELOC_UNUSED;
7835     }
7836   else if (mips_opts.micromips
7837 	   && address_expr
7838 	   && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7839 	       || *reloc_type > BFD_RELOC_UNUSED)
7840 	   && (delayed_branch_p (ip) || compact_branch_p (ip))
7841 	   /* Don't try branch relaxation when users specify
7842 	      16-bit/32-bit instructions.  */
7843 	   && !forced_insn_length)
7844     {
7845       bool relax16 = (method != APPEND_ADD_COMPACT
7846 		      && *reloc_type > BFD_RELOC_UNUSED);
7847       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
7848       int uncond = uncond_branch_p (ip) ? -1 : 0;
7849       int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7850       int nods = method == APPEND_ADD_WITH_NOP;
7851       int al = pinfo & INSN_WRITE_GPR_31;
7852       int length32 = nods ? 8 : 4;
7853 
7854       gas_assert (address_expr != NULL);
7855       gas_assert (!mips_relax.sequence);
7856 
7857       relaxed_branch = true;
7858       if (nods)
7859 	method = APPEND_ADD;
7860       if (relax32)
7861 	length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7862       add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
7863 			RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
7864 						mips_pic != NO_PIC,
7865 						uncond, compact, al, nods,
7866 						relax32, 0, 0),
7867 			address_expr->X_add_symbol,
7868 			address_expr->X_add_number);
7869       *reloc_type = BFD_RELOC_UNUSED;
7870     }
7871   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
7872     {
7873       bool require_unextended;
7874       bool require_extended;
7875       symbolS *symbol;
7876       offsetT offset;
7877 
7878       if (forced_insn_length != 0)
7879 	{
7880 	  require_unextended = forced_insn_length == 2;
7881 	  require_extended = forced_insn_length == 4;
7882 	}
7883       else
7884 	{
7885 	  require_unextended = (mips_opts.noautoextend
7886 				&& !mips_opcode_32bit_p (ip->insn_mo));
7887 	  require_extended = 0;
7888 	}
7889 
7890       /* We need to set up a variant frag.  */
7891       gas_assert (address_expr != NULL);
7892       /* Pass any `O_symbol' expression unchanged as an `expr_section'
7893          symbol created by `make_expr_symbol' may not get a necessary
7894          external relocation produced.  */
7895       if (address_expr->X_op == O_symbol)
7896 	{
7897 	  symbol = address_expr->X_add_symbol;
7898 	  offset = address_expr->X_add_number;
7899 	}
7900       else
7901 	{
7902 	  symbol = make_expr_symbol (address_expr);
7903 	  symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
7904 	  offset = 0;
7905 	}
7906       add_relaxed_insn (ip, 12, 0,
7907 			RELAX_MIPS16_ENCODE
7908 			(*reloc_type - BFD_RELOC_UNUSED,
7909 			 mips_opts.ase & ASE_MIPS16E2,
7910 			 mips_pic != NO_PIC,
7911 			 HAVE_32BIT_SYMBOLS,
7912 			 mips_opts.warn_about_macros,
7913 			 require_unextended, require_extended,
7914 			 delayed_branch_p (&history[0]),
7915 			 history[0].mips16_absolute_jump_p),
7916 			symbol, offset);
7917     }
7918   else if (mips_opts.mips16 && insn_length (ip) == 2)
7919     {
7920       if (!delayed_branch_p (ip))
7921 	/* Make sure there is enough room to swap this instruction with
7922 	   a following jump instruction.  */
7923 	frag_grow (6);
7924       add_fixed_insn (ip);
7925     }
7926   else
7927     {
7928       if (mips_opts.mips16
7929 	  && mips_opts.noreorder
7930 	  && delayed_branch_p (&history[0]))
7931 	as_warn (_("extended instruction in delay slot"));
7932 
7933       if (mips_relax.sequence)
7934 	{
7935 	  /* If we've reached the end of this frag, turn it into a variant
7936 	     frag and record the information for the instructions we've
7937 	     written so far.  */
7938 	  if (frag_room () < 4)
7939 	    relax_close_frag ();
7940 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
7941 	}
7942 
7943       if (mips_relax.sequence != 2)
7944 	{
7945 	  if (mips_macro_warning.first_insn_sizes[0] == 0)
7946 	    mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7947 	  mips_macro_warning.sizes[0] += insn_length (ip);
7948 	  mips_macro_warning.insns[0]++;
7949 	}
7950       if (mips_relax.sequence != 1)
7951 	{
7952 	  if (mips_macro_warning.first_insn_sizes[1] == 0)
7953 	    mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7954 	  mips_macro_warning.sizes[1] += insn_length (ip);
7955 	  mips_macro_warning.insns[1]++;
7956 	}
7957 
7958       if (mips_opts.mips16)
7959 	{
7960 	  ip->fixed_p = 1;
7961 	  ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7962 	}
7963       add_fixed_insn (ip);
7964     }
7965 
7966   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
7967     {
7968       bfd_reloc_code_real_type final_type[3];
7969       reloc_howto_type *howto0;
7970       reloc_howto_type *howto;
7971       int i;
7972 
7973       /* Perform any necessary conversion to microMIPS relocations
7974 	 and find out how many relocations there actually are.  */
7975       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7976 	final_type[i] = micromips_map_reloc (reloc_type[i]);
7977 
7978       /* In a compound relocation, it is the final (outermost)
7979 	 operator that determines the relocated field.  */
7980       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
7981       if (!howto)
7982 	abort ();
7983 
7984       if (i > 1)
7985 	howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
7986       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7987 				 bfd_get_reloc_size (howto),
7988 				 address_expr,
7989 				 howto0 && howto0->pc_relative,
7990 				 final_type[0]);
7991       /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'.  */
7992       ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
7993 
7994       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
7995       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
7996 	*symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7997 
7998       /* These relocations can have an addend that won't fit in
7999 	 4 octets for 64bit assembly.  */
8000       if (GPR_SIZE == 64
8001 	  && ! howto->partial_inplace
8002 	  && (reloc_type[0] == BFD_RELOC_16
8003 	      || reloc_type[0] == BFD_RELOC_32
8004 	      || reloc_type[0] == BFD_RELOC_MIPS_JMP
8005 	      || reloc_type[0] == BFD_RELOC_GPREL16
8006 	      || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
8007 	      || reloc_type[0] == BFD_RELOC_GPREL32
8008 	      || reloc_type[0] == BFD_RELOC_64
8009 	      || reloc_type[0] == BFD_RELOC_CTOR
8010 	      || reloc_type[0] == BFD_RELOC_MIPS_SUB
8011 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
8012 	      || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
8013 	      || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
8014 	      || reloc_type[0] == BFD_RELOC_MIPS_16
8015 	      || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
8016 	      || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
8017 	      || hi16_reloc_p (reloc_type[0])
8018 	      || lo16_reloc_p (reloc_type[0])))
8019 	ip->fixp[0]->fx_no_overflow = 1;
8020 
8021       /* These relocations can have an addend that won't fit in 2 octets.  */
8022       if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
8023 	  || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
8024 	ip->fixp[0]->fx_no_overflow = 1;
8025 
8026       if (mips_relax.sequence)
8027 	{
8028 	  if (mips_relax.first_fixup == 0)
8029 	    mips_relax.first_fixup = ip->fixp[0];
8030 	}
8031       else if (reloc_needs_lo_p (*reloc_type))
8032 	{
8033 	  struct mips_hi_fixup *hi_fixup;
8034 
8035 	  /* Reuse the last entry if it already has a matching %lo.  */
8036 	  hi_fixup = mips_hi_fixup_list;
8037 	  if (hi_fixup == 0
8038 	      || !fixup_has_matching_lo_p (hi_fixup->fixp))
8039 	    {
8040 	      hi_fixup = XNEW (struct mips_hi_fixup);
8041 	      hi_fixup->next = mips_hi_fixup_list;
8042 	      mips_hi_fixup_list = hi_fixup;
8043 	    }
8044 	  hi_fixup->fixp = ip->fixp[0];
8045 	  hi_fixup->seg = now_seg;
8046 	}
8047 
8048       /* Add fixups for the second and third relocations, if given.
8049 	 Note that the ABI allows the second relocation to be
8050 	 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
8051 	 moment we only use RSS_UNDEF, but we could add support
8052 	 for the others if it ever becomes necessary.  */
8053       for (i = 1; i < 3; i++)
8054 	if (reloc_type[i] != BFD_RELOC_UNUSED)
8055 	  {
8056 	    ip->fixp[i] = fix_new (ip->frag, ip->where,
8057 				   ip->fixp[0]->fx_size, NULL, 0,
8058 				   false, final_type[i]);
8059 
8060 	    /* Use fx_tcbit to mark compound relocs.  */
8061 	    ip->fixp[0]->fx_tcbit = 1;
8062 	    ip->fixp[i]->fx_tcbit = 1;
8063 	  }
8064     }
8065 
8066   /* Update the register mask information.  */
8067   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
8068   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
8069 
8070   switch (method)
8071     {
8072     case APPEND_ADD:
8073       insert_into_history (0, 1, ip);
8074       break;
8075 
8076     case APPEND_ADD_WITH_NOP:
8077       {
8078 	struct mips_cl_insn *nop;
8079 
8080 	insert_into_history (0, 1, ip);
8081 	nop = get_delay_slot_nop (ip);
8082 	add_fixed_insn (nop);
8083 	insert_into_history (0, 1, nop);
8084 	if (mips_relax.sequence)
8085 	  mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
8086       }
8087       break;
8088 
8089     case APPEND_ADD_COMPACT:
8090       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
8091       if (mips_opts.mips16)
8092 	{
8093 	  ip->insn_opcode |= 0x0080;
8094 	  find_altered_mips16_opcode (ip);
8095 	}
8096       /* Convert microMIPS instructions.  */
8097       else if (mips_opts.micromips)
8098 	{
8099 	  /* jr16->jrc */
8100 	  if ((ip->insn_opcode & 0xffe0) == 0x4580)
8101 	    ip->insn_opcode |= 0x0020;
8102 	  /* b16->bc */
8103 	  else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
8104 	    ip->insn_opcode = 0x40e00000;
8105 	  /* beqz16->beqzc, bnez16->bnezc */
8106 	  else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
8107 	    {
8108 	      unsigned long regno;
8109 
8110 	      regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
8111 	      regno &= MICROMIPSOP_MASK_MD;
8112 	      regno = micromips_to_32_reg_d_map[regno];
8113 	      ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8114 				 | (regno << MICROMIPSOP_SH_RS)
8115 				 | 0x40a00000) ^ 0x00400000;
8116 	    }
8117 	  /* beqz->beqzc, bnez->bnezc */
8118 	  else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8119 	    ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8120 			       | ((ip->insn_opcode >> 7) & 0x00400000)
8121 			       | 0x40a00000) ^ 0x00400000;
8122 	  /* beq $0->beqzc, bne $0->bnezc */
8123 	  else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8124 	    ip->insn_opcode = (((ip->insn_opcode >>
8125 				 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8126 				& (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8127 			       | ((ip->insn_opcode >> 7) & 0x00400000)
8128 			       | 0x40a00000) ^ 0x00400000;
8129 	  else
8130 	    abort ();
8131 	  find_altered_micromips_opcode (ip);
8132 	}
8133       else
8134 	abort ();
8135       install_insn (ip);
8136       insert_into_history (0, 1, ip);
8137       break;
8138 
8139     case APPEND_SWAP:
8140       {
8141 	struct mips_cl_insn delay = history[0];
8142 
8143 	if (relaxed_branch || delay.frag != ip->frag)
8144 	  {
8145 	    /* Add the delay slot instruction to the end of the
8146 	       current frag and shrink the fixed part of the
8147 	       original frag.  If the branch occupies the tail of
8148 	       the latter, move it backwards to cover the gap.  */
8149 	    delay.frag->fr_fix -= branch_disp;
8150 	    if (delay.frag == ip->frag)
8151 	      move_insn (ip, ip->frag, ip->where - branch_disp);
8152 	    add_fixed_insn (&delay);
8153 	  }
8154 	else
8155 	  {
8156 	    /* If this is not a relaxed branch and we are in the
8157 	       same frag, then just swap the instructions.  */
8158 	    move_insn (ip, delay.frag, delay.where);
8159 	    move_insn (&delay, ip->frag, ip->where + insn_length (ip));
8160 	  }
8161 	history[0] = *ip;
8162 	delay.fixed_p = 1;
8163 	insert_into_history (0, 1, &delay);
8164       }
8165       break;
8166     }
8167 
8168   /* If we have just completed an unconditional branch, clear the history.  */
8169   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8170       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
8171     {
8172       unsigned int i;
8173 
8174       mips_no_prev_insn ();
8175 
8176       for (i = 0; i < ARRAY_SIZE (history); i++)
8177 	history[i].cleared_p = 1;
8178     }
8179 
8180   /* We need to emit a label at the end of branch-likely macros.  */
8181   if (emit_branch_likely_macro)
8182     {
8183       emit_branch_likely_macro = false;
8184       micromips_add_label ();
8185     }
8186 
8187   /* We just output an insn, so the next one doesn't have a label.  */
8188   mips_clear_insn_labels ();
8189 }
8190 
8191 /* Forget that there was any previous instruction or label.
8192    When BRANCH is true, the branch history is also flushed.  */
8193 
8194 static void
8195 mips_no_prev_insn (void)
8196 {
8197   prev_nop_frag = NULL;
8198   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
8199   mips_clear_insn_labels ();
8200 }
8201 
8202 /* This function must be called before we emit something other than
8203    instructions.  It is like mips_no_prev_insn except that it inserts
8204    any NOPS that might be needed by previous instructions.  */
8205 
8206 void
8207 mips_emit_delays (void)
8208 {
8209   if (! mips_opts.noreorder)
8210     {
8211       int nops = nops_for_insn (0, history, NULL);
8212       if (nops > 0)
8213 	{
8214 	  while (nops-- > 0)
8215 	    add_fixed_insn (NOP_INSN);
8216 	  mips_move_text_labels ();
8217 	}
8218     }
8219   mips_no_prev_insn ();
8220 }
8221 
8222 /* Start a (possibly nested) noreorder block.  */
8223 
8224 static void
8225 start_noreorder (void)
8226 {
8227   if (mips_opts.noreorder == 0)
8228     {
8229       unsigned int i;
8230       int nops;
8231 
8232       /* None of the instructions before the .set noreorder can be moved.  */
8233       for (i = 0; i < ARRAY_SIZE (history); i++)
8234 	history[i].fixed_p = 1;
8235 
8236       /* Insert any nops that might be needed between the .set noreorder
8237 	 block and the previous instructions.  We will later remove any
8238 	 nops that turn out not to be needed.  */
8239       nops = nops_for_insn (0, history, NULL);
8240       if (nops > 0)
8241 	{
8242 	  if (mips_optimize != 0)
8243 	    {
8244 	      /* Record the frag which holds the nop instructions, so
8245                  that we can remove them if we don't need them.  */
8246 	      frag_grow (nops * NOP_INSN_SIZE);
8247 	      prev_nop_frag = frag_now;
8248 	      prev_nop_frag_holds = nops;
8249 	      prev_nop_frag_required = 0;
8250 	      prev_nop_frag_since = 0;
8251 	    }
8252 
8253 	  for (; nops > 0; --nops)
8254 	    add_fixed_insn (NOP_INSN);
8255 
8256 	  /* Move on to a new frag, so that it is safe to simply
8257 	     decrease the size of prev_nop_frag.  */
8258 	  frag_wane (frag_now);
8259 	  frag_new (0);
8260 	  mips_move_text_labels ();
8261 	}
8262       mips_mark_labels ();
8263       mips_clear_insn_labels ();
8264     }
8265   mips_opts.noreorder++;
8266   mips_any_noreorder = 1;
8267 }
8268 
8269 /* End a nested noreorder block.  */
8270 
8271 static void
8272 end_noreorder (void)
8273 {
8274   mips_opts.noreorder--;
8275   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8276     {
8277       /* Commit to inserting prev_nop_frag_required nops and go back to
8278 	 handling nop insertion the .set reorder way.  */
8279       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
8280 				* NOP_INSN_SIZE);
8281       insert_into_history (prev_nop_frag_since,
8282 			   prev_nop_frag_required, NOP_INSN);
8283       prev_nop_frag = NULL;
8284     }
8285 }
8286 
8287 /* Sign-extend 32-bit mode constants that have bit 31 set and all
8288    higher bits unset.  */
8289 
8290 static void
8291 normalize_constant_expr (expressionS *ex)
8292 {
8293   if (ex->X_op == O_constant
8294       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8295     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8296 			- 0x80000000);
8297 }
8298 
8299 /* Sign-extend 32-bit mode address offsets that have bit 31 set and
8300    all higher bits unset.  */
8301 
8302 static void
8303 normalize_address_expr (expressionS *ex)
8304 {
8305   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8306 	|| (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8307       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8308     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8309 			- 0x80000000);
8310 }
8311 
8312 /* Try to match TOKENS against OPCODE, storing the result in INSN.
8313    Return true if the match was successful.
8314 
8315    OPCODE_EXTRA is a value that should be ORed into the opcode
8316    (used for VU0 channel suffixes, etc.).  MORE_ALTS is true if
8317    there are more alternatives after OPCODE and SOFT_MATCH is
8318    as for mips_arg_info.  */
8319 
8320 static bool
8321 match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8322 	    struct mips_operand_token *tokens, unsigned int opcode_extra,
8323 	    bool lax_match, bool complete_p)
8324 {
8325   const char *args;
8326   struct mips_arg_info arg;
8327   const struct mips_operand *operand;
8328   char c;
8329 
8330   imm_expr.X_op = O_absent;
8331   offset_expr.X_op = O_absent;
8332   offset_reloc[0] = BFD_RELOC_UNUSED;
8333   offset_reloc[1] = BFD_RELOC_UNUSED;
8334   offset_reloc[2] = BFD_RELOC_UNUSED;
8335 
8336   create_insn (insn, opcode);
8337   /* When no opcode suffix is specified, assume ".xyzw". */
8338   if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8339     insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8340   else
8341     insn->insn_opcode |= opcode_extra;
8342   memset (&arg, 0, sizeof (arg));
8343   arg.insn = insn;
8344   arg.token = tokens;
8345   arg.argnum = 1;
8346   arg.last_regno = ILLEGAL_REG;
8347   arg.dest_regno = ILLEGAL_REG;
8348   arg.lax_match = lax_match;
8349   for (args = opcode->args;; ++args)
8350     {
8351       if (arg.token->type == OT_END)
8352 	{
8353 	  /* Handle unary instructions in which only one operand is given.
8354 	     The source is then the same as the destination.  */
8355 	  if (arg.opnum == 1 && *args == ',')
8356 	    {
8357 	      operand = (mips_opts.micromips
8358 			 ? decode_micromips_operand (args + 1)
8359 			 : decode_mips_operand (args + 1));
8360 	      if (operand && mips_optional_operand_p (operand))
8361 		{
8362 		  arg.token = tokens;
8363 		  arg.argnum = 1;
8364 		  continue;
8365 		}
8366 	    }
8367 
8368 	  /* Treat elided base registers as $0.  */
8369 	  if (strcmp (args, "(b)") == 0)
8370 	    args += 3;
8371 
8372 	  if (args[0] == '+')
8373 	    switch (args[1])
8374 	      {
8375 	      case 'K':
8376 	      case 'N':
8377 		/* The register suffix is optional. */
8378 		args += 2;
8379 		break;
8380 	      }
8381 
8382 	  /* Fail the match if there were too few operands.  */
8383 	  if (*args)
8384 	    return false;
8385 
8386 	  /* Successful match.  */
8387 	  if (!complete_p)
8388 	    return true;
8389 	  clear_insn_error ();
8390 	  if (arg.dest_regno == arg.last_regno
8391 	      && startswith (insn->insn_mo->name, "jalr"))
8392 	    {
8393 	      if (arg.opnum == 2)
8394 		set_insn_error
8395 		  (0, _("source and destination must be different"));
8396 	      else if (arg.last_regno == 31)
8397 		set_insn_error
8398 		  (0, _("a destination register must be supplied"));
8399 	    }
8400 	  else if (arg.last_regno == 31
8401 		   && (startswith (insn->insn_mo->name, "bltzal")
8402 		       || startswith (insn->insn_mo->name, "bgezal")))
8403 	    set_insn_error (0, _("the source register must not be $31"));
8404 	  check_completed_insn (&arg);
8405 	  return true;
8406 	}
8407 
8408       /* Fail the match if the line has too many operands.   */
8409       if (*args == 0)
8410 	return false;
8411 
8412       /* Handle characters that need to match exactly.  */
8413       if (*args == '(' || *args == ')' || *args == ',')
8414 	{
8415 	  if (match_char (&arg, *args))
8416 	    continue;
8417 	  return false;
8418 	}
8419       if (*args == '#')
8420 	{
8421 	  ++args;
8422 	  if (arg.token->type == OT_DOUBLE_CHAR
8423 	      && arg.token->u.ch == *args)
8424 	    {
8425 	      ++arg.token;
8426 	      continue;
8427 	    }
8428 	  return false;
8429 	}
8430 
8431       /* Handle special macro operands.  Work out the properties of
8432 	 other operands.  */
8433       arg.opnum += 1;
8434       switch (*args)
8435 	{
8436 	case '-':
8437 	  switch (args[1])
8438 	    {
8439 	    case 'A':
8440 	      *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8441 	      break;
8442 
8443 	    case 'B':
8444 	      *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8445 	      break;
8446 	    }
8447 	  break;
8448 
8449 	case '+':
8450 	  switch (args[1])
8451 	    {
8452 	    case 'i':
8453 	      *offset_reloc = BFD_RELOC_MIPS_JMP;
8454 	      break;
8455 
8456 	    case '\'':
8457 	      *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8458 	      break;
8459 
8460 	    case '\"':
8461 	      *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8462 	      break;
8463 	    }
8464 	  break;
8465 
8466 	case 'I':
8467 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
8468 	    return false;
8469 	  imm_expr.X_op = O_constant;
8470 	  if (GPR_SIZE == 32)
8471 	    normalize_constant_expr (&imm_expr);
8472 	  continue;
8473 
8474 	case 'A':
8475 	  if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8476 	    {
8477 	      /* Assume that the offset has been elided and that what
8478 		 we saw was a base register.  The match will fail later
8479 		 if that assumption turns out to be wrong.  */
8480 	      offset_expr.X_op = O_constant;
8481 	      offset_expr.X_add_number = 0;
8482 	    }
8483 	  else
8484 	    {
8485 	      if (!match_expression (&arg, &offset_expr, offset_reloc))
8486 		return false;
8487 	      normalize_address_expr (&offset_expr);
8488 	    }
8489 	  continue;
8490 
8491 	case 'F':
8492 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8493 				     8, true))
8494 	    return false;
8495 	  continue;
8496 
8497 	case 'L':
8498 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8499 				     8, false))
8500 	    return false;
8501 	  continue;
8502 
8503 	case 'f':
8504 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8505 				     4, true))
8506 	    return false;
8507 	  continue;
8508 
8509 	case 'l':
8510 	  if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8511 				     4, false))
8512 	    return false;
8513 	  continue;
8514 
8515 	case 'p':
8516 	  *offset_reloc = BFD_RELOC_16_PCREL_S2;
8517 	  break;
8518 
8519 	case 'a':
8520 	  *offset_reloc = BFD_RELOC_MIPS_JMP;
8521 	  break;
8522 
8523 	case 'm':
8524 	  gas_assert (mips_opts.micromips);
8525 	  c = args[1];
8526 	  switch (c)
8527 	    {
8528 	    case 'D':
8529 	    case 'E':
8530 	      if (!forced_insn_length)
8531 		*offset_reloc = (int) BFD_RELOC_UNUSED + c;
8532 	      else if (c == 'D')
8533 		*offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8534 	      else
8535 		*offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8536 	      break;
8537 	    }
8538 	  break;
8539 	}
8540 
8541       operand = (mips_opts.micromips
8542 		 ? decode_micromips_operand (args)
8543 		 : decode_mips_operand (args));
8544       if (!operand)
8545 	abort ();
8546 
8547       /* Skip prefixes.  */
8548       if (*args == '+' || *args == 'm' || *args == '-')
8549 	args++;
8550 
8551       if (mips_optional_operand_p (operand)
8552 	  && args[1] == ','
8553 	  && (arg.token[0].type != OT_REG
8554 	      || arg.token[1].type == OT_END))
8555 	{
8556 	  /* Assume that the register has been elided and is the
8557 	     same as the first operand.  */
8558 	  arg.token = tokens;
8559 	  arg.argnum = 1;
8560 	}
8561 
8562       if (!match_operand (&arg, operand))
8563 	return false;
8564     }
8565 }
8566 
8567 /* Like match_insn, but for MIPS16.  */
8568 
8569 static bool
8570 match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8571 		   struct mips_operand_token *tokens)
8572 {
8573   const char *args;
8574   const struct mips_operand *operand;
8575   const struct mips_operand *ext_operand;
8576   bool pcrel = false;
8577   int required_insn_length;
8578   struct mips_arg_info arg;
8579   int relax_char;
8580 
8581   if (forced_insn_length)
8582     required_insn_length = forced_insn_length;
8583   else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8584     required_insn_length = 2;
8585   else
8586     required_insn_length = 0;
8587 
8588   create_insn (insn, opcode);
8589   imm_expr.X_op = O_absent;
8590   offset_expr.X_op = O_absent;
8591   offset_reloc[0] = BFD_RELOC_UNUSED;
8592   offset_reloc[1] = BFD_RELOC_UNUSED;
8593   offset_reloc[2] = BFD_RELOC_UNUSED;
8594   relax_char = 0;
8595 
8596   memset (&arg, 0, sizeof (arg));
8597   arg.insn = insn;
8598   arg.token = tokens;
8599   arg.argnum = 1;
8600   arg.last_regno = ILLEGAL_REG;
8601   arg.dest_regno = ILLEGAL_REG;
8602   relax_char = 0;
8603   for (args = opcode->args;; ++args)
8604     {
8605       int c;
8606 
8607       if (arg.token->type == OT_END)
8608 	{
8609 	  offsetT value;
8610 
8611 	  /* Handle unary instructions in which only one operand is given.
8612 	     The source is then the same as the destination.  */
8613 	  if (arg.opnum == 1 && *args == ',')
8614 	    {
8615 	      operand = decode_mips16_operand (args[1], false);
8616 	      if (operand && mips_optional_operand_p (operand))
8617 		{
8618 		  arg.token = tokens;
8619 		  arg.argnum = 1;
8620 		  continue;
8621 		}
8622 	    }
8623 
8624 	  /* Fail the match if there were too few operands.  */
8625 	  if (*args)
8626 	    return false;
8627 
8628 	  /* Successful match.  Stuff the immediate value in now, if
8629 	     we can.  */
8630 	  clear_insn_error ();
8631 	  if (opcode->pinfo == INSN_MACRO)
8632 	    {
8633 	      gas_assert (relax_char == 0 || relax_char == 'p');
8634 	      gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8635 	    }
8636 	  else if (relax_char
8637 		   && offset_expr.X_op == O_constant
8638 		   && !pcrel
8639 		   && calculate_reloc (*offset_reloc,
8640 				       offset_expr.X_add_number,
8641 				       &value))
8642 	    {
8643 	      mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8644 			    required_insn_length, &insn->insn_opcode);
8645 	      offset_expr.X_op = O_absent;
8646 	      *offset_reloc = BFD_RELOC_UNUSED;
8647 	    }
8648 	  else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8649 	    {
8650 	      if (required_insn_length == 2)
8651 		set_insn_error (0, _("invalid unextended operand value"));
8652 	      else if (!mips_opcode_32bit_p (opcode))
8653 		{
8654 		  forced_insn_length = 4;
8655 		  insn->insn_opcode |= MIPS16_EXTEND;
8656 		}
8657 	    }
8658 	  else if (relax_char)
8659 	    *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8660 
8661 	  check_completed_insn (&arg);
8662 	  return true;
8663 	}
8664 
8665       /* Fail the match if the line has too many operands.   */
8666       if (*args == 0)
8667 	return false;
8668 
8669       /* Handle characters that need to match exactly.  */
8670       if (*args == '(' || *args == ')' || *args == ',')
8671 	{
8672 	  if (match_char (&arg, *args))
8673 	    continue;
8674 	  return false;
8675 	}
8676 
8677       arg.opnum += 1;
8678       c = *args;
8679       switch (c)
8680 	{
8681 	case 'p':
8682 	case 'q':
8683 	case 'A':
8684 	case 'B':
8685 	case 'E':
8686 	case 'V':
8687 	case 'u':
8688 	  relax_char = c;
8689 	  break;
8690 
8691 	case 'I':
8692 	  if (!match_const_int (&arg, &imm_expr.X_add_number))
8693 	    return false;
8694 	  imm_expr.X_op = O_constant;
8695 	  if (GPR_SIZE == 32)
8696 	    normalize_constant_expr (&imm_expr);
8697 	  continue;
8698 
8699 	case 'a':
8700 	case 'i':
8701 	  *offset_reloc = BFD_RELOC_MIPS16_JMP;
8702 	  break;
8703 	}
8704 
8705       operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
8706       if (!operand)
8707 	abort ();
8708 
8709       if (operand->type == OP_PCREL)
8710 	pcrel = true;
8711       else
8712 	{
8713 	  ext_operand = decode_mips16_operand (c, true);
8714 	  if (operand != ext_operand)
8715 	    {
8716 	      if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8717 		{
8718 		  offset_expr.X_op = O_constant;
8719 		  offset_expr.X_add_number = 0;
8720 		  relax_char = c;
8721 		  continue;
8722 		}
8723 
8724 	      if (!match_expression (&arg, &offset_expr, offset_reloc))
8725 		return false;
8726 
8727 	      /* '8' is used for SLTI(U) and has traditionally not
8728 		 been allowed to take relocation operators.  */
8729 	      if (offset_reloc[0] != BFD_RELOC_UNUSED
8730 		  && (ext_operand->size != 16 || c == '8'))
8731 		{
8732 		  match_not_constant (&arg);
8733 		  return false;
8734 		}
8735 
8736 	      if (offset_expr.X_op == O_big)
8737 		{
8738 		  match_out_of_range (&arg);
8739 		  return false;
8740 		}
8741 
8742 	      relax_char = c;
8743 	      continue;
8744 	    }
8745 	}
8746 
8747       if (mips_optional_operand_p (operand)
8748 	  && args[1] == ','
8749 	  && (arg.token[0].type != OT_REG
8750 	      || arg.token[1].type == OT_END))
8751 	{
8752 	  /* Assume that the register has been elided and is the
8753 	     same as the first operand.  */
8754 	  arg.token = tokens;
8755 	  arg.argnum = 1;
8756 	}
8757 
8758       if (!match_operand (&arg, operand))
8759 	return false;
8760     }
8761 }
8762 
8763 /* Record that the current instruction is invalid for the current ISA.  */
8764 
8765 static void
8766 match_invalid_for_isa (void)
8767 {
8768   set_insn_error_ss
8769     (0, _("opcode not supported on this processor: %s (%s)"),
8770      mips_cpu_info_from_arch (mips_opts.arch)->name,
8771      mips_cpu_info_from_isa (mips_opts.isa)->name);
8772 }
8773 
8774 /* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8775    Return true if a definite match or failure was found, storing any match
8776    in INSN.  OPCODE_EXTRA is a value that should be ORed into the opcode
8777    (to handle things like VU0 suffixes).  LAX_MATCH is true if we have already
8778    tried and failed to match under normal conditions and now want to try a
8779    more relaxed match.  */
8780 
8781 static bool
8782 match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8783 	     const struct mips_opcode *past, struct mips_operand_token *tokens,
8784 	     int opcode_extra, bool lax_match)
8785 {
8786   const struct mips_opcode *opcode;
8787   const struct mips_opcode *invalid_delay_slot;
8788   bool seen_valid_for_isa, seen_valid_for_size;
8789 
8790   /* Search for a match, ignoring alternatives that don't satisfy the
8791      current ISA or forced_length.  */
8792   invalid_delay_slot = 0;
8793   seen_valid_for_isa = false;
8794   seen_valid_for_size = false;
8795   opcode = first;
8796   do
8797     {
8798       gas_assert (strcmp (opcode->name, first->name) == 0);
8799       if (is_opcode_valid (opcode))
8800 	{
8801 	  seen_valid_for_isa = true;
8802 	  if (is_size_valid (opcode))
8803 	    {
8804 	      bool delay_slot_ok;
8805 
8806 	      seen_valid_for_size = true;
8807 	      delay_slot_ok = is_delay_slot_valid (opcode);
8808 	      if (match_insn (insn, opcode, tokens, opcode_extra,
8809 			      lax_match, delay_slot_ok))
8810 		{
8811 		  if (!delay_slot_ok)
8812 		    {
8813 		      if (!invalid_delay_slot)
8814 			invalid_delay_slot = opcode;
8815 		    }
8816 		  else
8817 		    return true;
8818 		}
8819 	    }
8820 	}
8821       ++opcode;
8822     }
8823   while (opcode < past && strcmp (opcode->name, first->name) == 0);
8824 
8825   /* If the only matches we found had the wrong length for the delay slot,
8826      pick the first such match.  We'll issue an appropriate warning later.  */
8827   if (invalid_delay_slot)
8828     {
8829       if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8830 		      lax_match, true))
8831 	return true;
8832       abort ();
8833     }
8834 
8835   /* Handle the case where we didn't try to match an instruction because
8836      all the alternatives were incompatible with the current ISA.  */
8837   if (!seen_valid_for_isa)
8838     {
8839       match_invalid_for_isa ();
8840       return true;
8841     }
8842 
8843   /* Handle the case where we didn't try to match an instruction because
8844      all the alternatives were of the wrong size.  */
8845   if (!seen_valid_for_size)
8846     {
8847       if (mips_opts.insn32)
8848 	set_insn_error (0, _("opcode not supported in the `insn32' mode"));
8849       else
8850 	set_insn_error_i
8851 	  (0, _("unrecognized %d-bit version of microMIPS opcode"),
8852 	   8 * forced_insn_length);
8853       return true;
8854     }
8855 
8856   return false;
8857 }
8858 
8859 /* Like match_insns, but for MIPS16.  */
8860 
8861 static bool
8862 match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8863 		    struct mips_operand_token *tokens)
8864 {
8865   const struct mips_opcode *opcode;
8866   bool seen_valid_for_isa;
8867   bool seen_valid_for_size;
8868 
8869   /* Search for a match, ignoring alternatives that don't satisfy the
8870      current ISA.  There are no separate entries for extended forms so
8871      we deal with forced_length later.  */
8872   seen_valid_for_isa = false;
8873   seen_valid_for_size = false;
8874   opcode = first;
8875   do
8876     {
8877       gas_assert (strcmp (opcode->name, first->name) == 0);
8878       if (is_opcode_valid_16 (opcode))
8879 	{
8880 	  seen_valid_for_isa = true;
8881 	  if (is_size_valid_16 (opcode))
8882 	    {
8883 	      seen_valid_for_size = true;
8884 	      if (match_mips16_insn (insn, opcode, tokens))
8885 		return true;
8886 	    }
8887 	}
8888       ++opcode;
8889     }
8890   while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8891 	 && strcmp (opcode->name, first->name) == 0);
8892 
8893   /* Handle the case where we didn't try to match an instruction because
8894      all the alternatives were incompatible with the current ISA.  */
8895   if (!seen_valid_for_isa)
8896     {
8897       match_invalid_for_isa ();
8898       return true;
8899     }
8900 
8901   /* Handle the case where we didn't try to match an instruction because
8902      all the alternatives were of the wrong size.  */
8903   if (!seen_valid_for_size)
8904     {
8905       if (forced_insn_length == 2)
8906 	set_insn_error
8907 	  (0, _("unrecognized unextended version of MIPS16 opcode"));
8908       else
8909 	set_insn_error
8910 	  (0, _("unrecognized extended version of MIPS16 opcode"));
8911       return true;
8912     }
8913 
8914   return false;
8915 }
8916 
8917 /* Set up global variables for the start of a new macro.  */
8918 
8919 static void
8920 macro_start (void)
8921 {
8922   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
8923   memset (&mips_macro_warning.first_insn_sizes, 0,
8924 	  sizeof (mips_macro_warning.first_insn_sizes));
8925   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
8926   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
8927 				     && delayed_branch_p (&history[0]));
8928   if (history[0].frag
8929       && history[0].frag->fr_type == rs_machine_dependent
8930       && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8931       && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8932     mips_macro_warning.delay_slot_length = 0;
8933   else
8934     switch (history[0].insn_mo->pinfo2
8935 	    & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8936       {
8937       case INSN2_BRANCH_DELAY_32BIT:
8938 	mips_macro_warning.delay_slot_length = 4;
8939 	break;
8940       case INSN2_BRANCH_DELAY_16BIT:
8941 	mips_macro_warning.delay_slot_length = 2;
8942 	break;
8943       default:
8944 	mips_macro_warning.delay_slot_length = 0;
8945 	break;
8946       }
8947   mips_macro_warning.first_frag = NULL;
8948 }
8949 
8950 /* Given that a macro is longer than one instruction or of the wrong size,
8951    return the appropriate warning for it.  Return null if no warning is
8952    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8953    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8954    and RELAX_NOMACRO.  */
8955 
8956 static const char *
8957 macro_warning (relax_substateT subtype)
8958 {
8959   if (subtype & RELAX_DELAY_SLOT)
8960     return _("macro instruction expanded into multiple instructions"
8961 	     " in a branch delay slot");
8962   else if (subtype & RELAX_NOMACRO)
8963     return _("macro instruction expanded into multiple instructions");
8964   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8965 		      | RELAX_DELAY_SLOT_SIZE_SECOND))
8966     return ((subtype & RELAX_DELAY_SLOT_16BIT)
8967 	    ? _("macro instruction expanded into a wrong size instruction"
8968 		" in a 16-bit branch delay slot")
8969 	    : _("macro instruction expanded into a wrong size instruction"
8970 		" in a 32-bit branch delay slot"));
8971   else
8972     return 0;
8973 }
8974 
8975 /* Finish up a macro.  Emit warnings as appropriate.  */
8976 
8977 static void
8978 macro_end (void)
8979 {
8980   /* Relaxation warning flags.  */
8981   relax_substateT subtype = 0;
8982 
8983   /* Check delay slot size requirements.  */
8984   if (mips_macro_warning.delay_slot_length == 2)
8985     subtype |= RELAX_DELAY_SLOT_16BIT;
8986   if (mips_macro_warning.delay_slot_length != 0)
8987     {
8988       if (mips_macro_warning.delay_slot_length
8989 	  != mips_macro_warning.first_insn_sizes[0])
8990 	subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8991       if (mips_macro_warning.delay_slot_length
8992 	  != mips_macro_warning.first_insn_sizes[1])
8993 	subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8994     }
8995 
8996   /* Check instruction count requirements.  */
8997   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8998     {
8999       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
9000 	subtype |= RELAX_SECOND_LONGER;
9001       if (mips_opts.warn_about_macros)
9002 	subtype |= RELAX_NOMACRO;
9003       if (mips_macro_warning.delay_slot_p)
9004 	subtype |= RELAX_DELAY_SLOT;
9005     }
9006 
9007   /* If both alternatives fail to fill a delay slot correctly,
9008      emit the warning now.  */
9009   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
9010       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
9011     {
9012       relax_substateT s;
9013       const char *msg;
9014 
9015       s = subtype & (RELAX_DELAY_SLOT_16BIT
9016 		     | RELAX_DELAY_SLOT_SIZE_FIRST
9017 		     | RELAX_DELAY_SLOT_SIZE_SECOND);
9018       msg = macro_warning (s);
9019       if (msg != NULL)
9020 	as_warn ("%s", msg);
9021       subtype &= ~s;
9022     }
9023 
9024   /* If both implementations are longer than 1 instruction, then emit the
9025      warning now.  */
9026   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
9027     {
9028       relax_substateT s;
9029       const char *msg;
9030 
9031       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
9032       msg = macro_warning (s);
9033       if (msg != NULL)
9034 	as_warn ("%s", msg);
9035       subtype &= ~s;
9036     }
9037 
9038   /* If any flags still set, then one implementation might need a warning
9039      and the other either will need one of a different kind or none at all.
9040      Pass any remaining flags over to relaxation.  */
9041   if (mips_macro_warning.first_frag != NULL)
9042     mips_macro_warning.first_frag->fr_subtype |= subtype;
9043 }
9044 
9045 /* Instruction operand formats used in macros that vary between
9046    standard MIPS and microMIPS code.  */
9047 
9048 static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
9049 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
9050 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
9051 static const char * const lui_fmt[2] = { "t,u", "s,u" };
9052 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
9053 static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
9054 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
9055 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
9056 
9057 #define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
9058 #define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
9059 					     : cop12_fmt[mips_opts.micromips])
9060 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
9061 #define LUI_FMT (lui_fmt[mips_opts.micromips])
9062 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
9063 #define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
9064 					     : mem12_fmt[mips_opts.micromips])
9065 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
9066 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
9067 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
9068 
9069 /* Read a macro's relocation codes from *ARGS and store them in *R.
9070    The first argument in *ARGS will be either the code for a single
9071    relocation or -1 followed by the three codes that make up a
9072    composite relocation.  */
9073 
9074 static void
9075 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
9076 {
9077   int i, next;
9078 
9079   next = va_arg (*args, int);
9080   if (next >= 0)
9081     r[0] = (bfd_reloc_code_real_type) next;
9082   else
9083     {
9084       for (i = 0; i < 3; i++)
9085 	r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
9086       /* This function is only used for 16-bit relocation fields.
9087 	 To make the macro code simpler, treat an unrelocated value
9088 	 in the same way as BFD_RELOC_LO16.  */
9089       if (r[0] == BFD_RELOC_UNUSED)
9090 	r[0] = BFD_RELOC_LO16;
9091     }
9092 }
9093 
9094 /* Build an instruction created by a macro expansion.  This is passed
9095    a pointer to the count of instructions created so far, an
9096    expression, the name of the instruction to build, an operand format
9097    string, and corresponding arguments.  */
9098 
9099 static void
9100 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
9101 {
9102   const struct mips_opcode *mo = NULL;
9103   bfd_reloc_code_real_type r[3];
9104   const struct mips_opcode *amo;
9105   const struct mips_operand *operand;
9106   htab_t hash;
9107   struct mips_cl_insn insn;
9108   va_list args;
9109   unsigned int uval;
9110 
9111   va_start (args, fmt);
9112 
9113   if (mips_opts.mips16)
9114     {
9115       mips16_macro_build (ep, name, fmt, &args);
9116       va_end (args);
9117       return;
9118     }
9119 
9120   r[0] = BFD_RELOC_UNUSED;
9121   r[1] = BFD_RELOC_UNUSED;
9122   r[2] = BFD_RELOC_UNUSED;
9123   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
9124   amo = (struct mips_opcode *) str_hash_find (hash, name);
9125   gas_assert (amo);
9126   gas_assert (strcmp (name, amo->name) == 0);
9127 
9128   do
9129     {
9130       /* Search until we get a match for NAME.  It is assumed here that
9131 	 macros will never generate MDMX, MIPS-3D, or MT instructions.
9132 	 We try to match an instruction that fulfills the branch delay
9133 	 slot instruction length requirement (if any) of the previous
9134 	 instruction.  While doing this we record the first instruction
9135 	 seen that matches all the other conditions and use it anyway
9136 	 if the requirement cannot be met; we will issue an appropriate
9137 	 warning later on.  */
9138       if (strcmp (fmt, amo->args) == 0
9139 	  && amo->pinfo != INSN_MACRO
9140 	  && is_opcode_valid (amo)
9141 	  && is_size_valid (amo))
9142 	{
9143 	  if (is_delay_slot_valid (amo))
9144 	    {
9145 	      mo = amo;
9146 	      break;
9147 	    }
9148 	  else if (!mo)
9149 	    mo = amo;
9150 	}
9151 
9152       ++amo;
9153       gas_assert (amo->name);
9154     }
9155   while (strcmp (name, amo->name) == 0);
9156 
9157   gas_assert (mo);
9158   create_insn (&insn, mo);
9159   for (; *fmt; ++fmt)
9160     {
9161       switch (*fmt)
9162 	{
9163 	case ',':
9164 	case '(':
9165 	case ')':
9166 	case 'z':
9167 	  break;
9168 
9169 	case 'i':
9170 	case 'j':
9171 	  macro_read_relocs (&args, r);
9172 	  gas_assert (*r == BFD_RELOC_GPREL16
9173 		      || *r == BFD_RELOC_MIPS_HIGHER
9174 		      || *r == BFD_RELOC_HI16_S
9175 		      || *r == BFD_RELOC_LO16
9176 		      || *r == BFD_RELOC_MIPS_GOT_OFST
9177 		      || (mips_opts.micromips
9178 			  && (*r == BFD_RELOC_MIPS_16
9179 			      || *r == BFD_RELOC_MIPS_GOT16
9180 			      || *r == BFD_RELOC_MIPS_CALL16
9181 			      || *r == BFD_RELOC_MIPS_GOT_HI16
9182 			      || *r == BFD_RELOC_MIPS_GOT_LO16
9183 			      || *r == BFD_RELOC_MIPS_CALL_HI16
9184 			      || *r == BFD_RELOC_MIPS_CALL_LO16
9185 			      || *r == BFD_RELOC_MIPS_SUB
9186 			      || *r == BFD_RELOC_MIPS_GOT_PAGE
9187 			      || *r == BFD_RELOC_MIPS_HIGHEST
9188 			      || *r == BFD_RELOC_MIPS_GOT_DISP
9189 			      || *r == BFD_RELOC_MIPS_TLS_GD
9190 			      || *r == BFD_RELOC_MIPS_TLS_LDM
9191 			      || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9192 			      || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9193 			      || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9194 			      || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9195 			      || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
9196 	  break;
9197 
9198 	case 'o':
9199 	  macro_read_relocs (&args, r);
9200 	  break;
9201 
9202 	case 'u':
9203 	  macro_read_relocs (&args, r);
9204 	  gas_assert (ep != NULL
9205 		      && (ep->X_op == O_constant
9206 			  || (ep->X_op == O_symbol
9207 			      && (*r == BFD_RELOC_MIPS_HIGHEST
9208 				  || *r == BFD_RELOC_HI16_S
9209 				  || *r == BFD_RELOC_HI16
9210 				  || *r == BFD_RELOC_GPREL16
9211 				  || *r == BFD_RELOC_MIPS_GOT_HI16
9212 				  || *r == BFD_RELOC_MIPS_CALL_HI16))));
9213 	  break;
9214 
9215 	case 'p':
9216 	  gas_assert (ep != NULL);
9217 
9218 	  /*
9219 	   * This allows macro() to pass an immediate expression for
9220 	   * creating short branches without creating a symbol.
9221 	   *
9222 	   * We don't allow branch relaxation for these branches, as
9223 	   * they should only appear in ".set nomacro" anyway.
9224 	   */
9225 	  if (ep->X_op == O_constant)
9226 	    {
9227 	      /* For microMIPS we always use relocations for branches.
9228 	         So we should not resolve immediate values.  */
9229 	      gas_assert (!mips_opts.micromips);
9230 
9231 	      if ((ep->X_add_number & 3) != 0)
9232 		as_bad (_("branch to misaligned address (0x%lx)"),
9233 			(unsigned long) ep->X_add_number);
9234 	      if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9235 		as_bad (_("branch address range overflow (0x%lx)"),
9236 			(unsigned long) ep->X_add_number);
9237 	      insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9238 	      ep = NULL;
9239 	    }
9240 	  else
9241 	    *r = BFD_RELOC_16_PCREL_S2;
9242 	  break;
9243 
9244 	case 'a':
9245 	  gas_assert (ep != NULL);
9246 	  *r = BFD_RELOC_MIPS_JMP;
9247 	  break;
9248 
9249 	default:
9250 	  operand = (mips_opts.micromips
9251 		     ? decode_micromips_operand (fmt)
9252 		     : decode_mips_operand (fmt));
9253 	  if (!operand)
9254 	    abort ();
9255 
9256 	  uval = va_arg (args, int);
9257 	  if (operand->type == OP_CLO_CLZ_DEST)
9258 	    uval |= (uval << 5);
9259 	  insn_insert_operand (&insn, operand, uval);
9260 
9261 	  if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
9262 	    ++fmt;
9263 	  break;
9264 	}
9265     }
9266   va_end (args);
9267   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9268 
9269   append_insn (&insn, ep, r, true);
9270 }
9271 
9272 static void
9273 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
9274 		    va_list *args)
9275 {
9276   struct mips_opcode *mo;
9277   struct mips_cl_insn insn;
9278   const struct mips_operand *operand;
9279   bfd_reloc_code_real_type r[3]
9280     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
9281 
9282   mo = (struct mips_opcode *) str_hash_find (mips16_op_hash, name);
9283   gas_assert (mo);
9284   gas_assert (strcmp (name, mo->name) == 0);
9285 
9286   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
9287     {
9288       ++mo;
9289       gas_assert (mo->name);
9290       gas_assert (strcmp (name, mo->name) == 0);
9291     }
9292 
9293   create_insn (&insn, mo);
9294   for (; *fmt; ++fmt)
9295     {
9296       int c;
9297 
9298       c = *fmt;
9299       switch (c)
9300 	{
9301 	case ',':
9302 	case '(':
9303 	case ')':
9304 	  break;
9305 
9306 	case '.':
9307 	case 'S':
9308 	case 'P':
9309 	case 'R':
9310 	  break;
9311 
9312 	case '<':
9313 	case '5':
9314 	case 'F':
9315 	case 'H':
9316 	case 'W':
9317 	case 'D':
9318 	case 'j':
9319 	case '8':
9320 	case 'V':
9321 	case 'C':
9322 	case 'U':
9323 	case 'k':
9324 	case 'K':
9325 	case 'p':
9326 	case 'q':
9327 	  {
9328 	    offsetT value;
9329 
9330 	    gas_assert (ep != NULL);
9331 
9332 	    if (ep->X_op != O_constant)
9333 	      *r = (int) BFD_RELOC_UNUSED + c;
9334 	    else if (calculate_reloc (*r, ep->X_add_number, &value))
9335 	      {
9336 		mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
9337 		ep = NULL;
9338 		*r = BFD_RELOC_UNUSED;
9339 	      }
9340 	  }
9341 	  break;
9342 
9343 	default:
9344 	  operand = decode_mips16_operand (c, false);
9345 	  if (!operand)
9346 	    abort ();
9347 
9348 	  insn_insert_operand (&insn, operand, va_arg (*args, int));
9349 	  break;
9350 	}
9351     }
9352 
9353   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
9354 
9355   append_insn (&insn, ep, r, true);
9356 }
9357 
9358 /*
9359  * Generate a "jalr" instruction with a relocation hint to the called
9360  * function.  This occurs in NewABI PIC code.
9361  */
9362 static void
9363 macro_build_jalr (expressionS *ep, int cprestore)
9364 {
9365   static const bfd_reloc_code_real_type jalr_relocs[2]
9366     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9367   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9368   const char *jalr;
9369   char *f = NULL;
9370 
9371   if (MIPS_JALR_HINT_P (ep))
9372     {
9373       frag_grow (8);
9374       f = frag_more (0);
9375     }
9376   if (mips_opts.micromips)
9377     {
9378       jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9379 	      ? "jalr" : "jalrs");
9380       if (MIPS_JALR_HINT_P (ep)
9381 	  || mips_opts.insn32
9382 	  || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
9383 	macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9384       else
9385 	macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9386     }
9387   else
9388     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
9389   if (MIPS_JALR_HINT_P (ep))
9390     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, false, jalr_reloc);
9391 }
9392 
9393 /*
9394  * Generate a "lui" instruction.
9395  */
9396 static void
9397 macro_build_lui (expressionS *ep, int regnum)
9398 {
9399   gas_assert (! mips_opts.mips16);
9400 
9401   if (ep->X_op != O_constant)
9402     {
9403       gas_assert (ep->X_op == O_symbol);
9404       /* _gp_disp is a special case, used from s_cpload.
9405 	 __gnu_local_gp is used if mips_no_shared.  */
9406       gas_assert (mips_pic == NO_PIC
9407 	      || (! HAVE_NEWABI
9408 		  && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9409 	      || (! mips_in_shared
9410 		  && strcmp (S_GET_NAME (ep->X_add_symbol),
9411                              "__gnu_local_gp") == 0));
9412     }
9413 
9414   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
9415 }
9416 
9417 /* Generate a sequence of instructions to do a load or store from a constant
9418    offset off of a base register (breg) into/from a target register (treg),
9419    using AT if necessary.  */
9420 static void
9421 macro_build_ldst_constoffset (expressionS *ep, const char *op,
9422 			      int treg, int breg, int dbl)
9423 {
9424   gas_assert (ep->X_op == O_constant);
9425 
9426   /* Sign-extending 32-bit constants makes their handling easier.  */
9427   if (!dbl)
9428     normalize_constant_expr (ep);
9429 
9430   /* Right now, this routine can only handle signed 32-bit constants.  */
9431   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
9432     as_warn (_("operand overflow"));
9433 
9434   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9435     {
9436       /* Signed 16-bit offset will fit in the op.  Easy!  */
9437       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
9438     }
9439   else
9440     {
9441       /* 32-bit offset, need multiple instructions and AT, like:
9442 	   lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
9443 	   addu     $tempreg,$tempreg,$breg
9444            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
9445          to handle the complete offset.  */
9446       macro_build_lui (ep, AT);
9447       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9448       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
9449 
9450       if (!mips_opts.at)
9451 	as_bad (_("macro used $at after \".set noat\""));
9452     }
9453 }
9454 
9455 /*			set_at()
9456  * Generates code to set the $at register to true (one)
9457  * if reg is less than the immediate expression.
9458  */
9459 static void
9460 set_at (int reg, int unsignedp)
9461 {
9462   if (imm_expr.X_add_number >= -0x8000
9463       && imm_expr.X_add_number < 0x8000)
9464     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9465 		 AT, reg, BFD_RELOC_LO16);
9466   else
9467     {
9468       load_register (AT, &imm_expr, GPR_SIZE == 64);
9469       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
9470     }
9471 }
9472 
9473 /* Count the leading zeroes by performing a binary chop. This is a
9474    bulky bit of source, but performance is a LOT better for the
9475    majority of values than a simple loop to count the bits:
9476        for (lcnt = 0; (lcnt < 32); lcnt++)
9477          if ((v) & (1 << (31 - lcnt)))
9478            break;
9479   However it is not code size friendly, and the gain will drop a bit
9480   on certain cached systems.
9481 */
9482 #define COUNT_TOP_ZEROES(v)             \
9483   (((v) & ~0xffff) == 0                 \
9484    ? ((v) & ~0xff) == 0                 \
9485      ? ((v) & ~0xf) == 0                \
9486        ? ((v) & ~0x3) == 0              \
9487          ? ((v) & ~0x1) == 0            \
9488            ? !(v)                       \
9489              ? 32                       \
9490              : 31                       \
9491            : 30                         \
9492          : ((v) & ~0x7) == 0            \
9493            ? 29                         \
9494            : 28                         \
9495        : ((v) & ~0x3f) == 0             \
9496          ? ((v) & ~0x1f) == 0           \
9497            ? 27                         \
9498            : 26                         \
9499          : ((v) & ~0x7f) == 0           \
9500            ? 25                         \
9501            : 24                         \
9502      : ((v) & ~0xfff) == 0              \
9503        ? ((v) & ~0x3ff) == 0            \
9504          ? ((v) & ~0x1ff) == 0          \
9505            ? 23                         \
9506            : 22                         \
9507          : ((v) & ~0x7ff) == 0          \
9508            ? 21                         \
9509            : 20                         \
9510        : ((v) & ~0x3fff) == 0           \
9511          ? ((v) & ~0x1fff) == 0         \
9512            ? 19                         \
9513            : 18                         \
9514          : ((v) & ~0x7fff) == 0         \
9515            ? 17                         \
9516            : 16                         \
9517    : ((v) & ~0xffffff) == 0             \
9518      ? ((v) & ~0xfffff) == 0            \
9519        ? ((v) & ~0x3ffff) == 0          \
9520          ? ((v) & ~0x1ffff) == 0        \
9521            ? 15                         \
9522            : 14                         \
9523          : ((v) & ~0x7ffff) == 0        \
9524            ? 13                         \
9525            : 12                         \
9526        : ((v) & ~0x3fffff) == 0         \
9527          ? ((v) & ~0x1fffff) == 0       \
9528            ? 11                         \
9529            : 10                         \
9530          : ((v) & ~0x7fffff) == 0       \
9531            ? 9                          \
9532            : 8                          \
9533      : ((v) & ~0xfffffff) == 0          \
9534        ? ((v) & ~0x3ffffff) == 0        \
9535          ? ((v) & ~0x1ffffff) == 0      \
9536            ? 7                          \
9537            : 6                          \
9538          : ((v) & ~0x7ffffff) == 0      \
9539            ? 5                          \
9540            : 4                          \
9541        : ((v) & ~0x3fffffff) == 0       \
9542          ? ((v) & ~0x1fffffff) == 0     \
9543            ? 3                          \
9544            : 2                          \
9545          : ((v) & ~0x7fffffff) == 0     \
9546            ? 1                          \
9547            : 0)
9548 
9549 /*			load_register()
9550  *  This routine generates the least number of instructions necessary to load
9551  *  an absolute expression value into a register.
9552  */
9553 static void
9554 load_register (int reg, expressionS *ep, int dbl)
9555 {
9556   int freg;
9557   expressionS hi32, lo32;
9558 
9559   if (ep->X_op != O_big)
9560     {
9561       gas_assert (ep->X_op == O_constant);
9562 
9563       /* Sign-extending 32-bit constants makes their handling easier.  */
9564       if (!dbl)
9565 	normalize_constant_expr (ep);
9566 
9567       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
9568 	{
9569 	  /* We can handle 16 bit signed values with an addiu to
9570 	     $zero.  No need to ever use daddiu here, since $zero and
9571 	     the result are always correct in 32 bit mode.  */
9572 	  macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9573 	  return;
9574 	}
9575       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9576 	{
9577 	  /* We can handle 16 bit unsigned values with an ori to
9578              $zero.  */
9579 	  macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9580 	  return;
9581 	}
9582       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
9583 	{
9584 	  /* 32 bit values require an lui.  */
9585 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9586 	  if ((ep->X_add_number & 0xffff) != 0)
9587 	    macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9588 	  return;
9589 	}
9590     }
9591 
9592   /* The value is larger than 32 bits.  */
9593 
9594   if (!dbl || GPR_SIZE == 32)
9595     {
9596       char value[32];
9597 
9598       sprintf_vma (value, ep->X_add_number);
9599       as_bad (_("number (0x%s) larger than 32 bits"), value);
9600       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9601       return;
9602     }
9603 
9604   if (ep->X_op != O_big)
9605     {
9606       hi32 = *ep;
9607       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9608       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9609       hi32.X_add_number &= 0xffffffff;
9610       lo32 = *ep;
9611       lo32.X_add_number &= 0xffffffff;
9612     }
9613   else
9614     {
9615       gas_assert (ep->X_add_number > 2);
9616       if (ep->X_add_number == 3)
9617 	generic_bignum[3] = 0;
9618       else if (ep->X_add_number > 4)
9619 	as_bad (_("number larger than 64 bits"));
9620       lo32.X_op = O_constant;
9621       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9622       hi32.X_op = O_constant;
9623       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9624     }
9625 
9626   if (hi32.X_add_number == 0)
9627     freg = 0;
9628   else
9629     {
9630       int shift, bit;
9631       unsigned long hi, lo;
9632 
9633       if (hi32.X_add_number == (offsetT) 0xffffffff)
9634 	{
9635 	  if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9636 	    {
9637 	      macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9638 	      return;
9639 	    }
9640 	  if (lo32.X_add_number & 0x80000000)
9641 	    {
9642 	      macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9643 	      if (lo32.X_add_number & 0xffff)
9644 		macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
9645 	      return;
9646 	    }
9647 	}
9648 
9649       /* Check for 16bit shifted constant.  We know that hi32 is
9650          non-zero, so start the mask on the first bit of the hi32
9651          value.  */
9652       shift = 17;
9653       do
9654 	{
9655 	  unsigned long himask, lomask;
9656 
9657 	  if (shift < 32)
9658 	    {
9659 	      himask = 0xffff >> (32 - shift);
9660 	      lomask = (0xffffU << shift) & 0xffffffff;
9661 	    }
9662 	  else
9663 	    {
9664 	      himask = 0xffffU << (shift - 32);
9665 	      lomask = 0;
9666 	    }
9667 	  if ((hi32.X_add_number & ~(offsetT) himask) == 0
9668 	      && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9669 	    {
9670 	      expressionS tmp;
9671 
9672 	      tmp.X_op = O_constant;
9673 	      if (shift < 32)
9674 		tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9675 				    | (lo32.X_add_number >> shift));
9676 	      else
9677 		tmp.X_add_number = hi32.X_add_number >> (shift - 32);
9678 	      macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
9679 	      macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9680 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9681 	      return;
9682 	    }
9683 	  ++shift;
9684 	}
9685       while (shift <= (64 - 16));
9686 
9687       /* Find the bit number of the lowest one bit, and store the
9688          shifted value in hi/lo.  */
9689       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9690       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9691       if (lo != 0)
9692 	{
9693 	  bit = 0;
9694 	  while ((lo & 1) == 0)
9695 	    {
9696 	      lo >>= 1;
9697 	      ++bit;
9698 	    }
9699 	  if (bit != 0)
9700 	    {
9701 	      lo |= (hi & ((2UL << (bit - 1)) - 1)) << (32 - bit);
9702 	      hi >>= bit;
9703 	    }
9704 	}
9705       else
9706 	{
9707 	  bit = 32;
9708 	  while ((hi & 1) == 0)
9709 	    {
9710 	      hi >>= 1;
9711 	      ++bit;
9712 	    }
9713 	  lo = hi;
9714 	  hi = 0;
9715 	}
9716 
9717       /* Optimize if the shifted value is a (power of 2) - 1.  */
9718       if ((hi == 0 && ((lo + 1) & lo) == 0)
9719 	  || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
9720 	{
9721 	  shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
9722 	  if (shift != 0)
9723 	    {
9724 	      expressionS tmp;
9725 
9726 	      /* This instruction will set the register to be all
9727                  ones.  */
9728 	      tmp.X_op = O_constant;
9729 	      tmp.X_add_number = (offsetT) -1;
9730 	      macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
9731 	      if (bit != 0)
9732 		{
9733 		  bit += shift;
9734 		  macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
9735 			       reg, reg, (bit >= 32) ? bit - 32 : bit);
9736 		}
9737 	      macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
9738 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
9739 	      return;
9740 	    }
9741 	}
9742 
9743       /* Sign extend hi32 before calling load_register, because we can
9744          generally get better code when we load a sign extended value.  */
9745       if ((hi32.X_add_number & 0x80000000) != 0)
9746 	hi32.X_add_number |= ~(offsetT) 0xffffffff;
9747       load_register (reg, &hi32, 0);
9748       freg = reg;
9749     }
9750   if ((lo32.X_add_number & 0xffff0000) == 0)
9751     {
9752       if (freg != 0)
9753 	{
9754 	  macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
9755 	  freg = reg;
9756 	}
9757     }
9758   else
9759     {
9760       expressionS mid16;
9761 
9762       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
9763 	{
9764 	  macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9765 	  macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
9766 	  return;
9767 	}
9768 
9769       if (freg != 0)
9770 	{
9771 	  macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
9772 	  freg = reg;
9773 	}
9774       mid16 = lo32;
9775       mid16.X_add_number >>= 16;
9776       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9777       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9778       freg = reg;
9779     }
9780   if ((lo32.X_add_number & 0xffff) != 0)
9781     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
9782 }
9783 
9784 static inline void
9785 load_delay_nop (void)
9786 {
9787   if (!gpr_interlocks)
9788     macro_build (NULL, "nop", "");
9789 }
9790 
9791 /* Load an address into a register.  */
9792 
9793 static void
9794 load_address (int reg, expressionS *ep, int *used_at)
9795 {
9796   if (ep->X_op != O_constant
9797       && ep->X_op != O_symbol)
9798     {
9799       as_bad (_("expression too complex"));
9800       ep->X_op = O_constant;
9801     }
9802 
9803   if (ep->X_op == O_constant)
9804     {
9805       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
9806       return;
9807     }
9808 
9809   if (mips_pic == NO_PIC)
9810     {
9811       /* If this is a reference to a GP relative symbol, we want
9812 	   addiu	$reg,$gp,<sym>		(BFD_RELOC_GPREL16)
9813 	 Otherwise we want
9814 	   lui		$reg,<sym>		(BFD_RELOC_HI16_S)
9815 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9816 	 If we have an addend, we always use the latter form.
9817 
9818 	 With 64bit address space and a usable $at we want
9819 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9820 	   lui		$at,<sym>		(BFD_RELOC_HI16_S)
9821 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9822 	   daddiu	$at,<sym>		(BFD_RELOC_LO16)
9823 	   dsll32	$reg,0
9824 	   daddu	$reg,$reg,$at
9825 
9826 	 If $at is already in use, we use a path which is suboptimal
9827 	 on superscalar processors.
9828 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
9829 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
9830 	   dsll		$reg,16
9831 	   daddiu	$reg,<sym>		(BFD_RELOC_HI16_S)
9832 	   dsll		$reg,16
9833 	   daddiu	$reg,<sym>		(BFD_RELOC_LO16)
9834 
9835 	 For GP relative symbols in 64bit address space we can use
9836 	 the same sequence as in 32bit address space.  */
9837       if (HAVE_64BIT_SYMBOLS)
9838 	{
9839 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9840 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9841 	    {
9842 	      relax_start (ep->X_add_symbol);
9843 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9844 			   mips_gp_register, BFD_RELOC_GPREL16);
9845 	      relax_switch ();
9846 	    }
9847 
9848 	  if (*used_at == 0 && mips_opts.at)
9849 	    {
9850 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9851 	      macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
9852 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9853 			   BFD_RELOC_MIPS_HIGHER);
9854 	      macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
9855 	      macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
9856 	      macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
9857 	      *used_at = 1;
9858 	    }
9859 	  else
9860 	    {
9861 	      macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9862 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
9863 			   BFD_RELOC_MIPS_HIGHER);
9864 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9865 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
9866 	      macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
9867 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
9868 	    }
9869 
9870 	  if (mips_relax.sequence)
9871 	    relax_end ();
9872 	}
9873       else
9874 	{
9875 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9876 	      && !nopic_need_relax (ep->X_add_symbol, 1))
9877 	    {
9878 	      relax_start (ep->X_add_symbol);
9879 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9880 			   mips_gp_register, BFD_RELOC_GPREL16);
9881 	      relax_switch ();
9882 	    }
9883 	  macro_build_lui (ep, reg);
9884 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9885 		       reg, reg, BFD_RELOC_LO16);
9886 	  if (mips_relax.sequence)
9887 	    relax_end ();
9888 	}
9889     }
9890   else if (!mips_big_got)
9891     {
9892       expressionS ex;
9893 
9894       /* If this is a reference to an external symbol, we want
9895 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9896 	 Otherwise we want
9897 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9898 	   nop
9899 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9900 	 If there is a constant, it must be added in after.
9901 
9902 	 If we have NewABI, we want
9903 	   lw		$reg,<sym+cst>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
9904          unless we're referencing a global symbol with a non-zero
9905          offset, in which case cst must be added separately.  */
9906       if (HAVE_NEWABI)
9907 	{
9908 	  if (ep->X_add_number)
9909 	    {
9910 	      ex.X_add_number = ep->X_add_number;
9911 	      ep->X_add_number = 0;
9912 	      relax_start (ep->X_add_symbol);
9913 	      macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9914 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9915 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9916 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9917 	      ex.X_op = O_constant;
9918 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9919 			   reg, reg, BFD_RELOC_LO16);
9920 	      ep->X_add_number = ex.X_add_number;
9921 	      relax_switch ();
9922 	    }
9923 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9924 		       BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
9925 	  if (mips_relax.sequence)
9926 	    relax_end ();
9927 	}
9928       else
9929 	{
9930 	  ex.X_add_number = ep->X_add_number;
9931 	  ep->X_add_number = 0;
9932 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9933 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
9934 	  load_delay_nop ();
9935 	  relax_start (ep->X_add_symbol);
9936 	  relax_switch ();
9937 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9938 		       BFD_RELOC_LO16);
9939 	  relax_end ();
9940 
9941 	  if (ex.X_add_number != 0)
9942 	    {
9943 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9944 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9945 	      ex.X_op = O_constant;
9946 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
9947 			   reg, reg, BFD_RELOC_LO16);
9948 	    }
9949 	}
9950     }
9951   else if (mips_big_got)
9952     {
9953       expressionS ex;
9954 
9955       /* This is the large GOT case.  If this is a reference to an
9956 	 external symbol, we want
9957 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
9958 	   addu		$reg,$reg,$gp
9959 	   lw		$reg,<sym>($reg)	(BFD_RELOC_MIPS_GOT_LO16)
9960 
9961 	 Otherwise, for a reference to a local symbol in old ABI, we want
9962 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
9963 	   nop
9964 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
9965 	 If there is a constant, it must be added in after.
9966 
9967 	 In the NewABI, for local symbols, with or without offsets, we want:
9968 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
9969 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
9970       */
9971       if (HAVE_NEWABI)
9972 	{
9973 	  ex.X_add_number = ep->X_add_number;
9974 	  ep->X_add_number = 0;
9975 	  relax_start (ep->X_add_symbol);
9976 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
9977 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9978 		       reg, reg, mips_gp_register);
9979 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9980 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
9981 	  if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9982 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9983 	  else if (ex.X_add_number)
9984 	    {
9985 	      ex.X_op = O_constant;
9986 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9987 			   BFD_RELOC_LO16);
9988 	    }
9989 
9990 	  ep->X_add_number = ex.X_add_number;
9991 	  relax_switch ();
9992 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9993 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
9994 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9995 		       BFD_RELOC_MIPS_GOT_OFST);
9996 	  relax_end ();
9997 	}
9998       else
9999 	{
10000 	  ex.X_add_number = ep->X_add_number;
10001 	  ep->X_add_number = 0;
10002 	  relax_start (ep->X_add_symbol);
10003 	  macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
10004 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10005 		       reg, reg, mips_gp_register);
10006 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
10007 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
10008 	  relax_switch ();
10009 	  if (reg_needs_delay (mips_gp_register))
10010 	    {
10011 	      /* We need a nop before loading from $gp.  This special
10012 		 check is required because the lui which starts the main
10013 		 instruction stream does not refer to $gp, and so will not
10014 		 insert the nop which may be required.  */
10015 	      macro_build (NULL, "nop", "");
10016 	    }
10017 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
10018 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
10019 	  load_delay_nop ();
10020 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
10021 		       BFD_RELOC_LO16);
10022 	  relax_end ();
10023 
10024 	  if (ex.X_add_number != 0)
10025 	    {
10026 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
10027 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
10028 	      ex.X_op = O_constant;
10029 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
10030 			   BFD_RELOC_LO16);
10031 	    }
10032 	}
10033     }
10034   else
10035     abort ();
10036 
10037   if (!mips_opts.at && *used_at == 1)
10038     as_bad (_("macro used $at after \".set noat\""));
10039 }
10040 
10041 /* Move the contents of register SOURCE into register DEST.  */
10042 
10043 static void
10044 move_register (int dest, int source)
10045 {
10046   /* Prefer to use a 16-bit microMIPS instruction unless the previous
10047      instruction specifically requires a 32-bit one.  */
10048   if (mips_opts.micromips
10049       && !mips_opts.insn32
10050       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
10051     macro_build (NULL, "move", "mp,mj", dest, source);
10052   else
10053     macro_build (NULL, "or", "d,v,t", dest, source, 0);
10054 }
10055 
10056 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
10057    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
10058    The two alternatives are:
10059 
10060    Global symbol		Local symbol
10061    -------------		------------
10062    lw DEST,%got(SYMBOL)		lw DEST,%got(SYMBOL + OFFSET)
10063    ...				...
10064    addiu DEST,DEST,OFFSET	addiu DEST,DEST,%lo(SYMBOL + OFFSET)
10065 
10066    load_got_offset emits the first instruction and add_got_offset
10067    emits the second for a 16-bit offset or add_got_offset_hilo emits
10068    a sequence to add a 32-bit offset using a scratch register.  */
10069 
10070 static void
10071 load_got_offset (int dest, expressionS *local)
10072 {
10073   expressionS global;
10074 
10075   global = *local;
10076   global.X_add_number = 0;
10077 
10078   relax_start (local->X_add_symbol);
10079   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
10080 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
10081   relax_switch ();
10082   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
10083 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
10084   relax_end ();
10085 }
10086 
10087 static void
10088 add_got_offset (int dest, expressionS *local)
10089 {
10090   expressionS global;
10091 
10092   global.X_op = O_constant;
10093   global.X_op_symbol = NULL;
10094   global.X_add_symbol = NULL;
10095   global.X_add_number = local->X_add_number;
10096 
10097   relax_start (local->X_add_symbol);
10098   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
10099 	       dest, dest, BFD_RELOC_LO16);
10100   relax_switch ();
10101   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
10102   relax_end ();
10103 }
10104 
10105 static void
10106 add_got_offset_hilo (int dest, expressionS *local, int tmp)
10107 {
10108   expressionS global;
10109   int hold_mips_optimize;
10110 
10111   global.X_op = O_constant;
10112   global.X_op_symbol = NULL;
10113   global.X_add_symbol = NULL;
10114   global.X_add_number = local->X_add_number;
10115 
10116   relax_start (local->X_add_symbol);
10117   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10118   relax_switch ();
10119   /* Set mips_optimize around the lui instruction to avoid
10120      inserting an unnecessary nop after the lw.  */
10121   hold_mips_optimize = mips_optimize;
10122   mips_optimize = 2;
10123   macro_build_lui (&global, tmp);
10124   mips_optimize = hold_mips_optimize;
10125   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10126   relax_end ();
10127 
10128   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10129 }
10130 
10131 /* Emit a sequence of instructions to emulate a branch likely operation.
10132    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
10133    is its complementing branch with the original condition negated.
10134    CALL is set if the original branch specified the link operation.
10135    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10136 
10137    Code like this is produced in the noreorder mode:
10138 
10139 	BRNEG	<args>, 1f
10140 	 nop
10141 	b	<sym>
10142 	 delay slot (executed only if branch taken)
10143     1:
10144 
10145    or, if CALL is set:
10146 
10147 	BRNEG	<args>, 1f
10148 	 nop
10149 	bal	<sym>
10150 	 delay slot (executed only if branch taken)
10151     1:
10152 
10153    In the reorder mode the delay slot would be filled with a nop anyway,
10154    so code produced is simply:
10155 
10156 	BR	<args>, <sym>
10157 	 nop
10158 
10159    This function is used when producing code for the microMIPS ASE that
10160    does not implement branch likely instructions in hardware.  */
10161 
10162 static void
10163 macro_build_branch_likely (const char *br, const char *brneg,
10164 			   int call, expressionS *ep, const char *fmt,
10165 			   unsigned int sreg, unsigned int treg)
10166 {
10167   int noreorder = mips_opts.noreorder;
10168   expressionS expr1;
10169 
10170   gas_assert (mips_opts.micromips);
10171   start_noreorder ();
10172   if (noreorder)
10173     {
10174       micromips_label_expr (&expr1);
10175       macro_build (&expr1, brneg, fmt, sreg, treg);
10176       macro_build (NULL, "nop", "");
10177       macro_build (ep, call ? "bal" : "b", "p");
10178 
10179       /* Set to true so that append_insn adds a label.  */
10180       emit_branch_likely_macro = true;
10181     }
10182   else
10183     {
10184       macro_build (ep, br, fmt, sreg, treg);
10185       macro_build (NULL, "nop", "");
10186     }
10187   end_noreorder ();
10188 }
10189 
10190 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10191    the condition code tested.  EP specifies the branch target.  */
10192 
10193 static void
10194 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10195 {
10196   const int call = 0;
10197   const char *brneg;
10198   const char *br;
10199 
10200   switch (type)
10201     {
10202     case M_BC1FL:
10203       br = "bc1f";
10204       brneg = "bc1t";
10205       break;
10206     case M_BC1TL:
10207       br = "bc1t";
10208       brneg = "bc1f";
10209       break;
10210     case M_BC2FL:
10211       br = "bc2f";
10212       brneg = "bc2t";
10213       break;
10214     case M_BC2TL:
10215       br = "bc2t";
10216       brneg = "bc2f";
10217       break;
10218     default:
10219       abort ();
10220     }
10221   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10222 }
10223 
10224 /* Emit a two-argument branch macro specified by TYPE, using SREG as
10225    the register tested.  EP specifies the branch target.  */
10226 
10227 static void
10228 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10229 {
10230   const char *brneg = NULL;
10231   const char *br;
10232   int call = 0;
10233 
10234   switch (type)
10235     {
10236     case M_BGEZ:
10237       br = "bgez";
10238       break;
10239     case M_BGEZL:
10240       br = mips_opts.micromips ? "bgez" : "bgezl";
10241       brneg = "bltz";
10242       break;
10243     case M_BGEZALL:
10244       gas_assert (mips_opts.micromips);
10245       br = mips_opts.insn32 ? "bgezal" : "bgezals";
10246       brneg = "bltz";
10247       call = 1;
10248       break;
10249     case M_BGTZ:
10250       br = "bgtz";
10251       break;
10252     case M_BGTZL:
10253       br = mips_opts.micromips ? "bgtz" : "bgtzl";
10254       brneg = "blez";
10255       break;
10256     case M_BLEZ:
10257       br = "blez";
10258       break;
10259     case M_BLEZL:
10260       br = mips_opts.micromips ? "blez" : "blezl";
10261       brneg = "bgtz";
10262       break;
10263     case M_BLTZ:
10264       br = "bltz";
10265       break;
10266     case M_BLTZL:
10267       br = mips_opts.micromips ? "bltz" : "bltzl";
10268       brneg = "bgez";
10269       break;
10270     case M_BLTZALL:
10271       gas_assert (mips_opts.micromips);
10272       br = mips_opts.insn32 ? "bltzal" : "bltzals";
10273       brneg = "bgez";
10274       call = 1;
10275       break;
10276     default:
10277       abort ();
10278     }
10279   if (mips_opts.micromips && brneg)
10280     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10281   else
10282     macro_build (ep, br, "s,p", sreg);
10283 }
10284 
10285 /* Emit a three-argument branch macro specified by TYPE, using SREG and
10286    TREG as the registers tested.  EP specifies the branch target.  */
10287 
10288 static void
10289 macro_build_branch_rsrt (int type, expressionS *ep,
10290 			 unsigned int sreg, unsigned int treg)
10291 {
10292   const char *brneg = NULL;
10293   const int call = 0;
10294   const char *br;
10295 
10296   switch (type)
10297     {
10298     case M_BEQ:
10299     case M_BEQ_I:
10300       br = "beq";
10301       break;
10302     case M_BEQL:
10303     case M_BEQL_I:
10304       br = mips_opts.micromips ? "beq" : "beql";
10305       brneg = "bne";
10306       break;
10307     case M_BNE:
10308     case M_BNE_I:
10309       br = "bne";
10310       break;
10311     case M_BNEL:
10312     case M_BNEL_I:
10313       br = mips_opts.micromips ? "bne" : "bnel";
10314       brneg = "beq";
10315       break;
10316     default:
10317       abort ();
10318     }
10319   if (mips_opts.micromips && brneg)
10320     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10321   else
10322     macro_build (ep, br, "s,t,p", sreg, treg);
10323 }
10324 
10325 /* Return the high part that should be loaded in order to make the low
10326    part of VALUE accessible using an offset of OFFBITS bits.  */
10327 
10328 static offsetT
10329 offset_high_part (offsetT value, unsigned int offbits)
10330 {
10331   offsetT bias;
10332   addressT low_mask;
10333 
10334   if (offbits == 0)
10335     return value;
10336   bias = 1 << (offbits - 1);
10337   low_mask = bias * 2 - 1;
10338   return (value + bias) & ~low_mask;
10339 }
10340 
10341 /* Return true if the value stored in offset_expr and offset_reloc
10342    fits into a signed offset of OFFBITS bits.  RANGE is the maximum
10343    amount that the caller wants to add without inducing overflow
10344    and ALIGN is the known alignment of the value in bytes.  */
10345 
10346 static bool
10347 small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10348 {
10349   if (offbits == 16)
10350     {
10351       /* Accept any relocation operator if overflow isn't a concern.  */
10352       if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10353 	return true;
10354 
10355       /* These relocations are guaranteed not to overflow in correct links.  */
10356       if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10357 	  || gprel16_reloc_p (*offset_reloc))
10358 	return true;
10359     }
10360   if (offset_expr.X_op == O_constant
10361       && offset_high_part (offset_expr.X_add_number, offbits) == 0
10362       && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10363     return true;
10364   return false;
10365 }
10366 
10367 /*
10368  *			Build macros
10369  *   This routine implements the seemingly endless macro or synthesized
10370  * instructions and addressing modes in the mips assembly language. Many
10371  * of these macros are simple and are similar to each other. These could
10372  * probably be handled by some kind of table or grammar approach instead of
10373  * this verbose method. Others are not simple macros but are more like
10374  * optimizing code generation.
10375  *   One interesting optimization is when several store macros appear
10376  * consecutively that would load AT with the upper half of the same address.
10377  * The ensuing load upper instructions are omitted. This implies some kind
10378  * of global optimization. We currently only optimize within a single macro.
10379  *   For many of the load and store macros if the address is specified as a
10380  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10381  * first load register 'at' with zero and use it as the base register. The
10382  * mips assembler simply uses register $zero. Just one tiny optimization
10383  * we're missing.
10384  */
10385 static void
10386 macro (struct mips_cl_insn *ip, char *str)
10387 {
10388   const struct mips_operand_array *operands;
10389   unsigned int breg, i;
10390   unsigned int tempreg;
10391   int mask;
10392   int used_at = 0;
10393   expressionS label_expr;
10394   expressionS expr1;
10395   expressionS *ep;
10396   const char *s;
10397   const char *s2;
10398   const char *fmt;
10399   int likely = 0;
10400   int coproc = 0;
10401   int offbits = 16;
10402   int call = 0;
10403   int jals = 0;
10404   int dbl = 0;
10405   int imm = 0;
10406   int ust = 0;
10407   int lp = 0;
10408   int ll_sc_paired = 0;
10409   bool large_offset;
10410   int off;
10411   int hold_mips_optimize;
10412   unsigned int align;
10413   unsigned int op[MAX_OPERANDS];
10414 
10415   gas_assert (! mips_opts.mips16);
10416 
10417   operands = insn_operands (ip);
10418   for (i = 0; i < MAX_OPERANDS; i++)
10419     if (operands->operand[i])
10420       op[i] = insn_extract_operand (ip, operands->operand[i]);
10421     else
10422       op[i] = -1;
10423 
10424   mask = ip->insn_mo->mask;
10425 
10426   label_expr.X_op = O_constant;
10427   label_expr.X_op_symbol = NULL;
10428   label_expr.X_add_symbol = NULL;
10429   label_expr.X_add_number = 0;
10430 
10431   expr1.X_op = O_constant;
10432   expr1.X_op_symbol = NULL;
10433   expr1.X_add_symbol = NULL;
10434   expr1.X_add_number = 1;
10435   align = 1;
10436 
10437   switch (mask)
10438     {
10439     case M_DABS:
10440       dbl = 1;
10441       /* Fall through.  */
10442     case M_ABS:
10443       /*    bgez    $a0,1f
10444 	    move    v0,$a0
10445 	    sub     v0,$zero,$a0
10446 	 1:
10447        */
10448 
10449       start_noreorder ();
10450 
10451       if (mips_opts.micromips)
10452 	micromips_label_expr (&label_expr);
10453       else
10454 	label_expr.X_add_number = 8;
10455       macro_build (&label_expr, "bgez", "s,p", op[1]);
10456       if (op[0] == op[1])
10457 	macro_build (NULL, "nop", "");
10458       else
10459 	move_register (op[0], op[1]);
10460       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
10461       if (mips_opts.micromips)
10462 	micromips_add_label ();
10463 
10464       end_noreorder ();
10465       break;
10466 
10467     case M_ADD_I:
10468       s = "addi";
10469       s2 = "add";
10470       if (ISA_IS_R6 (mips_opts.isa))
10471 	goto do_addi_i;
10472       else
10473 	goto do_addi;
10474     case M_ADDU_I:
10475       s = "addiu";
10476       s2 = "addu";
10477       goto do_addi;
10478     case M_DADD_I:
10479       dbl = 1;
10480       s = "daddi";
10481       s2 = "dadd";
10482       if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
10483 	goto do_addi;
10484       if (imm_expr.X_add_number >= -0x200
10485 	  && imm_expr.X_add_number < 0x200
10486 	  && !ISA_IS_R6 (mips_opts.isa))
10487 	{
10488 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
10489 		       (int) imm_expr.X_add_number);
10490 	  break;
10491 	}
10492       goto do_addi_i;
10493     case M_DADDU_I:
10494       dbl = 1;
10495       s = "daddiu";
10496       s2 = "daddu";
10497     do_addi:
10498       if (imm_expr.X_add_number >= -0x8000
10499 	  && imm_expr.X_add_number < 0x8000)
10500 	{
10501 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
10502 	  break;
10503 	}
10504     do_addi_i:
10505       used_at = 1;
10506       load_register (AT, &imm_expr, dbl);
10507       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10508       break;
10509 
10510     case M_AND_I:
10511       s = "andi";
10512       s2 = "and";
10513       goto do_bit;
10514     case M_OR_I:
10515       s = "ori";
10516       s2 = "or";
10517       goto do_bit;
10518     case M_NOR_I:
10519       s = "";
10520       s2 = "nor";
10521       goto do_bit;
10522     case M_XOR_I:
10523       s = "xori";
10524       s2 = "xor";
10525     do_bit:
10526       if (imm_expr.X_add_number >= 0
10527 	  && imm_expr.X_add_number < 0x10000)
10528 	{
10529 	  if (mask != M_NOR_I)
10530 	    macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
10531 	  else
10532 	    {
10533 	      macro_build (&imm_expr, "ori", "t,r,i",
10534 			   op[0], op[1], BFD_RELOC_LO16);
10535 	      macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
10536 	    }
10537 	  break;
10538 	}
10539 
10540       used_at = 1;
10541       load_register (AT, &imm_expr, GPR_SIZE == 64);
10542       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
10543       break;
10544 
10545     case M_BALIGN:
10546       switch (imm_expr.X_add_number)
10547 	{
10548 	case 0:
10549 	  macro_build (NULL, "nop", "");
10550 	  break;
10551 	case 2:
10552 	  macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
10553 	  break;
10554 	case 1:
10555 	case 3:
10556 	  macro_build (NULL, "balign", "t,s,2", op[0], op[1],
10557 		       (int) imm_expr.X_add_number);
10558 	  break;
10559 	default:
10560 	  as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10561 		  (unsigned long) imm_expr.X_add_number);
10562 	  break;
10563 	}
10564       break;
10565 
10566     case M_BC1FL:
10567     case M_BC1TL:
10568     case M_BC2FL:
10569     case M_BC2TL:
10570       gas_assert (mips_opts.micromips);
10571       macro_build_branch_ccl (mask, &offset_expr,
10572 			      EXTRACT_OPERAND (1, BCC, *ip));
10573       break;
10574 
10575     case M_BEQ_I:
10576     case M_BEQL_I:
10577     case M_BNE_I:
10578     case M_BNEL_I:
10579       if (imm_expr.X_add_number == 0)
10580 	op[1] = 0;
10581       else
10582 	{
10583 	  op[1] = AT;
10584 	  used_at = 1;
10585 	  load_register (op[1], &imm_expr, GPR_SIZE == 64);
10586 	}
10587       /* Fall through.  */
10588     case M_BEQL:
10589     case M_BNEL:
10590       macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
10591       break;
10592 
10593     case M_BGEL:
10594       likely = 1;
10595       /* Fall through.  */
10596     case M_BGE:
10597       if (op[1] == 0)
10598 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10599       else if (op[0] == 0)
10600 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
10601       else
10602 	{
10603 	  used_at = 1;
10604 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10605 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10606 				   &offset_expr, AT, ZERO);
10607 	}
10608       break;
10609 
10610     case M_BGEZL:
10611     case M_BGEZALL:
10612     case M_BGTZL:
10613     case M_BLEZL:
10614     case M_BLTZL:
10615     case M_BLTZALL:
10616       macro_build_branch_rs (mask, &offset_expr, op[0]);
10617       break;
10618 
10619     case M_BGTL_I:
10620       likely = 1;
10621       /* Fall through.  */
10622     case M_BGT_I:
10623       /* Check for > max integer.  */
10624       if (imm_expr.X_add_number >= GPR_SMAX)
10625 	{
10626 	do_false:
10627 	  /* Result is always false.  */
10628 	  if (! likely)
10629 	    macro_build (NULL, "nop", "");
10630 	  else
10631 	    macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
10632 	  break;
10633 	}
10634       ++imm_expr.X_add_number;
10635       /* Fall through.  */
10636     case M_BGE_I:
10637     case M_BGEL_I:
10638       if (mask == M_BGEL_I)
10639 	likely = 1;
10640       if (imm_expr.X_add_number == 0)
10641 	{
10642 	  macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
10643 				 &offset_expr, op[0]);
10644 	  break;
10645 	}
10646       if (imm_expr.X_add_number == 1)
10647 	{
10648 	  macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
10649 				 &offset_expr, op[0]);
10650 	  break;
10651 	}
10652       if (imm_expr.X_add_number <= GPR_SMIN)
10653 	{
10654 	do_true:
10655 	  /* Result is always true.  */
10656 	  as_warn (_("branch %s is always true"), ip->insn_mo->name);
10657 	  macro_build (&offset_expr, "b", "p");
10658 	  break;
10659 	}
10660       used_at = 1;
10661       set_at (op[0], 0);
10662       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10663 			       &offset_expr, AT, ZERO);
10664       break;
10665 
10666     case M_BGEUL:
10667       likely = 1;
10668       /* Fall through.  */
10669     case M_BGEU:
10670       if (op[1] == 0)
10671 	goto do_true;
10672       else if (op[0] == 0)
10673 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10674 				 &offset_expr, ZERO, op[1]);
10675       else
10676 	{
10677 	  used_at = 1;
10678 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10679 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10680 				   &offset_expr, AT, ZERO);
10681 	}
10682       break;
10683 
10684     case M_BGTUL_I:
10685       likely = 1;
10686       /* Fall through.  */
10687     case M_BGTU_I:
10688       if (op[0] == 0
10689 	  || (GPR_SIZE == 32
10690 	      && imm_expr.X_add_number == -1))
10691 	goto do_false;
10692       ++imm_expr.X_add_number;
10693       /* Fall through.  */
10694     case M_BGEU_I:
10695     case M_BGEUL_I:
10696       if (mask == M_BGEUL_I)
10697 	likely = 1;
10698       if (imm_expr.X_add_number == 0)
10699 	goto do_true;
10700       else if (imm_expr.X_add_number == 1)
10701 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10702 				 &offset_expr, op[0], ZERO);
10703       else
10704 	{
10705 	  used_at = 1;
10706 	  set_at (op[0], 1);
10707 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10708 				   &offset_expr, AT, ZERO);
10709 	}
10710       break;
10711 
10712     case M_BGTL:
10713       likely = 1;
10714       /* Fall through.  */
10715     case M_BGT:
10716       if (op[1] == 0)
10717 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10718       else if (op[0] == 0)
10719 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
10720       else
10721 	{
10722 	  used_at = 1;
10723 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10724 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10725 				   &offset_expr, AT, ZERO);
10726 	}
10727       break;
10728 
10729     case M_BGTUL:
10730       likely = 1;
10731       /* Fall through.  */
10732     case M_BGTU:
10733       if (op[1] == 0)
10734 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10735 				 &offset_expr, op[0], ZERO);
10736       else if (op[0] == 0)
10737 	goto do_false;
10738       else
10739 	{
10740 	  used_at = 1;
10741 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10742 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10743 				   &offset_expr, AT, ZERO);
10744 	}
10745       break;
10746 
10747     case M_BLEL:
10748       likely = 1;
10749       /* Fall through.  */
10750     case M_BLE:
10751       if (op[1] == 0)
10752 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10753       else if (op[0] == 0)
10754 	macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
10755       else
10756 	{
10757 	  used_at = 1;
10758 	  macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
10759 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10760 				   &offset_expr, AT, ZERO);
10761 	}
10762       break;
10763 
10764     case M_BLEL_I:
10765       likely = 1;
10766       /* Fall through.  */
10767     case M_BLE_I:
10768       if (imm_expr.X_add_number >= GPR_SMAX)
10769 	goto do_true;
10770       ++imm_expr.X_add_number;
10771       /* Fall through.  */
10772     case M_BLT_I:
10773     case M_BLTL_I:
10774       if (mask == M_BLTL_I)
10775 	likely = 1;
10776       if (imm_expr.X_add_number == 0)
10777 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10778       else if (imm_expr.X_add_number == 1)
10779 	macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10780       else
10781 	{
10782 	  used_at = 1;
10783 	  set_at (op[0], 0);
10784 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10785 				   &offset_expr, AT, ZERO);
10786 	}
10787       break;
10788 
10789     case M_BLEUL:
10790       likely = 1;
10791       /* Fall through.  */
10792     case M_BLEU:
10793       if (op[1] == 0)
10794 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10795 				 &offset_expr, op[0], ZERO);
10796       else if (op[0] == 0)
10797 	goto do_true;
10798       else
10799 	{
10800 	  used_at = 1;
10801 	  macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
10802 	  macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10803 				   &offset_expr, AT, ZERO);
10804 	}
10805       break;
10806 
10807     case M_BLEUL_I:
10808       likely = 1;
10809       /* Fall through.  */
10810     case M_BLEU_I:
10811       if (op[0] == 0
10812 	  || (GPR_SIZE == 32
10813 	      && imm_expr.X_add_number == -1))
10814 	goto do_true;
10815       ++imm_expr.X_add_number;
10816       /* Fall through.  */
10817     case M_BLTU_I:
10818     case M_BLTUL_I:
10819       if (mask == M_BLTUL_I)
10820 	likely = 1;
10821       if (imm_expr.X_add_number == 0)
10822 	goto do_false;
10823       else if (imm_expr.X_add_number == 1)
10824 	macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10825 				 &offset_expr, op[0], ZERO);
10826       else
10827 	{
10828 	  used_at = 1;
10829 	  set_at (op[0], 1);
10830 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10831 				   &offset_expr, AT, ZERO);
10832 	}
10833       break;
10834 
10835     case M_BLTL:
10836       likely = 1;
10837       /* Fall through.  */
10838     case M_BLT:
10839       if (op[1] == 0)
10840 	macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10841       else if (op[0] == 0)
10842 	macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
10843       else
10844 	{
10845 	  used_at = 1;
10846 	  macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
10847 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10848 				   &offset_expr, AT, ZERO);
10849 	}
10850       break;
10851 
10852     case M_BLTUL:
10853       likely = 1;
10854       /* Fall through.  */
10855     case M_BLTU:
10856       if (op[1] == 0)
10857 	goto do_false;
10858       else if (op[0] == 0)
10859 	macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10860 				 &offset_expr, ZERO, op[1]);
10861       else
10862 	{
10863 	  used_at = 1;
10864 	  macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
10865 	  macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10866 				   &offset_expr, AT, ZERO);
10867 	}
10868       break;
10869 
10870     case M_DDIV_3:
10871       dbl = 1;
10872       /* Fall through.  */
10873     case M_DIV_3:
10874       s = "mflo";
10875       goto do_div3;
10876     case M_DREM_3:
10877       dbl = 1;
10878       /* Fall through.  */
10879     case M_REM_3:
10880       s = "mfhi";
10881     do_div3:
10882       if (op[2] == 0)
10883 	{
10884 	  as_warn (_("divide by zero"));
10885 	  if (mips_trap)
10886 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10887 	  else
10888 	    macro_build (NULL, "break", BRK_FMT, 7);
10889 	  break;
10890 	}
10891 
10892       start_noreorder ();
10893       if (mips_trap)
10894 	{
10895 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10896 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10897 	}
10898       else
10899 	{
10900 	  if (mips_opts.micromips)
10901 	    micromips_label_expr (&label_expr);
10902 	  else
10903 	    label_expr.X_add_number = 8;
10904 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10905 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
10906 	  macro_build (NULL, "break", BRK_FMT, 7);
10907 	  if (mips_opts.micromips)
10908 	    micromips_add_label ();
10909 	}
10910       expr1.X_add_number = -1;
10911       used_at = 1;
10912       load_register (AT, &expr1, dbl);
10913       if (mips_opts.micromips)
10914 	micromips_label_expr (&label_expr);
10915       else
10916 	label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
10917       macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
10918       if (dbl)
10919 	{
10920 	  expr1.X_add_number = 1;
10921 	  load_register (AT, &expr1, dbl);
10922 	  macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
10923 	}
10924       else
10925 	{
10926 	  expr1.X_add_number = 0x80000000;
10927 	  macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
10928 	}
10929       if (mips_trap)
10930 	{
10931 	  macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
10932 	  /* We want to close the noreorder block as soon as possible, so
10933 	     that later insns are available for delay slot filling.  */
10934 	  end_noreorder ();
10935 	}
10936       else
10937 	{
10938 	  if (mips_opts.micromips)
10939 	    micromips_label_expr (&label_expr);
10940 	  else
10941 	    label_expr.X_add_number = 8;
10942 	  macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
10943 	  macro_build (NULL, "nop", "");
10944 
10945 	  /* We want to close the noreorder block as soon as possible, so
10946 	     that later insns are available for delay slot filling.  */
10947 	  end_noreorder ();
10948 
10949 	  macro_build (NULL, "break", BRK_FMT, 6);
10950 	}
10951       if (mips_opts.micromips)
10952 	micromips_add_label ();
10953       macro_build (NULL, s, MFHL_FMT, op[0]);
10954       break;
10955 
10956     case M_DIV_3I:
10957       s = "div";
10958       s2 = "mflo";
10959       goto do_divi;
10960     case M_DIVU_3I:
10961       s = "divu";
10962       s2 = "mflo";
10963       goto do_divi;
10964     case M_REM_3I:
10965       s = "div";
10966       s2 = "mfhi";
10967       goto do_divi;
10968     case M_REMU_3I:
10969       s = "divu";
10970       s2 = "mfhi";
10971       goto do_divi;
10972     case M_DDIV_3I:
10973       dbl = 1;
10974       s = "ddiv";
10975       s2 = "mflo";
10976       goto do_divi;
10977     case M_DDIVU_3I:
10978       dbl = 1;
10979       s = "ddivu";
10980       s2 = "mflo";
10981       goto do_divi;
10982     case M_DREM_3I:
10983       dbl = 1;
10984       s = "ddiv";
10985       s2 = "mfhi";
10986       goto do_divi;
10987     case M_DREMU_3I:
10988       dbl = 1;
10989       s = "ddivu";
10990       s2 = "mfhi";
10991     do_divi:
10992       if (imm_expr.X_add_number == 0)
10993 	{
10994 	  as_warn (_("divide by zero"));
10995 	  if (mips_trap)
10996 	    macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
10997 	  else
10998 	    macro_build (NULL, "break", BRK_FMT, 7);
10999 	  break;
11000 	}
11001       if (imm_expr.X_add_number == 1)
11002 	{
11003 	  if (strcmp (s2, "mflo") == 0)
11004 	    move_register (op[0], op[1]);
11005 	  else
11006 	    move_register (op[0], ZERO);
11007 	  break;
11008 	}
11009       if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
11010 	{
11011 	  if (strcmp (s2, "mflo") == 0)
11012 	    macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
11013 	  else
11014 	    move_register (op[0], ZERO);
11015 	  break;
11016 	}
11017 
11018       used_at = 1;
11019       load_register (AT, &imm_expr, dbl);
11020       macro_build (NULL, s, "z,s,t", op[1], AT);
11021       macro_build (NULL, s2, MFHL_FMT, op[0]);
11022       break;
11023 
11024     case M_DIVU_3:
11025       s = "divu";
11026       s2 = "mflo";
11027       goto do_divu3;
11028     case M_REMU_3:
11029       s = "divu";
11030       s2 = "mfhi";
11031       goto do_divu3;
11032     case M_DDIVU_3:
11033       s = "ddivu";
11034       s2 = "mflo";
11035       goto do_divu3;
11036     case M_DREMU_3:
11037       s = "ddivu";
11038       s2 = "mfhi";
11039     do_divu3:
11040       start_noreorder ();
11041       if (mips_trap)
11042 	{
11043 	  macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
11044 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
11045 	  /* We want to close the noreorder block as soon as possible, so
11046 	     that later insns are available for delay slot filling.  */
11047 	  end_noreorder ();
11048 	}
11049       else
11050 	{
11051 	  if (mips_opts.micromips)
11052 	    micromips_label_expr (&label_expr);
11053 	  else
11054 	    label_expr.X_add_number = 8;
11055 	  macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
11056 	  macro_build (NULL, s, "z,s,t", op[1], op[2]);
11057 
11058 	  /* We want to close the noreorder block as soon as possible, so
11059 	     that later insns are available for delay slot filling.  */
11060 	  end_noreorder ();
11061 	  macro_build (NULL, "break", BRK_FMT, 7);
11062 	  if (mips_opts.micromips)
11063 	    micromips_add_label ();
11064 	}
11065       macro_build (NULL, s2, MFHL_FMT, op[0]);
11066       break;
11067 
11068     case M_DLCA_AB:
11069       dbl = 1;
11070       /* Fall through.  */
11071     case M_LCA_AB:
11072       call = 1;
11073       goto do_la;
11074     case M_DLA_AB:
11075       dbl = 1;
11076       /* Fall through.  */
11077     case M_LA_AB:
11078     do_la:
11079       /* Load the address of a symbol into a register.  If breg is not
11080 	 zero, we then add a base register to it.  */
11081 
11082       breg = op[2];
11083       if (dbl && GPR_SIZE == 32)
11084 	as_warn (_("dla used to load 32-bit register; recommend using la "
11085 		   "instead"));
11086 
11087       if (!dbl && HAVE_64BIT_OBJECTS)
11088 	as_warn (_("la used to load 64-bit address; recommend using dla "
11089 		   "instead"));
11090 
11091       if (small_offset_p (0, align, 16))
11092 	{
11093 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
11094 		       -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
11095 	  break;
11096 	}
11097 
11098       if (mips_opts.at && (op[0] == breg))
11099 	{
11100 	  tempreg = AT;
11101 	  used_at = 1;
11102 	}
11103       else
11104 	tempreg = op[0];
11105 
11106       if (offset_expr.X_op != O_symbol
11107 	  && offset_expr.X_op != O_constant)
11108 	{
11109 	  as_bad (_("expression too complex"));
11110 	  offset_expr.X_op = O_constant;
11111 	}
11112 
11113       if (offset_expr.X_op == O_constant)
11114 	load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
11115       else if (mips_pic == NO_PIC)
11116 	{
11117 	  /* If this is a reference to a GP relative symbol, we want
11118 	       addiu	$tempreg,$gp,<sym>	(BFD_RELOC_GPREL16)
11119 	     Otherwise we want
11120 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11121 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11122 	     If we have a constant, we need two instructions anyhow,
11123 	     so we may as well always use the latter form.
11124 
11125 	     With 64bit address space and a usable $at we want
11126 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11127 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
11128 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11129 	       daddiu	$at,<sym>		(BFD_RELOC_LO16)
11130 	       dsll32	$tempreg,0
11131 	       daddu	$tempreg,$tempreg,$at
11132 
11133 	     If $at is already in use, we use a path which is suboptimal
11134 	     on superscalar processors.
11135 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
11136 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
11137 	       dsll	$tempreg,16
11138 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
11139 	       dsll	$tempreg,16
11140 	       daddiu	$tempreg,<sym>		(BFD_RELOC_LO16)
11141 
11142 	     For GP relative symbols in 64bit address space we can use
11143 	     the same sequence as in 32bit address space.  */
11144 	  if (HAVE_64BIT_SYMBOLS)
11145 	    {
11146 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11147 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11148 		{
11149 		  relax_start (offset_expr.X_add_symbol);
11150 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11151 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11152 		  relax_switch ();
11153 		}
11154 
11155 	      if (used_at == 0 && mips_opts.at)
11156 		{
11157 		  macro_build (&offset_expr, "lui", LUI_FMT,
11158 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
11159 		  macro_build (&offset_expr, "lui", LUI_FMT,
11160 			       AT, BFD_RELOC_HI16_S);
11161 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11162 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11163 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11164 			       AT, AT, BFD_RELOC_LO16);
11165 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
11166 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
11167 		  used_at = 1;
11168 		}
11169 	      else
11170 		{
11171 		  macro_build (&offset_expr, "lui", LUI_FMT,
11172 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
11173 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11174 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
11175 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11176 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11177 			       tempreg, tempreg, BFD_RELOC_HI16_S);
11178 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
11179 		  macro_build (&offset_expr, "daddiu", "t,r,j",
11180 			       tempreg, tempreg, BFD_RELOC_LO16);
11181 		}
11182 
11183 	      if (mips_relax.sequence)
11184 		relax_end ();
11185 	    }
11186 	  else
11187 	    {
11188 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11189 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11190 		{
11191 		  relax_start (offset_expr.X_add_symbol);
11192 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11193 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11194 		  relax_switch ();
11195 		}
11196 	      if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
11197 		as_bad (_("offset too large"));
11198 	      macro_build_lui (&offset_expr, tempreg);
11199 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11200 			   tempreg, tempreg, BFD_RELOC_LO16);
11201 	      if (mips_relax.sequence)
11202 		relax_end ();
11203 	    }
11204 	}
11205       else if (!mips_big_got && !HAVE_NEWABI)
11206 	{
11207 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11208 
11209 	  /* If this is a reference to an external symbol, and there
11210 	     is no constant, we want
11211 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11212 	     or for lca or if tempreg is PIC_CALL_REG
11213 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
11214 	     For a local symbol, we want
11215 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11216 	       nop
11217 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11218 
11219 	     If we have a small constant, and this is a reference to
11220 	     an external symbol, we want
11221 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11222 	       nop
11223 	       addiu	$tempreg,$tempreg,<constant>
11224 	     For a local symbol, we want the same instruction
11225 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
11226 	     addiu instruction.
11227 
11228 	     If we have a large constant, and this is a reference to
11229 	     an external symbol, we want
11230 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11231 	       lui	$at,<hiconstant>
11232 	       addiu	$at,$at,<loconstant>
11233 	       addu	$tempreg,$tempreg,$at
11234 	     For a local symbol, we want the same instruction
11235 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
11236 	     addiu instruction.
11237 	   */
11238 
11239 	  if (offset_expr.X_add_number == 0)
11240 	    {
11241 	      if (mips_pic == SVR4_PIC
11242 		  && breg == 0
11243 		  && (call || tempreg == PIC_CALL_REG))
11244 		lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11245 
11246 	      relax_start (offset_expr.X_add_symbol);
11247 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11248 			   lw_reloc_type, mips_gp_register);
11249 	      if (breg != 0)
11250 		{
11251 		  /* We're going to put in an addu instruction using
11252 		     tempreg, so we may as well insert the nop right
11253 		     now.  */
11254 		  load_delay_nop ();
11255 		}
11256 	      relax_switch ();
11257 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11258 			   tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
11259 	      load_delay_nop ();
11260 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11261 			   tempreg, tempreg, BFD_RELOC_LO16);
11262 	      relax_end ();
11263 	      /* FIXME: If breg == 0, and the next instruction uses
11264 		 $tempreg, then if this variant case is used an extra
11265 		 nop will be generated.  */
11266 	    }
11267 	  else if (offset_expr.X_add_number >= -0x8000
11268 		   && offset_expr.X_add_number < 0x8000)
11269 	    {
11270 	      load_got_offset (tempreg, &offset_expr);
11271 	      load_delay_nop ();
11272 	      add_got_offset (tempreg, &offset_expr);
11273 	    }
11274 	  else
11275 	    {
11276 	      expr1.X_add_number = offset_expr.X_add_number;
11277 	      offset_expr.X_add_number =
11278 		SEXT_16BIT (offset_expr.X_add_number);
11279 	      load_got_offset (tempreg, &offset_expr);
11280 	      offset_expr.X_add_number = expr1.X_add_number;
11281 	      /* If we are going to add in a base register, and the
11282 		 target register and the base register are the same,
11283 		 then we are using AT as a temporary register.  Since
11284 		 we want to load the constant into AT, we add our
11285 		 current AT (from the global offset table) and the
11286 		 register into the register now, and pretend we were
11287 		 not using a base register.  */
11288 	      if (breg == op[0])
11289 		{
11290 		  load_delay_nop ();
11291 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11292 			       op[0], AT, breg);
11293 		  breg = 0;
11294 		  tempreg = op[0];
11295 		}
11296 	      add_got_offset_hilo (tempreg, &offset_expr, AT);
11297 	      used_at = 1;
11298 	    }
11299 	}
11300       else if (!mips_big_got && HAVE_NEWABI)
11301 	{
11302 	  int add_breg_early = 0;
11303 
11304 	  /* If this is a reference to an external, and there is no
11305 	     constant, or local symbol (*), with or without a
11306 	     constant, we want
11307 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11308 	     or for lca or if tempreg is PIC_CALL_REG
11309 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
11310 
11311 	     If we have a small constant, and this is a reference to
11312 	     an external symbol, we want
11313 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11314 	       addiu	$tempreg,$tempreg,<constant>
11315 
11316 	     If we have a large constant, and this is a reference to
11317 	     an external symbol, we want
11318 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
11319 	       lui	$at,<hiconstant>
11320 	       addiu	$at,$at,<loconstant>
11321 	       addu	$tempreg,$tempreg,$at
11322 
11323 	     (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11324 	     local symbols, even though it introduces an additional
11325 	     instruction.  */
11326 
11327 	  if (offset_expr.X_add_number)
11328 	    {
11329 	      expr1.X_add_number = offset_expr.X_add_number;
11330 	      offset_expr.X_add_number = 0;
11331 
11332 	      relax_start (offset_expr.X_add_symbol);
11333 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11334 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11335 
11336 	      if (expr1.X_add_number >= -0x8000
11337 		  && expr1.X_add_number < 0x8000)
11338 		{
11339 		  macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11340 			       tempreg, tempreg, BFD_RELOC_LO16);
11341 		}
11342 	      else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11343 		{
11344 		  unsigned int dreg;
11345 
11346 		  /* If we are going to add in a base register, and the
11347 		     target register and the base register are the same,
11348 		     then we are using AT as a temporary register.  Since
11349 		     we want to load the constant into AT, we add our
11350 		     current AT (from the global offset table) and the
11351 		     register into the register now, and pretend we were
11352 		     not using a base register.  */
11353 		  if (breg != op[0])
11354 		    dreg = tempreg;
11355 		  else
11356 		    {
11357 		      gas_assert (tempreg == AT);
11358 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11359 				   op[0], AT, breg);
11360 		      dreg = op[0];
11361 		      add_breg_early = 1;
11362 		    }
11363 
11364 		  load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11365 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11366 			       dreg, dreg, AT);
11367 
11368 		  used_at = 1;
11369 		}
11370 	      else
11371 		as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11372 
11373 	      relax_switch ();
11374 	      offset_expr.X_add_number = expr1.X_add_number;
11375 
11376 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11377 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11378 	      if (add_breg_early)
11379 		{
11380 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11381 			       op[0], tempreg, breg);
11382 		  breg = 0;
11383 		  tempreg = op[0];
11384 		}
11385 	      relax_end ();
11386 	    }
11387 	  else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
11388 	    {
11389 	      relax_start (offset_expr.X_add_symbol);
11390 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11391 			   BFD_RELOC_MIPS_CALL16, mips_gp_register);
11392 	      relax_switch ();
11393 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11394 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11395 	      relax_end ();
11396 	    }
11397 	  else
11398 	    {
11399 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11400 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11401 	    }
11402 	}
11403       else if (mips_big_got && !HAVE_NEWABI)
11404 	{
11405 	  int gpdelay;
11406 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11407 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11408 	  int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11409 
11410 	  /* This is the large GOT case.  If this is a reference to an
11411 	     external symbol, and there is no constant, we want
11412 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11413 	       addu	$tempreg,$tempreg,$gp
11414 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11415 	     or for lca or if tempreg is PIC_CALL_REG
11416 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11417 	       addu	$tempreg,$tempreg,$gp
11418 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11419 	     For a local symbol, we want
11420 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11421 	       nop
11422 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
11423 
11424 	     If we have a small constant, and this is a reference to
11425 	     an external symbol, we want
11426 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11427 	       addu	$tempreg,$tempreg,$gp
11428 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11429 	       nop
11430 	       addiu	$tempreg,$tempreg,<constant>
11431 	     For a local symbol, we want
11432 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11433 	       nop
11434 	       addiu	$tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11435 
11436 	     If we have a large constant, and this is a reference to
11437 	     an external symbol, we want
11438 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11439 	       addu	$tempreg,$tempreg,$gp
11440 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11441 	       lui	$at,<hiconstant>
11442 	       addiu	$at,$at,<loconstant>
11443 	       addu	$tempreg,$tempreg,$at
11444 	     For a local symbol, we want
11445 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
11446 	       lui	$at,<hiconstant>
11447 	       addiu	$at,$at,<loconstant>	(BFD_RELOC_LO16)
11448 	       addu	$tempreg,$tempreg,$at
11449 	  */
11450 
11451 	  expr1.X_add_number = offset_expr.X_add_number;
11452 	  offset_expr.X_add_number = 0;
11453 	  relax_start (offset_expr.X_add_symbol);
11454 	  gpdelay = reg_needs_delay (mips_gp_register);
11455 	  if (expr1.X_add_number == 0 && breg == 0
11456 	      && (call || tempreg == PIC_CALL_REG))
11457 	    {
11458 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11459 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11460 	    }
11461 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11462 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11463 		       tempreg, tempreg, mips_gp_register);
11464 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11465 		       tempreg, lw_reloc_type, tempreg);
11466 	  if (expr1.X_add_number == 0)
11467 	    {
11468 	      if (breg != 0)
11469 		{
11470 		  /* We're going to put in an addu instruction using
11471 		     tempreg, so we may as well insert the nop right
11472 		     now.  */
11473 		  load_delay_nop ();
11474 		}
11475 	    }
11476 	  else if (expr1.X_add_number >= -0x8000
11477 		   && expr1.X_add_number < 0x8000)
11478 	    {
11479 	      load_delay_nop ();
11480 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11481 			   tempreg, tempreg, BFD_RELOC_LO16);
11482 	    }
11483 	  else
11484 	    {
11485 	      unsigned int dreg;
11486 
11487 	      /* If we are going to add in a base register, and the
11488 		 target register and the base register are the same,
11489 		 then we are using AT as a temporary register.  Since
11490 		 we want to load the constant into AT, we add our
11491 		 current AT (from the global offset table) and the
11492 		 register into the register now, and pretend we were
11493 		 not using a base register.  */
11494 	      if (breg != op[0])
11495 		dreg = tempreg;
11496 	      else
11497 		{
11498 		  gas_assert (tempreg == AT);
11499 		  load_delay_nop ();
11500 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11501 			       op[0], AT, breg);
11502 		  dreg = op[0];
11503 		}
11504 
11505 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11506 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11507 
11508 	      used_at = 1;
11509 	    }
11510 	  offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
11511 	  relax_switch ();
11512 
11513 	  if (gpdelay)
11514 	    {
11515 	      /* This is needed because this instruction uses $gp, but
11516 		 the first instruction on the main stream does not.  */
11517 	      macro_build (NULL, "nop", "");
11518 	    }
11519 
11520 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11521 		       local_reloc_type, mips_gp_register);
11522 	  if (expr1.X_add_number >= -0x8000
11523 	      && expr1.X_add_number < 0x8000)
11524 	    {
11525 	      load_delay_nop ();
11526 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11527 			   tempreg, tempreg, BFD_RELOC_LO16);
11528 	      /* FIXME: If add_number is 0, and there was no base
11529 		 register, the external symbol case ended with a load,
11530 		 so if the symbol turns out to not be external, and
11531 		 the next instruction uses tempreg, an unnecessary nop
11532 		 will be inserted.  */
11533 	    }
11534 	  else
11535 	    {
11536 	      if (breg == op[0])
11537 		{
11538 		  /* We must add in the base register now, as in the
11539 		     external symbol case.  */
11540 		  gas_assert (tempreg == AT);
11541 		  load_delay_nop ();
11542 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11543 			       op[0], AT, breg);
11544 		  tempreg = op[0];
11545 		  /* We set breg to 0 because we have arranged to add
11546 		     it in in both cases.  */
11547 		  breg = 0;
11548 		}
11549 
11550 	      macro_build_lui (&expr1, AT);
11551 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11552 			   AT, AT, BFD_RELOC_LO16);
11553 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11554 			   tempreg, tempreg, AT);
11555 	      used_at = 1;
11556 	    }
11557 	  relax_end ();
11558 	}
11559       else if (mips_big_got && HAVE_NEWABI)
11560 	{
11561 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11562 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
11563 	  int add_breg_early = 0;
11564 
11565 	  /* This is the large GOT case.  If this is a reference to an
11566 	     external symbol, and there is no constant, we want
11567 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11568 	       add	$tempreg,$tempreg,$gp
11569 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11570 	     or for lca or if tempreg is PIC_CALL_REG
11571 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11572 	       add	$tempreg,$tempreg,$gp
11573 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11574 
11575 	     If we have a small constant, and this is a reference to
11576 	     an external symbol, we want
11577 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11578 	       add	$tempreg,$tempreg,$gp
11579 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11580 	       addi	$tempreg,$tempreg,<constant>
11581 
11582 	     If we have a large constant, and this is a reference to
11583 	     an external symbol, we want
11584 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
11585 	       addu	$tempreg,$tempreg,$gp
11586 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11587 	       lui	$at,<hiconstant>
11588 	       addi	$at,$at,<loconstant>
11589 	       add	$tempreg,$tempreg,$at
11590 
11591 	     If we have NewABI, and we know it's a local symbol, we want
11592 	       lw	$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
11593 	       addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
11594 	     otherwise we have to resort to GOT_HI16/GOT_LO16.  */
11595 
11596 	  relax_start (offset_expr.X_add_symbol);
11597 
11598 	  expr1.X_add_number = offset_expr.X_add_number;
11599 	  offset_expr.X_add_number = 0;
11600 
11601 	  if (expr1.X_add_number == 0 && breg == 0
11602 	      && (call || tempreg == PIC_CALL_REG))
11603 	    {
11604 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11605 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11606 	    }
11607 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
11608 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11609 		       tempreg, tempreg, mips_gp_register);
11610 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11611 		       tempreg, lw_reloc_type, tempreg);
11612 
11613 	  if (expr1.X_add_number == 0)
11614 	    ;
11615 	  else if (expr1.X_add_number >= -0x8000
11616 		   && expr1.X_add_number < 0x8000)
11617 	    {
11618 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11619 			   tempreg, tempreg, BFD_RELOC_LO16);
11620 	    }
11621 	  else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
11622 	    {
11623 	      unsigned int dreg;
11624 
11625 	      /* If we are going to add in a base register, and the
11626 		 target register and the base register are the same,
11627 		 then we are using AT as a temporary register.  Since
11628 		 we want to load the constant into AT, we add our
11629 		 current AT (from the global offset table) and the
11630 		 register into the register now, and pretend we were
11631 		 not using a base register.  */
11632 	      if (breg != op[0])
11633 		dreg = tempreg;
11634 	      else
11635 		{
11636 		  gas_assert (tempreg == AT);
11637 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11638 			       op[0], AT, breg);
11639 		  dreg = op[0];
11640 		  add_breg_early = 1;
11641 		}
11642 
11643 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
11644 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
11645 
11646 	      used_at = 1;
11647 	    }
11648 	  else
11649 	    as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11650 
11651 	  relax_switch ();
11652 	  offset_expr.X_add_number = expr1.X_add_number;
11653 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11654 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11655 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11656 		       tempreg, BFD_RELOC_MIPS_GOT_OFST);
11657 	  if (add_breg_early)
11658 	    {
11659 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11660 			   op[0], tempreg, breg);
11661 	      breg = 0;
11662 	      tempreg = op[0];
11663 	    }
11664 	  relax_end ();
11665 	}
11666       else
11667 	abort ();
11668 
11669       if (breg != 0)
11670 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
11671       break;
11672 
11673     case M_JR_S:
11674       macro_build_jrpatch (&expr1, op[2]);
11675       macro_build (NULL, "jr", "s", op[2]);
11676       return;	/* didn't modify $at */
11677 
11678     case M_J_S:
11679       macro_build_jrpatch (&expr1, op[2]);
11680       macro_build (NULL, "j", "s", op[2]);
11681       return;	/* didn't modify $at */
11682 
11683     case M_JALR_S:
11684       macro_build_jrpatch (&expr1, op[2]);
11685       macro_build (NULL, "jalr", "s", op[2]);
11686       return;	/* didn't modify $at */
11687 
11688     case M_JALR_DS:
11689       macro_build_jrpatch (&expr1, op[2]);
11690       macro_build (NULL, "jalr", "d,s", op[0], op[2]);
11691       return;	/* didn't modify $at */
11692 
11693     case M_MSGSND:
11694       gas_assert (!mips_opts.micromips);
11695       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
11696       break;
11697 
11698     case M_MSGLD:
11699       gas_assert (!mips_opts.micromips);
11700       macro_build (NULL, "c2", "C", 0x02);
11701       break;
11702 
11703     case M_MSGLD_T:
11704       gas_assert (!mips_opts.micromips);
11705       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
11706       break;
11707 
11708     case M_MSGWAIT:
11709       gas_assert (!mips_opts.micromips);
11710       macro_build (NULL, "c2", "C", 3);
11711       break;
11712 
11713     case M_MSGWAIT_T:
11714       gas_assert (!mips_opts.micromips);
11715       macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
11716       break;
11717 
11718     case M_J_A:
11719       /* The j instruction may not be used in PIC code, since it
11720 	 requires an absolute address.  We convert it to a b
11721 	 instruction.  */
11722       if (mips_pic == NO_PIC)
11723 	macro_build (&offset_expr, "j", "a");
11724       else
11725 	macro_build (&offset_expr, "b", "p");
11726       break;
11727 
11728       /* The jal instructions must be handled as macros because when
11729 	 generating PIC code they expand to multi-instruction
11730 	 sequences.  Normally they are simple instructions.  */
11731     case M_JALS_1:
11732       op[1] = op[0];
11733       op[0] = RA;
11734       /* Fall through.  */
11735     case M_JALS_2:
11736       gas_assert (mips_opts.micromips);
11737       if (mips_opts.insn32)
11738 	{
11739 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11740 	  break;
11741 	}
11742       jals = 1;
11743       goto jal;
11744     case M_JAL_1:
11745       op[1] = op[0];
11746       op[0] = RA;
11747       /* Fall through.  */
11748     case M_JAL_2:
11749     jal:
11750       if (mips_pic == NO_PIC)
11751 	{
11752 	  s = jals ? "jalrs" : "jalr";
11753 	  if (mips_opts.micromips
11754 	      && !mips_opts.insn32
11755 	      && op[0] == RA
11756 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11757 	    macro_build (NULL, s, "mj", op[1]);
11758 	  else
11759 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11760 	}
11761       else
11762 	{
11763 	  int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11764 			   && mips_cprestore_offset >= 0);
11765 
11766 	  if (op[1] != PIC_CALL_REG)
11767 	    as_warn (_("MIPS PIC call to register other than $25"));
11768 
11769 	  s = ((mips_opts.micromips
11770 		&& !mips_opts.insn32
11771 		&& (!mips_opts.noreorder || cprestore))
11772 	       ? "jalrs" : "jalr");
11773 	  if (mips_opts.micromips
11774 	      && !mips_opts.insn32
11775 	      && op[0] == RA
11776 	      && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
11777 	    macro_build (NULL, s, "mj", op[1]);
11778 	  else
11779 	    macro_build (NULL, s, JALR_FMT, op[0], op[1]);
11780 	  if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
11781 	    {
11782 	      if (mips_cprestore_offset < 0)
11783 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11784 	      else
11785 		{
11786 		  if (!mips_frame_reg_valid)
11787 		    {
11788 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11789 		      /* Quiet this warning.  */
11790 		      mips_frame_reg_valid = 1;
11791 		    }
11792 		  if (!mips_cprestore_valid)
11793 		    {
11794 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11795 		      /* Quiet this warning.  */
11796 		      mips_cprestore_valid = 1;
11797 		    }
11798 		  if (mips_opts.noreorder)
11799 		    macro_build (NULL, "nop", "");
11800 		  expr1.X_add_number = mips_cprestore_offset;
11801 		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11802 						mips_gp_register,
11803 						mips_frame_reg,
11804 						HAVE_64BIT_ADDRESSES);
11805 		}
11806 	    }
11807 	}
11808 
11809       break;
11810 
11811     case M_JALS_A:
11812       gas_assert (mips_opts.micromips);
11813       if (mips_opts.insn32)
11814 	{
11815 	  as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
11816 	  break;
11817 	}
11818       jals = 1;
11819       /* Fall through.  */
11820     case M_JAL_A:
11821       if (mips_pic == NO_PIC)
11822 	macro_build (&offset_expr, jals ? "jals" : "jal", "a");
11823       else if (mips_pic == SVR4_PIC)
11824 	{
11825 	  /* If this is a reference to an external symbol, and we are
11826 	     using a small GOT, we want
11827 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_CALL16)
11828 	       nop
11829 	       jalr	$ra,$25
11830 	       nop
11831 	       lw	$gp,cprestore($sp)
11832 	     The cprestore value is set using the .cprestore
11833 	     pseudo-op.  If we are using a big GOT, we want
11834 	       lui	$25,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
11835 	       addu	$25,$25,$gp
11836 	       lw	$25,<sym>($25)		(BFD_RELOC_MIPS_CALL_LO16)
11837 	       nop
11838 	       jalr	$ra,$25
11839 	       nop
11840 	       lw	$gp,cprestore($sp)
11841 	     If the symbol is not external, we want
11842 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
11843 	       nop
11844 	       addiu	$25,$25,<sym>		(BFD_RELOC_LO16)
11845 	       jalr	$ra,$25
11846 	       nop
11847 	       lw $gp,cprestore($sp)
11848 
11849 	     For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11850 	     sequences above, minus nops, unless the symbol is local,
11851 	     which enables us to use GOT_PAGE/GOT_OFST (big got) or
11852 	     GOT_DISP.  */
11853 	  if (HAVE_NEWABI)
11854 	    {
11855 	      if (!mips_big_got)
11856 		{
11857 		  relax_start (offset_expr.X_add_symbol);
11858 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11859 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11860 			       mips_gp_register);
11861 		  relax_switch ();
11862 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11863 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
11864 			       mips_gp_register);
11865 		  relax_end ();
11866 		}
11867 	      else
11868 		{
11869 		  relax_start (offset_expr.X_add_symbol);
11870 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11871 			       BFD_RELOC_MIPS_CALL_HI16);
11872 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11873 			       PIC_CALL_REG, mips_gp_register);
11874 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11875 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11876 			       PIC_CALL_REG);
11877 		  relax_switch ();
11878 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11879 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11880 			       mips_gp_register);
11881 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11882 			       PIC_CALL_REG, PIC_CALL_REG,
11883 			       BFD_RELOC_MIPS_GOT_OFST);
11884 		  relax_end ();
11885 		}
11886 
11887 	      macro_build_jalr (&offset_expr, 0);
11888 	    }
11889 	  else
11890 	    {
11891 	      relax_start (offset_expr.X_add_symbol);
11892 	      if (!mips_big_got)
11893 		{
11894 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11895 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
11896 			       mips_gp_register);
11897 		  load_delay_nop ();
11898 		  relax_switch ();
11899 		}
11900 	      else
11901 		{
11902 		  int gpdelay;
11903 
11904 		  gpdelay = reg_needs_delay (mips_gp_register);
11905 		  macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
11906 			       BFD_RELOC_MIPS_CALL_HI16);
11907 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11908 			       PIC_CALL_REG, mips_gp_register);
11909 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11910 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11911 			       PIC_CALL_REG);
11912 		  load_delay_nop ();
11913 		  relax_switch ();
11914 		  if (gpdelay)
11915 		    macro_build (NULL, "nop", "");
11916 		}
11917 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11918 			   PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
11919 			   mips_gp_register);
11920 	      load_delay_nop ();
11921 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11922 			   PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
11923 	      relax_end ();
11924 	      macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
11925 
11926 	      if (mips_cprestore_offset < 0)
11927 		as_warn (_("no .cprestore pseudo-op used in PIC code"));
11928 	      else
11929 		{
11930 		  if (!mips_frame_reg_valid)
11931 		    {
11932 		      as_warn (_("no .frame pseudo-op used in PIC code"));
11933 		      /* Quiet this warning.  */
11934 		      mips_frame_reg_valid = 1;
11935 		    }
11936 		  if (!mips_cprestore_valid)
11937 		    {
11938 		      as_warn (_("no .cprestore pseudo-op used in PIC code"));
11939 		      /* Quiet this warning.  */
11940 		      mips_cprestore_valid = 1;
11941 		    }
11942 		  if (mips_opts.noreorder)
11943 		    macro_build (NULL, "nop", "");
11944 		  expr1.X_add_number = mips_cprestore_offset;
11945 		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
11946 						mips_gp_register,
11947 						mips_frame_reg,
11948 						HAVE_64BIT_ADDRESSES);
11949 		}
11950 	    }
11951 	}
11952       else if (mips_pic == VXWORKS_PIC)
11953 	as_bad (_("non-PIC jump used in PIC library"));
11954       else
11955 	abort ();
11956 
11957       break;
11958 
11959     case M_LBUE_AB:
11960       s = "lbue";
11961       fmt = "t,+j(b)";
11962       offbits = 9;
11963       goto ld_st;
11964     case M_LHUE_AB:
11965       s = "lhue";
11966       fmt = "t,+j(b)";
11967       offbits = 9;
11968       goto ld_st;
11969     case M_LBE_AB:
11970       s = "lbe";
11971       fmt = "t,+j(b)";
11972       offbits = 9;
11973       goto ld_st;
11974     case M_LHE_AB:
11975       s = "lhe";
11976       fmt = "t,+j(b)";
11977       offbits = 9;
11978       goto ld_st;
11979     case M_LLE_AB:
11980       s = "lle";
11981       fmt = "t,+j(b)";
11982       offbits = 9;
11983       goto ld_st;
11984     case M_LWE_AB:
11985       s = "lwe";
11986       fmt = "t,+j(b)";
11987       offbits = 9;
11988       goto ld_st;
11989     case M_LWLE_AB:
11990       s = "lwle";
11991       fmt = "t,+j(b)";
11992       offbits = 9;
11993       goto ld_st;
11994     case M_LWRE_AB:
11995       s = "lwre";
11996       fmt = "t,+j(b)";
11997       offbits = 9;
11998       goto ld_st;
11999     case M_SBE_AB:
12000       s = "sbe";
12001       fmt = "t,+j(b)";
12002       offbits = 9;
12003       goto ld_st;
12004     case M_SCE_AB:
12005       s = "sce";
12006       fmt = "t,+j(b)";
12007       offbits = 9;
12008       goto ld_st;
12009     case M_SHE_AB:
12010       s = "she";
12011       fmt = "t,+j(b)";
12012       offbits = 9;
12013       goto ld_st;
12014     case M_SWE_AB:
12015       s = "swe";
12016       fmt = "t,+j(b)";
12017       offbits = 9;
12018       goto ld_st;
12019     case M_SWLE_AB:
12020       s = "swle";
12021       fmt = "t,+j(b)";
12022       offbits = 9;
12023       goto ld_st;
12024     case M_SWRE_AB:
12025       s = "swre";
12026       fmt = "t,+j(b)";
12027       offbits = 9;
12028       goto ld_st;
12029     case M_ACLR_AB:
12030       s = "aclr";
12031       fmt = "\\,~(b)";
12032       offbits = 12;
12033       goto ld_st;
12034     case M_ASET_AB:
12035       s = "aset";
12036       fmt = "\\,~(b)";
12037       offbits = 12;
12038       goto ld_st;
12039     case M_LB_AB:
12040       s = "lb";
12041       fmt = "t,o(b)";
12042       goto ld;
12043     case M_LBU_AB:
12044       s = "lbu";
12045       fmt = "t,o(b)";
12046       goto ld;
12047     case M_LH_AB:
12048       s = "lh";
12049       fmt = "t,o(b)";
12050       goto ld;
12051     case M_LHU_AB:
12052       s = "lhu";
12053       fmt = "t,o(b)";
12054       goto ld;
12055     case M_LW_AB:
12056       s = "lw";
12057       fmt = "t,o(b)";
12058       goto ld;
12059     case M_LWC0_AB:
12060       gas_assert (!mips_opts.micromips);
12061       s = "lwc0";
12062       fmt = "E,o(b)";
12063       /* Itbl support may require additional care here.  */
12064       coproc = 1;
12065       goto ld_st;
12066     case M_LWC1_AB:
12067       s = "lwc1";
12068       fmt = "T,o(b)";
12069       /* Itbl support may require additional care here.  */
12070       coproc = 1;
12071       goto ld_st;
12072     case M_LWC2_AB:
12073       s = "lwc2";
12074       fmt = COP12_FMT;
12075       offbits = (mips_opts.micromips ? 12
12076 		 : ISA_IS_R6 (mips_opts.isa) ? 11
12077 		 : 16);
12078       /* Itbl support may require additional care here.  */
12079       coproc = 1;
12080       goto ld_st;
12081     case M_LWC3_AB:
12082       gas_assert (!mips_opts.micromips);
12083       s = "lwc3";
12084       fmt = "E,o(b)";
12085       /* Itbl support may require additional care here.  */
12086       coproc = 1;
12087       goto ld_st;
12088     case M_LWL_AB:
12089       s = "lwl";
12090       fmt = MEM12_FMT;
12091       offbits = (mips_opts.micromips ? 12 : 16);
12092       goto ld_st;
12093     case M_LWR_AB:
12094       s = "lwr";
12095       fmt = MEM12_FMT;
12096       offbits = (mips_opts.micromips ? 12 : 16);
12097       goto ld_st;
12098     case M_LDC1_AB:
12099       s = "ldc1";
12100       fmt = "T,o(b)";
12101       /* Itbl support may require additional care here.  */
12102       coproc = 1;
12103       goto ld_st;
12104     case M_LDC2_AB:
12105       s = "ldc2";
12106       fmt = COP12_FMT;
12107       offbits = (mips_opts.micromips ? 12
12108 		 : ISA_IS_R6 (mips_opts.isa) ? 11
12109 		 : 16);
12110       /* Itbl support may require additional care here.  */
12111       coproc = 1;
12112       goto ld_st;
12113     case M_LQC2_AB:
12114       s = "lqc2";
12115       fmt = "+7,o(b)";
12116       /* Itbl support may require additional care here.  */
12117       coproc = 1;
12118       goto ld_st;
12119     case M_LDC3_AB:
12120       s = "ldc3";
12121       fmt = "E,o(b)";
12122       /* Itbl support may require additional care here.  */
12123       coproc = 1;
12124       goto ld_st;
12125     case M_LDL_AB:
12126       s = "ldl";
12127       fmt = MEM12_FMT;
12128       offbits = (mips_opts.micromips ? 12 : 16);
12129       goto ld_st;
12130     case M_LDR_AB:
12131       s = "ldr";
12132       fmt = MEM12_FMT;
12133       offbits = (mips_opts.micromips ? 12 : 16);
12134       goto ld_st;
12135     case M_LL_AB:
12136       s = "ll";
12137       fmt = LL_SC_FMT;
12138       offbits = (mips_opts.micromips ? 12
12139 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12140 		 : 16);
12141       goto ld;
12142     case M_LLD_AB:
12143       s = "lld";
12144       fmt = LL_SC_FMT;
12145       offbits = (mips_opts.micromips ? 12
12146 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12147 		 : 16);
12148       goto ld;
12149     case M_LWU_AB:
12150       s = "lwu";
12151       fmt = MEM12_FMT;
12152       offbits = (mips_opts.micromips ? 12 : 16);
12153       goto ld;
12154     case M_LWP_AB:
12155       gas_assert (mips_opts.micromips);
12156       s = "lwp";
12157       fmt = "t,~(b)";
12158       offbits = 12;
12159       lp = 1;
12160       goto ld;
12161     case M_LDP_AB:
12162       gas_assert (mips_opts.micromips);
12163       s = "ldp";
12164       fmt = "t,~(b)";
12165       offbits = 12;
12166       lp = 1;
12167       goto ld;
12168     case M_LLDP_AB:
12169     case M_LLWP_AB:
12170     case M_LLWPE_AB:
12171       s = ip->insn_mo->name;
12172       fmt = "t,d,s";
12173       ll_sc_paired = 1;
12174       offbits = 0;
12175       goto ld;
12176     case M_LWM_AB:
12177       gas_assert (mips_opts.micromips);
12178       s = "lwm";
12179       fmt = "n,~(b)";
12180       offbits = 12;
12181       goto ld_st;
12182     case M_LDM_AB:
12183       gas_assert (mips_opts.micromips);
12184       s = "ldm";
12185       fmt = "n,~(b)";
12186       offbits = 12;
12187       goto ld_st;
12188 
12189     ld:
12190       /* Try to use one the the load registers to compute the base address.
12191 	 We don't want to use $0 as tempreg.  */
12192       if (ll_sc_paired)
12193 	{
12194 	  if ((op[0] == ZERO && op[3] == op[1])
12195 	      || (op[1] == ZERO && op[3] == op[0])
12196 	      || (op[0] == ZERO && op[1] == ZERO))
12197 	    goto ld_st;
12198 	  else if (op[0] != op[3] && op[0] != ZERO)
12199 	    tempreg = op[0];
12200 	  else
12201 	    tempreg = op[1];
12202 	}
12203       else
12204         {
12205 	  if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12206 	    goto ld_st;
12207 	  else
12208 	    tempreg = op[0] + lp;
12209 	}
12210       goto ld_noat;
12211 
12212     case M_SB_AB:
12213       s = "sb";
12214       fmt = "t,o(b)";
12215       goto ld_st;
12216     case M_SH_AB:
12217       s = "sh";
12218       fmt = "t,o(b)";
12219       goto ld_st;
12220     case M_SW_AB:
12221       s = "sw";
12222       fmt = "t,o(b)";
12223       goto ld_st;
12224     case M_SWC0_AB:
12225       gas_assert (!mips_opts.micromips);
12226       s = "swc0";
12227       fmt = "E,o(b)";
12228       /* Itbl support may require additional care here.  */
12229       coproc = 1;
12230       goto ld_st;
12231     case M_SWC1_AB:
12232       s = "swc1";
12233       fmt = "T,o(b)";
12234       /* Itbl support may require additional care here.  */
12235       coproc = 1;
12236       goto ld_st;
12237     case M_SWC2_AB:
12238       s = "swc2";
12239       fmt = COP12_FMT;
12240       offbits = (mips_opts.micromips ? 12
12241 		 : ISA_IS_R6 (mips_opts.isa) ? 11
12242 		 : 16);
12243       /* Itbl support may require additional care here.  */
12244       coproc = 1;
12245       goto ld_st;
12246     case M_SWC3_AB:
12247       gas_assert (!mips_opts.micromips);
12248       s = "swc3";
12249       fmt = "E,o(b)";
12250       /* Itbl support may require additional care here.  */
12251       coproc = 1;
12252       goto ld_st;
12253     case M_SWL_AB:
12254       s = "swl";
12255       fmt = MEM12_FMT;
12256       offbits = (mips_opts.micromips ? 12 : 16);
12257       goto ld_st;
12258     case M_SWR_AB:
12259       s = "swr";
12260       fmt = MEM12_FMT;
12261       offbits = (mips_opts.micromips ? 12 : 16);
12262       goto ld_st;
12263     case M_SC_AB:
12264       s = "sc";
12265       fmt = LL_SC_FMT;
12266       offbits = (mips_opts.micromips ? 12
12267 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12268 		 : 16);
12269       goto ld_st;
12270     case M_SCD_AB:
12271       s = "scd";
12272       fmt = LL_SC_FMT;
12273       offbits = (mips_opts.micromips ? 12
12274 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12275 		 : 16);
12276       goto ld_st;
12277     case M_SCDP_AB:
12278     case M_SCWP_AB:
12279     case M_SCWPE_AB:
12280       s = ip->insn_mo->name;
12281       fmt = "t,d,s";
12282       ll_sc_paired = 1;
12283       offbits = 0;
12284       goto ld_st;
12285     case M_CACHE_AB:
12286       s = "cache";
12287       fmt = (mips_opts.micromips ? "k,~(b)"
12288 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12289 	     : "k,o(b)");
12290       offbits = (mips_opts.micromips ? 12
12291 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12292 		 : 16);
12293       goto ld_st;
12294     case M_CACHEE_AB:
12295       s = "cachee";
12296       fmt = "k,+j(b)";
12297       offbits = 9;
12298       goto ld_st;
12299     case M_PREF_AB:
12300       s = "pref";
12301       fmt = (mips_opts.micromips ? "k,~(b)"
12302 	     : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12303 	     : "k,o(b)");
12304       offbits = (mips_opts.micromips ? 12
12305 		 : ISA_IS_R6 (mips_opts.isa) ? 9
12306 		 : 16);
12307       goto ld_st;
12308     case M_PREFE_AB:
12309       s = "prefe";
12310       fmt = "k,+j(b)";
12311       offbits = 9;
12312       goto ld_st;
12313     case M_SDC1_AB:
12314       s = "sdc1";
12315       fmt = "T,o(b)";
12316       coproc = 1;
12317       /* Itbl support may require additional care here.  */
12318       goto ld_st;
12319     case M_SDC2_AB:
12320       s = "sdc2";
12321       fmt = COP12_FMT;
12322       offbits = (mips_opts.micromips ? 12
12323 		 : ISA_IS_R6 (mips_opts.isa) ? 11
12324 		 : 16);
12325       /* Itbl support may require additional care here.  */
12326       coproc = 1;
12327       goto ld_st;
12328     case M_SQC2_AB:
12329       s = "sqc2";
12330       fmt = "+7,o(b)";
12331       /* Itbl support may require additional care here.  */
12332       coproc = 1;
12333       goto ld_st;
12334     case M_SDC3_AB:
12335       gas_assert (!mips_opts.micromips);
12336       s = "sdc3";
12337       fmt = "E,o(b)";
12338       /* Itbl support may require additional care here.  */
12339       coproc = 1;
12340       goto ld_st;
12341     case M_SDL_AB:
12342       s = "sdl";
12343       fmt = MEM12_FMT;
12344       offbits = (mips_opts.micromips ? 12 : 16);
12345       goto ld_st;
12346     case M_SDR_AB:
12347       s = "sdr";
12348       fmt = MEM12_FMT;
12349       offbits = (mips_opts.micromips ? 12 : 16);
12350       goto ld_st;
12351     case M_SWP_AB:
12352       gas_assert (mips_opts.micromips);
12353       s = "swp";
12354       fmt = "t,~(b)";
12355       offbits = 12;
12356       goto ld_st;
12357     case M_SDP_AB:
12358       gas_assert (mips_opts.micromips);
12359       s = "sdp";
12360       fmt = "t,~(b)";
12361       offbits = 12;
12362       goto ld_st;
12363     case M_SWM_AB:
12364       gas_assert (mips_opts.micromips);
12365       s = "swm";
12366       fmt = "n,~(b)";
12367       offbits = 12;
12368       goto ld_st;
12369     case M_SDM_AB:
12370       gas_assert (mips_opts.micromips);
12371       s = "sdm";
12372       fmt = "n,~(b)";
12373       offbits = 12;
12374 
12375     ld_st:
12376       tempreg = AT;
12377     ld_noat:
12378       breg = ll_sc_paired ? op[3] : op[2];
12379       if (small_offset_p (0, align, 16))
12380 	{
12381 	  /* The first case exists for M_LD_AB and M_SD_AB, which are
12382 	     macros for o32 but which should act like normal instructions
12383 	     otherwise.  */
12384 	  if (offbits == 16)
12385 	    macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
12386 			 offset_reloc[1], offset_reloc[2], breg);
12387 	  else if (small_offset_p (0, align, offbits))
12388 	    {
12389 	      if (offbits == 0)
12390 		{
12391 		  if (ll_sc_paired)
12392 		   macro_build (NULL, s, fmt, op[0], op[1], breg);
12393 		  else
12394 		   macro_build (NULL, s, fmt, op[0], breg);
12395 		}
12396 	      else
12397 		macro_build (NULL, s, fmt, op[0],
12398 			     (int) offset_expr.X_add_number, breg);
12399 	    }
12400 	  else
12401 	    {
12402 	      if (tempreg == AT)
12403 		used_at = 1;
12404 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12405 			   tempreg, breg, -1, offset_reloc[0],
12406 			   offset_reloc[1], offset_reloc[2]);
12407 	      if (offbits == 0)
12408 		{
12409 		  if (ll_sc_paired)
12410 		    macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12411 		  else
12412 		    macro_build (NULL, s, fmt, op[0], tempreg);
12413 		}
12414 	      else
12415 		macro_build (NULL, s, fmt, op[0], 0, tempreg);
12416 	    }
12417 	  break;
12418 	}
12419 
12420       if (tempreg == AT)
12421 	used_at = 1;
12422 
12423       if (offset_expr.X_op != O_constant
12424 	  && offset_expr.X_op != O_symbol)
12425 	{
12426 	  as_bad (_("expression too complex"));
12427 	  offset_expr.X_op = O_constant;
12428 	}
12429 
12430       if (HAVE_32BIT_ADDRESSES
12431 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
12432 	{
12433 	  char value [32];
12434 
12435 	  sprintf_vma (value, offset_expr.X_add_number);
12436 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
12437 	}
12438 
12439       /* A constant expression in PIC code can be handled just as it
12440 	 is in non PIC code.  */
12441       if (offset_expr.X_op == O_constant)
12442 	{
12443 	  expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12444 						 offbits == 0 ? 16 : offbits);
12445 	  offset_expr.X_add_number -= expr1.X_add_number;
12446 
12447 	  load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12448 	  if (breg != 0)
12449 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12450 			 tempreg, tempreg, breg);
12451 	  if (offbits == 0)
12452 	    {
12453 	      if (offset_expr.X_add_number != 0)
12454 		macro_build (&offset_expr, ADDRESS_ADDI_INSN,
12455 			     "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
12456 	      if (ll_sc_paired)
12457 		macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12458 	      else
12459 		macro_build (NULL, s, fmt, op[0], tempreg);
12460 	    }
12461 	  else if (offbits == 16)
12462 	    macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12463 	  else
12464 	    macro_build (NULL, s, fmt, op[0],
12465 			 (int) offset_expr.X_add_number, tempreg);
12466 	}
12467       else if (offbits != 16)
12468 	{
12469 	  /* The offset field is too narrow to be used for a low-part
12470 	     relocation, so load the whole address into the auxiliary
12471 	     register.  */
12472 	  load_address (tempreg, &offset_expr, &used_at);
12473 	  if (breg != 0)
12474 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12475 			 tempreg, tempreg, breg);
12476 	  if (offbits == 0)
12477 	    {
12478 	      if (ll_sc_paired)
12479 		macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12480 	      else
12481 		macro_build (NULL, s, fmt, op[0], tempreg);
12482 	    }
12483 	  else
12484 	    macro_build (NULL, s, fmt, op[0], 0, tempreg);
12485 	}
12486       else if (mips_pic == NO_PIC)
12487 	{
12488 	  /* If this is a reference to a GP relative symbol, and there
12489 	     is no base register, we want
12490 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
12491 	     Otherwise, if there is no base register, we want
12492 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12493 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12494 	     If we have a constant, we need two instructions anyhow,
12495 	     so we always use the latter form.
12496 
12497 	     If we have a base register, and this is a reference to a
12498 	     GP relative symbol, we want
12499 	       addu	$tempreg,$breg,$gp
12500 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_GPREL16)
12501 	     Otherwise we want
12502 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12503 	       addu	$tempreg,$tempreg,$breg
12504 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12505 	     With a constant we always use the latter case.
12506 
12507 	     With 64bit address space and no base register and $at usable,
12508 	     we want
12509 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12510 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12511 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12512 	       dsll32	$tempreg,0
12513 	       daddu	$tempreg,$at
12514 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12515 	     If we have a base register, we want
12516 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12517 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
12518 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12519 	       daddu	$at,$breg
12520 	       dsll32	$tempreg,0
12521 	       daddu	$tempreg,$at
12522 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12523 
12524 	     Without $at we can't generate the optimal path for superscalar
12525 	     processors here since this would require two temporary registers.
12526 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12527 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12528 	       dsll	$tempreg,16
12529 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12530 	       dsll	$tempreg,16
12531 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12532 	     If we have a base register, we want
12533 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
12534 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
12535 	       dsll	$tempreg,16
12536 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
12537 	       dsll	$tempreg,16
12538 	       daddu	$tempreg,$tempreg,$breg
12539 	       <op>	op[0],<sym>($tempreg)	(BFD_RELOC_LO16)
12540 
12541 	     For GP relative symbols in 64bit address space we can use
12542 	     the same sequence as in 32bit address space.  */
12543 	  if (HAVE_64BIT_SYMBOLS)
12544 	    {
12545 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12546 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12547 		{
12548 		  relax_start (offset_expr.X_add_symbol);
12549 		  if (breg == 0)
12550 		    {
12551 		      macro_build (&offset_expr, s, fmt, op[0],
12552 				   BFD_RELOC_GPREL16, mips_gp_register);
12553 		    }
12554 		  else
12555 		    {
12556 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12557 				   tempreg, breg, mips_gp_register);
12558 		      macro_build (&offset_expr, s, fmt, op[0],
12559 				   BFD_RELOC_GPREL16, tempreg);
12560 		    }
12561 		  relax_switch ();
12562 		}
12563 
12564 	      if (used_at == 0 && mips_opts.at)
12565 		{
12566 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12567 			       BFD_RELOC_MIPS_HIGHEST);
12568 		  macro_build (&offset_expr, "lui", LUI_FMT, AT,
12569 			       BFD_RELOC_HI16_S);
12570 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12571 			       tempreg, BFD_RELOC_MIPS_HIGHER);
12572 		  if (breg != 0)
12573 		    macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
12574 		  macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
12575 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
12576 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
12577 			       tempreg);
12578 		  used_at = 1;
12579 		}
12580 	      else
12581 		{
12582 		  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12583 			       BFD_RELOC_MIPS_HIGHEST);
12584 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12585 			       tempreg, BFD_RELOC_MIPS_HIGHER);
12586 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12587 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12588 			       tempreg, BFD_RELOC_HI16_S);
12589 		  macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
12590 		  if (breg != 0)
12591 		    macro_build (NULL, "daddu", "d,v,t",
12592 				 tempreg, tempreg, breg);
12593 		  macro_build (&offset_expr, s, fmt, op[0],
12594 			       BFD_RELOC_LO16, tempreg);
12595 		}
12596 
12597 	      if (mips_relax.sequence)
12598 		relax_end ();
12599 	      break;
12600 	    }
12601 
12602 	  if (breg == 0)
12603 	    {
12604 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12605 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12606 		{
12607 		  relax_start (offset_expr.X_add_symbol);
12608 		  macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
12609 			       mips_gp_register);
12610 		  relax_switch ();
12611 		}
12612 	      macro_build_lui (&offset_expr, tempreg);
12613 	      macro_build (&offset_expr, s, fmt, op[0],
12614 			   BFD_RELOC_LO16, tempreg);
12615 	      if (mips_relax.sequence)
12616 		relax_end ();
12617 	    }
12618 	  else
12619 	    {
12620 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
12621 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12622 		{
12623 		  relax_start (offset_expr.X_add_symbol);
12624 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12625 			       tempreg, breg, mips_gp_register);
12626 		  macro_build (&offset_expr, s, fmt, op[0],
12627 			       BFD_RELOC_GPREL16, tempreg);
12628 		  relax_switch ();
12629 		}
12630 	      macro_build_lui (&offset_expr, tempreg);
12631 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12632 			   tempreg, tempreg, breg);
12633 	      macro_build (&offset_expr, s, fmt, op[0],
12634 			   BFD_RELOC_LO16, tempreg);
12635 	      if (mips_relax.sequence)
12636 		relax_end ();
12637 	    }
12638 	}
12639       else if (!mips_big_got)
12640 	{
12641 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
12642 
12643 	  /* If this is a reference to an external symbol, we want
12644 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12645 	       nop
12646 	       <op>	op[0],0($tempreg)
12647 	     Otherwise we want
12648 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12649 	       nop
12650 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
12651 	       <op>	op[0],0($tempreg)
12652 
12653 	     For NewABI, we want
12654 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
12655 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
12656 
12657 	     If there is a base register, we add it to $tempreg before
12658 	     the <op>.  If there is a constant, we stick it in the
12659 	     <op> instruction.  We don't handle constants larger than
12660 	     16 bits, because we have no way to load the upper 16 bits
12661 	     (actually, we could handle them for the subset of cases
12662 	     in which we are not using $at).  */
12663 	  gas_assert (offset_expr.X_op == O_symbol);
12664 	  if (HAVE_NEWABI)
12665 	    {
12666 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12667 			   BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12668 	      if (breg != 0)
12669 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12670 			     tempreg, tempreg, breg);
12671 	      macro_build (&offset_expr, s, fmt, op[0],
12672 			   BFD_RELOC_MIPS_GOT_OFST, tempreg);
12673 	      break;
12674 	    }
12675 	  expr1.X_add_number = offset_expr.X_add_number;
12676 	  offset_expr.X_add_number = 0;
12677 	  if (expr1.X_add_number < -0x8000
12678 	      || expr1.X_add_number >= 0x8000)
12679 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12680 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12681 		       lw_reloc_type, mips_gp_register);
12682 	  load_delay_nop ();
12683 	  relax_start (offset_expr.X_add_symbol);
12684 	  relax_switch ();
12685 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12686 		       tempreg, BFD_RELOC_LO16);
12687 	  relax_end ();
12688 	  if (breg != 0)
12689 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12690 			 tempreg, tempreg, breg);
12691 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12692 	}
12693       else if (mips_big_got && !HAVE_NEWABI)
12694 	{
12695 	  int gpdelay;
12696 
12697 	  /* If this is a reference to an external symbol, we want
12698 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12699 	       addu	$tempreg,$tempreg,$gp
12700 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12701 	       <op>	op[0],0($tempreg)
12702 	     Otherwise we want
12703 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
12704 	       nop
12705 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
12706 	       <op>	op[0],0($tempreg)
12707 	     If there is a base register, we add it to $tempreg before
12708 	     the <op>.  If there is a constant, we stick it in the
12709 	     <op> instruction.  We don't handle constants larger than
12710 	     16 bits, because we have no way to load the upper 16 bits
12711 	     (actually, we could handle them for the subset of cases
12712 	     in which we are not using $at).  */
12713 	  gas_assert (offset_expr.X_op == O_symbol);
12714 	  expr1.X_add_number = offset_expr.X_add_number;
12715 	  offset_expr.X_add_number = 0;
12716 	  if (expr1.X_add_number < -0x8000
12717 	      || expr1.X_add_number >= 0x8000)
12718 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12719 	  gpdelay = reg_needs_delay (mips_gp_register);
12720 	  relax_start (offset_expr.X_add_symbol);
12721 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12722 		       BFD_RELOC_MIPS_GOT_HI16);
12723 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12724 		       mips_gp_register);
12725 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12726 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12727 	  relax_switch ();
12728 	  if (gpdelay)
12729 	    macro_build (NULL, "nop", "");
12730 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12731 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12732 	  load_delay_nop ();
12733 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12734 		       tempreg, BFD_RELOC_LO16);
12735 	  relax_end ();
12736 
12737 	  if (breg != 0)
12738 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12739 			 tempreg, tempreg, breg);
12740 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12741 	}
12742       else if (mips_big_got && HAVE_NEWABI)
12743 	{
12744 	  /* If this is a reference to an external symbol, we want
12745 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
12746 	       add	$tempreg,$tempreg,$gp
12747 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
12748 	       <op>	op[0],<ofst>($tempreg)
12749 	     Otherwise, for local symbols, we want:
12750 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
12751 	       <op>	op[0],<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
12752 	  gas_assert (offset_expr.X_op == O_symbol);
12753 	  expr1.X_add_number = offset_expr.X_add_number;
12754 	  offset_expr.X_add_number = 0;
12755 	  if (expr1.X_add_number < -0x8000
12756 	      || expr1.X_add_number >= 0x8000)
12757 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
12758 	  relax_start (offset_expr.X_add_symbol);
12759 	  macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
12760 		       BFD_RELOC_MIPS_GOT_HI16);
12761 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12762 		       mips_gp_register);
12763 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12764 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
12765 	  if (breg != 0)
12766 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12767 			 tempreg, tempreg, breg);
12768 	  macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
12769 
12770 	  relax_switch ();
12771 	  offset_expr.X_add_number = expr1.X_add_number;
12772 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12773 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
12774 	  if (breg != 0)
12775 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12776 			 tempreg, tempreg, breg);
12777 	  macro_build (&offset_expr, s, fmt, op[0],
12778 		       BFD_RELOC_MIPS_GOT_OFST, tempreg);
12779 	  relax_end ();
12780 	}
12781       else
12782 	abort ();
12783 
12784       break;
12785 
12786     case M_JRADDIUSP:
12787       gas_assert (mips_opts.micromips);
12788       gas_assert (mips_opts.insn32);
12789       start_noreorder ();
12790       macro_build (NULL, "jr", "s", RA);
12791       expr1.X_add_number = op[0] << 2;
12792       macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12793       end_noreorder ();
12794       break;
12795 
12796     case M_JRC:
12797       gas_assert (mips_opts.micromips);
12798       gas_assert (mips_opts.insn32);
12799       macro_build (NULL, "jr", "s", op[0]);
12800       if (mips_opts.noreorder)
12801 	macro_build (NULL, "nop", "");
12802       break;
12803 
12804     case M_LI:
12805     case M_LI_S:
12806       load_register (op[0], &imm_expr, 0);
12807       break;
12808 
12809     case M_DLI:
12810       load_register (op[0], &imm_expr, 1);
12811       break;
12812 
12813     case M_LI_SS:
12814       if (imm_expr.X_op == O_constant)
12815 	{
12816 	  used_at = 1;
12817 	  load_register (AT, &imm_expr, 0);
12818 	  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12819 	  break;
12820 	}
12821       else
12822 	{
12823 	  gas_assert (imm_expr.X_op == O_absent
12824 		      && offset_expr.X_op == O_symbol
12825 		      && strcmp (segment_name (S_GET_SEGMENT
12826 					       (offset_expr.X_add_symbol)),
12827 				 ".lit4") == 0
12828 		      && offset_expr.X_add_number == 0);
12829 	  macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
12830 		       BFD_RELOC_MIPS_LITERAL, mips_gp_register);
12831 	  break;
12832 	}
12833 
12834     case M_LI_D:
12835       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
12836          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
12837          order 32 bits of the value and the low order 32 bits are either
12838          zero or in OFFSET_EXPR.  */
12839       if (imm_expr.X_op == O_constant)
12840 	{
12841 	  if (GPR_SIZE == 64)
12842 	    load_register (op[0], &imm_expr, 1);
12843 	  else
12844 	    {
12845 	      int hreg, lreg;
12846 
12847 	      if (target_big_endian)
12848 		{
12849 		  hreg = op[0];
12850 		  lreg = op[0] + 1;
12851 		}
12852 	      else
12853 		{
12854 		  hreg = op[0] + 1;
12855 		  lreg = op[0];
12856 		}
12857 
12858 	      if (hreg <= 31)
12859 		load_register (hreg, &imm_expr, 0);
12860 	      if (lreg <= 31)
12861 		{
12862 		  if (offset_expr.X_op == O_absent)
12863 		    move_register (lreg, 0);
12864 		  else
12865 		    {
12866 		      gas_assert (offset_expr.X_op == O_constant);
12867 		      load_register (lreg, &offset_expr, 0);
12868 		    }
12869 		}
12870 	    }
12871 	  break;
12872 	}
12873       gas_assert (imm_expr.X_op == O_absent);
12874 
12875       /* We know that sym is in the .rdata section.  First we get the
12876 	 upper 16 bits of the address.  */
12877       if (mips_pic == NO_PIC)
12878 	{
12879 	  macro_build_lui (&offset_expr, AT);
12880 	  used_at = 1;
12881 	}
12882       else
12883 	{
12884 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12885 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
12886 	  used_at = 1;
12887 	}
12888 
12889       /* Now we load the register(s).  */
12890       if (GPR_SIZE == 64)
12891 	{
12892 	  used_at = 1;
12893 	  macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12894 		       BFD_RELOC_LO16, AT);
12895 	}
12896       else
12897 	{
12898 	  used_at = 1;
12899 	  macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12900 		       BFD_RELOC_LO16, AT);
12901 	  if (op[0] != RA)
12902 	    {
12903 	      /* FIXME: How in the world do we deal with the possible
12904 		 overflow here?  */
12905 	      offset_expr.X_add_number += 4;
12906 	      macro_build (&offset_expr, "lw", "t,o(b)",
12907 			   op[0] + 1, BFD_RELOC_LO16, AT);
12908 	    }
12909 	}
12910       break;
12911 
12912     case M_LI_DD:
12913       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
12914          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12915          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
12916          the value and the low order 32 bits are either zero or in
12917          OFFSET_EXPR.  */
12918       if (imm_expr.X_op == O_constant)
12919 	{
12920 	  tempreg = ZERO;
12921 	  if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12922 	       || !ISA_HAS_MXHC1 (mips_opts.isa))
12923 	      && imm_expr.X_add_number != 0)
12924 	    {
12925 	      used_at = 1;
12926 	      tempreg = AT;
12927 	      load_register (AT, &imm_expr, FPR_SIZE == 64);
12928 	    }
12929 	  if (FPR_SIZE == 64 && GPR_SIZE == 64)
12930 	    macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
12931 	  else
12932 	    {
12933 	      if (!ISA_HAS_MXHC1 (mips_opts.isa))
12934 		{
12935 		  if (FPR_SIZE != 32)
12936 		    as_bad (_("Unable to generate `%s' compliant code "
12937 			      "without mthc1"),
12938 			    (FPR_SIZE == 64) ? "fp64" : "fpxx");
12939 		  else
12940 		    macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12941 		}
12942 	      if (offset_expr.X_op == O_absent)
12943 		macro_build (NULL, "mtc1", "t,G", 0, op[0]);
12944 	      else
12945 		{
12946 		  gas_assert (offset_expr.X_op == O_constant);
12947 		  load_register (AT, &offset_expr, 0);
12948 		  macro_build (NULL, "mtc1", "t,G", AT, op[0]);
12949 		}
12950 	      if (ISA_HAS_MXHC1 (mips_opts.isa))
12951 		{
12952 		  if (imm_expr.X_add_number != 0)
12953 		    {
12954 		      used_at = 1;
12955 		      tempreg = AT;
12956 		      load_register (AT, &imm_expr, 0);
12957 		    }
12958 		  macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12959 		}
12960 	    }
12961 	  break;
12962 	}
12963 
12964       gas_assert (imm_expr.X_op == O_absent
12965 		  && offset_expr.X_op == O_symbol
12966 		  && offset_expr.X_add_number == 0);
12967       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12968       if (strcmp (s, ".lit8") == 0)
12969 	{
12970 	  op[2] = mips_gp_register;
12971 	  offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12972 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12973 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12974 	}
12975       else
12976 	{
12977 	  gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
12978 	  used_at = 1;
12979 	  if (mips_pic != NO_PIC)
12980 	    macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12981 			 BFD_RELOC_MIPS_GOT16, mips_gp_register);
12982 	  else
12983 	    {
12984 	      /* FIXME: This won't work for a 64 bit address.  */
12985 	      macro_build_lui (&offset_expr, AT);
12986 	    }
12987 
12988 	  op[2] = AT;
12989 	  offset_reloc[0] = BFD_RELOC_LO16;
12990 	  offset_reloc[1] = BFD_RELOC_UNUSED;
12991 	  offset_reloc[2] = BFD_RELOC_UNUSED;
12992 	}
12993       align = 8;
12994       /* Fall through.  */
12995 
12996     case M_L_DAB:
12997       /* The MIPS assembler seems to check for X_add_number not
12998          being double aligned and generating:
12999         	lui	at,%hi(foo+1)
13000         	addu	at,at,v1
13001         	addiu	at,at,%lo(foo+1)
13002         	lwc1	f2,0(at)
13003         	lwc1	f3,4(at)
13004          But, the resulting address is the same after relocation so why
13005          generate the extra instruction?  */
13006       /* Itbl support may require additional care here.  */
13007       coproc = 1;
13008       fmt = "T,o(b)";
13009       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
13010 	{
13011 	  s = "ldc1";
13012 	  goto ld_st;
13013 	}
13014       s = "lwc1";
13015       goto ldd_std;
13016 
13017     case M_S_DAB:
13018       gas_assert (!mips_opts.micromips);
13019       /* Itbl support may require additional care here.  */
13020       coproc = 1;
13021       fmt = "T,o(b)";
13022       if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
13023 	{
13024 	  s = "sdc1";
13025 	  goto ld_st;
13026 	}
13027       s = "swc1";
13028       goto ldd_std;
13029 
13030     case M_LQ_AB:
13031       fmt = "t,o(b)";
13032       s = "lq";
13033       goto ld;
13034 
13035     case M_SQ_AB:
13036       fmt = "t,o(b)";
13037       s = "sq";
13038       goto ld_st;
13039 
13040     case M_LD_AB:
13041       fmt = "t,o(b)";
13042       if (GPR_SIZE == 64)
13043 	{
13044 	  s = "ld";
13045 	  goto ld;
13046 	}
13047       s = "lw";
13048       goto ldd_std;
13049 
13050     case M_SD_AB:
13051       fmt = "t,o(b)";
13052       if (GPR_SIZE == 64)
13053 	{
13054 	  s = "sd";
13055 	  goto ld_st;
13056 	}
13057       s = "sw";
13058 
13059     ldd_std:
13060       /* Even on a big endian machine $fn comes before $fn+1.  We have
13061 	 to adjust when loading from memory.  We set coproc if we must
13062 	 load $fn+1 first.  */
13063       /* Itbl support may require additional care here.  */
13064       if (!target_big_endian)
13065 	coproc = 0;
13066 
13067       breg = op[2];
13068       if (small_offset_p (0, align, 16))
13069 	{
13070 	  ep = &offset_expr;
13071 	  if (!small_offset_p (4, align, 16))
13072 	    {
13073 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
13074 			   -1, offset_reloc[0], offset_reloc[1],
13075 			   offset_reloc[2]);
13076 	      expr1.X_add_number = 0;
13077 	      ep = &expr1;
13078 	      breg = AT;
13079 	      used_at = 1;
13080 	      offset_reloc[0] = BFD_RELOC_LO16;
13081 	      offset_reloc[1] = BFD_RELOC_UNUSED;
13082 	      offset_reloc[2] = BFD_RELOC_UNUSED;
13083 	    }
13084 	  if (strcmp (s, "lw") == 0 && op[0] == breg)
13085 	    {
13086 	      ep->X_add_number += 4;
13087 	      macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
13088 			   offset_reloc[1], offset_reloc[2], breg);
13089 	      ep->X_add_number -= 4;
13090 	      macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
13091 			   offset_reloc[1], offset_reloc[2], breg);
13092 	    }
13093 	  else
13094 	    {
13095 	      macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
13096 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
13097 			   breg);
13098 	      ep->X_add_number += 4;
13099 	      macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
13100 			   offset_reloc[0], offset_reloc[1], offset_reloc[2],
13101 			   breg);
13102 	    }
13103 	  break;
13104 	}
13105 
13106       if (offset_expr.X_op != O_symbol
13107 	  && offset_expr.X_op != O_constant)
13108 	{
13109 	  as_bad (_("expression too complex"));
13110 	  offset_expr.X_op = O_constant;
13111 	}
13112 
13113       if (HAVE_32BIT_ADDRESSES
13114 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
13115 	{
13116 	  char value [32];
13117 
13118 	  sprintf_vma (value, offset_expr.X_add_number);
13119 	  as_bad (_("number (0x%s) larger than 32 bits"), value);
13120 	}
13121 
13122       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
13123 	{
13124 	  /* If this is a reference to a GP relative symbol, we want
13125 	       <op>	op[0],<sym>($gp)	(BFD_RELOC_GPREL16)
13126 	       <op>	op[0]+1,<sym>+4($gp)	(BFD_RELOC_GPREL16)
13127 	     If we have a base register, we use this
13128 	       addu	$at,$breg,$gp
13129 	       <op>	op[0],<sym>($at)	(BFD_RELOC_GPREL16)
13130 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_GPREL16)
13131 	     If this is not a GP relative symbol, we want
13132 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
13133 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
13134 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
13135 	     If there is a base register, we add it to $at after the
13136 	     lui instruction.  If there is a constant, we always use
13137 	     the last case.  */
13138 	  if (offset_expr.X_op == O_symbol
13139 	      && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
13140 	      && !nopic_need_relax (offset_expr.X_add_symbol, 1))
13141 	    {
13142 	      relax_start (offset_expr.X_add_symbol);
13143 	      if (breg == 0)
13144 		{
13145 		  tempreg = mips_gp_register;
13146 		}
13147 	      else
13148 		{
13149 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13150 			       AT, breg, mips_gp_register);
13151 		  tempreg = AT;
13152 		  used_at = 1;
13153 		}
13154 
13155 	      /* Itbl support may require additional care here.  */
13156 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13157 			   BFD_RELOC_GPREL16, tempreg);
13158 	      offset_expr.X_add_number += 4;
13159 
13160 	      /* Set mips_optimize to 2 to avoid inserting an
13161                  undesired nop.  */
13162 	      hold_mips_optimize = mips_optimize;
13163 	      mips_optimize = 2;
13164 	      /* Itbl support may require additional care here.  */
13165 	      macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13166 			   BFD_RELOC_GPREL16, tempreg);
13167 	      mips_optimize = hold_mips_optimize;
13168 
13169 	      relax_switch ();
13170 
13171 	      offset_expr.X_add_number -= 4;
13172 	    }
13173 	  used_at = 1;
13174 	  if (offset_high_part (offset_expr.X_add_number, 16)
13175 	      != offset_high_part (offset_expr.X_add_number + 4, 16))
13176 	    {
13177 	      load_address (AT, &offset_expr, &used_at);
13178 	      offset_expr.X_op = O_constant;
13179 	      offset_expr.X_add_number = 0;
13180 	    }
13181 	  else
13182 	    macro_build_lui (&offset_expr, AT);
13183 	  if (breg != 0)
13184 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13185 	  /* Itbl support may require additional care here.  */
13186 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13187 		       BFD_RELOC_LO16, AT);
13188 	  /* FIXME: How do we handle overflow here?  */
13189 	  offset_expr.X_add_number += 4;
13190 	  /* Itbl support may require additional care here.  */
13191 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13192 		       BFD_RELOC_LO16, AT);
13193 	  if (mips_relax.sequence)
13194 	    relax_end ();
13195 	}
13196       else if (!mips_big_got)
13197 	{
13198 	  /* If this is a reference to an external symbol, we want
13199 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
13200 	       nop
13201 	       <op>	op[0],0($at)
13202 	       <op>	op[0]+1,4($at)
13203 	     Otherwise we want
13204 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
13205 	       nop
13206 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
13207 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
13208 	     If there is a base register we add it to $at before the
13209 	     lwc1 instructions.  If there is a constant we include it
13210 	     in the lwc1 instructions.  */
13211 	  used_at = 1;
13212 	  expr1.X_add_number = offset_expr.X_add_number;
13213 	  if (expr1.X_add_number < -0x8000
13214 	      || expr1.X_add_number >= 0x8000 - 4)
13215 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13216 	  load_got_offset (AT, &offset_expr);
13217 	  load_delay_nop ();
13218 	  if (breg != 0)
13219 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13220 
13221 	  /* Set mips_optimize to 2 to avoid inserting an undesired
13222              nop.  */
13223 	  hold_mips_optimize = mips_optimize;
13224 	  mips_optimize = 2;
13225 
13226 	  /* Itbl support may require additional care here.  */
13227 	  relax_start (offset_expr.X_add_symbol);
13228 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13229 		       BFD_RELOC_LO16, AT);
13230 	  expr1.X_add_number += 4;
13231 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13232 		       BFD_RELOC_LO16, AT);
13233 	  relax_switch ();
13234 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13235 		       BFD_RELOC_LO16, AT);
13236 	  offset_expr.X_add_number += 4;
13237 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13238 		       BFD_RELOC_LO16, AT);
13239 	  relax_end ();
13240 
13241 	  mips_optimize = hold_mips_optimize;
13242 	}
13243       else if (mips_big_got)
13244 	{
13245 	  int gpdelay;
13246 
13247 	  /* If this is a reference to an external symbol, we want
13248 	       lui	$at,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
13249 	       addu	$at,$at,$gp
13250 	       lw	$at,<sym>($at)		(BFD_RELOC_MIPS_GOT_LO16)
13251 	       nop
13252 	       <op>	op[0],0($at)
13253 	       <op>	op[0]+1,4($at)
13254 	     Otherwise we want
13255 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
13256 	       nop
13257 	       <op>	op[0],<sym>($at)	(BFD_RELOC_LO16)
13258 	       <op>	op[0]+1,<sym>+4($at)	(BFD_RELOC_LO16)
13259 	     If there is a base register we add it to $at before the
13260 	     lwc1 instructions.  If there is a constant we include it
13261 	     in the lwc1 instructions.  */
13262 	  used_at = 1;
13263 	  expr1.X_add_number = offset_expr.X_add_number;
13264 	  offset_expr.X_add_number = 0;
13265 	  if (expr1.X_add_number < -0x8000
13266 	      || expr1.X_add_number >= 0x8000 - 4)
13267 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
13268 	  gpdelay = reg_needs_delay (mips_gp_register);
13269 	  relax_start (offset_expr.X_add_symbol);
13270 	  macro_build (&offset_expr, "lui", LUI_FMT,
13271 		       AT, BFD_RELOC_MIPS_GOT_HI16);
13272 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13273 		       AT, AT, mips_gp_register);
13274 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
13275 		       AT, BFD_RELOC_MIPS_GOT_LO16, AT);
13276 	  load_delay_nop ();
13277 	  if (breg != 0)
13278 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13279 	  /* Itbl support may require additional care here.  */
13280 	  macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
13281 		       BFD_RELOC_LO16, AT);
13282 	  expr1.X_add_number += 4;
13283 
13284 	  /* Set mips_optimize to 2 to avoid inserting an undesired
13285              nop.  */
13286 	  hold_mips_optimize = mips_optimize;
13287 	  mips_optimize = 2;
13288 	  /* Itbl support may require additional care here.  */
13289 	  macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
13290 		       BFD_RELOC_LO16, AT);
13291 	  mips_optimize = hold_mips_optimize;
13292 	  expr1.X_add_number -= 4;
13293 
13294 	  relax_switch ();
13295 	  offset_expr.X_add_number = expr1.X_add_number;
13296 	  if (gpdelay)
13297 	    macro_build (NULL, "nop", "");
13298 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13299 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
13300 	  load_delay_nop ();
13301 	  if (breg != 0)
13302 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
13303 	  /* Itbl support may require additional care here.  */
13304 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
13305 		       BFD_RELOC_LO16, AT);
13306 	  offset_expr.X_add_number += 4;
13307 
13308 	  /* Set mips_optimize to 2 to avoid inserting an undesired
13309              nop.  */
13310 	  hold_mips_optimize = mips_optimize;
13311 	  mips_optimize = 2;
13312 	  /* Itbl support may require additional care here.  */
13313 	  macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
13314 		       BFD_RELOC_LO16, AT);
13315 	  mips_optimize = hold_mips_optimize;
13316 	  relax_end ();
13317 	}
13318       else
13319 	abort ();
13320 
13321       break;
13322 
13323     case M_SAA_AB:
13324       s = "saa";
13325       goto saa_saad;
13326     case M_SAAD_AB:
13327       s = "saad";
13328     saa_saad:
13329       gas_assert (!mips_opts.micromips);
13330       offbits = 0;
13331       fmt = "t,(b)";
13332       goto ld_st;
13333 
13334    /* New code added to support COPZ instructions.
13335       This code builds table entries out of the macros in mip_opcodes.
13336       R4000 uses interlocks to handle coproc delays.
13337       Other chips (like the R3000) require nops to be inserted for delays.
13338 
13339       FIXME: Currently, we require that the user handle delays.
13340       In order to fill delay slots for non-interlocked chips,
13341       we must have a way to specify delays based on the coprocessor.
13342       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13343       What are the side-effects of the cop instruction?
13344       What cache support might we have and what are its effects?
13345       Both coprocessor & memory require delays. how long???
13346       What registers are read/set/modified?
13347 
13348       If an itbl is provided to interpret cop instructions,
13349       this knowledge can be encoded in the itbl spec.  */
13350 
13351     case M_COP0:
13352       s = "c0";
13353       goto copz;
13354     case M_COP1:
13355       s = "c1";
13356       goto copz;
13357     case M_COP2:
13358       s = "c2";
13359       goto copz;
13360     case M_COP3:
13361       s = "c3";
13362     copz:
13363       gas_assert (!mips_opts.micromips);
13364       /* For now we just do C (same as Cz).  The parameter will be
13365          stored in insn_opcode by mips_ip.  */
13366       macro_build (NULL, s, "C", (int) ip->insn_opcode);
13367       break;
13368 
13369     case M_MOVE:
13370       move_register (op[0], op[1]);
13371       break;
13372 
13373     case M_MOVEP:
13374       gas_assert (mips_opts.micromips);
13375       gas_assert (mips_opts.insn32);
13376       move_register (micromips_to_32_reg_h_map1[op[0]],
13377 		     micromips_to_32_reg_m_map[op[1]]);
13378       move_register (micromips_to_32_reg_h_map2[op[0]],
13379 		     micromips_to_32_reg_n_map[op[2]]);
13380       break;
13381 
13382     case M_DMUL:
13383       dbl = 1;
13384       /* Fall through.  */
13385     case M_MUL:
13386       if (mips_opts.arch == CPU_R5900)
13387 	macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13388 		     op[2]);
13389       else
13390         {
13391 	  macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13392 	  macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13393         }
13394       break;
13395 
13396     case M_DMUL_I:
13397       dbl = 1;
13398       /* Fall through.  */
13399     case M_MUL_I:
13400       /* The MIPS assembler some times generates shifts and adds.  I'm
13401 	 not trying to be that fancy. GCC should do this for us
13402 	 anyway.  */
13403       used_at = 1;
13404       load_register (AT, &imm_expr, dbl);
13405       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13406       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13407       break;
13408 
13409     case M_DMULO_I:
13410       dbl = 1;
13411       /* Fall through.  */
13412     case M_MULO_I:
13413       imm = 1;
13414       goto do_mulo;
13415 
13416     case M_DMULO:
13417       dbl = 1;
13418       /* Fall through.  */
13419     case M_MULO:
13420     do_mulo:
13421       start_noreorder ();
13422       used_at = 1;
13423       if (imm)
13424 	load_register (AT, &imm_expr, dbl);
13425       macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13426 		   op[1], imm ? AT : op[2]);
13427       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13428       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
13429       macro_build (NULL, "mfhi", MFHL_FMT, AT);
13430       if (mips_trap)
13431 	macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
13432       else
13433 	{
13434 	  if (mips_opts.micromips)
13435 	    micromips_label_expr (&label_expr);
13436 	  else
13437 	    label_expr.X_add_number = 8;
13438 	  macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
13439 	  macro_build (NULL, "nop", "");
13440 	  macro_build (NULL, "break", BRK_FMT, 6);
13441 	  if (mips_opts.micromips)
13442 	    micromips_add_label ();
13443 	}
13444       end_noreorder ();
13445       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13446       break;
13447 
13448     case M_DMULOU_I:
13449       dbl = 1;
13450       /* Fall through.  */
13451     case M_MULOU_I:
13452       imm = 1;
13453       goto do_mulou;
13454 
13455     case M_DMULOU:
13456       dbl = 1;
13457       /* Fall through.  */
13458     case M_MULOU:
13459     do_mulou:
13460       start_noreorder ();
13461       used_at = 1;
13462       if (imm)
13463 	load_register (AT, &imm_expr, dbl);
13464       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
13465 		   op[1], imm ? AT : op[2]);
13466       macro_build (NULL, "mfhi", MFHL_FMT, AT);
13467       macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13468       if (mips_trap)
13469 	macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
13470       else
13471 	{
13472 	  if (mips_opts.micromips)
13473 	    micromips_label_expr (&label_expr);
13474 	  else
13475 	    label_expr.X_add_number = 8;
13476 	  macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
13477 	  macro_build (NULL, "nop", "");
13478 	  macro_build (NULL, "break", BRK_FMT, 6);
13479 	  if (mips_opts.micromips)
13480 	    micromips_add_label ();
13481 	}
13482       end_noreorder ();
13483       break;
13484 
13485     case M_DROL:
13486       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13487 	{
13488 	  if (op[0] == op[1])
13489 	    {
13490 	      tempreg = AT;
13491 	      used_at = 1;
13492 	    }
13493 	  else
13494 	    tempreg = op[0];
13495 	  macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13496 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
13497 	  break;
13498 	}
13499       used_at = 1;
13500       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13501       macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13502       macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13503       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13504       break;
13505 
13506     case M_ROL:
13507       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13508 	{
13509 	  if (op[0] == op[1])
13510 	    {
13511 	      tempreg = AT;
13512 	      used_at = 1;
13513 	    }
13514 	  else
13515 	    tempreg = op[0];
13516 	  macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13517 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
13518 	  break;
13519 	}
13520       used_at = 1;
13521       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13522       macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13523       macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13524       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13525       break;
13526 
13527     case M_DROL_I:
13528       {
13529 	unsigned int rot;
13530 	const char *l;
13531 	const char *rr;
13532 
13533 	rot = imm_expr.X_add_number & 0x3f;
13534 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13535 	  {
13536 	    rot = (64 - rot) & 0x3f;
13537 	    if (rot >= 32)
13538 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13539 	    else
13540 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13541 	    break;
13542 	  }
13543 	if (rot == 0)
13544 	  {
13545 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13546 	    break;
13547 	  }
13548 	l = (rot < 0x20) ? "dsll" : "dsll32";
13549 	rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
13550 	rot &= 0x1f;
13551 	used_at = 1;
13552 	macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13553 	macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13554 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13555       }
13556       break;
13557 
13558     case M_ROL_I:
13559       {
13560 	unsigned int rot;
13561 
13562 	rot = imm_expr.X_add_number & 0x1f;
13563 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13564 	  {
13565 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13566 			 (32 - rot) & 0x1f);
13567 	    break;
13568 	  }
13569 	if (rot == 0)
13570 	  {
13571 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13572 	    break;
13573 	  }
13574 	used_at = 1;
13575 	macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13576 	macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13577 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13578       }
13579       break;
13580 
13581     case M_DROR:
13582       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13583 	{
13584 	  macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
13585 	  break;
13586 	}
13587       used_at = 1;
13588       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13589       macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13590       macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13591       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13592       break;
13593 
13594     case M_ROR:
13595       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13596 	{
13597 	  macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
13598 	  break;
13599 	}
13600       used_at = 1;
13601       macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13602       macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13603       macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13604       macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13605       break;
13606 
13607     case M_DROR_I:
13608       {
13609 	unsigned int rot;
13610 	const char *l;
13611 	const char *rr;
13612 
13613 	rot = imm_expr.X_add_number & 0x3f;
13614 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
13615 	  {
13616 	    if (rot >= 32)
13617 	      macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
13618 	    else
13619 	      macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
13620 	    break;
13621 	  }
13622 	if (rot == 0)
13623 	  {
13624 	    macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
13625 	    break;
13626 	  }
13627 	rr = (rot < 0x20) ? "dsrl" : "dsrl32";
13628 	l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13629 	rot &= 0x1f;
13630 	used_at = 1;
13631 	macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13632 	macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13633 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13634       }
13635       break;
13636 
13637     case M_ROR_I:
13638       {
13639 	unsigned int rot;
13640 
13641 	rot = imm_expr.X_add_number & 0x1f;
13642 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
13643 	  {
13644 	    macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
13645 	    break;
13646 	  }
13647 	if (rot == 0)
13648 	  {
13649 	    macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
13650 	    break;
13651 	  }
13652 	used_at = 1;
13653 	macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13654 	macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13655 	macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
13656       }
13657       break;
13658 
13659     case M_SEQ:
13660       if (op[1] == 0)
13661 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13662       else if (op[2] == 0)
13663 	macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13664       else
13665 	{
13666 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13667 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13668 	}
13669       break;
13670 
13671     case M_SEQ_I:
13672       if (imm_expr.X_add_number == 0)
13673 	{
13674 	  macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13675 	  break;
13676 	}
13677       if (op[1] == 0)
13678 	{
13679 	  as_warn (_("instruction %s: result is always false"),
13680 		   ip->insn_mo->name);
13681 	  move_register (op[0], 0);
13682 	  break;
13683 	}
13684       if (CPU_HAS_SEQ (mips_opts.arch)
13685 	  && -512 <= imm_expr.X_add_number
13686 	  && imm_expr.X_add_number < 512)
13687 	{
13688 	  macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
13689 		       (int) imm_expr.X_add_number);
13690 	  break;
13691 	}
13692       if (imm_expr.X_add_number >= 0
13693 	  && imm_expr.X_add_number < 0x10000)
13694 	macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
13695       else if (imm_expr.X_add_number > -0x8000
13696 	       && imm_expr.X_add_number < 0)
13697 	{
13698 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13699 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13700 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13701 	}
13702       else if (CPU_HAS_SEQ (mips_opts.arch))
13703 	{
13704 	  used_at = 1;
13705 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13706 	  macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
13707 	  break;
13708 	}
13709       else
13710 	{
13711 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13712 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13713 	  used_at = 1;
13714 	}
13715       macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
13716       break;
13717 
13718     case M_SGE:		/* X >= Y  <==>  not (X < Y) */
13719       s = "slt";
13720       goto sge;
13721     case M_SGEU:
13722       s = "sltu";
13723     sge:
13724       macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13725       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13726       break;
13727 
13728     case M_SGE_I:	/* X >= I  <==>  not (X < I).  */
13729     case M_SGEU_I:
13730       if (imm_expr.X_add_number >= -0x8000
13731 	  && imm_expr.X_add_number < 0x8000)
13732 	macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13733 		     op[0], op[1], BFD_RELOC_LO16);
13734       else
13735 	{
13736 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13737 	  macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
13738 		       op[0], op[1], AT);
13739 	  used_at = 1;
13740 	}
13741       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13742       break;
13743 
13744     case M_SGT:		/* X > Y  <==>  Y < X.  */
13745       s = "slt";
13746       goto sgt;
13747     case M_SGTU:
13748       s = "sltu";
13749     sgt:
13750       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13751       break;
13752 
13753     case M_SGT_I:	/* X > I  <==>  I < X.  */
13754       s = "slt";
13755       goto sgti;
13756     case M_SGTU_I:
13757       s = "sltu";
13758     sgti:
13759       used_at = 1;
13760       load_register (AT, &imm_expr, GPR_SIZE == 64);
13761       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13762       break;
13763 
13764     case M_SLE:		/* X <= Y  <==>  Y >= X  <==>  not (Y < X).  */
13765       s = "slt";
13766       goto sle;
13767     case M_SLEU:
13768       s = "sltu";
13769     sle:
13770       macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13771       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13772       break;
13773 
13774     case M_SLE_I:	/* X <= I  <==>  I >= X  <==>  not (I < X) */
13775       s = "slt";
13776       goto slei;
13777     case M_SLEU_I:
13778       s = "sltu";
13779     slei:
13780       used_at = 1;
13781       load_register (AT, &imm_expr, GPR_SIZE == 64);
13782       macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13783       macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
13784       break;
13785 
13786     case M_SLT_I:
13787       if (imm_expr.X_add_number >= -0x8000
13788 	  && imm_expr.X_add_number < 0x8000)
13789 	{
13790 	  macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13791 		       BFD_RELOC_LO16);
13792 	  break;
13793 	}
13794       used_at = 1;
13795       load_register (AT, &imm_expr, GPR_SIZE == 64);
13796       macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
13797       break;
13798 
13799     case M_SLTU_I:
13800       if (imm_expr.X_add_number >= -0x8000
13801 	  && imm_expr.X_add_number < 0x8000)
13802 	{
13803 	  macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
13804 		       BFD_RELOC_LO16);
13805 	  break;
13806 	}
13807       used_at = 1;
13808       load_register (AT, &imm_expr, GPR_SIZE == 64);
13809       macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
13810       break;
13811 
13812     case M_SNE:
13813       if (op[1] == 0)
13814 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13815       else if (op[2] == 0)
13816 	macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13817       else
13818 	{
13819 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13820 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13821 	}
13822       break;
13823 
13824     case M_SNE_I:
13825       if (imm_expr.X_add_number == 0)
13826 	{
13827 	  macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
13828 	  break;
13829 	}
13830       if (op[1] == 0)
13831 	{
13832 	  as_warn (_("instruction %s: result is always true"),
13833 		   ip->insn_mo->name);
13834 	  macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
13835 		       op[0], 0, BFD_RELOC_LO16);
13836 	  break;
13837 	}
13838       if (CPU_HAS_SEQ (mips_opts.arch)
13839 	  && -512 <= imm_expr.X_add_number
13840 	  && imm_expr.X_add_number < 512)
13841 	{
13842 	  macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
13843 		       (int) imm_expr.X_add_number);
13844 	  break;
13845 	}
13846       if (imm_expr.X_add_number >= 0
13847 	  && imm_expr.X_add_number < 0x10000)
13848 	{
13849 	  macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13850 		       BFD_RELOC_LO16);
13851 	}
13852       else if (imm_expr.X_add_number > -0x8000
13853 	       && imm_expr.X_add_number < 0)
13854 	{
13855 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13856 	  macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
13857 		       "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13858 	}
13859       else if (CPU_HAS_SEQ (mips_opts.arch))
13860 	{
13861 	  used_at = 1;
13862 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13863 	  macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
13864 	  break;
13865 	}
13866       else
13867 	{
13868 	  load_register (AT, &imm_expr, GPR_SIZE == 64);
13869 	  macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
13870 	  used_at = 1;
13871 	}
13872       macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
13873       break;
13874 
13875     case M_SUB_I:
13876       s = "addi";
13877       s2 = "sub";
13878       if (ISA_IS_R6 (mips_opts.isa))
13879 	goto do_subi_i;
13880       else
13881 	goto do_subi;
13882     case M_SUBU_I:
13883       s = "addiu";
13884       s2 = "subu";
13885       goto do_subi;
13886     case M_DSUB_I:
13887       dbl = 1;
13888       s = "daddi";
13889       s2 = "dsub";
13890       if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
13891 	goto do_subi;
13892       if (imm_expr.X_add_number > -0x200
13893 	  && imm_expr.X_add_number <= 0x200
13894 	  && !ISA_IS_R6 (mips_opts.isa))
13895 	{
13896 	  macro_build (NULL, s, "t,r,.", op[0], op[1],
13897 		       (int) -imm_expr.X_add_number);
13898 	  break;
13899 	}
13900       goto do_subi_i;
13901     case M_DSUBU_I:
13902       dbl = 1;
13903       s = "daddiu";
13904       s2 = "dsubu";
13905     do_subi:
13906       if (imm_expr.X_add_number > -0x8000
13907 	  && imm_expr.X_add_number <= 0x8000)
13908 	{
13909 	  imm_expr.X_add_number = -imm_expr.X_add_number;
13910 	  macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
13911 	  break;
13912 	}
13913     do_subi_i:
13914       used_at = 1;
13915       load_register (AT, &imm_expr, dbl);
13916       macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
13917       break;
13918 
13919     case M_TEQ_I:
13920       s = "teq";
13921       goto trap;
13922     case M_TGE_I:
13923       s = "tge";
13924       goto trap;
13925     case M_TGEU_I:
13926       s = "tgeu";
13927       goto trap;
13928     case M_TLT_I:
13929       s = "tlt";
13930       goto trap;
13931     case M_TLTU_I:
13932       s = "tltu";
13933       goto trap;
13934     case M_TNE_I:
13935       s = "tne";
13936     trap:
13937       used_at = 1;
13938       load_register (AT, &imm_expr, GPR_SIZE == 64);
13939       macro_build (NULL, s, "s,t", op[0], AT);
13940       break;
13941 
13942     case M_TRUNCWS:
13943     case M_TRUNCWD:
13944       gas_assert (!mips_opts.micromips);
13945       gas_assert (mips_opts.isa == ISA_MIPS1);
13946       used_at = 1;
13947 
13948       /*
13949        * Is the double cfc1 instruction a bug in the mips assembler;
13950        * or is there a reason for it?
13951        */
13952       start_noreorder ();
13953       macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
13954       macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
13955       macro_build (NULL, "nop", "");
13956       expr1.X_add_number = 3;
13957       macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
13958       expr1.X_add_number = 2;
13959       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13960       macro_build (NULL, "ctc1", "t,g", AT, FCSR);
13961       macro_build (NULL, "nop", "");
13962       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
13963 		   op[0], op[1]);
13964       macro_build (NULL, "ctc1", "t,g", op[2], FCSR);
13965       macro_build (NULL, "nop", "");
13966       end_noreorder ();
13967       break;
13968 
13969     case M_ULH_AB:
13970       s = "lb";
13971       s2 = "lbu";
13972       off = 1;
13973       goto uld_st;
13974     case M_ULHU_AB:
13975       s = "lbu";
13976       s2 = "lbu";
13977       off = 1;
13978       goto uld_st;
13979     case M_ULW_AB:
13980       s = "lwl";
13981       s2 = "lwr";
13982       offbits = (mips_opts.micromips ? 12 : 16);
13983       off = 3;
13984       goto uld_st;
13985     case M_ULD_AB:
13986       s = "ldl";
13987       s2 = "ldr";
13988       offbits = (mips_opts.micromips ? 12 : 16);
13989       off = 7;
13990       goto uld_st;
13991     case M_USH_AB:
13992       s = "sb";
13993       s2 = "sb";
13994       off = 1;
13995       ust = 1;
13996       goto uld_st;
13997     case M_USW_AB:
13998       s = "swl";
13999       s2 = "swr";
14000       offbits = (mips_opts.micromips ? 12 : 16);
14001       off = 3;
14002       ust = 1;
14003       goto uld_st;
14004     case M_USD_AB:
14005       s = "sdl";
14006       s2 = "sdr";
14007       offbits = (mips_opts.micromips ? 12 : 16);
14008       off = 7;
14009       ust = 1;
14010 
14011     uld_st:
14012       breg = op[2];
14013       large_offset = !small_offset_p (off, align, offbits);
14014       ep = &offset_expr;
14015       expr1.X_add_number = 0;
14016       if (large_offset)
14017 	{
14018 	  used_at = 1;
14019 	  tempreg = AT;
14020 	  if (small_offset_p (0, align, 16))
14021 	    macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
14022 			 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
14023 	  else
14024 	    {
14025 	      load_address (tempreg, ep, &used_at);
14026 	      if (breg != 0)
14027 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
14028 			     tempreg, tempreg, breg);
14029 	    }
14030 	  offset_reloc[0] = BFD_RELOC_LO16;
14031 	  offset_reloc[1] = BFD_RELOC_UNUSED;
14032 	  offset_reloc[2] = BFD_RELOC_UNUSED;
14033 	  breg = tempreg;
14034 	  tempreg = op[0];
14035 	  ep = &expr1;
14036 	}
14037       else if (!ust && op[0] == breg)
14038 	{
14039 	  used_at = 1;
14040 	  tempreg = AT;
14041 	}
14042       else
14043 	tempreg = op[0];
14044 
14045       if (off == 1)
14046 	goto ulh_sh;
14047 
14048       if (!target_big_endian)
14049 	ep->X_add_number += off;
14050       if (offbits == 12)
14051 	macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
14052       else
14053 	macro_build (ep, s, "t,o(b)", tempreg, -1,
14054 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
14055 
14056       if (!target_big_endian)
14057 	ep->X_add_number -= off;
14058       else
14059 	ep->X_add_number += off;
14060       if (offbits == 12)
14061 	macro_build (NULL, s2, "t,~(b)",
14062 		     tempreg, (int) ep->X_add_number, breg);
14063       else
14064 	macro_build (ep, s2, "t,o(b)", tempreg, -1,
14065 		     offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
14066 
14067       /* If necessary, move the result in tempreg to the final destination.  */
14068       if (!ust && op[0] != tempreg)
14069         {
14070 	  /* Protect second load's delay slot.  */
14071 	  load_delay_nop ();
14072 	  move_register (op[0], tempreg);
14073 	}
14074       break;
14075 
14076     ulh_sh:
14077       used_at = 1;
14078       if (target_big_endian == ust)
14079 	ep->X_add_number += off;
14080       tempreg = ust || large_offset ? op[0] : AT;
14081       macro_build (ep, s, "t,o(b)", tempreg, -1,
14082 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
14083 
14084       /* For halfword transfers we need a temporary register to shuffle
14085          bytes.  Unfortunately for M_USH_A we have none available before
14086          the next store as AT holds the base address.  We deal with this
14087          case by clobbering TREG and then restoring it as with ULH.  */
14088       tempreg = ust == large_offset ? op[0] : AT;
14089       if (ust)
14090 	macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
14091 
14092       if (target_big_endian == ust)
14093 	ep->X_add_number -= off;
14094       else
14095 	ep->X_add_number += off;
14096       macro_build (ep, s2, "t,o(b)", tempreg, -1,
14097 		   offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
14098 
14099       /* For M_USH_A re-retrieve the LSB.  */
14100       if (ust && large_offset)
14101 	{
14102 	  if (target_big_endian)
14103 	    ep->X_add_number += off;
14104 	  else
14105 	    ep->X_add_number -= off;
14106 	  macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
14107 		       offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
14108 	}
14109       /* For ULH and M_USH_A OR the LSB in.  */
14110       if (!ust || large_offset)
14111 	{
14112 	  tempreg = !large_offset ? AT : op[0];
14113 	  macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
14114 	  macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
14115 	}
14116       break;
14117 
14118     default:
14119       /* FIXME: Check if this is one of the itbl macros, since they
14120 	 are added dynamically.  */
14121       as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
14122       break;
14123     }
14124   if (!mips_opts.at && used_at)
14125     as_bad (_("macro used $at after \".set noat\""));
14126 }
14127 
14128 /* Implement macros in mips16 mode.  */
14129 
14130 static void
14131 mips16_macro (struct mips_cl_insn *ip)
14132 {
14133   const struct mips_operand_array *operands;
14134   int mask;
14135   int tmp;
14136   expressionS expr1;
14137   int dbl;
14138   const char *s, *s2, *s3;
14139   unsigned int op[MAX_OPERANDS];
14140   unsigned int i;
14141 
14142   mask = ip->insn_mo->mask;
14143 
14144   operands = insn_operands (ip);
14145   for (i = 0; i < MAX_OPERANDS; i++)
14146     if (operands->operand[i])
14147       op[i] = insn_extract_operand (ip, operands->operand[i]);
14148     else
14149       op[i] = -1;
14150 
14151   expr1.X_op = O_constant;
14152   expr1.X_op_symbol = NULL;
14153   expr1.X_add_symbol = NULL;
14154   expr1.X_add_number = 1;
14155 
14156   dbl = 0;
14157 
14158   switch (mask)
14159     {
14160     default:
14161       abort ();
14162 
14163     case M_DDIV_3:
14164       dbl = 1;
14165       /* Fall through.  */
14166     case M_DIV_3:
14167       s = "mflo";
14168       goto do_div3;
14169     case M_DREM_3:
14170       dbl = 1;
14171       /* Fall through.  */
14172     case M_REM_3:
14173       s = "mfhi";
14174     do_div3:
14175       start_noreorder ();
14176       macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
14177       expr1.X_add_number = 2;
14178       macro_build (&expr1, "bnez", "x,p", op[2]);
14179       macro_build (NULL, "break", "6", 7);
14180 
14181       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14182          since that causes an overflow.  We should do that as well,
14183          but I don't see how to do the comparisons without a temporary
14184          register.  */
14185       end_noreorder ();
14186       macro_build (NULL, s, "x", op[0]);
14187       break;
14188 
14189     case M_DIVU_3:
14190       s = "divu";
14191       s2 = "mflo";
14192       goto do_divu3;
14193     case M_REMU_3:
14194       s = "divu";
14195       s2 = "mfhi";
14196       goto do_divu3;
14197     case M_DDIVU_3:
14198       s = "ddivu";
14199       s2 = "mflo";
14200       goto do_divu3;
14201     case M_DREMU_3:
14202       s = "ddivu";
14203       s2 = "mfhi";
14204     do_divu3:
14205       start_noreorder ();
14206       macro_build (NULL, s, ".,x,y", op[1], op[2]);
14207       expr1.X_add_number = 2;
14208       macro_build (&expr1, "bnez", "x,p", op[2]);
14209       macro_build (NULL, "break", "6", 7);
14210       end_noreorder ();
14211       macro_build (NULL, s2, "x", op[0]);
14212       break;
14213 
14214     case M_DMUL:
14215       dbl = 1;
14216       /* Fall through.  */
14217     case M_MUL:
14218       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14219       macro_build (NULL, "mflo", "x", op[0]);
14220       break;
14221 
14222     case M_DSUBU_I:
14223       dbl = 1;
14224       goto do_subu;
14225     case M_SUBU_I:
14226     do_subu:
14227       imm_expr.X_add_number = -imm_expr.X_add_number;
14228       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
14229       break;
14230 
14231     case M_SUBU_I_2:
14232       imm_expr.X_add_number = -imm_expr.X_add_number;
14233       macro_build (&imm_expr, "addiu", "x,k", op[0]);
14234       break;
14235 
14236     case M_DSUBU_I_2:
14237       imm_expr.X_add_number = -imm_expr.X_add_number;
14238       macro_build (&imm_expr, "daddiu", "y,j", op[0]);
14239       break;
14240 
14241     case M_BEQ:
14242       s = "cmp";
14243       s2 = "bteqz";
14244       goto do_branch;
14245     case M_BNE:
14246       s = "cmp";
14247       s2 = "btnez";
14248       goto do_branch;
14249     case M_BLT:
14250       s = "slt";
14251       s2 = "btnez";
14252       goto do_branch;
14253     case M_BLTU:
14254       s = "sltu";
14255       s2 = "btnez";
14256       goto do_branch;
14257     case M_BLE:
14258       s = "slt";
14259       s2 = "bteqz";
14260       goto do_reverse_branch;
14261     case M_BLEU:
14262       s = "sltu";
14263       s2 = "bteqz";
14264       goto do_reverse_branch;
14265     case M_BGE:
14266       s = "slt";
14267       s2 = "bteqz";
14268       goto do_branch;
14269     case M_BGEU:
14270       s = "sltu";
14271       s2 = "bteqz";
14272       goto do_branch;
14273     case M_BGT:
14274       s = "slt";
14275       s2 = "btnez";
14276       goto do_reverse_branch;
14277     case M_BGTU:
14278       s = "sltu";
14279       s2 = "btnez";
14280 
14281     do_reverse_branch:
14282       tmp = op[1];
14283       op[1] = op[0];
14284       op[0] = tmp;
14285 
14286     do_branch:
14287       macro_build (NULL, s, "x,y", op[0], op[1]);
14288       macro_build (&offset_expr, s2, "p");
14289       break;
14290 
14291     case M_BEQ_I:
14292       s = "cmpi";
14293       s2 = "bteqz";
14294       s3 = "x,U";
14295       goto do_branch_i;
14296     case M_BNE_I:
14297       s = "cmpi";
14298       s2 = "btnez";
14299       s3 = "x,U";
14300       goto do_branch_i;
14301     case M_BLT_I:
14302       s = "slti";
14303       s2 = "btnez";
14304       s3 = "x,8";
14305       goto do_branch_i;
14306     case M_BLTU_I:
14307       s = "sltiu";
14308       s2 = "btnez";
14309       s3 = "x,8";
14310       goto do_branch_i;
14311     case M_BLE_I:
14312       s = "slti";
14313       s2 = "btnez";
14314       s3 = "x,8";
14315       goto do_addone_branch_i;
14316     case M_BLEU_I:
14317       s = "sltiu";
14318       s2 = "btnez";
14319       s3 = "x,8";
14320       goto do_addone_branch_i;
14321     case M_BGE_I:
14322       s = "slti";
14323       s2 = "bteqz";
14324       s3 = "x,8";
14325       goto do_branch_i;
14326     case M_BGEU_I:
14327       s = "sltiu";
14328       s2 = "bteqz";
14329       s3 = "x,8";
14330       goto do_branch_i;
14331     case M_BGT_I:
14332       s = "slti";
14333       s2 = "bteqz";
14334       s3 = "x,8";
14335       goto do_addone_branch_i;
14336     case M_BGTU_I:
14337       s = "sltiu";
14338       s2 = "bteqz";
14339       s3 = "x,8";
14340 
14341     do_addone_branch_i:
14342       ++imm_expr.X_add_number;
14343 
14344     do_branch_i:
14345       macro_build (&imm_expr, s, s3, op[0]);
14346       macro_build (&offset_expr, s2, "p");
14347       break;
14348 
14349     case M_ABS:
14350       expr1.X_add_number = 0;
14351       macro_build (&expr1, "slti", "x,8", op[1]);
14352       if (op[0] != op[1])
14353 	macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
14354       expr1.X_add_number = 2;
14355       macro_build (&expr1, "bteqz", "p");
14356       macro_build (NULL, "neg", "x,w", op[0], op[0]);
14357       break;
14358     }
14359 }
14360 
14361 /* Look up instruction [START, START + LENGTH) in HASH.  Record any extra
14362    opcode bits in *OPCODE_EXTRA.  */
14363 
14364 static struct mips_opcode *
14365 mips_lookup_insn (htab_t hash, const char *start,
14366 		  ssize_t length, unsigned int *opcode_extra)
14367 {
14368   char *name, *dot, *p;
14369   unsigned int mask, suffix;
14370   ssize_t opend;
14371   struct mips_opcode *insn;
14372 
14373   /* Make a copy of the instruction so that we can fiddle with it.  */
14374   name = xstrndup (start, length);
14375 
14376   /* Look up the instruction as-is.  */
14377   insn = (struct mips_opcode *) str_hash_find (hash, name);
14378   if (insn)
14379     goto end;
14380 
14381   dot = strchr (name, '.');
14382   if (dot && dot[1])
14383     {
14384       /* Try to interpret the text after the dot as a VU0 channel suffix.  */
14385       p = mips_parse_vu0_channels (dot + 1, &mask);
14386       if (*p == 0 && mask != 0)
14387 	{
14388 	  *dot = 0;
14389 	  insn = (struct mips_opcode *) str_hash_find (hash, name);
14390 	  *dot = '.';
14391 	  if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14392 	    {
14393 	      *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
14394 	      goto end;
14395 	    }
14396 	}
14397     }
14398 
14399   if (mips_opts.micromips)
14400     {
14401       /* See if there's an instruction size override suffix,
14402 	 either `16' or `32', at the end of the mnemonic proper,
14403 	 that defines the operation, i.e. before the first `.'
14404 	 character if any.  Strip it and retry.  */
14405       opend = dot != NULL ? dot - name : length;
14406       if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14407 	suffix = 2;
14408       else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14409 	suffix = 4;
14410       else
14411 	suffix = 0;
14412       if (suffix)
14413 	{
14414 	  memmove (name + opend - 2, name + opend, length - opend + 1);
14415 	  insn = (struct mips_opcode *) str_hash_find (hash, name);
14416 	  if (insn)
14417 	    {
14418 	      forced_insn_length = suffix;
14419 	      goto end;
14420 	    }
14421 	}
14422     }
14423 
14424   insn = NULL;
14425  end:
14426   free (name);
14427   return insn;
14428 }
14429 
14430 /* Assemble an instruction into its binary format.  If the instruction
14431    is a macro, set imm_expr and offset_expr to the values associated
14432    with "I" and "A" operands respectively.  Otherwise store the value
14433    of the relocatable field (if any) in offset_expr.  In both cases
14434    set offset_reloc to the relocation operators applied to offset_expr.  */
14435 
14436 static void
14437 mips_ip (char *str, struct mips_cl_insn *insn)
14438 {
14439   const struct mips_opcode *first, *past;
14440   htab_t hash;
14441   char format;
14442   size_t end;
14443   struct mips_operand_token *tokens;
14444   unsigned int opcode_extra;
14445 
14446   if (mips_opts.micromips)
14447     {
14448       hash = micromips_op_hash;
14449       past = &micromips_opcodes[bfd_micromips_num_opcodes];
14450     }
14451   else
14452     {
14453       hash = op_hash;
14454       past = &mips_opcodes[NUMOPCODES];
14455     }
14456   forced_insn_length = 0;
14457   opcode_extra = 0;
14458 
14459   /* We first try to match an instruction up to a space or to the end.  */
14460   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14461     continue;
14462 
14463   first = mips_lookup_insn (hash, str, end, &opcode_extra);
14464   if (first == NULL)
14465     {
14466       set_insn_error (0, _("unrecognized opcode"));
14467       return;
14468     }
14469 
14470   if (strcmp (first->name, "li.s") == 0)
14471     format = 'f';
14472   else if (strcmp (first->name, "li.d") == 0)
14473     format = 'd';
14474   else
14475     format = 0;
14476   tokens = mips_parse_arguments (str + end, format);
14477   if (!tokens)
14478     return;
14479 
14480   if (!match_insns (insn, first, past, tokens, opcode_extra, false)
14481       && !match_insns (insn, first, past, tokens, opcode_extra, true))
14482     set_insn_error (0, _("invalid operands"));
14483 
14484   obstack_free (&mips_operand_tokens, tokens);
14485 }
14486 
14487 /* As for mips_ip, but used when assembling MIPS16 code.
14488    Also set forced_insn_length to the resulting instruction size in
14489    bytes if the user explicitly requested a small or extended instruction.  */
14490 
14491 static void
14492 mips16_ip (char *str, struct mips_cl_insn *insn)
14493 {
14494   char *end, *s, c;
14495   struct mips_opcode *first;
14496   struct mips_operand_token *tokens;
14497   unsigned int l;
14498 
14499   for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
14500     ;
14501   end = s;
14502   c = *end;
14503 
14504   l = 0;
14505   switch (c)
14506     {
14507     case '\0':
14508       break;
14509 
14510     case ' ':
14511       s++;
14512       break;
14513 
14514     case '.':
14515       s++;
14516       if (*s == 't')
14517 	{
14518 	  l = 2;
14519 	  s++;
14520 	}
14521       else if (*s == 'e')
14522 	{
14523 	  l = 4;
14524 	  s++;
14525 	}
14526       if (*s == '\0')
14527 	break;
14528       else if (*s++ == ' ')
14529 	break;
14530       set_insn_error (0, _("unrecognized opcode"));
14531       return;
14532     }
14533   forced_insn_length = l;
14534 
14535   *end = 0;
14536   first = (struct mips_opcode *) str_hash_find (mips16_op_hash, str);
14537   *end = c;
14538 
14539   if (!first)
14540     {
14541       set_insn_error (0, _("unrecognized opcode"));
14542       return;
14543     }
14544 
14545   tokens = mips_parse_arguments (s, 0);
14546   if (!tokens)
14547     return;
14548 
14549   if (!match_mips16_insns (insn, first, tokens))
14550     set_insn_error (0, _("invalid operands"));
14551 
14552   obstack_free (&mips_operand_tokens, tokens);
14553 }
14554 
14555 /* Marshal immediate value VAL for an extended MIPS16 instruction.
14556    NBITS is the number of significant bits in VAL.  */
14557 
14558 static unsigned long
14559 mips16_immed_extend (offsetT val, unsigned int nbits)
14560 {
14561   int extval;
14562 
14563   extval = 0;
14564   val &= (1U << nbits) - 1;
14565   if (nbits == 16 || nbits == 9)
14566     {
14567       extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14568       val &= 0x1f;
14569     }
14570   else if (nbits == 15)
14571     {
14572       extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14573       val &= 0xf;
14574     }
14575   else if (nbits == 6)
14576     {
14577       extval = ((val & 0x1f) << 6) | (val & 0x20);
14578       val = 0;
14579     }
14580   return (extval << 16) | val;
14581 }
14582 
14583 /* Like decode_mips16_operand, but require the operand to be defined and
14584    require it to be an integer.  */
14585 
14586 static const struct mips_int_operand *
14587 mips16_immed_operand (int type, bool extended_p)
14588 {
14589   const struct mips_operand *operand;
14590 
14591   operand = decode_mips16_operand (type, extended_p);
14592   if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14593     abort ();
14594   return (const struct mips_int_operand *) operand;
14595 }
14596 
14597 /* Return true if SVAL fits OPERAND.  RELOC is as for mips16_immed.  */
14598 
14599 static bool
14600 mips16_immed_in_range_p (const struct mips_int_operand *operand,
14601 			 bfd_reloc_code_real_type reloc, offsetT sval)
14602 {
14603   int min_val, max_val;
14604 
14605   min_val = mips_int_operand_min (operand);
14606   max_val = mips_int_operand_max (operand);
14607   if (reloc != BFD_RELOC_UNUSED)
14608     {
14609       if (min_val < 0)
14610 	sval = SEXT_16BIT (sval);
14611       else
14612 	sval &= 0xffff;
14613     }
14614 
14615   return (sval >= min_val
14616 	  && sval <= max_val
14617 	  && (sval & ((1 << operand->shift) - 1)) == 0);
14618 }
14619 
14620 /* Install immediate value VAL into MIPS16 instruction *INSN,
14621    extending it if necessary.  The instruction in *INSN may
14622    already be extended.
14623 
14624    RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14625    if none.  In the former case, VAL is a 16-bit number with no
14626    defined signedness.
14627 
14628    TYPE is the type of the immediate field.  USER_INSN_LENGTH
14629    is the length that the user requested, or 0 if none.  */
14630 
14631 static void
14632 mips16_immed (const char *file, unsigned int line, int type,
14633 	      bfd_reloc_code_real_type reloc, offsetT val,
14634 	      unsigned int user_insn_length, unsigned long *insn)
14635 {
14636   const struct mips_int_operand *operand;
14637   unsigned int uval, length;
14638 
14639   operand = mips16_immed_operand (type, false);
14640   if (!mips16_immed_in_range_p (operand, reloc, val))
14641     {
14642       /* We need an extended instruction.  */
14643       if (user_insn_length == 2)
14644 	as_bad_where (file, line, _("invalid unextended operand value"));
14645       else
14646 	*insn |= MIPS16_EXTEND;
14647     }
14648   else if (user_insn_length == 4)
14649     {
14650       /* The operand doesn't force an unextended instruction to be extended.
14651 	 Warn if the user wanted an extended instruction anyway.  */
14652       *insn |= MIPS16_EXTEND;
14653       as_warn_where (file, line,
14654 		     _("extended operand requested but not required"));
14655     }
14656 
14657   length = mips16_opcode_length (*insn);
14658   if (length == 4)
14659     {
14660       operand = mips16_immed_operand (type, true);
14661       if (!mips16_immed_in_range_p (operand, reloc, val))
14662 	as_bad_where (file, line,
14663 		      _("operand value out of range for instruction"));
14664     }
14665   uval = ((unsigned int) val >> operand->shift) - operand->bias;
14666   if (length == 2 || operand->root.lsb != 0)
14667     *insn = mips_insert_operand (&operand->root, *insn, uval);
14668   else
14669     *insn |= mips16_immed_extend (uval, operand->root.size);
14670 }
14671 
14672 struct percent_op_match
14673 {
14674   const char *str;
14675   bfd_reloc_code_real_type reloc;
14676 };
14677 
14678 static const struct percent_op_match mips_percent_op[] =
14679 {
14680   {"%lo", BFD_RELOC_LO16},
14681   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14682   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14683   {"%call16", BFD_RELOC_MIPS_CALL16},
14684   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14685   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14686   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14687   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14688   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14689   {"%got", BFD_RELOC_MIPS_GOT16},
14690   {"%gp_rel", BFD_RELOC_GPREL16},
14691   {"%gprel", BFD_RELOC_GPREL16},
14692   {"%half", BFD_RELOC_MIPS_16},
14693   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14694   {"%higher", BFD_RELOC_MIPS_HIGHER},
14695   {"%neg", BFD_RELOC_MIPS_SUB},
14696   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14697   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14698   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14699   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14700   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14701   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14702   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14703   {"%hi", BFD_RELOC_HI16_S},
14704   {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14705   {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
14706 };
14707 
14708 static const struct percent_op_match mips16_percent_op[] =
14709 {
14710   {"%lo", BFD_RELOC_MIPS16_LO16},
14711   {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
14712   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14713   {"%got", BFD_RELOC_MIPS16_GOT16},
14714   {"%call16", BFD_RELOC_MIPS16_CALL16},
14715   {"%hi", BFD_RELOC_MIPS16_HI16_S},
14716   {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14717   {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14718   {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14719   {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14720   {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14721   {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14722   {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
14723 };
14724 
14725 
14726 /* Return true if *STR points to a relocation operator.  When returning true,
14727    move *STR over the operator and store its relocation code in *RELOC.
14728    Leave both *STR and *RELOC alone when returning false.  */
14729 
14730 static bool
14731 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14732 {
14733   const struct percent_op_match *percent_op;
14734   size_t limit, i;
14735 
14736   if (mips_opts.mips16)
14737     {
14738       percent_op = mips16_percent_op;
14739       limit = ARRAY_SIZE (mips16_percent_op);
14740     }
14741   else
14742     {
14743       percent_op = mips_percent_op;
14744       limit = ARRAY_SIZE (mips_percent_op);
14745     }
14746 
14747   for (i = 0; i < limit; i++)
14748     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14749       {
14750 	int len = strlen (percent_op[i].str);
14751 
14752 	if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14753 	  continue;
14754 
14755 	*str += strlen (percent_op[i].str);
14756 	*reloc = percent_op[i].reloc;
14757 
14758 	/* Check whether the output BFD supports this relocation.
14759 	   If not, issue an error and fall back on something safe.  */
14760 	if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14761 	  {
14762 	    as_bad (_("relocation %s isn't supported by the current ABI"),
14763 		    percent_op[i].str);
14764 	    *reloc = BFD_RELOC_UNUSED;
14765 	  }
14766 	return true;
14767       }
14768   return false;
14769 }
14770 
14771 
14772 /* Parse string STR as a 16-bit relocatable operand.  Store the
14773    expression in *EP and the relocations in the array starting
14774    at RELOC.  Return the number of relocation operators used.
14775 
14776    On exit, EXPR_END points to the first character after the expression.  */
14777 
14778 static size_t
14779 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14780 		       char *str)
14781 {
14782   bfd_reloc_code_real_type reversed_reloc[3];
14783   size_t reloc_index, i;
14784   int crux_depth, str_depth;
14785   char *crux;
14786 
14787   /* Search for the start of the main expression, recoding relocations
14788      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14789      of the main expression and with CRUX_DEPTH containing the number
14790      of open brackets at that point.  */
14791   reloc_index = -1;
14792   str_depth = 0;
14793   do
14794     {
14795       reloc_index++;
14796       crux = str;
14797       crux_depth = str_depth;
14798 
14799       /* Skip over whitespace and brackets, keeping count of the number
14800 	 of brackets.  */
14801       while (*str == ' ' || *str == '\t' || *str == '(')
14802 	if (*str++ == '(')
14803 	  str_depth++;
14804     }
14805   while (*str == '%'
14806 	 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14807 	 && parse_relocation (&str, &reversed_reloc[reloc_index]));
14808 
14809   my_getExpression (ep, crux);
14810   str = expr_end;
14811 
14812   /* Match every open bracket.  */
14813   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14814     if (*str++ == ')')
14815       crux_depth--;
14816 
14817   if (crux_depth > 0)
14818     as_bad (_("unclosed '('"));
14819 
14820   expr_end = str;
14821 
14822   for (i = 0; i < reloc_index; i++)
14823     reloc[i] = reversed_reloc[reloc_index - 1 - i];
14824 
14825   return reloc_index;
14826 }
14827 
14828 static void
14829 my_getExpression (expressionS *ep, char *str)
14830 {
14831   char *save_in;
14832 
14833   save_in = input_line_pointer;
14834   input_line_pointer = str;
14835   expression (ep);
14836   expr_end = input_line_pointer;
14837   input_line_pointer = save_in;
14838 }
14839 
14840 const char *
14841 md_atof (int type, char *litP, int *sizeP)
14842 {
14843   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14844 }
14845 
14846 void
14847 md_number_to_chars (char *buf, valueT val, int n)
14848 {
14849   if (target_big_endian)
14850     number_to_chars_bigendian (buf, val, n);
14851   else
14852     number_to_chars_littleendian (buf, val, n);
14853 }
14854 
14855 static int support_64bit_objects(void)
14856 {
14857   const char **list, **l;
14858   int yes;
14859 
14860   list = bfd_target_list ();
14861   for (l = list; *l != NULL; l++)
14862     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14863 	|| strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14864       break;
14865   yes = (*l != NULL);
14866   free (list);
14867   return yes;
14868 }
14869 
14870 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14871    NEW_VALUE.  Warn if another value was already specified.  Note:
14872    we have to defer parsing the -march and -mtune arguments in order
14873    to handle 'from-abi' correctly, since the ABI might be specified
14874    in a later argument.  */
14875 
14876 static void
14877 mips_set_option_string (const char **string_ptr, const char *new_value)
14878 {
14879   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14880     as_warn (_("a different %s was already specified, is now %s"),
14881 	     string_ptr == &mips_arch_string ? "-march" : "-mtune",
14882 	     new_value);
14883 
14884   *string_ptr = new_value;
14885 }
14886 
14887 int
14888 md_parse_option (int c, const char *arg)
14889 {
14890   unsigned int i;
14891 
14892   for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14893     if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14894       {
14895 	file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
14896 					   c == mips_ases[i].option_on);
14897 	return 1;
14898       }
14899 
14900   switch (c)
14901     {
14902     case OPTION_CONSTRUCT_FLOATS:
14903       mips_disable_float_construction = 0;
14904       break;
14905 
14906     case OPTION_NO_CONSTRUCT_FLOATS:
14907       mips_disable_float_construction = 1;
14908       break;
14909 
14910     case OPTION_TRAP:
14911       mips_trap = 1;
14912       break;
14913 
14914     case OPTION_BREAK:
14915       mips_trap = 0;
14916       break;
14917 
14918     case OPTION_EB:
14919       target_big_endian = 1;
14920       break;
14921 
14922     case OPTION_EL:
14923       target_big_endian = 0;
14924       break;
14925 
14926     case 'O':
14927       if (arg == NULL)
14928 	mips_optimize = 1;
14929       else if (arg[0] == '0')
14930 	mips_optimize = 0;
14931       else if (arg[0] == '1')
14932 	mips_optimize = 1;
14933       else
14934 	mips_optimize = 2;
14935       break;
14936 
14937     case 'g':
14938       if (arg == NULL)
14939 	mips_debug = 2;
14940       else
14941 	mips_debug = atoi (arg);
14942       break;
14943 
14944     case OPTION_MIPS1:
14945       file_mips_opts.isa = ISA_MIPS1;
14946       break;
14947 
14948     case OPTION_MIPS2:
14949       file_mips_opts.isa = ISA_MIPS2;
14950       break;
14951 
14952     case OPTION_MIPS3:
14953       file_mips_opts.isa = ISA_MIPS3;
14954       break;
14955 
14956     case OPTION_MIPS4:
14957       file_mips_opts.isa = ISA_MIPS4;
14958       break;
14959 
14960     case OPTION_MIPS5:
14961       file_mips_opts.isa = ISA_MIPS5;
14962       break;
14963 
14964     case OPTION_MIPS32:
14965       file_mips_opts.isa = ISA_MIPS32;
14966       break;
14967 
14968     case OPTION_MIPS32R2:
14969       file_mips_opts.isa = ISA_MIPS32R2;
14970       break;
14971 
14972     case OPTION_MIPS32R3:
14973       file_mips_opts.isa = ISA_MIPS32R3;
14974       break;
14975 
14976     case OPTION_MIPS32R5:
14977       file_mips_opts.isa = ISA_MIPS32R5;
14978       break;
14979 
14980     case OPTION_MIPS32R6:
14981       file_mips_opts.isa = ISA_MIPS32R6;
14982       break;
14983 
14984     case OPTION_MIPS64R2:
14985       file_mips_opts.isa = ISA_MIPS64R2;
14986       break;
14987 
14988     case OPTION_MIPS64R3:
14989       file_mips_opts.isa = ISA_MIPS64R3;
14990       break;
14991 
14992     case OPTION_MIPS64R5:
14993       file_mips_opts.isa = ISA_MIPS64R5;
14994       break;
14995 
14996     case OPTION_MIPS64R6:
14997       file_mips_opts.isa = ISA_MIPS64R6;
14998       break;
14999 
15000     case OPTION_MIPS64:
15001       file_mips_opts.isa = ISA_MIPS64;
15002       break;
15003 
15004     case OPTION_MTUNE:
15005       mips_set_option_string (&mips_tune_string, arg);
15006       break;
15007 
15008     case OPTION_MARCH:
15009       mips_set_option_string (&mips_arch_string, arg);
15010       break;
15011 
15012     case OPTION_M4650:
15013       mips_set_option_string (&mips_arch_string, "4650");
15014       mips_set_option_string (&mips_tune_string, "4650");
15015       break;
15016 
15017     case OPTION_NO_M4650:
15018       break;
15019 
15020     case OPTION_M4010:
15021       mips_set_option_string (&mips_arch_string, "4010");
15022       mips_set_option_string (&mips_tune_string, "4010");
15023       break;
15024 
15025     case OPTION_NO_M4010:
15026       break;
15027 
15028     case OPTION_M4100:
15029       mips_set_option_string (&mips_arch_string, "4100");
15030       mips_set_option_string (&mips_tune_string, "4100");
15031       break;
15032 
15033     case OPTION_NO_M4100:
15034       break;
15035 
15036     case OPTION_M3900:
15037       mips_set_option_string (&mips_arch_string, "3900");
15038       mips_set_option_string (&mips_tune_string, "3900");
15039       break;
15040 
15041     case OPTION_NO_M3900:
15042       break;
15043 
15044     case OPTION_MICROMIPS:
15045       if (file_mips_opts.mips16 == 1)
15046 	{
15047 	  as_bad (_("-mmicromips cannot be used with -mips16"));
15048 	  return 0;
15049 	}
15050       file_mips_opts.micromips = 1;
15051       mips_no_prev_insn ();
15052       break;
15053 
15054     case OPTION_NO_MICROMIPS:
15055       file_mips_opts.micromips = 0;
15056       mips_no_prev_insn ();
15057       break;
15058 
15059     case OPTION_MIPS16:
15060       if (file_mips_opts.micromips == 1)
15061 	{
15062 	  as_bad (_("-mips16 cannot be used with -micromips"));
15063 	  return 0;
15064 	}
15065       file_mips_opts.mips16 = 1;
15066       mips_no_prev_insn ();
15067       break;
15068 
15069     case OPTION_NO_MIPS16:
15070       file_mips_opts.mips16 = 0;
15071       mips_no_prev_insn ();
15072       break;
15073 
15074     case OPTION_FIX_24K:
15075       mips_fix_24k = 1;
15076       break;
15077 
15078     case OPTION_NO_FIX_24K:
15079       mips_fix_24k = 0;
15080       break;
15081 
15082     case OPTION_FIX_RM7000:
15083       mips_fix_rm7000 = 1;
15084       break;
15085 
15086     case OPTION_NO_FIX_RM7000:
15087       mips_fix_rm7000 = 0;
15088       break;
15089 
15090     case OPTION_FIX_LOONGSON3_LLSC:
15091       mips_fix_loongson3_llsc = true;
15092       break;
15093 
15094     case OPTION_NO_FIX_LOONGSON3_LLSC:
15095       mips_fix_loongson3_llsc = false;
15096       break;
15097 
15098     case OPTION_FIX_LOONGSON2F_JUMP:
15099       mips_fix_loongson2f_jump = true;
15100       break;
15101 
15102     case OPTION_NO_FIX_LOONGSON2F_JUMP:
15103       mips_fix_loongson2f_jump = false;
15104       break;
15105 
15106     case OPTION_FIX_LOONGSON2F_NOP:
15107       mips_fix_loongson2f_nop = true;
15108       break;
15109 
15110     case OPTION_NO_FIX_LOONGSON2F_NOP:
15111       mips_fix_loongson2f_nop = false;
15112       break;
15113 
15114     case OPTION_FIX_VR4120:
15115       mips_fix_vr4120 = 1;
15116       break;
15117 
15118     case OPTION_NO_FIX_VR4120:
15119       mips_fix_vr4120 = 0;
15120       break;
15121 
15122     case OPTION_FIX_VR4130:
15123       mips_fix_vr4130 = 1;
15124       break;
15125 
15126     case OPTION_NO_FIX_VR4130:
15127       mips_fix_vr4130 = 0;
15128       break;
15129 
15130     case OPTION_FIX_LOONGSON2F_BTB:
15131       mips_fix_loongson2f_btb = 1;
15132       break;
15133 
15134     case OPTION_NO_FIX_LOONGSON2F_BTB:
15135       mips_fix_loongson2f_btb = 0;
15136       break;
15137 
15138     case OPTION_FIX_CN63XXP1:
15139       mips_fix_cn63xxp1 = true;
15140       break;
15141 
15142     case OPTION_NO_FIX_CN63XXP1:
15143       mips_fix_cn63xxp1 = false;
15144       break;
15145 
15146     case OPTION_FIX_R5900:
15147       mips_fix_r5900 = true;
15148       mips_fix_r5900_explicit = true;
15149       break;
15150 
15151     case OPTION_NO_FIX_R5900:
15152       mips_fix_r5900 = false;
15153       mips_fix_r5900_explicit = true;
15154       break;
15155 
15156     case OPTION_RELAX_BRANCH:
15157       mips_relax_branch = 1;
15158       break;
15159 
15160     case OPTION_NO_RELAX_BRANCH:
15161       mips_relax_branch = 0;
15162       break;
15163 
15164     case OPTION_IGNORE_BRANCH_ISA:
15165       mips_ignore_branch_isa = true;
15166       break;
15167 
15168     case OPTION_NO_IGNORE_BRANCH_ISA:
15169       mips_ignore_branch_isa = false;
15170       break;
15171 
15172     case OPTION_INSN32:
15173       file_mips_opts.insn32 = true;
15174       break;
15175 
15176     case OPTION_NO_INSN32:
15177       file_mips_opts.insn32 = false;
15178       break;
15179 
15180     case OPTION_MSHARED:
15181       mips_in_shared = true;
15182       break;
15183 
15184     case OPTION_MNO_SHARED:
15185       mips_in_shared = false;
15186       break;
15187 
15188     case OPTION_MSYM32:
15189       file_mips_opts.sym32 = true;
15190       break;
15191 
15192     case OPTION_MNO_SYM32:
15193       file_mips_opts.sym32 = false;
15194       break;
15195 
15196       /* When generating ELF code, we permit -KPIC and -call_shared to
15197 	 select SVR4_PIC, and -non_shared to select no PIC.  This is
15198 	 intended to be compatible with Irix 5.  */
15199     case OPTION_CALL_SHARED:
15200       mips_pic = SVR4_PIC;
15201       mips_abicalls = true;
15202       break;
15203 
15204     case OPTION_CALL_NONPIC:
15205       mips_pic = NO_PIC;
15206       mips_abicalls = true;
15207       break;
15208 
15209     case OPTION_NON_SHARED:
15210       mips_pic = NO_PIC;
15211       mips_abicalls = false;
15212       break;
15213 
15214       /* The -xgot option tells the assembler to use 32 bit offsets
15215          when accessing the got in SVR4_PIC mode.  It is for Irix
15216          compatibility.  */
15217     case OPTION_XGOT:
15218       mips_big_got = 1;
15219       break;
15220 
15221     case 'G':
15222       g_switch_value = atoi (arg);
15223       g_switch_seen = 1;
15224       break;
15225 
15226       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15227 	 and -mabi=64.  */
15228     case OPTION_32:
15229       mips_abi = O32_ABI;
15230       break;
15231 
15232     case OPTION_N32:
15233       mips_abi = N32_ABI;
15234       break;
15235 
15236     case OPTION_64:
15237       mips_abi = N64_ABI;
15238       if (!support_64bit_objects())
15239 	as_fatal (_("no compiled in support for 64 bit object file format"));
15240       break;
15241 
15242     case OPTION_GP32:
15243       file_mips_opts.gp = 32;
15244       break;
15245 
15246     case OPTION_GP64:
15247       file_mips_opts.gp = 64;
15248       break;
15249 
15250     case OPTION_FP32:
15251       file_mips_opts.fp = 32;
15252       break;
15253 
15254     case OPTION_FPXX:
15255       file_mips_opts.fp = 0;
15256       break;
15257 
15258     case OPTION_FP64:
15259       file_mips_opts.fp = 64;
15260       break;
15261 
15262     case OPTION_ODD_SPREG:
15263       file_mips_opts.oddspreg = 1;
15264       break;
15265 
15266     case OPTION_NO_ODD_SPREG:
15267       file_mips_opts.oddspreg = 0;
15268       break;
15269 
15270     case OPTION_SINGLE_FLOAT:
15271       file_mips_opts.single_float = 1;
15272       break;
15273 
15274     case OPTION_DOUBLE_FLOAT:
15275       file_mips_opts.single_float = 0;
15276       break;
15277 
15278     case OPTION_SOFT_FLOAT:
15279       file_mips_opts.soft_float = 1;
15280       break;
15281 
15282     case OPTION_HARD_FLOAT:
15283       file_mips_opts.soft_float = 0;
15284       break;
15285 
15286     case OPTION_MABI:
15287       if (strcmp (arg, "32") == 0)
15288 	mips_abi = O32_ABI;
15289       else if (strcmp (arg, "o64") == 0)
15290 	mips_abi = O64_ABI;
15291       else if (strcmp (arg, "n32") == 0)
15292 	mips_abi = N32_ABI;
15293       else if (strcmp (arg, "64") == 0)
15294 	{
15295 	  mips_abi = N64_ABI;
15296 	  if (! support_64bit_objects())
15297 	    as_fatal (_("no compiled in support for 64 bit object file "
15298 			"format"));
15299 	}
15300       else if (strcmp (arg, "eabi") == 0)
15301 	mips_abi = EABI_ABI;
15302       else
15303 	{
15304 	  as_fatal (_("invalid abi -mabi=%s"), arg);
15305 	  return 0;
15306 	}
15307       break;
15308 
15309     case OPTION_M7000_HILO_FIX:
15310       mips_7000_hilo_fix = true;
15311       break;
15312 
15313     case OPTION_MNO_7000_HILO_FIX:
15314       mips_7000_hilo_fix = false;
15315       break;
15316 
15317     case OPTION_MDEBUG:
15318       mips_flag_mdebug = true;
15319       break;
15320 
15321     case OPTION_NO_MDEBUG:
15322       mips_flag_mdebug = false;
15323       break;
15324 
15325     case OPTION_PDR:
15326       mips_flag_pdr = true;
15327       break;
15328 
15329     case OPTION_NO_PDR:
15330       mips_flag_pdr = false;
15331       break;
15332 
15333     case OPTION_MVXWORKS_PIC:
15334       mips_pic = VXWORKS_PIC;
15335       break;
15336 
15337     case OPTION_NAN:
15338       if (strcmp (arg, "2008") == 0)
15339 	mips_nan2008 = 1;
15340       else if (strcmp (arg, "legacy") == 0)
15341 	mips_nan2008 = 0;
15342       else
15343 	{
15344 	  as_fatal (_("invalid NaN setting -mnan=%s"), arg);
15345 	  return 0;
15346 	}
15347       break;
15348 
15349     default:
15350       return 0;
15351     }
15352 
15353     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15354 
15355   return 1;
15356 }
15357 
15358 /* Set up globals to tune for the ISA or processor described by INFO.  */
15359 
15360 static void
15361 mips_set_tune (const struct mips_cpu_info *info)
15362 {
15363   if (info != 0)
15364     mips_tune = info->cpu;
15365 }
15366 
15367 
15368 void
15369 mips_after_parse_args (void)
15370 {
15371   const struct mips_cpu_info *arch_info = 0;
15372   const struct mips_cpu_info *tune_info = 0;
15373 
15374   /* GP relative stuff not working for PE.  */
15375   if (startswith (TARGET_OS, "pe"))
15376     {
15377       if (g_switch_seen && g_switch_value != 0)
15378 	as_bad (_("-G not supported in this configuration"));
15379       g_switch_value = 0;
15380     }
15381 
15382   if (mips_abi == NO_ABI)
15383     mips_abi = MIPS_DEFAULT_ABI;
15384 
15385   /* The following code determines the architecture.
15386      Similar code was added to GCC 3.3 (see override_options() in
15387      config/mips/mips.c).  The GAS and GCC code should be kept in sync
15388      as much as possible.  */
15389 
15390   if (mips_arch_string != 0)
15391     arch_info = mips_parse_cpu ("-march", mips_arch_string);
15392 
15393   if (file_mips_opts.isa != ISA_UNKNOWN)
15394     {
15395       /* Handle -mipsN.  At this point, file_mips_opts.isa contains the
15396 	 ISA level specified by -mipsN, while arch_info->isa contains
15397 	 the -march selection (if any).  */
15398       if (arch_info != 0)
15399 	{
15400 	  /* -march takes precedence over -mipsN, since it is more descriptive.
15401 	     There's no harm in specifying both as long as the ISA levels
15402 	     are the same.  */
15403 	  if (file_mips_opts.isa != arch_info->isa)
15404 	    as_bad (_("-%s conflicts with the other architecture options,"
15405 		      " which imply -%s"),
15406 		    mips_cpu_info_from_isa (file_mips_opts.isa)->name,
15407 		    mips_cpu_info_from_isa (arch_info->isa)->name);
15408 	}
15409       else
15410 	arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
15411     }
15412 
15413   if (arch_info == 0)
15414     {
15415       arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15416       gas_assert (arch_info);
15417     }
15418 
15419   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
15420     as_bad (_("-march=%s is not compatible with the selected ABI"),
15421 	    arch_info->name);
15422 
15423   file_mips_opts.arch = arch_info->cpu;
15424   file_mips_opts.isa = arch_info->isa;
15425   file_mips_opts.init_ase = arch_info->ase;
15426 
15427   /* The EVA Extension has instructions which are only valid when the R6 ISA
15428      is enabled.  This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15429      present.  */
15430   if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15431     file_mips_opts.ase |= ASE_EVA_R6;
15432 
15433   /* Set up initial mips_opts state.  */
15434   mips_opts = file_mips_opts;
15435 
15436   /* For the R5900 default to `-mfix-r5900' unless the user told otherwise.  */
15437   if (!mips_fix_r5900_explicit)
15438     mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15439 
15440   /* The register size inference code is now placed in
15441      file_mips_check_options.  */
15442 
15443   /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15444      processor.  */
15445   if (mips_tune_string != 0)
15446     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
15447 
15448   if (tune_info == 0)
15449     mips_set_tune (arch_info);
15450   else
15451     mips_set_tune (tune_info);
15452 
15453   if (mips_flag_mdebug < 0)
15454     mips_flag_mdebug = 0;
15455 }
15456 
15457 void
15458 mips_init_after_args (void)
15459 {
15460   /* Initialize opcodes.  */
15461   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15462   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15463 }
15464 
15465 long
15466 md_pcrel_from (fixS *fixP)
15467 {
15468   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15469 
15470   switch (fixP->fx_r_type)
15471     {
15472     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15473     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15474       /* Return the address of the delay slot.  */
15475       return addr + 2;
15476 
15477     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15478     case BFD_RELOC_MICROMIPS_JMP:
15479     case BFD_RELOC_MIPS16_16_PCREL_S1:
15480     case BFD_RELOC_16_PCREL_S2:
15481     case BFD_RELOC_MIPS_21_PCREL_S2:
15482     case BFD_RELOC_MIPS_26_PCREL_S2:
15483     case BFD_RELOC_MIPS_JMP:
15484       /* Return the address of the delay slot.  */
15485       return addr + 4;
15486 
15487     case BFD_RELOC_MIPS_18_PCREL_S3:
15488       /* Return the aligned address of the doubleword containing
15489          the instruction.  */
15490       return addr & ~7;
15491 
15492     default:
15493       return addr;
15494     }
15495 }
15496 
15497 /* This is called before the symbol table is processed.  In order to
15498    work with gcc when using mips-tfile, we must keep all local labels.
15499    However, in other cases, we want to discard them.  If we were
15500    called with -g, but we didn't see any debugging information, it may
15501    mean that gcc is smuggling debugging information through to
15502    mips-tfile, in which case we must generate all local labels.  */
15503 
15504 void
15505 mips_frob_file_before_adjust (void)
15506 {
15507 #ifndef NO_ECOFF_DEBUGGING
15508   if (ECOFF_DEBUGGING
15509       && mips_debug != 0
15510       && ! ecoff_debugging_seen)
15511     flag_keep_locals = 1;
15512 #endif
15513 }
15514 
15515 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15516    the corresponding LO16 reloc.  This is called before md_apply_fix and
15517    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15518    relocation operators.
15519 
15520    For our purposes, a %lo() expression matches a %got() or %hi()
15521    expression if:
15522 
15523       (a) it refers to the same symbol; and
15524       (b) the offset applied in the %lo() expression is no lower than
15525 	  the offset applied in the %got() or %hi().
15526 
15527    (b) allows us to cope with code like:
15528 
15529 	lui	$4,%hi(foo)
15530 	lh	$4,%lo(foo+2)($4)
15531 
15532    ...which is legal on RELA targets, and has a well-defined behaviour
15533    if the user knows that adding 2 to "foo" will not induce a carry to
15534    the high 16 bits.
15535 
15536    When several %lo()s match a particular %got() or %hi(), we use the
15537    following rules to distinguish them:
15538 
15539      (1) %lo()s with smaller offsets are a better match than %lo()s with
15540          higher offsets.
15541 
15542      (2) %lo()s with no matching %got() or %hi() are better than those
15543          that already have a matching %got() or %hi().
15544 
15545      (3) later %lo()s are better than earlier %lo()s.
15546 
15547    These rules are applied in order.
15548 
15549    (1) means, among other things, that %lo()s with identical offsets are
15550    chosen if they exist.
15551 
15552    (2) means that we won't associate several high-part relocations with
15553    the same low-part relocation unless there's no alternative.  Having
15554    several high parts for the same low part is a GNU extension; this rule
15555    allows careful users to avoid it.
15556 
15557    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15558    with the last high-part relocation being at the front of the list.
15559    It therefore makes sense to choose the last matching low-part
15560    relocation, all other things being equal.  It's also easier
15561    to code that way.  */
15562 
15563 void
15564 mips_frob_file (void)
15565 {
15566   struct mips_hi_fixup *l;
15567   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15568 
15569   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15570     {
15571       segment_info_type *seginfo;
15572       bool matched_lo_p;
15573       fixS **hi_pos, **lo_pos, **pos;
15574 
15575       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15576 
15577       /* If a GOT16 relocation turns out to be against a global symbol,
15578 	 there isn't supposed to be a matching LO.  Ignore %gots against
15579 	 constants; we'll report an error for those later.  */
15580       if (got16_reloc_p (l->fixp->fx_r_type)
15581 	  && !(l->fixp->fx_addsy
15582 	       && pic_need_relax (l->fixp->fx_addsy)))
15583 	continue;
15584 
15585       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15586       if (fixup_has_matching_lo_p (l->fixp))
15587 	continue;
15588 
15589       seginfo = seg_info (l->seg);
15590 
15591       /* Set HI_POS to the position of this relocation in the chain.
15592 	 Set LO_POS to the position of the chosen low-part relocation.
15593 	 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15594 	 relocation that matches an immediately-preceding high-part
15595 	 relocation.  */
15596       hi_pos = NULL;
15597       lo_pos = NULL;
15598       matched_lo_p = false;
15599       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15600 
15601       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15602 	{
15603 	  if (*pos == l->fixp)
15604 	    hi_pos = pos;
15605 
15606 	  if ((*pos)->fx_r_type == looking_for_rtype
15607 	      && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15608 	      && (*pos)->fx_offset >= l->fixp->fx_offset
15609 	      && (lo_pos == NULL
15610 		  || (*pos)->fx_offset < (*lo_pos)->fx_offset
15611 		  || (!matched_lo_p
15612 		      && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15613 	    lo_pos = pos;
15614 
15615 	  matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15616 			  && fixup_has_matching_lo_p (*pos));
15617 	}
15618 
15619       /* If we found a match, remove the high-part relocation from its
15620 	 current position and insert it before the low-part relocation.
15621 	 Make the offsets match so that fixup_has_matching_lo_p()
15622 	 will return true.
15623 
15624 	 We don't warn about unmatched high-part relocations since some
15625 	 versions of gcc have been known to emit dead "lui ...%hi(...)"
15626 	 instructions.  */
15627       if (lo_pos != NULL)
15628 	{
15629 	  l->fixp->fx_offset = (*lo_pos)->fx_offset;
15630 	  if (l->fixp->fx_next != *lo_pos)
15631 	    {
15632 	      *hi_pos = l->fixp->fx_next;
15633 	      l->fixp->fx_next = *lo_pos;
15634 	      *lo_pos = l->fixp;
15635 	    }
15636 	}
15637     }
15638 }
15639 
15640 int
15641 mips_force_relocation (fixS *fixp)
15642 {
15643   if (generic_force_reloc (fixp))
15644     return 1;
15645 
15646   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15647      so that the linker relaxation can update targets.  */
15648   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15649       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15650       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15651     return 1;
15652 
15653   /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15654      and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15655      microMIPS symbols so that we can do cross-mode branch diagnostics
15656      and BAL to JALX conversion by the linker.  */
15657   if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15658        || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15659        || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15660       && fixp->fx_addsy
15661       && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15662     return 1;
15663 
15664   /* We want all PC-relative relocations to be kept for R6 relaxation.  */
15665   if (ISA_IS_R6 (file_mips_opts.isa)
15666       && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15667 	  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15668 	  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15669 	  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15670 	  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15671 	  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15672 	  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15673     return 1;
15674 
15675   return 0;
15676 }
15677 
15678 /* Implement TC_FORCE_RELOCATION_ABS.  */
15679 
15680 bool
15681 mips_force_relocation_abs (fixS *fixp)
15682 {
15683   if (generic_force_reloc (fixp))
15684     return true;
15685 
15686   /* These relocations do not have enough bits in the in-place addend
15687      to hold an arbitrary absolute section's offset.  */
15688   if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15689     return true;
15690 
15691   return false;
15692 }
15693 
15694 /* Read the instruction associated with RELOC from BUF.  */
15695 
15696 static unsigned int
15697 read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15698 {
15699   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15700     return read_compressed_insn (buf, 4);
15701   else
15702     return read_insn (buf);
15703 }
15704 
15705 /* Write instruction INSN to BUF, given that it has been relocated
15706    by RELOC.  */
15707 
15708 static void
15709 write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15710 		  unsigned long insn)
15711 {
15712   if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15713     write_compressed_insn (buf, insn, 4);
15714   else
15715     write_insn (buf, insn);
15716 }
15717 
15718 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15719    to a symbol in another ISA mode, which cannot be converted to JALX.  */
15720 
15721 static bool
15722 fix_bad_cross_mode_jump_p (fixS *fixP)
15723 {
15724   unsigned long opcode;
15725   int other;
15726   char *buf;
15727 
15728   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15729     return false;
15730 
15731   other = S_GET_OTHER (fixP->fx_addsy);
15732   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15733   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15734   switch (fixP->fx_r_type)
15735     {
15736     case BFD_RELOC_MIPS_JMP:
15737       return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15738     case BFD_RELOC_MICROMIPS_JMP:
15739       return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15740     default:
15741       return false;
15742     }
15743 }
15744 
15745 /* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15746    jump to a symbol in the same ISA mode.  */
15747 
15748 static bool
15749 fix_bad_same_mode_jalx_p (fixS *fixP)
15750 {
15751   unsigned long opcode;
15752   int other;
15753   char *buf;
15754 
15755   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15756     return false;
15757 
15758   other = S_GET_OTHER (fixP->fx_addsy);
15759   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15760   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15761   switch (fixP->fx_r_type)
15762     {
15763     case BFD_RELOC_MIPS_JMP:
15764       return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15765     case BFD_RELOC_MIPS16_JMP:
15766       return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15767     case BFD_RELOC_MICROMIPS_JMP:
15768       return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15769     default:
15770       return false;
15771     }
15772 }
15773 
15774 /* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15775    to a symbol whose value plus addend is not aligned according to the
15776    ultimate (after linker relaxation) jump instruction's immediate field
15777    requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15778    regular MIPS code, to (1 << 2).  */
15779 
15780 static bool
15781 fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15782 {
15783   bool micro_to_mips_p;
15784   valueT val;
15785   int other;
15786 
15787   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15788     return false;
15789 
15790   other = S_GET_OTHER (fixP->fx_addsy);
15791   val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15792   val += fixP->fx_offset;
15793   micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15794 		     && !ELF_ST_IS_MICROMIPS (other));
15795   return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15796 	  != ELF_ST_IS_COMPRESSED (other));
15797 }
15798 
15799 /* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15800    to a symbol whose annotation indicates another ISA mode.  For absolute
15801    symbols check the ISA bit instead.
15802 
15803    We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15804    symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15805    MIPS symbols and associated with BAL instructions as these instructions
15806    may be converted to JALX by the linker.  */
15807 
15808 static bool
15809 fix_bad_cross_mode_branch_p (fixS *fixP)
15810 {
15811   bool absolute_p;
15812   unsigned long opcode;
15813   asection *symsec;
15814   valueT val;
15815   int other;
15816   char *buf;
15817 
15818   if (mips_ignore_branch_isa)
15819     return false;
15820 
15821   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15822     return false;
15823 
15824   symsec = S_GET_SEGMENT (fixP->fx_addsy);
15825   absolute_p = bfd_is_abs_section (symsec);
15826 
15827   val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15828   other = S_GET_OTHER (fixP->fx_addsy);
15829 
15830   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15831   opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15832   switch (fixP->fx_r_type)
15833     {
15834     case BFD_RELOC_16_PCREL_S2:
15835       return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15836 	      && opcode != 0x0411);
15837     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15838       return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15839 	      && opcode != 0x4060);
15840     case BFD_RELOC_MIPS_21_PCREL_S2:
15841     case BFD_RELOC_MIPS_26_PCREL_S2:
15842       return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15843     case BFD_RELOC_MIPS16_16_PCREL_S1:
15844       return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15845     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15846     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15847       return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15848     default:
15849       abort ();
15850     }
15851 }
15852 
15853 /* Return TRUE if the symbol plus addend associated with a regular MIPS
15854    branch instruction pointed to by FIXP is not aligned according to the
15855    branch instruction's immediate field requirement.  We need the addend
15856    to preserve the ISA bit and also the sum must not have bit 2 set.  We
15857    must explicitly OR in the ISA bit from symbol annotation as the bit
15858    won't be set in the symbol's value then.  */
15859 
15860 static bool
15861 fix_bad_misaligned_branch_p (fixS *fixP)
15862 {
15863   bool absolute_p;
15864   asection *symsec;
15865   valueT isa_bit;
15866   valueT val;
15867   valueT off;
15868   int other;
15869 
15870   if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15871     return false;
15872 
15873   symsec = S_GET_SEGMENT (fixP->fx_addsy);
15874   absolute_p = bfd_is_abs_section (symsec);
15875 
15876   val = S_GET_VALUE (fixP->fx_addsy);
15877   other = S_GET_OTHER (fixP->fx_addsy);
15878   off = fixP->fx_offset;
15879 
15880   isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15881   val |= ELF_ST_IS_COMPRESSED (other);
15882   val += off;
15883   return (val & 0x3) != isa_bit;
15884 }
15885 
15886 /* Calculate the relocation target by masking off ISA mode bit before
15887    combining symbol and addend.  */
15888 
15889 static valueT
15890 fix_bad_misaligned_address (fixS *fixP)
15891 {
15892   valueT val;
15893   valueT off;
15894   unsigned isa_mode;
15895   gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15896   val = S_GET_VALUE (fixP->fx_addsy);
15897   off = fixP->fx_offset;
15898   isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15899 	      ? 1 : 0);
15900 
15901   return ((val & ~isa_mode) + off);
15902 }
15903 
15904 /* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15905    and its calculated value VAL.  */
15906 
15907 static void
15908 fix_validate_branch (fixS *fixP, valueT val)
15909 {
15910   if (fixP->fx_done && (val & 0x3) != 0)
15911     as_bad_where (fixP->fx_file, fixP->fx_line,
15912 		  _("branch to misaligned address (0x%lx)"),
15913 		  (long) (val + md_pcrel_from (fixP)));
15914   else if (fix_bad_cross_mode_branch_p (fixP))
15915     as_bad_where (fixP->fx_file, fixP->fx_line,
15916 		  _("branch to a symbol in another ISA mode"));
15917   else if (fix_bad_misaligned_branch_p (fixP))
15918     as_bad_where (fixP->fx_file, fixP->fx_line,
15919 		  _("branch to misaligned address (0x%lx)"),
15920 		  (long) fix_bad_misaligned_address (fixP));
15921   else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15922     as_bad_where (fixP->fx_file, fixP->fx_line,
15923 		  _("cannot encode misaligned addend "
15924 		    "in the relocatable field (0x%lx)"),
15925 		  (long) fixP->fx_offset);
15926 }
15927 
15928 /* Apply a fixup to the object file.  */
15929 
15930 void
15931 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15932 {
15933   char *buf;
15934   unsigned long insn;
15935   reloc_howto_type *howto;
15936 
15937   if (fixP->fx_pcrel)
15938     switch (fixP->fx_r_type)
15939       {
15940       case BFD_RELOC_16_PCREL_S2:
15941       case BFD_RELOC_MIPS16_16_PCREL_S1:
15942       case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15943       case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15944       case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15945       case BFD_RELOC_32_PCREL:
15946       case BFD_RELOC_MIPS_21_PCREL_S2:
15947       case BFD_RELOC_MIPS_26_PCREL_S2:
15948       case BFD_RELOC_MIPS_18_PCREL_S3:
15949       case BFD_RELOC_MIPS_19_PCREL_S2:
15950       case BFD_RELOC_HI16_S_PCREL:
15951       case BFD_RELOC_LO16_PCREL:
15952 	break;
15953 
15954       case BFD_RELOC_32:
15955 	fixP->fx_r_type = BFD_RELOC_32_PCREL;
15956 	break;
15957 
15958       default:
15959 	as_bad_where (fixP->fx_file, fixP->fx_line,
15960 		      _("PC-relative reference to a different section"));
15961 	break;
15962       }
15963 
15964   /* Handle BFD_RELOC_8 and BFD_RELOC_16.  Punt on other bfd
15965      relocations that have no MIPS ELF equivalent.  */
15966   if (fixP->fx_r_type != BFD_RELOC_8
15967       && fixP->fx_r_type != BFD_RELOC_16)
15968     {
15969       howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15970       if (!howto)
15971 	return;
15972     }
15973 
15974   gas_assert (fixP->fx_size == 2
15975 	      || fixP->fx_size == 4
15976 	      || fixP->fx_r_type == BFD_RELOC_8
15977 	      || fixP->fx_r_type == BFD_RELOC_64
15978 	      || fixP->fx_r_type == BFD_RELOC_CTOR
15979 	      || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15980 	      || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15981 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15982 	      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15983 	      || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15984 	      || fixP->fx_r_type == BFD_RELOC_NONE);
15985 
15986   buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15987 
15988   /* Don't treat parts of a composite relocation as done.  There are two
15989      reasons for this:
15990 
15991      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15992 	 should nevertheless be emitted if the first part is.
15993 
15994      (2) In normal usage, composite relocations are never assembly-time
15995 	 constants.  The easiest way of dealing with the pathological
15996 	 exceptions is to generate a relocation against STN_UNDEF and
15997 	 leave everything up to the linker.  */
15998   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15999     fixP->fx_done = 1;
16000 
16001   switch (fixP->fx_r_type)
16002     {
16003     case BFD_RELOC_MIPS_TLS_GD:
16004     case BFD_RELOC_MIPS_TLS_LDM:
16005     case BFD_RELOC_MIPS_TLS_DTPREL32:
16006     case BFD_RELOC_MIPS_TLS_DTPREL64:
16007     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
16008     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
16009     case BFD_RELOC_MIPS_TLS_GOTTPREL:
16010     case BFD_RELOC_MIPS_TLS_TPREL32:
16011     case BFD_RELOC_MIPS_TLS_TPREL64:
16012     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
16013     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
16014     case BFD_RELOC_MICROMIPS_TLS_GD:
16015     case BFD_RELOC_MICROMIPS_TLS_LDM:
16016     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
16017     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
16018     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
16019     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
16020     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
16021     case BFD_RELOC_MIPS16_TLS_GD:
16022     case BFD_RELOC_MIPS16_TLS_LDM:
16023     case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
16024     case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
16025     case BFD_RELOC_MIPS16_TLS_GOTTPREL:
16026     case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
16027     case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
16028       if (fixP->fx_addsy)
16029 	S_SET_THREAD_LOCAL (fixP->fx_addsy);
16030       else
16031 	as_bad_where (fixP->fx_file, fixP->fx_line,
16032 		      _("TLS relocation against a constant"));
16033       break;
16034 
16035     case BFD_RELOC_MIPS_JMP:
16036     case BFD_RELOC_MIPS16_JMP:
16037     case BFD_RELOC_MICROMIPS_JMP:
16038       {
16039 	int shift;
16040 
16041 	gas_assert (!fixP->fx_done);
16042 
16043 	/* Shift is 2, unusually, for microMIPS JALX.  */
16044 	if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
16045 	    && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
16046 	  shift = 1;
16047 	else
16048 	  shift = 2;
16049 
16050 	if (fix_bad_cross_mode_jump_p (fixP))
16051 	  as_bad_where (fixP->fx_file, fixP->fx_line,
16052 			_("jump to a symbol in another ISA mode"));
16053 	else if (fix_bad_same_mode_jalx_p (fixP))
16054 	  as_bad_where (fixP->fx_file, fixP->fx_line,
16055 			_("JALX to a symbol in the same ISA mode"));
16056 	else if (fix_bad_misaligned_jump_p (fixP, shift))
16057 	  as_bad_where (fixP->fx_file, fixP->fx_line,
16058 			_("jump to misaligned address (0x%lx)"),
16059 			(long) fix_bad_misaligned_address (fixP));
16060 	else if (HAVE_IN_PLACE_ADDENDS
16061 		 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
16062 	  as_bad_where (fixP->fx_file, fixP->fx_line,
16063 			_("cannot encode misaligned addend "
16064 			  "in the relocatable field (0x%lx)"),
16065 			(long) fixP->fx_offset);
16066       }
16067       /* Fall through.  */
16068 
16069     case BFD_RELOC_MIPS_SHIFT5:
16070     case BFD_RELOC_MIPS_SHIFT6:
16071     case BFD_RELOC_MIPS_GOT_DISP:
16072     case BFD_RELOC_MIPS_GOT_PAGE:
16073     case BFD_RELOC_MIPS_GOT_OFST:
16074     case BFD_RELOC_MIPS_SUB:
16075     case BFD_RELOC_MIPS_INSERT_A:
16076     case BFD_RELOC_MIPS_INSERT_B:
16077     case BFD_RELOC_MIPS_DELETE:
16078     case BFD_RELOC_MIPS_HIGHEST:
16079     case BFD_RELOC_MIPS_HIGHER:
16080     case BFD_RELOC_MIPS_SCN_DISP:
16081     case BFD_RELOC_MIPS_RELGOT:
16082     case BFD_RELOC_MIPS_JALR:
16083     case BFD_RELOC_HI16:
16084     case BFD_RELOC_HI16_S:
16085     case BFD_RELOC_LO16:
16086     case BFD_RELOC_GPREL16:
16087     case BFD_RELOC_MIPS_LITERAL:
16088     case BFD_RELOC_MIPS_CALL16:
16089     case BFD_RELOC_MIPS_GOT16:
16090     case BFD_RELOC_GPREL32:
16091     case BFD_RELOC_MIPS_GOT_HI16:
16092     case BFD_RELOC_MIPS_GOT_LO16:
16093     case BFD_RELOC_MIPS_CALL_HI16:
16094     case BFD_RELOC_MIPS_CALL_LO16:
16095     case BFD_RELOC_HI16_S_PCREL:
16096     case BFD_RELOC_LO16_PCREL:
16097     case BFD_RELOC_MIPS16_GPREL:
16098     case BFD_RELOC_MIPS16_GOT16:
16099     case BFD_RELOC_MIPS16_CALL16:
16100     case BFD_RELOC_MIPS16_HI16:
16101     case BFD_RELOC_MIPS16_HI16_S:
16102     case BFD_RELOC_MIPS16_LO16:
16103     case BFD_RELOC_MICROMIPS_GOT_DISP:
16104     case BFD_RELOC_MICROMIPS_GOT_PAGE:
16105     case BFD_RELOC_MICROMIPS_GOT_OFST:
16106     case BFD_RELOC_MICROMIPS_SUB:
16107     case BFD_RELOC_MICROMIPS_HIGHEST:
16108     case BFD_RELOC_MICROMIPS_HIGHER:
16109     case BFD_RELOC_MICROMIPS_SCN_DISP:
16110     case BFD_RELOC_MICROMIPS_JALR:
16111     case BFD_RELOC_MICROMIPS_HI16:
16112     case BFD_RELOC_MICROMIPS_HI16_S:
16113     case BFD_RELOC_MICROMIPS_LO16:
16114     case BFD_RELOC_MICROMIPS_GPREL16:
16115     case BFD_RELOC_MICROMIPS_LITERAL:
16116     case BFD_RELOC_MICROMIPS_CALL16:
16117     case BFD_RELOC_MICROMIPS_GOT16:
16118     case BFD_RELOC_MICROMIPS_GOT_HI16:
16119     case BFD_RELOC_MICROMIPS_GOT_LO16:
16120     case BFD_RELOC_MICROMIPS_CALL_HI16:
16121     case BFD_RELOC_MICROMIPS_CALL_LO16:
16122     case BFD_RELOC_MIPS_EH:
16123       if (fixP->fx_done)
16124 	{
16125 	  offsetT value;
16126 
16127 	  if (calculate_reloc (fixP->fx_r_type, *valP, &value))
16128 	    {
16129 	      insn = read_reloc_insn (buf, fixP->fx_r_type);
16130 	      if (mips16_reloc_p (fixP->fx_r_type))
16131 		insn |= mips16_immed_extend (value, 16);
16132 	      else
16133 		insn |= (value & 0xffff);
16134 	      write_reloc_insn (buf, fixP->fx_r_type, insn);
16135 	    }
16136 	  else
16137 	    as_bad_where (fixP->fx_file, fixP->fx_line,
16138 			  _("unsupported constant in relocation"));
16139 	}
16140       break;
16141 
16142     case BFD_RELOC_64:
16143       /* This is handled like BFD_RELOC_32, but we output a sign
16144          extended value if we are only 32 bits.  */
16145       if (fixP->fx_done)
16146 	{
16147 	  if (8 <= sizeof (valueT))
16148 	    md_number_to_chars (buf, *valP, 8);
16149 	  else
16150 	    {
16151 	      valueT hiv;
16152 
16153 	      if ((*valP & 0x80000000) != 0)
16154 		hiv = 0xffffffff;
16155 	      else
16156 		hiv = 0;
16157 	      md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16158 	      md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
16159 	    }
16160 	}
16161       break;
16162 
16163     case BFD_RELOC_RVA:
16164     case BFD_RELOC_32:
16165     case BFD_RELOC_32_PCREL:
16166     case BFD_RELOC_MIPS_16:
16167     case BFD_RELOC_16:
16168     case BFD_RELOC_8:
16169       /* If we are deleting this reloc entry, we must fill in the
16170 	 value now.  This can happen if we have a .word which is not
16171 	 resolved when it appears but is later defined.  */
16172       if (fixP->fx_done)
16173 	md_number_to_chars (buf, *valP, fixP->fx_size);
16174       break;
16175 
16176     case BFD_RELOC_MIPS_21_PCREL_S2:
16177       fix_validate_branch (fixP, *valP);
16178       if (!fixP->fx_done)
16179 	break;
16180 
16181       if (*valP + 0x400000 <= 0x7fffff)
16182 	{
16183 	  insn = read_insn (buf);
16184 	  insn |= (*valP >> 2) & 0x1fffff;
16185 	  write_insn (buf, insn);
16186 	}
16187       else
16188 	as_bad_where (fixP->fx_file, fixP->fx_line,
16189 		      _("branch out of range"));
16190       break;
16191 
16192     case BFD_RELOC_MIPS_26_PCREL_S2:
16193       fix_validate_branch (fixP, *valP);
16194       if (!fixP->fx_done)
16195 	break;
16196 
16197       if (*valP + 0x8000000 <= 0xfffffff)
16198 	{
16199 	  insn = read_insn (buf);
16200 	  insn |= (*valP >> 2) & 0x3ffffff;
16201 	  write_insn (buf, insn);
16202 	}
16203       else
16204 	as_bad_where (fixP->fx_file, fixP->fx_line,
16205 		      _("branch out of range"));
16206       break;
16207 
16208     case BFD_RELOC_MIPS_18_PCREL_S3:
16209       if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
16210 	as_bad_where (fixP->fx_file, fixP->fx_line,
16211 		      _("PC-relative access using misaligned symbol (%lx)"),
16212 		      (long) S_GET_VALUE (fixP->fx_addsy));
16213       if ((fixP->fx_offset & 0x7) != 0)
16214 	as_bad_where (fixP->fx_file, fixP->fx_line,
16215 		      _("PC-relative access using misaligned offset (%lx)"),
16216 		      (long) fixP->fx_offset);
16217       if (!fixP->fx_done)
16218 	break;
16219 
16220       if (*valP + 0x100000 <= 0x1fffff)
16221 	{
16222 	  insn = read_insn (buf);
16223 	  insn |= (*valP >> 3) & 0x3ffff;
16224 	  write_insn (buf, insn);
16225 	}
16226       else
16227 	as_bad_where (fixP->fx_file, fixP->fx_line,
16228 		      _("PC-relative access out of range"));
16229       break;
16230 
16231     case BFD_RELOC_MIPS_19_PCREL_S2:
16232       if ((*valP & 0x3) != 0)
16233 	as_bad_where (fixP->fx_file, fixP->fx_line,
16234 		      _("PC-relative access to misaligned address (%lx)"),
16235 		      (long) *valP);
16236       if (!fixP->fx_done)
16237 	break;
16238 
16239       if (*valP + 0x100000 <= 0x1fffff)
16240 	{
16241 	  insn = read_insn (buf);
16242 	  insn |= (*valP >> 2) & 0x7ffff;
16243 	  write_insn (buf, insn);
16244 	}
16245       else
16246 	as_bad_where (fixP->fx_file, fixP->fx_line,
16247 		      _("PC-relative access out of range"));
16248       break;
16249 
16250     case BFD_RELOC_16_PCREL_S2:
16251       fix_validate_branch (fixP, *valP);
16252 
16253       /* We need to save the bits in the instruction since fixup_segment()
16254 	 might be deleting the relocation entry (i.e., a branch within
16255 	 the current segment).  */
16256       if (! fixP->fx_done)
16257 	break;
16258 
16259       /* Update old instruction data.  */
16260       insn = read_insn (buf);
16261 
16262       if (*valP + 0x20000 <= 0x3ffff)
16263 	{
16264 	  insn |= (*valP >> 2) & 0xffff;
16265 	  write_insn (buf, insn);
16266 	}
16267       else if (fixP->fx_tcbit2
16268 	       && fixP->fx_done
16269 	       && fixP->fx_frag->fr_address >= text_section->vma
16270 	       && (fixP->fx_frag->fr_address
16271 		   < text_section->vma + bfd_section_size (text_section))
16272 	       && ((insn & 0xffff0000) == 0x10000000	 /* beq $0,$0 */
16273 		   || (insn & 0xffff0000) == 0x04010000	 /* bgez $0 */
16274 		   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
16275 	{
16276 	  /* The branch offset is too large.  If this is an
16277              unconditional branch, and we are not generating PIC code,
16278              we can convert it to an absolute jump instruction.  */
16279 	  if ((insn & 0xffff0000) == 0x04110000)	 /* bgezal $0 */
16280 	    insn = 0x0c000000;	/* jal */
16281 	  else
16282 	    insn = 0x08000000;	/* j */
16283 	  fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16284 	  fixP->fx_done = 0;
16285 	  fixP->fx_addsy = section_symbol (text_section);
16286 	  *valP += md_pcrel_from (fixP);
16287 	  write_insn (buf, insn);
16288 	}
16289       else
16290 	{
16291 	  /* If we got here, we have branch-relaxation disabled,
16292 	     and there's nothing we can do to fix this instruction
16293 	     without turning it into a longer sequence.  */
16294 	  as_bad_where (fixP->fx_file, fixP->fx_line,
16295 			_("branch out of range"));
16296 	}
16297       break;
16298 
16299     case BFD_RELOC_MIPS16_16_PCREL_S1:
16300     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16301     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16302     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16303       gas_assert (!fixP->fx_done);
16304       if (fix_bad_cross_mode_branch_p (fixP))
16305 	as_bad_where (fixP->fx_file, fixP->fx_line,
16306 		      _("branch to a symbol in another ISA mode"));
16307       else if (fixP->fx_addsy
16308 	       && !S_FORCE_RELOC (fixP->fx_addsy, true)
16309 	       && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16310 	       && (fixP->fx_offset & 0x1) != 0)
16311 	as_bad_where (fixP->fx_file, fixP->fx_line,
16312 		      _("branch to misaligned address (0x%lx)"),
16313 		      (long) fix_bad_misaligned_address (fixP));
16314       else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16315 	as_bad_where (fixP->fx_file, fixP->fx_line,
16316 		      _("cannot encode misaligned addend "
16317 			"in the relocatable field (0x%lx)"),
16318 		      (long) fixP->fx_offset);
16319       break;
16320 
16321     case BFD_RELOC_VTABLE_INHERIT:
16322       fixP->fx_done = 0;
16323       if (fixP->fx_addsy
16324           && !S_IS_DEFINED (fixP->fx_addsy)
16325           && !S_IS_WEAK (fixP->fx_addsy))
16326         S_SET_WEAK (fixP->fx_addsy);
16327       break;
16328 
16329     case BFD_RELOC_NONE:
16330     case BFD_RELOC_VTABLE_ENTRY:
16331       fixP->fx_done = 0;
16332       break;
16333 
16334     default:
16335       abort ();
16336     }
16337 
16338   /* Remember value for tc_gen_reloc.  */
16339   fixP->fx_addnumber = *valP;
16340 }
16341 
16342 static symbolS *
16343 get_symbol (void)
16344 {
16345   int c;
16346   char *name;
16347   symbolS *p;
16348 
16349   c = get_symbol_name (&name);
16350   p = (symbolS *) symbol_find_or_make (name);
16351   (void) restore_line_pointer (c);
16352   return p;
16353 }
16354 
16355 /* Align the current frag to a given power of two.  If a particular
16356    fill byte should be used, FILL points to an integer that contains
16357    that byte, otherwise FILL is null.
16358 
16359    This function used to have the comment:
16360 
16361       The MIPS assembler also automatically adjusts any preceding label.
16362 
16363    The implementation therefore applied the adjustment to a maximum of
16364    one label.  However, other label adjustments are applied to batches
16365    of labels, and adjusting just one caused problems when new labels
16366    were added for the sake of debugging or unwind information.
16367    We therefore adjust all preceding labels (given as LABELS) instead.  */
16368 
16369 static void
16370 mips_align (int to, int *fill, struct insn_label_list *labels)
16371 {
16372   mips_emit_delays ();
16373   mips_record_compressed_mode ();
16374   if (fill == NULL && subseg_text_p (now_seg))
16375     frag_align_code (to, 0);
16376   else
16377     frag_align (to, fill ? *fill : 0, 0);
16378   record_alignment (now_seg, to);
16379   mips_move_labels (labels, subseg_text_p (now_seg));
16380 }
16381 
16382 /* Align to a given power of two.  .align 0 turns off the automatic
16383    alignment used by the data creating pseudo-ops.  */
16384 
16385 static void
16386 s_align (int x ATTRIBUTE_UNUSED)
16387 {
16388   int temp, fill_value, *fill_ptr;
16389   long max_alignment = 28;
16390 
16391   /* o Note that the assembler pulls down any immediately preceding label
16392        to the aligned address.
16393      o It's not documented but auto alignment is reinstated by
16394        a .align pseudo instruction.
16395      o Note also that after auto alignment is turned off the mips assembler
16396        issues an error on attempt to assemble an improperly aligned data item.
16397        We don't.  */
16398 
16399   temp = get_absolute_expression ();
16400   if (temp > max_alignment)
16401     as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
16402   else if (temp < 0)
16403     {
16404       as_warn (_("alignment negative, 0 assumed"));
16405       temp = 0;
16406     }
16407   if (*input_line_pointer == ',')
16408     {
16409       ++input_line_pointer;
16410       fill_value = get_absolute_expression ();
16411       fill_ptr = &fill_value;
16412     }
16413   else
16414     fill_ptr = 0;
16415   if (temp)
16416     {
16417       segment_info_type *si = seg_info (now_seg);
16418       struct insn_label_list *l = si->label_list;
16419       /* Auto alignment should be switched on by next section change.  */
16420       auto_align = 1;
16421       mips_align (temp, fill_ptr, l);
16422     }
16423   else
16424     {
16425       auto_align = 0;
16426     }
16427 
16428   demand_empty_rest_of_line ();
16429 }
16430 
16431 static void
16432 s_change_sec (int sec)
16433 {
16434   segT seg;
16435 
16436   /* The ELF backend needs to know that we are changing sections, so
16437      that .previous works correctly.  We could do something like check
16438      for an obj_section_change_hook macro, but that might be confusing
16439      as it would not be appropriate to use it in the section changing
16440      functions in read.c, since obj-elf.c intercepts those.  FIXME:
16441      This should be cleaner, somehow.  */
16442   obj_elf_section_change_hook ();
16443 
16444   mips_emit_delays ();
16445 
16446   switch (sec)
16447     {
16448     case 't':
16449       s_text (0);
16450       break;
16451     case 'd':
16452       s_data (0);
16453       break;
16454     case 'b':
16455       subseg_set (bss_section, (subsegT) get_absolute_expression ());
16456       demand_empty_rest_of_line ();
16457       break;
16458 
16459     case 'r':
16460       seg = subseg_new (RDATA_SECTION_NAME,
16461 			(subsegT) get_absolute_expression ());
16462       bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_READONLY
16463 				   | SEC_RELOC | SEC_DATA));
16464       if (!startswith (TARGET_OS, "elf"))
16465 	record_alignment (seg, 4);
16466       demand_empty_rest_of_line ();
16467       break;
16468 
16469     case 's':
16470       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
16471       bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_RELOC
16472 				   | SEC_DATA | SEC_SMALL_DATA));
16473       if (!startswith (TARGET_OS, "elf"))
16474 	record_alignment (seg, 4);
16475       demand_empty_rest_of_line ();
16476       break;
16477 
16478     case 'B':
16479       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
16480       bfd_set_section_flags (seg, SEC_ALLOC | SEC_SMALL_DATA);
16481       if (!startswith (TARGET_OS, "elf"))
16482 	record_alignment (seg, 4);
16483       demand_empty_rest_of_line ();
16484       break;
16485     }
16486 
16487   auto_align = 1;
16488 }
16489 
16490 void
16491 s_change_section (int ignore ATTRIBUTE_UNUSED)
16492 {
16493   char *saved_ilp;
16494   const char *section_name;
16495   char c, next_c = 0;
16496   int section_type;
16497   int section_flag;
16498   int section_entry_size;
16499   int section_alignment;
16500 
16501   saved_ilp = input_line_pointer;
16502   section_name = obj_elf_section_name ();
16503   if (section_name == NULL)
16504     return;
16505   c = input_line_pointer[0];
16506   if (c)
16507     next_c = input_line_pointer[1];
16508 
16509   /* Do we have .section Name<,"flags">?  */
16510   if (c != ',' || (c == ',' && next_c == '"'))
16511     {
16512       input_line_pointer = saved_ilp;
16513       obj_elf_section (ignore);
16514       return;
16515     }
16516 
16517   input_line_pointer++;
16518 
16519   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
16520   if (c == ',')
16521     section_type = get_absolute_expression ();
16522   else
16523     section_type = 0;
16524 
16525   if (*input_line_pointer++ == ',')
16526     section_flag = get_absolute_expression ();
16527   else
16528     section_flag = 0;
16529 
16530   if (*input_line_pointer++ == ',')
16531     section_entry_size = get_absolute_expression ();
16532   else
16533     section_entry_size = 0;
16534 
16535   if (*input_line_pointer++ == ',')
16536     section_alignment = get_absolute_expression ();
16537   else
16538     section_alignment = 0;
16539 
16540   /* FIXME: really ignore?  */
16541   (void) section_alignment;
16542 
16543   /* When using the generic form of .section (as implemented by obj-elf.c),
16544      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
16545      traditionally had to fall back on the more common @progbits instead.
16546 
16547      There's nothing really harmful in this, since bfd will correct
16548      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
16549      means that, for backwards compatibility, the special_section entries
16550      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16551 
16552      Even so, we shouldn't force users of the MIPS .section syntax to
16553      incorrectly label the sections as SHT_PROGBITS.  The best compromise
16554      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16555      generic type-checking code.  */
16556   if (section_type == SHT_MIPS_DWARF)
16557     section_type = SHT_PROGBITS;
16558 
16559   obj_elf_change_section (section_name, section_type, section_flag,
16560 			  section_entry_size, 0, 0, 0);
16561 }
16562 
16563 void
16564 mips_enable_auto_align (void)
16565 {
16566   auto_align = 1;
16567 }
16568 
16569 static void
16570 s_cons (int log_size)
16571 {
16572   segment_info_type *si = seg_info (now_seg);
16573   struct insn_label_list *l = si->label_list;
16574 
16575   mips_emit_delays ();
16576   if (log_size > 0 && auto_align)
16577     mips_align (log_size, 0, l);
16578   cons (1 << log_size);
16579   mips_clear_insn_labels ();
16580 }
16581 
16582 static void
16583 s_float_cons (int type)
16584 {
16585   segment_info_type *si = seg_info (now_seg);
16586   struct insn_label_list *l = si->label_list;
16587 
16588   mips_emit_delays ();
16589 
16590   if (auto_align)
16591     {
16592       if (type == 'd')
16593 	mips_align (3, 0, l);
16594       else
16595 	mips_align (2, 0, l);
16596     }
16597 
16598   float_cons (type);
16599   mips_clear_insn_labels ();
16600 }
16601 
16602 /* Handle .globl.  We need to override it because on Irix 5 you are
16603    permitted to say
16604        .globl foo .text
16605    where foo is an undefined symbol, to mean that foo should be
16606    considered to be the address of a function.  */
16607 
16608 static void
16609 s_mips_globl (int x ATTRIBUTE_UNUSED)
16610 {
16611   char *name;
16612   int c;
16613   symbolS *symbolP;
16614 
16615   do
16616     {
16617       c = get_symbol_name (&name);
16618       symbolP = symbol_find_or_make (name);
16619       S_SET_EXTERNAL (symbolP);
16620 
16621       *input_line_pointer = c;
16622       SKIP_WHITESPACE_AFTER_NAME ();
16623 
16624       if (!is_end_of_line[(unsigned char) *input_line_pointer]
16625 	  && (*input_line_pointer != ','))
16626 	{
16627 	  char *secname;
16628 	  asection *sec;
16629 
16630 	  c = get_symbol_name (&secname);
16631 	  sec = bfd_get_section_by_name (stdoutput, secname);
16632 	  if (sec == NULL)
16633 	    as_bad (_("%s: no such section"), secname);
16634 	  (void) restore_line_pointer (c);
16635 
16636 	  if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16637 	    symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
16638 	}
16639 
16640       c = *input_line_pointer;
16641       if (c == ',')
16642 	{
16643 	  input_line_pointer++;
16644 	  SKIP_WHITESPACE ();
16645 	  if (is_end_of_line[(unsigned char) *input_line_pointer])
16646 	    c = '\n';
16647 	}
16648     }
16649   while (c == ',');
16650 
16651   demand_empty_rest_of_line ();
16652 }
16653 
16654 #ifdef TE_IRIX
16655 /* The Irix 5 and 6 assemblers set the type of any common symbol and
16656    any undefined non-function symbol to STT_OBJECT.  We try to be
16657    compatible, since newer Irix 5 and 6 linkers care.  */
16658 
16659 void
16660 mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16661 {
16662   /* This late in assembly we can set BSF_OBJECT indiscriminately
16663      and let elf.c:swap_out_syms sort out the symbol type.  */
16664   flagword *flags = &symbol_get_bfdsym (symp)->flags;
16665   if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16666       || !S_IS_DEFINED (symp))
16667     *flags |= BSF_OBJECT;
16668 }
16669 #endif
16670 
16671 static void
16672 s_option (int x ATTRIBUTE_UNUSED)
16673 {
16674   char *opt;
16675   char c;
16676 
16677   c = get_symbol_name (&opt);
16678 
16679   if (*opt == 'O')
16680     {
16681       /* FIXME: What does this mean?  */
16682     }
16683   else if (startswith (opt, "pic") && ISDIGIT (opt[3]) && opt[4] == '\0')
16684     {
16685       int i;
16686 
16687       i = atoi (opt + 3);
16688       if (i != 0 && i != 2)
16689 	as_bad (_(".option pic%d not supported"), i);
16690       else if (mips_pic == VXWORKS_PIC)
16691 	as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16692       else if (i == 0)
16693 	mips_pic = NO_PIC;
16694       else if (i == 2)
16695 	{
16696 	  mips_pic = SVR4_PIC;
16697 	  mips_abicalls = true;
16698 	}
16699 
16700       if (mips_pic == SVR4_PIC)
16701 	{
16702 	  if (g_switch_seen && g_switch_value != 0)
16703 	    as_warn (_("-G may not be used with SVR4 PIC code"));
16704 	  g_switch_value = 0;
16705 	  bfd_set_gp_size (stdoutput, 0);
16706 	}
16707     }
16708   else
16709     as_warn (_("unrecognized option \"%s\""), opt);
16710 
16711   (void) restore_line_pointer (c);
16712   demand_empty_rest_of_line ();
16713 }
16714 
16715 /* This structure is used to hold a stack of .set values.  */
16716 
16717 struct mips_option_stack
16718 {
16719   struct mips_option_stack *next;
16720   struct mips_set_options options;
16721 };
16722 
16723 static struct mips_option_stack *mips_opts_stack;
16724 
16725 /* Return status for .set/.module option handling.  */
16726 
16727 enum code_option_type
16728 {
16729   /* Unrecognized option.  */
16730   OPTION_TYPE_BAD = -1,
16731 
16732   /* Ordinary option.  */
16733   OPTION_TYPE_NORMAL,
16734 
16735   /* ISA changing option.  */
16736   OPTION_TYPE_ISA
16737 };
16738 
16739 /* Handle common .set/.module options.  Return status indicating option
16740    type.  */
16741 
16742 static enum code_option_type
16743 parse_code_option (char * name)
16744 {
16745   bool isa_set = false;
16746   const struct mips_ase *ase;
16747 
16748   if (startswith (name, "at="))
16749     {
16750       char *s = name + 3;
16751 
16752       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16753 	as_bad (_("unrecognized register name `%s'"), s);
16754     }
16755   else if (strcmp (name, "at") == 0)
16756     mips_opts.at = ATREG;
16757   else if (strcmp (name, "noat") == 0)
16758     mips_opts.at = ZERO;
16759   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16760     mips_opts.nomove = 0;
16761   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16762     mips_opts.nomove = 1;
16763   else if (strcmp (name, "bopt") == 0)
16764     mips_opts.nobopt = 0;
16765   else if (strcmp (name, "nobopt") == 0)
16766     mips_opts.nobopt = 1;
16767   else if (strcmp (name, "gp=32") == 0)
16768     mips_opts.gp = 32;
16769   else if (strcmp (name, "gp=64") == 0)
16770     mips_opts.gp = 64;
16771   else if (strcmp (name, "fp=32") == 0)
16772     mips_opts.fp = 32;
16773   else if (strcmp (name, "fp=xx") == 0)
16774     mips_opts.fp = 0;
16775   else if (strcmp (name, "fp=64") == 0)
16776     mips_opts.fp = 64;
16777   else if (strcmp (name, "softfloat") == 0)
16778     mips_opts.soft_float = 1;
16779   else if (strcmp (name, "hardfloat") == 0)
16780     mips_opts.soft_float = 0;
16781   else if (strcmp (name, "singlefloat") == 0)
16782     mips_opts.single_float = 1;
16783   else if (strcmp (name, "doublefloat") == 0)
16784     mips_opts.single_float = 0;
16785   else if (strcmp (name, "nooddspreg") == 0)
16786     mips_opts.oddspreg = 0;
16787   else if (strcmp (name, "oddspreg") == 0)
16788     mips_opts.oddspreg = 1;
16789   else if (strcmp (name, "mips16") == 0
16790 	   || strcmp (name, "MIPS-16") == 0)
16791     mips_opts.mips16 = 1;
16792   else if (strcmp (name, "nomips16") == 0
16793 	   || strcmp (name, "noMIPS-16") == 0)
16794     mips_opts.mips16 = 0;
16795   else if (strcmp (name, "micromips") == 0)
16796     mips_opts.micromips = 1;
16797   else if (strcmp (name, "nomicromips") == 0)
16798     mips_opts.micromips = 0;
16799   else if (name[0] == 'n'
16800 	   && name[1] == 'o'
16801 	   && (ase = mips_lookup_ase (name + 2)))
16802     mips_set_ase (ase, &mips_opts, false);
16803   else if ((ase = mips_lookup_ase (name)))
16804     mips_set_ase (ase, &mips_opts, true);
16805   else if (startswith (name, "mips") || startswith (name, "arch="))
16806     {
16807       /* Permit the user to change the ISA and architecture on the fly.
16808 	 Needless to say, misuse can cause serious problems.  */
16809       if (startswith (name, "arch="))
16810 	{
16811 	  const struct mips_cpu_info *p;
16812 
16813 	  p = mips_parse_cpu ("internal use", name + 5);
16814 	  if (!p)
16815 	    as_bad (_("unknown architecture %s"), name + 5);
16816 	  else
16817 	    {
16818 	      mips_opts.arch = p->cpu;
16819 	      mips_opts.isa = p->isa;
16820 	      isa_set = true;
16821 	      mips_opts.init_ase = p->ase;
16822 	    }
16823 	}
16824       else if (startswith (name, "mips"))
16825 	{
16826 	  const struct mips_cpu_info *p;
16827 
16828 	  p = mips_parse_cpu ("internal use", name);
16829 	  if (!p)
16830 	    as_bad (_("unknown ISA level %s"), name + 4);
16831 	  else
16832 	    {
16833 	      mips_opts.arch = p->cpu;
16834 	      mips_opts.isa = p->isa;
16835 	      isa_set = true;
16836 	      mips_opts.init_ase = p->ase;
16837 	    }
16838 	}
16839       else
16840 	as_bad (_("unknown ISA or architecture %s"), name);
16841     }
16842   else if (strcmp (name, "autoextend") == 0)
16843     mips_opts.noautoextend = 0;
16844   else if (strcmp (name, "noautoextend") == 0)
16845     mips_opts.noautoextend = 1;
16846   else if (strcmp (name, "insn32") == 0)
16847     mips_opts.insn32 = true;
16848   else if (strcmp (name, "noinsn32") == 0)
16849     mips_opts.insn32 = false;
16850   else if (strcmp (name, "sym32") == 0)
16851     mips_opts.sym32 = true;
16852   else if (strcmp (name, "nosym32") == 0)
16853     mips_opts.sym32 = false;
16854   else
16855     return OPTION_TYPE_BAD;
16856 
16857   return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
16858 }
16859 
16860 /* Handle the .set pseudo-op.  */
16861 
16862 static void
16863 s_mipsset (int x ATTRIBUTE_UNUSED)
16864 {
16865   enum code_option_type type = OPTION_TYPE_NORMAL;
16866   char *name = input_line_pointer, ch;
16867 
16868   file_mips_check_options ();
16869 
16870   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16871     ++input_line_pointer;
16872   ch = *input_line_pointer;
16873   *input_line_pointer = '\0';
16874 
16875   if (strchr (name, ','))
16876     {
16877       /* Generic ".set" directive; use the generic handler.  */
16878       *input_line_pointer = ch;
16879       input_line_pointer = name;
16880       s_set (0);
16881       return;
16882     }
16883 
16884   if (strcmp (name, "reorder") == 0)
16885     {
16886       if (mips_opts.noreorder)
16887 	end_noreorder ();
16888     }
16889   else if (strcmp (name, "noreorder") == 0)
16890     {
16891       if (!mips_opts.noreorder)
16892 	start_noreorder ();
16893     }
16894   else if (strcmp (name, "macro") == 0)
16895     mips_opts.warn_about_macros = 0;
16896   else if (strcmp (name, "nomacro") == 0)
16897     {
16898       if (mips_opts.noreorder == 0)
16899 	as_bad (_("`noreorder' must be set before `nomacro'"));
16900       mips_opts.warn_about_macros = 1;
16901     }
16902   else if (strcmp (name, "gp=default") == 0)
16903     mips_opts.gp = file_mips_opts.gp;
16904   else if (strcmp (name, "fp=default") == 0)
16905     mips_opts.fp = file_mips_opts.fp;
16906   else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16907     {
16908       mips_opts.isa = file_mips_opts.isa;
16909       mips_opts.arch = file_mips_opts.arch;
16910       mips_opts.init_ase = file_mips_opts.init_ase;
16911       mips_opts.gp = file_mips_opts.gp;
16912       mips_opts.fp = file_mips_opts.fp;
16913     }
16914   else if (strcmp (name, "push") == 0)
16915     {
16916       struct mips_option_stack *s;
16917 
16918       s = XNEW (struct mips_option_stack);
16919       s->next = mips_opts_stack;
16920       s->options = mips_opts;
16921       mips_opts_stack = s;
16922     }
16923   else if (strcmp (name, "pop") == 0)
16924     {
16925       struct mips_option_stack *s;
16926 
16927       s = mips_opts_stack;
16928       if (s == NULL)
16929 	as_bad (_(".set pop with no .set push"));
16930       else
16931 	{
16932 	  /* If we're changing the reorder mode we need to handle
16933              delay slots correctly.  */
16934 	  if (s->options.noreorder && ! mips_opts.noreorder)
16935 	    start_noreorder ();
16936 	  else if (! s->options.noreorder && mips_opts.noreorder)
16937 	    end_noreorder ();
16938 
16939 	  mips_opts = s->options;
16940 	  mips_opts_stack = s->next;
16941 	  free (s);
16942 	}
16943     }
16944   else
16945     {
16946       type = parse_code_option (name);
16947       if (type == OPTION_TYPE_BAD)
16948 	as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16949     }
16950 
16951   /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16952      registers based on what is supported by the arch/cpu.  */
16953   if (type == OPTION_TYPE_ISA)
16954     {
16955       switch (mips_opts.isa)
16956 	{
16957 	case 0:
16958 	  break;
16959 	case ISA_MIPS1:
16960 	  /* MIPS I cannot support FPXX.  */
16961 	  mips_opts.fp = 32;
16962 	  /* fall-through.  */
16963 	case ISA_MIPS2:
16964 	case ISA_MIPS32:
16965 	case ISA_MIPS32R2:
16966 	case ISA_MIPS32R3:
16967 	case ISA_MIPS32R5:
16968 	  mips_opts.gp = 32;
16969 	  if (mips_opts.fp != 0)
16970 	    mips_opts.fp = 32;
16971 	  break;
16972 	case ISA_MIPS32R6:
16973 	  mips_opts.gp = 32;
16974 	  mips_opts.fp = 64;
16975 	  break;
16976 	case ISA_MIPS3:
16977 	case ISA_MIPS4:
16978 	case ISA_MIPS5:
16979 	case ISA_MIPS64:
16980 	case ISA_MIPS64R2:
16981 	case ISA_MIPS64R3:
16982 	case ISA_MIPS64R5:
16983 	case ISA_MIPS64R6:
16984 	  mips_opts.gp = 64;
16985 	  if (mips_opts.fp != 0)
16986 	    {
16987 	      if (mips_opts.arch == CPU_R5900)
16988 		mips_opts.fp = 32;
16989 	      else
16990 		mips_opts.fp = 64;
16991 	    }
16992 	  break;
16993 	default:
16994 	  as_bad (_("unknown ISA level %s"), name + 4);
16995 	  break;
16996 	}
16997     }
16998 
16999   mips_check_options (&mips_opts, false);
17000 
17001   mips_check_isa_supports_ases ();
17002   *input_line_pointer = ch;
17003   demand_empty_rest_of_line ();
17004 }
17005 
17006 /* Handle the .module pseudo-op.  */
17007 
17008 static void
17009 s_module (int ignore ATTRIBUTE_UNUSED)
17010 {
17011   char *name = input_line_pointer, ch;
17012 
17013   while (!is_end_of_line[(unsigned char) *input_line_pointer])
17014     ++input_line_pointer;
17015   ch = *input_line_pointer;
17016   *input_line_pointer = '\0';
17017 
17018   if (!file_mips_opts_checked)
17019     {
17020       if (parse_code_option (name) == OPTION_TYPE_BAD)
17021 	as_bad (_(".module used with unrecognized symbol: %s\n"), name);
17022 
17023       /* Update module level settings from mips_opts.  */
17024       file_mips_opts = mips_opts;
17025     }
17026   else
17027     as_bad (_(".module is not permitted after generating code"));
17028 
17029   *input_line_pointer = ch;
17030   demand_empty_rest_of_line ();
17031 }
17032 
17033 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
17034    .option pic2.  It means to generate SVR4 PIC calls.  */
17035 
17036 static void
17037 s_abicalls (int ignore ATTRIBUTE_UNUSED)
17038 {
17039   mips_pic = SVR4_PIC;
17040   mips_abicalls = true;
17041 
17042   if (g_switch_seen && g_switch_value != 0)
17043     as_warn (_("-G may not be used with SVR4 PIC code"));
17044   g_switch_value = 0;
17045 
17046   bfd_set_gp_size (stdoutput, 0);
17047   demand_empty_rest_of_line ();
17048 }
17049 
17050 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
17051    PIC code.  It sets the $gp register for the function based on the
17052    function address, which is in the register named in the argument.
17053    This uses a relocation against _gp_disp, which is handled specially
17054    by the linker.  The result is:
17055 	lui	$gp,%hi(_gp_disp)
17056 	addiu	$gp,$gp,%lo(_gp_disp)
17057 	addu	$gp,$gp,.cpload argument
17058    The .cpload argument is normally $25 == $t9.
17059 
17060    The -mno-shared option changes this to:
17061 	lui	$gp,%hi(__gnu_local_gp)
17062 	addiu	$gp,$gp,%lo(__gnu_local_gp)
17063    and the argument is ignored.  This saves an instruction, but the
17064    resulting code is not position independent; it uses an absolute
17065    address for __gnu_local_gp.  Thus code assembled with -mno-shared
17066    can go into an ordinary executable, but not into a shared library.  */
17067 
17068 static void
17069 s_cpload (int ignore ATTRIBUTE_UNUSED)
17070 {
17071   expressionS ex;
17072   int reg;
17073   int in_shared;
17074 
17075   file_mips_check_options ();
17076 
17077   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
17078      .cpload is ignored.  */
17079   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
17080     {
17081       s_ignore (0);
17082       return;
17083     }
17084 
17085   if (mips_opts.mips16)
17086     {
17087       as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
17088       ignore_rest_of_line ();
17089       return;
17090     }
17091 
17092   /* .cpload should be in a .set noreorder section.  */
17093   if (mips_opts.noreorder == 0)
17094     as_warn (_(".cpload not in noreorder section"));
17095 
17096   reg = tc_get_register (0);
17097 
17098   /* If we need to produce a 64-bit address, we are better off using
17099      the default instruction sequence.  */
17100   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
17101 
17102   ex.X_op = O_symbol;
17103   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
17104                                          "__gnu_local_gp");
17105   ex.X_op_symbol = NULL;
17106   ex.X_add_number = 0;
17107 
17108   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
17109   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17110 
17111   mips_mark_labels ();
17112   mips_assembling_insn = true;
17113 
17114   macro_start ();
17115   macro_build_lui (&ex, mips_gp_register);
17116   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17117 	       mips_gp_register, BFD_RELOC_LO16);
17118   if (in_shared)
17119     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17120 		 mips_gp_register, reg);
17121   macro_end ();
17122 
17123   mips_assembling_insn = false;
17124   demand_empty_rest_of_line ();
17125 }
17126 
17127 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
17128      .cpsetup $reg1, offset|$reg2, label
17129 
17130    If offset is given, this results in:
17131      sd		$gp, offset($sp)
17132      lui	$gp, %hi(%neg(%gp_rel(label)))
17133      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
17134      daddu	$gp, $gp, $reg1
17135 
17136    If $reg2 is given, this results in:
17137      or		$reg2, $gp, $0
17138      lui	$gp, %hi(%neg(%gp_rel(label)))
17139      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
17140      daddu	$gp, $gp, $reg1
17141    $reg1 is normally $25 == $t9.
17142 
17143    The -mno-shared option replaces the last three instructions with
17144 	lui	$gp,%hi(_gp)
17145 	addiu	$gp,$gp,%lo(_gp)  */
17146 
17147 static void
17148 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
17149 {
17150   expressionS ex_off;
17151   expressionS ex_sym;
17152   int reg1;
17153 
17154   file_mips_check_options ();
17155 
17156   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
17157      We also need NewABI support.  */
17158   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17159     {
17160       s_ignore (0);
17161       return;
17162     }
17163 
17164   if (mips_opts.mips16)
17165     {
17166       as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17167       ignore_rest_of_line ();
17168       return;
17169     }
17170 
17171   reg1 = tc_get_register (0);
17172   SKIP_WHITESPACE ();
17173   if (*input_line_pointer != ',')
17174     {
17175       as_bad (_("missing argument separator ',' for .cpsetup"));
17176       return;
17177     }
17178   else
17179     ++input_line_pointer;
17180   SKIP_WHITESPACE ();
17181   if (*input_line_pointer == '$')
17182     {
17183       mips_cpreturn_register = tc_get_register (0);
17184       mips_cpreturn_offset = -1;
17185     }
17186   else
17187     {
17188       mips_cpreturn_offset = get_absolute_expression ();
17189       mips_cpreturn_register = -1;
17190     }
17191   SKIP_WHITESPACE ();
17192   if (*input_line_pointer != ',')
17193     {
17194       as_bad (_("missing argument separator ',' for .cpsetup"));
17195       return;
17196     }
17197   else
17198     ++input_line_pointer;
17199   SKIP_WHITESPACE ();
17200   expression (&ex_sym);
17201 
17202   mips_mark_labels ();
17203   mips_assembling_insn = true;
17204 
17205   macro_start ();
17206   if (mips_cpreturn_register == -1)
17207     {
17208       ex_off.X_op = O_constant;
17209       ex_off.X_add_symbol = NULL;
17210       ex_off.X_op_symbol = NULL;
17211       ex_off.X_add_number = mips_cpreturn_offset;
17212 
17213       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17214 		   BFD_RELOC_LO16, SP);
17215     }
17216   else
17217     move_register (mips_cpreturn_register, mips_gp_register);
17218 
17219   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
17220     {
17221       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
17222 		   -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17223 		   BFD_RELOC_HI16_S);
17224 
17225       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17226 		   mips_gp_register, -1, BFD_RELOC_GPREL16,
17227 		   BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17228 
17229       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17230 		   mips_gp_register, reg1);
17231     }
17232   else
17233     {
17234       expressionS ex;
17235 
17236       ex.X_op = O_symbol;
17237       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
17238       ex.X_op_symbol = NULL;
17239       ex.X_add_number = 0;
17240 
17241       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
17242       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17243 
17244       macro_build_lui (&ex, mips_gp_register);
17245       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17246 		   mips_gp_register, BFD_RELOC_LO16);
17247     }
17248 
17249   macro_end ();
17250 
17251   mips_assembling_insn = false;
17252   demand_empty_rest_of_line ();
17253 }
17254 
17255 static void
17256 s_cplocal (int ignore ATTRIBUTE_UNUSED)
17257 {
17258   file_mips_check_options ();
17259 
17260   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
17261      .cplocal is ignored.  */
17262   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17263     {
17264       s_ignore (0);
17265       return;
17266     }
17267 
17268   if (mips_opts.mips16)
17269     {
17270       as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17271       ignore_rest_of_line ();
17272       return;
17273     }
17274 
17275   mips_gp_register = tc_get_register (0);
17276   demand_empty_rest_of_line ();
17277 }
17278 
17279 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
17280    offset from $sp.  The offset is remembered, and after making a PIC
17281    call $gp is restored from that location.  */
17282 
17283 static void
17284 s_cprestore (int ignore ATTRIBUTE_UNUSED)
17285 {
17286   expressionS ex;
17287 
17288   file_mips_check_options ();
17289 
17290   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
17291      .cprestore is ignored.  */
17292   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
17293     {
17294       s_ignore (0);
17295       return;
17296     }
17297 
17298   if (mips_opts.mips16)
17299     {
17300       as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17301       ignore_rest_of_line ();
17302       return;
17303     }
17304 
17305   mips_cprestore_offset = get_absolute_expression ();
17306   mips_cprestore_valid = 1;
17307 
17308   ex.X_op = O_constant;
17309   ex.X_add_symbol = NULL;
17310   ex.X_op_symbol = NULL;
17311   ex.X_add_number = mips_cprestore_offset;
17312 
17313   mips_mark_labels ();
17314   mips_assembling_insn = true;
17315 
17316   macro_start ();
17317   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17318 				SP, HAVE_64BIT_ADDRESSES);
17319   macro_end ();
17320 
17321   mips_assembling_insn = false;
17322   demand_empty_rest_of_line ();
17323 }
17324 
17325 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
17326    was given in the preceding .cpsetup, it results in:
17327      ld		$gp, offset($sp)
17328 
17329    If a register $reg2 was given there, it results in:
17330      or		$gp, $reg2, $0  */
17331 
17332 static void
17333 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
17334 {
17335   expressionS ex;
17336 
17337   file_mips_check_options ();
17338 
17339   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17340      We also need NewABI support.  */
17341   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17342     {
17343       s_ignore (0);
17344       return;
17345     }
17346 
17347   if (mips_opts.mips16)
17348     {
17349       as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17350       ignore_rest_of_line ();
17351       return;
17352     }
17353 
17354   mips_mark_labels ();
17355   mips_assembling_insn = true;
17356 
17357   macro_start ();
17358   if (mips_cpreturn_register == -1)
17359     {
17360       ex.X_op = O_constant;
17361       ex.X_add_symbol = NULL;
17362       ex.X_op_symbol = NULL;
17363       ex.X_add_number = mips_cpreturn_offset;
17364 
17365       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
17366     }
17367   else
17368     move_register (mips_gp_register, mips_cpreturn_register);
17369 
17370   macro_end ();
17371 
17372   mips_assembling_insn = false;
17373   demand_empty_rest_of_line ();
17374 }
17375 
17376 /* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17377    pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17378    DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17379    debug information or MIPS16 TLS.  */
17380 
17381 static void
17382 s_tls_rel_directive (const size_t bytes, const char *dirstr,
17383 		     bfd_reloc_code_real_type rtype)
17384 {
17385   expressionS ex;
17386   char *p;
17387 
17388   expression (&ex);
17389 
17390   if (ex.X_op != O_symbol)
17391     {
17392       as_bad (_("unsupported use of %s"), dirstr);
17393       ignore_rest_of_line ();
17394     }
17395 
17396   p = frag_more (bytes);
17397   md_number_to_chars (p, 0, bytes);
17398   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false, rtype);
17399   demand_empty_rest_of_line ();
17400   mips_clear_insn_labels ();
17401 }
17402 
17403 /* Handle .dtprelword.  */
17404 
17405 static void
17406 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17407 {
17408   s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
17409 }
17410 
17411 /* Handle .dtpreldword.  */
17412 
17413 static void
17414 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17415 {
17416   s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17417 }
17418 
17419 /* Handle .tprelword.  */
17420 
17421 static void
17422 s_tprelword (int ignore ATTRIBUTE_UNUSED)
17423 {
17424   s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17425 }
17426 
17427 /* Handle .tpreldword.  */
17428 
17429 static void
17430 s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17431 {
17432   s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
17433 }
17434 
17435 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
17436    code.  It sets the offset to use in gp_rel relocations.  */
17437 
17438 static void
17439 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
17440 {
17441   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17442      We also need NewABI support.  */
17443   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17444     {
17445       s_ignore (0);
17446       return;
17447     }
17448 
17449   mips_gprel_offset = get_absolute_expression ();
17450 
17451   demand_empty_rest_of_line ();
17452 }
17453 
17454 /* Handle the .gpword pseudo-op.  This is used when generating PIC
17455    code.  It generates a 32 bit GP relative reloc.  */
17456 
17457 static void
17458 s_gpword (int ignore ATTRIBUTE_UNUSED)
17459 {
17460   segment_info_type *si;
17461   struct insn_label_list *l;
17462   expressionS ex;
17463   char *p;
17464 
17465   /* When not generating PIC code, this is treated as .word.  */
17466   if (mips_pic != SVR4_PIC)
17467     {
17468       s_cons (2);
17469       return;
17470     }
17471 
17472   si = seg_info (now_seg);
17473   l = si->label_list;
17474   mips_emit_delays ();
17475   if (auto_align)
17476     mips_align (2, 0, l);
17477 
17478   expression (&ex);
17479   mips_clear_insn_labels ();
17480 
17481   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17482     {
17483       as_bad (_("unsupported use of .gpword"));
17484       ignore_rest_of_line ();
17485     }
17486 
17487   p = frag_more (4);
17488   md_number_to_chars (p, 0, 4);
17489   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
17490 	       BFD_RELOC_GPREL32);
17491 
17492   demand_empty_rest_of_line ();
17493 }
17494 
17495 static void
17496 s_gpdword (int ignore ATTRIBUTE_UNUSED)
17497 {
17498   segment_info_type *si;
17499   struct insn_label_list *l;
17500   expressionS ex;
17501   char *p;
17502 
17503   /* When not generating PIC code, this is treated as .dword.  */
17504   if (mips_pic != SVR4_PIC)
17505     {
17506       s_cons (3);
17507       return;
17508     }
17509 
17510   si = seg_info (now_seg);
17511   l = si->label_list;
17512   mips_emit_delays ();
17513   if (auto_align)
17514     mips_align (3, 0, l);
17515 
17516   expression (&ex);
17517   mips_clear_insn_labels ();
17518 
17519   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17520     {
17521       as_bad (_("unsupported use of .gpdword"));
17522       ignore_rest_of_line ();
17523     }
17524 
17525   p = frag_more (8);
17526   md_number_to_chars (p, 0, 8);
17527   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
17528 	       BFD_RELOC_GPREL32)->fx_tcbit = 1;
17529 
17530   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
17531   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17532 	   false, BFD_RELOC_64)->fx_tcbit = 1;
17533 
17534   demand_empty_rest_of_line ();
17535 }
17536 
17537 /* Handle the .ehword pseudo-op.  This is used when generating unwinding
17538    tables.  It generates a R_MIPS_EH reloc.  */
17539 
17540 static void
17541 s_ehword (int ignore ATTRIBUTE_UNUSED)
17542 {
17543   expressionS ex;
17544   char *p;
17545 
17546   mips_emit_delays ();
17547 
17548   expression (&ex);
17549   mips_clear_insn_labels ();
17550 
17551   if (ex.X_op != O_symbol || ex.X_add_number != 0)
17552     {
17553       as_bad (_("unsupported use of .ehword"));
17554       ignore_rest_of_line ();
17555     }
17556 
17557   p = frag_more (4);
17558   md_number_to_chars (p, 0, 4);
17559   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
17560 	       BFD_RELOC_32_PCREL);
17561 
17562   demand_empty_rest_of_line ();
17563 }
17564 
17565 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
17566    tables in SVR4 PIC code.  */
17567 
17568 static void
17569 s_cpadd (int ignore ATTRIBUTE_UNUSED)
17570 {
17571   int reg;
17572 
17573   file_mips_check_options ();
17574 
17575   /* This is ignored when not generating SVR4 PIC code.  */
17576   if (mips_pic != SVR4_PIC)
17577     {
17578       s_ignore (0);
17579       return;
17580     }
17581 
17582   mips_mark_labels ();
17583   mips_assembling_insn = true;
17584 
17585   /* Add $gp to the register named as an argument.  */
17586   macro_start ();
17587   reg = tc_get_register (0);
17588   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
17589   macro_end ();
17590 
17591   mips_assembling_insn = false;
17592   demand_empty_rest_of_line ();
17593 }
17594 
17595 /* Handle the .insn pseudo-op.  This marks instruction labels in
17596    mips16/micromips mode.  This permits the linker to handle them specially,
17597    such as generating jalx instructions when needed.  We also make
17598    them odd for the duration of the assembly, in order to generate the
17599    right sort of code.  We will make them even in the adjust_symtab
17600    routine, while leaving them marked.  This is convenient for the
17601    debugger and the disassembler.  The linker knows to make them odd
17602    again.  */
17603 
17604 static void
17605 s_insn (int ignore ATTRIBUTE_UNUSED)
17606 {
17607   file_mips_check_options ();
17608   file_ase_mips16 |= mips_opts.mips16;
17609   file_ase_micromips |= mips_opts.micromips;
17610 
17611   mips_mark_labels ();
17612 
17613   demand_empty_rest_of_line ();
17614 }
17615 
17616 /* Handle the .nan pseudo-op.  */
17617 
17618 static void
17619 s_nan (int ignore ATTRIBUTE_UNUSED)
17620 {
17621   static const char str_legacy[] = "legacy";
17622   static const char str_2008[] = "2008";
17623   size_t i;
17624 
17625   for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17626 
17627   if (i == sizeof (str_2008) - 1
17628       && memcmp (input_line_pointer, str_2008, i) == 0)
17629     mips_nan2008 = 1;
17630   else if (i == sizeof (str_legacy) - 1
17631 	   && memcmp (input_line_pointer, str_legacy, i) == 0)
17632     {
17633       if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17634 	mips_nan2008 = 0;
17635       else
17636 	as_bad (_("`%s' does not support legacy NaN"),
17637 	          mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17638     }
17639   else
17640     as_bad (_("bad .nan directive"));
17641 
17642   input_line_pointer += i;
17643   demand_empty_rest_of_line ();
17644 }
17645 
17646 /* Handle a .stab[snd] directive.  Ideally these directives would be
17647    implemented in a transparent way, so that removing them would not
17648    have any effect on the generated instructions.  However, s_stab
17649    internally changes the section, so in practice we need to decide
17650    now whether the preceding label marks compressed code.  We do not
17651    support changing the compression mode of a label after a .stab*
17652    directive, such as in:
17653 
17654    foo:
17655 	.stabs ...
17656 	.set mips16
17657 
17658    so the current mode wins.  */
17659 
17660 static void
17661 s_mips_stab (int type)
17662 {
17663   file_mips_check_options ();
17664   mips_mark_labels ();
17665   s_stab (type);
17666 }
17667 
17668 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
17669 
17670 static void
17671 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
17672 {
17673   char *name;
17674   int c;
17675   symbolS *symbolP;
17676   expressionS exp;
17677 
17678   c = get_symbol_name (&name);
17679   symbolP = symbol_find_or_make (name);
17680   S_SET_WEAK (symbolP);
17681   *input_line_pointer = c;
17682 
17683   SKIP_WHITESPACE_AFTER_NAME ();
17684 
17685   if (! is_end_of_line[(unsigned char) *input_line_pointer])
17686     {
17687       if (S_IS_DEFINED (symbolP))
17688 	{
17689 	  as_bad (_("ignoring attempt to redefine symbol %s"),
17690 		  S_GET_NAME (symbolP));
17691 	  ignore_rest_of_line ();
17692 	  return;
17693 	}
17694 
17695       if (*input_line_pointer == ',')
17696 	{
17697 	  ++input_line_pointer;
17698 	  SKIP_WHITESPACE ();
17699 	}
17700 
17701       expression (&exp);
17702       if (exp.X_op != O_symbol)
17703 	{
17704 	  as_bad (_("bad .weakext directive"));
17705 	  ignore_rest_of_line ();
17706 	  return;
17707 	}
17708       symbol_set_value_expression (symbolP, &exp);
17709     }
17710 
17711   demand_empty_rest_of_line ();
17712 }
17713 
17714 /* Parse a register string into a number.  Called from the ECOFF code
17715    to parse .frame.  The argument is non-zero if this is the frame
17716    register, so that we can record it in mips_frame_reg.  */
17717 
17718 int
17719 tc_get_register (int frame)
17720 {
17721   unsigned int reg;
17722 
17723   SKIP_WHITESPACE ();
17724   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17725     reg = 0;
17726   if (frame)
17727     {
17728       mips_frame_reg = reg != 0 ? reg : SP;
17729       mips_frame_reg_valid = 1;
17730       mips_cprestore_valid = 0;
17731     }
17732   return reg;
17733 }
17734 
17735 valueT
17736 md_section_align (asection *seg, valueT addr)
17737 {
17738   int align = bfd_section_alignment (seg);
17739 
17740   /* We don't need to align ELF sections to the full alignment.
17741      However, Irix 5 may prefer that we align them at least to a 16
17742      byte boundary.  We don't bother to align the sections if we
17743      are targeted for an embedded system.  */
17744   if (startswith (TARGET_OS, "elf"))
17745     return addr;
17746   if (align > 4)
17747     align = 4;
17748 
17749   return ((addr + (1 << align) - 1) & -(1 << align));
17750 }
17751 
17752 /* Utility routine, called from above as well.  If called while the
17753    input file is still being read, it's only an approximation.  (For
17754    example, a symbol may later become defined which appeared to be
17755    undefined earlier.)  */
17756 
17757 static int
17758 nopic_need_relax (symbolS *sym, int before_relaxing)
17759 {
17760   if (sym == 0)
17761     return 0;
17762 
17763   if (g_switch_value > 0)
17764     {
17765       const char *symname;
17766       int change;
17767 
17768       /* Find out whether this symbol can be referenced off the $gp
17769 	 register.  It can be if it is smaller than the -G size or if
17770 	 it is in the .sdata or .sbss section.  Certain symbols can
17771 	 not be referenced off the $gp, although it appears as though
17772 	 they can.  */
17773       symname = S_GET_NAME (sym);
17774       if (symname != (const char *) NULL
17775 	  && (strcmp (symname, "eprol") == 0
17776 	      || strcmp (symname, "etext") == 0
17777 	      || strcmp (symname, "_gp") == 0
17778 	      || strcmp (symname, "edata") == 0
17779 	      || strcmp (symname, "_fbss") == 0
17780 	      || strcmp (symname, "_fdata") == 0
17781 	      || strcmp (symname, "_ftext") == 0
17782 	      || strcmp (symname, "end") == 0
17783 	      || strcmp (symname, "_gp_disp") == 0))
17784 	change = 1;
17785       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17786 	       && (0
17787 #ifndef NO_ECOFF_DEBUGGING
17788 		   || (symbol_get_obj (sym)->ecoff_extern_size != 0
17789 		       && (symbol_get_obj (sym)->ecoff_extern_size
17790 			   <= g_switch_value))
17791 #endif
17792 		   /* We must defer this decision until after the whole
17793 		      file has been read, since there might be a .extern
17794 		      after the first use of this symbol.  */
17795 		   || (before_relaxing
17796 #ifndef NO_ECOFF_DEBUGGING
17797 		       && symbol_get_obj (sym)->ecoff_extern_size == 0
17798 #endif
17799 		       && S_GET_VALUE (sym) == 0)
17800 		   || (S_GET_VALUE (sym) != 0
17801 		       && S_GET_VALUE (sym) <= g_switch_value)))
17802 	change = 0;
17803       else
17804 	{
17805 	  const char *segname;
17806 
17807 	  segname = segment_name (S_GET_SEGMENT (sym));
17808 	  gas_assert (strcmp (segname, ".lit8") != 0
17809 		  && strcmp (segname, ".lit4") != 0);
17810 	  change = (strcmp (segname, ".sdata") != 0
17811 		    && strcmp (segname, ".sbss") != 0
17812 		    && !startswith (segname, ".sdata.")
17813 		    && !startswith (segname, ".sbss.")
17814 		    && !startswith (segname, ".gnu.linkonce.sb.")
17815 		    && !startswith (segname, ".gnu.linkonce.s."));
17816 	}
17817       return change;
17818     }
17819   else
17820     /* We are not optimizing for the $gp register.  */
17821     return 1;
17822 }
17823 
17824 
17825 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
17826 
17827 static bool
17828 pic_need_relax (symbolS *sym)
17829 {
17830   asection *symsec;
17831 
17832   /* Handle the case of a symbol equated to another symbol.  */
17833   while (symbol_equated_reloc_p (sym))
17834     {
17835       symbolS *n;
17836 
17837       /* It's possible to get a loop here in a badly written program.  */
17838       n = symbol_get_value_expression (sym)->X_add_symbol;
17839       if (n == sym)
17840 	break;
17841       sym = n;
17842     }
17843 
17844   if (symbol_section_p (sym))
17845     return true;
17846 
17847   symsec = S_GET_SEGMENT (sym);
17848 
17849   /* This must duplicate the test in adjust_reloc_syms.  */
17850   return (!bfd_is_und_section (symsec)
17851 	  && !bfd_is_abs_section (symsec)
17852 	  && !bfd_is_com_section (symsec)
17853 	  /* A global or weak symbol is treated as external.  */
17854 	  && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
17855 }
17856 
17857 /* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17858    convert a section-relative value VAL to the equivalent PC-relative
17859    value.  */
17860 
17861 static offsetT
17862 mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17863 		  offsetT val, long stretch)
17864 {
17865   fragS *sym_frag;
17866   addressT addr;
17867 
17868   gas_assert (pcrel_op->root.root.type == OP_PCREL);
17869 
17870   sym_frag = symbol_get_frag (fragp->fr_symbol);
17871 
17872   /* If the relax_marker of the symbol fragment differs from the
17873      relax_marker of this fragment, we have not yet adjusted the
17874      symbol fragment fr_address.  We want to add in STRETCH in
17875      order to get a better estimate of the address.  This
17876      particularly matters because of the shift bits.  */
17877   if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17878     {
17879       fragS *f;
17880 
17881       /* Adjust stretch for any alignment frag.  Note that if have
17882 	 been expanding the earlier code, the symbol may be
17883 	 defined in what appears to be an earlier frag.  FIXME:
17884 	 This doesn't handle the fr_subtype field, which specifies
17885 	 a maximum number of bytes to skip when doing an
17886 	 alignment.  */
17887       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17888 	{
17889 	  if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17890 	    {
17891 	      if (stretch < 0)
17892 		stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17893 	      else
17894 		stretch &= ~((1 << (int) f->fr_offset) - 1);
17895 	      if (stretch == 0)
17896 		break;
17897 	    }
17898 	}
17899       if (f != NULL)
17900 	val += stretch;
17901     }
17902 
17903   addr = fragp->fr_address + fragp->fr_fix;
17904 
17905   /* The base address rules are complicated.  The base address of
17906      a branch is the following instruction.  The base address of a
17907      PC relative load or add is the instruction itself, but if it
17908      is in a delay slot (in which case it can not be extended) use
17909      the address of the instruction whose delay slot it is in.  */
17910   if (pcrel_op->include_isa_bit)
17911     {
17912       addr += 2;
17913 
17914       /* If we are currently assuming that this frag should be
17915 	 extended, then the current address is two bytes higher.  */
17916       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17917 	addr += 2;
17918 
17919       /* Ignore the low bit in the target, since it will be set
17920 	 for a text label.  */
17921       val &= -2;
17922     }
17923   else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17924     addr -= 4;
17925   else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17926     addr -= 2;
17927 
17928   val -= addr & -(1 << pcrel_op->align_log2);
17929 
17930   return val;
17931 }
17932 
17933 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17934    extended opcode.  SEC is the section the frag is in.  */
17935 
17936 static int
17937 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
17938 {
17939   const struct mips_int_operand *operand;
17940   offsetT val;
17941   segT symsec;
17942   int type;
17943 
17944   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17945     return 0;
17946   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17947     return 1;
17948 
17949   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17950   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17951   operand = mips16_immed_operand (type, false);
17952   if (S_FORCE_RELOC (fragp->fr_symbol, true)
17953       || (operand->root.type == OP_PCREL
17954 	  ? sec != symsec
17955 	  : !bfd_is_abs_section (symsec)))
17956     return 1;
17957 
17958   val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17959 
17960   if (operand->root.type == OP_PCREL)
17961     {
17962       const struct mips_pcrel_operand *pcrel_op;
17963       offsetT maxtiny;
17964 
17965       if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
17966 	return 1;
17967 
17968       pcrel_op = (const struct mips_pcrel_operand *) operand;
17969       val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17970 
17971       /* If any of the shifted bits are set, we must use an extended
17972          opcode.  If the address depends on the size of this
17973          instruction, this can lead to a loop, so we arrange to always
17974          use an extended opcode.  */
17975       if ((val & ((1 << operand->shift) - 1)) != 0)
17976 	{
17977 	  fragp->fr_subtype =
17978 	    RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
17979 	  return 1;
17980 	}
17981 
17982       /* If we are about to mark a frag as extended because the value
17983          is precisely the next value above maxtiny, then there is a
17984          chance of an infinite loop as in the following code:
17985 	     la	$4,foo
17986 	     .skip	1020
17987 	     .align	2
17988 	   foo:
17989 	 In this case when the la is extended, foo is 0x3fc bytes
17990 	 away, so the la can be shrunk, but then foo is 0x400 away, so
17991 	 the la must be extended.  To avoid this loop, we mark the
17992 	 frag as extended if it was small, and is about to become
17993 	 extended with the next value above maxtiny.  */
17994       maxtiny = mips_int_operand_max (operand);
17995       if (val == maxtiny + (1 << operand->shift)
17996 	  && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17997 	{
17998 	  fragp->fr_subtype =
17999 	    RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
18000 	  return 1;
18001 	}
18002     }
18003 
18004   return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
18005 }
18006 
18007 /* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
18008    macro expansion.  SEC is the section the frag is in.  We only
18009    support PC-relative instructions (LA, DLA, LW, LD) here, in
18010    non-PIC code using 32-bit addressing.  */
18011 
18012 static int
18013 mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
18014 {
18015   const struct mips_pcrel_operand *pcrel_op;
18016   const struct mips_int_operand *operand;
18017   offsetT val;
18018   segT symsec;
18019   int type;
18020 
18021   gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
18022 
18023   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18024     return 0;
18025   if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
18026     return 0;
18027 
18028   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18029   switch (type)
18030     {
18031     case 'A':
18032     case 'B':
18033     case 'E':
18034       symsec = S_GET_SEGMENT (fragp->fr_symbol);
18035       if (bfd_is_abs_section (symsec))
18036 	return 1;
18037       if (RELAX_MIPS16_PIC (fragp->fr_subtype))
18038 	return 0;
18039       if (S_FORCE_RELOC (fragp->fr_symbol, true) || sec != symsec)
18040 	return 1;
18041 
18042       operand = mips16_immed_operand (type, true);
18043       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18044       pcrel_op = (const struct mips_pcrel_operand *) operand;
18045       val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
18046 
18047       return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
18048 
18049     default:
18050       return 0;
18051     }
18052 }
18053 
18054 /* Compute the length of a branch sequence, and adjust the
18055    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
18056    worst-case length is computed, with UPDATE being used to indicate
18057    whether an unconditional (-1), branch-likely (+1) or regular (0)
18058    branch is to be computed.  */
18059 static int
18060 relaxed_branch_length (fragS *fragp, asection *sec, int update)
18061 {
18062   bool toofar;
18063   int length;
18064 
18065   if (fragp
18066       && S_IS_DEFINED (fragp->fr_symbol)
18067       && !S_IS_WEAK (fragp->fr_symbol)
18068       && sec == S_GET_SEGMENT (fragp->fr_symbol))
18069     {
18070       addressT addr;
18071       offsetT val;
18072 
18073       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18074 
18075       addr = fragp->fr_address + fragp->fr_fix + 4;
18076 
18077       val -= addr;
18078 
18079       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
18080     }
18081   else
18082     /* If the symbol is not defined or it's in a different segment,
18083        we emit the long sequence.  */
18084     toofar = true;
18085 
18086   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18087     fragp->fr_subtype
18088       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
18089 			     RELAX_BRANCH_PIC (fragp->fr_subtype),
18090 			     RELAX_BRANCH_UNCOND (fragp->fr_subtype),
18091 			     RELAX_BRANCH_LIKELY (fragp->fr_subtype),
18092 			     RELAX_BRANCH_LINK (fragp->fr_subtype),
18093 			     toofar);
18094 
18095   length = 4;
18096   if (toofar)
18097     {
18098       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
18099 	length += 8;
18100 
18101       if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
18102 	{
18103 	  /* Additional space for PIC loading of target address.  */
18104 	  length += 8;
18105 	  if (mips_opts.isa == ISA_MIPS1)
18106 	    /* Additional space for $at-stabilizing nop.  */
18107 	    length += 4;
18108 	}
18109 
18110       /* If branch is conditional.  */
18111       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
18112 	length += 8;
18113     }
18114 
18115   return length;
18116 }
18117 
18118 /* Get a FRAG's branch instruction delay slot size, either from the
18119    short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
18120    or SHORT_INSN_SIZE otherwise.  */
18121 
18122 static int
18123 frag_branch_delay_slot_size (fragS *fragp, bool al, int short_insn_size)
18124 {
18125   char *buf = fragp->fr_literal + fragp->fr_fix;
18126 
18127   if (al)
18128     return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
18129   else
18130     return short_insn_size;
18131 }
18132 
18133 /* Compute the length of a branch sequence, and adjust the
18134    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
18135    worst-case length is computed, with UPDATE being used to indicate
18136    whether an unconditional (-1), or regular (0) branch is to be
18137    computed.  */
18138 
18139 static int
18140 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18141 {
18142   bool insn32 = true;
18143   bool nods = true;
18144   bool pic = true;
18145   bool al = true;
18146   int short_insn_size;
18147   bool toofar;
18148   int length;
18149 
18150   if (fragp)
18151     {
18152       insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18153       nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18154       pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18155       al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18156     }
18157   short_insn_size = insn32 ? 4 : 2;
18158 
18159   if (fragp
18160       && S_IS_DEFINED (fragp->fr_symbol)
18161       && !S_IS_WEAK (fragp->fr_symbol)
18162       && sec == S_GET_SEGMENT (fragp->fr_symbol))
18163     {
18164       addressT addr;
18165       offsetT val;
18166 
18167       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18168       /* Ignore the low bit in the target, since it will be set
18169 	 for a text label.  */
18170       if ((val & 1) != 0)
18171 	--val;
18172 
18173       addr = fragp->fr_address + fragp->fr_fix + 4;
18174 
18175       val -= addr;
18176 
18177       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18178     }
18179   else
18180     /* If the symbol is not defined or it's in a different segment,
18181        we emit the long sequence.  */
18182     toofar = true;
18183 
18184   if (fragp && update
18185       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18186     fragp->fr_subtype = (toofar
18187 			 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18188 			 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18189 
18190   length = 4;
18191   if (toofar)
18192     {
18193       bool compact_known = fragp != NULL;
18194       bool compact = false;
18195       bool uncond;
18196 
18197       if (fragp)
18198 	{
18199 	  compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18200 	  uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
18201 	}
18202       else
18203 	uncond = update < 0;
18204 
18205       /* If label is out of range, we turn branch <br>:
18206 
18207 		<br>	label			# 4 bytes
18208 	    0:
18209 
18210          into:
18211 
18212 		j	label			# 4 bytes
18213 		nop				# 2/4 bytes if
18214 						#  compact && (!PIC || insn32)
18215 	    0:
18216        */
18217       if ((!pic || insn32) && (!compact_known || compact))
18218 	length += short_insn_size;
18219 
18220       /* If assembling PIC code, we further turn:
18221 
18222 			j	label			# 4 bytes
18223 
18224          into:
18225 
18226 			lw/ld	at, %got(label)(gp)	# 4 bytes
18227 			d/addiu	at, %lo(label)		# 4 bytes
18228 			jr/c	at			# 2/4 bytes
18229        */
18230       if (pic)
18231 	length += 4 + short_insn_size;
18232 
18233       /* Add an extra nop if the jump has no compact form and we need
18234          to fill the delay slot.  */
18235       if ((!pic || al) && nods)
18236 	length += (fragp
18237 		   ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18238 		   : short_insn_size);
18239 
18240       /* If branch <br> is conditional, we prepend negated branch <brneg>:
18241 
18242 			<brneg>	0f			# 4 bytes
18243 			nop				# 2/4 bytes if !compact
18244        */
18245       if (!uncond)
18246 	length += (compact_known && compact) ? 4 : 4 + short_insn_size;
18247     }
18248   else if (nods)
18249     {
18250       /* Add an extra nop to fill the delay slot.  */
18251       gas_assert (fragp);
18252       length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18253     }
18254 
18255   return length;
18256 }
18257 
18258 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18259    bit accordingly.  */
18260 
18261 static int
18262 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18263 {
18264   bool toofar;
18265 
18266   if (fragp
18267       && S_IS_DEFINED (fragp->fr_symbol)
18268       && !S_IS_WEAK (fragp->fr_symbol)
18269       && sec == S_GET_SEGMENT (fragp->fr_symbol))
18270     {
18271       addressT addr;
18272       offsetT val;
18273       int type;
18274 
18275       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18276       /* Ignore the low bit in the target, since it will be set
18277 	 for a text label.  */
18278       if ((val & 1) != 0)
18279 	--val;
18280 
18281       /* Assume this is a 2-byte branch.  */
18282       addr = fragp->fr_address + fragp->fr_fix + 2;
18283 
18284       /* We try to avoid the infinite loop by not adding 2 more bytes for
18285 	 long branches.  */
18286 
18287       val -= addr;
18288 
18289       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18290       if (type == 'D')
18291 	toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18292       else if (type == 'E')
18293 	toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18294       else
18295 	abort ();
18296     }
18297   else
18298     /* If the symbol is not defined or it's in a different segment,
18299        we emit a normal 32-bit branch.  */
18300     toofar = true;
18301 
18302   if (fragp && update
18303       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18304     fragp->fr_subtype
18305       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18306 	       : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18307 
18308   if (toofar)
18309     return 4;
18310 
18311   return 2;
18312 }
18313 
18314 /* Estimate the size of a frag before relaxing.  Unless this is the
18315    mips16, we are not really relaxing here, and the final size is
18316    encoded in the subtype information.  For the mips16, we have to
18317    decide whether we are using an extended opcode or not.  */
18318 
18319 int
18320 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
18321 {
18322   int change;
18323 
18324   if (RELAX_BRANCH_P (fragp->fr_subtype))
18325     {
18326 
18327       fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
18328 
18329       return fragp->fr_var;
18330     }
18331 
18332   if (RELAX_MIPS16_P (fragp->fr_subtype))
18333     {
18334       /* We don't want to modify the EXTENDED bit here; it might get us
18335 	 into infinite loops.  We change it only in mips_relax_frag().  */
18336       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18337 	return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
18338       else
18339 	return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18340     }
18341 
18342   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18343     {
18344       int length = 4;
18345 
18346       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18347 	length = relaxed_micromips_16bit_branch_length (fragp, segtype, false);
18348       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18349 	length = relaxed_micromips_32bit_branch_length (fragp, segtype, false);
18350       fragp->fr_var = length;
18351 
18352       return length;
18353     }
18354 
18355   if (mips_pic == VXWORKS_PIC)
18356     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
18357     change = 0;
18358   else if (RELAX_PIC (fragp->fr_subtype))
18359     change = pic_need_relax (fragp->fr_symbol);
18360   else
18361     change = nopic_need_relax (fragp->fr_symbol, 0);
18362 
18363   if (change)
18364     {
18365       fragp->fr_subtype |= RELAX_USE_SECOND;
18366       return -RELAX_FIRST (fragp->fr_subtype);
18367     }
18368   else
18369     return -RELAX_SECOND (fragp->fr_subtype);
18370 }
18371 
18372 /* This is called to see whether a reloc against a defined symbol
18373    should be converted into a reloc against a section.  */
18374 
18375 int
18376 mips_fix_adjustable (fixS *fixp)
18377 {
18378   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18379       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18380     return 0;
18381 
18382   if (fixp->fx_addsy == NULL)
18383     return 1;
18384 
18385   /* Allow relocs used for EH tables.  */
18386   if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18387     return 1;
18388 
18389   /* If symbol SYM is in a mergeable section, relocations of the form
18390      SYM + 0 can usually be made section-relative.  The mergeable data
18391      is then identified by the section offset rather than by the symbol.
18392 
18393      However, if we're generating REL LO16 relocations, the offset is split
18394      between the LO16 and partnering high part relocation.  The linker will
18395      need to recalculate the complete offset in order to correctly identify
18396      the merge data.
18397 
18398      The linker has traditionally not looked for the partnering high part
18399      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18400      placed anywhere.  Rather than break backwards compatibility by changing
18401      this, it seems better not to force the issue, and instead keep the
18402      original symbol.  This will work with either linker behavior.  */
18403   if ((lo16_reloc_p (fixp->fx_r_type)
18404        || reloc_needs_lo_p (fixp->fx_r_type))
18405       && HAVE_IN_PLACE_ADDENDS
18406       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18407     return 0;
18408 
18409   /* There is no place to store an in-place offset for JALR relocations.  */
18410   if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18411     return 0;
18412 
18413   /* Likewise an in-range offset of limited PC-relative relocations may
18414      overflow the in-place relocatable field if recalculated against the
18415      start address of the symbol's containing section.
18416 
18417      Also, PC relative relocations for MIPS R6 need to be symbol rather than
18418      section relative to allow linker relaxations to be performed later on.  */
18419   if (limited_pcrel_reloc_p (fixp->fx_r_type)
18420       && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
18421     return 0;
18422 
18423   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18424      to a floating-point stub.  The same is true for non-R_MIPS16_26
18425      relocations against MIPS16 functions; in this case, the stub becomes
18426      the function's canonical address.
18427 
18428      Floating-point stubs are stored in unique .mips16.call.* or
18429      .mips16.fn.* sections.  If a stub T for function F is in section S,
18430      the first relocation in section S must be against F; this is how the
18431      linker determines the target function.  All relocations that might
18432      resolve to T must also be against F.  We therefore have the following
18433      restrictions, which are given in an intentionally-redundant way:
18434 
18435        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18436 	  symbols.
18437 
18438        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18439 	  if that stub might be used.
18440 
18441        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18442 	  symbols.
18443 
18444        4. We cannot reduce a stub's relocations against MIPS16 symbols if
18445 	  that stub might be used.
18446 
18447      There is a further restriction:
18448 
18449        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18450 	  R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
18451 	  R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18452 	  R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18453 	  against MIPS16 or microMIPS symbols because we need to keep the
18454 	  MIPS16 or microMIPS symbol for the purpose of mode mismatch
18455 	  detection and JAL or BAL to JALX instruction conversion in the
18456 	  linker.
18457 
18458      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18459      against a MIPS16 symbol.  We deal with (5) by additionally leaving
18460      alone any jump and branch relocations against a microMIPS symbol.
18461 
18462      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18463      relocation against some symbol R, no relocation against R may be
18464      reduced.  (Note that this deals with (2) as well as (1) because
18465      relocations against global symbols will never be reduced on ELF
18466      targets.)  This approach is a little simpler than trying to detect
18467      stub sections, and gives the "all or nothing" per-symbol consistency
18468      that we have for MIPS16 symbols.  */
18469   if (fixp->fx_subsy == NULL
18470       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
18471 	  || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18472 	      && (jmp_reloc_p (fixp->fx_r_type)
18473 		  || b_reloc_p (fixp->fx_r_type)))
18474 	  || *symbol_get_tc (fixp->fx_addsy)))
18475     return 0;
18476 
18477   return 1;
18478 }
18479 
18480 /* Translate internal representation of relocation info to BFD target
18481    format.  */
18482 
18483 arelent **
18484 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18485 {
18486   static arelent *retval[4];
18487   arelent *reloc;
18488   bfd_reloc_code_real_type code;
18489 
18490   memset (retval, 0, sizeof(retval));
18491   reloc = retval[0] = XCNEW (arelent);
18492   reloc->sym_ptr_ptr = XNEW (asymbol *);
18493   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18494   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18495 
18496   if (fixp->fx_pcrel)
18497     {
18498       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18499 		  || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
18500 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18501 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
18502 		  || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18503 		  || fixp->fx_r_type == BFD_RELOC_32_PCREL
18504 		  || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18505 		  || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18506 		  || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18507 		  || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18508 		  || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18509 		  || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
18510 
18511       /* At this point, fx_addnumber is "symbol offset - pcrel address".
18512 	 Relocations want only the symbol offset.  */
18513       switch (fixp->fx_r_type)
18514 	{
18515 	case BFD_RELOC_MIPS_18_PCREL_S3:
18516 	  reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18517 	  break;
18518 	default:
18519 	  reloc->addend = fixp->fx_addnumber + reloc->address;
18520 	  break;
18521 	}
18522     }
18523   else if (HAVE_IN_PLACE_ADDENDS
18524 	   && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18525 	   && (read_compressed_insn (fixp->fx_frag->fr_literal
18526 				     + fixp->fx_where, 4) >> 26) == 0x3c)
18527     {
18528       /* Shift is 2, unusually, for microMIPS JALX.  Adjust the in-place
18529          addend accordingly.  */
18530       reloc->addend = fixp->fx_addnumber >> 1;
18531     }
18532   else
18533     reloc->addend = fixp->fx_addnumber;
18534 
18535   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18536      entry to be used in the relocation's section offset.  */
18537   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18538     {
18539       reloc->address = reloc->addend;
18540       reloc->addend = 0;
18541     }
18542 
18543   code = fixp->fx_r_type;
18544 
18545   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
18546   if (reloc->howto == NULL)
18547     {
18548       as_bad_where (fixp->fx_file, fixp->fx_line,
18549 		    _("cannot represent %s relocation in this object file"
18550 		      " format"),
18551 		    bfd_get_reloc_code_name (code));
18552       retval[0] = NULL;
18553     }
18554 
18555   return retval;
18556 }
18557 
18558 /* Relax a machine dependent frag.  This returns the amount by which
18559    the current size of the frag should change.  */
18560 
18561 int
18562 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
18563 {
18564   if (RELAX_BRANCH_P (fragp->fr_subtype))
18565     {
18566       offsetT old_var = fragp->fr_var;
18567 
18568       fragp->fr_var = relaxed_branch_length (fragp, sec, true);
18569 
18570       return fragp->fr_var - old_var;
18571     }
18572 
18573   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18574     {
18575       offsetT old_var = fragp->fr_var;
18576       offsetT new_var = 4;
18577 
18578       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18579 	new_var = relaxed_micromips_16bit_branch_length (fragp, sec, true);
18580       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18581 	new_var = relaxed_micromips_32bit_branch_length (fragp, sec, true);
18582       fragp->fr_var = new_var;
18583 
18584       return new_var - old_var;
18585     }
18586 
18587   if (! RELAX_MIPS16_P (fragp->fr_subtype))
18588     return 0;
18589 
18590   if (!mips16_extended_frag (fragp, sec, stretch))
18591     {
18592       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18593 	{
18594 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18595 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
18596 	}
18597       else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18598 	{
18599 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18600 	  return -2;
18601 	}
18602       else
18603 	return 0;
18604     }
18605   else if (!mips16_macro_frag (fragp, sec, stretch))
18606     {
18607       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18608 	{
18609 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18610 	  fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18611 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
18612 	}
18613       else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18614 	{
18615 	  fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18616 	  return 2;
18617 	}
18618       else
18619 	return 0;
18620     }
18621   else
18622     {
18623       if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18624 	return 0;
18625       else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18626 	{
18627 	  fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18628 	  fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18629 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
18630 	}
18631       else
18632 	{
18633 	  fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
18634 	  return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
18635 	}
18636     }
18637 
18638   return 0;
18639 }
18640 
18641 /* Convert a machine dependent frag.  */
18642 
18643 void
18644 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
18645 {
18646   if (RELAX_BRANCH_P (fragp->fr_subtype))
18647     {
18648       char *buf;
18649       unsigned long insn;
18650       fixS *fixp;
18651 
18652       buf = fragp->fr_literal + fragp->fr_fix;
18653       insn = read_insn (buf);
18654 
18655       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18656 	{
18657 	  /* We generate a fixup instead of applying it right now
18658 	     because, if there are linker relaxations, we're going to
18659 	     need the relocations.  */
18660 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18661 			  fragp->fr_symbol, fragp->fr_offset,
18662 			  true, BFD_RELOC_16_PCREL_S2);
18663 	  fixp->fx_file = fragp->fr_file;
18664 	  fixp->fx_line = fragp->fr_line;
18665 
18666 	  buf = write_insn (buf, insn);
18667 	}
18668       else
18669 	{
18670 	  int i;
18671 
18672 	  as_warn_where (fragp->fr_file, fragp->fr_line,
18673 			 _("relaxed out-of-range branch into a jump"));
18674 
18675 	  if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18676 	    goto uncond;
18677 
18678 	  if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18679 	    {
18680 	      /* Reverse the branch.  */
18681 	      switch ((insn >> 28) & 0xf)
18682 		{
18683 		case 4:
18684 		  if ((insn & 0xff000000) == 0x47000000
18685 		      || (insn & 0xff600000) == 0x45600000)
18686 		    {
18687 		      /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18688 			 reversed by tweaking bit 23.  */
18689 		      insn ^= 0x00800000;
18690 		    }
18691 		  else
18692 		    {
18693 		      /* bc[0-3][tf]l? instructions can have the condition
18694 			 reversed by tweaking a single TF bit, and their
18695 			 opcodes all have 0x4???????.  */
18696 		      gas_assert ((insn & 0xf3e00000) == 0x41000000);
18697 		      insn ^= 0x00010000;
18698 		    }
18699 		  break;
18700 
18701 		case 0:
18702 		  /* bltz	0x04000000	bgez	0x04010000
18703 		     bltzal	0x04100000	bgezal	0x04110000  */
18704 		  gas_assert ((insn & 0xfc0e0000) == 0x04000000);
18705 		  insn ^= 0x00010000;
18706 		  break;
18707 
18708 		case 1:
18709 		  /* beq	0x10000000	bne	0x14000000
18710 		     blez	0x18000000	bgtz	0x1c000000  */
18711 		  insn ^= 0x04000000;
18712 		  break;
18713 
18714 		default:
18715 		  abort ();
18716 		}
18717 	    }
18718 
18719 	  if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18720 	    {
18721 	      /* Clear the and-link bit.  */
18722 	      gas_assert ((insn & 0xfc1c0000) == 0x04100000);
18723 
18724 	      /* bltzal		0x04100000	bgezal	0x04110000
18725 		 bltzall	0x04120000	bgezall	0x04130000  */
18726 	      insn &= ~0x00100000;
18727 	    }
18728 
18729 	  /* Branch over the branch (if the branch was likely) or the
18730 	     full jump (not likely case).  Compute the offset from the
18731 	     current instruction to branch to.  */
18732 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18733 	    i = 16;
18734 	  else
18735 	    {
18736 	      /* How many bytes in instructions we've already emitted?  */
18737 	      i = buf - fragp->fr_literal - fragp->fr_fix;
18738 	      /* How many bytes in instructions from here to the end?  */
18739 	      i = fragp->fr_var - i;
18740 	    }
18741 	  /* Convert to instruction count.  */
18742 	  i >>= 2;
18743 	  /* Branch counts from the next instruction.  */
18744 	  i--;
18745 	  insn |= i;
18746 	  /* Branch over the jump.  */
18747 	  buf = write_insn (buf, insn);
18748 
18749 	  /* nop */
18750 	  buf = write_insn (buf, 0);
18751 
18752 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18753 	    {
18754 	      /* beql $0, $0, 2f */
18755 	      insn = 0x50000000;
18756 	      /* Compute the PC offset from the current instruction to
18757 		 the end of the variable frag.  */
18758 	      /* How many bytes in instructions we've already emitted?  */
18759 	      i = buf - fragp->fr_literal - fragp->fr_fix;
18760 	      /* How many bytes in instructions from here to the end?  */
18761 	      i = fragp->fr_var - i;
18762 	      /* Convert to instruction count.  */
18763 	      i >>= 2;
18764 	      /* Don't decrement i, because we want to branch over the
18765 		 delay slot.  */
18766 	      insn |= i;
18767 
18768 	      buf = write_insn (buf, insn);
18769 	      buf = write_insn (buf, 0);
18770 	    }
18771 
18772 	uncond:
18773 	  if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
18774 	    {
18775 	      /* j or jal.  */
18776 	      insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18777 		      ? 0x0c000000 : 0x08000000);
18778 
18779 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18780 			      fragp->fr_symbol, fragp->fr_offset,
18781 			      false, BFD_RELOC_MIPS_JMP);
18782 	      fixp->fx_file = fragp->fr_file;
18783 	      fixp->fx_line = fragp->fr_line;
18784 
18785 	      buf = write_insn (buf, insn);
18786 	    }
18787 	  else
18788 	    {
18789 	      unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18790 
18791 	      /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
18792 	      insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18793 	      insn |= at << OP_SH_RT;
18794 
18795 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18796 			      fragp->fr_symbol, fragp->fr_offset,
18797 			      false, BFD_RELOC_MIPS_GOT16);
18798 	      fixp->fx_file = fragp->fr_file;
18799 	      fixp->fx_line = fragp->fr_line;
18800 
18801 	      buf = write_insn (buf, insn);
18802 
18803 	      if (mips_opts.isa == ISA_MIPS1)
18804 		/* nop */
18805 		buf = write_insn (buf, 0);
18806 
18807 	      /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
18808 	      insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18809 	      insn |= at << OP_SH_RS | at << OP_SH_RT;
18810 
18811 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18812 			      fragp->fr_symbol, fragp->fr_offset,
18813 			      false, BFD_RELOC_LO16);
18814 	      fixp->fx_file = fragp->fr_file;
18815 	      fixp->fx_line = fragp->fr_line;
18816 
18817 	      buf = write_insn (buf, insn);
18818 
18819 	      /* j(al)r $at.  */
18820 	      if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18821 		insn = 0x0000f809;
18822 	      else
18823 		insn = 0x00000008;
18824 	      insn |= at << OP_SH_RS;
18825 
18826 	      buf = write_insn (buf, insn);
18827 	    }
18828 	}
18829 
18830       fragp->fr_fix += fragp->fr_var;
18831       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18832       return;
18833     }
18834 
18835   /* Relax microMIPS branches.  */
18836   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18837     {
18838       char *buf = fragp->fr_literal + fragp->fr_fix;
18839       bool compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18840       bool insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18841       bool nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18842       bool pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18843       bool al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18844       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18845       bool short_ds;
18846       unsigned long insn;
18847       fixS *fixp;
18848 
18849       fragp->fr_fix += fragp->fr_var;
18850 
18851       /* Handle 16-bit branches that fit or are forced to fit.  */
18852       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18853 	{
18854 	  /* We generate a fixup instead of applying it right now,
18855 	     because if there is linker relaxation, we're going to
18856 	     need the relocations.  */
18857 	  switch (type)
18858 	    {
18859 	    case 'D':
18860 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18861 			      fragp->fr_symbol, fragp->fr_offset,
18862 			      true, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18863 	      break;
18864 	    case 'E':
18865 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18866 			      fragp->fr_symbol, fragp->fr_offset,
18867 			      true, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18868 	      break;
18869 	    default:
18870 	      abort ();
18871 	    }
18872 
18873 	  fixp->fx_file = fragp->fr_file;
18874 	  fixp->fx_line = fragp->fr_line;
18875 
18876 	  /* These relocations can have an addend that won't fit in
18877 	     2 octets.  */
18878 	  fixp->fx_no_overflow = 1;
18879 
18880 	  return;
18881 	}
18882 
18883       /* Handle 32-bit branches that fit or are forced to fit.  */
18884       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18885 	  || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18886 	{
18887 	  /* We generate a fixup instead of applying it right now,
18888 	     because if there is linker relaxation, we're going to
18889 	     need the relocations.  */
18890 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18891 			  fragp->fr_symbol, fragp->fr_offset,
18892 			  true, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18893 	  fixp->fx_file = fragp->fr_file;
18894 	  fixp->fx_line = fragp->fr_line;
18895 
18896 	  if (type == 0)
18897 	    {
18898 	      insn = read_compressed_insn (buf, 4);
18899 	      buf += 4;
18900 
18901 	      if (nods)
18902 		{
18903 		  /* Check the short-delay-slot bit.  */
18904 		  if (!al || (insn & 0x02000000) != 0)
18905 		    buf = write_compressed_insn (buf, 0x0c00, 2);
18906 		  else
18907 		    buf = write_compressed_insn (buf, 0x00000000, 4);
18908 		}
18909 
18910 	      gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18911 	      return;
18912 	    }
18913 	}
18914 
18915       /* Relax 16-bit branches to 32-bit branches.  */
18916       if (type != 0)
18917 	{
18918 	  insn = read_compressed_insn (buf, 2);
18919 
18920 	  if ((insn & 0xfc00) == 0xcc00)		/* b16  */
18921 	    insn = 0x94000000;				/* beq  */
18922 	  else if ((insn & 0xdc00) == 0x8c00)		/* beqz16/bnez16  */
18923 	    {
18924 	      unsigned long regno;
18925 
18926 	      regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18927 	      regno = micromips_to_32_reg_d_map [regno];
18928 	      insn = ((insn & 0x2000) << 16) | 0x94000000;	/* beq/bne  */
18929 	      insn |= regno << MICROMIPSOP_SH_RS;
18930 	    }
18931 	  else
18932 	    abort ();
18933 
18934 	  /* Nothing else to do, just write it out.  */
18935 	  if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18936 	      || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18937 	    {
18938 	      buf = write_compressed_insn (buf, insn, 4);
18939 	      if (nods)
18940 		buf = write_compressed_insn (buf, 0x0c00, 2);
18941 	      gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18942 	      return;
18943 	    }
18944 	}
18945       else
18946 	insn = read_compressed_insn (buf, 4);
18947 
18948       /* Relax 32-bit branches to a sequence of instructions.  */
18949       as_warn_where (fragp->fr_file, fragp->fr_line,
18950 		     _("relaxed out-of-range branch into a jump"));
18951 
18952       /* Set the short-delay-slot bit.  */
18953       short_ds = !al || (insn & 0x02000000) != 0;
18954 
18955       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18956 	{
18957 	  symbolS *l;
18958 
18959 	  /* Reverse the branch.  */
18960 	  if ((insn & 0xfc000000) == 0x94000000			/* beq  */
18961 	      || (insn & 0xfc000000) == 0xb4000000)		/* bne  */
18962 	    insn ^= 0x20000000;
18963 	  else if ((insn & 0xffe00000) == 0x40000000		/* bltz  */
18964 		   || (insn & 0xffe00000) == 0x40400000		/* bgez  */
18965 		   || (insn & 0xffe00000) == 0x40800000		/* blez  */
18966 		   || (insn & 0xffe00000) == 0x40c00000		/* bgtz  */
18967 		   || (insn & 0xffe00000) == 0x40a00000		/* bnezc  */
18968 		   || (insn & 0xffe00000) == 0x40e00000		/* beqzc  */
18969 		   || (insn & 0xffe00000) == 0x40200000		/* bltzal  */
18970 		   || (insn & 0xffe00000) == 0x40600000		/* bgezal  */
18971 		   || (insn & 0xffe00000) == 0x42200000		/* bltzals  */
18972 		   || (insn & 0xffe00000) == 0x42600000)	/* bgezals  */
18973 	    insn ^= 0x00400000;
18974 	  else if ((insn & 0xffe30000) == 0x43800000		/* bc1f  */
18975 		   || (insn & 0xffe30000) == 0x43a00000		/* bc1t  */
18976 		   || (insn & 0xffe30000) == 0x42800000		/* bc2f  */
18977 		   || (insn & 0xffe30000) == 0x42a00000)	/* bc2t  */
18978 	    insn ^= 0x00200000;
18979 	  else if ((insn & 0xff000000) == 0x83000000		/* BZ.df
18980 								   BNZ.df  */
18981 		    || (insn & 0xff600000) == 0x81600000)	/* BZ.V
18982 								   BNZ.V */
18983 	    insn ^= 0x00800000;
18984 	  else
18985 	    abort ();
18986 
18987 	  if (al)
18988 	    {
18989 	      /* Clear the and-link and short-delay-slot bits.  */
18990 	      gas_assert ((insn & 0xfda00000) == 0x40200000);
18991 
18992 	      /* bltzal  0x40200000	bgezal  0x40600000  */
18993 	      /* bltzals 0x42200000	bgezals 0x42600000  */
18994 	      insn &= ~0x02200000;
18995 	    }
18996 
18997 	  /* Make a label at the end for use with the branch.  */
18998 	  l = symbol_new (micromips_label_name (), asec, fragp, fragp->fr_fix);
18999 	  micromips_label_inc ();
19000 	  S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
19001 
19002 	  /* Refer to it.  */
19003 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, true,
19004 			  BFD_RELOC_MICROMIPS_16_PCREL_S1);
19005 	  fixp->fx_file = fragp->fr_file;
19006 	  fixp->fx_line = fragp->fr_line;
19007 
19008 	  /* Branch over the jump.  */
19009 	  buf = write_compressed_insn (buf, insn, 4);
19010 
19011 	  if (!compact)
19012 	    {
19013 	      /* nop  */
19014 	      if (insn32)
19015 		buf = write_compressed_insn (buf, 0x00000000, 4);
19016 	      else
19017 		buf = write_compressed_insn (buf, 0x0c00, 2);
19018 	    }
19019 	}
19020 
19021       if (!pic)
19022 	{
19023 	  unsigned long jal = (short_ds || nods
19024 			       ? 0x74000000 : 0xf4000000);	/* jal/s  */
19025 
19026 	  /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
19027 	  insn = al ? jal : 0xd4000000;
19028 
19029 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19030 			  fragp->fr_symbol, fragp->fr_offset,
19031 			  false, BFD_RELOC_MICROMIPS_JMP);
19032 	  fixp->fx_file = fragp->fr_file;
19033 	  fixp->fx_line = fragp->fr_line;
19034 
19035 	  buf = write_compressed_insn (buf, insn, 4);
19036 
19037 	  if (compact || nods)
19038 	    {
19039 	      /* nop  */
19040 	      if (insn32)
19041 		buf = write_compressed_insn (buf, 0x00000000, 4);
19042 	      else
19043 		buf = write_compressed_insn (buf, 0x0c00, 2);
19044 	    }
19045 	}
19046       else
19047 	{
19048 	  unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
19049 
19050 	  /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
19051 	  insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
19052 	  insn |= at << MICROMIPSOP_SH_RT;
19053 
19054 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19055 			  fragp->fr_symbol, fragp->fr_offset,
19056 			  false, BFD_RELOC_MICROMIPS_GOT16);
19057 	  fixp->fx_file = fragp->fr_file;
19058 	  fixp->fx_line = fragp->fr_line;
19059 
19060 	  buf = write_compressed_insn (buf, insn, 4);
19061 
19062 	  /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
19063 	  insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
19064 	  insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
19065 
19066 	  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19067 			  fragp->fr_symbol, fragp->fr_offset,
19068 			  false, BFD_RELOC_MICROMIPS_LO16);
19069 	  fixp->fx_file = fragp->fr_file;
19070 	  fixp->fx_line = fragp->fr_line;
19071 
19072 	  buf = write_compressed_insn (buf, insn, 4);
19073 
19074 	  if (insn32)
19075 	    {
19076 	      /* jr/jalr $at  */
19077 	      insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
19078 	      insn |= at << MICROMIPSOP_SH_RS;
19079 
19080 	      buf = write_compressed_insn (buf, insn, 4);
19081 
19082 	      if (compact || nods)
19083 		/* nop  */
19084 		buf = write_compressed_insn (buf, 0x00000000, 4);
19085 	    }
19086 	  else
19087 	    {
19088 	      /* jr/jrc/jalr/jalrs $at  */
19089 	      unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;	/* jalr/s  */
19090 	      unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c  */
19091 
19092 	      insn = al ? jalr : jr;
19093 	      insn |= at << MICROMIPSOP_SH_MJ;
19094 
19095 	      buf = write_compressed_insn (buf, insn, 2);
19096 	      if (al && nods)
19097 		{
19098 		  /* nop  */
19099 		  if (short_ds)
19100 		    buf = write_compressed_insn (buf, 0x0c00, 2);
19101 		  else
19102 		    buf = write_compressed_insn (buf, 0x00000000, 4);
19103 		}
19104 	    }
19105 	}
19106 
19107       gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
19108       return;
19109     }
19110 
19111   if (RELAX_MIPS16_P (fragp->fr_subtype))
19112     {
19113       int type;
19114       const struct mips_int_operand *operand;
19115       offsetT val;
19116       char *buf;
19117       unsigned int user_length;
19118       bool need_reloc;
19119       unsigned long insn;
19120       bool mac;
19121       bool ext;
19122       segT symsec;
19123 
19124       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
19125       operand = mips16_immed_operand (type, false);
19126 
19127       mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
19128       ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
19129       val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
19130 
19131       symsec = S_GET_SEGMENT (fragp->fr_symbol);
19132       need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, true)
19133 		    || (operand->root.type == OP_PCREL && !mac
19134 			? asec != symsec
19135 			: !bfd_is_abs_section (symsec)));
19136 
19137       if (operand->root.type == OP_PCREL && !mac)
19138 	{
19139 	  const struct mips_pcrel_operand *pcrel_op;
19140 
19141 	  pcrel_op = (const struct mips_pcrel_operand *) operand;
19142 
19143 	  if (pcrel_op->include_isa_bit && !need_reloc)
19144 	    {
19145 	      if (!mips_ignore_branch_isa
19146 		  && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
19147 		as_bad_where (fragp->fr_file, fragp->fr_line,
19148 			      _("branch to a symbol in another ISA mode"));
19149 	      else if ((fragp->fr_offset & 0x1) != 0)
19150 		as_bad_where (fragp->fr_file, fragp->fr_line,
19151 			      _("branch to misaligned address (0x%lx)"),
19152 			      (long) (resolve_symbol_value (fragp->fr_symbol)
19153 				      + (fragp->fr_offset & ~1)));
19154 	    }
19155 
19156 	  val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
19157 
19158 	  /* Make sure the section winds up with the alignment we have
19159              assumed.  */
19160 	  if (operand->shift > 0)
19161 	    record_alignment (asec, operand->shift);
19162 	}
19163 
19164       if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19165 	  || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19166 	{
19167 	  if (mac)
19168 	    as_warn_where (fragp->fr_file, fragp->fr_line,
19169 			   _("macro instruction expanded into multiple "
19170 			     "instructions in a branch delay slot"));
19171 	  else if (ext)
19172 	    as_warn_where (fragp->fr_file, fragp->fr_line,
19173 			   _("extended instruction in a branch delay slot"));
19174 	}
19175       else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
19176 	as_warn_where (fragp->fr_file, fragp->fr_line,
19177 		       _("macro instruction expanded into multiple "
19178 			 "instructions"));
19179 
19180       buf = fragp->fr_literal + fragp->fr_fix;
19181 
19182       insn = read_compressed_insn (buf, 2);
19183       if (ext)
19184 	insn |= MIPS16_EXTEND;
19185 
19186       if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19187 	user_length = 4;
19188       else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19189 	user_length = 2;
19190       else
19191 	user_length = 0;
19192 
19193       if (mac)
19194 	{
19195 	  unsigned long reg;
19196 	  unsigned long new;
19197 	  unsigned long op;
19198 	  bool e2;
19199 
19200 	  gas_assert (type == 'A' || type == 'B' || type == 'E');
19201 	  gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
19202 
19203 	  e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19204 
19205 	  if (need_reloc)
19206 	    {
19207 	      fixS *fixp;
19208 
19209 	      gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19210 
19211 	      fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19212 			      fragp->fr_symbol, fragp->fr_offset,
19213 			      false, BFD_RELOC_MIPS16_HI16_S);
19214 	      fixp->fx_file = fragp->fr_file;
19215 	      fixp->fx_line = fragp->fr_line;
19216 
19217 	      fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
19218 			      fragp->fr_symbol, fragp->fr_offset,
19219 			      false, BFD_RELOC_MIPS16_LO16);
19220 	      fixp->fx_file = fragp->fr_file;
19221 	      fixp->fx_line = fragp->fr_line;
19222 
19223 	      val = 0;
19224 	    }
19225 
19226 	  switch (insn & 0xf800)
19227 	    {
19228 	    case 0x0800:					/* ADDIU */
19229 	      reg = (insn >> 8) & 0x7;
19230 	      op = 0xf0004800 | (reg << 8);
19231 	      break;
19232 	    case 0xb000:					/* LW */
19233 	      reg = (insn >> 8) & 0x7;
19234 	      op = 0xf0009800 | (reg << 8) | (reg << 5);
19235 	      break;
19236 	    case 0xf800:					/* I64 */
19237 	      reg = (insn >> 5) & 0x7;
19238 	      switch (insn & 0x0700)
19239 		{
19240 		case 0x0400:					/* LD */
19241 		  op = 0xf0003800 | (reg << 8) | (reg << 5);
19242 		  break;
19243 		case 0x0600:					/* DADDIU */
19244 		  op = 0xf000fd00 | (reg << 5);
19245 		  break;
19246 		default:
19247 		  abort ();
19248 		}
19249 	      break;
19250 	    default:
19251 	      abort ();
19252 	    }
19253 
19254 	  new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8);	/* LUI/LI */
19255 	  new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19256 	  buf = write_compressed_insn (buf, new, 4);
19257 	  if (!e2)
19258 	    {
19259 	      new = 0xf4003000 | (reg << 8) | (reg << 5);	/* SLL */
19260 	      buf = write_compressed_insn (buf, new, 4);
19261 	    }
19262 	  op |= mips16_immed_extend (val, 16);
19263 	  buf = write_compressed_insn (buf, op, 4);
19264 
19265 	  fragp->fr_fix += e2 ? 8 : 12;
19266 	}
19267       else
19268 	{
19269 	  unsigned int length = ext ? 4 : 2;
19270 
19271 	  if (need_reloc)
19272 	    {
19273 	      bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
19274 	      fixS *fixp;
19275 
19276 	      switch (type)
19277 		{
19278 		case 'p':
19279 		case 'q':
19280 		  reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19281 		  break;
19282 		default:
19283 		  break;
19284 		}
19285 	      if (mac || reloc == BFD_RELOC_NONE)
19286 		as_bad_where (fragp->fr_file, fragp->fr_line,
19287 			      _("unsupported relocation"));
19288 	      else if (ext)
19289 		{
19290 		  fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19291 				  fragp->fr_symbol, fragp->fr_offset,
19292 				  true, reloc);
19293 		  fixp->fx_file = fragp->fr_file;
19294 		  fixp->fx_line = fragp->fr_line;
19295 		}
19296 	      else
19297 		as_bad_where (fragp->fr_file, fragp->fr_line,
19298 			      _("invalid unextended operand value"));
19299 	    }
19300 	  else
19301 	    mips16_immed (fragp->fr_file, fragp->fr_line, type,
19302 			  BFD_RELOC_UNUSED, val, user_length, &insn);
19303 
19304 	  gas_assert (mips16_opcode_length (insn) == length);
19305 	  write_compressed_insn (buf, insn, length);
19306 	  fragp->fr_fix += length;
19307 	}
19308     }
19309   else
19310     {
19311       relax_substateT subtype = fragp->fr_subtype;
19312       bool second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19313       bool use_second = (subtype & RELAX_USE_SECOND) != 0;
19314       unsigned int first, second;
19315       fixS *fixp;
19316 
19317       first = RELAX_FIRST (subtype);
19318       second = RELAX_SECOND (subtype);
19319       fixp = (fixS *) fragp->fr_opcode;
19320 
19321       /* If the delay slot chosen does not match the size of the instruction,
19322          then emit a warning.  */
19323       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19324 	   || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19325 	{
19326 	  relax_substateT s;
19327 	  const char *msg;
19328 
19329 	  s = subtype & (RELAX_DELAY_SLOT_16BIT
19330 			 | RELAX_DELAY_SLOT_SIZE_FIRST
19331 			 | RELAX_DELAY_SLOT_SIZE_SECOND);
19332 	  msg = macro_warning (s);
19333 	  if (msg != NULL)
19334 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19335 	  subtype &= ~s;
19336 	}
19337 
19338       /* Possibly emit a warning if we've chosen the longer option.  */
19339       if (use_second == second_longer)
19340 	{
19341 	  relax_substateT s;
19342 	  const char *msg;
19343 
19344 	  s = (subtype
19345 	       & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19346 	  msg = macro_warning (s);
19347 	  if (msg != NULL)
19348 	    as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
19349 	  subtype &= ~s;
19350 	}
19351 
19352       /* Go through all the fixups for the first sequence.  Disable them
19353 	 (by marking them as done) if we're going to use the second
19354 	 sequence instead.  */
19355       while (fixp
19356 	     && fixp->fx_frag == fragp
19357 	     && fixp->fx_where + second < fragp->fr_fix)
19358 	{
19359 	  if (subtype & RELAX_USE_SECOND)
19360 	    fixp->fx_done = 1;
19361 	  fixp = fixp->fx_next;
19362 	}
19363 
19364       /* Go through the fixups for the second sequence.  Disable them if
19365 	 we're going to use the first sequence, otherwise adjust their
19366 	 addresses to account for the relaxation.  */
19367       while (fixp && fixp->fx_frag == fragp)
19368 	{
19369 	  if (subtype & RELAX_USE_SECOND)
19370 	    fixp->fx_where -= first;
19371 	  else
19372 	    fixp->fx_done = 1;
19373 	  fixp = fixp->fx_next;
19374 	}
19375 
19376       /* Now modify the frag contents.  */
19377       if (subtype & RELAX_USE_SECOND)
19378 	{
19379 	  char *start;
19380 
19381 	  start = fragp->fr_literal + fragp->fr_fix - first - second;
19382 	  memmove (start, start + first, second);
19383 	  fragp->fr_fix -= first;
19384 	}
19385       else
19386 	fragp->fr_fix -= second;
19387     }
19388 }
19389 
19390 /* This function is called after the relocs have been generated.
19391    We've been storing mips16 text labels as odd.  Here we convert them
19392    back to even for the convenience of the debugger.  */
19393 
19394 void
19395 mips_frob_file_after_relocs (void)
19396 {
19397   asymbol **syms;
19398   unsigned int count, i;
19399 
19400   syms = bfd_get_outsymbols (stdoutput);
19401   count = bfd_get_symcount (stdoutput);
19402   for (i = 0; i < count; i++, syms++)
19403     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19404 	&& ((*syms)->value & 1) != 0)
19405       {
19406 	(*syms)->value &= ~1;
19407 	/* If the symbol has an odd size, it was probably computed
19408 	   incorrectly, so adjust that as well.  */
19409 	if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19410 	  ++elf_symbol (*syms)->internal_elf_sym.st_size;
19411       }
19412 }
19413 
19414 /* This function is called whenever a label is defined, including fake
19415    labels instantiated off the dot special symbol.  It is used when
19416    handling branch delays; if a branch has a label, we assume we cannot
19417    move it.  This also bumps the value of the symbol by 1 in compressed
19418    code.  */
19419 
19420 static void
19421 mips_record_label (symbolS *sym)
19422 {
19423   segment_info_type *si = seg_info (now_seg);
19424   struct insn_label_list *l;
19425 
19426   if (free_insn_labels == NULL)
19427     l = XNEW (struct insn_label_list);
19428   else
19429     {
19430       l = free_insn_labels;
19431       free_insn_labels = l->next;
19432     }
19433 
19434   l->label = sym;
19435   l->next = si->label_list;
19436   si->label_list = l;
19437 }
19438 
19439 /* This function is called as tc_frob_label() whenever a label is defined
19440    and adds a DWARF-2 record we only want for true labels.  */
19441 
19442 void
19443 mips_define_label (symbolS *sym)
19444 {
19445   mips_record_label (sym);
19446   dwarf2_emit_label (sym);
19447 }
19448 
19449 /* This function is called by tc_new_dot_label whenever a new dot symbol
19450    is defined.  */
19451 
19452 void
19453 mips_add_dot_label (symbolS *sym)
19454 {
19455   mips_record_label (sym);
19456   if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19457     mips_compressed_mark_label (sym);
19458 }
19459 
19460 /* Converting ASE flags from internal to .MIPS.abiflags values.  */
19461 static unsigned int
19462 mips_convert_ase_flags (int ase)
19463 {
19464   unsigned int ext_ases = 0;
19465 
19466   if (ase & ASE_DSP)
19467     ext_ases |= AFL_ASE_DSP;
19468   if (ase & ASE_DSPR2)
19469     ext_ases |= AFL_ASE_DSPR2;
19470   if (ase & ASE_DSPR3)
19471     ext_ases |= AFL_ASE_DSPR3;
19472   if (ase & ASE_EVA)
19473     ext_ases |= AFL_ASE_EVA;
19474   if (ase & ASE_MCU)
19475     ext_ases |= AFL_ASE_MCU;
19476   if (ase & ASE_MDMX)
19477     ext_ases |= AFL_ASE_MDMX;
19478   if (ase & ASE_MIPS3D)
19479     ext_ases |= AFL_ASE_MIPS3D;
19480   if (ase & ASE_MT)
19481     ext_ases |= AFL_ASE_MT;
19482   if (ase & ASE_SMARTMIPS)
19483     ext_ases |= AFL_ASE_SMARTMIPS;
19484   if (ase & ASE_VIRT)
19485     ext_ases |= AFL_ASE_VIRT;
19486   if (ase & ASE_MSA)
19487     ext_ases |= AFL_ASE_MSA;
19488   if (ase & ASE_XPA)
19489     ext_ases |= AFL_ASE_XPA;
19490   if (ase & ASE_MIPS16E2)
19491     ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
19492   if (ase & ASE_CRC)
19493     ext_ases |= AFL_ASE_CRC;
19494   if (ase & ASE_GINV)
19495     ext_ases |= AFL_ASE_GINV;
19496   if (ase & ASE_LOONGSON_MMI)
19497     ext_ases |= AFL_ASE_LOONGSON_MMI;
19498   if (ase & ASE_LOONGSON_CAM)
19499     ext_ases |= AFL_ASE_LOONGSON_CAM;
19500   if (ase & ASE_LOONGSON_EXT)
19501     ext_ases |= AFL_ASE_LOONGSON_EXT;
19502   if (ase & ASE_LOONGSON_EXT2)
19503     ext_ases |= AFL_ASE_LOONGSON_EXT2;
19504 
19505   return ext_ases;
19506 }
19507 /* Some special processing for a MIPS ELF file.  */
19508 
19509 void
19510 mips_elf_final_processing (void)
19511 {
19512   int fpabi;
19513   Elf_Internal_ABIFlags_v0 flags;
19514 
19515   flags.version = 0;
19516   flags.isa_rev = 0;
19517   switch (file_mips_opts.isa)
19518     {
19519     case INSN_ISA1:
19520       flags.isa_level = 1;
19521       break;
19522     case INSN_ISA2:
19523       flags.isa_level = 2;
19524       break;
19525     case INSN_ISA3:
19526       flags.isa_level = 3;
19527       break;
19528     case INSN_ISA4:
19529       flags.isa_level = 4;
19530       break;
19531     case INSN_ISA5:
19532       flags.isa_level = 5;
19533       break;
19534     case INSN_ISA32:
19535       flags.isa_level = 32;
19536       flags.isa_rev = 1;
19537       break;
19538     case INSN_ISA32R2:
19539       flags.isa_level = 32;
19540       flags.isa_rev = 2;
19541       break;
19542     case INSN_ISA32R3:
19543       flags.isa_level = 32;
19544       flags.isa_rev = 3;
19545       break;
19546     case INSN_ISA32R5:
19547       flags.isa_level = 32;
19548       flags.isa_rev = 5;
19549       break;
19550     case INSN_ISA32R6:
19551       flags.isa_level = 32;
19552       flags.isa_rev = 6;
19553       break;
19554     case INSN_ISA64:
19555       flags.isa_level = 64;
19556       flags.isa_rev = 1;
19557       break;
19558     case INSN_ISA64R2:
19559       flags.isa_level = 64;
19560       flags.isa_rev = 2;
19561       break;
19562     case INSN_ISA64R3:
19563       flags.isa_level = 64;
19564       flags.isa_rev = 3;
19565       break;
19566     case INSN_ISA64R5:
19567       flags.isa_level = 64;
19568       flags.isa_rev = 5;
19569       break;
19570     case INSN_ISA64R6:
19571       flags.isa_level = 64;
19572       flags.isa_rev = 6;
19573       break;
19574     }
19575 
19576   flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19577   flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19578 		    : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19579 		    : (file_mips_opts.fp == 64) ? AFL_REG_64
19580 		    : AFL_REG_32;
19581   flags.cpr2_size = AFL_REG_NONE;
19582   flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19583                                            Tag_GNU_MIPS_ABI_FP);
19584   flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19585   flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19586   if (file_ase_mips16)
19587     flags.ases |= AFL_ASE_MIPS16;
19588   if (file_ase_micromips)
19589     flags.ases |= AFL_ASE_MICROMIPS;
19590   flags.flags1 = 0;
19591   if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19592        || file_mips_opts.fp == 64)
19593       && file_mips_opts.oddspreg)
19594     flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19595   flags.flags2 = 0;
19596 
19597   bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19598 				     ((Elf_External_ABIFlags_v0 *)
19599 				     mips_flags_frag));
19600 
19601   /* Write out the register information.  */
19602   if (mips_abi != N64_ABI)
19603     {
19604       Elf32_RegInfo s;
19605 
19606       s.ri_gprmask = mips_gprmask;
19607       s.ri_cprmask[0] = mips_cprmask[0];
19608       s.ri_cprmask[1] = mips_cprmask[1];
19609       s.ri_cprmask[2] = mips_cprmask[2];
19610       s.ri_cprmask[3] = mips_cprmask[3];
19611       /* The gp_value field is set by the MIPS ELF backend.  */
19612 
19613       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19614 				       ((Elf32_External_RegInfo *)
19615 					mips_regmask_frag));
19616     }
19617   else
19618     {
19619       Elf64_Internal_RegInfo s;
19620 
19621       s.ri_gprmask = mips_gprmask;
19622       s.ri_pad = 0;
19623       s.ri_cprmask[0] = mips_cprmask[0];
19624       s.ri_cprmask[1] = mips_cprmask[1];
19625       s.ri_cprmask[2] = mips_cprmask[2];
19626       s.ri_cprmask[3] = mips_cprmask[3];
19627       /* The gp_value field is set by the MIPS ELF backend.  */
19628 
19629       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19630 				       ((Elf64_External_RegInfo *)
19631 					mips_regmask_frag));
19632     }
19633 
19634   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
19635      sort of BFD interface for this.  */
19636   if (mips_any_noreorder)
19637     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19638   if (mips_pic != NO_PIC)
19639     {
19640       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
19641       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19642     }
19643   if (mips_abicalls)
19644     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19645 
19646   /* Set MIPS ELF flags for ASEs.  Note that not all ASEs have flags
19647      defined at present; this might need to change in future.  */
19648   if (file_ase_mips16)
19649     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
19650   if (file_ase_micromips)
19651     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
19652   if (file_mips_opts.ase & ASE_MDMX)
19653     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
19654 
19655   /* Set the MIPS ELF ABI flags.  */
19656   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
19657     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
19658   else if (mips_abi == O64_ABI)
19659     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
19660   else if (mips_abi == EABI_ABI)
19661     {
19662       if (file_mips_opts.gp == 64)
19663 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19664       else
19665 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19666     }
19667 
19668   /* Nothing to do for N32_ABI or N64_ABI.  */
19669 
19670   if (mips_32bitmode)
19671     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
19672 
19673   if (mips_nan2008 == 1)
19674     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19675 
19676   /* 32 bit code with 64 bit FP registers.  */
19677   fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19678 				    Tag_GNU_MIPS_ABI_FP);
19679   if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
19680     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
19681 }
19682 
19683 typedef struct proc {
19684   symbolS *func_sym;
19685   symbolS *func_end_sym;
19686   unsigned long reg_mask;
19687   unsigned long reg_offset;
19688   unsigned long fpreg_mask;
19689   unsigned long fpreg_offset;
19690   unsigned long frame_offset;
19691   unsigned long frame_reg;
19692   unsigned long pc_reg;
19693 } procS;
19694 
19695 static procS cur_proc;
19696 static procS *cur_proc_ptr;
19697 static int numprocs;
19698 
19699 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
19700    as "2", and a normal nop as "0".  */
19701 
19702 #define NOP_OPCODE_MIPS		0
19703 #define NOP_OPCODE_MIPS16	1
19704 #define NOP_OPCODE_MICROMIPS	2
19705 
19706 char
19707 mips_nop_opcode (void)
19708 {
19709   if (seg_info (now_seg)->tc_segment_info_data.micromips)
19710     return NOP_OPCODE_MICROMIPS;
19711   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19712     return NOP_OPCODE_MIPS16;
19713   else
19714     return NOP_OPCODE_MIPS;
19715 }
19716 
19717 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
19718    32-bit microMIPS NOPs here (if applicable).  */
19719 
19720 void
19721 mips_handle_align (fragS *fragp)
19722 {
19723   char nop_opcode;
19724   char *p;
19725   int bytes, size, excess;
19726   valueT opcode;
19727 
19728   if (fragp->fr_type != rs_align_code)
19729     return;
19730 
19731   p = fragp->fr_literal + fragp->fr_fix;
19732   nop_opcode = *p;
19733   switch (nop_opcode)
19734     {
19735     case NOP_OPCODE_MICROMIPS:
19736       opcode = micromips_nop32_insn.insn_opcode;
19737       size = 4;
19738       break;
19739     case NOP_OPCODE_MIPS16:
19740       opcode = mips16_nop_insn.insn_opcode;
19741       size = 2;
19742       break;
19743     case NOP_OPCODE_MIPS:
19744     default:
19745       opcode = nop_insn.insn_opcode;
19746       size = 4;
19747       break;
19748     }
19749 
19750   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19751   excess = bytes % size;
19752 
19753   /* Handle the leading part if we're not inserting a whole number of
19754      instructions, and make it the end of the fixed part of the frag.
19755      Try to fit in a short microMIPS NOP if applicable and possible,
19756      and use zeroes otherwise.  */
19757   gas_assert (excess < 4);
19758   fragp->fr_fix += excess;
19759   switch (excess)
19760     {
19761     case 3:
19762       *p++ = '\0';
19763       /* Fall through.  */
19764     case 2:
19765       if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
19766 	{
19767 	  p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
19768 	  break;
19769 	}
19770       *p++ = '\0';
19771       /* Fall through.  */
19772     case 1:
19773       *p++ = '\0';
19774       /* Fall through.  */
19775     case 0:
19776       break;
19777     }
19778 
19779   md_number_to_chars (p, opcode, size);
19780   fragp->fr_var = size;
19781 }
19782 
19783 static long
19784 get_number (void)
19785 {
19786   int negative = 0;
19787   long val = 0;
19788 
19789   if (*input_line_pointer == '-')
19790     {
19791       ++input_line_pointer;
19792       negative = 1;
19793     }
19794   if (!ISDIGIT (*input_line_pointer))
19795     as_bad (_("expected simple number"));
19796   if (input_line_pointer[0] == '0')
19797     {
19798       if (input_line_pointer[1] == 'x')
19799 	{
19800 	  input_line_pointer += 2;
19801 	  while (ISXDIGIT (*input_line_pointer))
19802 	    {
19803 	      val <<= 4;
19804 	      val |= hex_value (*input_line_pointer++);
19805 	    }
19806 	  return negative ? -val : val;
19807 	}
19808       else
19809 	{
19810 	  ++input_line_pointer;
19811 	  while (ISDIGIT (*input_line_pointer))
19812 	    {
19813 	      val <<= 3;
19814 	      val |= *input_line_pointer++ - '0';
19815 	    }
19816 	  return negative ? -val : val;
19817 	}
19818     }
19819   if (!ISDIGIT (*input_line_pointer))
19820     {
19821       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19822 	      *input_line_pointer, *input_line_pointer);
19823       as_warn (_("invalid number"));
19824       return -1;
19825     }
19826   while (ISDIGIT (*input_line_pointer))
19827     {
19828       val *= 10;
19829       val += *input_line_pointer++ - '0';
19830     }
19831   return negative ? -val : val;
19832 }
19833 
19834 /* The .file directive; just like the usual .file directive, but there
19835    is an initial number which is the ECOFF file index.  In the non-ECOFF
19836    case .file implies DWARF-2.  */
19837 
19838 static void
19839 s_mips_file (int x ATTRIBUTE_UNUSED)
19840 {
19841   static int first_file_directive = 0;
19842 
19843   if (ECOFF_DEBUGGING)
19844     {
19845       get_number ();
19846       s_file (0);
19847     }
19848   else
19849     {
19850       char *filename;
19851 
19852       filename = dwarf2_directive_filename ();
19853 
19854       /* Versions of GCC up to 3.1 start files with a ".file"
19855 	 directive even for stabs output.  Make sure that this
19856 	 ".file" is handled.  Note that you need a version of GCC
19857          after 3.1 in order to support DWARF-2 on MIPS.  */
19858       if (filename != NULL && ! first_file_directive)
19859 	{
19860 	  new_logical_line (filename, -1);
19861 	  s_file_string (filename);
19862 	}
19863       first_file_directive = 1;
19864     }
19865 }
19866 
19867 /* The .loc directive, implying DWARF-2.  */
19868 
19869 static void
19870 s_mips_loc (int x ATTRIBUTE_UNUSED)
19871 {
19872   if (!ECOFF_DEBUGGING)
19873     dwarf2_directive_loc (0);
19874 }
19875 
19876 /* The .end directive.  */
19877 
19878 static void
19879 s_mips_end (int x ATTRIBUTE_UNUSED)
19880 {
19881   symbolS *p;
19882 
19883   /* Following functions need their own .frame and .cprestore directives.  */
19884   mips_frame_reg_valid = 0;
19885   mips_cprestore_valid = 0;
19886 
19887   if (!is_end_of_line[(unsigned char) *input_line_pointer])
19888     {
19889       p = get_symbol ();
19890       demand_empty_rest_of_line ();
19891     }
19892   else
19893     p = NULL;
19894 
19895   if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
19896     as_warn (_(".end not in text section"));
19897 
19898   if (!cur_proc_ptr)
19899     {
19900       as_warn (_(".end directive without a preceding .ent directive"));
19901       demand_empty_rest_of_line ();
19902       return;
19903     }
19904 
19905   if (p != NULL)
19906     {
19907       gas_assert (S_GET_NAME (p));
19908       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
19909 	as_warn (_(".end symbol does not match .ent symbol"));
19910 
19911       if (debug_type == DEBUG_STABS)
19912 	stabs_generate_asm_endfunc (S_GET_NAME (p),
19913 				    S_GET_NAME (p));
19914     }
19915   else
19916     as_warn (_(".end directive missing or unknown symbol"));
19917 
19918   /* Create an expression to calculate the size of the function.  */
19919   if (p && cur_proc_ptr)
19920     {
19921       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19922       expressionS *exp = XNEW (expressionS);
19923 
19924       obj->size = exp;
19925       exp->X_op = O_subtract;
19926       exp->X_add_symbol = symbol_temp_new_now ();
19927       exp->X_op_symbol = p;
19928       exp->X_add_number = 0;
19929 
19930       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19931     }
19932 
19933 #ifdef md_flush_pending_output
19934   md_flush_pending_output ();
19935 #endif
19936 
19937   /* Generate a .pdr section.  */
19938   if (!ECOFF_DEBUGGING && mips_flag_pdr)
19939     {
19940       segT saved_seg = now_seg;
19941       subsegT saved_subseg = now_subseg;
19942       expressionS exp;
19943       char *fragp;
19944 
19945       gas_assert (pdr_seg);
19946       subseg_set (pdr_seg, 0);
19947 
19948       /* Write the symbol.  */
19949       exp.X_op = O_symbol;
19950       exp.X_add_symbol = p;
19951       exp.X_add_number = 0;
19952       emit_expr (&exp, 4);
19953 
19954       fragp = frag_more (7 * 4);
19955 
19956       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19957       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19958       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19959       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19960       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19961       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19962       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
19963 
19964       subseg_set (saved_seg, saved_subseg);
19965     }
19966 
19967   cur_proc_ptr = NULL;
19968 }
19969 
19970 /* The .aent and .ent directives.  */
19971 
19972 static void
19973 s_mips_ent (int aent)
19974 {
19975   symbolS *symbolP;
19976 
19977   symbolP = get_symbol ();
19978   if (*input_line_pointer == ',')
19979     ++input_line_pointer;
19980   SKIP_WHITESPACE ();
19981   if (ISDIGIT (*input_line_pointer)
19982       || *input_line_pointer == '-')
19983     get_number ();
19984 
19985   if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
19986     as_warn (_(".ent or .aent not in text section"));
19987 
19988   if (!aent && cur_proc_ptr)
19989     as_warn (_("missing .end"));
19990 
19991   if (!aent)
19992     {
19993       /* This function needs its own .frame and .cprestore directives.  */
19994       mips_frame_reg_valid = 0;
19995       mips_cprestore_valid = 0;
19996 
19997       cur_proc_ptr = &cur_proc;
19998       memset (cur_proc_ptr, '\0', sizeof (procS));
19999 
20000       cur_proc_ptr->func_sym = symbolP;
20001 
20002       ++numprocs;
20003 
20004       if (debug_type == DEBUG_STABS)
20005         stabs_generate_asm_func (S_GET_NAME (symbolP),
20006 				 S_GET_NAME (symbolP));
20007     }
20008 
20009   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
20010 
20011   demand_empty_rest_of_line ();
20012 }
20013 
20014 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
20015    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
20016    s_mips_frame is used so that we can set the PDR information correctly.
20017    We can't use the ecoff routines because they make reference to the ecoff
20018    symbol table (in the mdebug section).  */
20019 
20020 static void
20021 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
20022 {
20023   if (ECOFF_DEBUGGING)
20024     s_ignore (ignore);
20025   else
20026     {
20027       long val;
20028 
20029       if (cur_proc_ptr == (procS *) NULL)
20030 	{
20031 	  as_warn (_(".frame outside of .ent"));
20032 	  demand_empty_rest_of_line ();
20033 	  return;
20034 	}
20035 
20036       cur_proc_ptr->frame_reg = tc_get_register (1);
20037 
20038       SKIP_WHITESPACE ();
20039       if (*input_line_pointer++ != ','
20040 	  || get_absolute_expression_and_terminator (&val) != ',')
20041 	{
20042 	  as_warn (_("bad .frame directive"));
20043 	  --input_line_pointer;
20044 	  demand_empty_rest_of_line ();
20045 	  return;
20046 	}
20047 
20048       cur_proc_ptr->frame_offset = val;
20049       cur_proc_ptr->pc_reg = tc_get_register (0);
20050 
20051       demand_empty_rest_of_line ();
20052     }
20053 }
20054 
20055 /* The .fmask and .mask directives. If the mdebug section is present
20056    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
20057    embedded targets, s_mips_mask is used so that we can set the PDR
20058    information correctly. We can't use the ecoff routines because they
20059    make reference to the ecoff symbol table (in the mdebug section).  */
20060 
20061 static void
20062 s_mips_mask (int reg_type)
20063 {
20064   if (ECOFF_DEBUGGING)
20065     s_ignore (reg_type);
20066   else
20067     {
20068       long mask, off;
20069 
20070       if (cur_proc_ptr == (procS *) NULL)
20071 	{
20072 	  as_warn (_(".mask/.fmask outside of .ent"));
20073 	  demand_empty_rest_of_line ();
20074 	  return;
20075 	}
20076 
20077       if (get_absolute_expression_and_terminator (&mask) != ',')
20078 	{
20079 	  as_warn (_("bad .mask/.fmask directive"));
20080 	  --input_line_pointer;
20081 	  demand_empty_rest_of_line ();
20082 	  return;
20083 	}
20084 
20085       off = get_absolute_expression ();
20086 
20087       if (reg_type == 'F')
20088 	{
20089 	  cur_proc_ptr->fpreg_mask = mask;
20090 	  cur_proc_ptr->fpreg_offset = off;
20091 	}
20092       else
20093 	{
20094 	  cur_proc_ptr->reg_mask = mask;
20095 	  cur_proc_ptr->reg_offset = off;
20096 	}
20097 
20098       demand_empty_rest_of_line ();
20099     }
20100 }
20101 
20102 /* A table describing all the processors gas knows about.  Names are
20103    matched in the order listed.
20104 
20105    To ease comparison, please keep this table in the same order as
20106    gcc's mips_cpu_info_table[].  */
20107 static const struct mips_cpu_info mips_cpu_info_table[] =
20108 {
20109   /* Entries for generic ISAs.  */
20110   { "mips1",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS1,    CPU_R3000 },
20111   { "mips2",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS2,    CPU_R6000 },
20112   { "mips3",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS3,    CPU_R4000 },
20113   { "mips4",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS4,    CPU_R8000 },
20114   { "mips5",          MIPS_CPU_IS_ISA, 0,	ISA_MIPS5,    CPU_MIPS5 },
20115   { "mips32",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS32,   CPU_MIPS32 },
20116   { "mips32r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R2, CPU_MIPS32R2 },
20117   { "mips32r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R3, CPU_MIPS32R3 },
20118   { "mips32r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R5, CPU_MIPS32R5 },
20119   { "mips32r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS32R6, CPU_MIPS32R6 },
20120   { "mips64",         MIPS_CPU_IS_ISA, 0,	ISA_MIPS64,   CPU_MIPS64 },
20121   { "mips64r2",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R2, CPU_MIPS64R2 },
20122   { "mips64r3",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R3, CPU_MIPS64R3 },
20123   { "mips64r5",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R5, CPU_MIPS64R5 },
20124   { "mips64r6",       MIPS_CPU_IS_ISA, 0,	ISA_MIPS64R6, CPU_MIPS64R6 },
20125 
20126   /* MIPS I */
20127   { "r3000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
20128   { "r2000",          0, 0,			ISA_MIPS1,    CPU_R3000 },
20129   { "r3900",          0, 0,			ISA_MIPS1,    CPU_R3900 },
20130 
20131   /* MIPS II */
20132   { "r6000",          0, 0,			ISA_MIPS2,    CPU_R6000 },
20133 
20134   /* MIPS III */
20135   { "r4000",          0, 0,			ISA_MIPS3,    CPU_R4000 },
20136   { "r4010",          0, 0,			ISA_MIPS2,    CPU_R4010 },
20137   { "vr4100",         0, 0,			ISA_MIPS3,    CPU_VR4100 },
20138   { "vr4111",         0, 0,			ISA_MIPS3,    CPU_R4111 },
20139   { "vr4120",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
20140   { "vr4130",         0, 0,			ISA_MIPS3,    CPU_VR4120 },
20141   { "vr4181",         0, 0,			ISA_MIPS3,    CPU_R4111 },
20142   { "vr4300",         0, 0,			ISA_MIPS3,    CPU_R4300 },
20143   { "r4400",          0, 0,			ISA_MIPS3,    CPU_R4400 },
20144   { "r4600",          0, 0,			ISA_MIPS3,    CPU_R4600 },
20145   { "orion",          0, 0,			ISA_MIPS3,    CPU_R4600 },
20146   { "r4650",          0, 0,			ISA_MIPS3,    CPU_R4650 },
20147   { "r5900",          0, 0,			ISA_MIPS3,    CPU_R5900 },
20148   /* ST Microelectronics Loongson 2E and 2F cores.  */
20149   { "loongson2e",     0, 0,			ISA_MIPS3,    CPU_LOONGSON_2E },
20150   { "loongson2f",     0, ASE_LOONGSON_MMI,	ISA_MIPS3,    CPU_LOONGSON_2F },
20151 
20152   /* MIPS IV */
20153   { "r8000",          0, 0,			ISA_MIPS4,    CPU_R8000 },
20154   { "r10000",         0, 0,			ISA_MIPS4,    CPU_R10000 },
20155   { "r12000",         0, 0,			ISA_MIPS4,    CPU_R12000 },
20156   { "r14000",         0, 0,			ISA_MIPS4,    CPU_R14000 },
20157   { "r16000",         0, 0,			ISA_MIPS4,    CPU_R16000 },
20158   { "vr5000",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20159   { "vr5400",         0, 0,			ISA_MIPS4,    CPU_VR5400 },
20160   { "vr5500",         0, 0,			ISA_MIPS4,    CPU_VR5500 },
20161   { "rm5200",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20162   { "rm5230",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20163   { "rm5231",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20164   { "rm5261",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20165   { "rm5721",         0, 0,			ISA_MIPS4,    CPU_R5000 },
20166   { "rm7000",         0, 0,			ISA_MIPS4,    CPU_RM7000 },
20167   { "rm9000",         0, 0,			ISA_MIPS4,    CPU_RM9000 },
20168 
20169   /* MIPS 32 */
20170   { "4kc",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
20171   { "4km",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
20172   { "4kp",            0, 0,			ISA_MIPS32,   CPU_MIPS32 },
20173   { "4ksc",           0, ASE_SMARTMIPS,		ISA_MIPS32,   CPU_MIPS32 },
20174 
20175   /* MIPS 32 Release 2 */
20176   { "4kec",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20177   { "4kem",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20178   { "4kep",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20179   { "4ksd",           0, ASE_SMARTMIPS,		ISA_MIPS32R2, CPU_MIPS32R2 },
20180   { "m4k",            0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20181   { "m4kp",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20182   { "m14k",           0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
20183   { "m14kc",          0, ASE_MCU,		ISA_MIPS32R2, CPU_MIPS32R2 },
20184   { "m14ke",          0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20185 						ISA_MIPS32R2, CPU_MIPS32R2 },
20186   { "m14kec",         0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
20187 						ISA_MIPS32R2, CPU_MIPS32R2 },
20188   { "24kc",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20189   { "24kf2_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20190   { "24kf",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20191   { "24kf1_1",        0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20192   /* Deprecated forms of the above.  */
20193   { "24kfx",          0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20194   { "24kx",           0, 0,			ISA_MIPS32R2, CPU_MIPS32R2 },
20195   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
20196   { "24kec",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20197   { "24kef2_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20198   { "24kef",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20199   { "24kef1_1",       0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20200   /* Deprecated forms of the above.  */
20201   { "24kefx",         0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20202   { "24kex",          0, ASE_DSP,		ISA_MIPS32R2, CPU_MIPS32R2 },
20203   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
20204   { "34kc",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20205   { "34kf2_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20206   { "34kf",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20207   { "34kf1_1",        0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20208   /* Deprecated forms of the above.  */
20209   { "34kfx",          0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20210   { "34kx",           0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20211   /* 34Kn is a 34kc without DSP.  */
20212   { "34kn",           0, ASE_MT,		ISA_MIPS32R2, CPU_MIPS32R2 },
20213   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
20214   { "74kc",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20215   { "74kf2_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20216   { "74kf",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20217   { "74kf1_1",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20218   { "74kf3_2",        0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20219   /* Deprecated forms of the above.  */
20220   { "74kfx",          0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20221   { "74kx",           0, ASE_DSP | ASE_DSPR2,	ISA_MIPS32R2, CPU_MIPS32R2 },
20222   /* 1004K cores are multiprocessor versions of the 34K.  */
20223   { "1004kc",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20224   { "1004kf2_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20225   { "1004kf",         0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20226   { "1004kf1_1",      0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20227   /* interaptiv is the new name for 1004kf.  */
20228   { "interaptiv",     0, ASE_DSP | ASE_MT,	ISA_MIPS32R2, CPU_MIPS32R2 },
20229   { "interaptiv-mr2", 0,
20230     ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
20231     ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
20232   /* M5100 family.  */
20233   { "m5100",          0, ASE_MCU,		ISA_MIPS32R5, CPU_MIPS32R5 },
20234   { "m5101",          0, ASE_MCU,		ISA_MIPS32R5, CPU_MIPS32R5 },
20235   /* P5600 with EVA and Virtualization ASEs, other ASEs are optional.  */
20236   { "p5600",          0, ASE_VIRT | ASE_EVA | ASE_XPA,	ISA_MIPS32R5, CPU_MIPS32R5 },
20237 
20238   /* MIPS 64 */
20239   { "5kc",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
20240   { "5kf",            0, 0,			ISA_MIPS64,   CPU_MIPS64 },
20241   { "20kc",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
20242   { "25kf",           0, ASE_MIPS3D,		ISA_MIPS64,   CPU_MIPS64 },
20243 
20244   /* Broadcom SB-1 CPU core.  */
20245   { "sb1",            0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
20246   /* Broadcom SB-1A CPU core.  */
20247   { "sb1a",           0, ASE_MIPS3D | ASE_MDMX,	ISA_MIPS64,   CPU_SB1 },
20248 
20249   /* MIPS 64 Release 2.  */
20250   /* Loongson CPU core.  */
20251   /* -march=loongson3a is an alias of -march=gs464 for compatibility.  */
20252   { "loongson3a",     0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20253      ISA_MIPS64R2,	CPU_GS464 },
20254   { "gs464",          0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
20255      ISA_MIPS64R2,	CPU_GS464 },
20256   { "gs464e",         0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20257      | ASE_LOONGSON_EXT2,	ISA_MIPS64R2,	CPU_GS464E },
20258   { "gs264e",         0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20259      | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64,	ISA_MIPS64R2,	CPU_GS264E },
20260 
20261   /* Cavium Networks Octeon CPU core.  */
20262   { "octeon",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON },
20263   { "octeon+",	      0, 0,			ISA_MIPS64R2, CPU_OCTEONP },
20264   { "octeon2",	      0, 0,			ISA_MIPS64R2, CPU_OCTEON2 },
20265   { "octeon3",	      0, ASE_VIRT | ASE_VIRT64,	ISA_MIPS64R5, CPU_OCTEON3 },
20266 
20267   /* RMI Xlr */
20268   { "xlr",	      0, 0,			ISA_MIPS64,   CPU_XLR },
20269 
20270   /* Broadcom XLP.
20271      XLP is mostly like XLR, with the prominent exception that it is
20272      MIPS64R2 rather than MIPS64.  */
20273   { "xlp",	      0, 0,			ISA_MIPS64R2, CPU_XLR },
20274 
20275   /* MIPS 64 Release 6.  */
20276   { "i6400",	      0, ASE_VIRT | ASE_MSA,	ISA_MIPS64R6, CPU_MIPS64R6},
20277   { "i6500",	      0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
20278 						ISA_MIPS64R6, CPU_MIPS64R6},
20279   { "p6600",	      0, ASE_VIRT | ASE_MSA,	ISA_MIPS64R6, CPU_MIPS64R6},
20280 
20281   /* End marker.  */
20282   { NULL, 0, 0, 0, 0 }
20283 };
20284 
20285 
20286 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20287    with a final "000" replaced by "k".  Ignore case.
20288 
20289    Note: this function is shared between GCC and GAS.  */
20290 
20291 static bool
20292 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
20293 {
20294   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20295     given++, canonical++;
20296 
20297   return ((*given == 0 && *canonical == 0)
20298 	  || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20299 }
20300 
20301 
20302 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20303    CPU name.  We've traditionally allowed a lot of variation here.
20304 
20305    Note: this function is shared between GCC and GAS.  */
20306 
20307 static bool
20308 mips_matching_cpu_name_p (const char *canonical, const char *given)
20309 {
20310   /* First see if the name matches exactly, or with a final "000"
20311      turned into "k".  */
20312   if (mips_strict_matching_cpu_name_p (canonical, given))
20313     return true;
20314 
20315   /* If not, try comparing based on numerical designation alone.
20316      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
20317   if (TOLOWER (*given) == 'r')
20318     given++;
20319   if (!ISDIGIT (*given))
20320     return false;
20321 
20322   /* Skip over some well-known prefixes in the canonical name,
20323      hoping to find a number there too.  */
20324   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20325     canonical += 2;
20326   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20327     canonical += 2;
20328   else if (TOLOWER (canonical[0]) == 'r')
20329     canonical += 1;
20330 
20331   return mips_strict_matching_cpu_name_p (canonical, given);
20332 }
20333 
20334 
20335 /* Parse an option that takes the name of a processor as its argument.
20336    OPTION is the name of the option and CPU_STRING is the argument.
20337    Return the corresponding processor enumeration if the CPU_STRING is
20338    recognized, otherwise report an error and return null.
20339 
20340    A similar function exists in GCC.  */
20341 
20342 static const struct mips_cpu_info *
20343 mips_parse_cpu (const char *option, const char *cpu_string)
20344 {
20345   const struct mips_cpu_info *p;
20346 
20347   /* 'from-abi' selects the most compatible architecture for the given
20348      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
20349      EABIs, we have to decide whether we're using the 32-bit or 64-bit
20350      version.  Look first at the -mgp options, if given, otherwise base
20351      the choice on MIPS_DEFAULT_64BIT.
20352 
20353      Treat NO_ABI like the EABIs.  One reason to do this is that the
20354      plain 'mips' and 'mips64' configs have 'from-abi' as their default
20355      architecture.  This code picks MIPS I for 'mips' and MIPS III for
20356      'mips64', just as we did in the days before 'from-abi'.  */
20357   if (strcasecmp (cpu_string, "from-abi") == 0)
20358     {
20359       if (ABI_NEEDS_32BIT_REGS (mips_abi))
20360 	return mips_cpu_info_from_isa (ISA_MIPS1);
20361 
20362       if (ABI_NEEDS_64BIT_REGS (mips_abi))
20363 	return mips_cpu_info_from_isa (ISA_MIPS3);
20364 
20365       if (file_mips_opts.gp >= 0)
20366 	return mips_cpu_info_from_isa (file_mips_opts.gp == 32
20367 				       ? ISA_MIPS1 : ISA_MIPS3);
20368 
20369       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20370 				     ? ISA_MIPS3
20371 				     : ISA_MIPS1);
20372     }
20373 
20374   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
20375   if (strcasecmp (cpu_string, "default") == 0)
20376     return 0;
20377 
20378   for (p = mips_cpu_info_table; p->name != 0; p++)
20379     if (mips_matching_cpu_name_p (p->name, cpu_string))
20380       return p;
20381 
20382   as_bad (_("bad value (%s) for %s"), cpu_string, option);
20383   return 0;
20384 }
20385 
20386 /* Return the canonical processor information for ISA (a member of the
20387    ISA_MIPS* enumeration).  */
20388 
20389 static const struct mips_cpu_info *
20390 mips_cpu_info_from_isa (int isa)
20391 {
20392   int i;
20393 
20394   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20395     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
20396 	&& isa == mips_cpu_info_table[i].isa)
20397       return (&mips_cpu_info_table[i]);
20398 
20399   return NULL;
20400 }
20401 
20402 static const struct mips_cpu_info *
20403 mips_cpu_info_from_arch (int arch)
20404 {
20405   int i;
20406 
20407   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20408     if (arch == mips_cpu_info_table[i].cpu)
20409       return (&mips_cpu_info_table[i]);
20410 
20411   return NULL;
20412 }
20413 
20414 static void
20415 show (FILE *stream, const char *string, int *col_p, int *first_p)
20416 {
20417   if (*first_p)
20418     {
20419       fprintf (stream, "%24s", "");
20420       *col_p = 24;
20421     }
20422   else
20423     {
20424       fprintf (stream, ", ");
20425       *col_p += 2;
20426     }
20427 
20428   if (*col_p + strlen (string) > 72)
20429     {
20430       fprintf (stream, "\n%24s", "");
20431       *col_p = 24;
20432     }
20433 
20434   fprintf (stream, "%s", string);
20435   *col_p += strlen (string);
20436 
20437   *first_p = 0;
20438 }
20439 
20440 void
20441 md_show_usage (FILE *stream)
20442 {
20443   int column, first;
20444   size_t i;
20445 
20446   fprintf (stream, _("\
20447 MIPS options:\n\
20448 -EB			generate big endian output\n\
20449 -EL			generate little endian output\n\
20450 -g, -g2			do not remove unneeded NOPs or swap branches\n\
20451 -G NUM			allow referencing objects up to NUM bytes\n\
20452 			implicitly with the gp register [default 8]\n"));
20453   fprintf (stream, _("\
20454 -mips1			generate MIPS ISA I instructions\n\
20455 -mips2			generate MIPS ISA II instructions\n\
20456 -mips3			generate MIPS ISA III instructions\n\
20457 -mips4			generate MIPS ISA IV instructions\n\
20458 -mips5                  generate MIPS ISA V instructions\n\
20459 -mips32                 generate MIPS32 ISA instructions\n\
20460 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
20461 -mips32r3               generate MIPS32 release 3 ISA instructions\n\
20462 -mips32r5               generate MIPS32 release 5 ISA instructions\n\
20463 -mips32r6               generate MIPS32 release 6 ISA instructions\n\
20464 -mips64                 generate MIPS64 ISA instructions\n\
20465 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
20466 -mips64r3               generate MIPS64 release 3 ISA instructions\n\
20467 -mips64r5               generate MIPS64 release 5 ISA instructions\n\
20468 -mips64r6               generate MIPS64 release 6 ISA instructions\n\
20469 -march=CPU/-mtune=CPU	generate code/schedule for CPU, where CPU is one of:\n"));
20470 
20471   first = 1;
20472 
20473   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20474     show (stream, mips_cpu_info_table[i].name, &column, &first);
20475   show (stream, "from-abi", &column, &first);
20476   fputc ('\n', stream);
20477 
20478   fprintf (stream, _("\
20479 -mCPU			equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20480 -no-mCPU		don't generate code specific to CPU.\n\
20481 			For -mCPU and -no-mCPU, CPU must be one of:\n"));
20482 
20483   first = 1;
20484 
20485   show (stream, "3900", &column, &first);
20486   show (stream, "4010", &column, &first);
20487   show (stream, "4100", &column, &first);
20488   show (stream, "4650", &column, &first);
20489   fputc ('\n', stream);
20490 
20491   fprintf (stream, _("\
20492 -mips16			generate mips16 instructions\n\
20493 -no-mips16		do not generate mips16 instructions\n"));
20494   fprintf (stream, _("\
20495 -mmips16e2		generate MIPS16e2 instructions\n\
20496 -mno-mips16e2		do not generate MIPS16e2 instructions\n"));
20497   fprintf (stream, _("\
20498 -mmicromips		generate microMIPS instructions\n\
20499 -mno-micromips		do not generate microMIPS instructions\n"));
20500   fprintf (stream, _("\
20501 -msmartmips		generate smartmips instructions\n\
20502 -mno-smartmips		do not generate smartmips instructions\n"));
20503   fprintf (stream, _("\
20504 -mdsp			generate DSP instructions\n\
20505 -mno-dsp		do not generate DSP instructions\n"));
20506   fprintf (stream, _("\
20507 -mdspr2			generate DSP R2 instructions\n\
20508 -mno-dspr2		do not generate DSP R2 instructions\n"));
20509   fprintf (stream, _("\
20510 -mdspr3			generate DSP R3 instructions\n\
20511 -mno-dspr3		do not generate DSP R3 instructions\n"));
20512   fprintf (stream, _("\
20513 -mmt			generate MT instructions\n\
20514 -mno-mt			do not generate MT instructions\n"));
20515   fprintf (stream, _("\
20516 -mmcu			generate MCU instructions\n\
20517 -mno-mcu		do not generate MCU instructions\n"));
20518   fprintf (stream, _("\
20519 -mmsa			generate MSA instructions\n\
20520 -mno-msa		do not generate MSA instructions\n"));
20521   fprintf (stream, _("\
20522 -mxpa			generate eXtended Physical Address (XPA) instructions\n\
20523 -mno-xpa		do not generate eXtended Physical Address (XPA) instructions\n"));
20524   fprintf (stream, _("\
20525 -mvirt			generate Virtualization instructions\n\
20526 -mno-virt		do not generate Virtualization instructions\n"));
20527   fprintf (stream, _("\
20528 -mcrc			generate CRC instructions\n\
20529 -mno-crc		do not generate CRC instructions\n"));
20530   fprintf (stream, _("\
20531 -mginv			generate Global INValidate (GINV) instructions\n\
20532 -mno-ginv		do not generate Global INValidate instructions\n"));
20533   fprintf (stream, _("\
20534 -mloongson-mmi		generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20535 -mno-loongson-mmi	do not generate Loongson MultiMedia extensions Instructions\n"));
20536   fprintf (stream, _("\
20537 -mloongson-cam		generate Loongson Content Address Memory (CAM) instructions\n\
20538 -mno-loongson-cam	do not generate Loongson Content Address Memory Instructions\n"));
20539   fprintf (stream, _("\
20540 -mloongson-ext		generate Loongson EXTensions (EXT) instructions\n\
20541 -mno-loongson-ext	do not generate Loongson EXTensions Instructions\n"));
20542   fprintf (stream, _("\
20543 -mloongson-ext2		generate Loongson EXTensions R2 (EXT2) instructions\n\
20544 -mno-loongson-ext2	do not generate Loongson EXTensions R2 Instructions\n"));
20545   fprintf (stream, _("\
20546 -minsn32		only generate 32-bit microMIPS instructions\n\
20547 -mno-insn32		generate all microMIPS instructions\n"));
20548 #if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20549   fprintf (stream, _("\
20550 -mfix-loongson3-llsc	work around Loongson3 LL/SC errata, default\n\
20551 -mno-fix-loongson3-llsc	disable work around Loongson3 LL/SC errata\n"));
20552 #else
20553   fprintf (stream, _("\
20554 -mfix-loongson3-llsc	work around Loongson3 LL/SC errata\n\
20555 -mno-fix-loongson3-llsc	disable work around Loongson3 LL/SC errata, default\n"));
20556 #endif
20557   fprintf (stream, _("\
20558 -mfix-loongson2f-jump	work around Loongson2F JUMP instructions\n\
20559 -mfix-loongson2f-nop	work around Loongson2F NOP errata\n\
20560 -mfix-loongson2f-btb	work around Loongson2F BTB errata\n\
20561 -mfix-loongson3-llsc	work around Loongson3 LL/SC errata\n\
20562 -mno-fix-loongson3-llsc	disable work around Loongson3 LL/SC errata\n\
20563 -mfix-vr4120		work around certain VR4120 errata\n\
20564 -mfix-vr4130		work around VR4130 mflo/mfhi errata\n\
20565 -mfix-24k		insert a nop after ERET and DERET instructions\n\
20566 -mfix-cn63xxp1		work around CN63XXP1 PREF errata\n\
20567 -mfix-r5900		work around R5900 short loop errata\n\
20568 -mgp32			use 32-bit GPRs, regardless of the chosen ISA\n\
20569 -mfp32			use 32-bit FPRs, regardless of the chosen ISA\n\
20570 -msym32			assume all symbols have 32-bit values\n\
20571 -O0			do not remove unneeded NOPs, do not swap branches\n\
20572 -O, -O1			remove unneeded NOPs, do not swap branches\n\
20573 -O2			remove unneeded NOPs and swap branches\n\
20574 --trap, --no-break	trap exception on div by 0 and mult overflow\n\
20575 --break, --no-trap	break exception on div by 0 and mult overflow\n"));
20576   fprintf (stream, _("\
20577 -mhard-float		allow floating-point instructions\n\
20578 -msoft-float		do not allow floating-point instructions\n\
20579 -msingle-float		only allow 32-bit floating-point operations\n\
20580 -mdouble-float		allow 32-bit and 64-bit floating-point operations\n\
20581 --[no-]construct-floats	[dis]allow floating point values to be constructed\n\
20582 --[no-]relax-branch	[dis]allow out-of-range branches to be relaxed\n\
20583 -mignore-branch-isa	accept invalid branches requiring an ISA mode switch\n\
20584 -mno-ignore-branch-isa	reject invalid branches requiring an ISA mode switch\n\
20585 -mnan=ENCODING		select an IEEE 754 NaN encoding convention, either of:\n"));
20586 
20587   first = 1;
20588 
20589   show (stream, "legacy", &column, &first);
20590   show (stream, "2008", &column, &first);
20591 
20592   fputc ('\n', stream);
20593 
20594   fprintf (stream, _("\
20595 -KPIC, -call_shared	generate SVR4 position independent code\n\
20596 -call_nonpic		generate non-PIC code that can operate with DSOs\n\
20597 -mvxworks-pic		generate VxWorks position independent code\n\
20598 -non_shared		do not generate code that can operate with DSOs\n\
20599 -xgot			assume a 32 bit GOT\n\
20600 -mpdr, -mno-pdr		enable/disable creation of .pdr sections\n\
20601 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
20602                         position dependent (non shared) code\n\
20603 -mabi=ABI		create ABI conformant object file for:\n"));
20604 
20605   first = 1;
20606 
20607   show (stream, "32", &column, &first);
20608   show (stream, "o64", &column, &first);
20609   show (stream, "n32", &column, &first);
20610   show (stream, "64", &column, &first);
20611   show (stream, "eabi", &column, &first);
20612 
20613   fputc ('\n', stream);
20614 
20615   fprintf (stream, _("\
20616 -32			create o32 ABI object file%s\n"),
20617 	   MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20618   fprintf (stream, _("\
20619 -n32			create n32 ABI object file%s\n"),
20620 	   MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20621   fprintf (stream, _("\
20622 -64			create 64 ABI object file%s\n"),
20623 	   MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
20624 }
20625 
20626 #ifdef TE_IRIX
20627 enum dwarf2_format
20628 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
20629 {
20630   if (HAVE_64BIT_SYMBOLS)
20631     return dwarf2_format_64bit_irix;
20632   else
20633     return dwarf2_format_32bit;
20634 }
20635 #endif
20636 
20637 int
20638 mips_dwarf2_addr_size (void)
20639 {
20640   if (HAVE_64BIT_OBJECTS)
20641     return 8;
20642   else
20643     return 4;
20644 }
20645 
20646 /* Standard calling conventions leave the CFA at SP on entry.  */
20647 void
20648 mips_cfi_frame_initial_instructions (void)
20649 {
20650   cfi_add_CFA_def_cfa_register (SP);
20651 }
20652 
20653 int
20654 tc_mips_regname_to_dw2regnum (char *regname)
20655 {
20656   unsigned int regnum = -1;
20657   unsigned int reg;
20658 
20659   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20660     regnum = reg;
20661 
20662   return regnum;
20663 }
20664 
20665 /* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20666    Given a symbolic attribute NAME, return the proper integer value.
20667    Returns -1 if the attribute is not known.  */
20668 
20669 int
20670 mips_convert_symbolic_attribute (const char *name)
20671 {
20672   static const struct
20673   {
20674     const char * name;
20675     const int    tag;
20676   }
20677   attribute_table[] =
20678     {
20679 #define T(tag) {#tag, tag}
20680       T (Tag_GNU_MIPS_ABI_FP),
20681       T (Tag_GNU_MIPS_ABI_MSA),
20682 #undef T
20683     };
20684   unsigned int i;
20685 
20686   if (name == NULL)
20687     return -1;
20688 
20689   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20690     if (streq (name, attribute_table[i].name))
20691       return attribute_table[i].tag;
20692 
20693   return -1;
20694 }
20695 
20696 void
20697 md_mips_end (void)
20698 {
20699   int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20700 
20701   mips_emit_delays ();
20702   if (cur_proc_ptr)
20703     as_warn (_("missing .end at end of assembly"));
20704 
20705   /* Just in case no code was emitted, do the consistency check.  */
20706   file_mips_check_options ();
20707 
20708   /* Set a floating-point ABI if the user did not.  */
20709   if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20710     {
20711       /* Perform consistency checks on the floating-point ABI.  */
20712       fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20713 					Tag_GNU_MIPS_ABI_FP);
20714       if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20715 	check_fpabi (fpabi);
20716     }
20717   else
20718     {
20719       /* Soft-float gets precedence over single-float, the two options should
20720          not be used together so this should not matter.  */
20721       if (file_mips_opts.soft_float == 1)
20722 	fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20723       /* Single-float gets precedence over all double_float cases.  */
20724       else if (file_mips_opts.single_float == 1)
20725 	fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20726       else
20727 	{
20728 	  switch (file_mips_opts.fp)
20729 	    {
20730 	    case 32:
20731 	      if (file_mips_opts.gp == 32)
20732 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20733 	      break;
20734 	    case 0:
20735 	      fpabi = Val_GNU_MIPS_ABI_FP_XX;
20736 	      break;
20737 	    case 64:
20738 	      if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20739 		fpabi = Val_GNU_MIPS_ABI_FP_64A;
20740 	      else if (file_mips_opts.gp == 32)
20741 		fpabi = Val_GNU_MIPS_ABI_FP_64;
20742 	      else
20743 		fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20744 	      break;
20745 	    }
20746 	}
20747 
20748       bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20749 				Tag_GNU_MIPS_ABI_FP, fpabi);
20750     }
20751 }
20752 
20753 /*  Returns the relocation type required for a particular CFI encoding.  */
20754 
20755 bfd_reloc_code_real_type
20756 mips_cfi_reloc_for_encoding (int encoding)
20757 {
20758   if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20759     return BFD_RELOC_32_PCREL;
20760   else return BFD_RELOC_NONE;
20761 }
20762