bfd:
[external/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by the OSF and Ralph Campbell.
6    Written by Keith Knowles and Ralph Campbell, working independently.
7    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
8    Support.
9
10    This file is part of GAS.
11
12    GAS is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3, or (at your option)
15    any later version.
16
17    GAS is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with GAS; see the file COPYING.  If not, write to the Free
24    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25    02110-1301, USA.  */
26
27 #include "as.h"
28 #include "config.h"
29 #include "subsegs.h"
30 #include "safe-ctype.h"
31
32 #include "opcode/mips.h"
33 #include "itbl-ops.h"
34 #include "dwarf2dbg.h"
35 #include "dw2gencfi.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too.  */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about.  */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections.  Off by default on IRIX: the native
78    linker doesn't know about and discards them, but relocations against them
79    remain, leading to rld crashes.  */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define ATREG 1
94 #define S0  16
95 #define S7  23
96 #define TREG 24
97 #define PIC_CALL_REG 25
98 #define KT0 26
99 #define KT1 27
100 #define GP  28
101 #define SP  29
102 #define FP  30
103 #define RA  31
104
105 #define ILLEGAL_REG (32)
106
107 #define AT  mips_opts.at
108
109 /* Allow override of standard little-endian ECOFF format.  */
110
111 #ifndef ECOFF_LITTLE_FORMAT
112 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
113 #endif
114
115 extern int target_big_endian;
116
117 /* The name of the readonly data section.  */
118 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
119                             ? ".rdata" \
120                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
121                             ? ".rdata" \
122                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
123                             ? ".rodata" \
124                             : (abort (), ""))
125
126 /* Ways in which an instruction can be "appended" to the output.  */
127 enum append_method {
128   /* Just add it normally.  */
129   APPEND_ADD,
130
131   /* Add it normally and then add a nop.  */
132   APPEND_ADD_WITH_NOP,
133
134   /* Turn an instruction with a delay slot into a "compact" version.  */
135   APPEND_ADD_COMPACT,
136
137   /* Insert the instruction before the last one.  */
138   APPEND_SWAP
139 };
140
141 /* Information about an instruction, including its format, operands
142    and fixups.  */
143 struct mips_cl_insn
144 {
145   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
146   const struct mips_opcode *insn_mo;
147
148   /* True if this is a mips16 instruction and if we want the extended
149      form of INSN_MO.  */
150   bfd_boolean use_extend;
151
152   /* The 16-bit extension instruction to use when USE_EXTEND is true.  */
153   unsigned short extend;
154
155   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
156      a copy of INSN_MO->match with the operands filled in.  */
157   unsigned long insn_opcode;
158
159   /* The frag that contains the instruction.  */
160   struct frag *frag;
161
162   /* The offset into FRAG of the first instruction byte.  */
163   long where;
164
165   /* The relocs associated with the instruction, if any.  */
166   fixS *fixp[3];
167
168   /* True if this entry cannot be moved from its current position.  */
169   unsigned int fixed_p : 1;
170
171   /* True if this instruction occurred in a .set noreorder block.  */
172   unsigned int noreorder_p : 1;
173
174   /* True for mips16 instructions that jump to an absolute address.  */
175   unsigned int mips16_absolute_jump_p : 1;
176
177   /* True if this instruction is complete.  */
178   unsigned int complete_p : 1;
179 };
180
181 /* The ABI to use.  */
182 enum mips_abi_level
183 {
184   NO_ABI = 0,
185   O32_ABI,
186   O64_ABI,
187   N32_ABI,
188   N64_ABI,
189   EABI_ABI
190 };
191
192 /* MIPS ABI we are using for this output file.  */
193 static enum mips_abi_level mips_abi = NO_ABI;
194
195 /* Whether or not we have code that can call pic code.  */
196 int mips_abicalls = FALSE;
197
198 /* Whether or not we have code which can be put into a shared
199    library.  */
200 static bfd_boolean mips_in_shared = TRUE;
201
202 /* This is the set of options which may be modified by the .set
203    pseudo-op.  We use a struct so that .set push and .set pop are more
204    reliable.  */
205
206 struct mips_set_options
207 {
208   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
209      if it has not been initialized.  Changed by `.set mipsN', and the
210      -mipsN command line option, and the default CPU.  */
211   int isa;
212   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
213      if they have not been initialized.  Changed by `.set <asename>', by
214      command line options, and based on the default architecture.  */
215   int ase_mips3d;
216   int ase_mdmx;
217   int ase_smartmips;
218   int ase_dsp;
219   int ase_dspr2;
220   int ase_mt;
221   int ase_mcu;
222   /* Whether we are assembling for the mips16 processor.  0 if we are
223      not, 1 if we are, and -1 if the value has not been initialized.
224      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
225      -nomips16 command line options, and the default CPU.  */
226   int mips16;
227   /* Whether we are assembling for the mipsMIPS ASE.  0 if we are not,
228      1 if we are, and -1 if the value has not been initialized.  Changed
229      by `.set micromips' and `.set nomicromips', and the -mmicromips
230      and -mno-micromips command line options, and the default CPU.  */
231   int micromips;
232   /* Non-zero if we should not reorder instructions.  Changed by `.set
233      reorder' and `.set noreorder'.  */
234   int noreorder;
235   /* Non-zero if we should not permit the register designated "assembler
236      temporary" to be used in instructions.  The value is the register
237      number, normally $at ($1).  Changed by `.set at=REG', `.set noat'
238      (same as `.set at=$0') and `.set at' (same as `.set at=$1').  */
239   unsigned int at;
240   /* Non-zero if we should warn when a macro instruction expands into
241      more than one machine instruction.  Changed by `.set nomacro' and
242      `.set macro'.  */
243   int warn_about_macros;
244   /* Non-zero if we should not move instructions.  Changed by `.set
245      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
246   int nomove;
247   /* Non-zero if we should not optimize branches by moving the target
248      of the branch into the delay slot.  Actually, we don't perform
249      this optimization anyhow.  Changed by `.set bopt' and `.set
250      nobopt'.  */
251   int nobopt;
252   /* Non-zero if we should not autoextend mips16 instructions.
253      Changed by `.set autoextend' and `.set noautoextend'.  */
254   int noautoextend;
255   /* Restrict general purpose registers and floating point registers
256      to 32 bit.  This is initially determined when -mgp32 or -mfp32
257      is passed but can changed if the assembler code uses .set mipsN.  */
258   int gp32;
259   int fp32;
260   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
261      command line option, and the default CPU.  */
262   int arch;
263   /* True if ".set sym32" is in effect.  */
264   bfd_boolean sym32;
265   /* True if floating-point operations are not allowed.  Changed by .set
266      softfloat or .set hardfloat, by command line options -msoft-float or
267      -mhard-float.  The default is false.  */
268   bfd_boolean soft_float;
269
270   /* True if only single-precision floating-point operations are allowed.
271      Changed by .set singlefloat or .set doublefloat, command-line options
272      -msingle-float or -mdouble-float.  The default is false.  */
273   bfd_boolean single_float;
274 };
275
276 /* This is the struct we use to hold the current set of options.  Note
277    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
278    -1 to indicate that they have not been initialized.  */
279
280 /* True if -mgp32 was passed.  */
281 static int file_mips_gp32 = -1;
282
283 /* True if -mfp32 was passed.  */
284 static int file_mips_fp32 = -1;
285
286 /* 1 if -msoft-float, 0 if -mhard-float.  The default is 0.  */
287 static int file_mips_soft_float = 0;
288
289 /* 1 if -msingle-float, 0 if -mdouble-float.  The default is 0.   */
290 static int file_mips_single_float = 0;
291
292 static struct mips_set_options mips_opts =
293 {
294   /* isa */ ISA_UNKNOWN, /* ase_mips3d */ -1, /* ase_mdmx */ -1,
295   /* ase_smartmips */ 0, /* ase_dsp */ -1, /* ase_dspr2 */ -1, /* ase_mt */ -1,
296   /* ase_mcu */ -1, /* mips16 */ -1, /* micromips */ -1, /* noreorder */ 0,
297   /* at */ ATREG, /* warn_about_macros */ 0, /* nomove */ 0, /* nobopt */ 0,
298   /* noautoextend */ 0, /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN,
299   /* sym32 */ FALSE, /* soft_float */ FALSE, /* single_float */ FALSE
300 };
301
302 /* These variables are filled in with the masks of registers used.
303    The object format code reads them and puts them in the appropriate
304    place.  */
305 unsigned long mips_gprmask;
306 unsigned long mips_cprmask[4];
307
308 /* MIPS ISA we are using for this output file.  */
309 static int file_mips_isa = ISA_UNKNOWN;
310
311 /* True if any MIPS16 code was produced.  */
312 static int file_ase_mips16;
313
314 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32               \
315                               || mips_opts.isa == ISA_MIPS32R2          \
316                               || mips_opts.isa == ISA_MIPS64            \
317                               || mips_opts.isa == ISA_MIPS64R2)
318
319 /* True if any microMIPS code was produced.  */
320 static int file_ase_micromips;
321
322 /* True if we want to create R_MIPS_JALR for jalr $25.  */
323 #ifdef TE_IRIX
324 #define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
325 #else
326 /* As a GNU extension, we use R_MIPS_JALR for o32 too.  However,
327    because there's no place for any addend, the only acceptable
328    expression is a bare symbol.  */
329 #define MIPS_JALR_HINT_P(EXPR) \
330   (!HAVE_IN_PLACE_ADDENDS \
331    || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
332 #endif
333
334 /* True if -mips3d was passed or implied by arguments passed on the
335    command line (e.g., by -march).  */
336 static int file_ase_mips3d;
337
338 /* True if -mdmx was passed or implied by arguments passed on the
339    command line (e.g., by -march).  */
340 static int file_ase_mdmx;
341
342 /* True if -msmartmips was passed or implied by arguments passed on the
343    command line (e.g., by -march).  */
344 static int file_ase_smartmips;
345
346 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32             \
347                                 || mips_opts.isa == ISA_MIPS32R2)
348
349 /* True if -mdsp was passed or implied by arguments passed on the
350    command line (e.g., by -march).  */
351 static int file_ase_dsp;
352
353 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2             \
354                               || mips_opts.isa == ISA_MIPS64R2)
355
356 #define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
357
358 /* True if -mdspr2 was passed or implied by arguments passed on the
359    command line (e.g., by -march).  */
360 static int file_ase_dspr2;
361
362 #define ISA_SUPPORTS_DSPR2_ASE (mips_opts.isa == ISA_MIPS32R2           \
363                                 || mips_opts.isa == ISA_MIPS64R2)
364
365 /* True if -mmt was passed or implied by arguments passed on the
366    command line (e.g., by -march).  */
367 static int file_ase_mt;
368
369 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2              \
370                              || mips_opts.isa == ISA_MIPS64R2)
371
372 #define ISA_SUPPORTS_MCU_ASE (mips_opts.isa == ISA_MIPS32R2             \
373                               || mips_opts.isa == ISA_MIPS64R2          \
374                               || mips_opts.micromips)
375
376 /* The argument of the -march= flag.  The architecture we are assembling.  */
377 static int file_mips_arch = CPU_UNKNOWN;
378 static const char *mips_arch_string;
379
380 /* The argument of the -mtune= flag.  The architecture for which we
381    are optimizing.  */
382 static int mips_tune = CPU_UNKNOWN;
383 static const char *mips_tune_string;
384
385 /* True when generating 32-bit code for a 64-bit processor.  */
386 static int mips_32bitmode = 0;
387
388 /* True if the given ABI requires 32-bit registers.  */
389 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
390
391 /* Likewise 64-bit registers.  */
392 #define ABI_NEEDS_64BIT_REGS(ABI)       \
393   ((ABI) == N32_ABI                     \
394    || (ABI) == N64_ABI                  \
395    || (ABI) == O64_ABI)
396
397 /*  Return true if ISA supports 64 bit wide gp registers.  */
398 #define ISA_HAS_64BIT_REGS(ISA)         \
399   ((ISA) == ISA_MIPS3                   \
400    || (ISA) == ISA_MIPS4                \
401    || (ISA) == ISA_MIPS5                \
402    || (ISA) == ISA_MIPS64               \
403    || (ISA) == ISA_MIPS64R2)
404
405 /*  Return true if ISA supports 64 bit wide float registers.  */
406 #define ISA_HAS_64BIT_FPRS(ISA)         \
407   ((ISA) == ISA_MIPS3                   \
408    || (ISA) == ISA_MIPS4                \
409    || (ISA) == ISA_MIPS5                \
410    || (ISA) == ISA_MIPS32R2             \
411    || (ISA) == ISA_MIPS64               \
412    || (ISA) == ISA_MIPS64R2)
413
414 /* Return true if ISA supports 64-bit right rotate (dror et al.)
415    instructions.  */
416 #define ISA_HAS_DROR(ISA)               \
417   ((ISA) == ISA_MIPS64R2                \
418    || (mips_opts.micromips              \
419        && ISA_HAS_64BIT_REGS (ISA))     \
420    )
421
422 /* Return true if ISA supports 32-bit right rotate (ror et al.)
423    instructions.  */
424 #define ISA_HAS_ROR(ISA)                \
425   ((ISA) == ISA_MIPS32R2                \
426    || (ISA) == ISA_MIPS64R2             \
427    || mips_opts.ase_smartmips           \
428    || mips_opts.micromips               \
429    )
430
431 /* Return true if ISA supports single-precision floats in odd registers.  */
432 #define ISA_HAS_ODD_SINGLE_FPR(ISA)     \
433   ((ISA) == ISA_MIPS32                  \
434    || (ISA) == ISA_MIPS32R2             \
435    || (ISA) == ISA_MIPS64               \
436    || (ISA) == ISA_MIPS64R2)
437
438 /* Return true if ISA supports move to/from high part of a 64-bit
439    floating-point register. */
440 #define ISA_HAS_MXHC1(ISA)              \
441   ((ISA) == ISA_MIPS32R2                \
442    || (ISA) == ISA_MIPS64R2)
443
444 #define HAVE_32BIT_GPRS                            \
445     (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
446
447 #define HAVE_32BIT_FPRS                            \
448     (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
449
450 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
451 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
452
453 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
454
455 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
456
457 /* True if relocations are stored in-place.  */
458 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
459
460 /* The ABI-derived address size.  */
461 #define HAVE_64BIT_ADDRESSES \
462   (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
463 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
464
465 /* The size of symbolic constants (i.e., expressions of the form
466    "SYMBOL" or "SYMBOL + OFFSET").  */
467 #define HAVE_32BIT_SYMBOLS \
468   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
469 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
470
471 /* Addresses are loaded in different ways, depending on the address size
472    in use.  The n32 ABI Documentation also mandates the use of additions
473    with overflow checking, but existing implementations don't follow it.  */
474 #define ADDRESS_ADD_INSN                                                \
475    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
476
477 #define ADDRESS_ADDI_INSN                                               \
478    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
479
480 #define ADDRESS_LOAD_INSN                                               \
481    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
482
483 #define ADDRESS_STORE_INSN                                              \
484    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
485
486 /* Return true if the given CPU supports the MIPS16 ASE.  */
487 #define CPU_HAS_MIPS16(cpu)                                             \
488    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
489     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
490
491 /* Return true if the given CPU supports the microMIPS ASE.  */
492 #define CPU_HAS_MICROMIPS(cpu)  0
493
494 /* True if CPU has a dror instruction.  */
495 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
496
497 /* True if CPU has a ror instruction.  */
498 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
499
500 /* True if CPU is in the Octeon family */
501 #define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
502
503 /* True if CPU has seq/sne and seqi/snei instructions.  */
504 #define CPU_HAS_SEQ(CPU)        (CPU_IS_OCTEON (CPU))
505
506 /* True if CPU does not implement the all the coprocessor insns.  For these
507    CPUs only those COP insns are accepted that are explicitly marked to be
508    available on the CPU.  ISA membership for COP insns is ignored.  */
509 #define NO_ISA_COP(CPU)         (CPU_IS_OCTEON (CPU))
510
511 /* True if mflo and mfhi can be immediately followed by instructions
512    which write to the HI and LO registers.
513
514    According to MIPS specifications, MIPS ISAs I, II, and III need
515    (at least) two instructions between the reads of HI/LO and
516    instructions which write them, and later ISAs do not.  Contradicting
517    the MIPS specifications, some MIPS IV processor user manuals (e.g.
518    the UM for the NEC Vr5000) document needing the instructions between
519    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
520    MIPS64 and later ISAs to have the interlocks, plus any specific
521    earlier-ISA CPUs for which CPU documentation declares that the
522    instructions are really interlocked.  */
523 #define hilo_interlocks \
524   (mips_opts.isa == ISA_MIPS32                        \
525    || mips_opts.isa == ISA_MIPS32R2                   \
526    || mips_opts.isa == ISA_MIPS64                     \
527    || mips_opts.isa == ISA_MIPS64R2                   \
528    || mips_opts.arch == CPU_R4010                     \
529    || mips_opts.arch == CPU_R10000                    \
530    || mips_opts.arch == CPU_R12000                    \
531    || mips_opts.arch == CPU_R14000                    \
532    || mips_opts.arch == CPU_R16000                    \
533    || mips_opts.arch == CPU_RM7000                    \
534    || mips_opts.arch == CPU_VR5500                    \
535    || mips_opts.micromips                             \
536    )
537
538 /* Whether the processor uses hardware interlocks to protect reads
539    from the GPRs after they are loaded from memory, and thus does not
540    require nops to be inserted.  This applies to instructions marked
541    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
542    level I and microMIPS mode instructions are always interlocked.  */
543 #define gpr_interlocks                                \
544   (mips_opts.isa != ISA_MIPS1                         \
545    || mips_opts.arch == CPU_R3900                     \
546    || mips_opts.micromips                             \
547    )
548
549 /* Whether the processor uses hardware interlocks to avoid delays
550    required by coprocessor instructions, and thus does not require
551    nops to be inserted.  This applies to instructions marked
552    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
553    between instructions marked INSN_WRITE_COND_CODE and ones marked
554    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
555    levels I, II, and III and microMIPS mode instructions are always
556    interlocked.  */
557 /* Itbl support may require additional care here.  */
558 #define cop_interlocks                                \
559   ((mips_opts.isa != ISA_MIPS1                        \
560     && mips_opts.isa != ISA_MIPS2                     \
561     && mips_opts.isa != ISA_MIPS3)                    \
562    || mips_opts.arch == CPU_R4300                     \
563    || mips_opts.micromips                             \
564    )
565
566 /* Whether the processor uses hardware interlocks to protect reads
567    from coprocessor registers after they are loaded from memory, and
568    thus does not require nops to be inserted.  This applies to
569    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
570    requires at MIPS ISA level I and microMIPS mode instructions are
571    always interlocked.  */
572 #define cop_mem_interlocks                            \
573   (mips_opts.isa != ISA_MIPS1                         \
574    || mips_opts.micromips                             \
575    )
576
577 /* Is this a mfhi or mflo instruction?  */
578 #define MF_HILO_INSN(PINFO) \
579   ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
580
581 /* Returns true for a (non floating-point) coprocessor instruction.  Reading
582    or writing the condition code is only possible on the coprocessors and
583    these insns are not marked with INSN_COP.  Thus for these insns use the
584    condition-code flags.  */
585 #define COP_INSN(PINFO)                                                 \
586   (PINFO != INSN_MACRO                                                  \
587    && ((PINFO) & (FP_S | FP_D)) == 0                                    \
588    && ((PINFO) & (INSN_COP | INSN_READ_COND_CODE | INSN_WRITE_COND_CODE)))
589
590 /* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
591    has been selected.  This implies, in particular, that addresses of text
592    labels have their LSB set.  */
593 #define HAVE_CODE_COMPRESSION                                           \
594   ((mips_opts.mips16 | mips_opts.micromips) != 0)
595
596 /* MIPS PIC level.  */
597
598 enum mips_pic_level mips_pic;
599
600 /* 1 if we should generate 32 bit offsets from the $gp register in
601    SVR4_PIC mode.  Currently has no meaning in other modes.  */
602 static int mips_big_got = 0;
603
604 /* 1 if trap instructions should used for overflow rather than break
605    instructions.  */
606 static int mips_trap = 0;
607
608 /* 1 if double width floating point constants should not be constructed
609    by assembling two single width halves into two single width floating
610    point registers which just happen to alias the double width destination
611    register.  On some architectures this aliasing can be disabled by a bit
612    in the status register, and the setting of this bit cannot be determined
613    automatically at assemble time.  */
614 static int mips_disable_float_construction;
615
616 /* Non-zero if any .set noreorder directives were used.  */
617
618 static int mips_any_noreorder;
619
620 /* Non-zero if nops should be inserted when the register referenced in
621    an mfhi/mflo instruction is read in the next two instructions.  */
622 static int mips_7000_hilo_fix;
623
624 /* The size of objects in the small data section.  */
625 static unsigned int g_switch_value = 8;
626 /* Whether the -G option was used.  */
627 static int g_switch_seen = 0;
628
629 #define N_RMASK 0xc4
630 #define N_VFP   0xd4
631
632 /* If we can determine in advance that GP optimization won't be
633    possible, we can skip the relaxation stuff that tries to produce
634    GP-relative references.  This makes delay slot optimization work
635    better.
636
637    This function can only provide a guess, but it seems to work for
638    gcc output.  It needs to guess right for gcc, otherwise gcc
639    will put what it thinks is a GP-relative instruction in a branch
640    delay slot.
641
642    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
643    fixed it for the non-PIC mode.  KR 95/04/07  */
644 static int nopic_need_relax (symbolS *, int);
645
646 /* handle of the OPCODE hash table */
647 static struct hash_control *op_hash = NULL;
648
649 /* The opcode hash table we use for the mips16.  */
650 static struct hash_control *mips16_op_hash = NULL;
651
652 /* The opcode hash table we use for the microMIPS ASE.  */
653 static struct hash_control *micromips_op_hash = NULL;
654
655 /* This array holds the chars that always start a comment.  If the
656     pre-processor is disabled, these aren't very useful */
657 const char comment_chars[] = "#";
658
659 /* This array holds the chars that only start a comment at the beginning of
660    a line.  If the line seems to have the form '# 123 filename'
661    .line and .file directives will appear in the pre-processed output */
662 /* Note that input_file.c hand checks for '#' at the beginning of the
663    first line of the input file.  This is because the compiler outputs
664    #NO_APP at the beginning of its output.  */
665 /* Also note that C style comments are always supported.  */
666 const char line_comment_chars[] = "#";
667
668 /* This array holds machine specific line separator characters.  */
669 const char line_separator_chars[] = ";";
670
671 /* Chars that can be used to separate mant from exp in floating point nums */
672 const char EXP_CHARS[] = "eE";
673
674 /* Chars that mean this number is a floating point constant */
675 /* As in 0f12.456 */
676 /* or    0d1.2345e12 */
677 const char FLT_CHARS[] = "rRsSfFdDxXpP";
678
679 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
680    changed in read.c .  Ideally it shouldn't have to know about it at all,
681    but nothing is ideal around here.
682  */
683
684 static char *insn_error;
685
686 static int auto_align = 1;
687
688 /* When outputting SVR4 PIC code, the assembler needs to know the
689    offset in the stack frame from which to restore the $gp register.
690    This is set by the .cprestore pseudo-op, and saved in this
691    variable.  */
692 static offsetT mips_cprestore_offset = -1;
693
694 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
695    more optimizations, it can use a register value instead of a memory-saved
696    offset and even an other register than $gp as global pointer.  */
697 static offsetT mips_cpreturn_offset = -1;
698 static int mips_cpreturn_register = -1;
699 static int mips_gp_register = GP;
700 static int mips_gprel_offset = 0;
701
702 /* Whether mips_cprestore_offset has been set in the current function
703    (or whether it has already been warned about, if not).  */
704 static int mips_cprestore_valid = 0;
705
706 /* This is the register which holds the stack frame, as set by the
707    .frame pseudo-op.  This is needed to implement .cprestore.  */
708 static int mips_frame_reg = SP;
709
710 /* Whether mips_frame_reg has been set in the current function
711    (or whether it has already been warned about, if not).  */
712 static int mips_frame_reg_valid = 0;
713
714 /* To output NOP instructions correctly, we need to keep information
715    about the previous two instructions.  */
716
717 /* Whether we are optimizing.  The default value of 2 means to remove
718    unneeded NOPs and swap branch instructions when possible.  A value
719    of 1 means to not swap branches.  A value of 0 means to always
720    insert NOPs.  */
721 static int mips_optimize = 2;
722
723 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
724    equivalent to seeing no -g option at all.  */
725 static int mips_debug = 0;
726
727 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
728 #define MAX_VR4130_NOPS 4
729
730 /* The maximum number of NOPs needed to fill delay slots.  */
731 #define MAX_DELAY_NOPS 2
732
733 /* The maximum number of NOPs needed for any purpose.  */
734 #define MAX_NOPS 4
735
736 /* A list of previous instructions, with index 0 being the most recent.
737    We need to look back MAX_NOPS instructions when filling delay slots
738    or working around processor errata.  We need to look back one
739    instruction further if we're thinking about using history[0] to
740    fill a branch delay slot.  */
741 static struct mips_cl_insn history[1 + MAX_NOPS];
742
743 /* Nop instructions used by emit_nop.  */
744 static struct mips_cl_insn nop_insn;
745 static struct mips_cl_insn mips16_nop_insn;
746 static struct mips_cl_insn micromips_nop16_insn;
747 static struct mips_cl_insn micromips_nop32_insn;
748
749 /* The appropriate nop for the current mode.  */
750 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn \
751                   : (mips_opts.micromips ? &micromips_nop16_insn : &nop_insn))
752
753 /* The size of NOP_INSN in bytes.  */
754 #define NOP_INSN_SIZE (HAVE_CODE_COMPRESSION ? 2 : 4)
755
756 /* If this is set, it points to a frag holding nop instructions which
757    were inserted before the start of a noreorder section.  If those
758    nops turn out to be unnecessary, the size of the frag can be
759    decreased.  */
760 static fragS *prev_nop_frag;
761
762 /* The number of nop instructions we created in prev_nop_frag.  */
763 static int prev_nop_frag_holds;
764
765 /* The number of nop instructions that we know we need in
766    prev_nop_frag.  */
767 static int prev_nop_frag_required;
768
769 /* The number of instructions we've seen since prev_nop_frag.  */
770 static int prev_nop_frag_since;
771
772 /* For ECOFF and ELF, relocations against symbols are done in two
773    parts, with a HI relocation and a LO relocation.  Each relocation
774    has only 16 bits of space to store an addend.  This means that in
775    order for the linker to handle carries correctly, it must be able
776    to locate both the HI and the LO relocation.  This means that the
777    relocations must appear in order in the relocation table.
778
779    In order to implement this, we keep track of each unmatched HI
780    relocation.  We then sort them so that they immediately precede the
781    corresponding LO relocation.  */
782
783 struct mips_hi_fixup
784 {
785   /* Next HI fixup.  */
786   struct mips_hi_fixup *next;
787   /* This fixup.  */
788   fixS *fixp;
789   /* The section this fixup is in.  */
790   segT seg;
791 };
792
793 /* The list of unmatched HI relocs.  */
794
795 static struct mips_hi_fixup *mips_hi_fixup_list;
796
797 /* The frag containing the last explicit relocation operator.
798    Null if explicit relocations have not been used.  */
799
800 static fragS *prev_reloc_op_frag;
801
802 /* Map normal MIPS register numbers to mips16 register numbers.  */
803
804 #define X ILLEGAL_REG
805 static const int mips32_to_16_reg_map[] =
806 {
807   X, X, 2, 3, 4, 5, 6, 7,
808   X, X, X, X, X, X, X, X,
809   0, 1, X, X, X, X, X, X,
810   X, X, X, X, X, X, X, X
811 };
812 #undef X
813
814 /* Map mips16 register numbers to normal MIPS register numbers.  */
815
816 static const unsigned int mips16_to_32_reg_map[] =
817 {
818   16, 17, 2, 3, 4, 5, 6, 7
819 };
820
821 /* Map normal MIPS register numbers to microMIPS register numbers.  */
822
823 #define mips32_to_micromips_reg_b_map   mips32_to_16_reg_map
824 #define mips32_to_micromips_reg_c_map   mips32_to_16_reg_map
825 #define mips32_to_micromips_reg_d_map   mips32_to_16_reg_map
826 #define mips32_to_micromips_reg_e_map   mips32_to_16_reg_map
827 #define mips32_to_micromips_reg_f_map   mips32_to_16_reg_map
828 #define mips32_to_micromips_reg_g_map   mips32_to_16_reg_map
829 #define mips32_to_micromips_reg_l_map   mips32_to_16_reg_map
830
831 #define X ILLEGAL_REG
832 /* reg type h: 4, 5, 6.  */
833 static const int mips32_to_micromips_reg_h_map[] =
834 {
835   X, X, X, X, 4, 5, 6, X,
836   X, X, X, X, X, X, X, X,
837   X, X, X, X, X, X, X, X,
838   X, X, X, X, X, X, X, X
839 };
840
841 /* reg type m: 0, 17, 2, 3, 16, 18, 19, 20.  */
842 static const int mips32_to_micromips_reg_m_map[] =
843 {
844   0, X, 2, 3, X, X, X, X,
845   X, X, X, X, X, X, X, X,
846   4, 1, 5, 6, 7, X, X, X,
847   X, X, X, X, X, X, X, X
848 };
849
850 /* reg type q: 0, 2-7. 17.  */
851 static const int mips32_to_micromips_reg_q_map[] =
852 {
853   0, X, 2, 3, 4, 5, 6, 7,
854   X, X, X, X, X, X, X, X,
855   X, 1, X, X, X, X, X, X,
856   X, X, X, X, X, X, X, X
857 };
858
859 #define mips32_to_micromips_reg_n_map  mips32_to_micromips_reg_m_map
860 #undef X
861
862 /* Map microMIPS register numbers to normal MIPS register numbers.  */
863
864 #define micromips_to_32_reg_b_map       mips16_to_32_reg_map
865 #define micromips_to_32_reg_c_map       mips16_to_32_reg_map
866 #define micromips_to_32_reg_d_map       mips16_to_32_reg_map
867 #define micromips_to_32_reg_e_map       mips16_to_32_reg_map
868 #define micromips_to_32_reg_f_map       mips16_to_32_reg_map
869 #define micromips_to_32_reg_g_map       mips16_to_32_reg_map
870
871 /* The microMIPS registers with type h.  */
872 static const unsigned int micromips_to_32_reg_h_map[] =
873 {
874   5, 5, 6, 4, 4, 4, 4, 4
875 };
876
877 /* The microMIPS registers with type i.  */
878 static const unsigned int micromips_to_32_reg_i_map[] =
879 {
880   6, 7, 7, 21, 22, 5, 6, 7
881 };
882
883 #define micromips_to_32_reg_l_map       mips16_to_32_reg_map
884
885 /* The microMIPS registers with type m.  */
886 static const unsigned int micromips_to_32_reg_m_map[] =
887 {
888   0, 17, 2, 3, 16, 18, 19, 20
889 };
890
891 #define micromips_to_32_reg_n_map      micromips_to_32_reg_m_map
892
893 /* The microMIPS registers with type q.  */
894 static const unsigned int micromips_to_32_reg_q_map[] =
895 {
896   0, 17, 2, 3, 4, 5, 6, 7
897 };
898
899 /* microMIPS imm type B.  */
900 static const int micromips_imm_b_map[] =
901 {
902   1, 4, 8, 12, 16, 20, 24, -1
903 };
904
905 /* microMIPS imm type C.  */
906 static const int micromips_imm_c_map[] =
907 {
908   128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
909 };
910
911 /* Classifies the kind of instructions we're interested in when
912    implementing -mfix-vr4120.  */
913 enum fix_vr4120_class
914 {
915   FIX_VR4120_MACC,
916   FIX_VR4120_DMACC,
917   FIX_VR4120_MULT,
918   FIX_VR4120_DMULT,
919   FIX_VR4120_DIV,
920   FIX_VR4120_MTHILO,
921   NUM_FIX_VR4120_CLASSES
922 };
923
924 /* ...likewise -mfix-loongson2f-jump.  */
925 static bfd_boolean mips_fix_loongson2f_jump;
926
927 /* ...likewise -mfix-loongson2f-nop.  */
928 static bfd_boolean mips_fix_loongson2f_nop;
929
930 /* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed.  */
931 static bfd_boolean mips_fix_loongson2f;
932
933 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
934    there must be at least one other instruction between an instruction
935    of type X and an instruction of type Y.  */
936 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
937
938 /* True if -mfix-vr4120 is in force.  */
939 static int mips_fix_vr4120;
940
941 /* ...likewise -mfix-vr4130.  */
942 static int mips_fix_vr4130;
943
944 /* ...likewise -mfix-24k.  */
945 static int mips_fix_24k;
946
947 /* ...likewise -mfix-cn63xxp1 */
948 static bfd_boolean mips_fix_cn63xxp1;
949
950 /* We don't relax branches by default, since this causes us to expand
951    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
952    fail to compute the offset before expanding the macro to the most
953    efficient expansion.  */
954
955 static int mips_relax_branch;
956 \f
957 /* The expansion of many macros depends on the type of symbol that
958    they refer to.  For example, when generating position-dependent code,
959    a macro that refers to a symbol may have two different expansions,
960    one which uses GP-relative addresses and one which uses absolute
961    addresses.  When generating SVR4-style PIC, a macro may have
962    different expansions for local and global symbols.
963
964    We handle these situations by generating both sequences and putting
965    them in variant frags.  In position-dependent code, the first sequence
966    will be the GP-relative one and the second sequence will be the
967    absolute one.  In SVR4 PIC, the first sequence will be for global
968    symbols and the second will be for local symbols.
969
970    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
971    SECOND are the lengths of the two sequences in bytes.  These fields
972    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
973    the subtype has the following flags:
974
975    RELAX_USE_SECOND
976         Set if it has been decided that we should use the second
977         sequence instead of the first.
978
979    RELAX_SECOND_LONGER
980         Set in the first variant frag if the macro's second implementation
981         is longer than its first.  This refers to the macro as a whole,
982         not an individual relaxation.
983
984    RELAX_NOMACRO
985         Set in the first variant frag if the macro appeared in a .set nomacro
986         block and if one alternative requires a warning but the other does not.
987
988    RELAX_DELAY_SLOT
989         Like RELAX_NOMACRO, but indicates that the macro appears in a branch
990         delay slot.
991
992    RELAX_DELAY_SLOT_16BIT
993         Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
994         16-bit instruction.
995
996    RELAX_DELAY_SLOT_SIZE_FIRST
997         Like RELAX_DELAY_SLOT, but indicates that the first implementation of
998         the macro is of the wrong size for the branch delay slot.
999
1000    RELAX_DELAY_SLOT_SIZE_SECOND
1001         Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1002         the macro is of the wrong size for the branch delay slot.
1003
1004    The frag's "opcode" points to the first fixup for relaxable code.
1005
1006    Relaxable macros are generated using a sequence such as:
1007
1008       relax_start (SYMBOL);
1009       ... generate first expansion ...
1010       relax_switch ();
1011       ... generate second expansion ...
1012       relax_end ();
1013
1014    The code and fixups for the unwanted alternative are discarded
1015    by md_convert_frag.  */
1016 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
1017
1018 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1019 #define RELAX_SECOND(X) ((X) & 0xff)
1020 #define RELAX_USE_SECOND 0x10000
1021 #define RELAX_SECOND_LONGER 0x20000
1022 #define RELAX_NOMACRO 0x40000
1023 #define RELAX_DELAY_SLOT 0x80000
1024 #define RELAX_DELAY_SLOT_16BIT 0x100000
1025 #define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1026 #define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
1027
1028 /* Branch without likely bit.  If label is out of range, we turn:
1029
1030         beq reg1, reg2, label
1031         delay slot
1032
1033    into
1034
1035         bne reg1, reg2, 0f
1036         nop
1037         j label
1038      0: delay slot
1039
1040    with the following opcode replacements:
1041
1042         beq <-> bne
1043         blez <-> bgtz
1044         bltz <-> bgez
1045         bc1f <-> bc1t
1046
1047         bltzal <-> bgezal  (with jal label instead of j label)
1048
1049    Even though keeping the delay slot instruction in the delay slot of
1050    the branch would be more efficient, it would be very tricky to do
1051    correctly, because we'd have to introduce a variable frag *after*
1052    the delay slot instruction, and expand that instead.  Let's do it
1053    the easy way for now, even if the branch-not-taken case now costs
1054    one additional instruction.  Out-of-range branches are not supposed
1055    to be common, anyway.
1056
1057    Branch likely.  If label is out of range, we turn:
1058
1059         beql reg1, reg2, label
1060         delay slot (annulled if branch not taken)
1061
1062    into
1063
1064         beql reg1, reg2, 1f
1065         nop
1066         beql $0, $0, 2f
1067         nop
1068      1: j[al] label
1069         delay slot (executed only if branch taken)
1070      2:
1071
1072    It would be possible to generate a shorter sequence by losing the
1073    likely bit, generating something like:
1074
1075         bne reg1, reg2, 0f
1076         nop
1077         j[al] label
1078         delay slot (executed only if branch taken)
1079      0:
1080
1081         beql -> bne
1082         bnel -> beq
1083         blezl -> bgtz
1084         bgtzl -> blez
1085         bltzl -> bgez
1086         bgezl -> bltz
1087         bc1fl -> bc1t
1088         bc1tl -> bc1f
1089
1090         bltzall -> bgezal  (with jal label instead of j label)
1091         bgezall -> bltzal  (ditto)
1092
1093
1094    but it's not clear that it would actually improve performance.  */
1095 #define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar)   \
1096   ((relax_substateT)                                            \
1097    (0xc0000000                                                  \
1098     | ((at) & 0x1f)                                             \
1099     | ((toofar) ? 0x20 : 0)                                     \
1100     | ((link) ? 0x40 : 0)                                       \
1101     | ((likely) ? 0x80 : 0)                                     \
1102     | ((uncond) ? 0x100 : 0)))
1103 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
1104 #define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1105 #define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1106 #define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1107 #define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1108 #define RELAX_BRANCH_AT(i) ((i) & 0x1f)
1109
1110 /* For mips16 code, we use an entirely different form of relaxation.
1111    mips16 supports two versions of most instructions which take
1112    immediate values: a small one which takes some small value, and a
1113    larger one which takes a 16 bit value.  Since branches also follow
1114    this pattern, relaxing these values is required.
1115
1116    We can assemble both mips16 and normal MIPS code in a single
1117    object.  Therefore, we need to support this type of relaxation at
1118    the same time that we support the relaxation described above.  We
1119    use the high bit of the subtype field to distinguish these cases.
1120
1121    The information we store for this type of relaxation is the
1122    argument code found in the opcode file for this relocation, whether
1123    the user explicitly requested a small or extended form, and whether
1124    the relocation is in a jump or jal delay slot.  That tells us the
1125    size of the value, and how it should be stored.  We also store
1126    whether the fragment is considered to be extended or not.  We also
1127    store whether this is known to be a branch to a different section,
1128    whether we have tried to relax this frag yet, and whether we have
1129    ever extended a PC relative fragment because of a shift count.  */
1130 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1131   (0x80000000                                                   \
1132    | ((type) & 0xff)                                            \
1133    | ((small) ? 0x100 : 0)                                      \
1134    | ((ext) ? 0x200 : 0)                                        \
1135    | ((dslot) ? 0x400 : 0)                                      \
1136    | ((jal_dslot) ? 0x800 : 0))
1137 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
1138 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1139 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1140 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1141 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1142 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1143 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1144 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1145 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1146 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1147 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1148 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
1149
1150 /* For microMIPS code, we use relaxation similar to one we use for
1151    MIPS16 code.  Some instructions that take immediate values support
1152    two encodings: a small one which takes some small value, and a
1153    larger one which takes a 16 bit value.  As some branches also follow
1154    this pattern, relaxing these values is required.
1155
1156    We can assemble both microMIPS 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 one of the high bits of the subtype field to distinguish these
1160    cases.
1161
1162    The information we store for this type of relaxation is the argument
1163    code found in the opcode file for this relocation, the register
1164    selected as the assembler temporary, whether the branch is
1165    unconditional, whether it is compact, whether it stores the link
1166    address implicitly in $ra, whether relaxation of out-of-range 32-bit
1167    branches to a sequence of instructions is enabled, and whether the
1168    displacement of a branch is too large to fit as an immediate argument
1169    of a 16-bit and a 32-bit branch, respectively.  */
1170 #define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1171                                relax32, toofar16, toofar32)     \
1172   (0x40000000                                                   \
1173    | ((type) & 0xff)                                            \
1174    | (((at) & 0x1f) << 8)                                       \
1175    | ((uncond) ? 0x2000 : 0)                                    \
1176    | ((compact) ? 0x4000 : 0)                                   \
1177    | ((link) ? 0x8000 : 0)                                      \
1178    | ((relax32) ? 0x10000 : 0)                                  \
1179    | ((toofar16) ? 0x20000 : 0)                                 \
1180    | ((toofar32) ? 0x40000 : 0))
1181 #define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1182 #define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1183 #define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
1184 #define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1185 #define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1186 #define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1187 #define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1188
1189 #define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1190 #define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1191 #define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1192 #define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1193 #define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1194 #define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
1195
1196 /* Is the given value a sign-extended 32-bit value?  */
1197 #define IS_SEXT_32BIT_NUM(x)                                            \
1198   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
1199    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1200
1201 /* Is the given value a sign-extended 16-bit value?  */
1202 #define IS_SEXT_16BIT_NUM(x)                                            \
1203   (((x) &~ (offsetT) 0x7fff) == 0                                       \
1204    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1205
1206 /* Is the given value a sign-extended 12-bit value?  */
1207 #define IS_SEXT_12BIT_NUM(x)                                            \
1208   (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1209
1210 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
1211 #define IS_ZEXT_32BIT_NUM(x)                                            \
1212   (((x) &~ (offsetT) 0xffffffff) == 0                                   \
1213    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1214
1215 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1216    VALUE << SHIFT.  VALUE is evaluated exactly once.  */
1217 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1218   (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1219               | (((VALUE) & (MASK)) << (SHIFT)))
1220
1221 /* Extract bits MASK << SHIFT from STRUCT and shift them right
1222    SHIFT places.  */
1223 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1224   (((STRUCT) >> (SHIFT)) & (MASK))
1225
1226 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1227    INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1228
1229    include/opcode/mips.h specifies operand fields using the macros
1230    OP_MASK_<FIELD> and OP_SH_<FIELD>.  The MIPS16 equivalents start
1231    with "MIPS16OP" instead of "OP".  */
1232 #define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1233   do \
1234     if (!(MICROMIPS)) \
1235       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1236                    OP_MASK_##FIELD, OP_SH_##FIELD); \
1237     else \
1238       INSERT_BITS ((INSN).insn_opcode, VALUE, \
1239                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1240   while (0)
1241 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1242   INSERT_BITS ((INSN).insn_opcode, VALUE, \
1243                 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1244
1245 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
1246 #define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1247   (!(MICROMIPS) \
1248    ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1249    : EXTRACT_BITS ((INSN).insn_opcode, \
1250                    MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
1251 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1252   EXTRACT_BITS ((INSN).insn_opcode, \
1253                 MIPS16OP_MASK_##FIELD, \
1254                 MIPS16OP_SH_##FIELD)
1255 \f
1256 /* Whether or not we are emitting a branch-likely macro.  */
1257 static bfd_boolean emit_branch_likely_macro = FALSE;
1258
1259 /* Global variables used when generating relaxable macros.  See the
1260    comment above RELAX_ENCODE for more details about how relaxation
1261    is used.  */
1262 static struct {
1263   /* 0 if we're not emitting a relaxable macro.
1264      1 if we're emitting the first of the two relaxation alternatives.
1265      2 if we're emitting the second alternative.  */
1266   int sequence;
1267
1268   /* The first relaxable fixup in the current frag.  (In other words,
1269      the first fixup that refers to relaxable code.)  */
1270   fixS *first_fixup;
1271
1272   /* sizes[0] says how many bytes of the first alternative are stored in
1273      the current frag.  Likewise sizes[1] for the second alternative.  */
1274   unsigned int sizes[2];
1275
1276   /* The symbol on which the choice of sequence depends.  */
1277   symbolS *symbol;
1278 } mips_relax;
1279 \f
1280 /* Global variables used to decide whether a macro needs a warning.  */
1281 static struct {
1282   /* True if the macro is in a branch delay slot.  */
1283   bfd_boolean delay_slot_p;
1284
1285   /* Set to the length in bytes required if the macro is in a delay slot
1286      that requires a specific length of instruction, otherwise zero.  */
1287   unsigned int delay_slot_length;
1288
1289   /* For relaxable macros, sizes[0] is the length of the first alternative
1290      in bytes and sizes[1] is the length of the second alternative.
1291      For non-relaxable macros, both elements give the length of the
1292      macro in bytes.  */
1293   unsigned int sizes[2];
1294
1295   /* For relaxable macros, first_insn_sizes[0] is the length of the first
1296      instruction of the first alternative in bytes and first_insn_sizes[1]
1297      is the length of the first instruction of the second alternative.
1298      For non-relaxable macros, both elements give the length of the first
1299      instruction in bytes.
1300
1301      Set to zero if we haven't yet seen the first instruction.  */
1302   unsigned int first_insn_sizes[2];
1303
1304   /* For relaxable macros, insns[0] is the number of instructions for the
1305      first alternative and insns[1] is the number of instructions for the
1306      second alternative.
1307
1308      For non-relaxable macros, both elements give the number of
1309      instructions for the macro.  */
1310   unsigned int insns[2];
1311
1312   /* The first variant frag for this macro.  */
1313   fragS *first_frag;
1314 } mips_macro_warning;
1315 \f
1316 /* Prototypes for static functions.  */
1317
1318 #define internalError()                                                 \
1319     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
1320
1321 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1322
1323 static void append_insn
1324   (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1325    bfd_boolean expansionp);
1326 static void mips_no_prev_insn (void);
1327 static void macro_build (expressionS *, const char *, const char *, ...);
1328 static void mips16_macro_build
1329   (expressionS *, const char *, const char *, va_list *);
1330 static void load_register (int, expressionS *, int);
1331 static void macro_start (void);
1332 static void macro_end (void);
1333 static void macro (struct mips_cl_insn * ip);
1334 static void mips16_macro (struct mips_cl_insn * ip);
1335 static void mips_ip (char *str, struct mips_cl_insn * ip);
1336 static void mips16_ip (char *str, struct mips_cl_insn * ip);
1337 static void mips16_immed
1338   (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1339    unsigned long *, bfd_boolean *, unsigned short *);
1340 static size_t my_getSmallExpression
1341   (expressionS *, bfd_reloc_code_real_type *, char *);
1342 static void my_getExpression (expressionS *, char *);
1343 static void s_align (int);
1344 static void s_change_sec (int);
1345 static void s_change_section (int);
1346 static void s_cons (int);
1347 static void s_float_cons (int);
1348 static void s_mips_globl (int);
1349 static void s_option (int);
1350 static void s_mipsset (int);
1351 static void s_abicalls (int);
1352 static void s_cpload (int);
1353 static void s_cpsetup (int);
1354 static void s_cplocal (int);
1355 static void s_cprestore (int);
1356 static void s_cpreturn (int);
1357 static void s_dtprelword (int);
1358 static void s_dtpreldword (int);
1359 static void s_gpvalue (int);
1360 static void s_gpword (int);
1361 static void s_gpdword (int);
1362 static void s_cpadd (int);
1363 static void s_insn (int);
1364 static void md_obj_begin (void);
1365 static void md_obj_end (void);
1366 static void s_mips_ent (int);
1367 static void s_mips_end (int);
1368 static void s_mips_frame (int);
1369 static void s_mips_mask (int reg_type);
1370 static void s_mips_stab (int);
1371 static void s_mips_weakext (int);
1372 static void s_mips_file (int);
1373 static void s_mips_loc (int);
1374 static bfd_boolean pic_need_relax (symbolS *, asection *);
1375 static int relaxed_branch_length (fragS *, asection *, int);
1376 static int validate_mips_insn (const struct mips_opcode *);
1377 static int validate_micromips_insn (const struct mips_opcode *);
1378 static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1379 static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
1380
1381 /* Table and functions used to map between CPU/ISA names, and
1382    ISA levels, and CPU numbers.  */
1383
1384 struct mips_cpu_info
1385 {
1386   const char *name;           /* CPU or ISA name.  */
1387   int flags;                  /* ASEs available, or ISA flag.  */
1388   int isa;                    /* ISA level.  */
1389   int cpu;                    /* CPU number (default CPU if ISA).  */
1390 };
1391
1392 #define MIPS_CPU_IS_ISA         0x0001  /* Is this an ISA?  (If 0, a CPU.) */
1393 #define MIPS_CPU_ASE_SMARTMIPS  0x0002  /* CPU implements SmartMIPS ASE */
1394 #define MIPS_CPU_ASE_DSP        0x0004  /* CPU implements DSP ASE */
1395 #define MIPS_CPU_ASE_MT         0x0008  /* CPU implements MT ASE */
1396 #define MIPS_CPU_ASE_MIPS3D     0x0010  /* CPU implements MIPS-3D ASE */
1397 #define MIPS_CPU_ASE_MDMX       0x0020  /* CPU implements MDMX ASE */
1398 #define MIPS_CPU_ASE_DSPR2      0x0040  /* CPU implements DSP R2 ASE */
1399 #define MIPS_CPU_ASE_MCU        0x0080  /* CPU implements MCU ASE */
1400
1401 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1402 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1403 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1404 \f
1405 /* Pseudo-op table.
1406
1407    The following pseudo-ops from the Kane and Heinrich MIPS book
1408    should be defined here, but are currently unsupported: .alias,
1409    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1410
1411    The following pseudo-ops from the Kane and Heinrich MIPS book are
1412    specific to the type of debugging information being generated, and
1413    should be defined by the object format: .aent, .begin, .bend,
1414    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1415    .vreg.
1416
1417    The following pseudo-ops from the Kane and Heinrich MIPS book are
1418    not MIPS CPU specific, but are also not specific to the object file
1419    format.  This file is probably the best place to define them, but
1420    they are not currently supported: .asm0, .endr, .lab, .struct.  */
1421
1422 static const pseudo_typeS mips_pseudo_table[] =
1423 {
1424   /* MIPS specific pseudo-ops.  */
1425   {"option", s_option, 0},
1426   {"set", s_mipsset, 0},
1427   {"rdata", s_change_sec, 'r'},
1428   {"sdata", s_change_sec, 's'},
1429   {"livereg", s_ignore, 0},
1430   {"abicalls", s_abicalls, 0},
1431   {"cpload", s_cpload, 0},
1432   {"cpsetup", s_cpsetup, 0},
1433   {"cplocal", s_cplocal, 0},
1434   {"cprestore", s_cprestore, 0},
1435   {"cpreturn", s_cpreturn, 0},
1436   {"dtprelword", s_dtprelword, 0},
1437   {"dtpreldword", s_dtpreldword, 0},
1438   {"gpvalue", s_gpvalue, 0},
1439   {"gpword", s_gpword, 0},
1440   {"gpdword", s_gpdword, 0},
1441   {"cpadd", s_cpadd, 0},
1442   {"insn", s_insn, 0},
1443
1444   /* Relatively generic pseudo-ops that happen to be used on MIPS
1445      chips.  */
1446   {"asciiz", stringer, 8 + 1},
1447   {"bss", s_change_sec, 'b'},
1448   {"err", s_err, 0},
1449   {"half", s_cons, 1},
1450   {"dword", s_cons, 3},
1451   {"weakext", s_mips_weakext, 0},
1452   {"origin", s_org, 0},
1453   {"repeat", s_rept, 0},
1454
1455   /* For MIPS this is non-standard, but we define it for consistency.  */
1456   {"sbss", s_change_sec, 'B'},
1457
1458   /* These pseudo-ops are defined in read.c, but must be overridden
1459      here for one reason or another.  */
1460   {"align", s_align, 0},
1461   {"byte", s_cons, 0},
1462   {"data", s_change_sec, 'd'},
1463   {"double", s_float_cons, 'd'},
1464   {"float", s_float_cons, 'f'},
1465   {"globl", s_mips_globl, 0},
1466   {"global", s_mips_globl, 0},
1467   {"hword", s_cons, 1},
1468   {"int", s_cons, 2},
1469   {"long", s_cons, 2},
1470   {"octa", s_cons, 4},
1471   {"quad", s_cons, 3},
1472   {"section", s_change_section, 0},
1473   {"short", s_cons, 1},
1474   {"single", s_float_cons, 'f'},
1475   {"stabn", s_mips_stab, 'n'},
1476   {"text", s_change_sec, 't'},
1477   {"word", s_cons, 2},
1478
1479   { "extern", ecoff_directive_extern, 0},
1480
1481   { NULL, NULL, 0 },
1482 };
1483
1484 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1485 {
1486   /* These pseudo-ops should be defined by the object file format.
1487      However, a.out doesn't support them, so we have versions here.  */
1488   {"aent", s_mips_ent, 1},
1489   {"bgnb", s_ignore, 0},
1490   {"end", s_mips_end, 0},
1491   {"endb", s_ignore, 0},
1492   {"ent", s_mips_ent, 0},
1493   {"file", s_mips_file, 0},
1494   {"fmask", s_mips_mask, 'F'},
1495   {"frame", s_mips_frame, 0},
1496   {"loc", s_mips_loc, 0},
1497   {"mask", s_mips_mask, 'R'},
1498   {"verstamp", s_ignore, 0},
1499   { NULL, NULL, 0 },
1500 };
1501
1502 /* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1503    purpose of the `.dc.a' internal pseudo-op.  */
1504
1505 int
1506 mips_address_bytes (void)
1507 {
1508   return HAVE_64BIT_ADDRESSES ? 8 : 4;
1509 }
1510
1511 extern void pop_insert (const pseudo_typeS *);
1512
1513 void
1514 mips_pop_insert (void)
1515 {
1516   pop_insert (mips_pseudo_table);
1517   if (! ECOFF_DEBUGGING)
1518     pop_insert (mips_nonecoff_pseudo_table);
1519 }
1520 \f
1521 /* Symbols labelling the current insn.  */
1522
1523 struct insn_label_list
1524 {
1525   struct insn_label_list *next;
1526   symbolS *label;
1527 };
1528
1529 static struct insn_label_list *free_insn_labels;
1530 #define label_list tc_segment_info_data.labels
1531
1532 static void mips_clear_insn_labels (void);
1533 static void mips_mark_labels (void);
1534 static void mips_compressed_mark_labels (void);
1535
1536 static inline void
1537 mips_clear_insn_labels (void)
1538 {
1539   register struct insn_label_list **pl;
1540   segment_info_type *si;
1541
1542   if (now_seg)
1543     {
1544       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1545         ;
1546       
1547       si = seg_info (now_seg);
1548       *pl = si->label_list;
1549       si->label_list = NULL;
1550     }
1551 }
1552
1553 /* Mark instruction labels in MIPS16/microMIPS mode.  */
1554
1555 static inline void
1556 mips_mark_labels (void)
1557 {
1558   if (HAVE_CODE_COMPRESSION)
1559     mips_compressed_mark_labels ();
1560 }
1561 \f
1562 static char *expr_end;
1563
1564 /* Expressions which appear in instructions.  These are set by
1565    mips_ip.  */
1566
1567 static expressionS imm_expr;
1568 static expressionS imm2_expr;
1569 static expressionS offset_expr;
1570
1571 /* Relocs associated with imm_expr and offset_expr.  */
1572
1573 static bfd_reloc_code_real_type imm_reloc[3]
1574   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1575 static bfd_reloc_code_real_type offset_reloc[3]
1576   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1577
1578 /* This is set to the resulting size of the instruction to be produced
1579    by mips16_ip if an explicit extension is used or by mips_ip if an
1580    explicit size is supplied.  */
1581
1582 static unsigned int forced_insn_length;
1583
1584 #ifdef OBJ_ELF
1585 /* The pdr segment for per procedure frame/regmask info.  Not used for
1586    ECOFF debugging.  */
1587
1588 static segT pdr_seg;
1589 #endif
1590
1591 /* The default target format to use.  */
1592
1593 #if defined (TE_FreeBSD)
1594 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1595 #elif defined (TE_TMIPS)
1596 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1597 #else
1598 #define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1599 #endif
1600
1601 const char *
1602 mips_target_format (void)
1603 {
1604   switch (OUTPUT_FLAVOR)
1605     {
1606     case bfd_target_ecoff_flavour:
1607       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1608     case bfd_target_coff_flavour:
1609       return "pe-mips";
1610     case bfd_target_elf_flavour:
1611 #ifdef TE_VXWORKS
1612       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1613         return (target_big_endian
1614                 ? "elf32-bigmips-vxworks"
1615                 : "elf32-littlemips-vxworks");
1616 #endif
1617       return (target_big_endian
1618               ? (HAVE_64BIT_OBJECTS
1619                  ? ELF_TARGET ("elf64-", "big")
1620                  : (HAVE_NEWABI
1621                     ? ELF_TARGET ("elf32-n", "big")
1622                     : ELF_TARGET ("elf32-", "big")))
1623               : (HAVE_64BIT_OBJECTS
1624                  ? ELF_TARGET ("elf64-", "little")
1625                  : (HAVE_NEWABI
1626                     ? ELF_TARGET ("elf32-n", "little")
1627                     : ELF_TARGET ("elf32-", "little"))));
1628     default:
1629       abort ();
1630       return NULL;
1631     }
1632 }
1633
1634 /* Return the length of a microMIPS instruction in bytes.  If bits of
1635    the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1636    Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1637    major opcode) will require further modifications to the opcode
1638    table.  */
1639
1640 static inline unsigned int
1641 micromips_insn_length (const struct mips_opcode *mo)
1642 {
1643   return (mo->mask >> 16) == 0 ? 2 : 4;
1644 }
1645
1646 /* Return the length of instruction INSN.  */
1647
1648 static inline unsigned int
1649 insn_length (const struct mips_cl_insn *insn)
1650 {
1651   if (mips_opts.micromips)
1652     return micromips_insn_length (insn->insn_mo);
1653   else if (mips_opts.mips16)
1654     return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1655   else
1656     return 4;
1657 }
1658
1659 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
1660
1661 static void
1662 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1663 {
1664   size_t i;
1665
1666   insn->insn_mo = mo;
1667   insn->use_extend = FALSE;
1668   insn->extend = 0;
1669   insn->insn_opcode = mo->match;
1670   insn->frag = NULL;
1671   insn->where = 0;
1672   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1673     insn->fixp[i] = NULL;
1674   insn->fixed_p = (mips_opts.noreorder > 0);
1675   insn->noreorder_p = (mips_opts.noreorder > 0);
1676   insn->mips16_absolute_jump_p = 0;
1677   insn->complete_p = 0;
1678 }
1679
1680 /* Record the current MIPS16/microMIPS mode in now_seg.  */
1681
1682 static void
1683 mips_record_compressed_mode (void)
1684 {
1685   segment_info_type *si;
1686
1687   si = seg_info (now_seg);
1688   if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
1689     si->tc_segment_info_data.mips16 = mips_opts.mips16;
1690   if (si->tc_segment_info_data.micromips != mips_opts.micromips)
1691     si->tc_segment_info_data.micromips = mips_opts.micromips;
1692 }
1693
1694 /* Install INSN at the location specified by its "frag" and "where" fields.  */
1695
1696 static void
1697 install_insn (const struct mips_cl_insn *insn)
1698 {
1699   char *f = insn->frag->fr_literal + insn->where;
1700   if (!HAVE_CODE_COMPRESSION)
1701     md_number_to_chars (f, insn->insn_opcode, 4);
1702   else if (mips_opts.micromips)
1703     {
1704       unsigned int length = insn_length (insn);
1705       if (length == 2)
1706         md_number_to_chars (f, insn->insn_opcode, 2);
1707       else if (length == 4)
1708         {
1709           md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1710           f += 2;
1711           md_number_to_chars (f, insn->insn_opcode & 0xffff, 2);
1712         }
1713       else
1714         as_bad (_("48-bit microMIPS instructions are not supported"));
1715     }
1716   else if (insn->mips16_absolute_jump_p)
1717     {
1718       md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1719       md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1720     }
1721   else
1722     {
1723       if (insn->use_extend)
1724         {
1725           md_number_to_chars (f, 0xf000 | insn->extend, 2);
1726           f += 2;
1727         }
1728       md_number_to_chars (f, insn->insn_opcode, 2);
1729     }
1730   mips_record_compressed_mode ();
1731 }
1732
1733 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
1734    and install the opcode in the new location.  */
1735
1736 static void
1737 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1738 {
1739   size_t i;
1740
1741   insn->frag = frag;
1742   insn->where = where;
1743   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1744     if (insn->fixp[i] != NULL)
1745       {
1746         insn->fixp[i]->fx_frag = frag;
1747         insn->fixp[i]->fx_where = where;
1748       }
1749   install_insn (insn);
1750 }
1751
1752 /* Add INSN to the end of the output.  */
1753
1754 static void
1755 add_fixed_insn (struct mips_cl_insn *insn)
1756 {
1757   char *f = frag_more (insn_length (insn));
1758   move_insn (insn, frag_now, f - frag_now->fr_literal);
1759 }
1760
1761 /* Start a variant frag and move INSN to the start of the variant part,
1762    marking it as fixed.  The other arguments are as for frag_var.  */
1763
1764 static void
1765 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1766                   relax_substateT subtype, symbolS *symbol, offsetT offset)
1767 {
1768   frag_grow (max_chars);
1769   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1770   insn->fixed_p = 1;
1771   frag_var (rs_machine_dependent, max_chars, var,
1772             subtype, symbol, offset, NULL);
1773 }
1774
1775 /* Insert N copies of INSN into the history buffer, starting at
1776    position FIRST.  Neither FIRST nor N need to be clipped.  */
1777
1778 static void
1779 insert_into_history (unsigned int first, unsigned int n,
1780                      const struct mips_cl_insn *insn)
1781 {
1782   if (mips_relax.sequence != 2)
1783     {
1784       unsigned int i;
1785
1786       for (i = ARRAY_SIZE (history); i-- > first;)
1787         if (i >= first + n)
1788           history[i] = history[i - n];
1789         else
1790           history[i] = *insn;
1791     }
1792 }
1793
1794 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
1795    the idea is to make it obvious at a glance that each errata is
1796    included.  */
1797
1798 static void
1799 init_vr4120_conflicts (void)
1800 {
1801 #define CONFLICT(FIRST, SECOND) \
1802     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1803
1804   /* Errata 21 - [D]DIV[U] after [D]MACC */
1805   CONFLICT (MACC, DIV);
1806   CONFLICT (DMACC, DIV);
1807
1808   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
1809   CONFLICT (DMULT, DMULT);
1810   CONFLICT (DMULT, DMACC);
1811   CONFLICT (DMACC, DMULT);
1812   CONFLICT (DMACC, DMACC);
1813
1814   /* Errata 24 - MT{LO,HI} after [D]MACC */
1815   CONFLICT (MACC, MTHILO);
1816   CONFLICT (DMACC, MTHILO);
1817
1818   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1819      instruction is executed immediately after a MACC or DMACC
1820      instruction, the result of [either instruction] is incorrect."  */
1821   CONFLICT (MACC, MULT);
1822   CONFLICT (MACC, DMULT);
1823   CONFLICT (DMACC, MULT);
1824   CONFLICT (DMACC, DMULT);
1825
1826   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1827      executed immediately after a DMULT, DMULTU, DIV, DIVU,
1828      DDIV or DDIVU instruction, the result of the MACC or
1829      DMACC instruction is incorrect.".  */
1830   CONFLICT (DMULT, MACC);
1831   CONFLICT (DMULT, DMACC);
1832   CONFLICT (DIV, MACC);
1833   CONFLICT (DIV, DMACC);
1834
1835 #undef CONFLICT
1836 }
1837
1838 struct regname {
1839   const char *name;
1840   unsigned int num;
1841 };
1842
1843 #define RTYPE_MASK      0x1ff00
1844 #define RTYPE_NUM       0x00100
1845 #define RTYPE_FPU       0x00200
1846 #define RTYPE_FCC       0x00400
1847 #define RTYPE_VEC       0x00800
1848 #define RTYPE_GP        0x01000
1849 #define RTYPE_CP0       0x02000
1850 #define RTYPE_PC        0x04000
1851 #define RTYPE_ACC       0x08000
1852 #define RTYPE_CCC       0x10000
1853 #define RNUM_MASK       0x000ff
1854 #define RWARN           0x80000
1855
1856 #define GENERIC_REGISTER_NUMBERS \
1857     {"$0",      RTYPE_NUM | 0},  \
1858     {"$1",      RTYPE_NUM | 1},  \
1859     {"$2",      RTYPE_NUM | 2},  \
1860     {"$3",      RTYPE_NUM | 3},  \
1861     {"$4",      RTYPE_NUM | 4},  \
1862     {"$5",      RTYPE_NUM | 5},  \
1863     {"$6",      RTYPE_NUM | 6},  \
1864     {"$7",      RTYPE_NUM | 7},  \
1865     {"$8",      RTYPE_NUM | 8},  \
1866     {"$9",      RTYPE_NUM | 9},  \
1867     {"$10",     RTYPE_NUM | 10}, \
1868     {"$11",     RTYPE_NUM | 11}, \
1869     {"$12",     RTYPE_NUM | 12}, \
1870     {"$13",     RTYPE_NUM | 13}, \
1871     {"$14",     RTYPE_NUM | 14}, \
1872     {"$15",     RTYPE_NUM | 15}, \
1873     {"$16",     RTYPE_NUM | 16}, \
1874     {"$17",     RTYPE_NUM | 17}, \
1875     {"$18",     RTYPE_NUM | 18}, \
1876     {"$19",     RTYPE_NUM | 19}, \
1877     {"$20",     RTYPE_NUM | 20}, \
1878     {"$21",     RTYPE_NUM | 21}, \
1879     {"$22",     RTYPE_NUM | 22}, \
1880     {"$23",     RTYPE_NUM | 23}, \
1881     {"$24",     RTYPE_NUM | 24}, \
1882     {"$25",     RTYPE_NUM | 25}, \
1883     {"$26",     RTYPE_NUM | 26}, \
1884     {"$27",     RTYPE_NUM | 27}, \
1885     {"$28",     RTYPE_NUM | 28}, \
1886     {"$29",     RTYPE_NUM | 29}, \
1887     {"$30",     RTYPE_NUM | 30}, \
1888     {"$31",     RTYPE_NUM | 31} 
1889
1890 #define FPU_REGISTER_NAMES       \
1891     {"$f0",     RTYPE_FPU | 0},  \
1892     {"$f1",     RTYPE_FPU | 1},  \
1893     {"$f2",     RTYPE_FPU | 2},  \
1894     {"$f3",     RTYPE_FPU | 3},  \
1895     {"$f4",     RTYPE_FPU | 4},  \
1896     {"$f5",     RTYPE_FPU | 5},  \
1897     {"$f6",     RTYPE_FPU | 6},  \
1898     {"$f7",     RTYPE_FPU | 7},  \
1899     {"$f8",     RTYPE_FPU | 8},  \
1900     {"$f9",     RTYPE_FPU | 9},  \
1901     {"$f10",    RTYPE_FPU | 10}, \
1902     {"$f11",    RTYPE_FPU | 11}, \
1903     {"$f12",    RTYPE_FPU | 12}, \
1904     {"$f13",    RTYPE_FPU | 13}, \
1905     {"$f14",    RTYPE_FPU | 14}, \
1906     {"$f15",    RTYPE_FPU | 15}, \
1907     {"$f16",    RTYPE_FPU | 16}, \
1908     {"$f17",    RTYPE_FPU | 17}, \
1909     {"$f18",    RTYPE_FPU | 18}, \
1910     {"$f19",    RTYPE_FPU | 19}, \
1911     {"$f20",    RTYPE_FPU | 20}, \
1912     {"$f21",    RTYPE_FPU | 21}, \
1913     {"$f22",    RTYPE_FPU | 22}, \
1914     {"$f23",    RTYPE_FPU | 23}, \
1915     {"$f24",    RTYPE_FPU | 24}, \
1916     {"$f25",    RTYPE_FPU | 25}, \
1917     {"$f26",    RTYPE_FPU | 26}, \
1918     {"$f27",    RTYPE_FPU | 27}, \
1919     {"$f28",    RTYPE_FPU | 28}, \
1920     {"$f29",    RTYPE_FPU | 29}, \
1921     {"$f30",    RTYPE_FPU | 30}, \
1922     {"$f31",    RTYPE_FPU | 31}
1923
1924 #define FPU_CONDITION_CODE_NAMES \
1925     {"$fcc0",   RTYPE_FCC | 0},  \
1926     {"$fcc1",   RTYPE_FCC | 1},  \
1927     {"$fcc2",   RTYPE_FCC | 2},  \
1928     {"$fcc3",   RTYPE_FCC | 3},  \
1929     {"$fcc4",   RTYPE_FCC | 4},  \
1930     {"$fcc5",   RTYPE_FCC | 5},  \
1931     {"$fcc6",   RTYPE_FCC | 6},  \
1932     {"$fcc7",   RTYPE_FCC | 7}
1933
1934 #define COPROC_CONDITION_CODE_NAMES         \
1935     {"$cc0",    RTYPE_FCC | RTYPE_CCC | 0}, \
1936     {"$cc1",    RTYPE_FCC | RTYPE_CCC | 1}, \
1937     {"$cc2",    RTYPE_FCC | RTYPE_CCC | 2}, \
1938     {"$cc3",    RTYPE_FCC | RTYPE_CCC | 3}, \
1939     {"$cc4",    RTYPE_FCC | RTYPE_CCC | 4}, \
1940     {"$cc5",    RTYPE_FCC | RTYPE_CCC | 5}, \
1941     {"$cc6",    RTYPE_FCC | RTYPE_CCC | 6}, \
1942     {"$cc7",    RTYPE_FCC | RTYPE_CCC | 7}
1943
1944 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1945     {"$a4",     RTYPE_GP | 8},  \
1946     {"$a5",     RTYPE_GP | 9},  \
1947     {"$a6",     RTYPE_GP | 10}, \
1948     {"$a7",     RTYPE_GP | 11}, \
1949     {"$ta0",    RTYPE_GP | 8},  /* alias for $a4 */ \
1950     {"$ta1",    RTYPE_GP | 9},  /* alias for $a5 */ \
1951     {"$ta2",    RTYPE_GP | 10}, /* alias for $a6 */ \
1952     {"$ta3",    RTYPE_GP | 11}, /* alias for $a7 */ \
1953     {"$t0",     RTYPE_GP | 12}, \
1954     {"$t1",     RTYPE_GP | 13}, \
1955     {"$t2",     RTYPE_GP | 14}, \
1956     {"$t3",     RTYPE_GP | 15}
1957
1958 #define O32_SYMBOLIC_REGISTER_NAMES \
1959     {"$t0",     RTYPE_GP | 8},  \
1960     {"$t1",     RTYPE_GP | 9},  \
1961     {"$t2",     RTYPE_GP | 10}, \
1962     {"$t3",     RTYPE_GP | 11}, \
1963     {"$t4",     RTYPE_GP | 12}, \
1964     {"$t5",     RTYPE_GP | 13}, \
1965     {"$t6",     RTYPE_GP | 14}, \
1966     {"$t7",     RTYPE_GP | 15}, \
1967     {"$ta0",    RTYPE_GP | 12}, /* alias for $t4 */ \
1968     {"$ta1",    RTYPE_GP | 13}, /* alias for $t5 */ \
1969     {"$ta2",    RTYPE_GP | 14}, /* alias for $t6 */ \
1970     {"$ta3",    RTYPE_GP | 15}  /* alias for $t7 */ 
1971
1972 /* Remaining symbolic register names */
1973 #define SYMBOLIC_REGISTER_NAMES \
1974     {"$zero",   RTYPE_GP | 0},  \
1975     {"$at",     RTYPE_GP | 1},  \
1976     {"$AT",     RTYPE_GP | 1},  \
1977     {"$v0",     RTYPE_GP | 2},  \
1978     {"$v1",     RTYPE_GP | 3},  \
1979     {"$a0",     RTYPE_GP | 4},  \
1980     {"$a1",     RTYPE_GP | 5},  \
1981     {"$a2",     RTYPE_GP | 6},  \
1982     {"$a3",     RTYPE_GP | 7},  \
1983     {"$s0",     RTYPE_GP | 16}, \
1984     {"$s1",     RTYPE_GP | 17}, \
1985     {"$s2",     RTYPE_GP | 18}, \
1986     {"$s3",     RTYPE_GP | 19}, \
1987     {"$s4",     RTYPE_GP | 20}, \
1988     {"$s5",     RTYPE_GP | 21}, \
1989     {"$s6",     RTYPE_GP | 22}, \
1990     {"$s7",     RTYPE_GP | 23}, \
1991     {"$t8",     RTYPE_GP | 24}, \
1992     {"$t9",     RTYPE_GP | 25}, \
1993     {"$k0",     RTYPE_GP | 26}, \
1994     {"$kt0",    RTYPE_GP | 26}, \
1995     {"$k1",     RTYPE_GP | 27}, \
1996     {"$kt1",    RTYPE_GP | 27}, \
1997     {"$gp",     RTYPE_GP | 28}, \
1998     {"$sp",     RTYPE_GP | 29}, \
1999     {"$s8",     RTYPE_GP | 30}, \
2000     {"$fp",     RTYPE_GP | 30}, \
2001     {"$ra",     RTYPE_GP | 31}
2002
2003 #define MIPS16_SPECIAL_REGISTER_NAMES \
2004     {"$pc",     RTYPE_PC | 0}
2005
2006 #define MDMX_VECTOR_REGISTER_NAMES \
2007     /* {"$v0",  RTYPE_VEC | 0},  clash with REG 2 above */ \
2008     /* {"$v1",  RTYPE_VEC | 1},  clash with REG 3 above */ \
2009     {"$v2",     RTYPE_VEC | 2},  \
2010     {"$v3",     RTYPE_VEC | 3},  \
2011     {"$v4",     RTYPE_VEC | 4},  \
2012     {"$v5",     RTYPE_VEC | 5},  \
2013     {"$v6",     RTYPE_VEC | 6},  \
2014     {"$v7",     RTYPE_VEC | 7},  \
2015     {"$v8",     RTYPE_VEC | 8},  \
2016     {"$v9",     RTYPE_VEC | 9},  \
2017     {"$v10",    RTYPE_VEC | 10}, \
2018     {"$v11",    RTYPE_VEC | 11}, \
2019     {"$v12",    RTYPE_VEC | 12}, \
2020     {"$v13",    RTYPE_VEC | 13}, \
2021     {"$v14",    RTYPE_VEC | 14}, \
2022     {"$v15",    RTYPE_VEC | 15}, \
2023     {"$v16",    RTYPE_VEC | 16}, \
2024     {"$v17",    RTYPE_VEC | 17}, \
2025     {"$v18",    RTYPE_VEC | 18}, \
2026     {"$v19",    RTYPE_VEC | 19}, \
2027     {"$v20",    RTYPE_VEC | 20}, \
2028     {"$v21",    RTYPE_VEC | 21}, \
2029     {"$v22",    RTYPE_VEC | 22}, \
2030     {"$v23",    RTYPE_VEC | 23}, \
2031     {"$v24",    RTYPE_VEC | 24}, \
2032     {"$v25",    RTYPE_VEC | 25}, \
2033     {"$v26",    RTYPE_VEC | 26}, \
2034     {"$v27",    RTYPE_VEC | 27}, \
2035     {"$v28",    RTYPE_VEC | 28}, \
2036     {"$v29",    RTYPE_VEC | 29}, \
2037     {"$v30",    RTYPE_VEC | 30}, \
2038     {"$v31",    RTYPE_VEC | 31}
2039
2040 #define MIPS_DSP_ACCUMULATOR_NAMES \
2041     {"$ac0",    RTYPE_ACC | 0}, \
2042     {"$ac1",    RTYPE_ACC | 1}, \
2043     {"$ac2",    RTYPE_ACC | 2}, \
2044     {"$ac3",    RTYPE_ACC | 3}
2045
2046 static const struct regname reg_names[] = {
2047   GENERIC_REGISTER_NUMBERS,
2048   FPU_REGISTER_NAMES,
2049   FPU_CONDITION_CODE_NAMES,
2050   COPROC_CONDITION_CODE_NAMES,
2051
2052   /* The $txx registers depends on the abi,
2053      these will be added later into the symbol table from
2054      one of the tables below once mips_abi is set after 
2055      parsing of arguments from the command line. */
2056   SYMBOLIC_REGISTER_NAMES,
2057
2058   MIPS16_SPECIAL_REGISTER_NAMES,
2059   MDMX_VECTOR_REGISTER_NAMES,
2060   MIPS_DSP_ACCUMULATOR_NAMES,
2061   {0, 0}
2062 };
2063
2064 static const struct regname reg_names_o32[] = {
2065   O32_SYMBOLIC_REGISTER_NAMES,
2066   {0, 0}
2067 };
2068
2069 static const struct regname reg_names_n32n64[] = {
2070   N32N64_SYMBOLIC_REGISTER_NAMES,
2071   {0, 0}
2072 };
2073
2074 /* Check if S points at a valid register specifier according to TYPES.
2075    If so, then return 1, advance S to consume the specifier and store
2076    the register's number in REGNOP, otherwise return 0.  */
2077
2078 static int
2079 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2080 {
2081   symbolS *symbolP;
2082   char *e;
2083   char save_c;
2084   int reg = -1;
2085
2086   /* Find end of name.  */
2087   e = *s;
2088   if (is_name_beginner (*e))
2089     ++e;
2090   while (is_part_of_name (*e))
2091     ++e;
2092
2093   /* Terminate name.  */
2094   save_c = *e;
2095   *e = '\0';
2096
2097   /* Look for a register symbol.  */
2098   if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2099     {
2100       int r = S_GET_VALUE (symbolP);
2101       if (r & types)
2102         reg = r & RNUM_MASK;
2103       else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2104         /* Convert GP reg $v0/1 to MDMX reg $v0/1!  */
2105         reg = (r & RNUM_MASK) - 2;
2106     }
2107   /* Else see if this is a register defined in an itbl entry.  */
2108   else if ((types & RTYPE_GP) && itbl_have_entries)
2109     {
2110       char *n = *s;
2111       unsigned long r;
2112
2113       if (*n == '$')
2114         ++n;
2115       if (itbl_get_reg_val (n, &r))
2116         reg = r & RNUM_MASK;
2117     }
2118
2119   /* Advance to next token if a register was recognised.  */
2120   if (reg >= 0)
2121     *s = e;
2122   else if (types & RWARN)
2123     as_warn (_("Unrecognized register name `%s'"), *s);
2124
2125   *e = save_c;
2126   if (regnop)
2127     *regnop = reg;
2128   return reg >= 0;
2129 }
2130
2131 /* Check if S points at a valid register list according to TYPES.
2132    If so, then return 1, advance S to consume the list and store
2133    the registers present on the list as a bitmask of ones in REGLISTP,
2134    otherwise return 0.  A valid list comprises a comma-separated
2135    enumeration of valid single registers and/or dash-separated
2136    contiguous register ranges as determined by their numbers.
2137
2138    As a special exception if one of s0-s7 registers is specified as
2139    the range's lower delimiter and s8 (fp) is its upper one, then no
2140    registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2141    are selected; they have to be listed separately if needed.  */
2142
2143 static int
2144 reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2145 {
2146   unsigned int reglist = 0;
2147   unsigned int lastregno;
2148   bfd_boolean ok = TRUE;
2149   unsigned int regmask;
2150   char *s_endlist = *s;
2151   char *s_reset = *s;
2152   unsigned int regno;
2153
2154   while (reg_lookup (s, types, &regno))
2155     {
2156       lastregno = regno;
2157       if (**s == '-')
2158         {
2159           (*s)++;
2160           ok = reg_lookup (s, types, &lastregno);
2161           if (ok && lastregno < regno)
2162             ok = FALSE;
2163           if (!ok)
2164             break;
2165         }
2166
2167       if (lastregno == FP && regno >= S0 && regno <= S7)
2168         {
2169           lastregno = S7;
2170           reglist |= 1 << FP;
2171         }
2172       regmask = 1 << lastregno;
2173       regmask = (regmask << 1) - 1;
2174       regmask ^= (1 << regno) - 1;
2175       reglist |= regmask;
2176
2177       s_endlist = *s;
2178       if (**s != ',')
2179         break;
2180       (*s)++;
2181     }
2182
2183   if (ok)
2184     *s = s_endlist;
2185   else
2186     *s = s_reset;
2187   if (reglistp)
2188     *reglistp = reglist;
2189   return ok && reglist != 0;
2190 }
2191
2192 /* Return TRUE if opcode MO is valid on the currently selected ISA and
2193    architecture.  Use is_opcode_valid_16 for MIPS16 opcodes.  */
2194
2195 static bfd_boolean
2196 is_opcode_valid (const struct mips_opcode *mo)
2197 {
2198   int isa = mips_opts.isa;
2199   int fp_s, fp_d;
2200
2201   if (mips_opts.ase_mdmx)
2202     isa |= INSN_MDMX;
2203   if (mips_opts.ase_dsp)
2204     isa |= INSN_DSP;
2205   if (mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
2206     isa |= INSN_DSP64;
2207   if (mips_opts.ase_dspr2)
2208     isa |= INSN_DSPR2;
2209   if (mips_opts.ase_mt)
2210     isa |= INSN_MT;
2211   if (mips_opts.ase_mips3d)
2212     isa |= INSN_MIPS3D;
2213   if (mips_opts.ase_smartmips)
2214     isa |= INSN_SMARTMIPS;
2215   if (mips_opts.ase_mcu)
2216     isa |= INSN_MCU;
2217
2218   /* Don't accept instructions based on the ISA if the CPU does not implement
2219      all the coprocessor insns. */
2220   if (NO_ISA_COP (mips_opts.arch)
2221       && COP_INSN (mo->pinfo))
2222     isa = 0;
2223
2224   if (!OPCODE_IS_MEMBER (mo, isa, mips_opts.arch))
2225     return FALSE;
2226
2227   /* Check whether the instruction or macro requires single-precision or
2228      double-precision floating-point support.  Note that this information is
2229      stored differently in the opcode table for insns and macros.  */
2230   if (mo->pinfo == INSN_MACRO)
2231     {
2232       fp_s = mo->pinfo2 & INSN2_M_FP_S;
2233       fp_d = mo->pinfo2 & INSN2_M_FP_D;
2234     }
2235   else
2236     {
2237       fp_s = mo->pinfo & FP_S;
2238       fp_d = mo->pinfo & FP_D;
2239     }
2240
2241   if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2242     return FALSE;
2243
2244   if (fp_s && mips_opts.soft_float)
2245     return FALSE;
2246
2247   return TRUE;
2248 }
2249
2250 /* Return TRUE if the MIPS16 opcode MO is valid on the currently
2251    selected ISA and architecture.  */
2252
2253 static bfd_boolean
2254 is_opcode_valid_16 (const struct mips_opcode *mo)
2255 {
2256   return OPCODE_IS_MEMBER (mo, mips_opts.isa, mips_opts.arch) ? TRUE : FALSE;
2257 }
2258
2259 /* Return TRUE if the size of the microMIPS opcode MO matches one
2260    explicitly requested.  Always TRUE in the standard MIPS mode.  */
2261
2262 static bfd_boolean
2263 is_size_valid (const struct mips_opcode *mo)
2264 {
2265   if (!mips_opts.micromips)
2266     return TRUE;
2267
2268   if (!forced_insn_length)
2269     return TRUE;
2270   if (mo->pinfo == INSN_MACRO)
2271     return FALSE;
2272   return forced_insn_length == micromips_insn_length (mo);
2273 }
2274
2275 /* Return TRUE if the microMIPS opcode MO is valid for the delay slot
2276    of the preceding instruction.  Always TRUE in the standard MIPS mode.  */
2277
2278 static bfd_boolean
2279 is_delay_slot_valid (const struct mips_opcode *mo)
2280 {
2281   if (!mips_opts.micromips)
2282     return TRUE;
2283
2284   if (mo->pinfo == INSN_MACRO)
2285     return TRUE;
2286   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2287       && micromips_insn_length (mo) != 4)
2288     return FALSE;
2289   if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2290       && micromips_insn_length (mo) != 2)
2291     return FALSE;
2292
2293   return TRUE;
2294 }
2295
2296 /* This function is called once, at assembler startup time.  It should set up
2297    all the tables, etc. that the MD part of the assembler will need.  */
2298
2299 void
2300 md_begin (void)
2301 {
2302   const char *retval = NULL;
2303   int i = 0;
2304   int broken = 0;
2305
2306   if (mips_pic != NO_PIC)
2307     {
2308       if (g_switch_seen && g_switch_value != 0)
2309         as_bad (_("-G may not be used in position-independent code"));
2310       g_switch_value = 0;
2311     }
2312
2313   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
2314     as_warn (_("Could not set architecture and machine"));
2315
2316   op_hash = hash_new ();
2317
2318   for (i = 0; i < NUMOPCODES;)
2319     {
2320       const char *name = mips_opcodes[i].name;
2321
2322       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
2323       if (retval != NULL)
2324         {
2325           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2326                    mips_opcodes[i].name, retval);
2327           /* Probably a memory allocation problem?  Give up now.  */
2328           as_fatal (_("Broken assembler.  No assembly attempted."));
2329         }
2330       do
2331         {
2332           if (mips_opcodes[i].pinfo != INSN_MACRO)
2333             {
2334               if (!validate_mips_insn (&mips_opcodes[i]))
2335                 broken = 1;
2336               if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2337                 {
2338                   create_insn (&nop_insn, mips_opcodes + i);
2339                   if (mips_fix_loongson2f_nop)
2340                     nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
2341                   nop_insn.fixed_p = 1;
2342                 }
2343             }
2344           ++i;
2345         }
2346       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2347     }
2348
2349   mips16_op_hash = hash_new ();
2350
2351   i = 0;
2352   while (i < bfd_mips16_num_opcodes)
2353     {
2354       const char *name = mips16_opcodes[i].name;
2355
2356       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
2357       if (retval != NULL)
2358         as_fatal (_("internal: can't hash `%s': %s"),
2359                   mips16_opcodes[i].name, retval);
2360       do
2361         {
2362           if (mips16_opcodes[i].pinfo != INSN_MACRO
2363               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2364                   != mips16_opcodes[i].match))
2365             {
2366               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2367                        mips16_opcodes[i].name, mips16_opcodes[i].args);
2368               broken = 1;
2369             }
2370           if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2371             {
2372               create_insn (&mips16_nop_insn, mips16_opcodes + i);
2373               mips16_nop_insn.fixed_p = 1;
2374             }
2375           ++i;
2376         }
2377       while (i < bfd_mips16_num_opcodes
2378              && strcmp (mips16_opcodes[i].name, name) == 0);
2379     }
2380
2381   micromips_op_hash = hash_new ();
2382
2383   i = 0;
2384   while (i < bfd_micromips_num_opcodes)
2385     {
2386       const char *name = micromips_opcodes[i].name;
2387
2388       retval = hash_insert (micromips_op_hash, name,
2389                             (void *) &micromips_opcodes[i]);
2390       if (retval != NULL)
2391         as_fatal (_("internal: can't hash `%s': %s"),
2392                   micromips_opcodes[i].name, retval);
2393       do
2394         if (micromips_opcodes[i].pinfo != INSN_MACRO)
2395           {
2396             struct mips_cl_insn *micromips_nop_insn;
2397
2398             if (!validate_micromips_insn (&micromips_opcodes[i]))
2399               broken = 1;
2400
2401             if (micromips_insn_length (micromips_opcodes + i) == 2)
2402               micromips_nop_insn = &micromips_nop16_insn;
2403             else if (micromips_insn_length (micromips_opcodes + i) == 4)
2404               micromips_nop_insn = &micromips_nop32_insn;
2405             else
2406               continue;
2407
2408             if (micromips_nop_insn->insn_mo == NULL
2409                 && strcmp (name, "nop") == 0)
2410               {
2411                 create_insn (micromips_nop_insn, micromips_opcodes + i);
2412                 micromips_nop_insn->fixed_p = 1;
2413               }
2414           }
2415       while (++i < bfd_micromips_num_opcodes
2416              && strcmp (micromips_opcodes[i].name, name) == 0);
2417     }
2418
2419   if (broken)
2420     as_fatal (_("Broken assembler.  No assembly attempted."));
2421
2422   /* We add all the general register names to the symbol table.  This
2423      helps us detect invalid uses of them.  */
2424   for (i = 0; reg_names[i].name; i++) 
2425     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
2426                                      reg_names[i].num, /* & RNUM_MASK, */
2427                                      &zero_address_frag));
2428   if (HAVE_NEWABI)
2429     for (i = 0; reg_names_n32n64[i].name; i++) 
2430       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
2431                                        reg_names_n32n64[i].num, /* & RNUM_MASK, */
2432                                        &zero_address_frag));
2433   else
2434     for (i = 0; reg_names_o32[i].name; i++) 
2435       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
2436                                        reg_names_o32[i].num, /* & RNUM_MASK, */
2437                                        &zero_address_frag));
2438
2439   mips_no_prev_insn ();
2440
2441   mips_gprmask = 0;
2442   mips_cprmask[0] = 0;
2443   mips_cprmask[1] = 0;
2444   mips_cprmask[2] = 0;
2445   mips_cprmask[3] = 0;
2446
2447   /* set the default alignment for the text section (2**2) */
2448   record_alignment (text_section, 2);
2449
2450   bfd_set_gp_size (stdoutput, g_switch_value);
2451
2452 #ifdef OBJ_ELF
2453   if (IS_ELF)
2454     {
2455       /* On a native system other than VxWorks, sections must be aligned
2456          to 16 byte boundaries.  When configured for an embedded ELF
2457          target, we don't bother.  */
2458       if (strncmp (TARGET_OS, "elf", 3) != 0
2459           && strncmp (TARGET_OS, "vxworks", 7) != 0)
2460         {
2461           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2462           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2463           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2464         }
2465
2466       /* Create a .reginfo section for register masks and a .mdebug
2467          section for debugging information.  */
2468       {
2469         segT seg;
2470         subsegT subseg;
2471         flagword flags;
2472         segT sec;
2473
2474         seg = now_seg;
2475         subseg = now_subseg;
2476
2477         /* The ABI says this section should be loaded so that the
2478            running program can access it.  However, we don't load it
2479            if we are configured for an embedded target */
2480         flags = SEC_READONLY | SEC_DATA;
2481         if (strncmp (TARGET_OS, "elf", 3) != 0)
2482           flags |= SEC_ALLOC | SEC_LOAD;
2483
2484         if (mips_abi != N64_ABI)
2485           {
2486             sec = subseg_new (".reginfo", (subsegT) 0);
2487
2488             bfd_set_section_flags (stdoutput, sec, flags);
2489             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
2490
2491             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2492           }
2493         else
2494           {
2495             /* The 64-bit ABI uses a .MIPS.options section rather than
2496                .reginfo section.  */
2497             sec = subseg_new (".MIPS.options", (subsegT) 0);
2498             bfd_set_section_flags (stdoutput, sec, flags);
2499             bfd_set_section_alignment (stdoutput, sec, 3);
2500
2501             /* Set up the option header.  */
2502             {
2503               Elf_Internal_Options opthdr;
2504               char *f;
2505
2506               opthdr.kind = ODK_REGINFO;
2507               opthdr.size = (sizeof (Elf_External_Options)
2508                              + sizeof (Elf64_External_RegInfo));
2509               opthdr.section = 0;
2510               opthdr.info = 0;
2511               f = frag_more (sizeof (Elf_External_Options));
2512               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2513                                              (Elf_External_Options *) f);
2514
2515               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2516             }
2517           }
2518
2519         if (ECOFF_DEBUGGING)
2520           {
2521             sec = subseg_new (".mdebug", (subsegT) 0);
2522             (void) bfd_set_section_flags (stdoutput, sec,
2523                                           SEC_HAS_CONTENTS | SEC_READONLY);
2524             (void) bfd_set_section_alignment (stdoutput, sec, 2);
2525           }
2526         else if (mips_flag_pdr)
2527           {
2528             pdr_seg = subseg_new (".pdr", (subsegT) 0);
2529             (void) bfd_set_section_flags (stdoutput, pdr_seg,
2530                                           SEC_READONLY | SEC_RELOC
2531                                           | SEC_DEBUGGING);
2532             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2533           }
2534
2535         subseg_set (seg, subseg);
2536       }
2537     }
2538 #endif /* OBJ_ELF */
2539
2540   if (! ECOFF_DEBUGGING)
2541     md_obj_begin ();
2542
2543   if (mips_fix_vr4120)
2544     init_vr4120_conflicts ();
2545 }
2546
2547 void
2548 md_mips_end (void)
2549 {
2550   mips_emit_delays ();
2551   if (! ECOFF_DEBUGGING)
2552     md_obj_end ();
2553 }
2554
2555 void
2556 md_assemble (char *str)
2557 {
2558   struct mips_cl_insn insn;
2559   bfd_reloc_code_real_type unused_reloc[3]
2560     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2561
2562   imm_expr.X_op = O_absent;
2563   imm2_expr.X_op = O_absent;
2564   offset_expr.X_op = O_absent;
2565   imm_reloc[0] = BFD_RELOC_UNUSED;
2566   imm_reloc[1] = BFD_RELOC_UNUSED;
2567   imm_reloc[2] = BFD_RELOC_UNUSED;
2568   offset_reloc[0] = BFD_RELOC_UNUSED;
2569   offset_reloc[1] = BFD_RELOC_UNUSED;
2570   offset_reloc[2] = BFD_RELOC_UNUSED;
2571
2572   if (mips_opts.mips16)
2573     mips16_ip (str, &insn);
2574   else
2575     {
2576       mips_ip (str, &insn);
2577       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2578             str, insn.insn_opcode));
2579     }
2580
2581   if (insn_error)
2582     {
2583       as_bad ("%s `%s'", insn_error, str);
2584       return;
2585     }
2586
2587   if (insn.insn_mo->pinfo == INSN_MACRO)
2588     {
2589       macro_start ();
2590       if (mips_opts.mips16)
2591         mips16_macro (&insn);
2592       else
2593         macro (&insn);
2594       macro_end ();
2595     }
2596   else
2597     {
2598       if (imm_expr.X_op != O_absent)
2599         append_insn (&insn, &imm_expr, imm_reloc, FALSE);
2600       else if (offset_expr.X_op != O_absent)
2601         append_insn (&insn, &offset_expr, offset_reloc, FALSE);
2602       else
2603         append_insn (&insn, NULL, unused_reloc, FALSE);
2604     }
2605 }
2606
2607 /* Convenience functions for abstracting away the differences between
2608    MIPS16 and non-MIPS16 relocations.  */
2609
2610 static inline bfd_boolean
2611 mips16_reloc_p (bfd_reloc_code_real_type reloc)
2612 {
2613   switch (reloc)
2614     {
2615     case BFD_RELOC_MIPS16_JMP:
2616     case BFD_RELOC_MIPS16_GPREL:
2617     case BFD_RELOC_MIPS16_GOT16:
2618     case BFD_RELOC_MIPS16_CALL16:
2619     case BFD_RELOC_MIPS16_HI16_S:
2620     case BFD_RELOC_MIPS16_HI16:
2621     case BFD_RELOC_MIPS16_LO16:
2622       return TRUE;
2623
2624     default:
2625       return FALSE;
2626     }
2627 }
2628
2629 static inline bfd_boolean
2630 micromips_reloc_p (bfd_reloc_code_real_type reloc)
2631 {
2632   switch (reloc)
2633     {
2634     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
2635     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
2636     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
2637     case BFD_RELOC_MICROMIPS_GPREL16:
2638     case BFD_RELOC_MICROMIPS_JMP:
2639     case BFD_RELOC_MICROMIPS_HI16:
2640     case BFD_RELOC_MICROMIPS_HI16_S:
2641     case BFD_RELOC_MICROMIPS_LO16:
2642     case BFD_RELOC_MICROMIPS_LITERAL:
2643     case BFD_RELOC_MICROMIPS_GOT16:
2644     case BFD_RELOC_MICROMIPS_CALL16:
2645     case BFD_RELOC_MICROMIPS_GOT_HI16:
2646     case BFD_RELOC_MICROMIPS_GOT_LO16:
2647     case BFD_RELOC_MICROMIPS_CALL_HI16:
2648     case BFD_RELOC_MICROMIPS_CALL_LO16:
2649     case BFD_RELOC_MICROMIPS_SUB:
2650     case BFD_RELOC_MICROMIPS_GOT_PAGE:
2651     case BFD_RELOC_MICROMIPS_GOT_OFST:
2652     case BFD_RELOC_MICROMIPS_GOT_DISP:
2653     case BFD_RELOC_MICROMIPS_HIGHEST:
2654     case BFD_RELOC_MICROMIPS_HIGHER:
2655     case BFD_RELOC_MICROMIPS_SCN_DISP:
2656     case BFD_RELOC_MICROMIPS_JALR:
2657       return TRUE;
2658
2659     default:
2660       return FALSE;
2661     }
2662 }
2663
2664 static inline bfd_boolean
2665 jmp_reloc_p (bfd_reloc_code_real_type reloc)
2666 {
2667   return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
2668 }
2669
2670 static inline bfd_boolean
2671 got16_reloc_p (bfd_reloc_code_real_type reloc)
2672 {
2673   return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
2674           || reloc == BFD_RELOC_MICROMIPS_GOT16);
2675 }
2676
2677 static inline bfd_boolean
2678 hi16_reloc_p (bfd_reloc_code_real_type reloc)
2679 {
2680   return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
2681           || reloc == BFD_RELOC_MICROMIPS_HI16_S);
2682 }
2683
2684 static inline bfd_boolean
2685 lo16_reloc_p (bfd_reloc_code_real_type reloc)
2686 {
2687   return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
2688           || reloc == BFD_RELOC_MICROMIPS_LO16);
2689 }
2690
2691 static inline bfd_boolean
2692 jalr_reloc_p (bfd_reloc_code_real_type reloc)
2693 {
2694   return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
2695 }
2696
2697 /* Return true if the given relocation might need a matching %lo().
2698    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
2699    need a matching %lo() when applied to local symbols.  */
2700
2701 static inline bfd_boolean
2702 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2703 {
2704   return (HAVE_IN_PLACE_ADDENDS
2705           && (hi16_reloc_p (reloc)
2706               /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2707                  all GOT16 relocations evaluate to "G".  */
2708               || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
2709 }
2710
2711 /* Return the type of %lo() reloc needed by RELOC, given that
2712    reloc_needs_lo_p.  */
2713
2714 static inline bfd_reloc_code_real_type
2715 matching_lo_reloc (bfd_reloc_code_real_type reloc)
2716 {
2717   return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
2718           : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
2719              : BFD_RELOC_LO16));
2720 }
2721
2722 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2723    relocation.  */
2724
2725 static inline bfd_boolean
2726 fixup_has_matching_lo_p (fixS *fixp)
2727 {
2728   return (fixp->fx_next != NULL
2729           && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
2730           && fixp->fx_addsy == fixp->fx_next->fx_addsy
2731           && fixp->fx_offset == fixp->fx_next->fx_offset);
2732 }
2733
2734 /* This function returns true if modifying a register requires a
2735    delay.  */
2736
2737 static int
2738 reg_needs_delay (unsigned int reg)
2739 {
2740   unsigned long prev_pinfo;
2741
2742   prev_pinfo = history[0].insn_mo->pinfo;
2743   if (! mips_opts.noreorder
2744       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2745            && ! gpr_interlocks)
2746           || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2747               && ! cop_interlocks)))
2748     {
2749       /* A load from a coprocessor or from memory.  All load delays
2750          delay the use of general register rt for one instruction.  */
2751       /* Itbl support may require additional care here.  */
2752       know (prev_pinfo & INSN_WRITE_GPR_T);
2753       if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
2754         return 1;
2755     }
2756
2757   return 0;
2758 }
2759
2760 /* Move all labels in insn_labels to the current insertion point.  */
2761
2762 static void
2763 mips_move_labels (void)
2764 {
2765   segment_info_type *si = seg_info (now_seg);
2766   struct insn_label_list *l;
2767   valueT val;
2768
2769   for (l = si->label_list; l != NULL; l = l->next)
2770     {
2771       gas_assert (S_GET_SEGMENT (l->label) == now_seg);
2772       symbol_set_frag (l->label, frag_now);
2773       val = (valueT) frag_now_fix ();
2774       /* MIPS16/microMIPS text labels are stored as odd.  */
2775       if (HAVE_CODE_COMPRESSION)
2776         ++val;
2777       S_SET_VALUE (l->label, val);
2778     }
2779 }
2780
2781 static bfd_boolean
2782 s_is_linkonce (symbolS *sym, segT from_seg)
2783 {
2784   bfd_boolean linkonce = FALSE;
2785   segT symseg = S_GET_SEGMENT (sym);
2786
2787   if (symseg != from_seg && !S_IS_LOCAL (sym))
2788     {
2789       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2790         linkonce = TRUE;
2791 #ifdef OBJ_ELF
2792       /* The GNU toolchain uses an extension for ELF: a section
2793          beginning with the magic string .gnu.linkonce is a
2794          linkonce section.  */
2795       if (strncmp (segment_name (symseg), ".gnu.linkonce",
2796                    sizeof ".gnu.linkonce" - 1) == 0)
2797         linkonce = TRUE;
2798 #endif
2799     }
2800   return linkonce;
2801 }
2802
2803 /* Mark instruction labels in MIPS16/microMIPS mode.  This permits the
2804    linker to handle them specially, such as generating jalx instructions
2805    when needed.  We also make them odd for the duration of the assembly,
2806    in order to generate the right sort of code.  We will make them even
2807    in the adjust_symtab routine, while leaving them marked.  This is
2808    convenient for the debugger and the disassembler.  The linker knows
2809    to make them odd again.  */
2810
2811 static void
2812 mips_compressed_mark_labels (void)
2813 {
2814   segment_info_type *si = seg_info (now_seg);
2815   struct insn_label_list *l;
2816
2817   gas_assert (HAVE_CODE_COMPRESSION);
2818
2819   for (l = si->label_list; l != NULL; l = l->next)
2820    {
2821       symbolS *label = l->label;
2822
2823 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2824       if (IS_ELF)
2825         {
2826           if (mips_opts.mips16)
2827             S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
2828           else
2829             S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
2830         }
2831 #endif
2832       if ((S_GET_VALUE (label) & 1) == 0
2833         /* Don't adjust the address if the label is global or weak, or
2834            in a link-once section, since we'll be emitting symbol reloc
2835            references to it which will be patched up by the linker, and
2836            the final value of the symbol may or may not be MIPS16/microMIPS.  */
2837           && ! S_IS_WEAK (label)
2838           && ! S_IS_EXTERNAL (label)
2839           && ! s_is_linkonce (label, now_seg))
2840         S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2841     }
2842 }
2843
2844 /* End the current frag.  Make it a variant frag and record the
2845    relaxation info.  */
2846
2847 static void
2848 relax_close_frag (void)
2849 {
2850   mips_macro_warning.first_frag = frag_now;
2851   frag_var (rs_machine_dependent, 0, 0,
2852             RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2853             mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2854
2855   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2856   mips_relax.first_fixup = 0;
2857 }
2858
2859 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2860    See the comment above RELAX_ENCODE for more details.  */
2861
2862 static void
2863 relax_start (symbolS *symbol)
2864 {
2865   gas_assert (mips_relax.sequence == 0);
2866   mips_relax.sequence = 1;
2867   mips_relax.symbol = symbol;
2868 }
2869
2870 /* Start generating the second version of a relaxable sequence.
2871    See the comment above RELAX_ENCODE for more details.  */
2872
2873 static void
2874 relax_switch (void)
2875 {
2876   gas_assert (mips_relax.sequence == 1);
2877   mips_relax.sequence = 2;
2878 }
2879
2880 /* End the current relaxable sequence.  */
2881
2882 static void
2883 relax_end (void)
2884 {
2885   gas_assert (mips_relax.sequence == 2);
2886   relax_close_frag ();
2887   mips_relax.sequence = 0;
2888 }
2889
2890 /* Return true if IP is a delayed branch or jump.  */
2891
2892 static inline bfd_boolean
2893 delayed_branch_p (const struct mips_cl_insn *ip)
2894 {
2895   return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2896                                 | INSN_COND_BRANCH_DELAY
2897                                 | INSN_COND_BRANCH_LIKELY)) != 0;
2898 }
2899
2900 /* Return true if IP is a compact branch or jump.  */
2901
2902 static inline bfd_boolean
2903 compact_branch_p (const struct mips_cl_insn *ip)
2904 {
2905   if (mips_opts.mips16)
2906     return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
2907                                   | MIPS16_INSN_COND_BRANCH)) != 0;
2908   else
2909     return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
2910                                    | INSN2_COND_BRANCH)) != 0;
2911 }
2912
2913 /* Return true if IP is an unconditional branch or jump.  */
2914
2915 static inline bfd_boolean
2916 uncond_branch_p (const struct mips_cl_insn *ip)
2917 {
2918   return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
2919           || (mips_opts.mips16
2920               ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
2921               : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
2922 }
2923
2924 /* Return true if IP is a branch-likely instruction.  */
2925
2926 static inline bfd_boolean
2927 branch_likely_p (const struct mips_cl_insn *ip)
2928 {
2929   return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
2930 }
2931
2932 /* Return the type of nop that should be used to fill the delay slot
2933    of delayed branch IP.  */
2934
2935 static struct mips_cl_insn *
2936 get_delay_slot_nop (const struct mips_cl_insn *ip)
2937 {
2938   if (mips_opts.micromips
2939       && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
2940     return &micromips_nop32_insn;
2941   return NOP_INSN;
2942 }
2943
2944 /* Return the mask of core registers that IP reads or writes.  */
2945
2946 static unsigned int
2947 gpr_mod_mask (const struct mips_cl_insn *ip)
2948 {
2949   unsigned long pinfo2;
2950   unsigned int mask;
2951
2952   mask = 0;
2953   pinfo2 = ip->insn_mo->pinfo2;
2954   if (mips_opts.micromips)
2955     {
2956       if (pinfo2 & INSN2_MOD_GPR_MD)
2957         mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
2958       if (pinfo2 & INSN2_MOD_GPR_MF)
2959         mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
2960       if (pinfo2 & INSN2_MOD_SP)
2961         mask |= 1 << SP;
2962     }
2963   return mask;
2964 }
2965
2966 /* Return the mask of core registers that IP reads.  */
2967
2968 static unsigned int
2969 gpr_read_mask (const struct mips_cl_insn *ip)
2970 {
2971   unsigned long pinfo, pinfo2;
2972   unsigned int mask;
2973
2974   mask = gpr_mod_mask (ip);
2975   pinfo = ip->insn_mo->pinfo;
2976   pinfo2 = ip->insn_mo->pinfo2;
2977   if (mips_opts.mips16)
2978     {
2979       if (pinfo & MIPS16_INSN_READ_X)
2980         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
2981       if (pinfo & MIPS16_INSN_READ_Y)
2982         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
2983       if (pinfo & MIPS16_INSN_READ_T)
2984         mask |= 1 << TREG;
2985       if (pinfo & MIPS16_INSN_READ_SP)
2986         mask |= 1 << SP;
2987       if (pinfo & MIPS16_INSN_READ_31)
2988         mask |= 1 << RA;
2989       if (pinfo & MIPS16_INSN_READ_Z)
2990         mask |= 1 << (mips16_to_32_reg_map
2991                       [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
2992       if (pinfo & MIPS16_INSN_READ_GPR_X)
2993         mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2994     }
2995   else
2996     {
2997       if (pinfo2 & INSN2_READ_GPR_D)
2998         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
2999       if (pinfo & INSN_READ_GPR_T)
3000         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3001       if (pinfo & INSN_READ_GPR_S)
3002         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3003       if (pinfo2 & INSN2_READ_GP)
3004         mask |= 1 << GP;
3005       if (pinfo2 & INSN2_READ_GPR_31)
3006         mask |= 1 << RA;
3007       if (pinfo2 & INSN2_READ_GPR_Z)
3008         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3009     }
3010   if (mips_opts.micromips)
3011     {
3012       if (pinfo2 & INSN2_READ_GPR_MC)
3013         mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3014       if (pinfo2 & INSN2_READ_GPR_ME)
3015         mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3016       if (pinfo2 & INSN2_READ_GPR_MG)
3017         mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3018       if (pinfo2 & INSN2_READ_GPR_MJ)
3019         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3020       if (pinfo2 & INSN2_READ_GPR_MMN)
3021         {
3022           mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3023           mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3024         }
3025       if (pinfo2 & INSN2_READ_GPR_MP)
3026         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3027       if (pinfo2 & INSN2_READ_GPR_MQ)
3028         mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3029     }
3030   /* Don't include register 0.  */
3031   return mask & ~1;
3032 }
3033
3034 /* Return the mask of core registers that IP writes.  */
3035
3036 static unsigned int
3037 gpr_write_mask (const struct mips_cl_insn *ip)
3038 {
3039   unsigned long pinfo, pinfo2;
3040   unsigned int mask;
3041
3042   mask = gpr_mod_mask (ip);
3043   pinfo = ip->insn_mo->pinfo;
3044   pinfo2 = ip->insn_mo->pinfo2;
3045   if (mips_opts.mips16)
3046     {
3047       if (pinfo & MIPS16_INSN_WRITE_X)
3048         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3049       if (pinfo & MIPS16_INSN_WRITE_Y)
3050         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3051       if (pinfo & MIPS16_INSN_WRITE_Z)
3052         mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3053       if (pinfo & MIPS16_INSN_WRITE_T)
3054         mask |= 1 << TREG;
3055       if (pinfo & MIPS16_INSN_WRITE_SP)
3056         mask |= 1 << SP;
3057       if (pinfo & MIPS16_INSN_WRITE_31)
3058         mask |= 1 << RA;
3059       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3060         mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3061     }
3062   else
3063     {
3064       if (pinfo & INSN_WRITE_GPR_D)
3065         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
3066       if (pinfo & INSN_WRITE_GPR_T)
3067         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
3068       if (pinfo & INSN_WRITE_GPR_S)
3069         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3070       if (pinfo & INSN_WRITE_GPR_31)
3071         mask |= 1 << RA;
3072       if (pinfo2 & INSN2_WRITE_GPR_Z)
3073         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
3074     }
3075   if (mips_opts.micromips)
3076     {
3077       if (pinfo2 & INSN2_WRITE_GPR_MB)
3078         mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3079       if (pinfo2 & INSN2_WRITE_GPR_MHI)
3080         {
3081           mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3082           mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3083         }
3084       if (pinfo2 & INSN2_WRITE_GPR_MJ)
3085         mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3086       if (pinfo2 & INSN2_WRITE_GPR_MP)
3087         mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3088     }
3089   /* Don't include register 0.  */
3090   return mask & ~1;
3091 }
3092
3093 /* Return the mask of floating-point registers that IP reads.  */
3094
3095 static unsigned int
3096 fpr_read_mask (const struct mips_cl_insn *ip)
3097 {
3098   unsigned long pinfo, pinfo2;
3099   unsigned int mask;
3100
3101   mask = 0;
3102   pinfo = ip->insn_mo->pinfo;
3103   pinfo2 = ip->insn_mo->pinfo2;
3104   if (!mips_opts.mips16)
3105     {
3106       if (pinfo2 & INSN2_READ_FPR_D)
3107         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3108       if (pinfo & INSN_READ_FPR_S)
3109         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3110       if (pinfo & INSN_READ_FPR_T)
3111         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3112       if (pinfo & INSN_READ_FPR_R)
3113         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
3114       if (pinfo2 & INSN2_READ_FPR_Z)
3115         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3116     }
3117   /* Conservatively treat all operands to an FP_D instruction are doubles.
3118      (This is overly pessimistic for things like cvt.d.s.)  */
3119   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3120     mask |= mask << 1;
3121   return mask;
3122 }
3123
3124 /* Return the mask of floating-point registers that IP writes.  */
3125
3126 static unsigned int
3127 fpr_write_mask (const struct mips_cl_insn *ip)
3128 {
3129   unsigned long pinfo, pinfo2;
3130   unsigned int mask;
3131
3132   mask = 0;
3133   pinfo = ip->insn_mo->pinfo;
3134   pinfo2 = ip->insn_mo->pinfo2;
3135   if (!mips_opts.mips16)
3136     {
3137       if (pinfo & INSN_WRITE_FPR_D)
3138         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
3139       if (pinfo & INSN_WRITE_FPR_S)
3140         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
3141       if (pinfo & INSN_WRITE_FPR_T)
3142         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
3143       if (pinfo2 & INSN2_WRITE_FPR_Z)
3144         mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
3145     }
3146   /* Conservatively treat all operands to an FP_D instruction are doubles.
3147      (This is overly pessimistic for things like cvt.s.d.)  */
3148   if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3149     mask |= mask << 1;
3150   return mask;
3151 }
3152
3153 /* Classify an instruction according to the FIX_VR4120_* enumeration.
3154    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3155    by VR4120 errata.  */
3156
3157 static unsigned int
3158 classify_vr4120_insn (const char *name)
3159 {
3160   if (strncmp (name, "macc", 4) == 0)
3161     return FIX_VR4120_MACC;
3162   if (strncmp (name, "dmacc", 5) == 0)
3163     return FIX_VR4120_DMACC;
3164   if (strncmp (name, "mult", 4) == 0)
3165     return FIX_VR4120_MULT;
3166   if (strncmp (name, "dmult", 5) == 0)
3167     return FIX_VR4120_DMULT;
3168   if (strstr (name, "div"))
3169     return FIX_VR4120_DIV;
3170   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3171     return FIX_VR4120_MTHILO;
3172   return NUM_FIX_VR4120_CLASSES;
3173 }
3174
3175 #define INSN_ERET  0x42000018
3176 #define INSN_DERET 0x4200001f
3177
3178 /* Return the number of instructions that must separate INSN1 and INSN2,
3179    where INSN1 is the earlier instruction.  Return the worst-case value
3180    for any INSN2 if INSN2 is null.  */
3181
3182 static unsigned int
3183 insns_between (const struct mips_cl_insn *insn1,
3184                const struct mips_cl_insn *insn2)
3185 {
3186   unsigned long pinfo1, pinfo2;
3187   unsigned int mask;
3188
3189   /* This function needs to know which pinfo flags are set for INSN2
3190      and which registers INSN2 uses.  The former is stored in PINFO2 and
3191      the latter is tested via INSN2_USES_GPR.  If INSN2 is null, PINFO2
3192      will have every flag set and INSN2_USES_GPR will always return true.  */
3193   pinfo1 = insn1->insn_mo->pinfo;
3194   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
3195
3196 #define INSN2_USES_GPR(REG) \
3197   (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
3198
3199   /* For most targets, write-after-read dependencies on the HI and LO
3200      registers must be separated by at least two instructions.  */
3201   if (!hilo_interlocks)
3202     {
3203       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3204         return 2;
3205       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3206         return 2;
3207     }
3208
3209   /* If we're working around r7000 errata, there must be two instructions
3210      between an mfhi or mflo and any instruction that uses the result.  */
3211   if (mips_7000_hilo_fix
3212       && !mips_opts.micromips
3213       && MF_HILO_INSN (pinfo1)
3214       && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
3215     return 2;
3216
3217   /* If we're working around 24K errata, one instruction is required
3218      if an ERET or DERET is followed by a branch instruction.  */
3219   if (mips_fix_24k && !mips_opts.micromips)
3220     {
3221       if (insn1->insn_opcode == INSN_ERET
3222           || insn1->insn_opcode == INSN_DERET)
3223         {
3224           if (insn2 == NULL
3225               || insn2->insn_opcode == INSN_ERET
3226               || insn2->insn_opcode == INSN_DERET
3227               || delayed_branch_p (insn2))
3228             return 1;
3229         }
3230     }
3231
3232   /* If working around VR4120 errata, check for combinations that need
3233      a single intervening instruction.  */
3234   if (mips_fix_vr4120 && !mips_opts.micromips)
3235     {
3236       unsigned int class1, class2;
3237
3238       class1 = classify_vr4120_insn (insn1->insn_mo->name);
3239       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
3240         {
3241           if (insn2 == NULL)
3242             return 1;
3243           class2 = classify_vr4120_insn (insn2->insn_mo->name);
3244           if (vr4120_conflicts[class1] & (1 << class2))
3245             return 1;
3246         }
3247     }
3248
3249   if (!HAVE_CODE_COMPRESSION)
3250     {
3251       /* Check for GPR or coprocessor load delays.  All such delays
3252          are on the RT register.  */
3253       /* Itbl support may require additional care here.  */
3254       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3255           || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
3256         {
3257           know (pinfo1 & INSN_WRITE_GPR_T);
3258           if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
3259             return 1;
3260         }
3261
3262       /* Check for generic coprocessor hazards.
3263
3264          This case is not handled very well.  There is no special
3265          knowledge of CP0 handling, and the coprocessors other than
3266          the floating point unit are not distinguished at all.  */
3267       /* Itbl support may require additional care here. FIXME!
3268          Need to modify this to include knowledge about
3269          user specified delays!  */
3270       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3271                || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3272         {
3273           /* Handle cases where INSN1 writes to a known general coprocessor
3274              register.  There must be a one instruction delay before INSN2
3275              if INSN2 reads that register, otherwise no delay is needed.  */
3276           mask = fpr_write_mask (insn1);
3277           if (mask != 0)
3278             {
3279               if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
3280                 return 1;
3281             }
3282           else
3283             {
3284               /* Read-after-write dependencies on the control registers
3285                  require a two-instruction gap.  */
3286               if ((pinfo1 & INSN_WRITE_COND_CODE)
3287                   && (pinfo2 & INSN_READ_COND_CODE))
3288                 return 2;
3289
3290               /* We don't know exactly what INSN1 does.  If INSN2 is
3291                  also a coprocessor instruction, assume there must be
3292                  a one instruction gap.  */
3293               if (pinfo2 & INSN_COP)
3294                 return 1;
3295             }
3296         }
3297
3298       /* Check for read-after-write dependencies on the coprocessor
3299          control registers in cases where INSN1 does not need a general
3300          coprocessor delay.  This means that INSN1 is a floating point
3301          comparison instruction.  */
3302       /* Itbl support may require additional care here.  */
3303       else if (!cop_interlocks
3304                && (pinfo1 & INSN_WRITE_COND_CODE)
3305                && (pinfo2 & INSN_READ_COND_CODE))
3306         return 1;
3307     }
3308
3309 #undef INSN2_USES_GPR
3310
3311   return 0;
3312 }
3313
3314 /* Return the number of nops that would be needed to work around the
3315    VR4130 mflo/mfhi errata if instruction INSN immediately followed
3316    the MAX_VR4130_NOPS instructions described by HIST.  Ignore hazards
3317    that are contained within the first IGNORE instructions of HIST.  */
3318
3319 static int
3320 nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
3321                  const struct mips_cl_insn *insn)
3322 {
3323   int i, j;
3324   unsigned int mask;
3325
3326   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
3327      are not affected by the errata.  */
3328   if (insn != 0
3329       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3330           || strcmp (insn->insn_mo->name, "mtlo") == 0
3331           || strcmp (insn->insn_mo->name, "mthi") == 0))
3332     return 0;
3333
3334   /* Search for the first MFLO or MFHI.  */
3335   for (i = 0; i < MAX_VR4130_NOPS; i++)
3336     if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
3337       {
3338         /* Extract the destination register.  */
3339         mask = gpr_write_mask (&hist[i]);
3340
3341         /* No nops are needed if INSN reads that register.  */
3342         if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
3343           return 0;
3344
3345         /* ...or if any of the intervening instructions do.  */
3346         for (j = 0; j < i; j++)
3347           if (gpr_read_mask (&hist[j]) & mask)
3348             return 0;
3349
3350         if (i >= ignore)
3351           return MAX_VR4130_NOPS - i;
3352       }
3353   return 0;
3354 }
3355
3356 #define BASE_REG_EQ(INSN1, INSN2)       \
3357   ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3358       == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3359
3360 /* Return the minimum alignment for this store instruction.  */
3361
3362 static int
3363 fix_24k_align_to (const struct mips_opcode *mo)
3364 {
3365   if (strcmp (mo->name, "sh") == 0)
3366     return 2;
3367
3368   if (strcmp (mo->name, "swc1") == 0
3369       || strcmp (mo->name, "swc2") == 0
3370       || strcmp (mo->name, "sw") == 0
3371       || strcmp (mo->name, "sc") == 0
3372       || strcmp (mo->name, "s.s") == 0)
3373     return 4;
3374
3375   if (strcmp (mo->name, "sdc1") == 0
3376       || strcmp (mo->name, "sdc2") == 0
3377       || strcmp (mo->name, "s.d") == 0)
3378     return 8;
3379
3380   /* sb, swl, swr */
3381   return 1;
3382 }
3383
3384 struct fix_24k_store_info
3385   {
3386     /* Immediate offset, if any, for this store instruction.  */
3387     short off;
3388     /* Alignment required by this store instruction.  */
3389     int align_to;
3390     /* True for register offsets.  */
3391     int register_offset;
3392   };
3393
3394 /* Comparison function used by qsort.  */
3395
3396 static int
3397 fix_24k_sort (const void *a, const void *b)
3398 {
3399   const struct fix_24k_store_info *pos1 = a;
3400   const struct fix_24k_store_info *pos2 = b;
3401
3402   return (pos1->off - pos2->off);
3403 }
3404
3405 /* INSN is a store instruction.  Try to record the store information
3406    in STINFO.  Return false if the information isn't known.  */
3407
3408 static bfd_boolean
3409 fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
3410                            const struct mips_cl_insn *insn)
3411 {
3412   /* The instruction must have a known offset.  */
3413   if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3414     return FALSE;
3415
3416   stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3417   stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3418   return TRUE;
3419 }
3420
3421 /* Return the number of nops that would be needed to work around the 24k
3422    "lost data on stores during refill" errata if instruction INSN
3423    immediately followed the 2 instructions described by HIST.
3424    Ignore hazards that are contained within the first IGNORE
3425    instructions of HIST.
3426
3427    Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3428    for the data cache refills and store data. The following describes
3429    the scenario where the store data could be lost.
3430
3431    * A data cache miss, due to either a load or a store, causing fill
3432      data to be supplied by the memory subsystem
3433    * The first three doublewords of fill data are returned and written
3434      into the cache
3435    * A sequence of four stores occurs in consecutive cycles around the
3436      final doubleword of the fill:
3437    * Store A
3438    * Store B
3439    * Store C
3440    * Zero, One or more instructions
3441    * Store D
3442
3443    The four stores A-D must be to different doublewords of the line that
3444    is being filled. The fourth instruction in the sequence above permits
3445    the fill of the final doubleword to be transferred from the FSB into
3446    the cache. In the sequence above, the stores may be either integer
3447    (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3448    swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3449    different doublewords on the line. If the floating point unit is
3450    running in 1:2 mode, it is not possible to create the sequence above
3451    using only floating point store instructions.
3452
3453    In this case, the cache line being filled is incorrectly marked
3454    invalid, thereby losing the data from any store to the line that
3455    occurs between the original miss and the completion of the five
3456    cycle sequence shown above.
3457
3458    The workarounds are:
3459
3460    * Run the data cache in write-through mode.
3461    * Insert a non-store instruction between
3462      Store A and Store B or Store B and Store C.  */
3463   
3464 static int
3465 nops_for_24k (int ignore, const struct mips_cl_insn *hist,
3466               const struct mips_cl_insn *insn)
3467 {
3468   struct fix_24k_store_info pos[3];
3469   int align, i, base_offset;
3470
3471   if (ignore >= 2)
3472     return 0;
3473
3474   /* If the previous instruction wasn't a store, there's nothing to
3475      worry about.  */
3476   if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3477     return 0;
3478
3479   /* If the instructions after the previous one are unknown, we have
3480      to assume the worst.  */
3481   if (!insn)
3482     return 1;
3483
3484   /* Check whether we are dealing with three consecutive stores.  */
3485   if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3486       || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3487     return 0;
3488
3489   /* If we don't know the relationship between the store addresses,
3490      assume the worst.  */
3491   if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
3492       || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3493     return 1;
3494
3495   if (!fix_24k_record_store_info (&pos[0], insn)
3496       || !fix_24k_record_store_info (&pos[1], &hist[0])
3497       || !fix_24k_record_store_info (&pos[2], &hist[1]))
3498     return 1;
3499
3500   qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3501
3502   /* Pick a value of ALIGN and X such that all offsets are adjusted by
3503      X bytes and such that the base register + X is known to be aligned
3504      to align bytes.  */
3505
3506   if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3507     align = 8;
3508   else
3509     {
3510       align = pos[0].align_to;
3511       base_offset = pos[0].off;
3512       for (i = 1; i < 3; i++)
3513         if (align < pos[i].align_to)
3514           {
3515             align = pos[i].align_to;
3516             base_offset = pos[i].off;
3517           }
3518       for (i = 0; i < 3; i++)
3519         pos[i].off -= base_offset;
3520     }
3521
3522   pos[0].off &= ~align + 1;
3523   pos[1].off &= ~align + 1;
3524   pos[2].off &= ~align + 1;
3525
3526   /* If any two stores write to the same chunk, they also write to the
3527      same doubleword.  The offsets are still sorted at this point.  */
3528   if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3529     return 0;
3530
3531   /* A range of at least 9 bytes is needed for the stores to be in
3532      non-overlapping doublewords.  */
3533   if (pos[2].off - pos[0].off <= 8)
3534     return 0;
3535
3536   if (pos[2].off - pos[1].off >= 24
3537       || pos[1].off - pos[0].off >= 24
3538       || pos[2].off - pos[0].off >= 32)
3539     return 0;
3540
3541   return 1;
3542 }
3543
3544 /* Return the number of nops that would be needed if instruction INSN
3545    immediately followed the MAX_NOPS instructions given by HIST,
3546    where HIST[0] is the most recent instruction.  Ignore hazards
3547    between INSN and the first IGNORE instructions in HIST.
3548
3549    If INSN is null, return the worse-case number of nops for any
3550    instruction.  */
3551
3552 static int
3553 nops_for_insn (int ignore, const struct mips_cl_insn *hist,
3554                const struct mips_cl_insn *insn)
3555 {
3556   int i, nops, tmp_nops;
3557
3558   nops = 0;
3559   for (i = ignore; i < MAX_DELAY_NOPS; i++)
3560     {
3561       tmp_nops = insns_between (hist + i, insn) - i;
3562       if (tmp_nops > nops)
3563         nops = tmp_nops;
3564     }
3565
3566   if (mips_fix_vr4130 && !mips_opts.micromips)
3567     {
3568       tmp_nops = nops_for_vr4130 (ignore, hist, insn);
3569       if (tmp_nops > nops)
3570         nops = tmp_nops;
3571     }
3572
3573   if (mips_fix_24k && !mips_opts.micromips)
3574     {
3575       tmp_nops = nops_for_24k (ignore, hist, insn);
3576       if (tmp_nops > nops)
3577         nops = tmp_nops;
3578     }
3579
3580   return nops;
3581 }
3582
3583 /* The variable arguments provide NUM_INSNS extra instructions that
3584    might be added to HIST.  Return the largest number of nops that
3585    would be needed after the extended sequence, ignoring hazards
3586    in the first IGNORE instructions.  */
3587
3588 static int
3589 nops_for_sequence (int num_insns, int ignore,
3590                    const struct mips_cl_insn *hist, ...)
3591 {
3592   va_list args;
3593   struct mips_cl_insn buffer[MAX_NOPS];
3594   struct mips_cl_insn *cursor;
3595   int nops;
3596
3597   va_start (args, hist);
3598   cursor = buffer + num_insns;
3599   memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
3600   while (cursor > buffer)
3601     *--cursor = *va_arg (args, const struct mips_cl_insn *);
3602
3603   nops = nops_for_insn (ignore, buffer, NULL);
3604   va_end (args);
3605   return nops;
3606 }
3607
3608 /* Like nops_for_insn, but if INSN is a branch, take into account the
3609    worst-case delay for the branch target.  */
3610
3611 static int
3612 nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
3613                          const struct mips_cl_insn *insn)
3614 {
3615   int nops, tmp_nops;
3616
3617   nops = nops_for_insn (ignore, hist, insn);
3618   if (delayed_branch_p (insn))
3619     {
3620       tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
3621                                     hist, insn, get_delay_slot_nop (insn));
3622       if (tmp_nops > nops)
3623         nops = tmp_nops;
3624     }
3625   else if (compact_branch_p (insn))
3626     {
3627       tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
3628       if (tmp_nops > nops)
3629         nops = tmp_nops;
3630     }
3631   return nops;
3632 }
3633
3634 /* Fix NOP issue: Replace nops by "or at,at,zero".  */
3635
3636 static void
3637 fix_loongson2f_nop (struct mips_cl_insn * ip)
3638 {
3639   gas_assert (!HAVE_CODE_COMPRESSION);
3640   if (strcmp (ip->insn_mo->name, "nop") == 0)
3641     ip->insn_opcode = LOONGSON2F_NOP_INSN;
3642 }
3643
3644 /* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
3645                    jr target pc &= 'hffff_ffff_cfff_ffff.  */
3646
3647 static void
3648 fix_loongson2f_jump (struct mips_cl_insn * ip)
3649 {
3650   gas_assert (!HAVE_CODE_COMPRESSION);
3651   if (strcmp (ip->insn_mo->name, "j") == 0
3652       || strcmp (ip->insn_mo->name, "jr") == 0
3653       || strcmp (ip->insn_mo->name, "jalr") == 0)
3654     {
3655       int sreg;
3656       expressionS ep;
3657
3658       if (! mips_opts.at)
3659         return;
3660
3661       sreg = EXTRACT_OPERAND (0, RS, *ip);
3662       if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
3663         return;
3664
3665       ep.X_op = O_constant;
3666       ep.X_add_number = 0xcfff0000;
3667       macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
3668       ep.X_add_number = 0xffff;
3669       macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
3670       macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
3671     }
3672 }
3673
3674 static void
3675 fix_loongson2f (struct mips_cl_insn * ip)
3676 {
3677   if (mips_fix_loongson2f_nop)
3678     fix_loongson2f_nop (ip);
3679
3680   if (mips_fix_loongson2f_jump)
3681     fix_loongson2f_jump (ip);
3682 }
3683
3684 /* IP is a branch that has a delay slot, and we need to fill it
3685    automatically.   Return true if we can do that by swapping IP
3686    with the previous instruction.  */
3687
3688 static bfd_boolean
3689 can_swap_branch_p (struct mips_cl_insn *ip)
3690 {
3691   unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
3692   unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
3693
3694   /* -O2 and above is required for this optimization.  */
3695   if (mips_optimize < 2)
3696     return FALSE;
3697
3698   /* If we have seen .set volatile or .set nomove, don't optimize.  */
3699   if (mips_opts.nomove)
3700     return FALSE;
3701
3702   /* We can't swap if the previous instruction's position is fixed.  */
3703   if (history[0].fixed_p)
3704     return FALSE;
3705
3706   /* If the previous previous insn was in a .set noreorder, we can't
3707      swap.  Actually, the MIPS assembler will swap in this situation.
3708      However, gcc configured -with-gnu-as will generate code like
3709
3710         .set    noreorder
3711         lw      $4,XXX
3712         .set    reorder
3713         INSN
3714         bne     $4,$0,foo
3715
3716      in which we can not swap the bne and INSN.  If gcc is not configured
3717      -with-gnu-as, it does not output the .set pseudo-ops.  */
3718   if (history[1].noreorder_p)
3719     return FALSE;
3720
3721   /* If the previous instruction had a fixup in mips16 mode, we can not swap.
3722      This means that the previous instruction was a 4-byte one anyhow.  */
3723   if (mips_opts.mips16 && history[0].fixp[0])
3724     return FALSE;
3725
3726   /* If the branch is itself the target of a branch, we can not swap.
3727      We cheat on this; all we check for is whether there is a label on
3728      this instruction.  If there are any branches to anything other than
3729      a label, users must use .set noreorder.  */
3730   if (seg_info (now_seg)->label_list)
3731     return FALSE;
3732
3733   /* If the previous instruction is in a variant frag other than this
3734      branch's one, we cannot do the swap.  This does not apply to
3735      MIPS16 code, which uses variant frags for different purposes.  */
3736   if (!mips_opts.mips16
3737       && history[0].frag
3738       && history[0].frag->fr_type == rs_machine_dependent)
3739     return FALSE;
3740
3741   /* We do not swap with instructions that cannot architecturally
3742      be placed in a branch delay slot, such as SYNC or ERET.  We
3743      also refrain from swapping with a trap instruction, since it
3744      complicates trap handlers to have the trap instruction be in
3745      a delay slot.  */
3746   prev_pinfo = history[0].insn_mo->pinfo;
3747   if (prev_pinfo & INSN_NO_DELAY_SLOT)
3748     return FALSE;
3749
3750   /* Check for conflicts between the branch and the instructions
3751      before the candidate delay slot.  */
3752   if (nops_for_insn (0, history + 1, ip) > 0)
3753     return FALSE;
3754
3755   /* Check for conflicts between the swapped sequence and the
3756      target of the branch.  */
3757   if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
3758     return FALSE;
3759
3760   /* If the branch reads a register that the previous
3761      instruction sets, we can not swap.  */
3762   gpr_read = gpr_read_mask (ip);
3763   prev_gpr_write = gpr_write_mask (&history[0]);
3764   if (gpr_read & prev_gpr_write)
3765     return FALSE;
3766
3767   /* If the branch writes a register that the previous
3768      instruction sets, we can not swap.  */
3769   gpr_write = gpr_write_mask (ip);
3770   if (gpr_write & prev_gpr_write)
3771     return FALSE;
3772
3773   /* If the branch writes a register that the previous
3774      instruction reads, we can not swap.  */
3775   prev_gpr_read = gpr_read_mask (&history[0]);
3776   if (gpr_write & prev_gpr_read)
3777     return FALSE;
3778
3779   /* If one instruction sets a condition code and the
3780      other one uses a condition code, we can not swap.  */
3781   pinfo = ip->insn_mo->pinfo;
3782   if ((pinfo & INSN_READ_COND_CODE)
3783       && (prev_pinfo & INSN_WRITE_COND_CODE))
3784     return FALSE;
3785   if ((pinfo & INSN_WRITE_COND_CODE)
3786       && (prev_pinfo & INSN_READ_COND_CODE))
3787     return FALSE;
3788
3789   /* If the previous instruction uses the PC, we can not swap.  */
3790   prev_pinfo2 = history[0].insn_mo->pinfo2;
3791   if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
3792     return FALSE;
3793   if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
3794     return FALSE;
3795
3796   /* If the previous instruction has an incorrect size for a fixed
3797      branch delay slot in microMIPS mode, we cannot swap.  */
3798   pinfo2 = ip->insn_mo->pinfo2;
3799   if (mips_opts.micromips
3800       && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
3801       && insn_length (history) != 2)
3802     return FALSE;
3803   if (mips_opts.micromips
3804       && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
3805       && insn_length (history) != 4)
3806     return FALSE;
3807
3808   return TRUE;
3809 }
3810
3811 /* Decide how we should add IP to the instruction stream.  */
3812
3813 static enum append_method
3814 get_append_method (struct mips_cl_insn *ip)
3815 {
3816   unsigned long pinfo;
3817
3818   /* The relaxed version of a macro sequence must be inherently
3819      hazard-free.  */
3820   if (mips_relax.sequence == 2)
3821     return APPEND_ADD;
3822
3823   /* We must not dabble with instructions in a ".set norerorder" block.  */
3824   if (mips_opts.noreorder)
3825     return APPEND_ADD;
3826
3827   /* Otherwise, it's our responsibility to fill branch delay slots.  */
3828   if (delayed_branch_p (ip))
3829     {
3830       if (!branch_likely_p (ip) && can_swap_branch_p (ip))
3831         return APPEND_SWAP;
3832
3833       pinfo = ip->insn_mo->pinfo;
3834       if (mips_opts.mips16
3835           && ISA_SUPPORTS_MIPS16E
3836           && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
3837         return APPEND_ADD_COMPACT;
3838
3839       return APPEND_ADD_WITH_NOP;
3840     }
3841
3842   return APPEND_ADD;
3843 }
3844
3845 /* IP is a MIPS16 instruction whose opcode we have just changed.
3846    Point IP->insn_mo to the new opcode's definition.  */
3847
3848 static void
3849 find_altered_mips16_opcode (struct mips_cl_insn *ip)
3850 {
3851   const struct mips_opcode *mo, *end;
3852
3853   end = &mips16_opcodes[bfd_mips16_num_opcodes];
3854   for (mo = ip->insn_mo; mo < end; mo++)
3855     if ((ip->insn_opcode & mo->mask) == mo->match)
3856       {
3857         ip->insn_mo = mo;
3858         return;
3859       }
3860   abort ();
3861 }
3862
3863 /* For microMIPS macros, we need to generate a local number label
3864    as the target of branches.  */
3865 #define MICROMIPS_LABEL_CHAR            '\037'
3866 static unsigned long micromips_target_label;
3867 static char micromips_target_name[32];
3868
3869 static char *
3870 micromips_label_name (void)
3871 {
3872   char *p = micromips_target_name;
3873   char symbol_name_temporary[24];
3874   unsigned long l;
3875   int i;
3876
3877   if (*p)
3878     return p;
3879
3880   i = 0;
3881   l = micromips_target_label;
3882 #ifdef LOCAL_LABEL_PREFIX
3883   *p++ = LOCAL_LABEL_PREFIX;
3884 #endif
3885   *p++ = 'L';
3886   *p++ = MICROMIPS_LABEL_CHAR;
3887   do
3888     {
3889       symbol_name_temporary[i++] = l % 10 + '0';
3890       l /= 10;
3891     }
3892   while (l != 0);
3893   while (i > 0)
3894     *p++ = symbol_name_temporary[--i];
3895   *p = '\0';
3896
3897   return micromips_target_name;
3898 }
3899
3900 static void
3901 micromips_label_expr (expressionS *label_expr)
3902 {
3903   label_expr->X_op = O_symbol;
3904   label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
3905   label_expr->X_add_number = 0;
3906 }
3907
3908 static void
3909 micromips_label_inc (void)
3910 {
3911   micromips_target_label++;
3912   *micromips_target_name = '\0';
3913 }
3914
3915 static void
3916 micromips_add_label (void)
3917 {
3918   symbolS *s;
3919
3920   s = colon (micromips_label_name ());
3921   micromips_label_inc ();
3922 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
3923   if (IS_ELF)
3924     S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
3925 #else
3926   (void) s;
3927 #endif
3928 }
3929
3930 /* If assembling microMIPS code, then return the microMIPS reloc
3931    corresponding to the requested one if any.  Otherwise return
3932    the reloc unchanged.  */
3933
3934 static bfd_reloc_code_real_type
3935 micromips_map_reloc (bfd_reloc_code_real_type reloc)
3936 {
3937   static const bfd_reloc_code_real_type relocs[][2] =
3938     {
3939       /* Keep sorted incrementally by the left-hand key.  */
3940       { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
3941       { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
3942       { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
3943       { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
3944       { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
3945       { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
3946       { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
3947       { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
3948       { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
3949       { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
3950       { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
3951       { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
3952       { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
3953       { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
3954       { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
3955       { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
3956       { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
3957       { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
3958       { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
3959       { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
3960       { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
3961       { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
3962       { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
3963       { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
3964       { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
3965       { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
3966       { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
3967     };
3968   bfd_reloc_code_real_type r;
3969   size_t i;
3970
3971   if (!mips_opts.micromips)
3972     return reloc;
3973   for (i = 0; i < ARRAY_SIZE (relocs); i++)
3974     {
3975       r = relocs[i][0];
3976       if (r > reloc)
3977         return reloc;
3978       if (r == reloc)
3979         return relocs[i][1];
3980     }
3981   return reloc;
3982 }
3983
3984 /* Output an instruction.  IP is the instruction information.
3985    ADDRESS_EXPR is an operand of the instruction to be used with
3986    RELOC_TYPE.  EXPANSIONP is true if the instruction is part of
3987    a macro expansion.  */
3988
3989 static void
3990 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
3991              bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
3992 {
3993   unsigned long prev_pinfo2, pinfo;
3994   bfd_boolean relaxed_branch = FALSE;
3995   enum append_method method;
3996   bfd_boolean relax32;
3997   int branch_disp;
3998
3999   if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
4000     fix_loongson2f (ip);
4001
4002   mips_mark_labels ();
4003
4004   file_ase_mips16 |= mips_opts.mips16;
4005   file_ase_micromips |= mips_opts.micromips;
4006
4007   prev_pinfo2 = history[0].insn_mo->pinfo2;
4008   pinfo = ip->insn_mo->pinfo;
4009
4010   if (mips_opts.micromips
4011       && !expansionp
4012       && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4013            && micromips_insn_length (ip->insn_mo) != 2)
4014           || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4015               && micromips_insn_length (ip->insn_mo) != 4)))
4016     as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4017              (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
4018
4019   if (address_expr == NULL)
4020     ip->complete_p = 1;
4021   else if (*reloc_type <= BFD_RELOC_UNUSED
4022            && address_expr->X_op == O_constant)
4023     {
4024       unsigned int tmp;
4025
4026       ip->complete_p = 1;
4027       switch (*reloc_type)
4028         {
4029         case BFD_RELOC_32:
4030           ip->insn_opcode |= address_expr->X_add_number;
4031           break;
4032
4033         case BFD_RELOC_MIPS_HIGHEST:
4034           tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
4035           ip->insn_opcode |= tmp & 0xffff;
4036           break;
4037
4038         case BFD_RELOC_MIPS_HIGHER:
4039           tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
4040           ip->insn_opcode |= tmp & 0xffff;
4041           break;
4042
4043         case BFD_RELOC_HI16_S:
4044           tmp = (address_expr->X_add_number + 0x8000) >> 16;
4045           ip->insn_opcode |= tmp & 0xffff;
4046           break;
4047
4048         case BFD_RELOC_HI16:
4049           ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
4050           break;
4051
4052         case BFD_RELOC_UNUSED:
4053         case BFD_RELOC_LO16:
4054         case BFD_RELOC_MIPS_GOT_DISP:
4055           ip->insn_opcode |= address_expr->X_add_number & 0xffff;
4056           break;
4057
4058         case BFD_RELOC_MIPS_JMP:
4059           {
4060             int shift;
4061
4062             shift = mips_opts.micromips ? 1 : 2;
4063             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4064               as_bad (_("jump to misaligned address (0x%lx)"),
4065                       (unsigned long) address_expr->X_add_number);
4066             ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4067                                 & 0x3ffffff);
4068             ip->complete_p = 0;
4069           }
4070           break;
4071
4072         case BFD_RELOC_MIPS16_JMP:
4073           if ((address_expr->X_add_number & 3) != 0)
4074             as_bad (_("jump to misaligned address (0x%lx)"),
4075                     (unsigned long) address_expr->X_add_number);
4076           ip->insn_opcode |=
4077             (((address_expr->X_add_number & 0x7c0000) << 3)
4078                | ((address_expr->X_add_number & 0xf800000) >> 7)
4079                | ((address_expr->X_add_number & 0x3fffc) >> 2));
4080           ip->complete_p = 0;
4081           break;
4082
4083         case BFD_RELOC_16_PCREL_S2:
4084           {
4085             int shift;
4086
4087             shift = mips_opts.micromips ? 1 : 2;
4088             if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4089               as_bad (_("branch to misaligned address (0x%lx)"),
4090                       (unsigned long) address_expr->X_add_number);
4091             if (!mips_relax_branch)
4092               {
4093                 if ((address_expr->X_add_number + (1 << (shift + 15)))
4094                     & ~((1 << (shift + 16)) - 1))
4095                   as_bad (_("branch address range overflow (0x%lx)"),
4096                           (unsigned long) address_expr->X_add_number);
4097                 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4098                                     & 0xffff);
4099               }
4100             ip->complete_p = 0;
4101           }
4102           break;
4103
4104         default:
4105           internalError ();
4106         }       
4107     }
4108
4109   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4110     {
4111       /* There are a lot of optimizations we could do that we don't.
4112          In particular, we do not, in general, reorder instructions.
4113          If you use gcc with optimization, it will reorder
4114          instructions and generally do much more optimization then we
4115          do here; repeating all that work in the assembler would only
4116          benefit hand written assembly code, and does not seem worth
4117          it.  */
4118       int nops = (mips_optimize == 0
4119                   ? nops_for_insn (0, history, NULL)
4120                   : nops_for_insn_or_target (0, history, ip));
4121       if (nops > 0)
4122         {
4123           fragS *old_frag;
4124           unsigned long old_frag_offset;
4125           int i;
4126
4127           old_frag = frag_now;
4128           old_frag_offset = frag_now_fix ();
4129
4130           for (i = 0; i < nops; i++)
4131             add_fixed_insn (NOP_INSN);
4132           insert_into_history (0, nops, NOP_INSN);
4133
4134           if (listing)
4135             {
4136               listing_prev_line ();
4137               /* We may be at the start of a variant frag.  In case we
4138                  are, make sure there is enough space for the frag
4139                  after the frags created by listing_prev_line.  The
4140                  argument to frag_grow here must be at least as large
4141                  as the argument to all other calls to frag_grow in
4142                  this file.  We don't have to worry about being in the
4143                  middle of a variant frag, because the variants insert
4144                  all needed nop instructions themselves.  */
4145               frag_grow (40);
4146             }
4147
4148           mips_move_labels ();
4149
4150 #ifndef NO_ECOFF_DEBUGGING
4151           if (ECOFF_DEBUGGING)
4152             ecoff_fix_loc (old_frag, old_frag_offset);
4153 #endif
4154         }
4155     }
4156   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4157     {
4158       int nops;
4159
4160       /* Work out how many nops in prev_nop_frag are needed by IP,
4161          ignoring hazards generated by the first prev_nop_frag_since
4162          instructions.  */
4163       nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
4164       gas_assert (nops <= prev_nop_frag_holds);
4165
4166       /* Enforce NOPS as a minimum.  */
4167       if (nops > prev_nop_frag_required)
4168         prev_nop_frag_required = nops;
4169
4170       if (prev_nop_frag_holds == prev_nop_frag_required)
4171         {
4172           /* Settle for the current number of nops.  Update the history
4173              accordingly (for the benefit of any future .set reorder code).  */
4174           prev_nop_frag = NULL;
4175           insert_into_history (prev_nop_frag_since,
4176                                prev_nop_frag_holds, NOP_INSN);
4177         }
4178       else
4179         {
4180           /* Allow this instruction to replace one of the nops that was
4181              tentatively added to prev_nop_frag.  */
4182           prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
4183           prev_nop_frag_holds--;
4184           prev_nop_frag_since++;
4185         }
4186     }
4187
4188   method = get_append_method (ip);
4189   branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
4190
4191 #ifdef OBJ_ELF
4192   /* The value passed to dwarf2_emit_insn is the distance between
4193      the beginning of the current instruction and the address that
4194      should be recorded in the debug tables.  This is normally the
4195      current address.
4196
4197      For MIPS16/microMIPS debug info we want to use ISA-encoded
4198      addresses, so we use -1 for an address higher by one than the
4199      current one.
4200
4201      If the instruction produced is a branch that we will swap with
4202      the preceding instruction, then we add the displacement by which
4203      the branch will be moved backwards.  This is more appropriate
4204      and for MIPS16/microMIPS code also prevents a debugger from
4205      placing a breakpoint in the middle of the branch (and corrupting
4206      code if software breakpoints are used).  */
4207   dwarf2_emit_insn ((HAVE_CODE_COMPRESSION ? -1 : 0) + branch_disp);
4208 #endif
4209
4210   relax32 = (mips_relax_branch
4211              /* Don't try branch relaxation within .set nomacro, or within
4212                 .set noat if we use $at for PIC computations.  If it turns
4213                 out that the branch was out-of-range, we'll get an error.  */
4214              && !mips_opts.warn_about_macros
4215              && (mips_opts.at || mips_pic == NO_PIC)
4216              /* Don't relax BPOSGE32/64 as they have no complementing
4217                 branches.  */
4218              && !(ip->insn_mo->membership & (INSN_DSP64 | INSN_DSP)));
4219
4220   if (!HAVE_CODE_COMPRESSION
4221       && address_expr
4222       && relax32
4223       && *reloc_type == BFD_RELOC_16_PCREL_S2
4224       && delayed_branch_p (ip))
4225     {
4226       relaxed_branch = TRUE;
4227       add_relaxed_insn (ip, (relaxed_branch_length
4228                              (NULL, NULL,
4229                               uncond_branch_p (ip) ? -1
4230                               : branch_likely_p (ip) ? 1
4231                               : 0)), 4,
4232                         RELAX_BRANCH_ENCODE
4233                         (AT,
4234                          uncond_branch_p (ip),
4235                          branch_likely_p (ip),
4236                          pinfo & INSN_WRITE_GPR_31,
4237                          0),
4238                         address_expr->X_add_symbol,
4239                         address_expr->X_add_number);
4240       *reloc_type = BFD_RELOC_UNUSED;
4241     }
4242   else if (mips_opts.micromips
4243            && address_expr
4244            && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4245                || *reloc_type > BFD_RELOC_UNUSED)
4246            && (delayed_branch_p (ip) || compact_branch_p (ip))
4247            /* Don't try branch relaxation when users specify
4248               16-bit/32-bit instructions.  */
4249            && !forced_insn_length)
4250     {
4251       bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4252       int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
4253       int uncond = uncond_branch_p (ip) ? -1 : 0;
4254       int compact = compact_branch_p (ip);
4255       int al = pinfo & INSN_WRITE_GPR_31;
4256       int length32;
4257
4258       gas_assert (address_expr != NULL);
4259       gas_assert (!mips_relax.sequence);
4260
4261       relaxed_branch = TRUE;
4262       length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4263       add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
4264                         RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4265                                                 relax32, 0, 0),
4266                         address_expr->X_add_symbol,
4267                         address_expr->X_add_number);
4268       *reloc_type = BFD_RELOC_UNUSED;
4269     }
4270   else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
4271     {
4272       /* We need to set up a variant frag.  */
4273       gas_assert (address_expr != NULL);
4274       add_relaxed_insn (ip, 4, 0,
4275                         RELAX_MIPS16_ENCODE
4276                         (*reloc_type - BFD_RELOC_UNUSED,
4277                          forced_insn_length == 2, forced_insn_length == 4,
4278                          delayed_branch_p (&history[0]),
4279                          history[0].mips16_absolute_jump_p),
4280                         make_expr_symbol (address_expr), 0);
4281     }
4282   else if (mips_opts.mips16
4283            && ! ip->use_extend
4284            && *reloc_type != BFD_RELOC_MIPS16_JMP)
4285     {
4286       if (!delayed_branch_p (ip))
4287         /* Make sure there is enough room to swap this instruction with
4288            a following jump instruction.  */
4289         frag_grow (6);
4290       add_fixed_insn (ip);
4291     }
4292   else
4293     {
4294       if (mips_opts.mips16
4295           && mips_opts.noreorder
4296           && delayed_branch_p (&history[0]))
4297         as_warn (_("extended instruction in delay slot"));
4298
4299       if (mips_relax.sequence)
4300         {
4301           /* If we've reached the end of this frag, turn it into a variant
4302              frag and record the information for the instructions we've
4303              written so far.  */
4304           if (frag_room () < 4)
4305             relax_close_frag ();
4306           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4307         }
4308
4309       if (mips_relax.sequence != 2)
4310         {
4311           if (mips_macro_warning.first_insn_sizes[0] == 0)
4312             mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4313           mips_macro_warning.sizes[0] += insn_length (ip);
4314           mips_macro_warning.insns[0]++;
4315         }
4316       if (mips_relax.sequence != 1)
4317         {
4318           if (mips_macro_warning.first_insn_sizes[1] == 0)
4319             mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4320           mips_macro_warning.sizes[1] += insn_length (ip);
4321           mips_macro_warning.insns[1]++;
4322         }
4323
4324       if (mips_opts.mips16)
4325         {
4326           ip->fixed_p = 1;
4327           ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4328         }
4329       add_fixed_insn (ip);
4330     }
4331
4332   if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
4333     {
4334       bfd_reloc_code_real_type final_type[3];
4335       reloc_howto_type *howto0;
4336       reloc_howto_type *howto;
4337       int i;
4338
4339       /* Perform any necessary conversion to microMIPS relocations
4340          and find out how many relocations there actually are.  */
4341       for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4342         final_type[i] = micromips_map_reloc (reloc_type[i]);
4343
4344       /* In a compound relocation, it is the final (outermost)
4345          operator that determines the relocated field.  */
4346       howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
4347
4348       if (howto == NULL)
4349         {
4350           /* To reproduce this failure try assembling gas/testsuites/
4351              gas/mips/mips16-intermix.s with a mips-ecoff targeted
4352              assembler.  */
4353           as_bad (_("Unsupported MIPS relocation number %d"),
4354                   final_type[i - 1]);
4355           howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
4356         }
4357
4358       if (i > 1)
4359         howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
4360       ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4361                                  bfd_get_reloc_size (howto),
4362                                  address_expr,
4363                                  howto0 && howto0->pc_relative,
4364                                  final_type[0]);
4365
4366       /* Tag symbols that have a R_MIPS16_26 relocation against them.  */
4367       if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
4368         *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4369
4370       /* These relocations can have an addend that won't fit in
4371          4 octets for 64bit assembly.  */
4372       if (HAVE_64BIT_GPRS
4373           && ! howto->partial_inplace
4374           && (reloc_type[0] == BFD_RELOC_16
4375               || reloc_type[0] == BFD_RELOC_32
4376               || reloc_type[0] == BFD_RELOC_MIPS_JMP
4377               || reloc_type[0] == BFD_RELOC_GPREL16
4378               || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4379               || reloc_type[0] == BFD_RELOC_GPREL32
4380               || reloc_type[0] == BFD_RELOC_64
4381               || reloc_type[0] == BFD_RELOC_CTOR
4382               || reloc_type[0] == BFD_RELOC_MIPS_SUB
4383               || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4384               || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4385               || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4386               || reloc_type[0] == BFD_RELOC_MIPS_REL16
4387               || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4388               || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4389               || hi16_reloc_p (reloc_type[0])
4390               || lo16_reloc_p (reloc_type[0])))
4391         ip->fixp[0]->fx_no_overflow = 1;
4392
4393       if (mips_relax.sequence)
4394         {
4395           if (mips_relax.first_fixup == 0)
4396             mips_relax.first_fixup = ip->fixp[0];
4397         }
4398       else if (reloc_needs_lo_p (*reloc_type))
4399         {
4400           struct mips_hi_fixup *hi_fixup;
4401
4402           /* Reuse the last entry if it already has a matching %lo.  */
4403           hi_fixup = mips_hi_fixup_list;
4404           if (hi_fixup == 0
4405               || !fixup_has_matching_lo_p (hi_fixup->fixp))
4406             {
4407               hi_fixup = ((struct mips_hi_fixup *)
4408                           xmalloc (sizeof (struct mips_hi_fixup)));
4409               hi_fixup->next = mips_hi_fixup_list;
4410               mips_hi_fixup_list = hi_fixup;
4411             }
4412           hi_fixup->fixp = ip->fixp[0];
4413           hi_fixup->seg = now_seg;
4414         }
4415
4416       /* Add fixups for the second and third relocations, if given.
4417          Note that the ABI allows the second relocation to be
4418          against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
4419          moment we only use RSS_UNDEF, but we could add support
4420          for the others if it ever becomes necessary.  */
4421       for (i = 1; i < 3; i++)
4422         if (reloc_type[i] != BFD_RELOC_UNUSED)
4423           {
4424             ip->fixp[i] = fix_new (ip->frag, ip->where,
4425                                    ip->fixp[0]->fx_size, NULL, 0,
4426                                    FALSE, final_type[i]);
4427
4428             /* Use fx_tcbit to mark compound relocs.  */
4429             ip->fixp[0]->fx_tcbit = 1;
4430             ip->fixp[i]->fx_tcbit = 1;
4431           }
4432     }
4433   install_insn (ip);
4434
4435   /* Update the register mask information.  */
4436   mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4437   mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
4438
4439   switch (method)
4440     {
4441     case APPEND_ADD:
4442       insert_into_history (0, 1, ip);
4443       break;
4444
4445     case APPEND_ADD_WITH_NOP:
4446       {
4447         struct mips_cl_insn *nop;
4448
4449         insert_into_history (0, 1, ip);
4450         nop = get_delay_slot_nop (ip);
4451         add_fixed_insn (nop);
4452         insert_into_history (0, 1, nop);
4453         if (mips_relax.sequence)
4454           mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4455       }
4456       break;
4457
4458     case APPEND_ADD_COMPACT:
4459       /* Convert MIPS16 jr/jalr into a "compact" jump.  */
4460       gas_assert (mips_opts.mips16);
4461       ip->insn_opcode |= 0x0080;
4462       find_altered_mips16_opcode (ip);
4463       install_insn (ip);
4464       insert_into_history (0, 1, ip);
4465       break;
4466
4467     case APPEND_SWAP:
4468       {
4469         struct mips_cl_insn delay = history[0];
4470         if (mips_opts.mips16)
4471           {
4472             know (delay.frag == ip->frag);
4473             move_insn (ip, delay.frag, delay.where);
4474             move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4475           }
4476         else if (relaxed_branch)
4477           {
4478             /* Add the delay slot instruction to the end of the
4479                current frag and shrink the fixed part of the
4480                original frag.  If the branch occupies the tail of
4481                the latter, move it backwards to cover the gap.  */
4482             delay.frag->fr_fix -= branch_disp;
4483             if (delay.frag == ip->frag)
4484               move_insn (ip, ip->frag, ip->where - branch_disp);
4485             add_fixed_insn (&delay);
4486           }
4487         else
4488           {
4489             move_insn (&delay, ip->frag,
4490                        ip->where - branch_disp + insn_length (ip));
4491             move_insn (ip, history[0].frag, history[0].where);
4492           }
4493         history[0] = *ip;
4494         delay.fixed_p = 1;
4495         insert_into_history (0, 1, &delay);
4496       }
4497       break;
4498     }
4499
4500   /* If we have just completed an unconditional branch, clear the history.  */
4501   if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4502       || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
4503     mips_no_prev_insn ();
4504
4505   /* We need to emit a label at the end of branch-likely macros.  */
4506   if (emit_branch_likely_macro)
4507     {
4508       emit_branch_likely_macro = FALSE;
4509       micromips_add_label ();
4510     }
4511
4512   /* We just output an insn, so the next one doesn't have a label.  */
4513   mips_clear_insn_labels ();
4514 }
4515
4516 /* Forget that there was any previous instruction or label.  */
4517
4518 static void
4519 mips_no_prev_insn (void)
4520 {
4521   prev_nop_frag = NULL;
4522   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
4523   mips_clear_insn_labels ();
4524 }
4525
4526 /* This function must be called before we emit something other than
4527    instructions.  It is like mips_no_prev_insn except that it inserts
4528    any NOPS that might be needed by previous instructions.  */
4529
4530 void
4531 mips_emit_delays (void)
4532 {
4533   if (! mips_opts.noreorder)
4534     {
4535       int nops = nops_for_insn (0, history, NULL);
4536       if (nops > 0)
4537         {
4538           while (nops-- > 0)
4539             add_fixed_insn (NOP_INSN);
4540           mips_move_labels ();
4541         }
4542     }
4543   mips_no_prev_insn ();
4544 }
4545
4546 /* Start a (possibly nested) noreorder block.  */
4547
4548 static void
4549 start_noreorder (void)
4550 {
4551   if (mips_opts.noreorder == 0)
4552     {
4553       unsigned int i;
4554       int nops;
4555
4556       /* None of the instructions before the .set noreorder can be moved.  */
4557       for (i = 0; i < ARRAY_SIZE (history); i++)
4558         history[i].fixed_p = 1;
4559
4560       /* Insert any nops that might be needed between the .set noreorder
4561          block and the previous instructions.  We will later remove any
4562          nops that turn out not to be needed.  */
4563       nops = nops_for_insn (0, history, NULL);
4564       if (nops > 0)
4565         {
4566           if (mips_optimize != 0)
4567             {
4568               /* Record the frag which holds the nop instructions, so
4569                  that we can remove them if we don't need them.  */
4570               frag_grow (nops * NOP_INSN_SIZE);
4571               prev_nop_frag = frag_now;
4572               prev_nop_frag_holds = nops;
4573               prev_nop_frag_required = 0;
4574               prev_nop_frag_since = 0;
4575             }
4576
4577           for (; nops > 0; --nops)
4578             add_fixed_insn (NOP_INSN);
4579
4580           /* Move on to a new frag, so that it is safe to simply
4581              decrease the size of prev_nop_frag.  */
4582           frag_wane (frag_now);
4583           frag_new (0);
4584           mips_move_labels ();
4585         }
4586       mips_mark_labels ();
4587       mips_clear_insn_labels ();
4588     }
4589   mips_opts.noreorder++;
4590   mips_any_noreorder = 1;
4591 }
4592
4593 /* End a nested noreorder block.  */
4594
4595 static void
4596 end_noreorder (void)
4597 {
4598   mips_opts.noreorder--;
4599   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
4600     {
4601       /* Commit to inserting prev_nop_frag_required nops and go back to
4602          handling nop insertion the .set reorder way.  */
4603       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
4604                                 * NOP_INSN_SIZE);
4605       insert_into_history (prev_nop_frag_since,
4606                            prev_nop_frag_required, NOP_INSN);
4607       prev_nop_frag = NULL;
4608     }
4609 }
4610
4611 /* Set up global variables for the start of a new macro.  */
4612
4613 static void
4614 macro_start (void)
4615 {
4616   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
4617   memset (&mips_macro_warning.first_insn_sizes, 0,
4618           sizeof (mips_macro_warning.first_insn_sizes));
4619   memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
4620   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
4621                                      && delayed_branch_p (&history[0]));
4622   switch (history[0].insn_mo->pinfo2
4623           & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
4624     {
4625     case INSN2_BRANCH_DELAY_32BIT:
4626       mips_macro_warning.delay_slot_length = 4;
4627       break;
4628     case INSN2_BRANCH_DELAY_16BIT:
4629       mips_macro_warning.delay_slot_length = 2;
4630       break;
4631     default:
4632       mips_macro_warning.delay_slot_length = 0;
4633       break;
4634     }
4635   mips_macro_warning.first_frag = NULL;
4636 }
4637
4638 /* Given that a macro is longer than one instruction or of the wrong size,
4639    return the appropriate warning for it.  Return null if no warning is
4640    needed.  SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
4641    RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
4642    and RELAX_NOMACRO.  */
4643
4644 static const char *
4645 macro_warning (relax_substateT subtype)
4646 {
4647   if (subtype & RELAX_DELAY_SLOT)
4648     return _("Macro instruction expanded into multiple instructions"
4649              " in a branch delay slot");
4650   else if (subtype & RELAX_NOMACRO)
4651     return _("Macro instruction expanded into multiple instructions");
4652   else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
4653                       | RELAX_DELAY_SLOT_SIZE_SECOND))
4654     return ((subtype & RELAX_DELAY_SLOT_16BIT)
4655             ? _("Macro instruction expanded into a wrong size instruction"
4656                 " in a 16-bit branch delay slot")
4657             : _("Macro instruction expanded into a wrong size instruction"
4658                 " in a 32-bit branch delay slot"));
4659   else
4660     return 0;
4661 }
4662
4663 /* Finish up a macro.  Emit warnings as appropriate.  */
4664
4665 static void
4666 macro_end (void)
4667 {
4668   /* Relaxation warning flags.  */
4669   relax_substateT subtype = 0;
4670
4671   /* Check delay slot size requirements.  */
4672   if (mips_macro_warning.delay_slot_length == 2)
4673     subtype |= RELAX_DELAY_SLOT_16BIT;
4674   if (mips_macro_warning.delay_slot_length != 0)
4675     {
4676       if (mips_macro_warning.delay_slot_length
4677           != mips_macro_warning.first_insn_sizes[0])
4678         subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
4679       if (mips_macro_warning.delay_slot_length
4680           != mips_macro_warning.first_insn_sizes[1])
4681         subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
4682     }
4683
4684   /* Check instruction count requirements.  */
4685   if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
4686     {
4687       if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
4688         subtype |= RELAX_SECOND_LONGER;
4689       if (mips_opts.warn_about_macros)
4690         subtype |= RELAX_NOMACRO;
4691       if (mips_macro_warning.delay_slot_p)
4692         subtype |= RELAX_DELAY_SLOT;
4693     }
4694
4695   /* If both alternatives fail to fill a delay slot correctly,
4696      emit the warning now.  */
4697   if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
4698       && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
4699     {
4700       relax_substateT s;
4701       const char *msg;
4702
4703       s = subtype & (RELAX_DELAY_SLOT_16BIT
4704                      | RELAX_DELAY_SLOT_SIZE_FIRST
4705                      | RELAX_DELAY_SLOT_SIZE_SECOND);
4706       msg = macro_warning (s);
4707       if (msg != NULL)
4708         as_warn ("%s", msg);
4709       subtype &= ~s;
4710     }
4711
4712   /* If both implementations are longer than 1 instruction, then emit the
4713      warning now.  */
4714   if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
4715     {
4716       relax_substateT s;
4717       const char *msg;
4718
4719       s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
4720       msg = macro_warning (s);
4721       if (msg != NULL)
4722         as_warn ("%s", msg);
4723       subtype &= ~s;
4724     }
4725
4726   /* If any flags still set, then one implementation might need a warning
4727      and the other either will need one of a different kind or none at all.
4728      Pass any remaining flags over to relaxation.  */
4729   if (mips_macro_warning.first_frag != NULL)
4730     mips_macro_warning.first_frag->fr_subtype |= subtype;
4731 }
4732
4733 /* Instruction operand formats used in macros that vary between
4734    standard MIPS and microMIPS code.  */
4735
4736 static const char * const brk_fmt[2] = { "c", "mF" };
4737 static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
4738 static const char * const jalr_fmt[2] = { "d,s", "t,s" };
4739 static const char * const lui_fmt[2] = { "t,u", "s,u" };
4740 static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
4741 static const char * const mfhl_fmt[2] = { "d", "mj" };
4742 static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
4743 static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
4744
4745 #define BRK_FMT (brk_fmt[mips_opts.micromips])
4746 #define COP12_FMT (cop12_fmt[mips_opts.micromips])
4747 #define JALR_FMT (jalr_fmt[mips_opts.micromips])
4748 #define LUI_FMT (lui_fmt[mips_opts.micromips])
4749 #define MEM12_FMT (mem12_fmt[mips_opts.micromips])
4750 #define MFHL_FMT (mfhl_fmt[mips_opts.micromips])
4751 #define SHFT_FMT (shft_fmt[mips_opts.micromips])
4752 #define TRAP_FMT (trap_fmt[mips_opts.micromips])
4753
4754 /* Read a macro's relocation codes from *ARGS and store them in *R.
4755    The first argument in *ARGS will be either the code for a single
4756    relocation or -1 followed by the three codes that make up a
4757    composite relocation.  */
4758
4759 static void
4760 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
4761 {
4762   int i, next;
4763
4764   next = va_arg (*args, int);
4765   if (next >= 0)
4766     r[0] = (bfd_reloc_code_real_type) next;
4767   else
4768     for (i = 0; i < 3; i++)
4769       r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
4770 }
4771
4772 /* Build an instruction created by a macro expansion.  This is passed
4773    a pointer to the count of instructions created so far, an
4774    expression, the name of the instruction to build, an operand format
4775    string, and corresponding arguments.  */
4776
4777 static void
4778 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
4779 {
4780   const struct mips_opcode *mo = NULL;
4781   bfd_reloc_code_real_type r[3];
4782   const struct mips_opcode *amo;
4783   struct hash_control *hash;
4784   struct mips_cl_insn insn;
4785   va_list args;
4786
4787   va_start (args, fmt);
4788
4789   if (mips_opts.mips16)
4790     {
4791       mips16_macro_build (ep, name, fmt, &args);
4792       va_end (args);
4793       return;
4794     }
4795
4796   r[0] = BFD_RELOC_UNUSED;
4797   r[1] = BFD_RELOC_UNUSED;
4798   r[2] = BFD_RELOC_UNUSED;
4799   hash = mips_opts.micromips ? micromips_op_hash : op_hash;
4800   amo = (struct mips_opcode *) hash_find (hash, name);
4801   gas_assert (amo);
4802   gas_assert (strcmp (name, amo->name) == 0);
4803
4804   do
4805     {
4806       /* Search until we get a match for NAME.  It is assumed here that
4807          macros will never generate MDMX, MIPS-3D, or MT instructions.
4808          We try to match an instruction that fulfils the branch delay
4809          slot instruction length requirement (if any) of the previous
4810          instruction.  While doing this we record the first instruction
4811          seen that matches all the other conditions and use it anyway
4812          if the requirement cannot be met; we will issue an appropriate
4813          warning later on.  */
4814       if (strcmp (fmt, amo->args) == 0
4815           && amo->pinfo != INSN_MACRO
4816           && is_opcode_valid (amo)
4817           && is_size_valid (amo))
4818         {
4819           if (is_delay_slot_valid (amo))
4820             {
4821               mo = amo;
4822               break;
4823             }
4824           else if (!mo)
4825             mo = amo;
4826         }
4827
4828       ++amo;
4829       gas_assert (amo->name);
4830     }
4831   while (strcmp (name, amo->name) == 0);
4832
4833   gas_assert (mo);
4834   create_insn (&insn, mo);
4835   for (;;)
4836     {
4837       switch (*fmt++)
4838         {
4839         case '\0':
4840           break;
4841
4842         case ',':
4843         case '(':
4844         case ')':
4845           continue;
4846
4847         case '+':
4848           switch (*fmt++)
4849             {
4850             case 'A':
4851             case 'E':
4852               INSERT_OPERAND (mips_opts.micromips,
4853                               EXTLSB, insn, va_arg (args, int));
4854               continue;
4855
4856             case 'B':
4857             case 'F':
4858               /* Note that in the macro case, these arguments are already
4859                  in MSB form.  (When handling the instruction in the
4860                  non-macro case, these arguments are sizes from which
4861                  MSB values must be calculated.)  */
4862               INSERT_OPERAND (mips_opts.micromips,
4863                               INSMSB, insn, va_arg (args, int));
4864               continue;
4865
4866             case 'C':
4867             case 'G':
4868             case 'H':
4869               /* Note that in the macro case, these arguments are already
4870                  in MSBD form.  (When handling the instruction in the
4871                  non-macro case, these arguments are sizes from which
4872                  MSBD values must be calculated.)  */
4873               INSERT_OPERAND (mips_opts.micromips,
4874                               EXTMSBD, insn, va_arg (args, int));
4875               continue;
4876
4877             case 'Q':
4878               gas_assert (!mips_opts.micromips);
4879               INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
4880               continue;
4881
4882             default:
4883               internalError ();
4884             }
4885           continue;
4886
4887         case '2':
4888           gas_assert (!mips_opts.micromips);
4889           INSERT_OPERAND (0, BP, insn, va_arg (args, int));
4890           continue;
4891
4892         case 'n':
4893           gas_assert (mips_opts.micromips);
4894         case 't':
4895         case 'w':
4896         case 'E':
4897           INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
4898           continue;
4899
4900         case 'c':
4901           gas_assert (!mips_opts.micromips);
4902           INSERT_OPERAND (0, CODE, insn, va_arg (args, int));
4903           continue;
4904
4905         case 'W':
4906           gas_assert (!mips_opts.micromips);
4907         case 'T':
4908           INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
4909           continue;
4910
4911         case 'G':
4912           if (mips_opts.micromips)
4913             INSERT_OPERAND (1, RS, insn, va_arg (args, int));
4914           else
4915             INSERT_OPERAND (0, RD, insn, va_arg (args, int));
4916           continue;
4917
4918         case 'K':
4919           gas_assert (!mips_opts.micromips);
4920         case 'd':
4921           INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
4922           continue;
4923
4924         case 'U':
4925           gas_assert (!mips_opts.micromips);
4926           {
4927             int tmp = va_arg (args, int);
4928
4929             INSERT_OPERAND (0, RT, insn, tmp);
4930             INSERT_OPERAND (0, RD, insn, tmp);
4931           }
4932           continue;
4933
4934         case 'V':
4935         case 'S':
4936           gas_assert (!mips_opts.micromips);
4937           INSERT_OPERAND (0, FS, insn, va_arg (args, int));
4938           continue;
4939
4940         case 'z':
4941           continue;
4942
4943         case '<':
4944           INSERT_OPERAND (mips_opts.micromips,
4945                           SHAMT, insn, va_arg (args, int));
4946           continue;
4947
4948         case 'D':
4949           gas_assert (!mips_opts.micromips);
4950           INSERT_OPERAND (0, FD, insn, va_arg (args, int));
4951           continue;
4952
4953         case 'B':
4954           gas_assert (!mips_opts.micromips);
4955           INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
4956           continue;
4957
4958         case 'J':
4959           gas_assert (!mips_opts.micromips);
4960           INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4961           continue;
4962
4963         case 'q':
4964           gas_assert (!mips_opts.micromips);
4965           INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
4966           continue;
4967
4968         case 'b':
4969         case 's':
4970         case 'r':
4971         case 'v':
4972           INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
4973           continue;
4974
4975         case 'i':
4976         case 'j':
4977           macro_read_relocs (&args, r);
4978           gas_assert (*r == BFD_RELOC_GPREL16
4979                       || *r == BFD_RELOC_MIPS_HIGHER
4980                       || *r == BFD_RELOC_HI16_S
4981                       || *r == BFD_RELOC_LO16
4982                       || *r == BFD_RELOC_MIPS_GOT_OFST);
4983           continue;
4984
4985         case 'o':
4986           macro_read_relocs (&args, r);
4987           continue;
4988
4989         case 'u':
4990           macro_read_relocs (&args, r);
4991           gas_assert (ep != NULL
4992                       && (ep->X_op == O_constant
4993                           || (ep->X_op == O_symbol
4994                               && (*r == BFD_RELOC_MIPS_HIGHEST
4995                                   || *r == BFD_RELOC_HI16_S
4996                                   || *r == BFD_RELOC_HI16
4997                                   || *r == BFD_RELOC_GPREL16
4998                                   || *r == BFD_RELOC_MIPS_GOT_HI16
4999                                   || *r == BFD_RELOC_MIPS_CALL_HI16))));
5000           continue;
5001
5002         case 'p':
5003           gas_assert (ep != NULL);
5004
5005           /*
5006            * This allows macro() to pass an immediate expression for
5007            * creating short branches without creating a symbol.
5008            *
5009            * We don't allow branch relaxation for these branches, as
5010            * they should only appear in ".set nomacro" anyway.
5011            */
5012           if (ep->X_op == O_constant)
5013             {
5014               /* For microMIPS we always use relocations for branches.
5015                  So we should not resolve immediate values.  */
5016               gas_assert (!mips_opts.micromips);
5017
5018               if ((ep->X_add_number & 3) != 0)
5019                 as_bad (_("branch to misaligned address (0x%lx)"),
5020                         (unsigned long) ep->X_add_number);
5021               if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5022                 as_bad (_("branch address range overflow (0x%lx)"),
5023                         (unsigned long) ep->X_add_number);
5024               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5025               ep = NULL;
5026             }
5027           else
5028             *r = BFD_RELOC_16_PCREL_S2;
5029           continue;
5030
5031         case 'a':
5032           gas_assert (ep != NULL);
5033           *r = BFD_RELOC_MIPS_JMP;
5034           continue;
5035
5036         case 'C':
5037           gas_assert (!mips_opts.micromips);
5038           INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
5039           continue;
5040
5041         case 'k':
5042           INSERT_OPERAND (mips_opts.micromips,
5043                           CACHE, insn, va_arg (args, unsigned long));
5044           continue;
5045
5046         case '|':
5047           gas_assert (mips_opts.micromips);
5048           INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5049           continue;
5050
5051         case '.':
5052           gas_assert (mips_opts.micromips);
5053           INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5054           continue;
5055
5056         case '\\':
5057           INSERT_OPERAND (mips_opts.micromips,
5058                           3BITPOS, insn, va_arg (args, unsigned int));
5059           continue;
5060
5061         case '~':
5062           INSERT_OPERAND (mips_opts.micromips,
5063                           OFFSET12, insn, va_arg (args, unsigned long));
5064           continue;
5065
5066         case 'N':
5067           gas_assert (mips_opts.micromips);
5068           INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5069           continue;
5070
5071         case 'm':       /* Opcode extension character.  */
5072           gas_assert (mips_opts.micromips);
5073           switch (*fmt++)
5074             {
5075             case 'j':
5076               INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5077               break;
5078
5079             case 'p':
5080               INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5081               break;
5082
5083             case 'F':
5084               INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5085               break;
5086
5087             default:
5088               internalError ();
5089             }
5090           continue;
5091
5092         default:
5093           internalError ();
5094         }
5095       break;
5096     }
5097   va_end (args);
5098   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5099
5100   append_insn (&insn, ep, r, TRUE);
5101 }
5102
5103 static void
5104 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
5105                     va_list *args)
5106 {
5107   struct mips_opcode *mo;
5108   struct mips_cl_insn insn;
5109   bfd_reloc_code_real_type r[3]
5110     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5111
5112   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
5113   gas_assert (mo);
5114   gas_assert (strcmp (name, mo->name) == 0);
5115
5116   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
5117     {
5118       ++mo;
5119       gas_assert (mo->name);
5120       gas_assert (strcmp (name, mo->name) == 0);
5121     }
5122
5123   create_insn (&insn, mo);
5124   for (;;)
5125     {
5126       int c;
5127
5128       c = *fmt++;
5129       switch (c)
5130         {
5131         case '\0':
5132           break;
5133
5134         case ',':
5135         case '(':
5136         case ')':
5137           continue;
5138
5139         case 'y':
5140         case 'w':
5141           MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
5142           continue;
5143
5144         case 'x':
5145         case 'v':
5146           MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
5147           continue;
5148
5149         case 'z':
5150           MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
5151           continue;
5152
5153         case 'Z':
5154           MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
5155           continue;
5156
5157         case '0':
5158         case 'S':
5159         case 'P':
5160         case 'R':
5161           continue;
5162
5163         case 'X':
5164           MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
5165           continue;
5166
5167         case 'Y':
5168           {
5169             int regno;
5170
5171             regno = va_arg (*args, int);
5172             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
5173             MIPS16_INSERT_OPERAND (REG32R, insn, regno);
5174           }
5175           continue;
5176
5177         case '<':
5178         case '>':
5179         case '4':
5180         case '5':
5181         case 'H':
5182         case 'W':
5183         case 'D':
5184         case 'j':
5185         case '8':
5186         case 'V':
5187         case 'C':
5188         case 'U':
5189         case 'k':
5190         case 'K':
5191         case 'p':
5192         case 'q':
5193           {
5194             gas_assert (ep != NULL);
5195
5196             if (ep->X_op != O_constant)
5197               *r = (int) BFD_RELOC_UNUSED + c;
5198             else
5199               {
5200                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
5201                               FALSE, &insn.insn_opcode, &insn.use_extend,
5202                               &insn.extend);
5203                 ep = NULL;
5204                 *r = BFD_RELOC_UNUSED;
5205               }
5206           }
5207           continue;
5208
5209         case '6':
5210           MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
5211           continue;
5212         }
5213
5214       break;
5215     }
5216
5217   gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
5218
5219   append_insn (&insn, ep, r, TRUE);
5220 }
5221
5222 /*
5223  * Sign-extend 32-bit mode constants that have bit 31 set and all
5224  * higher bits unset.
5225  */
5226 static void
5227 normalize_constant_expr (expressionS *ex)
5228 {
5229   if (ex->X_op == O_constant
5230       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5231     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5232                         - 0x80000000);
5233 }
5234
5235 /*
5236  * Sign-extend 32-bit mode address offsets that have bit 31 set and
5237  * all higher bits unset.
5238  */
5239 static void
5240 normalize_address_expr (expressionS *ex)
5241 {
5242   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5243         || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5244       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5245     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5246                         - 0x80000000);
5247 }
5248
5249 /*
5250  * Generate a "jalr" instruction with a relocation hint to the called
5251  * function.  This occurs in NewABI PIC code.
5252  */
5253 static void
5254 macro_build_jalr (expressionS *ep, int cprestore)
5255 {
5256   static const bfd_reloc_code_real_type jalr_relocs[2]
5257     = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5258   bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5259   const char *jalr;
5260   char *f = NULL;
5261
5262   if (MIPS_JALR_HINT_P (ep))
5263     {
5264       frag_grow (8);
5265       f = frag_more (0);
5266     }
5267   if (mips_opts.micromips)
5268     {
5269       jalr = mips_opts.noreorder && !cprestore ? "jalr" : "jalrs";
5270       if (MIPS_JALR_HINT_P (ep))
5271         macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5272       else
5273         macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5274     }
5275   else
5276     macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
5277   if (MIPS_JALR_HINT_P (ep))
5278     fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
5279 }
5280
5281 /*
5282  * Generate a "lui" instruction.
5283  */
5284 static void
5285 macro_build_lui (expressionS *ep, int regnum)
5286 {
5287   gas_assert (! mips_opts.mips16);
5288
5289   if (ep->X_op != O_constant)
5290     {
5291       gas_assert (ep->X_op == O_symbol);
5292       /* _gp_disp is a special case, used from s_cpload.
5293          __gnu_local_gp is used if mips_no_shared.  */
5294       gas_assert (mips_pic == NO_PIC
5295               || (! HAVE_NEWABI
5296                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5297               || (! mips_in_shared
5298                   && strcmp (S_GET_NAME (ep->X_add_symbol),
5299                              "__gnu_local_gp") == 0));
5300     }
5301
5302   macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
5303 }
5304
5305 /* Generate a sequence of instructions to do a load or store from a constant
5306    offset off of a base register (breg) into/from a target register (treg),
5307    using AT if necessary.  */
5308 static void
5309 macro_build_ldst_constoffset (expressionS *ep, const char *op,
5310                               int treg, int breg, int dbl)
5311 {
5312   gas_assert (ep->X_op == O_constant);
5313
5314   /* Sign-extending 32-bit constants makes their handling easier.  */
5315   if (!dbl)
5316     normalize_constant_expr (ep);
5317
5318   /* Right now, this routine can only handle signed 32-bit constants.  */
5319   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
5320     as_warn (_("operand overflow"));
5321
5322   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5323     {
5324       /* Signed 16-bit offset will fit in the op.  Easy!  */
5325       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
5326     }
5327   else
5328     {
5329       /* 32-bit offset, need multiple instructions and AT, like:
5330            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
5331            addu     $tempreg,$tempreg,$breg
5332            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
5333          to handle the complete offset.  */
5334       macro_build_lui (ep, AT);
5335       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5336       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
5337
5338       if (!mips_opts.at)
5339         as_bad (_("Macro used $at after \".set noat\""));
5340     }
5341 }
5342
5343 /*                      set_at()
5344  * Generates code to set the $at register to true (one)
5345  * if reg is less than the immediate expression.
5346  */
5347 static void
5348 set_at (int reg, int unsignedp)
5349 {
5350   if (imm_expr.X_op == O_constant
5351       && imm_expr.X_add_number >= -0x8000
5352       && imm_expr.X_add_number < 0x8000)
5353     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5354                  AT, reg, BFD_RELOC_LO16);
5355   else
5356     {
5357       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5358       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
5359     }
5360 }
5361
5362 /* Warn if an expression is not a constant.  */
5363
5364 static void
5365 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
5366 {
5367   if (ex->X_op == O_big)
5368     as_bad (_("unsupported large constant"));
5369   else if (ex->X_op != O_constant)
5370     as_bad (_("Instruction %s requires absolute expression"),
5371             ip->insn_mo->name);
5372
5373   if (HAVE_32BIT_GPRS)
5374     normalize_constant_expr (ex);
5375 }
5376
5377 /* Count the leading zeroes by performing a binary chop. This is a
5378    bulky bit of source, but performance is a LOT better for the
5379    majority of values than a simple loop to count the bits:
5380        for (lcnt = 0; (lcnt < 32); lcnt++)
5381          if ((v) & (1 << (31 - lcnt)))
5382            break;
5383   However it is not code size friendly, and the gain will drop a bit
5384   on certain cached systems.
5385 */
5386 #define COUNT_TOP_ZEROES(v)             \
5387   (((v) & ~0xffff) == 0                 \
5388    ? ((v) & ~0xff) == 0                 \
5389      ? ((v) & ~0xf) == 0                \
5390        ? ((v) & ~0x3) == 0              \
5391          ? ((v) & ~0x1) == 0            \
5392            ? !(v)                       \
5393              ? 32                       \
5394              : 31                       \
5395            : 30                         \
5396          : ((v) & ~0x7) == 0            \
5397            ? 29                         \
5398            : 28                         \
5399        : ((v) & ~0x3f) == 0             \
5400          ? ((v) & ~0x1f) == 0           \
5401            ? 27                         \
5402            : 26                         \
5403          : ((v) & ~0x7f) == 0           \
5404            ? 25                         \
5405            : 24                         \
5406      : ((v) & ~0xfff) == 0              \
5407        ? ((v) & ~0x3ff) == 0            \
5408          ? ((v) & ~0x1ff) == 0          \
5409            ? 23                         \
5410            : 22                         \
5411          : ((v) & ~0x7ff) == 0          \
5412            ? 21                         \
5413            : 20                         \
5414        : ((v) & ~0x3fff) == 0           \
5415          ? ((v) & ~0x1fff) == 0         \
5416            ? 19                         \
5417            : 18                         \
5418          : ((v) & ~0x7fff) == 0         \
5419            ? 17                         \
5420            : 16                         \
5421    : ((v) & ~0xffffff) == 0             \
5422      ? ((v) & ~0xfffff) == 0            \
5423        ? ((v) & ~0x3ffff) == 0          \
5424          ? ((v) & ~0x1ffff) == 0        \
5425            ? 15                         \
5426            : 14                         \
5427          : ((v) & ~0x7ffff) == 0        \
5428            ? 13                         \
5429            : 12                         \
5430        : ((v) & ~0x3fffff) == 0         \
5431          ? ((v) & ~0x1fffff) == 0       \
5432            ? 11                         \
5433            : 10                         \
5434          : ((v) & ~0x7fffff) == 0       \
5435            ? 9                          \
5436            : 8                          \
5437      : ((v) & ~0xfffffff) == 0          \
5438        ? ((v) & ~0x3ffffff) == 0        \
5439          ? ((v) & ~0x1ffffff) == 0      \
5440            ? 7                          \
5441            : 6                          \
5442          : ((v) & ~0x7ffffff) == 0      \
5443            ? 5                          \
5444            : 4                          \
5445        : ((v) & ~0x3fffffff) == 0       \
5446          ? ((v) & ~0x1fffffff) == 0     \
5447            ? 3                          \
5448            : 2                          \
5449          : ((v) & ~0x7fffffff) == 0     \
5450            ? 1                          \
5451            : 0)
5452
5453 /*                      load_register()
5454  *  This routine generates the least number of instructions necessary to load
5455  *  an absolute expression value into a register.
5456  */
5457 static void
5458 load_register (int reg, expressionS *ep, int dbl)
5459 {
5460   int freg;
5461   expressionS hi32, lo32;
5462
5463   if (ep->X_op != O_big)
5464     {
5465       gas_assert (ep->X_op == O_constant);
5466
5467       /* Sign-extending 32-bit constants makes their handling easier.  */
5468       if (!dbl)
5469         normalize_constant_expr (ep);
5470
5471       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
5472         {
5473           /* We can handle 16 bit signed values with an addiu to
5474              $zero.  No need to ever use daddiu here, since $zero and
5475              the result are always correct in 32 bit mode.  */
5476           macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5477           return;
5478         }
5479       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5480         {
5481           /* We can handle 16 bit unsigned values with an ori to
5482              $zero.  */
5483           macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5484           return;
5485         }
5486       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
5487         {
5488           /* 32 bit values require an lui.  */
5489           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5490           if ((ep->X_add_number & 0xffff) != 0)
5491             macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5492           return;
5493         }
5494     }
5495
5496   /* The value is larger than 32 bits.  */
5497
5498   if (!dbl || HAVE_32BIT_GPRS)
5499     {
5500       char value[32];
5501
5502       sprintf_vma (value, ep->X_add_number);
5503       as_bad (_("Number (0x%s) larger than 32 bits"), value);
5504       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5505       return;
5506     }
5507
5508   if (ep->X_op != O_big)
5509     {
5510       hi32 = *ep;
5511       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5512       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
5513       hi32.X_add_number &= 0xffffffff;
5514       lo32 = *ep;
5515       lo32.X_add_number &= 0xffffffff;
5516     }
5517   else
5518     {
5519       gas_assert (ep->X_add_number > 2);
5520       if (ep->X_add_number == 3)
5521         generic_bignum[3] = 0;
5522       else if (ep->X_add_number > 4)
5523         as_bad (_("Number larger than 64 bits"));
5524       lo32.X_op = O_constant;
5525       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
5526       hi32.X_op = O_constant;
5527       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
5528     }
5529
5530   if (hi32.X_add_number == 0)
5531     freg = 0;
5532   else
5533     {
5534       int shift, bit;
5535       unsigned long hi, lo;
5536
5537       if (hi32.X_add_number == (offsetT) 0xffffffff)
5538         {
5539           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
5540             {
5541               macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5542               return;
5543             }
5544           if (lo32.X_add_number & 0x80000000)
5545             {
5546               macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5547               if (lo32.X_add_number & 0xffff)
5548                 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
5549               return;
5550             }
5551         }
5552
5553       /* Check for 16bit shifted constant.  We know that hi32 is
5554          non-zero, so start the mask on the first bit of the hi32
5555          value.  */
5556       shift = 17;
5557       do
5558         {
5559           unsigned long himask, lomask;
5560
5561           if (shift < 32)
5562             {
5563               himask = 0xffff >> (32 - shift);
5564               lomask = (0xffff << shift) & 0xffffffff;
5565             }
5566           else
5567             {
5568               himask = 0xffff << (shift - 32);
5569               lomask = 0;
5570             }
5571           if ((hi32.X_add_number & ~(offsetT) himask) == 0
5572               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
5573             {
5574               expressionS tmp;
5575
5576               tmp.X_op = O_constant;
5577               if (shift < 32)
5578                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
5579                                     | (lo32.X_add_number >> shift));
5580               else
5581                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
5582               macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
5583               macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5584                            reg, reg, (shift >= 32) ? shift - 32 : shift);
5585               return;
5586             }
5587           ++shift;
5588         }
5589       while (shift <= (64 - 16));
5590
5591       /* Find the bit number of the lowest one bit, and store the
5592          shifted value in hi/lo.  */
5593       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
5594       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
5595       if (lo != 0)
5596         {
5597           bit = 0;
5598           while ((lo & 1) == 0)
5599             {
5600               lo >>= 1;
5601               ++bit;
5602             }
5603           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
5604           hi >>= bit;
5605         }
5606       else
5607         {
5608           bit = 32;
5609           while ((hi & 1) == 0)
5610             {
5611               hi >>= 1;
5612               ++bit;
5613             }
5614           lo = hi;
5615           hi = 0;
5616         }
5617
5618       /* Optimize if the shifted value is a (power of 2) - 1.  */
5619       if ((hi == 0 && ((lo + 1) & lo) == 0)
5620           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
5621         {
5622           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
5623           if (shift != 0)
5624             {
5625               expressionS tmp;
5626
5627               /* This instruction will set the register to be all
5628                  ones.  */
5629               tmp.X_op = O_constant;
5630               tmp.X_add_number = (offsetT) -1;
5631               macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
5632               if (bit != 0)
5633                 {
5634                   bit += shift;
5635                   macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
5636                                reg, reg, (bit >= 32) ? bit - 32 : bit);
5637                 }
5638               macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
5639                            reg, reg, (shift >= 32) ? shift - 32 : shift);
5640               return;
5641             }
5642         }
5643
5644       /* Sign extend hi32 before calling load_register, because we can
5645          generally get better code when we load a sign extended value.  */
5646       if ((hi32.X_add_number & 0x80000000) != 0)
5647         hi32.X_add_number |= ~(offsetT) 0xffffffff;
5648       load_register (reg, &hi32, 0);
5649       freg = reg;
5650     }
5651   if ((lo32.X_add_number & 0xffff0000) == 0)
5652     {
5653       if (freg != 0)
5654         {
5655           macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
5656           freg = reg;
5657         }
5658     }
5659   else
5660     {
5661       expressionS mid16;
5662
5663       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
5664         {
5665           macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
5666           macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
5667           return;
5668         }
5669
5670       if (freg != 0)
5671         {
5672           macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
5673           freg = reg;
5674         }
5675       mid16 = lo32;
5676       mid16.X_add_number >>= 16;
5677       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5678       macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5679       freg = reg;
5680     }
5681   if ((lo32.X_add_number & 0xffff) != 0)
5682     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
5683 }
5684
5685 static inline void
5686 load_delay_nop (void)
5687 {
5688   if (!gpr_interlocks)
5689     macro_build (NULL, "nop", "");
5690 }
5691
5692 /* Load an address into a register.  */
5693
5694 static void
5695 load_address (int reg, expressionS *ep, int *used_at)
5696 {
5697   if (ep->X_op != O_constant
5698       && ep->X_op != O_symbol)
5699     {
5700       as_bad (_("expression too complex"));
5701       ep->X_op = O_constant;
5702     }
5703
5704   if (ep->X_op == O_constant)
5705     {
5706       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
5707       return;
5708     }
5709
5710   if (mips_pic == NO_PIC)
5711     {
5712       /* If this is a reference to a GP relative symbol, we want
5713            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
5714          Otherwise we want
5715            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
5716            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
5717          If we have an addend, we always use the latter form.
5718
5719          With 64bit address space and a usable $at we want
5720            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
5721            lui          $at,<sym>               (BFD_RELOC_HI16_S)
5722            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
5723            daddiu       $at,<sym>               (BFD_RELOC_LO16)
5724            dsll32       $reg,0
5725            daddu        $reg,$reg,$at
5726
5727          If $at is already in use, we use a path which is suboptimal
5728          on superscalar processors.
5729            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
5730            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
5731            dsll         $reg,16
5732            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
5733            dsll         $reg,16
5734            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
5735
5736          For GP relative symbols in 64bit address space we can use
5737          the same sequence as in 32bit address space.  */
5738       if (HAVE_64BIT_SYMBOLS)
5739         {
5740           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5741               && !nopic_need_relax (ep->X_add_symbol, 1))
5742             {
5743               relax_start (ep->X_add_symbol);
5744               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5745                            mips_gp_register, BFD_RELOC_GPREL16);
5746               relax_switch ();
5747             }
5748
5749           if (*used_at == 0 && mips_opts.at)
5750             {
5751               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5752               macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
5753               macro_build (ep, "daddiu", "t,r,j", reg, reg,
5754                            BFD_RELOC_MIPS_HIGHER);
5755               macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
5756               macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
5757               macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
5758               *used_at = 1;
5759             }
5760           else
5761             {
5762               macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
5763               macro_build (ep, "daddiu", "t,r,j", reg, reg,
5764                            BFD_RELOC_MIPS_HIGHER);
5765               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5766               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
5767               macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
5768               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
5769             }
5770
5771           if (mips_relax.sequence)
5772             relax_end ();
5773         }
5774       else
5775         {
5776           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
5777               && !nopic_need_relax (ep->X_add_symbol, 1))
5778             {
5779               relax_start (ep->X_add_symbol);
5780               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
5781                            mips_gp_register, BFD_RELOC_GPREL16);
5782               relax_switch ();
5783             }
5784           macro_build_lui (ep, reg);
5785           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
5786                        reg, reg, BFD_RELOC_LO16);
5787           if (mips_relax.sequence)
5788             relax_end ();
5789         }
5790     }
5791   else if (!mips_big_got)
5792     {
5793       expressionS ex;
5794
5795       /* If this is a reference to an external symbol, we want
5796            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
5797          Otherwise we want
5798            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
5799            nop
5800            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
5801          If there is a constant, it must be added in after.
5802
5803          If we have NewABI, we want
5804            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5805          unless we're referencing a global symbol with a non-zero
5806          offset, in which case cst must be added separately.  */
5807       if (HAVE_NEWABI)
5808         {
5809           if (ep->X_add_number)
5810             {
5811               ex.X_add_number = ep->X_add_number;
5812               ep->X_add_number = 0;
5813               relax_start (ep->X_add_symbol);
5814               macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5815                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5816               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5817                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5818               ex.X_op = O_constant;
5819               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5820                            reg, reg, BFD_RELOC_LO16);
5821               ep->X_add_number = ex.X_add_number;
5822               relax_switch ();
5823             }
5824           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5825                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5826           if (mips_relax.sequence)
5827             relax_end ();
5828         }
5829       else
5830         {
5831           ex.X_add_number = ep->X_add_number;
5832           ep->X_add_number = 0;
5833           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5834                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
5835           load_delay_nop ();
5836           relax_start (ep->X_add_symbol);
5837           relax_switch ();
5838           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5839                        BFD_RELOC_LO16);
5840           relax_end ();
5841
5842           if (ex.X_add_number != 0)
5843             {
5844               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5845                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5846               ex.X_op = O_constant;
5847               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
5848                            reg, reg, BFD_RELOC_LO16);
5849             }
5850         }
5851     }
5852   else if (mips_big_got)
5853     {
5854       expressionS ex;
5855
5856       /* This is the large GOT case.  If this is a reference to an
5857          external symbol, we want
5858            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
5859            addu         $reg,$reg,$gp
5860            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
5861
5862          Otherwise, for a reference to a local symbol in old ABI, we want
5863            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
5864            nop
5865            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
5866          If there is a constant, it must be added in after.
5867
5868          In the NewABI, for local symbols, with or without offsets, we want:
5869            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5870            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5871       */
5872       if (HAVE_NEWABI)
5873         {
5874           ex.X_add_number = ep->X_add_number;
5875           ep->X_add_number = 0;
5876           relax_start (ep->X_add_symbol);
5877           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
5878           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5879                        reg, reg, mips_gp_register);
5880           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5881                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
5882           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5883             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5884           else if (ex.X_add_number)
5885             {
5886               ex.X_op = O_constant;
5887               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5888                            BFD_RELOC_LO16);
5889             }
5890
5891           ep->X_add_number = ex.X_add_number;
5892           relax_switch ();
5893           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5894                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5895           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5896                        BFD_RELOC_MIPS_GOT_OFST);
5897           relax_end ();
5898         }
5899       else
5900         {
5901           ex.X_add_number = ep->X_add_number;
5902           ep->X_add_number = 0;
5903           relax_start (ep->X_add_symbol);
5904           macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
5905           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5906                        reg, reg, mips_gp_register);
5907           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
5908                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
5909           relax_switch ();
5910           if (reg_needs_delay (mips_gp_register))
5911             {
5912               /* We need a nop before loading from $gp.  This special
5913                  check is required because the lui which starts the main
5914                  instruction stream does not refer to $gp, and so will not
5915                  insert the nop which may be required.  */
5916               macro_build (NULL, "nop", "");
5917             }
5918           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
5919                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
5920           load_delay_nop ();
5921           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5922                        BFD_RELOC_LO16);
5923           relax_end ();
5924
5925           if (ex.X_add_number != 0)
5926             {
5927               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
5928                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5929               ex.X_op = O_constant;
5930               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
5931                            BFD_RELOC_LO16);
5932             }
5933         }
5934     }
5935   else
5936     abort ();
5937
5938   if (!mips_opts.at && *used_at == 1)
5939     as_bad (_("Macro used $at after \".set noat\""));
5940 }
5941
5942 /* Move the contents of register SOURCE into register DEST.  */
5943
5944 static void
5945 move_register (int dest, int source)
5946 {
5947   /* Prefer to use a 16-bit microMIPS instruction unless the previous
5948      instruction specifically requires a 32-bit one.  */
5949   if (mips_opts.micromips
5950       && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
5951     macro_build (NULL, "move", "mp,mj", dest, source);
5952   else
5953     macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
5954                  dest, source, 0);
5955 }
5956
5957 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
5958    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
5959    The two alternatives are:
5960
5961    Global symbol                Local sybmol
5962    -------------                ------------
5963    lw DEST,%got(SYMBOL)         lw DEST,%got(SYMBOL + OFFSET)
5964    ...                          ...
5965    addiu DEST,DEST,OFFSET       addiu DEST,DEST,%lo(SYMBOL + OFFSET)
5966
5967    load_got_offset emits the first instruction and add_got_offset
5968    emits the second for a 16-bit offset or add_got_offset_hilo emits
5969    a sequence to add a 32-bit offset using a scratch register.  */
5970
5971 static void
5972 load_got_offset (int dest, expressionS *local)
5973 {
5974   expressionS global;
5975
5976   global = *local;
5977   global.X_add_number = 0;
5978
5979   relax_start (local->X_add_symbol);
5980   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
5981                BFD_RELOC_MIPS_GOT16, mips_gp_register);
5982   relax_switch ();
5983   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
5984                BFD_RELOC_MIPS_GOT16, mips_gp_register);
5985   relax_end ();
5986 }
5987
5988 static void
5989 add_got_offset (int dest, expressionS *local)
5990 {
5991   expressionS global;
5992
5993   global.X_op = O_constant;
5994   global.X_op_symbol = NULL;
5995   global.X_add_symbol = NULL;
5996   global.X_add_number = local->X_add_number;
5997
5998   relax_start (local->X_add_symbol);
5999   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
6000                dest, dest, BFD_RELOC_LO16);
6001   relax_switch ();
6002   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
6003   relax_end ();
6004 }
6005
6006 static void
6007 add_got_offset_hilo (int dest, expressionS *local, int tmp)
6008 {
6009   expressionS global;
6010   int hold_mips_optimize;
6011
6012   global.X_op = O_constant;
6013   global.X_op_symbol = NULL;
6014   global.X_add_symbol = NULL;
6015   global.X_add_number = local->X_add_number;
6016
6017   relax_start (local->X_add_symbol);
6018   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6019   relax_switch ();
6020   /* Set mips_optimize around the lui instruction to avoid
6021      inserting an unnecessary nop after the lw.  */
6022   hold_mips_optimize = mips_optimize;
6023   mips_optimize = 2;
6024   macro_build_lui (&global, tmp);
6025   mips_optimize = hold_mips_optimize;
6026   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6027   relax_end ();
6028
6029   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6030 }
6031
6032 /* Emit a sequence of instructions to emulate a branch likely operation.
6033    BR is an ordinary branch corresponding to one to be emulated.  BRNEG
6034    is its complementing branch with the original condition negated.
6035    CALL is set if the original branch specified the link operation.
6036    EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6037
6038    Code like this is produced in the noreorder mode:
6039
6040         BRNEG   <args>, 1f
6041          nop
6042         b       <sym>
6043          delay slot (executed only if branch taken)
6044     1:
6045
6046    or, if CALL is set:
6047
6048         BRNEG   <args>, 1f
6049          nop
6050         bal     <sym>
6051          delay slot (executed only if branch taken)
6052     1:
6053
6054    In the reorder mode the delay slot would be filled with a nop anyway,
6055    so code produced is simply:
6056
6057         BR      <args>, <sym>
6058          nop
6059
6060    This function is used when producing code for the microMIPS ASE that
6061    does not implement branch likely instructions in hardware.  */
6062
6063 static void
6064 macro_build_branch_likely (const char *br, const char *brneg,
6065                            int call, expressionS *ep, const char *fmt,
6066                            unsigned int sreg, unsigned int treg)
6067 {
6068   int noreorder = mips_opts.noreorder;
6069   expressionS expr1;
6070
6071   gas_assert (mips_opts.micromips);
6072   start_noreorder ();
6073   if (noreorder)
6074     {
6075       micromips_label_expr (&expr1);
6076       macro_build (&expr1, brneg, fmt, sreg, treg);
6077       macro_build (NULL, "nop", "");
6078       macro_build (ep, call ? "bal" : "b", "p");
6079
6080       /* Set to true so that append_insn adds a label.  */
6081       emit_branch_likely_macro = TRUE;
6082     }
6083   else
6084     {
6085       macro_build (ep, br, fmt, sreg, treg);
6086       macro_build (NULL, "nop", "");
6087     }
6088   end_noreorder ();
6089 }
6090
6091 /* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6092    the condition code tested.  EP specifies the branch target.  */
6093
6094 static void
6095 macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6096 {
6097   const int call = 0;
6098   const char *brneg;
6099   const char *br;
6100
6101   switch (type)
6102     {
6103     case M_BC1FL:
6104       br = "bc1f";
6105       brneg = "bc1t";
6106       break;
6107     case M_BC1TL:
6108       br = "bc1t";
6109       brneg = "bc1f";
6110       break;
6111     case M_BC2FL:
6112       br = "bc2f";
6113       brneg = "bc2t";
6114       break;
6115     case M_BC2TL:
6116       br = "bc2t";
6117       brneg = "bc2f";
6118       break;
6119     default:
6120       abort ();
6121     }
6122   macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6123 }
6124
6125 /* Emit a two-argument branch macro specified by TYPE, using SREG as
6126    the register tested.  EP specifies the branch target.  */
6127
6128 static void
6129 macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6130 {
6131   const char *brneg = NULL;
6132   const char *br;
6133   int call = 0;
6134
6135   switch (type)
6136     {
6137     case M_BGEZ:
6138       br = "bgez";
6139       break;
6140     case M_BGEZL:
6141       br = mips_opts.micromips ? "bgez" : "bgezl";
6142       brneg = "bltz";
6143       break;
6144     case M_BGEZALL:
6145       gas_assert (mips_opts.micromips);
6146       br = "bgezals";
6147       brneg = "bltz";
6148       call = 1;
6149       break;
6150     case M_BGTZ:
6151       br = "bgtz";
6152       break;
6153     case M_BGTZL:
6154       br = mips_opts.micromips ? "bgtz" : "bgtzl";
6155       brneg = "blez";
6156       break;
6157     case M_BLEZ:
6158       br = "blez";
6159       break;
6160     case M_BLEZL:
6161       br = mips_opts.micromips ? "blez" : "blezl";
6162       brneg = "bgtz";
6163       break;
6164     case M_BLTZ:
6165       br = "bltz";
6166       break;
6167     case M_BLTZL:
6168       br = mips_opts.micromips ? "bltz" : "bltzl";
6169       brneg = "bgez";
6170       break;
6171     case M_BLTZALL:
6172       gas_assert (mips_opts.micromips);
6173       br = "bltzals";
6174       brneg = "bgez";
6175       call = 1;
6176       break;
6177     default:
6178       abort ();
6179     }
6180   if (mips_opts.micromips && brneg)
6181     macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6182   else
6183     macro_build (ep, br, "s,p", sreg);
6184 }
6185
6186 /* Emit a three-argument branch macro specified by TYPE, using SREG and
6187    TREG as the registers tested.  EP specifies the branch target.  */
6188
6189 static void
6190 macro_build_branch_rsrt (int type, expressionS *ep,
6191                          unsigned int sreg, unsigned int treg)
6192 {
6193   const char *brneg = NULL;
6194   const int call = 0;
6195   const char *br;
6196
6197   switch (type)
6198     {
6199     case M_BEQ:
6200     case M_BEQ_I:
6201       br = "beq";
6202       break;
6203     case M_BEQL:
6204     case M_BEQL_I:
6205       br = mips_opts.micromips ? "beq" : "beql";
6206       brneg = "bne";
6207       break;
6208     case M_BNE:
6209     case M_BNE_I:
6210       br = "bne";
6211       break;
6212     case M_BNEL:
6213     case M_BNEL_I:
6214       br = mips_opts.micromips ? "bne" : "bnel";
6215       brneg = "beq";
6216       break;
6217     default:
6218       abort ();
6219     }
6220   if (mips_opts.micromips && brneg)
6221     macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6222   else
6223     macro_build (ep, br, "s,t,p", sreg, treg);
6224 }
6225
6226 /*
6227  *                      Build macros
6228  *   This routine implements the seemingly endless macro or synthesized
6229  * instructions and addressing modes in the mips assembly language. Many
6230  * of these macros are simple and are similar to each other. These could
6231  * probably be handled by some kind of table or grammar approach instead of
6232  * this verbose method. Others are not simple macros but are more like
6233  * optimizing code generation.
6234  *   One interesting optimization is when several store macros appear
6235  * consecutively that would load AT with the upper half of the same address.
6236  * The ensuing load upper instructions are ommited. This implies some kind
6237  * of global optimization. We currently only optimize within a single macro.
6238  *   For many of the load and store macros if the address is specified as a
6239  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6240  * first load register 'at' with zero and use it as the base register. The
6241  * mips assembler simply uses register $zero. Just one tiny optimization
6242  * we're missing.
6243  */
6244 static void
6245 macro (struct mips_cl_insn *ip)
6246 {
6247   unsigned int treg, sreg, dreg, breg;
6248   unsigned int tempreg;
6249   int mask;
6250   int used_at = 0;
6251   expressionS label_expr;
6252   expressionS expr1;
6253   expressionS *ep;
6254   const char *s;
6255   const char *s2;
6256   const char *fmt;
6257   int likely = 0;
6258   int coproc = 0;
6259   int off12 = 0;
6260   int call = 0;
6261   int jals = 0;
6262   int dbl = 0;
6263   int imm = 0;
6264   int ust = 0;
6265   int lp = 0;
6266   int ab = 0;
6267   int off0 = 0;
6268   int off;
6269   offsetT maxnum;
6270   bfd_reloc_code_real_type r;
6271   int hold_mips_optimize;
6272
6273   gas_assert (! mips_opts.mips16);
6274
6275   treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6276   dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6277   sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
6278   mask = ip->insn_mo->mask;
6279
6280   label_expr.X_op = O_constant;
6281   label_expr.X_op_symbol = NULL;
6282   label_expr.X_add_symbol = NULL;
6283   label_expr.X_add_number = 0;
6284
6285   expr1.X_op = O_constant;
6286   expr1.X_op_symbol = NULL;
6287   expr1.X_add_symbol = NULL;
6288   expr1.X_add_number = 1;
6289
6290   switch (mask)
6291     {
6292     case M_DABS:
6293       dbl = 1;
6294     case M_ABS:
6295       /*    bgez    $a0,1f
6296             move    v0,$a0
6297             sub     v0,$zero,$a0
6298          1:
6299        */
6300
6301       start_noreorder ();
6302
6303       if (mips_opts.micromips)
6304         micromips_label_expr (&label_expr);
6305       else
6306         label_expr.X_add_number = 8;
6307       macro_build (&label_expr, "bgez", "s,p", sreg);
6308       if (dreg == sreg)
6309         macro_build (NULL, "nop", "");
6310       else
6311         move_register (dreg, sreg);
6312       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
6313       if (mips_opts.micromips)
6314         micromips_add_label ();
6315
6316       end_noreorder ();
6317       break;
6318
6319     case M_ADD_I:
6320       s = "addi";
6321       s2 = "add";
6322       goto do_addi;
6323     case M_ADDU_I:
6324       s = "addiu";
6325       s2 = "addu";
6326       goto do_addi;
6327     case M_DADD_I:
6328       dbl = 1;
6329       s = "daddi";
6330       s2 = "dadd";
6331       if (!mips_opts.micromips)
6332         goto do_addi;
6333       if (imm_expr.X_op == O_constant
6334           && imm_expr.X_add_number >= -0x200
6335           && imm_expr.X_add_number < 0x200)
6336         {
6337           macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6338           break;
6339         }
6340       goto do_addi_i;
6341     case M_DADDU_I:
6342       dbl = 1;
6343       s = "daddiu";
6344       s2 = "daddu";
6345     do_addi:
6346       if (imm_expr.X_op == O_constant
6347           && imm_expr.X_add_number >= -0x8000
6348           && imm_expr.X_add_number < 0x8000)
6349         {
6350           macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
6351           break;
6352         }
6353     do_addi_i:
6354       used_at = 1;
6355       load_register (AT, &imm_expr, dbl);
6356       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6357       break;
6358
6359     case M_AND_I:
6360       s = "andi";
6361       s2 = "and";
6362       goto do_bit;
6363     case M_OR_I:
6364       s = "ori";
6365       s2 = "or";
6366       goto do_bit;
6367     case M_NOR_I:
6368       s = "";
6369       s2 = "nor";
6370       goto do_bit;
6371     case M_XOR_I:
6372       s = "xori";
6373       s2 = "xor";
6374     do_bit:
6375       if (imm_expr.X_op == O_constant
6376           && imm_expr.X_add_number >= 0
6377           && imm_expr.X_add_number < 0x10000)
6378         {
6379           if (mask != M_NOR_I)
6380             macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
6381           else
6382             {
6383               macro_build (&imm_expr, "ori", "t,r,i",
6384                            treg, sreg, BFD_RELOC_LO16);
6385               macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
6386             }
6387           break;
6388         }
6389
6390       used_at = 1;
6391       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6392       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
6393       break;
6394
6395     case M_BALIGN:
6396       switch (imm_expr.X_add_number)
6397         {
6398         case 0:
6399           macro_build (NULL, "nop", "");
6400           break;
6401         case 2:
6402           macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6403           break;
6404         default:
6405           macro_build (NULL, "balign", "t,s,2", treg, sreg,
6406                        (int) imm_expr.X_add_number);
6407           break;
6408         }
6409       break;
6410
6411     case M_BC1FL:
6412     case M_BC1TL:
6413     case M_BC2FL:
6414     case M_BC2TL:
6415       gas_assert (mips_opts.micromips);
6416       macro_build_branch_ccl (mask, &offset_expr,
6417                               EXTRACT_OPERAND (1, BCC, *ip));
6418       break;
6419
6420     case M_BEQ_I:
6421     case M_BEQL_I:
6422     case M_BNE_I:
6423     case M_BNEL_I:
6424       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6425         treg = 0;
6426       else
6427         {
6428           treg = AT;
6429           used_at = 1;
6430           load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
6431         }
6432       /* Fall through.  */
6433     case M_BEQL:
6434     case M_BNEL:
6435       macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
6436       break;
6437
6438     case M_BGEL:
6439       likely = 1;
6440     case M_BGE:
6441       if (treg == 0)
6442         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6443       else if (sreg == 0)
6444         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6445       else
6446         {
6447           used_at = 1;
6448           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6449           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6450                                    &offset_expr, AT, ZERO);
6451         }
6452       break;
6453
6454     case M_BGEZL:
6455     case M_BGEZALL:
6456     case M_BGTZL:
6457     case M_BLEZL:
6458     case M_BLTZL:
6459     case M_BLTZALL:
6460       macro_build_branch_rs (mask, &offset_expr, sreg);
6461       break;
6462
6463     case M_BGTL_I:
6464       likely = 1;
6465     case M_BGT_I:
6466       /* Check for > max integer.  */
6467       maxnum = 0x7fffffff;
6468       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6469         {
6470           maxnum <<= 16;
6471           maxnum |= 0xffff;
6472           maxnum <<= 16;
6473           maxnum |= 0xffff;
6474         }
6475       if (imm_expr.X_op == O_constant
6476           && imm_expr.X_add_number >= maxnum
6477           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6478         {
6479         do_false:
6480           /* Result is always false.  */
6481           if (! likely)
6482             macro_build (NULL, "nop", "");
6483           else
6484             macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
6485           break;
6486         }
6487       if (imm_expr.X_op != O_constant)
6488         as_bad (_("Unsupported large constant"));
6489       ++imm_expr.X_add_number;
6490       /* FALLTHROUGH */
6491     case M_BGE_I:
6492     case M_BGEL_I:
6493       if (mask == M_BGEL_I)
6494         likely = 1;
6495       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6496         {
6497           macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
6498                                  &offset_expr, sreg);
6499           break;
6500         }
6501       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6502         {
6503           macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
6504                                  &offset_expr, sreg);
6505           break;
6506         }
6507       maxnum = 0x7fffffff;
6508       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6509         {
6510           maxnum <<= 16;
6511           maxnum |= 0xffff;
6512           maxnum <<= 16;
6513           maxnum |= 0xffff;
6514         }
6515       maxnum = - maxnum - 1;
6516       if (imm_expr.X_op == O_constant
6517           && imm_expr.X_add_number <= maxnum
6518           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6519         {
6520         do_true:
6521           /* result is always true */
6522           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
6523           macro_build (&offset_expr, "b", "p");
6524           break;
6525         }
6526       used_at = 1;
6527       set_at (sreg, 0);
6528       macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6529                                &offset_expr, AT, ZERO);
6530       break;
6531
6532     case M_BGEUL:
6533       likely = 1;
6534     case M_BGEU:
6535       if (treg == 0)
6536         goto do_true;
6537       else if (sreg == 0)
6538         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6539                                  &offset_expr, ZERO, treg);
6540       else
6541         {
6542           used_at = 1;
6543           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6544           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6545                                    &offset_expr, AT, ZERO);
6546         }
6547       break;
6548
6549     case M_BGTUL_I:
6550       likely = 1;
6551     case M_BGTU_I:
6552       if (sreg == 0
6553           || (HAVE_32BIT_GPRS
6554               && imm_expr.X_op == O_constant
6555               && imm_expr.X_add_number == -1))
6556         goto do_false;
6557       if (imm_expr.X_op != O_constant)
6558         as_bad (_("Unsupported large constant"));
6559       ++imm_expr.X_add_number;
6560       /* FALLTHROUGH */
6561     case M_BGEU_I:
6562     case M_BGEUL_I:
6563       if (mask == M_BGEUL_I)
6564         likely = 1;
6565       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6566         goto do_true;
6567       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6568         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6569                                  &offset_expr, sreg, ZERO);
6570       else
6571         {
6572           used_at = 1;
6573           set_at (sreg, 1);
6574           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6575                                    &offset_expr, AT, ZERO);
6576         }
6577       break;
6578
6579     case M_BGTL:
6580       likely = 1;
6581     case M_BGT:
6582       if (treg == 0)
6583         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
6584       else if (sreg == 0)
6585         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
6586       else
6587         {
6588           used_at = 1;
6589           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6590           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6591                                    &offset_expr, AT, ZERO);
6592         }
6593       break;
6594
6595     case M_BGTUL:
6596       likely = 1;
6597     case M_BGTU:
6598       if (treg == 0)
6599         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6600                                  &offset_expr, sreg, ZERO);
6601       else if (sreg == 0)
6602         goto do_false;
6603       else
6604         {
6605           used_at = 1;
6606           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6607           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6608                                    &offset_expr, AT, ZERO);
6609         }
6610       break;
6611
6612     case M_BLEL:
6613       likely = 1;
6614     case M_BLE:
6615       if (treg == 0)
6616         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6617       else if (sreg == 0)
6618         macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
6619       else
6620         {
6621           used_at = 1;
6622           macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
6623           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6624                                    &offset_expr, AT, ZERO);
6625         }
6626       break;
6627
6628     case M_BLEL_I:
6629       likely = 1;
6630     case M_BLE_I:
6631       maxnum = 0x7fffffff;
6632       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
6633         {
6634           maxnum <<= 16;
6635           maxnum |= 0xffff;
6636           maxnum <<= 16;
6637           maxnum |= 0xffff;
6638         }
6639       if (imm_expr.X_op == O_constant
6640           && imm_expr.X_add_number >= maxnum
6641           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
6642         goto do_true;
6643       if (imm_expr.X_op != O_constant)
6644         as_bad (_("Unsupported large constant"));
6645       ++imm_expr.X_add_number;
6646       /* FALLTHROUGH */
6647     case M_BLT_I:
6648     case M_BLTL_I:
6649       if (mask == M_BLTL_I)
6650         likely = 1;
6651       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6652         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6653       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6654         macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
6655       else
6656         {
6657           used_at = 1;
6658           set_at (sreg, 0);
6659           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6660                                    &offset_expr, AT, ZERO);
6661         }
6662       break;
6663
6664     case M_BLEUL:
6665       likely = 1;
6666     case M_BLEU:
6667       if (treg == 0)
6668         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6669                                  &offset_expr, sreg, ZERO);
6670       else if (sreg == 0)
6671         goto do_true;
6672       else
6673         {
6674           used_at = 1;
6675           macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
6676           macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6677                                    &offset_expr, AT, ZERO);
6678         }
6679       break;
6680
6681     case M_BLEUL_I:
6682       likely = 1;
6683     case M_BLEU_I:
6684       if (sreg == 0
6685           || (HAVE_32BIT_GPRS
6686               && imm_expr.X_op == O_constant
6687               && imm_expr.X_add_number == -1))
6688         goto do_true;
6689       if (imm_expr.X_op != O_constant)
6690         as_bad (_("Unsupported large constant"));
6691       ++imm_expr.X_add_number;
6692       /* FALLTHROUGH */
6693     case M_BLTU_I:
6694     case M_BLTUL_I:
6695       if (mask == M_BLTUL_I)
6696         likely = 1;
6697       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6698         goto do_false;
6699       else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6700         macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6701                                  &offset_expr, sreg, ZERO);
6702       else
6703         {
6704           used_at = 1;
6705           set_at (sreg, 1);
6706           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6707                                    &offset_expr, AT, ZERO);
6708         }
6709       break;
6710
6711     case M_BLTL:
6712       likely = 1;
6713     case M_BLT:
6714       if (treg == 0)
6715         macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
6716       else if (sreg == 0)
6717         macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
6718       else
6719         {
6720           used_at = 1;
6721           macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6722           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6723                                    &offset_expr, AT, ZERO);
6724         }
6725       break;
6726
6727     case M_BLTUL:
6728       likely = 1;
6729     case M_BLTU:
6730       if (treg == 0)
6731         goto do_false;
6732       else if (sreg == 0)
6733         macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6734                                  &offset_expr, ZERO, treg);
6735       else
6736         {
6737           used_at = 1;
6738           macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
6739           macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
6740                                    &offset_expr, AT, ZERO);
6741         }
6742       break;
6743
6744     case M_DEXT:
6745       {
6746         /* Use unsigned arithmetic.  */
6747         addressT pos;
6748         addressT size;
6749
6750         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6751           {
6752             as_bad (_("Unsupported large constant"));
6753             pos = size = 1;
6754           }
6755         else
6756           {
6757             pos = imm_expr.X_add_number;
6758             size = imm2_expr.X_add_number;
6759           }
6760
6761         if (pos > 63)
6762           {
6763             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6764             pos = 1;
6765           }
6766         if (size == 0 || size > 64 || (pos + size - 1) > 63)
6767           {
6768             as_bad (_("Improper extract size (%lu, position %lu)"),
6769                     (unsigned long) size, (unsigned long) pos);
6770             size = 1;
6771           }
6772
6773         if (size <= 32 && pos < 32)
6774           {
6775             s = "dext";
6776             fmt = "t,r,+A,+C";
6777           }
6778         else if (size <= 32)
6779           {
6780             s = "dextu";
6781             fmt = "t,r,+E,+H";
6782           }
6783         else
6784           {
6785             s = "dextm";
6786             fmt = "t,r,+A,+G";
6787           }
6788         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6789                      (int) (size - 1));
6790       }
6791       break;
6792
6793     case M_DINS:
6794       {
6795         /* Use unsigned arithmetic.  */
6796         addressT pos;
6797         addressT size;
6798
6799         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
6800           {
6801             as_bad (_("Unsupported large constant"));
6802             pos = size = 1;
6803           }
6804         else
6805           {
6806             pos = imm_expr.X_add_number;
6807             size = imm2_expr.X_add_number;
6808           }
6809
6810         if (pos > 63)
6811           {
6812             as_bad (_("Improper position (%lu)"), (unsigned long) pos);
6813             pos = 1;
6814           }
6815         if (size == 0 || size > 64 || (pos + size - 1) > 63)
6816           {
6817             as_bad (_("Improper insert size (%lu, position %lu)"),
6818                     (unsigned long) size, (unsigned long) pos);
6819             size = 1;
6820           }
6821
6822         if (pos < 32 && (pos + size - 1) < 32)
6823           {
6824             s = "dins";
6825             fmt = "t,r,+A,+B";
6826           }
6827         else if (pos >= 32)
6828           {
6829             s = "dinsu";
6830             fmt = "t,r,+E,+F";
6831           }
6832         else
6833           {
6834             s = "dinsm";
6835             fmt = "t,r,+A,+F";
6836           }
6837         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
6838                      (int) (pos + size - 1));
6839       }
6840       break;
6841
6842     case M_DDIV_3:
6843       dbl = 1;
6844     case M_DIV_3:
6845       s = "mflo";
6846       goto do_div3;
6847     case M_DREM_3:
6848       dbl = 1;
6849     case M_REM_3:
6850       s = "mfhi";
6851     do_div3:
6852       if (treg == 0)
6853         {
6854           as_warn (_("Divide by zero."));
6855           if (mips_trap)
6856             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
6857           else
6858             macro_build (NULL, "break", BRK_FMT, 7);
6859           break;
6860         }
6861
6862       start_noreorder ();
6863       if (mips_trap)
6864         {
6865           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
6866           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
6867         }
6868       else
6869         {
6870           if (mips_opts.micromips)
6871             micromips_label_expr (&label_expr);
6872           else
6873             label_expr.X_add_number = 8;
6874           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
6875           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
6876           macro_build (NULL, "break", BRK_FMT, 7);
6877           if (mips_opts.micromips)
6878             micromips_add_label ();
6879         }
6880       expr1.X_add_number = -1;
6881       used_at = 1;
6882       load_register (AT, &expr1, dbl);
6883       if (mips_opts.micromips)
6884         micromips_label_expr (&label_expr);
6885       else
6886         label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
6887       macro_build (&label_expr, "bne", "s,t,p", treg, AT);
6888       if (dbl)
6889         {
6890           expr1.X_add_number = 1;
6891           load_register (AT, &expr1, dbl);
6892           macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
6893         }
6894       else
6895         {
6896           expr1.X_add_number = 0x80000000;
6897           macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
6898         }
6899       if (mips_trap)
6900         {
6901           macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
6902           /* We want to close the noreorder block as soon as possible, so
6903              that later insns are available for delay slot filling.  */
6904           end_noreorder ();
6905         }
6906       else
6907         {
6908           if (mips_opts.micromips)
6909             micromips_label_expr (&label_expr);
6910           else
6911             label_expr.X_add_number = 8;
6912           macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
6913           macro_build (NULL, "nop", "");
6914
6915           /* We want to close the noreorder block as soon as possible, so
6916              that later insns are available for delay slot filling.  */
6917           end_noreorder ();
6918
6919           macro_build (NULL, "break", BRK_FMT, 6);
6920         }
6921       if (mips_opts.micromips)
6922         micromips_add_label ();
6923       macro_build (NULL, s, MFHL_FMT, dreg);
6924       break;
6925
6926     case M_DIV_3I:
6927       s = "div";
6928       s2 = "mflo";
6929       goto do_divi;
6930     case M_DIVU_3I:
6931       s = "divu";
6932       s2 = "mflo";
6933       goto do_divi;
6934     case M_REM_3I:
6935       s = "div";
6936       s2 = "mfhi";
6937       goto do_divi;
6938     case M_REMU_3I:
6939       s = "divu";
6940       s2 = "mfhi";
6941       goto do_divi;
6942     case M_DDIV_3I:
6943       dbl = 1;
6944       s = "ddiv";
6945       s2 = "mflo";
6946       goto do_divi;
6947     case M_DDIVU_3I:
6948       dbl = 1;
6949       s = "ddivu";
6950       s2 = "mflo";
6951       goto do_divi;
6952     case M_DREM_3I:
6953       dbl = 1;
6954       s = "ddiv";
6955       s2 = "mfhi";
6956       goto do_divi;
6957     case M_DREMU_3I:
6958       dbl = 1;
6959       s = "ddivu";
6960       s2 = "mfhi";
6961     do_divi:
6962       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6963         {
6964           as_warn (_("Divide by zero."));
6965           if (mips_trap)
6966             macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
6967           else
6968             macro_build (NULL, "break", BRK_FMT, 7);
6969           break;
6970         }
6971       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
6972         {
6973           if (strcmp (s2, "mflo") == 0)
6974             move_register (dreg, sreg);
6975           else
6976             move_register (dreg, ZERO);
6977           break;
6978         }
6979       if (imm_expr.X_op == O_constant
6980           && imm_expr.X_add_number == -1
6981           && s[strlen (s) - 1] != 'u')
6982         {
6983           if (strcmp (s2, "mflo") == 0)
6984             {
6985               macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
6986             }
6987           else
6988             move_register (dreg, ZERO);
6989           break;
6990         }
6991
6992       used_at = 1;
6993       load_register (AT, &imm_expr, dbl);
6994       macro_build (NULL, s, "z,s,t", sreg, AT);
6995       macro_build (NULL, s2, MFHL_FMT, dreg);
6996       break;
6997
6998     case M_DIVU_3:
6999       s = "divu";
7000       s2 = "mflo";
7001       goto do_divu3;
7002     case M_REMU_3:
7003       s = "divu";
7004       s2 = "mfhi";
7005       goto do_divu3;
7006     case M_DDIVU_3:
7007       s = "ddivu";
7008       s2 = "mflo";
7009       goto do_divu3;
7010     case M_DREMU_3:
7011       s = "ddivu";
7012       s2 = "mfhi";
7013     do_divu3:
7014       start_noreorder ();
7015       if (mips_trap)
7016         {
7017           macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
7018           macro_build (NULL, s, "z,s,t", sreg, treg);
7019           /* We want to close the noreorder block as soon as possible, so
7020              that later insns are available for delay slot filling.  */
7021           end_noreorder ();
7022         }
7023       else
7024         {
7025           if (mips_opts.micromips)
7026             micromips_label_expr (&label_expr);
7027           else
7028             label_expr.X_add_number = 8;
7029           macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
7030           macro_build (NULL, s, "z,s,t", sreg, treg);
7031
7032           /* We want to close the noreorder block as soon as possible, so
7033              that later insns are available for delay slot filling.  */
7034           end_noreorder ();
7035           macro_build (NULL, "break", BRK_FMT, 7);
7036           if (mips_opts.micromips)
7037             micromips_add_label ();
7038         }
7039       macro_build (NULL, s2, MFHL_FMT, dreg);
7040       break;
7041
7042     case M_DLCA_AB:
7043       dbl = 1;
7044     case M_LCA_AB:
7045       call = 1;
7046       goto do_la;
7047     case M_DLA_AB:
7048       dbl = 1;
7049     case M_LA_AB:
7050     do_la:
7051       /* Load the address of a symbol into a register.  If breg is not
7052          zero, we then add a base register to it.  */
7053
7054       if (dbl && HAVE_32BIT_GPRS)
7055         as_warn (_("dla used to load 32-bit register"));
7056
7057       if (!dbl && HAVE_64BIT_OBJECTS)
7058         as_warn (_("la used to load 64-bit address"));
7059
7060       if (offset_expr.X_op == O_constant
7061           && offset_expr.X_add_number >= -0x8000
7062           && offset_expr.X_add_number < 0x8000)
7063         {
7064           macro_build (&offset_expr, ADDRESS_ADDI_INSN,
7065                        "t,r,j", treg, sreg, BFD_RELOC_LO16);
7066           break;
7067         }
7068
7069       if (mips_opts.at && (treg == breg))
7070         {
7071           tempreg = AT;
7072           used_at = 1;
7073         }
7074       else
7075         {
7076           tempreg = treg;
7077         }
7078
7079       if (offset_expr.X_op != O_symbol
7080           && offset_expr.X_op != O_constant)
7081         {
7082           as_bad (_("Expression too complex"));
7083           offset_expr.X_op = O_constant;
7084         }
7085
7086       if (offset_expr.X_op == O_constant)
7087         load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
7088       else if (mips_pic == NO_PIC)
7089         {
7090           /* If this is a reference to a GP relative symbol, we want
7091                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
7092              Otherwise we want
7093                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
7094                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7095              If we have a constant, we need two instructions anyhow,
7096              so we may as well always use the latter form.
7097
7098              With 64bit address space and a usable $at we want
7099                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7100                lui      $at,<sym>               (BFD_RELOC_HI16_S)
7101                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7102                daddiu   $at,<sym>               (BFD_RELOC_LO16)
7103                dsll32   $tempreg,0
7104                daddu    $tempreg,$tempreg,$at
7105
7106              If $at is already in use, we use a path which is suboptimal
7107              on superscalar processors.
7108                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
7109                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
7110                dsll     $tempreg,16
7111                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
7112                dsll     $tempreg,16
7113                daddiu   $tempreg,<sym>          (BFD_RELOC_LO16)
7114
7115              For GP relative symbols in 64bit address space we can use
7116              the same sequence as in 32bit address space.  */
7117           if (HAVE_64BIT_SYMBOLS)
7118             {
7119               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7120                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7121                 {
7122                   relax_start (offset_expr.X_add_symbol);
7123                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7124                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7125                   relax_switch ();
7126                 }
7127
7128               if (used_at == 0 && mips_opts.at)
7129                 {
7130                   macro_build (&offset_expr, "lui", LUI_FMT,
7131                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7132                   macro_build (&offset_expr, "lui", LUI_FMT,
7133                                AT, BFD_RELOC_HI16_S);
7134                   macro_build (&offset_expr, "daddiu", "t,r,j",
7135                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7136                   macro_build (&offset_expr, "daddiu", "t,r,j",
7137                                AT, AT, BFD_RELOC_LO16);
7138                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
7139                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
7140                   used_at = 1;
7141                 }
7142               else
7143                 {
7144                   macro_build (&offset_expr, "lui", LUI_FMT,
7145                                tempreg, BFD_RELOC_MIPS_HIGHEST);
7146                   macro_build (&offset_expr, "daddiu", "t,r,j",
7147                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
7148                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7149                   macro_build (&offset_expr, "daddiu", "t,r,j",
7150                                tempreg, tempreg, BFD_RELOC_HI16_S);
7151                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
7152                   macro_build (&offset_expr, "daddiu", "t,r,j",
7153                                tempreg, tempreg, BFD_RELOC_LO16);
7154                 }
7155
7156               if (mips_relax.sequence)
7157                 relax_end ();
7158             }
7159           else
7160             {
7161               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7162                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7163                 {
7164                   relax_start (offset_expr.X_add_symbol);
7165                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7166                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7167                   relax_switch ();
7168                 }
7169               if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
7170                 as_bad (_("Offset too large"));
7171               macro_build_lui (&offset_expr, tempreg);
7172               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7173                            tempreg, tempreg, BFD_RELOC_LO16);
7174               if (mips_relax.sequence)
7175                 relax_end ();
7176             }
7177         }
7178       else if (!mips_big_got && !HAVE_NEWABI)
7179         {
7180           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7181
7182           /* If this is a reference to an external symbol, and there
7183              is no constant, we want
7184                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7185              or for lca or if tempreg is PIC_CALL_REG
7186                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7187              For a local symbol, we want
7188                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7189                nop
7190                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7191
7192              If we have a small constant, and this is a reference to
7193              an external symbol, we want
7194                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7195                nop
7196                addiu    $tempreg,$tempreg,<constant>
7197              For a local symbol, we want the same instruction
7198              sequence, but we output a BFD_RELOC_LO16 reloc on the
7199              addiu instruction.
7200
7201              If we have a large constant, and this is a reference to
7202              an external symbol, we want
7203                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7204                lui      $at,<hiconstant>
7205                addiu    $at,$at,<loconstant>
7206                addu     $tempreg,$tempreg,$at
7207              For a local symbol, we want the same instruction
7208              sequence, but we output a BFD_RELOC_LO16 reloc on the
7209              addiu instruction.
7210            */
7211
7212           if (offset_expr.X_add_number == 0)
7213             {
7214               if (mips_pic == SVR4_PIC
7215                   && breg == 0
7216                   && (call || tempreg == PIC_CALL_REG))
7217                 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7218
7219               relax_start (offset_expr.X_add_symbol);
7220               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7221                            lw_reloc_type, mips_gp_register);
7222               if (breg != 0)
7223                 {
7224                   /* We're going to put in an addu instruction using
7225                      tempreg, so we may as well insert the nop right
7226                      now.  */
7227                   load_delay_nop ();
7228                 }
7229               relax_switch ();
7230               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7231                            tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
7232               load_delay_nop ();
7233               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7234                            tempreg, tempreg, BFD_RELOC_LO16);
7235               relax_end ();
7236               /* FIXME: If breg == 0, and the next instruction uses
7237                  $tempreg, then if this variant case is used an extra
7238                  nop will be generated.  */
7239             }
7240           else if (offset_expr.X_add_number >= -0x8000
7241                    && offset_expr.X_add_number < 0x8000)
7242             {
7243               load_got_offset (tempreg, &offset_expr);
7244               load_delay_nop ();
7245               add_got_offset (tempreg, &offset_expr);
7246             }
7247           else
7248             {
7249               expr1.X_add_number = offset_expr.X_add_number;
7250               offset_expr.X_add_number =
7251                 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
7252               load_got_offset (tempreg, &offset_expr);
7253               offset_expr.X_add_number = expr1.X_add_number;
7254               /* If we are going to add in a base register, and the
7255                  target register and the base register are the same,
7256                  then we are using AT as a temporary register.  Since
7257                  we want to load the constant into AT, we add our
7258                  current AT (from the global offset table) and the
7259                  register into the register now, and pretend we were
7260                  not using a base register.  */
7261               if (breg == treg)
7262                 {
7263                   load_delay_nop ();
7264                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7265                                treg, AT, breg);
7266                   breg = 0;
7267                   tempreg = treg;
7268                 }
7269               add_got_offset_hilo (tempreg, &offset_expr, AT);
7270               used_at = 1;
7271             }
7272         }
7273       else if (!mips_big_got && HAVE_NEWABI)
7274         {
7275           int add_breg_early = 0;
7276
7277           /* If this is a reference to an external, and there is no
7278              constant, or local symbol (*), with or without a
7279              constant, we want
7280                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7281              or for lca or if tempreg is PIC_CALL_REG
7282                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
7283
7284              If we have a small constant, and this is a reference to
7285              an external symbol, we want
7286                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7287                addiu    $tempreg,$tempreg,<constant>
7288
7289              If we have a large constant, and this is a reference to
7290              an external symbol, we want
7291                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
7292                lui      $at,<hiconstant>
7293                addiu    $at,$at,<loconstant>
7294                addu     $tempreg,$tempreg,$at
7295
7296              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7297              local symbols, even though it introduces an additional
7298              instruction.  */
7299
7300           if (offset_expr.X_add_number)
7301             {
7302               expr1.X_add_number = offset_expr.X_add_number;
7303               offset_expr.X_add_number = 0;
7304
7305               relax_start (offset_expr.X_add_symbol);
7306               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7307                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7308
7309               if (expr1.X_add_number >= -0x8000
7310                   && expr1.X_add_number < 0x8000)
7311                 {
7312                   macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7313                                tempreg, tempreg, BFD_RELOC_LO16);
7314                 }
7315               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7316                 {
7317                   /* If we are going to add in a base register, and the
7318                      target register and the base register are the same,
7319                      then we are using AT as a temporary register.  Since
7320                      we want to load the constant into AT, we add our
7321                      current AT (from the global offset table) and the
7322                      register into the register now, and pretend we were
7323                      not using a base register.  */
7324                   if (breg != treg)
7325                     dreg = tempreg;
7326                   else
7327                     {
7328                       gas_assert (tempreg == AT);
7329                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7330                                    treg, AT, breg);
7331                       dreg = treg;
7332                       add_breg_early = 1;
7333                     }
7334
7335                   load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7336                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7337                                dreg, dreg, AT);
7338
7339                   used_at = 1;
7340                 }
7341               else
7342                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7343
7344               relax_switch ();
7345               offset_expr.X_add_number = expr1.X_add_number;
7346
7347               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7348                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7349               if (add_breg_early)
7350                 {
7351                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7352                                treg, tempreg, breg);
7353                   breg = 0;
7354                   tempreg = treg;
7355                 }
7356               relax_end ();
7357             }
7358           else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
7359             {
7360               relax_start (offset_expr.X_add_symbol);
7361               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7362                            BFD_RELOC_MIPS_CALL16, mips_gp_register);
7363               relax_switch ();
7364               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7365                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7366               relax_end ();
7367             }
7368           else
7369             {
7370               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7371                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7372             }
7373         }
7374       else if (mips_big_got && !HAVE_NEWABI)
7375         {
7376           int gpdelay;
7377           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7378           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7379           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7380
7381           /* This is the large GOT case.  If this is a reference to an
7382              external symbol, and there is no constant, we want
7383                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7384                addu     $tempreg,$tempreg,$gp
7385                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7386              or for lca or if tempreg is PIC_CALL_REG
7387                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
7388                addu     $tempreg,$tempreg,$gp
7389                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7390              For a local symbol, we want
7391                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7392                nop
7393                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7394
7395              If we have a small constant, and this is a reference to
7396              an external symbol, we want
7397                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7398                addu     $tempreg,$tempreg,$gp
7399                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7400                nop
7401                addiu    $tempreg,$tempreg,<constant>
7402              For a local symbol, we want
7403                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7404                nop
7405                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7406
7407              If we have a large constant, and this is a reference to
7408              an external symbol, we want
7409                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7410                addu     $tempreg,$tempreg,$gp
7411                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7412                lui      $at,<hiconstant>
7413                addiu    $at,$at,<loconstant>
7414                addu     $tempreg,$tempreg,$at
7415              For a local symbol, we want
7416                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
7417                lui      $at,<hiconstant>
7418                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
7419                addu     $tempreg,$tempreg,$at
7420           */
7421
7422           expr1.X_add_number = offset_expr.X_add_number;
7423           offset_expr.X_add_number = 0;
7424           relax_start (offset_expr.X_add_symbol);
7425           gpdelay = reg_needs_delay (mips_gp_register);
7426           if (expr1.X_add_number == 0 && breg == 0
7427               && (call || tempreg == PIC_CALL_REG))
7428             {
7429               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7430               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7431             }
7432           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7433           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7434                        tempreg, tempreg, mips_gp_register);
7435           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7436                        tempreg, lw_reloc_type, tempreg);
7437           if (expr1.X_add_number == 0)
7438             {
7439               if (breg != 0)
7440                 {
7441                   /* We're going to put in an addu instruction using
7442                      tempreg, so we may as well insert the nop right
7443                      now.  */
7444                   load_delay_nop ();
7445                 }
7446             }
7447           else if (expr1.X_add_number >= -0x8000
7448                    && expr1.X_add_number < 0x8000)
7449             {
7450               load_delay_nop ();
7451               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7452                            tempreg, tempreg, BFD_RELOC_LO16);
7453             }
7454           else
7455             {
7456               /* If we are going to add in a base register, and the
7457                  target register and the base register are the same,
7458                  then we are using AT as a temporary register.  Since
7459                  we want to load the constant into AT, we add our
7460                  current AT (from the global offset table) and the
7461                  register into the register now, and pretend we were
7462                  not using a base register.  */
7463               if (breg != treg)
7464                 dreg = tempreg;
7465               else
7466                 {
7467                   gas_assert (tempreg == AT);
7468                   load_delay_nop ();
7469                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7470                                treg, AT, breg);
7471                   dreg = treg;
7472                 }
7473
7474               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7475               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7476
7477               used_at = 1;
7478             }
7479           offset_expr.X_add_number =
7480             ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
7481           relax_switch ();
7482
7483           if (gpdelay)
7484             {
7485               /* This is needed because this instruction uses $gp, but
7486                  the first instruction on the main stream does not.  */
7487               macro_build (NULL, "nop", "");
7488             }
7489
7490           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7491                        local_reloc_type, mips_gp_register);
7492           if (expr1.X_add_number >= -0x8000
7493               && expr1.X_add_number < 0x8000)
7494             {
7495               load_delay_nop ();
7496               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7497                            tempreg, tempreg, BFD_RELOC_LO16);
7498               /* FIXME: If add_number is 0, and there was no base
7499                  register, the external symbol case ended with a load,
7500                  so if the symbol turns out to not be external, and
7501                  the next instruction uses tempreg, an unnecessary nop
7502                  will be inserted.  */
7503             }
7504           else
7505             {
7506               if (breg == treg)
7507                 {
7508                   /* We must add in the base register now, as in the
7509                      external symbol case.  */
7510                   gas_assert (tempreg == AT);
7511                   load_delay_nop ();
7512                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7513                                treg, AT, breg);
7514                   tempreg = treg;
7515                   /* We set breg to 0 because we have arranged to add
7516                      it in in both cases.  */
7517                   breg = 0;
7518                 }
7519
7520               macro_build_lui (&expr1, AT);
7521               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7522                            AT, AT, BFD_RELOC_LO16);
7523               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7524                            tempreg, tempreg, AT);
7525               used_at = 1;
7526             }
7527           relax_end ();
7528         }
7529       else if (mips_big_got && HAVE_NEWABI)
7530         {
7531           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7532           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
7533           int add_breg_early = 0;
7534
7535           /* This is the large GOT case.  If this is a reference to an
7536              external symbol, and there is no constant, we want
7537                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7538                add      $tempreg,$tempreg,$gp
7539                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7540              or for lca or if tempreg is PIC_CALL_REG
7541                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
7542                add      $tempreg,$tempreg,$gp
7543                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
7544
7545              If we have a small constant, and this is a reference to
7546              an external symbol, we want
7547                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7548                add      $tempreg,$tempreg,$gp
7549                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7550                addi     $tempreg,$tempreg,<constant>
7551
7552              If we have a large constant, and this is a reference to
7553              an external symbol, we want
7554                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
7555                addu     $tempreg,$tempreg,$gp
7556                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7557                lui      $at,<hiconstant>
7558                addi     $at,$at,<loconstant>
7559                add      $tempreg,$tempreg,$at
7560
7561              If we have NewABI, and we know it's a local symbol, we want
7562                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
7563                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
7564              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
7565
7566           relax_start (offset_expr.X_add_symbol);
7567
7568           expr1.X_add_number = offset_expr.X_add_number;
7569           offset_expr.X_add_number = 0;
7570
7571           if (expr1.X_add_number == 0 && breg == 0
7572               && (call || tempreg == PIC_CALL_REG))
7573             {
7574               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7575               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7576             }
7577           macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
7578           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7579                        tempreg, tempreg, mips_gp_register);
7580           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7581                        tempreg, lw_reloc_type, tempreg);
7582
7583           if (expr1.X_add_number == 0)
7584             ;
7585           else if (expr1.X_add_number >= -0x8000
7586                    && expr1.X_add_number < 0x8000)
7587             {
7588               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7589                            tempreg, tempreg, BFD_RELOC_LO16);
7590             }
7591           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
7592             {
7593               /* If we are going to add in a base register, and the
7594                  target register and the base register are the same,
7595                  then we are using AT as a temporary register.  Since
7596                  we want to load the constant into AT, we add our
7597                  current AT (from the global offset table) and the
7598                  register into the register now, and pretend we were
7599                  not using a base register.  */
7600               if (breg != treg)
7601                 dreg = tempreg;
7602               else
7603                 {
7604                   gas_assert (tempreg == AT);
7605                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7606                                treg, AT, breg);
7607                   dreg = treg;
7608                   add_breg_early = 1;
7609                 }
7610
7611               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
7612               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
7613
7614               used_at = 1;
7615             }
7616           else
7617             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7618
7619           relax_switch ();
7620           offset_expr.X_add_number = expr1.X_add_number;
7621           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7622                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
7623           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
7624                        tempreg, BFD_RELOC_MIPS_GOT_OFST);
7625           if (add_breg_early)
7626             {
7627               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7628                            treg, tempreg, breg);
7629               breg = 0;
7630               tempreg = treg;
7631             }
7632           relax_end ();
7633         }
7634       else
7635         abort ();
7636
7637       if (breg != 0)
7638         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
7639       break;
7640
7641     case M_MSGSND:
7642       gas_assert (!mips_opts.micromips);
7643       {
7644         unsigned long temp = (treg << 16) | (0x01);
7645         macro_build (NULL, "c2", "C", temp);
7646       }
7647       break;
7648
7649     case M_MSGLD:
7650       gas_assert (!mips_opts.micromips);
7651       {
7652         unsigned long temp = (0x02);
7653         macro_build (NULL, "c2", "C", temp);
7654       }
7655       break;
7656
7657     case M_MSGLD_T:
7658       gas_assert (!mips_opts.micromips);
7659       {
7660         unsigned long temp = (treg << 16) | (0x02);
7661         macro_build (NULL, "c2", "C", temp);
7662       }
7663       break;
7664
7665     case M_MSGWAIT:
7666       gas_assert (!mips_opts.micromips);
7667       macro_build (NULL, "c2", "C", 3);
7668       break;
7669
7670     case M_MSGWAIT_T:
7671       gas_assert (!mips_opts.micromips);
7672       {
7673         unsigned long temp = (treg << 16) | 0x03;
7674         macro_build (NULL, "c2", "C", temp);
7675       }
7676       break;
7677
7678     case M_J_A:
7679       /* The j instruction may not be used in PIC code, since it
7680          requires an absolute address.  We convert it to a b
7681          instruction.  */
7682       if (mips_pic == NO_PIC)
7683         macro_build (&offset_expr, "j", "a");
7684       else
7685         macro_build (&offset_expr, "b", "p");
7686       break;
7687
7688       /* The jal instructions must be handled as macros because when
7689          generating PIC code they expand to multi-instruction
7690          sequences.  Normally they are simple instructions.  */
7691     case M_JALS_1:
7692       dreg = RA;
7693       /* Fall through.  */
7694     case M_JALS_2:
7695       gas_assert (mips_opts.micromips);
7696       jals = 1;
7697       goto jal;
7698     case M_JAL_1:
7699       dreg = RA;
7700       /* Fall through.  */
7701     case M_JAL_2:
7702     jal:
7703       if (mips_pic == NO_PIC)
7704         {
7705           s = jals ? "jalrs" : "jalr";
7706           if (mips_opts.micromips && dreg == RA)
7707             macro_build (NULL, s, "mj", sreg);
7708           else
7709             macro_build (NULL, s, JALR_FMT, dreg, sreg);
7710         }
7711       else
7712         {
7713           int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
7714                            && mips_cprestore_offset >= 0);
7715
7716           if (sreg != PIC_CALL_REG)
7717             as_warn (_("MIPS PIC call to register other than $25"));
7718
7719           s = (mips_opts.micromips && (!mips_opts.noreorder || cprestore)
7720                ? "jalrs" : "jalr");
7721           if (mips_opts.micromips && dreg == RA)
7722             macro_build (NULL, s, "mj", sreg);
7723           else
7724             macro_build (NULL, s, JALR_FMT, dreg, sreg);
7725           if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
7726             {
7727               if (mips_cprestore_offset < 0)
7728                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7729               else
7730                 {
7731                   if (!mips_frame_reg_valid)
7732                     {
7733                       as_warn (_("No .frame pseudo-op used in PIC code"));
7734                       /* Quiet this warning.  */
7735                       mips_frame_reg_valid = 1;
7736                     }
7737                   if (!mips_cprestore_valid)
7738                     {
7739                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
7740                       /* Quiet this warning.  */
7741                       mips_cprestore_valid = 1;
7742                     }
7743                   if (mips_opts.noreorder)
7744                     macro_build (NULL, "nop", "");
7745                   expr1.X_add_number = mips_cprestore_offset;
7746                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7747                                                 mips_gp_register,
7748                                                 mips_frame_reg,
7749                                                 HAVE_64BIT_ADDRESSES);
7750                 }
7751             }
7752         }
7753
7754       break;
7755
7756     case M_JALS_A:
7757       gas_assert (mips_opts.micromips);
7758       jals = 1;
7759       /* Fall through.  */
7760     case M_JAL_A:
7761       if (mips_pic == NO_PIC)
7762         macro_build (&offset_expr, jals ? "jals" : "jal", "a");
7763       else if (mips_pic == SVR4_PIC)
7764         {
7765           /* If this is a reference to an external symbol, and we are
7766              using a small GOT, we want
7767                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
7768                nop
7769                jalr     $ra,$25
7770                nop
7771                lw       $gp,cprestore($sp)
7772              The cprestore value is set using the .cprestore
7773              pseudo-op.  If we are using a big GOT, we want
7774                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
7775                addu     $25,$25,$gp
7776                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
7777                nop
7778                jalr     $ra,$25
7779                nop
7780                lw       $gp,cprestore($sp)
7781              If the symbol is not external, we want
7782                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7783                nop
7784                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
7785                jalr     $ra,$25
7786                nop
7787                lw $gp,cprestore($sp)
7788
7789              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
7790              sequences above, minus nops, unless the symbol is local,
7791              which enables us to use GOT_PAGE/GOT_OFST (big got) or
7792              GOT_DISP.  */
7793           if (HAVE_NEWABI)
7794             {
7795               if (!mips_big_got)
7796                 {
7797                   relax_start (offset_expr.X_add_symbol);
7798                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7799                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7800                                mips_gp_register);
7801                   relax_switch ();
7802                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7803                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
7804                                mips_gp_register);
7805                   relax_end ();
7806                 }
7807               else
7808                 {
7809                   relax_start (offset_expr.X_add_symbol);
7810                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
7811                                BFD_RELOC_MIPS_CALL_HI16);
7812                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7813                                PIC_CALL_REG, mips_gp_register);
7814                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7815                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7816                                PIC_CALL_REG);
7817                   relax_switch ();
7818                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7819                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
7820                                mips_gp_register);
7821                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7822                                PIC_CALL_REG, PIC_CALL_REG,
7823                                BFD_RELOC_MIPS_GOT_OFST);
7824                   relax_end ();
7825                 }
7826
7827               macro_build_jalr (&offset_expr, 0);
7828             }
7829           else
7830             {
7831               relax_start (offset_expr.X_add_symbol);
7832               if (!mips_big_got)
7833                 {
7834                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7835                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
7836                                mips_gp_register);
7837                   load_delay_nop ();
7838                   relax_switch ();
7839                 }
7840               else
7841                 {
7842                   int gpdelay;
7843
7844                   gpdelay = reg_needs_delay (mips_gp_register);
7845                   macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
7846                                BFD_RELOC_MIPS_CALL_HI16);
7847                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
7848                                PIC_CALL_REG, mips_gp_register);
7849                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7850                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
7851                                PIC_CALL_REG);
7852                   load_delay_nop ();
7853                   relax_switch ();
7854                   if (gpdelay)
7855                     macro_build (NULL, "nop", "");
7856                 }
7857               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7858                            PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
7859                            mips_gp_register);
7860               load_delay_nop ();
7861               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7862                            PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
7863               relax_end ();
7864               macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
7865
7866               if (mips_cprestore_offset < 0)
7867                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
7868               else
7869                 {
7870                   if (!mips_frame_reg_valid)
7871                     {
7872                       as_warn (_("No .frame pseudo-op used in PIC code"));
7873                       /* Quiet this warning.  */
7874                       mips_frame_reg_valid = 1;
7875                     }
7876                   if (!mips_cprestore_valid)
7877                     {
7878                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
7879                       /* Quiet this warning.  */
7880                       mips_cprestore_valid = 1;
7881                     }
7882                   if (mips_opts.noreorder)
7883                     macro_build (NULL, "nop", "");
7884                   expr1.X_add_number = mips_cprestore_offset;
7885                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
7886                                                 mips_gp_register,
7887                                                 mips_frame_reg,
7888                                                 HAVE_64BIT_ADDRESSES);
7889                 }
7890             }
7891         }
7892       else if (mips_pic == VXWORKS_PIC)
7893         as_bad (_("Non-PIC jump used in PIC library"));
7894       else
7895         abort ();
7896
7897       break;
7898
7899     case M_ACLR_AB:
7900       ab = 1;
7901     case M_ACLR_OB:
7902       s = "aclr";
7903       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
7904       fmt = "\\,~(b)";
7905       off12 = 1;
7906       goto ld_st;
7907     case M_ASET_AB:
7908       ab = 1;
7909     case M_ASET_OB:
7910       s = "aset";
7911       treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
7912       fmt = "\\,~(b)";
7913       off12 = 1;
7914       goto ld_st;
7915     case M_LB_AB:
7916       ab = 1;
7917       s = "lb";
7918       fmt = "t,o(b)";
7919       goto ld;
7920     case M_LBU_AB:
7921       ab = 1;
7922       s = "lbu";
7923       fmt = "t,o(b)";
7924       goto ld;
7925     case M_LH_AB:
7926       ab = 1;
7927       s = "lh";
7928       fmt = "t,o(b)";
7929       goto ld;
7930     case M_LHU_AB:
7931       ab = 1;
7932       s = "lhu";
7933       fmt = "t,o(b)";
7934       goto ld;
7935     case M_LW_AB:
7936       ab = 1;
7937       s = "lw";
7938       fmt = "t,o(b)";
7939       goto ld;
7940     case M_LWC0_AB:
7941       ab = 1;
7942       gas_assert (!mips_opts.micromips);
7943       s = "lwc0";
7944       fmt = "E,o(b)";
7945       /* Itbl support may require additional care here.  */
7946       coproc = 1;
7947       goto ld_st;
7948     case M_LWC1_AB:
7949       ab = 1;
7950       s = "lwc1";
7951       fmt = "T,o(b)";
7952       /* Itbl support may require additional care here.  */
7953       coproc = 1;
7954       goto ld_st;
7955     case M_LWC2_AB:
7956       ab = 1;
7957     case M_LWC2_OB:
7958       s = "lwc2";
7959       fmt = COP12_FMT;
7960       off12 = mips_opts.micromips;
7961       /* Itbl support may require additional care here.  */
7962       coproc = 1;
7963       goto ld_st;
7964     case M_LWC3_AB:
7965       ab = 1;
7966       gas_assert (!mips_opts.micromips);
7967       s = "lwc3";
7968       fmt = "E,o(b)";
7969       /* Itbl support may require additional care here.  */
7970       coproc = 1;
7971       goto ld_st;
7972     case M_LWL_AB:
7973       ab = 1;
7974     case M_LWL_OB:
7975       s = "lwl";
7976       fmt = MEM12_FMT;
7977       off12 = mips_opts.micromips;
7978       goto ld_st;
7979     case M_LWR_AB:
7980       ab = 1;
7981     case M_LWR_OB:
7982       s = "lwr";
7983       fmt = MEM12_FMT;
7984       off12 = mips_opts.micromips;
7985       goto ld_st;
7986     case M_LDC1_AB:
7987       ab = 1;
7988       s = "ldc1";
7989       fmt = "T,o(b)";
7990       /* Itbl support may require additional care here.  */
7991       coproc = 1;
7992       goto ld_st;
7993     case M_LDC2_AB:
7994       ab = 1;
7995     case M_LDC2_OB:
7996       s = "ldc2";
7997       fmt = COP12_FMT;
7998       off12 = mips_opts.micromips;
7999       /* Itbl support may require additional care here.  */
8000       coproc = 1;
8001       goto ld_st;
8002     case M_LDC3_AB:
8003       ab = 1;
8004       s = "ldc3";
8005       fmt = "E,o(b)";
8006       /* Itbl support may require additional care here.  */
8007       coproc = 1;
8008       goto ld_st;
8009     case M_LDL_AB:
8010       ab = 1;
8011     case M_LDL_OB:
8012       s = "ldl";
8013       fmt = MEM12_FMT;
8014       off12 = mips_opts.micromips;
8015       goto ld_st;
8016     case M_LDR_AB:
8017       ab = 1;
8018     case M_LDR_OB:
8019       s = "ldr";
8020       fmt = MEM12_FMT;
8021       off12 = mips_opts.micromips;
8022       goto ld_st;
8023     case M_LL_AB:
8024       ab = 1;
8025     case M_LL_OB:
8026       s = "ll";
8027       fmt = MEM12_FMT;
8028       off12 = mips_opts.micromips;
8029       goto ld;
8030     case M_LLD_AB:
8031       ab = 1;
8032     case M_LLD_OB:
8033       s = "lld";
8034       fmt = MEM12_FMT;
8035       off12 = mips_opts.micromips;
8036       goto ld;
8037     case M_LWU_AB:
8038       ab = 1;
8039     case M_LWU_OB:
8040       s = "lwu";
8041       fmt = MEM12_FMT;
8042       off12 = mips_opts.micromips;
8043       goto ld;
8044     case M_LWP_AB:
8045       ab = 1;
8046     case M_LWP_OB:
8047       gas_assert (mips_opts.micromips);
8048       s = "lwp";
8049       fmt = "t,~(b)";
8050       off12 = 1;
8051       lp = 1;
8052       goto ld;
8053     case M_LDP_AB:
8054       ab = 1;
8055     case M_LDP_OB:
8056       gas_assert (mips_opts.micromips);
8057       s = "ldp";
8058       fmt = "t,~(b)";
8059       off12 = 1;
8060       lp = 1;
8061       goto ld;
8062     case M_LWM_AB:
8063       ab = 1;
8064     case M_LWM_OB:
8065       gas_assert (mips_opts.micromips);
8066       s = "lwm";
8067       fmt = "n,~(b)";
8068       off12 = 1;
8069       goto ld_st;
8070     case M_LDM_AB:
8071       ab = 1;
8072     case M_LDM_OB:
8073       gas_assert (mips_opts.micromips);
8074       s = "ldm";
8075       fmt = "n,~(b)";
8076       off12 = 1;
8077       goto ld_st;
8078
8079     ld:
8080       if (breg == treg + lp)
8081         goto ld_st;
8082       else
8083         tempreg = treg + lp;
8084       goto ld_noat;
8085
8086     case M_SB_AB:
8087       ab = 1;
8088       s = "sb";
8089       fmt = "t,o(b)";
8090       goto ld_st;
8091     case M_SH_AB:
8092       ab = 1;
8093       s = "sh";
8094       fmt = "t,o(b)";
8095       goto ld_st;
8096     case M_SW_AB:
8097       ab = 1;
8098       s = "sw";
8099       fmt = "t,o(b)";
8100       goto ld_st;
8101     case M_SWC0_AB:
8102       ab = 1;
8103       gas_assert (!mips_opts.micromips);
8104       s = "swc0";
8105       fmt = "E,o(b)";
8106       /* Itbl support may require additional care here.  */
8107       coproc = 1;
8108       goto ld_st;
8109     case M_SWC1_AB:
8110       ab = 1;
8111       s = "swc1";
8112       fmt = "T,o(b)";
8113       /* Itbl support may require additional care here.  */
8114       coproc = 1;
8115       goto ld_st;
8116     case M_SWC2_AB:
8117       ab = 1;
8118     case M_SWC2_OB:
8119       s = "swc2";
8120       fmt = COP12_FMT;
8121       off12 = mips_opts.micromips;
8122       /* Itbl support may require additional care here.  */
8123       coproc = 1;
8124       goto ld_st;
8125     case M_SWC3_AB:
8126       ab = 1;
8127       gas_assert (!mips_opts.micromips);
8128       s = "swc3";
8129       fmt = "E,o(b)";
8130       /* Itbl support may require additional care here.  */
8131       coproc = 1;
8132       goto ld_st;
8133     case M_SWL_AB:
8134       ab = 1;
8135     case M_SWL_OB:
8136       s = "swl";
8137       fmt = MEM12_FMT;
8138       off12 = mips_opts.micromips;
8139       goto ld_st;
8140     case M_SWR_AB:
8141       ab = 1;
8142     case M_SWR_OB:
8143       s = "swr";
8144       fmt = MEM12_FMT;
8145       off12 = mips_opts.micromips;
8146       goto ld_st;
8147     case M_SC_AB:
8148       ab = 1;
8149     case M_SC_OB:
8150       s = "sc";
8151       fmt = MEM12_FMT;
8152       off12 = mips_opts.micromips;
8153       goto ld_st;
8154     case M_SCD_AB:
8155       ab = 1;
8156     case M_SCD_OB:
8157       s = "scd";
8158       fmt = MEM12_FMT;
8159       off12 = mips_opts.micromips;
8160       goto ld_st;
8161     case M_CACHE_AB:
8162       ab = 1;
8163     case M_CACHE_OB:
8164       s = "cache";
8165       fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
8166       off12 = mips_opts.micromips;
8167       goto ld_st;
8168     case M_PREF_AB:
8169       ab = 1;
8170     case M_PREF_OB:
8171       s = "pref";
8172       fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
8173       off12 = mips_opts.micromips;
8174       goto ld_st;
8175     case M_SDC1_AB:
8176       ab = 1;
8177       s = "sdc1";
8178       fmt = "T,o(b)";
8179       coproc = 1;
8180       /* Itbl support may require additional care here.  */
8181       goto ld_st;
8182     case M_SDC2_AB:
8183       ab = 1;
8184     case M_SDC2_OB:
8185       s = "sdc2";
8186       fmt = COP12_FMT;
8187       off12 = mips_opts.micromips;
8188       /* Itbl support may require additional care here.  */
8189       coproc = 1;
8190       goto ld_st;
8191     case M_SDC3_AB:
8192       ab = 1;
8193       gas_assert (!mips_opts.micromips);
8194       s = "sdc3";
8195       fmt = "E,o(b)";
8196       /* Itbl support may require additional care here.  */
8197       coproc = 1;
8198       goto ld_st;
8199     case M_SDL_AB:
8200       ab = 1;
8201     case M_SDL_OB:
8202       s = "sdl";
8203       fmt = MEM12_FMT;
8204       off12 = mips_opts.micromips;
8205       goto ld_st;
8206     case M_SDR_AB:
8207       ab = 1;
8208     case M_SDR_OB:
8209       s = "sdr";
8210       fmt = MEM12_FMT;
8211       off12 = mips_opts.micromips;
8212       goto ld_st;
8213     case M_SWP_AB:
8214       ab = 1;
8215     case M_SWP_OB:
8216       gas_assert (mips_opts.micromips);
8217       s = "swp";
8218       fmt = "t,~(b)";
8219       off12 = 1;
8220       goto ld_st;
8221     case M_SDP_AB:
8222       ab = 1;
8223     case M_SDP_OB:
8224       gas_assert (mips_opts.micromips);
8225       s = "sdp";
8226       fmt = "t,~(b)";
8227       off12 = 1;
8228       goto ld_st;
8229     case M_SWM_AB:
8230       ab = 1;
8231     case M_SWM_OB:
8232       gas_assert (mips_opts.micromips);
8233       s = "swm";
8234       fmt = "n,~(b)";
8235       off12 = 1;
8236       goto ld_st;
8237     case M_SDM_AB:
8238       ab = 1;
8239     case M_SDM_OB:
8240       gas_assert (mips_opts.micromips);
8241       s = "sdm";
8242       fmt = "n,~(b)";
8243       off12 = 1;
8244
8245     ld_st:
8246       tempreg = AT;
8247       used_at = 1;
8248     ld_noat:
8249       if (coproc
8250           && NO_ISA_COP (mips_opts.arch)
8251           && (ip->insn_mo->pinfo2 & (INSN2_M_FP_S | INSN2_M_FP_D)) == 0)
8252         {
8253           as_bad (_("Opcode not supported on this processor: %s"),
8254                   mips_cpu_info_from_arch (mips_opts.arch)->name);
8255           break;
8256         }
8257
8258       if (offset_expr.X_op != O_constant
8259           && offset_expr.X_op != O_symbol)
8260         {
8261           as_bad (_("Expression too complex"));
8262           offset_expr.X_op = O_constant;
8263         }
8264
8265       if (HAVE_32BIT_ADDRESSES
8266           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8267         {
8268           char value [32];
8269
8270           sprintf_vma (value, offset_expr.X_add_number);
8271           as_bad (_("Number (0x%s) larger than 32 bits"), value);
8272         }
8273
8274       /* A constant expression in PIC code can be handled just as it
8275          is in non PIC code.  */
8276       if (offset_expr.X_op == O_constant)
8277         {
8278           int hipart = 0;
8279
8280           expr1.X_add_number = offset_expr.X_add_number;
8281           normalize_address_expr (&expr1);
8282           if (!off12 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
8283             {
8284               expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8285                                     & ~(bfd_vma) 0xffff);
8286               hipart = 1;
8287             }
8288           else if (off12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
8289             {
8290               expr1.X_add_number = ((expr1.X_add_number + 0x800)
8291                                     & ~(bfd_vma) 0xfff);
8292               hipart = 1;
8293             }
8294           if (hipart)
8295             {
8296               load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8297               if (breg != 0)
8298                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8299                              tempreg, tempreg, breg);
8300               breg = tempreg;
8301             }
8302           if (off0)
8303             {
8304               if (offset_expr.X_add_number == 0)
8305                 tempreg = breg;
8306               else
8307                 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8308                              "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8309               macro_build (NULL, s, fmt, treg, tempreg);
8310             }
8311           else if (!off12)
8312             macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8313           else
8314             macro_build (NULL, s, fmt,
8315                          treg, (unsigned long) offset_expr.X_add_number, breg);
8316         }
8317       else if (off12 || off0)
8318         {
8319           /* A 12-bit or 0-bit offset field is too narrow to be used
8320              for a low-part relocation, so load the whole address into
8321              the auxillary register.  In the case of "A(b)" addresses,
8322              we first load absolute address "A" into the register and
8323              then add base register "b".  In the case of "o(b)" addresses,
8324              we simply need to add 16-bit offset "o" to base register "b", and
8325              offset_reloc already contains the relocations associated
8326              with "o".  */
8327           if (ab)
8328             {
8329               load_address (tempreg, &offset_expr, &used_at);
8330               if (breg != 0)
8331                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8332                              tempreg, tempreg, breg);
8333             }
8334           else
8335             macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8336                          tempreg, breg, -1,
8337                          offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8338           expr1.X_add_number = 0;
8339           if (off0)
8340             macro_build (NULL, s, fmt, treg, tempreg);
8341           else
8342             macro_build (NULL, s, fmt,
8343                          treg, (unsigned long) expr1.X_add_number, tempreg);
8344         }
8345       else if (mips_pic == NO_PIC)
8346         {
8347           /* If this is a reference to a GP relative symbol, and there
8348              is no base register, we want
8349                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
8350              Otherwise, if there is no base register, we want
8351                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8352                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8353              If we have a constant, we need two instructions anyhow,
8354              so we always use the latter form.
8355
8356              If we have a base register, and this is a reference to a
8357              GP relative symbol, we want
8358                addu     $tempreg,$breg,$gp
8359                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
8360              Otherwise we want
8361                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
8362                addu     $tempreg,$tempreg,$breg
8363                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8364              With a constant we always use the latter case.
8365
8366              With 64bit address space and no base register and $at usable,
8367              we want
8368                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8369                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8370                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8371                dsll32   $tempreg,0
8372                daddu    $tempreg,$at
8373                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8374              If we have a base register, we want
8375                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8376                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8377                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8378                daddu    $at,$breg
8379                dsll32   $tempreg,0
8380                daddu    $tempreg,$at
8381                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8382
8383              Without $at we can't generate the optimal path for superscalar
8384              processors here since this would require two temporary registers.
8385                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8386                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8387                dsll     $tempreg,16
8388                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8389                dsll     $tempreg,16
8390                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8391              If we have a base register, we want
8392                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
8393                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
8394                dsll     $tempreg,16
8395                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
8396                dsll     $tempreg,16
8397                daddu    $tempreg,$tempreg,$breg
8398                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
8399
8400              For GP relative symbols in 64bit address space we can use
8401              the same sequence as in 32bit address space.  */
8402           if (HAVE_64BIT_SYMBOLS)
8403             {
8404               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8405                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8406                 {
8407                   relax_start (offset_expr.X_add_symbol);
8408                   if (breg == 0)
8409                     {
8410                       macro_build (&offset_expr, s, fmt, treg,
8411                                    BFD_RELOC_GPREL16, mips_gp_register);
8412                     }
8413                   else
8414                     {
8415                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8416                                    tempreg, breg, mips_gp_register);
8417                       macro_build (&offset_expr, s, fmt, treg,
8418                                    BFD_RELOC_GPREL16, tempreg);
8419                     }
8420                   relax_switch ();
8421                 }
8422
8423               if (used_at == 0 && mips_opts.at)
8424                 {
8425                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8426                                BFD_RELOC_MIPS_HIGHEST);
8427                   macro_build (&offset_expr, "lui", LUI_FMT, AT,
8428                                BFD_RELOC_HI16_S);
8429                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8430                                tempreg, BFD_RELOC_MIPS_HIGHER);
8431                   if (breg != 0)
8432                     macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
8433                   macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
8434                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
8435                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
8436                                tempreg);
8437                   used_at = 1;
8438                 }
8439               else
8440                 {
8441                   macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8442                                BFD_RELOC_MIPS_HIGHEST);
8443                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8444                                tempreg, BFD_RELOC_MIPS_HIGHER);
8445                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8446                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
8447                                tempreg, BFD_RELOC_HI16_S);
8448                   macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
8449                   if (breg != 0)
8450                     macro_build (NULL, "daddu", "d,v,t",
8451                                  tempreg, tempreg, breg);
8452                   macro_build (&offset_expr, s, fmt, treg,
8453                                BFD_RELOC_LO16, tempreg);
8454                 }
8455
8456               if (mips_relax.sequence)
8457                 relax_end ();
8458               break;
8459             }
8460
8461           if (breg == 0)
8462             {
8463               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8464                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8465                 {
8466                   relax_start (offset_expr.X_add_symbol);
8467                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
8468                                mips_gp_register);
8469                   relax_switch ();
8470                 }
8471               macro_build_lui (&offset_expr, tempreg);
8472               macro_build (&offset_expr, s, fmt, treg,
8473                            BFD_RELOC_LO16, tempreg);
8474               if (mips_relax.sequence)
8475                 relax_end ();
8476             }
8477           else
8478             {
8479               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8480                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8481                 {
8482                   relax_start (offset_expr.X_add_symbol);
8483                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8484                                tempreg, breg, mips_gp_register);
8485                   macro_build (&offset_expr, s, fmt, treg,
8486                                BFD_RELOC_GPREL16, tempreg);
8487                   relax_switch ();
8488                 }
8489               macro_build_lui (&offset_expr, tempreg);
8490               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8491                            tempreg, tempreg, breg);
8492               macro_build (&offset_expr, s, fmt, treg,
8493                            BFD_RELOC_LO16, tempreg);
8494               if (mips_relax.sequence)
8495                 relax_end ();
8496             }
8497         }
8498       else if (!mips_big_got)
8499         {
8500           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
8501
8502           /* If this is a reference to an external symbol, we want
8503                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8504                nop
8505                <op>     $treg,0($tempreg)
8506              Otherwise we want
8507                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8508                nop
8509                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8510                <op>     $treg,0($tempreg)
8511
8512              For NewABI, we want
8513                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
8514                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
8515
8516              If there is a base register, we add it to $tempreg before
8517              the <op>.  If there is a constant, we stick it in the
8518              <op> instruction.  We don't handle constants larger than
8519              16 bits, because we have no way to load the upper 16 bits
8520              (actually, we could handle them for the subset of cases
8521              in which we are not using $at).  */
8522           gas_assert (offset_expr.X_op == O_symbol);
8523           if (HAVE_NEWABI)
8524             {
8525               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8526                            BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8527               if (breg != 0)
8528                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8529                              tempreg, tempreg, breg);
8530               macro_build (&offset_expr, s, fmt, treg,
8531                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
8532               break;
8533             }
8534           expr1.X_add_number = offset_expr.X_add_number;
8535           offset_expr.X_add_number = 0;
8536           if (expr1.X_add_number < -0x8000
8537               || expr1.X_add_number >= 0x8000)
8538             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8539           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8540                        lw_reloc_type, mips_gp_register);
8541           load_delay_nop ();
8542           relax_start (offset_expr.X_add_symbol);
8543           relax_switch ();
8544           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8545                        tempreg, BFD_RELOC_LO16);
8546           relax_end ();
8547           if (breg != 0)
8548             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8549                          tempreg, tempreg, breg);
8550           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8551         }
8552       else if (mips_big_got && !HAVE_NEWABI)
8553         {
8554           int gpdelay;
8555
8556           /* If this is a reference to an external symbol, we want
8557                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8558                addu     $tempreg,$tempreg,$gp
8559                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8560                <op>     $treg,0($tempreg)
8561              Otherwise we want
8562                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
8563                nop
8564                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
8565                <op>     $treg,0($tempreg)
8566              If there is a base register, we add it to $tempreg before
8567              the <op>.  If there is a constant, we stick it in the
8568              <op> instruction.  We don't handle constants larger than
8569              16 bits, because we have no way to load the upper 16 bits
8570              (actually, we could handle them for the subset of cases
8571              in which we are not using $at).  */
8572           gas_assert (offset_expr.X_op == O_symbol);
8573           expr1.X_add_number = offset_expr.X_add_number;
8574           offset_expr.X_add_number = 0;
8575           if (expr1.X_add_number < -0x8000
8576               || expr1.X_add_number >= 0x8000)
8577             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8578           gpdelay = reg_needs_delay (mips_gp_register);
8579           relax_start (offset_expr.X_add_symbol);
8580           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8581                        BFD_RELOC_MIPS_GOT_HI16);
8582           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8583                        mips_gp_register);
8584           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8585                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
8586           relax_switch ();
8587           if (gpdelay)
8588             macro_build (NULL, "nop", "");
8589           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8590                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
8591           load_delay_nop ();
8592           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8593                        tempreg, BFD_RELOC_LO16);
8594           relax_end ();
8595
8596           if (breg != 0)
8597             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8598                          tempreg, tempreg, breg);
8599           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8600         }
8601       else if (mips_big_got && HAVE_NEWABI)
8602         {
8603           /* If this is a reference to an external symbol, we want
8604                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
8605                add      $tempreg,$tempreg,$gp
8606                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8607                <op>     $treg,<ofst>($tempreg)
8608              Otherwise, for local symbols, we want:
8609                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
8610                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
8611           gas_assert (offset_expr.X_op == O_symbol);
8612           expr1.X_add_number = offset_expr.X_add_number;
8613           offset_expr.X_add_number = 0;
8614           if (expr1.X_add_number < -0x8000
8615               || expr1.X_add_number >= 0x8000)
8616             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
8617           relax_start (offset_expr.X_add_symbol);
8618           macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
8619                        BFD_RELOC_MIPS_GOT_HI16);
8620           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
8621                        mips_gp_register);
8622           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8623                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
8624           if (breg != 0)
8625             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8626                          tempreg, tempreg, breg);
8627           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
8628
8629           relax_switch ();
8630           offset_expr.X_add_number = expr1.X_add_number;
8631           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8632                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8633           if (breg != 0)
8634             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8635                          tempreg, tempreg, breg);
8636           macro_build (&offset_expr, s, fmt, treg,
8637                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
8638           relax_end ();
8639         }
8640       else
8641         abort ();
8642
8643       break;
8644
8645     case M_LI:
8646     case M_LI_S:
8647       load_register (treg, &imm_expr, 0);
8648       break;
8649
8650     case M_DLI:
8651       load_register (treg, &imm_expr, 1);
8652       break;
8653
8654     case M_LI_SS:
8655       if (imm_expr.X_op == O_constant)
8656         {
8657           used_at = 1;
8658           load_register (AT, &imm_expr, 0);
8659           macro_build (NULL, "mtc1", "t,G", AT, treg);
8660           break;
8661         }
8662       else
8663         {
8664           gas_assert (offset_expr.X_op == O_symbol
8665                       && strcmp (segment_name (S_GET_SEGMENT
8666                                                (offset_expr.X_add_symbol)),
8667                                  ".lit4") == 0
8668                       && offset_expr.X_add_number == 0);
8669           macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
8670                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8671           break;
8672         }
8673
8674     case M_LI_D:
8675       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
8676          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
8677          order 32 bits of the value and the low order 32 bits are either
8678          zero or in OFFSET_EXPR.  */
8679       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8680         {
8681           if (HAVE_64BIT_GPRS)
8682             load_register (treg, &imm_expr, 1);
8683           else
8684             {
8685               int hreg, lreg;
8686
8687               if (target_big_endian)
8688                 {
8689                   hreg = treg;
8690                   lreg = treg + 1;
8691                 }
8692               else
8693                 {
8694                   hreg = treg + 1;
8695                   lreg = treg;
8696                 }
8697
8698               if (hreg <= 31)
8699                 load_register (hreg, &imm_expr, 0);
8700               if (lreg <= 31)
8701                 {
8702                   if (offset_expr.X_op == O_absent)
8703                     move_register (lreg, 0);
8704                   else
8705                     {
8706                       gas_assert (offset_expr.X_op == O_constant);
8707                       load_register (lreg, &offset_expr, 0);
8708                     }
8709                 }
8710             }
8711           break;
8712         }
8713
8714       /* We know that sym is in the .rdata section.  First we get the
8715          upper 16 bits of the address.  */
8716       if (mips_pic == NO_PIC)
8717         {
8718           macro_build_lui (&offset_expr, AT);
8719           used_at = 1;
8720         }
8721       else
8722         {
8723           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8724                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
8725           used_at = 1;
8726         }
8727
8728       /* Now we load the register(s).  */
8729       if (HAVE_64BIT_GPRS)
8730         {
8731           used_at = 1;
8732           macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8733         }
8734       else
8735         {
8736           used_at = 1;
8737           macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
8738           if (treg != RA)
8739             {
8740               /* FIXME: How in the world do we deal with the possible
8741                  overflow here?  */
8742               offset_expr.X_add_number += 4;
8743               macro_build (&offset_expr, "lw", "t,o(b)",
8744                            treg + 1, BFD_RELOC_LO16, AT);
8745             }
8746         }
8747       break;
8748
8749     case M_LI_DD:
8750       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
8751          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
8752          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
8753          the value and the low order 32 bits are either zero or in
8754          OFFSET_EXPR.  */
8755       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
8756         {
8757           used_at = 1;
8758           load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
8759           if (HAVE_64BIT_FPRS)
8760             {
8761               gas_assert (HAVE_64BIT_GPRS);
8762               macro_build (NULL, "dmtc1", "t,S", AT, treg);
8763             }
8764           else
8765             {
8766               macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
8767               if (offset_expr.X_op == O_absent)
8768                 macro_build (NULL, "mtc1", "t,G", 0, treg);
8769               else
8770                 {
8771                   gas_assert (offset_expr.X_op == O_constant);
8772                   load_register (AT, &offset_expr, 0);
8773                   macro_build (NULL, "mtc1", "t,G", AT, treg);
8774                 }
8775             }
8776           break;
8777         }
8778
8779       gas_assert (offset_expr.X_op == O_symbol
8780                   && offset_expr.X_add_number == 0);
8781       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
8782       if (strcmp (s, ".lit8") == 0)
8783         {
8784           if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
8785             {
8786               macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
8787                            BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8788               break;
8789             }
8790           breg = mips_gp_register;
8791           r = BFD_RELOC_MIPS_LITERAL;
8792           goto dob;
8793         }
8794       else
8795         {
8796           gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8797           used_at = 1;
8798           if (mips_pic != NO_PIC)
8799             macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
8800                          BFD_RELOC_MIPS_GOT16, mips_gp_register);
8801           else
8802             {
8803               /* FIXME: This won't work for a 64 bit address.  */
8804               macro_build_lui (&offset_expr, AT);
8805             }
8806
8807           if (mips_opts.isa != ISA_MIPS1 || mips_opts.micromips)
8808             {
8809               macro_build (&offset_expr, "ldc1", "T,o(b)",
8810                            treg, BFD_RELOC_LO16, AT);
8811               break;
8812             }
8813           breg = AT;
8814           r = BFD_RELOC_LO16;
8815           goto dob;
8816         }
8817
8818     case M_L_DOB:
8819       /* Even on a big endian machine $fn comes before $fn+1.  We have
8820          to adjust when loading from memory.  */
8821       r = BFD_RELOC_LO16;
8822     dob:
8823       gas_assert (!mips_opts.micromips);
8824       gas_assert (mips_opts.isa == ISA_MIPS1);
8825       macro_build (&offset_expr, "lwc1", "T,o(b)",
8826                    target_big_endian ? treg + 1 : treg, r, breg);
8827       /* FIXME: A possible overflow which I don't know how to deal
8828          with.  */
8829       offset_expr.X_add_number += 4;
8830       macro_build (&offset_expr, "lwc1", "T,o(b)",
8831                    target_big_endian ? treg : treg + 1, r, breg);
8832       break;
8833
8834     case M_S_DOB:
8835       gas_assert (!mips_opts.micromips);
8836       gas_assert (mips_opts.isa == ISA_MIPS1);
8837       /* Even on a big endian machine $fn comes before $fn+1.  We have
8838          to adjust when storing to memory.  */
8839       macro_build (&offset_expr, "swc1", "T,o(b)",
8840                    target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
8841       offset_expr.X_add_number += 4;
8842       macro_build (&offset_expr, "swc1", "T,o(b)",
8843                    target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8844       break;
8845
8846     case M_L_DAB:
8847       gas_assert (!mips_opts.micromips);
8848       /*
8849        * The MIPS assembler seems to check for X_add_number not
8850        * being double aligned and generating:
8851        *        lui     at,%hi(foo+1)
8852        *        addu    at,at,v1
8853        *        addiu   at,at,%lo(foo+1)
8854        *        lwc1    f2,0(at)
8855        *        lwc1    f3,4(at)
8856        * But, the resulting address is the same after relocation so why
8857        * generate the extra instruction?
8858        */
8859       /* Itbl support may require additional care here.  */
8860       coproc = 1;
8861       fmt = "T,o(b)";
8862       if (mips_opts.isa != ISA_MIPS1)
8863         {
8864           s = "ldc1";
8865           goto ld_st;
8866         }
8867       s = "lwc1";
8868       goto ldd_std;
8869
8870     case M_S_DAB:
8871       gas_assert (!mips_opts.micromips);
8872       /* Itbl support may require additional care here.  */
8873       coproc = 1;
8874       fmt = "T,o(b)";
8875       if (mips_opts.isa != ISA_MIPS1)
8876         {
8877           s = "sdc1";
8878           goto ld_st;
8879         }
8880       s = "swc1";
8881       goto ldd_std;
8882
8883     case M_LD_AB:
8884       fmt = "t,o(b)";
8885       if (HAVE_64BIT_GPRS)
8886         {
8887           s = "ld";
8888           goto ld;
8889         }
8890       s = "lw";
8891       goto ldd_std;
8892
8893     case M_SD_AB:
8894       fmt = "t,o(b)";
8895       if (HAVE_64BIT_GPRS)
8896         {
8897           s = "sd";
8898           goto ld_st;
8899         }
8900       s = "sw";
8901
8902     ldd_std:
8903       if (offset_expr.X_op != O_symbol
8904           && offset_expr.X_op != O_constant)
8905         {
8906           as_bad (_("Expression too complex"));
8907           offset_expr.X_op = O_constant;
8908         }
8909
8910       if (HAVE_32BIT_ADDRESSES
8911           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
8912         {
8913           char value [32];
8914
8915           sprintf_vma (value, offset_expr.X_add_number);
8916           as_bad (_("Number (0x%s) larger than 32 bits"), value);
8917         }
8918
8919       /* Even on a big endian machine $fn comes before $fn+1.  We have
8920          to adjust when loading from memory.  We set coproc if we must
8921          load $fn+1 first.  */
8922       /* Itbl support may require additional care here.  */
8923       if (!target_big_endian)
8924         coproc = 0;
8925
8926       if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
8927         {
8928           /* If this is a reference to a GP relative symbol, we want
8929                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
8930                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
8931              If we have a base register, we use this
8932                addu     $at,$breg,$gp
8933                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
8934                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
8935              If this is not a GP relative symbol, we want
8936                lui      $at,<sym>               (BFD_RELOC_HI16_S)
8937                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
8938                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
8939              If there is a base register, we add it to $at after the
8940              lui instruction.  If there is a constant, we always use
8941              the last case.  */
8942           if (offset_expr.X_op == O_symbol
8943               && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
8944               && !nopic_need_relax (offset_expr.X_add_symbol, 1))
8945             {
8946               relax_start (offset_expr.X_add_symbol);
8947               if (breg == 0)
8948                 {
8949                   tempreg = mips_gp_register;
8950                 }
8951               else
8952                 {
8953                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8954                                AT, breg, mips_gp_register);
8955                   tempreg = AT;
8956                   used_at = 1;
8957                 }
8958
8959               /* Itbl support may require additional care here.  */
8960               macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
8961                            BFD_RELOC_GPREL16, tempreg);
8962               offset_expr.X_add_number += 4;
8963
8964               /* Set mips_optimize to 2 to avoid inserting an
8965                  undesired nop.  */
8966               hold_mips_optimize = mips_optimize;
8967               mips_optimize = 2;
8968               /* Itbl support may require additional care here.  */
8969               macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
8970                            BFD_RELOC_GPREL16, tempreg);
8971               mips_optimize = hold_mips_optimize;
8972
8973               relax_switch ();
8974
8975               offset_expr.X_add_number -= 4;
8976             }
8977           used_at = 1;
8978           macro_build_lui (&offset_expr, AT);
8979           if (breg != 0)
8980             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
8981           /* Itbl support may require additional care here.  */
8982           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
8983                        BFD_RELOC_LO16, AT);
8984           /* FIXME: How do we handle overflow here?  */
8985           offset_expr.X_add_number += 4;
8986           /* Itbl support may require additional care here.  */
8987           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
8988                        BFD_RELOC_LO16, AT);
8989           if (mips_relax.sequence)
8990             relax_end ();
8991         }
8992       else if (!mips_big_got)
8993         {
8994           /* If this is a reference to an external symbol, we want
8995                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
8996                nop
8997                <op>     $treg,0($at)
8998                <op>     $treg+1,4($at)
8999              Otherwise we want
9000                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9001                nop
9002                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9003                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9004              If there is a base register we add it to $at before the
9005              lwc1 instructions.  If there is a constant we include it
9006              in the lwc1 instructions.  */
9007           used_at = 1;
9008           expr1.X_add_number = offset_expr.X_add_number;
9009           if (expr1.X_add_number < -0x8000
9010               || expr1.X_add_number >= 0x8000 - 4)
9011             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9012           load_got_offset (AT, &offset_expr);
9013           load_delay_nop ();
9014           if (breg != 0)
9015             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9016
9017           /* Set mips_optimize to 2 to avoid inserting an undesired
9018              nop.  */
9019           hold_mips_optimize = mips_optimize;
9020           mips_optimize = 2;
9021
9022           /* Itbl support may require additional care here.  */
9023           relax_start (offset_expr.X_add_symbol);
9024           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9025                        BFD_RELOC_LO16, AT);
9026           expr1.X_add_number += 4;
9027           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9028                        BFD_RELOC_LO16, AT);
9029           relax_switch ();
9030           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9031                        BFD_RELOC_LO16, AT);
9032           offset_expr.X_add_number += 4;
9033           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9034                        BFD_RELOC_LO16, AT);
9035           relax_end ();
9036
9037           mips_optimize = hold_mips_optimize;
9038         }
9039       else if (mips_big_got)
9040         {
9041           int gpdelay;
9042
9043           /* If this is a reference to an external symbol, we want
9044                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
9045                addu     $at,$at,$gp
9046                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
9047                nop
9048                <op>     $treg,0($at)
9049                <op>     $treg+1,4($at)
9050              Otherwise we want
9051                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
9052                nop
9053                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
9054                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
9055              If there is a base register we add it to $at before the
9056              lwc1 instructions.  If there is a constant we include it
9057              in the lwc1 instructions.  */
9058           used_at = 1;
9059           expr1.X_add_number = offset_expr.X_add_number;
9060           offset_expr.X_add_number = 0;
9061           if (expr1.X_add_number < -0x8000
9062               || expr1.X_add_number >= 0x8000 - 4)
9063             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9064           gpdelay = reg_needs_delay (mips_gp_register);
9065           relax_start (offset_expr.X_add_symbol);
9066           macro_build (&offset_expr, "lui", LUI_FMT,
9067                        AT, BFD_RELOC_MIPS_GOT_HI16);
9068           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9069                        AT, AT, mips_gp_register);
9070           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
9071                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
9072           load_delay_nop ();
9073           if (breg != 0)
9074             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9075           /* Itbl support may require additional care here.  */
9076           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9077                        BFD_RELOC_LO16, AT);
9078           expr1.X_add_number += 4;
9079
9080           /* Set mips_optimize to 2 to avoid inserting an undesired
9081              nop.  */
9082           hold_mips_optimize = mips_optimize;
9083           mips_optimize = 2;
9084           /* Itbl support may require additional care here.  */
9085           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9086                        BFD_RELOC_LO16, AT);
9087           mips_optimize = hold_mips_optimize;
9088           expr1.X_add_number -= 4;
9089
9090           relax_switch ();
9091           offset_expr.X_add_number = expr1.X_add_number;
9092           if (gpdelay)
9093             macro_build (NULL, "nop", "");
9094           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9095                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
9096           load_delay_nop ();
9097           if (breg != 0)
9098             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
9099           /* Itbl support may require additional care here.  */
9100           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9101                        BFD_RELOC_LO16, AT);
9102           offset_expr.X_add_number += 4;
9103
9104           /* Set mips_optimize to 2 to avoid inserting an undesired
9105              nop.  */
9106           hold_mips_optimize = mips_optimize;
9107           mips_optimize = 2;
9108           /* Itbl support may require additional care here.  */
9109           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9110                        BFD_RELOC_LO16, AT);
9111           mips_optimize = hold_mips_optimize;
9112           relax_end ();
9113         }
9114       else
9115         abort ();
9116
9117       break;
9118
9119     case M_LD_OB:
9120       s = HAVE_64BIT_GPRS ? "ld" : "lw";
9121       goto sd_ob;
9122     case M_SD_OB:
9123       s = HAVE_64BIT_GPRS ? "sd" : "sw";
9124     sd_ob:
9125       macro_build (&offset_expr, s, "t,o(b)", treg,
9126                    -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9127                    breg);
9128       if (!HAVE_64BIT_GPRS)
9129         {
9130           offset_expr.X_add_number += 4;
9131           macro_build (&offset_expr, s, "t,o(b)", treg + 1,
9132                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9133                        breg);
9134         }
9135       break;
9136
9137         
9138     case M_SAA_AB:
9139       ab = 1;
9140     case M_SAA_OB:
9141       s = "saa";
9142       off0 = 1;
9143       fmt = "t,(b)";
9144       goto ld_st;
9145     case M_SAAD_AB:
9146       ab = 1;
9147     case M_SAAD_OB:
9148       s = "saad";
9149       off0 = 1;
9150       fmt = "t,(b)";
9151       goto ld_st;
9152
9153    /* New code added to support COPZ instructions.
9154       This code builds table entries out of the macros in mip_opcodes.
9155       R4000 uses interlocks to handle coproc delays.
9156       Other chips (like the R3000) require nops to be inserted for delays.
9157
9158       FIXME: Currently, we require that the user handle delays.
9159       In order to fill delay slots for non-interlocked chips,
9160       we must have a way to specify delays based on the coprocessor.
9161       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9162       What are the side-effects of the cop instruction?
9163       What cache support might we have and what are its effects?
9164       Both coprocessor & memory require delays. how long???
9165       What registers are read/set/modified?
9166
9167       If an itbl is provided to interpret cop instructions,
9168       this knowledge can be encoded in the itbl spec.  */
9169
9170     case M_COP0:
9171       s = "c0";
9172       goto copz;
9173     case M_COP1:
9174       s = "c1";
9175       goto copz;
9176     case M_COP2:
9177       s = "c2";
9178       goto copz;
9179     case M_COP3:
9180       s = "c3";
9181     copz:
9182       gas_assert (!mips_opts.micromips);
9183       if (NO_ISA_COP (mips_opts.arch)
9184           && (ip->insn_mo->pinfo2 & INSN2_M_FP_S) == 0)
9185         {
9186           as_bad (_("Opcode not supported on this processor: %s"),
9187                   mips_cpu_info_from_arch (mips_opts.arch)->name);
9188           break;
9189         }
9190
9191       /* For now we just do C (same as Cz).  The parameter will be
9192          stored in insn_opcode by mips_ip.  */
9193       macro_build (NULL, s, "C", ip->insn_opcode);
9194       break;
9195
9196     case M_MOVE:
9197       move_register (dreg, sreg);
9198       break;
9199
9200     case M_DMUL:
9201       dbl = 1;
9202     case M_MUL:
9203       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
9204       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9205       break;
9206
9207     case M_DMUL_I:
9208       dbl = 1;
9209     case M_MUL_I:
9210       /* The MIPS assembler some times generates shifts and adds.  I'm
9211          not trying to be that fancy. GCC should do this for us
9212          anyway.  */
9213       used_at = 1;
9214       load_register (AT, &imm_expr, dbl);
9215       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
9216       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9217       break;
9218
9219     case M_DMULO_I:
9220       dbl = 1;
9221     case M_MULO_I:
9222       imm = 1;
9223       goto do_mulo;
9224
9225     case M_DMULO:
9226       dbl = 1;
9227     case M_MULO:
9228     do_mulo:
9229       start_noreorder ();
9230       used_at = 1;
9231       if (imm)
9232         load_register (AT, &imm_expr, dbl);
9233       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
9234       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9235       macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9236       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9237       if (mips_trap)
9238         macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
9239       else
9240         {
9241           if (mips_opts.micromips)
9242             micromips_label_expr (&label_expr);
9243           else
9244             label_expr.X_add_number = 8;
9245           macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
9246           macro_build (NULL, "nop", "");
9247           macro_build (NULL, "break", BRK_FMT, 6);
9248           if (mips_opts.micromips)
9249             micromips_add_label ();
9250         }
9251       end_noreorder ();
9252       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9253       break;
9254
9255     case M_DMULOU_I:
9256       dbl = 1;
9257     case M_MULOU_I:
9258       imm = 1;
9259       goto do_mulou;
9260
9261     case M_DMULOU:
9262       dbl = 1;
9263     case M_MULOU:
9264     do_mulou:
9265       start_noreorder ();
9266       used_at = 1;
9267       if (imm)
9268         load_register (AT, &imm_expr, dbl);
9269       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
9270                    sreg, imm ? AT : treg);
9271       macro_build (NULL, "mfhi", MFHL_FMT, AT);
9272       macro_build (NULL, "mflo", MFHL_FMT, dreg);
9273       if (mips_trap)
9274         macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
9275       else
9276         {
9277           if (mips_opts.micromips)
9278             micromips_label_expr (&label_expr);
9279           else
9280             label_expr.X_add_number = 8;
9281           macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
9282           macro_build (NULL, "nop", "");
9283           macro_build (NULL, "break", BRK_FMT, 6);
9284           if (mips_opts.micromips)
9285             micromips_add_label ();
9286         }
9287       end_noreorder ();
9288       break;
9289
9290     case M_DROL:
9291       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9292         {
9293           if (dreg == sreg)
9294             {
9295               tempreg = AT;
9296               used_at = 1;
9297             }
9298           else
9299             {
9300               tempreg = dreg;
9301             }
9302           macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9303           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
9304           break;
9305         }
9306       used_at = 1;
9307       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9308       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9309       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9310       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9311       break;
9312
9313     case M_ROL:
9314       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9315         {
9316           if (dreg == sreg)
9317             {
9318               tempreg = AT;
9319               used_at = 1;
9320             }
9321           else
9322             {
9323               tempreg = dreg;
9324             }
9325           macro_build (NULL, "negu", "d,w", tempreg, treg);
9326           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
9327           break;
9328         }
9329       used_at = 1;
9330       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9331       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9332       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9333       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9334       break;
9335
9336     case M_DROL_I:
9337       {
9338         unsigned int rot;
9339         char *l;
9340         char *rr;
9341
9342         if (imm_expr.X_op != O_constant)
9343           as_bad (_("Improper rotate count"));
9344         rot = imm_expr.X_add_number & 0x3f;
9345         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9346           {
9347             rot = (64 - rot) & 0x3f;
9348             if (rot >= 32)
9349               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9350             else
9351               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9352             break;
9353           }
9354         if (rot == 0)
9355           {
9356             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9357             break;
9358           }
9359         l = (rot < 0x20) ? "dsll" : "dsll32";
9360         rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
9361         rot &= 0x1f;
9362         used_at = 1;
9363         macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
9364         macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9365         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9366       }
9367       break;
9368
9369     case M_ROL_I:
9370       {
9371         unsigned int rot;
9372
9373         if (imm_expr.X_op != O_constant)
9374           as_bad (_("Improper rotate count"));
9375         rot = imm_expr.X_add_number & 0x1f;
9376         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9377           {
9378             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
9379             break;
9380           }
9381         if (rot == 0)
9382           {
9383             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9384             break;
9385           }
9386         used_at = 1;
9387         macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
9388         macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9389         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9390       }
9391       break;
9392
9393     case M_DROR:
9394       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9395         {
9396           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
9397           break;
9398         }
9399       used_at = 1;
9400       macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
9401       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
9402       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
9403       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9404       break;
9405
9406     case M_ROR:
9407       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9408         {
9409           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
9410           break;
9411         }
9412       used_at = 1;
9413       macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
9414       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
9415       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
9416       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9417       break;
9418
9419     case M_DROR_I:
9420       {
9421         unsigned int rot;
9422         char *l;
9423         char *rr;
9424
9425         if (imm_expr.X_op != O_constant)
9426           as_bad (_("Improper rotate count"));
9427         rot = imm_expr.X_add_number & 0x3f;
9428         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
9429           {
9430             if (rot >= 32)
9431               macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
9432             else
9433               macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
9434             break;
9435           }
9436         if (rot == 0)
9437           {
9438             macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
9439             break;
9440           }
9441         rr = (rot < 0x20) ? "dsrl" : "dsrl32";
9442         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
9443         rot &= 0x1f;
9444         used_at = 1;
9445         macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
9446         macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9447         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9448       }
9449       break;
9450
9451     case M_ROR_I:
9452       {
9453         unsigned int rot;
9454
9455         if (imm_expr.X_op != O_constant)
9456           as_bad (_("Improper rotate count"));
9457         rot = imm_expr.X_add_number & 0x1f;
9458         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
9459           {
9460             macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
9461             break;
9462           }
9463         if (rot == 0)
9464           {
9465             macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
9466             break;
9467           }
9468         used_at = 1;
9469         macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
9470         macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
9471         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
9472       }
9473       break;
9474
9475     case M_SEQ:
9476       if (sreg == 0)
9477         macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
9478       else if (treg == 0)
9479         macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9480       else
9481         {
9482           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9483           macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9484         }
9485       break;
9486
9487     case M_SEQ_I:
9488       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9489         {
9490           macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9491           break;
9492         }
9493       if (sreg == 0)
9494         {
9495           as_warn (_("Instruction %s: result is always false"),
9496                    ip->insn_mo->name);
9497           move_register (dreg, 0);
9498           break;
9499         }
9500       if (CPU_HAS_SEQ (mips_opts.arch)
9501           && -512 <= imm_expr.X_add_number
9502           && imm_expr.X_add_number < 512)
9503         {
9504           macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
9505                        (int) imm_expr.X_add_number);
9506           break;
9507         }
9508       if (imm_expr.X_op == O_constant
9509           && imm_expr.X_add_number >= 0
9510           && imm_expr.X_add_number < 0x10000)
9511         {
9512           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9513         }
9514       else if (imm_expr.X_op == O_constant
9515                && imm_expr.X_add_number > -0x8000
9516                && imm_expr.X_add_number < 0)
9517         {
9518           imm_expr.X_add_number = -imm_expr.X_add_number;
9519           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9520                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9521         }
9522       else if (CPU_HAS_SEQ (mips_opts.arch))
9523         {
9524           used_at = 1;
9525           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9526           macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
9527           break;
9528         }
9529       else
9530         {
9531           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9532           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9533           used_at = 1;
9534         }
9535       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
9536       break;
9537
9538     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
9539       s = "slt";
9540       goto sge;
9541     case M_SGEU:
9542       s = "sltu";
9543     sge:
9544       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
9545       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9546       break;
9547
9548     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
9549     case M_SGEU_I:
9550       if (imm_expr.X_op == O_constant
9551           && imm_expr.X_add_number >= -0x8000
9552           && imm_expr.X_add_number < 0x8000)
9553         {
9554           macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
9555                        dreg, sreg, BFD_RELOC_LO16);
9556         }
9557       else
9558         {
9559           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9560           macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
9561                        dreg, sreg, AT);
9562           used_at = 1;
9563         }
9564       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9565       break;
9566
9567     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
9568       s = "slt";
9569       goto sgt;
9570     case M_SGTU:
9571       s = "sltu";
9572     sgt:
9573       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9574       break;
9575
9576     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
9577       s = "slt";
9578       goto sgti;
9579     case M_SGTU_I:
9580       s = "sltu";
9581     sgti:
9582       used_at = 1;
9583       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9584       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9585       break;
9586
9587     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
9588       s = "slt";
9589       goto sle;
9590     case M_SLEU:
9591       s = "sltu";
9592     sle:
9593       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
9594       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9595       break;
9596
9597     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
9598       s = "slt";
9599       goto slei;
9600     case M_SLEU_I:
9601       s = "sltu";
9602     slei:
9603       used_at = 1;
9604       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9605       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
9606       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
9607       break;
9608
9609     case M_SLT_I:
9610       if (imm_expr.X_op == O_constant
9611           && imm_expr.X_add_number >= -0x8000
9612           && imm_expr.X_add_number < 0x8000)
9613         {
9614           macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9615           break;
9616         }
9617       used_at = 1;
9618       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9619       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
9620       break;
9621
9622     case M_SLTU_I:
9623       if (imm_expr.X_op == O_constant
9624           && imm_expr.X_add_number >= -0x8000
9625           && imm_expr.X_add_number < 0x8000)
9626         {
9627           macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
9628                        BFD_RELOC_LO16);
9629           break;
9630         }
9631       used_at = 1;
9632       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9633       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
9634       break;
9635
9636     case M_SNE:
9637       if (sreg == 0)
9638         macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
9639       else if (treg == 0)
9640         macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9641       else
9642         {
9643           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
9644           macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9645         }
9646       break;
9647
9648     case M_SNE_I:
9649       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
9650         {
9651           macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
9652           break;
9653         }
9654       if (sreg == 0)
9655         {
9656           as_warn (_("Instruction %s: result is always true"),
9657                    ip->insn_mo->name);
9658           macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
9659                        dreg, 0, BFD_RELOC_LO16);
9660           break;
9661         }
9662       if (CPU_HAS_SEQ (mips_opts.arch)
9663           && -512 <= imm_expr.X_add_number
9664           && imm_expr.X_add_number < 512)
9665         {
9666           macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
9667                        (int) imm_expr.X_add_number);
9668           break;
9669         }
9670       if (imm_expr.X_op == O_constant
9671           && imm_expr.X_add_number >= 0
9672           && imm_expr.X_add_number < 0x10000)
9673         {
9674           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
9675         }
9676       else if (imm_expr.X_op == O_constant
9677                && imm_expr.X_add_number > -0x8000
9678                && imm_expr.X_add_number < 0)
9679         {
9680           imm_expr.X_add_number = -imm_expr.X_add_number;
9681           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
9682                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9683         }
9684       else if (CPU_HAS_SEQ (mips_opts.arch))
9685         {
9686           used_at = 1;
9687           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9688           macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
9689           break;
9690         }
9691       else
9692         {
9693           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9694           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
9695           used_at = 1;
9696         }
9697       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
9698       break;
9699
9700     case M_SUB_I:
9701       s = "addi";
9702       s2 = "sub";
9703       goto do_subi;
9704     case M_SUBU_I:
9705       s = "addiu";
9706       s2 = "subu";
9707       goto do_subi;
9708     case M_DSUB_I:
9709       dbl = 1;
9710       s = "daddi";
9711       s2 = "dsub";
9712       if (!mips_opts.micromips)
9713         goto do_subi;
9714       if (imm_expr.X_op == O_constant
9715           && imm_expr.X_add_number > -0x200
9716           && imm_expr.X_add_number <= 0x200)
9717         {
9718           macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
9719           break;
9720         }
9721       goto do_subi_i;
9722     case M_DSUBU_I:
9723       dbl = 1;
9724       s = "daddiu";
9725       s2 = "dsubu";
9726     do_subi:
9727       if (imm_expr.X_op == O_constant
9728           && imm_expr.X_add_number > -0x8000
9729           && imm_expr.X_add_number <= 0x8000)
9730         {
9731           imm_expr.X_add_number = -imm_expr.X_add_number;
9732           macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
9733           break;
9734         }
9735     do_subi_i:
9736       used_at = 1;
9737       load_register (AT, &imm_expr, dbl);
9738       macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
9739       break;
9740
9741     case M_TEQ_I:
9742       s = "teq";
9743       goto trap;
9744     case M_TGE_I:
9745       s = "tge";
9746       goto trap;
9747     case M_TGEU_I:
9748       s = "tgeu";
9749       goto trap;
9750     case M_TLT_I:
9751       s = "tlt";
9752       goto trap;
9753     case M_TLTU_I:
9754       s = "tltu";
9755       goto trap;
9756     case M_TNE_I:
9757       s = "tne";
9758     trap:
9759       used_at = 1;
9760       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
9761       macro_build (NULL, s, "s,t", sreg, AT);
9762       break;
9763
9764     case M_TRUNCWS:
9765     case M_TRUNCWD:
9766       gas_assert (!mips_opts.micromips);
9767       gas_assert (mips_opts.isa == ISA_MIPS1);
9768       used_at = 1;
9769       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
9770       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
9771
9772       /*
9773        * Is the double cfc1 instruction a bug in the mips assembler;
9774        * or is there a reason for it?
9775        */
9776       start_noreorder ();
9777       macro_build (NULL, "cfc1", "t,G", treg, RA);
9778       macro_build (NULL, "cfc1", "t,G", treg, RA);
9779       macro_build (NULL, "nop", "");
9780       expr1.X_add_number = 3;
9781       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
9782       expr1.X_add_number = 2;
9783       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9784       macro_build (NULL, "ctc1", "t,G", AT, RA);
9785       macro_build (NULL, "nop", "");
9786       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
9787                    dreg, sreg);
9788       macro_build (NULL, "ctc1", "t,G", treg, RA);
9789       macro_build (NULL, "nop", "");
9790       end_noreorder ();
9791       break;
9792
9793     case M_ULH_A:
9794       ab = 1;
9795     case M_ULH:
9796       s = "lb";
9797       s2 = "lbu";
9798       off = 1;
9799       goto uld_st;
9800     case M_ULHU_A:
9801       ab = 1;
9802     case M_ULHU:
9803       s = "lbu";
9804       s2 = "lbu";
9805       off = 1;
9806       goto uld_st;
9807     case M_ULW_A:
9808       ab = 1;
9809     case M_ULW:
9810       s = "lwl";
9811       s2 = "lwr";
9812       off12 = mips_opts.micromips;
9813       off = 3;
9814       goto uld_st;
9815     case M_ULD_A:
9816       ab = 1;
9817     case M_ULD:
9818       s = "ldl";
9819       s2 = "ldr";
9820       off12 = mips_opts.micromips;
9821       off = 7;
9822       goto uld_st;
9823     case M_USH_A:
9824       ab = 1;
9825     case M_USH:
9826       s = "sb";
9827       s2 = "sb";
9828       off = 1;
9829       ust = 1;
9830       goto uld_st;
9831     case M_USW_A:
9832       ab = 1;
9833     case M_USW:
9834       s = "swl";
9835       s2 = "swr";
9836       off12 = mips_opts.micromips;
9837       off = 3;
9838       ust = 1;
9839       goto uld_st;
9840     case M_USD_A:
9841       ab = 1;
9842     case M_USD:
9843       s = "sdl";
9844       s2 = "sdr";
9845       off12 = mips_opts.micromips;
9846       off = 7;
9847       ust = 1;
9848
9849     uld_st:
9850       if (!ab && offset_expr.X_add_number >= 0x8000 - off)
9851         as_bad (_("Operand overflow"));
9852
9853       ep = &offset_expr;
9854       expr1.X_add_number = 0;
9855       if (ab)
9856         {
9857           used_at = 1;
9858           tempreg = AT;
9859           load_address (tempreg, ep, &used_at);
9860           if (breg != 0)
9861             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9862                          tempreg, tempreg, breg);
9863           breg = tempreg;
9864           tempreg = treg;
9865           ep = &expr1;
9866         }
9867       else if (off12
9868                && (offset_expr.X_op != O_constant
9869                    || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
9870                    || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
9871         {
9872           used_at = 1;
9873           tempreg = AT;
9874           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
9875                        -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
9876           breg = tempreg;
9877           tempreg = treg;
9878           ep = &expr1;
9879         }
9880       else if (!ust && treg == breg)
9881         {
9882           used_at = 1;
9883           tempreg = AT;
9884         }
9885       else
9886         tempreg = treg;
9887
9888       if (off == 1)
9889         goto ulh_sh;
9890
9891       if (!target_big_endian)
9892         ep->X_add_number += off;
9893       if (!off12)
9894         macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9895       else
9896         macro_build (NULL, s, "t,~(b)",
9897                      tempreg, (unsigned long) ep->X_add_number, breg);
9898
9899       if (!target_big_endian)
9900         ep->X_add_number -= off;
9901       else
9902         ep->X_add_number += off;
9903       if (!off12)
9904         macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9905       else
9906         macro_build (NULL, s2, "t,~(b)",
9907                      tempreg, (unsigned long) ep->X_add_number, breg);
9908
9909       /* If necessary, move the result in tempreg to the final destination.  */
9910       if (!ust && treg != tempreg)
9911         {
9912           /* Protect second load's delay slot.  */
9913           load_delay_nop ();
9914           move_register (treg, tempreg);
9915         }
9916       break;
9917
9918     ulh_sh:
9919       used_at = 1;
9920       if (target_big_endian == ust)
9921         ep->X_add_number += off;
9922       tempreg = ust || ab ? treg : AT;
9923       macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9924
9925       /* For halfword transfers we need a temporary register to shuffle
9926          bytes.  Unfortunately for M_USH_A we have none available before
9927          the next store as AT holds the base address.  We deal with this
9928          case by clobbering TREG and then restoring it as with ULH.  */
9929       tempreg = ust == ab ? treg : AT;
9930       if (ust)
9931         macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
9932
9933       if (target_big_endian == ust)
9934         ep->X_add_number -= off;
9935       else
9936         ep->X_add_number += off;
9937       macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
9938
9939       /* For M_USH_A re-retrieve the LSB.  */
9940       if (ust && ab)
9941         {
9942           if (target_big_endian)
9943             ep->X_add_number += off;
9944           else
9945             ep->X_add_number -= off;
9946           macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
9947         }
9948       /* For ULH and M_USH_A OR the LSB in.  */
9949       if (!ust || ab)
9950         {
9951           tempreg = !ab ? AT : treg;
9952           macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
9953           macro_build (NULL, "or", "d,v,t", treg, treg, AT);
9954         }
9955       break;
9956
9957     default:
9958       /* FIXME: Check if this is one of the itbl macros, since they
9959          are added dynamically.  */
9960       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
9961       break;
9962     }
9963   if (!mips_opts.at && used_at)
9964     as_bad (_("Macro used $at after \".set noat\""));
9965 }
9966
9967 /* Implement macros in mips16 mode.  */
9968
9969 static void
9970 mips16_macro (struct mips_cl_insn *ip)
9971 {
9972   int mask;
9973   int xreg, yreg, zreg, tmp;
9974   expressionS expr1;
9975   int dbl;
9976   const char *s, *s2, *s3;
9977
9978   mask = ip->insn_mo->mask;
9979
9980   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
9981   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
9982   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
9983
9984   expr1.X_op = O_constant;
9985   expr1.X_op_symbol = NULL;
9986   expr1.X_add_symbol = NULL;
9987   expr1.X_add_number = 1;
9988
9989   dbl = 0;
9990
9991   switch (mask)
9992     {
9993     default:
9994       internalError ();
9995
9996     case M_DDIV_3:
9997       dbl = 1;
9998     case M_DIV_3:
9999       s = "mflo";
10000       goto do_div3;
10001     case M_DREM_3:
10002       dbl = 1;
10003     case M_REM_3:
10004       s = "mfhi";
10005     do_div3:
10006       start_noreorder ();
10007       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
10008       expr1.X_add_number = 2;
10009       macro_build (&expr1, "bnez", "x,p", yreg);
10010       macro_build (NULL, "break", "6", 7);
10011
10012       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10013          since that causes an overflow.  We should do that as well,
10014          but I don't see how to do the comparisons without a temporary
10015          register.  */
10016       end_noreorder ();
10017       macro_build (NULL, s, "x", zreg);
10018       break;
10019
10020     case M_DIVU_3:
10021       s = "divu";
10022       s2 = "mflo";
10023       goto do_divu3;
10024     case M_REMU_3:
10025       s = "divu";
10026       s2 = "mfhi";
10027       goto do_divu3;
10028     case M_DDIVU_3:
10029       s = "ddivu";
10030       s2 = "mflo";
10031       goto do_divu3;
10032     case M_DREMU_3:
10033       s = "ddivu";
10034       s2 = "mfhi";
10035     do_divu3:
10036       start_noreorder ();
10037       macro_build (NULL, s, "0,x,y", xreg, yreg);
10038       expr1.X_add_number = 2;
10039       macro_build (&expr1, "bnez", "x,p", yreg);
10040       macro_build (NULL, "break", "6", 7);
10041       end_noreorder ();
10042       macro_build (NULL, s2, "x", zreg);
10043       break;
10044
10045     case M_DMUL:
10046       dbl = 1;
10047     case M_MUL:
10048       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10049       macro_build (NULL, "mflo", "x", zreg);
10050       break;
10051
10052     case M_DSUBU_I:
10053       dbl = 1;
10054       goto do_subu;
10055     case M_SUBU_I:
10056     do_subu:
10057       if (imm_expr.X_op != O_constant)
10058         as_bad (_("Unsupported large constant"));
10059       imm_expr.X_add_number = -imm_expr.X_add_number;
10060       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
10061       break;
10062
10063     case M_SUBU_I_2:
10064       if (imm_expr.X_op != O_constant)
10065         as_bad (_("Unsupported large constant"));
10066       imm_expr.X_add_number = -imm_expr.X_add_number;
10067       macro_build (&imm_expr, "addiu", "x,k", xreg);
10068       break;
10069
10070     case M_DSUBU_I_2:
10071       if (imm_expr.X_op != O_constant)
10072         as_bad (_("Unsupported large constant"));
10073       imm_expr.X_add_number = -imm_expr.X_add_number;
10074       macro_build (&imm_expr, "daddiu", "y,j", yreg);
10075       break;
10076
10077     case M_BEQ:
10078       s = "cmp";
10079       s2 = "bteqz";
10080       goto do_branch;
10081     case M_BNE:
10082       s = "cmp";
10083       s2 = "btnez";
10084       goto do_branch;
10085     case M_BLT:
10086       s = "slt";
10087       s2 = "btnez";
10088       goto do_branch;
10089     case M_BLTU:
10090       s = "sltu";
10091       s2 = "btnez";
10092       goto do_branch;
10093     case M_BLE:
10094       s = "slt";
10095       s2 = "bteqz";
10096       goto do_reverse_branch;
10097     case M_BLEU:
10098       s = "sltu";
10099       s2 = "bteqz";
10100       goto do_reverse_branch;
10101     case M_BGE:
10102       s = "slt";
10103       s2 = "bteqz";
10104       goto do_branch;
10105     case M_BGEU:
10106       s = "sltu";
10107       s2 = "bteqz";
10108       goto do_branch;
10109     case M_BGT:
10110       s = "slt";
10111       s2 = "btnez";
10112       goto do_reverse_branch;
10113     case M_BGTU:
10114       s = "sltu";
10115       s2 = "btnez";
10116
10117     do_reverse_branch:
10118       tmp = xreg;
10119       xreg = yreg;
10120       yreg = tmp;
10121
10122     do_branch:
10123       macro_build (NULL, s, "x,y", xreg, yreg);
10124       macro_build (&offset_expr, s2, "p");
10125       break;
10126
10127     case M_BEQ_I:
10128       s = "cmpi";
10129       s2 = "bteqz";
10130       s3 = "x,U";
10131       goto do_branch_i;
10132     case M_BNE_I:
10133       s = "cmpi";
10134       s2 = "btnez";
10135       s3 = "x,U";
10136       goto do_branch_i;
10137     case M_BLT_I:
10138       s = "slti";
10139       s2 = "btnez";
10140       s3 = "x,8";
10141       goto do_branch_i;
10142     case M_BLTU_I:
10143       s = "sltiu";
10144       s2 = "btnez";
10145       s3 = "x,8";
10146       goto do_branch_i;
10147     case M_BLE_I:
10148       s = "slti";
10149       s2 = "btnez";
10150       s3 = "x,8";
10151       goto do_addone_branch_i;
10152     case M_BLEU_I:
10153       s = "sltiu";
10154       s2 = "btnez";
10155       s3 = "x,8";
10156       goto do_addone_branch_i;
10157     case M_BGE_I:
10158       s = "slti";
10159       s2 = "bteqz";
10160       s3 = "x,8";
10161       goto do_branch_i;
10162     case M_BGEU_I:
10163       s = "sltiu";
10164       s2 = "bteqz";
10165       s3 = "x,8";
10166       goto do_branch_i;
10167     case M_BGT_I:
10168       s = "slti";
10169       s2 = "bteqz";
10170       s3 = "x,8";
10171       goto do_addone_branch_i;
10172     case M_BGTU_I:
10173       s = "sltiu";
10174       s2 = "bteqz";
10175       s3 = "x,8";
10176
10177     do_addone_branch_i:
10178       if (imm_expr.X_op != O_constant)
10179         as_bad (_("Unsupported large constant"));
10180       ++imm_expr.X_add_number;
10181
10182     do_branch_i:
10183       macro_build (&imm_expr, s, s3, xreg);
10184       macro_build (&offset_expr, s2, "p");
10185       break;
10186
10187     case M_ABS:
10188       expr1.X_add_number = 0;
10189       macro_build (&expr1, "slti", "x,8", yreg);
10190       if (xreg != yreg)
10191         move_register (xreg, yreg);
10192       expr1.X_add_number = 2;
10193       macro_build (&expr1, "bteqz", "p");
10194       macro_build (NULL, "neg", "x,w", xreg, xreg);
10195     }
10196 }
10197
10198 /* For consistency checking, verify that all bits are specified either
10199    by the match/mask part of the instruction definition, or by the
10200    operand list.  */
10201 static int
10202 validate_mips_insn (const struct mips_opcode *opc)
10203 {
10204   const char *p = opc->args;
10205   char c;
10206   unsigned long used_bits = opc->mask;
10207
10208   if ((used_bits & opc->match) != opc->match)
10209     {
10210       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10211               opc->name, opc->args);
10212       return 0;
10213     }
10214 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
10215   while (*p)
10216     switch (c = *p++)
10217       {
10218       case ',': break;
10219       case '(': break;
10220       case ')': break;
10221       case '+':
10222         switch (c = *p++)
10223           {
10224           case '1': USE_BITS (OP_MASK_UDI1,     OP_SH_UDI1);    break;
10225           case '2': USE_BITS (OP_MASK_UDI2,     OP_SH_UDI2);    break;
10226           case '3': USE_BITS (OP_MASK_UDI3,     OP_SH_UDI3);    break;
10227           case '4': USE_BITS (OP_MASK_UDI4,     OP_SH_UDI4);    break;
10228           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10229           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10230           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10231           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
10232                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
10233           case 'E': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
10234           case 'F': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
10235           case 'G': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10236           case 'H': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
10237           case 'I': break;
10238           case 't': USE_BITS (OP_MASK_RT,       OP_SH_RT);      break;
10239           case 'T': USE_BITS (OP_MASK_RT,       OP_SH_RT);
10240                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
10241           case 'x': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10242           case 'X': USE_BITS (OP_MASK_BBITIND,  OP_SH_BBITIND); break;
10243           case 'p': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10244           case 'P': USE_BITS (OP_MASK_CINSPOS,  OP_SH_CINSPOS); break;
10245           case 'Q': USE_BITS (OP_MASK_SEQI,     OP_SH_SEQI);    break;
10246           case 's': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10247           case 'S': USE_BITS (OP_MASK_CINSLM1,  OP_SH_CINSLM1); break;
10248           case 'z': USE_BITS (OP_MASK_RZ,       OP_SH_RZ);      break;
10249           case 'Z': USE_BITS (OP_MASK_FZ,       OP_SH_FZ);      break;
10250           case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10251           case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10252           case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
10253
10254           default:
10255             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10256                     c, opc->name, opc->args);
10257             return 0;
10258           }
10259         break;
10260       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10261       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10262       case 'A': break;
10263       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
10264       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
10265       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10266       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10267       case 'F': break;
10268       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10269       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
10270       case 'I': break;
10271       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
10272       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10273       case 'L': break;
10274       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
10275       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
10276       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
10277       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
10278                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10279       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
10280       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10281       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10282       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10283       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10284       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
10285       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
10286       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
10287       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
10288       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10289       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
10290       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10291       case 'f': break;
10292       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
10293       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10294       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10295       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
10296       case 'l': break;
10297       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10298       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
10299       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
10300       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10301       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10302       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10303       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
10304       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10305       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10306       case 'x': break;
10307       case 'z': break;
10308       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
10309       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
10310                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
10311       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
10312       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
10313       case '[': break;
10314       case ']': break;
10315       case '1': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
10316       case '2': USE_BITS (OP_MASK_BP,           OP_SH_BP);      break;
10317       case '3': USE_BITS (OP_MASK_SA3,          OP_SH_SA3);     break;
10318       case '4': USE_BITS (OP_MASK_SA4,          OP_SH_SA4);     break;
10319       case '5': USE_BITS (OP_MASK_IMM8,         OP_SH_IMM8);    break;
10320       case '6': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
10321       case '7': USE_BITS (OP_MASK_DSPACC,       OP_SH_DSPACC);  break;
10322       case '8': USE_BITS (OP_MASK_WRDSP,        OP_SH_WRDSP);   break;
10323       case '9': USE_BITS (OP_MASK_DSPACC_S,     OP_SH_DSPACC_S);break;
10324       case '0': USE_BITS (OP_MASK_DSPSFT,       OP_SH_DSPSFT);  break;
10325       case '\'': USE_BITS (OP_MASK_RDDSP,       OP_SH_RDDSP);   break;
10326       case ':': USE_BITS (OP_MASK_DSPSFT_7,     OP_SH_DSPSFT_7);break;
10327       case '@': USE_BITS (OP_MASK_IMM10,        OP_SH_IMM10);   break;
10328       case '!': USE_BITS (OP_MASK_MT_U,         OP_SH_MT_U);    break;
10329       case '$': USE_BITS (OP_MASK_MT_H,         OP_SH_MT_H);    break;
10330       case '*': USE_BITS (OP_MASK_MTACC_T,      OP_SH_MTACC_T); break;
10331       case '&': USE_BITS (OP_MASK_MTACC_D,      OP_SH_MTACC_D); break;
10332       case '\\': USE_BITS (OP_MASK_3BITPOS,     OP_SH_3BITPOS); break;
10333       case '~': USE_BITS (OP_MASK_OFFSET12,     OP_SH_OFFSET12); break;
10334       case 'g': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
10335       default:
10336         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10337                 c, opc->name, opc->args);
10338         return 0;
10339       }
10340 #undef USE_BITS
10341   if (used_bits != 0xffffffff)
10342     {
10343       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
10344               ~used_bits & 0xffffffff, opc->name, opc->args);
10345       return 0;
10346     }
10347   return 1;
10348 }
10349
10350 /* For consistency checking, verify that the length implied matches the
10351    major opcode and that all bits are specified either by the match/mask
10352    part of the instruction definition, or by the operand list.  */
10353
10354 static int
10355 validate_micromips_insn (const struct mips_opcode *opc)
10356 {
10357   unsigned long match = opc->match;
10358   unsigned long mask = opc->mask;
10359   const char *p = opc->args;
10360   unsigned long insn_bits;
10361   unsigned long used_bits;
10362   unsigned long major;
10363   unsigned int length;
10364   char e;
10365   char c;
10366
10367   if ((mask & match) != match)
10368     {
10369       as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
10370               opc->name, opc->args);
10371       return 0;
10372     }
10373   length = micromips_insn_length (opc);
10374   if (length != 2 && length != 4)
10375     {
10376       as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
10377                 "%s %s"), length, opc->name, opc->args);
10378       return 0;
10379     }
10380   major = match >> (10 + 8 * (length - 2));
10381   if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
10382       || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
10383     {
10384       as_bad (_("Internal error: bad microMIPS opcode "
10385                 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
10386       return 0;
10387     }
10388
10389   /* Shift piecewise to avoid an overflow where unsigned long is 32-bit.  */
10390   insn_bits = 1 << 4 * length;
10391   insn_bits <<= 4 * length;
10392   insn_bits -= 1;
10393   used_bits = mask;
10394 #define USE_BITS(field) \
10395   (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
10396   while (*p)
10397     switch (c = *p++)
10398       {
10399       case ',': break;
10400       case '(': break;
10401       case ')': break;
10402       case '+':
10403         e = c;
10404         switch (c = *p++)
10405           {
10406           case 'A': USE_BITS (EXTLSB);  break;
10407           case 'B': USE_BITS (INSMSB);  break;
10408           case 'C': USE_BITS (EXTMSBD); break;
10409           case 'D': USE_BITS (RS);      USE_BITS (SEL); break;
10410           case 'E': USE_BITS (EXTLSB);  break;
10411           case 'F': USE_BITS (INSMSB);  break;
10412           case 'G': USE_BITS (EXTMSBD); break;
10413           case 'H': USE_BITS (EXTMSBD); break;
10414           default:
10415             as_bad (_("Internal error: bad mips opcode "
10416                       "(unknown extension operand type `%c%c'): %s %s"),
10417                     e, c, opc->name, opc->args);
10418             return 0;
10419           }
10420         break;
10421       case 'm':
10422         e = c;
10423         switch (c = *p++)
10424           {
10425           case 'A': USE_BITS (IMMA);    break;
10426           case 'B': USE_BITS (IMMB);    break;
10427           case 'C': USE_BITS (IMMC);    break;
10428           case 'D': USE_BITS (IMMD);    break;
10429           case 'E': USE_BITS (IMME);    break;
10430           case 'F': USE_BITS (IMMF);    break;
10431           case 'G': USE_BITS (IMMG);    break;
10432           case 'H': USE_BITS (IMMH);    break;
10433           case 'I': USE_BITS (IMMI);    break;
10434           case 'J': USE_BITS (IMMJ);    break;
10435           case 'L': USE_BITS (IMML);    break;
10436           case 'M': USE_BITS (IMMM);    break;
10437           case 'N': USE_BITS (IMMN);    break;
10438           case 'O': USE_BITS (IMMO);    break;
10439           case 'P': USE_BITS (IMMP);    break;
10440           case 'Q': USE_BITS (IMMQ);    break;
10441           case 'U': USE_BITS (IMMU);    break;
10442           case 'W': USE_BITS (IMMW);    break;
10443           case 'X': USE_BITS (IMMX);    break;
10444           case 'Y': USE_BITS (IMMY);    break;
10445           case 'Z': break;
10446           case 'a': break;
10447           case 'b': USE_BITS (MB);      break;
10448           case 'c': USE_BITS (MC);      break;
10449           case 'd': USE_BITS (MD);      break;
10450           case 'e': USE_BITS (ME);      break;
10451           case 'f': USE_BITS (MF);      break;
10452           case 'g': USE_BITS (MG);      break;
10453           case 'h': USE_BITS (MH);      break;
10454           case 'i': USE_BITS (MI);      break;
10455           case 'j': USE_BITS (MJ);      break;
10456           case 'l': USE_BITS (ML);      break;
10457           case 'm': USE_BITS (MM);      break;
10458           case 'n': USE_BITS (MN);      break;
10459           case 'p': USE_BITS (MP);      break;
10460           case 'q': USE_BITS (MQ);      break;
10461           case 'r': break;
10462           case 's': break;
10463           case 't': break;
10464           case 'x': break;
10465           case 'y': break;
10466           case 'z': break;
10467           default:
10468             as_bad (_("Internal error: bad mips opcode "
10469                       "(unknown extension operand type `%c%c'): %s %s"),
10470                     e, c, opc->name, opc->args);
10471             return 0;
10472           }
10473         break;
10474       case '.': USE_BITS (OFFSET10);    break;
10475       case '1': USE_BITS (STYPE);       break;
10476       case '<': USE_BITS (SHAMT);       break;
10477       case '>': USE_BITS (SHAMT);       break;
10478       case 'B': USE_BITS (CODE10);      break;
10479       case 'C': USE_BITS (COPZ);        break;
10480       case 'D': USE_BITS (FD);          break;
10481       case 'E': USE_BITS (RT);          break;
10482       case 'G': USE_BITS (RS);          break;
10483       case 'H': USE_BITS (SEL);         break;
10484       case 'K': USE_BITS (RS);          break;
10485       case 'M': USE_BITS (CCC);         break;
10486       case 'N': USE_BITS (BCC);         break;
10487       case 'R': USE_BITS (FR);          break;
10488       case 'S': USE_BITS (FS);          break;
10489       case 'T': USE_BITS (FT);          break;
10490       case 'V': USE_BITS (FS);          break;
10491       case '\\': USE_BITS (3BITPOS);    break;
10492       case 'a': USE_BITS (TARGET);      break;
10493       case 'b': USE_BITS (RS);          break;
10494       case 'c': USE_BITS (CODE);        break;
10495       case 'd': USE_BITS (RD);          break;
10496       case 'h': USE_BITS (PREFX);       break;
10497       case 'i': USE_BITS (IMMEDIATE);   break;
10498       case 'j': USE_BITS (DELTA);       break;
10499       case 'k': USE_BITS (CACHE);       break;
10500       case 'n': USE_BITS (RT);          break;
10501       case 'o': USE_BITS (DELTA);       break;
10502       case 'p': USE_BITS (DELTA);       break;
10503       case 'q': USE_BITS (CODE2);       break;
10504       case 'r': USE_BITS (RS);          break;
10505       case 's': USE_BITS (RS);          break;
10506       case 't': USE_BITS (RT);          break;
10507       case 'u': USE_BITS (IMMEDIATE);   break;
10508       case 'v': USE_BITS (RS);          break;
10509       case 'w': USE_BITS (RT);          break;
10510       case 'y': USE_BITS (RS3);         break;
10511       case 'z': break;
10512       case '|': USE_BITS (TRAP);        break;
10513       case '~': USE_BITS (OFFSET12);    break;
10514       default:
10515         as_bad (_("Internal error: bad microMIPS opcode "
10516                   "(unknown operand type `%c'): %s %s"),
10517                 c, opc->name, opc->args);
10518         return 0;
10519       }
10520 #undef USE_BITS
10521   if (used_bits != insn_bits)
10522     {
10523       if (~used_bits & insn_bits)
10524         as_bad (_("Internal error: bad microMIPS opcode "
10525                   "(bits 0x%lx undefined): %s %s"),
10526                 ~used_bits & insn_bits, opc->name, opc->args);
10527       if (used_bits & ~insn_bits)
10528         as_bad (_("Internal error: bad microMIPS opcode "
10529                   "(bits 0x%lx defined): %s %s"),
10530                 used_bits & ~insn_bits, opc->name, opc->args);
10531       return 0;
10532     }
10533   return 1;
10534 }
10535
10536 /* UDI immediates.  */
10537 struct mips_immed {
10538   char          type;
10539   unsigned int  shift;
10540   unsigned long mask;
10541   const char *  desc;
10542 };
10543
10544 static const struct mips_immed mips_immed[] = {
10545   { '1',        OP_SH_UDI1,     OP_MASK_UDI1,           0},
10546   { '2',        OP_SH_UDI2,     OP_MASK_UDI2,           0},
10547   { '3',        OP_SH_UDI3,     OP_MASK_UDI3,           0},
10548   { '4',        OP_SH_UDI4,     OP_MASK_UDI4,           0},
10549   { 0,0,0,0 }
10550 };
10551
10552 /* Check whether an odd floating-point register is allowed.  */
10553 static int
10554 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
10555 {
10556   const char *s = insn->name;
10557
10558   if (insn->pinfo == INSN_MACRO)
10559     /* Let a macro pass, we'll catch it later when it is expanded.  */
10560     return 1;
10561
10562   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
10563     {
10564       /* Allow odd registers for single-precision ops.  */
10565       switch (insn->pinfo & (FP_S | FP_D))
10566         {
10567         case FP_S:
10568         case 0:
10569           return 1;     /* both single precision - ok */
10570         case FP_D:
10571           return 0;     /* both double precision - fail */
10572         default:
10573           break;
10574         }
10575
10576       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
10577       s = strchr (insn->name, '.');
10578       if (argnum == 2)
10579         s = s != NULL ? strchr (s + 1, '.') : NULL;
10580       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
10581     } 
10582
10583   /* Single-precision coprocessor loads and moves are OK too.  */
10584   if ((insn->pinfo & FP_S)
10585       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
10586                          | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
10587     return 1;
10588
10589   return 0;
10590 }
10591
10592 /* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
10593    taking bits from BIT up.  */
10594 static int
10595 expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
10596 {
10597   return (ep->X_op == O_constant
10598           && (ep->X_add_number & ((1 << bit) - 1)) == 0
10599           && ep->X_add_number >= min << bit
10600           && ep->X_add_number < max << bit);
10601 }
10602
10603 /* This routine assembles an instruction into its binary format.  As a
10604    side effect, it sets one of the global variables imm_reloc or
10605    offset_reloc to the type of relocation to do if one of the operands
10606    is an address expression.  */
10607
10608 static void
10609 mips_ip (char *str, struct mips_cl_insn *ip)
10610 {
10611   bfd_boolean wrong_delay_slot_insns = FALSE;
10612   bfd_boolean need_delay_slot_ok = TRUE;
10613   struct mips_opcode *firstinsn = NULL;
10614   const struct mips_opcode *past;
10615   struct hash_control *hash;
10616   char *s;
10617   const char *args;
10618   char c = 0;
10619   struct mips_opcode *insn;
10620   char *argsStart;
10621   unsigned int regno;
10622   unsigned int lastregno;
10623   unsigned int destregno = 0;
10624   unsigned int lastpos = 0;
10625   unsigned int limlo, limhi;
10626   char *s_reset;
10627   offsetT min_range, max_range;
10628   long opend;
10629   char *name;
10630   int argnum;
10631   unsigned int rtype;
10632   char *dot;
10633   long end;
10634
10635   insn_error = NULL;
10636
10637   if (mips_opts.micromips)
10638     {
10639       hash = micromips_op_hash;
10640       past = &micromips_opcodes[bfd_micromips_num_opcodes];
10641     }
10642   else
10643     {
10644       hash = op_hash;
10645       past = &mips_opcodes[NUMOPCODES];
10646     }
10647   forced_insn_length = 0;
10648   insn = NULL;
10649
10650   /* We first try to match an instruction up to a space or to the end.  */
10651   for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
10652     continue;
10653
10654   /* Make a copy of the instruction so that we can fiddle with it.  */
10655   name = alloca (end + 1);
10656   memcpy (name, str, end);
10657   name[end] = '\0';
10658
10659   for (;;)
10660     {
10661       insn = (struct mips_opcode *) hash_find (hash, name);
10662
10663       if (insn != NULL || !mips_opts.micromips)
10664         break;
10665       if (forced_insn_length)
10666         break;
10667
10668       /* See if there's an instruction size override suffix,
10669          either `16' or `32', at the end of the mnemonic proper,
10670          that defines the operation, i.e. before the first `.'
10671          character if any.  Strip it and retry.  */
10672       dot = strchr (name, '.');
10673       opend = dot != NULL ? dot - name : end;
10674       if (opend < 3)
10675         break;
10676       if (name[opend - 2] == '1' && name[opend - 1] == '6')
10677         forced_insn_length = 2;
10678       else if (name[opend - 2] == '3' && name[opend - 1] == '2')
10679         forced_insn_length = 4;
10680       else
10681         break;
10682       memcpy (name + opend - 2, name + opend, end - opend + 1);
10683     }
10684   if (insn == NULL)
10685     {
10686       insn_error = _("Unrecognized opcode");
10687       return;
10688     }
10689
10690   /* For microMIPS instructions placed in a fixed-length branch delay slot
10691      we make up to two passes over the relevant fragment of the opcode
10692      table.  First we try instructions that meet the delay slot's length
10693      requirement.  If none matched, then we retry with the remaining ones
10694      and if one matches, then we use it and then issue an appropriate
10695      warning later on.  */
10696   argsStart = s = str + end;
10697   for (;;)
10698     {
10699       bfd_boolean delay_slot_ok;
10700       bfd_boolean size_ok;
10701       bfd_boolean ok;
10702
10703       gas_assert (strcmp (insn->name, name) == 0);
10704
10705       ok = is_opcode_valid (insn);
10706       size_ok = is_size_valid (insn);
10707       delay_slot_ok = is_delay_slot_valid (insn);
10708       if (!delay_slot_ok && !wrong_delay_slot_insns)
10709         {
10710           firstinsn = insn;
10711           wrong_delay_slot_insns = TRUE;
10712         }
10713       if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
10714         {
10715           static char buf[256];
10716
10717           if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
10718             {
10719               ++insn;
10720               continue;
10721             }
10722           if (wrong_delay_slot_insns && need_delay_slot_ok)
10723             {
10724               gas_assert (firstinsn);
10725               need_delay_slot_ok = FALSE;
10726               past = insn + 1;
10727               insn = firstinsn;
10728               continue;
10729             }
10730
10731           if (insn_error)
10732             return;
10733
10734           if (!ok)
10735             sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
10736                      mips_cpu_info_from_arch (mips_opts.arch)->name,
10737                      mips_cpu_info_from_isa (mips_opts.isa)->name);
10738           else
10739             sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
10740                      8 * forced_insn_length);
10741           insn_error = buf;
10742
10743           return;
10744         }
10745
10746       create_insn (ip, insn);
10747       insn_error = NULL;
10748       argnum = 1;
10749       lastregno = 0xffffffff;
10750       for (args = insn->args;; ++args)
10751         {
10752           int is_mdmx;
10753
10754           s += strspn (s, " \t");
10755           is_mdmx = 0;
10756           switch (*args)
10757             {
10758             case '\0':          /* end of args */
10759               if (*s == '\0')
10760                 return;
10761               break;
10762
10763             case '2': /* DSP 2-bit unsigned immediate in bit 11.  */
10764               gas_assert (!mips_opts.micromips);
10765               my_getExpression (&imm_expr, s);
10766               check_absolute_expr (ip, &imm_expr);
10767               if ((unsigned long) imm_expr.X_add_number != 1
10768                   && (unsigned long) imm_expr.X_add_number != 3)
10769                 {
10770                   as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
10771                           (unsigned long) imm_expr.X_add_number);
10772                 }
10773               INSERT_OPERAND (0, BP, *ip, imm_expr.X_add_number);
10774               imm_expr.X_op = O_absent;
10775               s = expr_end;
10776               continue;
10777
10778             case '3': /* DSP 3-bit unsigned immediate in bit 21.  */
10779               gas_assert (!mips_opts.micromips);
10780               my_getExpression (&imm_expr, s);
10781               check_absolute_expr (ip, &imm_expr);
10782               if (imm_expr.X_add_number & ~OP_MASK_SA3)
10783                 {
10784                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
10785                           OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
10786                 }
10787               INSERT_OPERAND (0, SA3, *ip, imm_expr.X_add_number);
10788               imm_expr.X_op = O_absent;
10789               s = expr_end;
10790               continue;
10791
10792             case '4': /* DSP 4-bit unsigned immediate in bit 21.  */
10793               gas_assert (!mips_opts.micromips);
10794               my_getExpression (&imm_expr, s);
10795               check_absolute_expr (ip, &imm_expr);
10796               if (imm_expr.X_add_number & ~OP_MASK_SA4)
10797                 {
10798                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
10799                           OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
10800                 }
10801               INSERT_OPERAND (0, SA4, *ip, imm_expr.X_add_number);
10802               imm_expr.X_op = O_absent;
10803               s = expr_end;
10804               continue;
10805
10806             case '5': /* DSP 8-bit unsigned immediate in bit 16.  */
10807               gas_assert (!mips_opts.micromips);
10808               my_getExpression (&imm_expr, s);
10809               check_absolute_expr (ip, &imm_expr);
10810               if (imm_expr.X_add_number & ~OP_MASK_IMM8)
10811                 {
10812                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
10813                           OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
10814                 }
10815               INSERT_OPERAND (0, IMM8, *ip, imm_expr.X_add_number);
10816               imm_expr.X_op = O_absent;
10817               s = expr_end;
10818               continue;
10819
10820             case '6': /* DSP 5-bit unsigned immediate in bit 21.  */
10821               gas_assert (!mips_opts.micromips);
10822               my_getExpression (&imm_expr, s);
10823               check_absolute_expr (ip, &imm_expr);
10824               if (imm_expr.X_add_number & ~OP_MASK_RS)
10825                 {
10826                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
10827                           OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
10828                 }
10829               INSERT_OPERAND (0, RS, *ip, imm_expr.X_add_number);
10830               imm_expr.X_op = O_absent;
10831               s = expr_end;
10832               continue;
10833
10834             case '7': /* Four DSP accumulators in bits 11,12.  */
10835               gas_assert (!mips_opts.micromips);
10836               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
10837                   s[3] >= '0' && s[3] <= '3')
10838                 {
10839                   regno = s[3] - '0';
10840                   s += 4;
10841                   INSERT_OPERAND (0, DSPACC, *ip, regno);
10842                   continue;
10843                 }
10844               else
10845                 as_bad (_("Invalid dsp acc register"));
10846               break;
10847
10848             case '8': /* DSP 6-bit unsigned immediate in bit 11.  */
10849               gas_assert (!mips_opts.micromips);
10850               my_getExpression (&imm_expr, s);
10851               check_absolute_expr (ip, &imm_expr);
10852               if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
10853                 {
10854                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
10855                           OP_MASK_WRDSP,
10856                           (unsigned long) imm_expr.X_add_number);
10857                 }
10858               INSERT_OPERAND (0, WRDSP, *ip, imm_expr.X_add_number);
10859               imm_expr.X_op = O_absent;
10860               s = expr_end;
10861               continue;
10862
10863             case '9': /* Four DSP accumulators in bits 21,22.  */
10864               gas_assert (!mips_opts.micromips);
10865               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
10866                   s[3] >= '0' && s[3] <= '3')
10867                 {
10868                   regno = s[3] - '0';
10869                   s += 4;
10870                   INSERT_OPERAND (0, DSPACC_S, *ip, regno);
10871                   continue;
10872                 }
10873               else
10874                 as_bad (_("Invalid dsp acc register"));
10875               break;
10876
10877             case '0': /* DSP 6-bit signed immediate in bit 20.  */
10878               gas_assert (!mips_opts.micromips);
10879               my_getExpression (&imm_expr, s);
10880               check_absolute_expr (ip, &imm_expr);
10881               min_range = -((OP_MASK_DSPSFT + 1) >> 1);
10882               max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
10883               if (imm_expr.X_add_number < min_range ||
10884                   imm_expr.X_add_number > max_range)
10885                 {
10886                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
10887                           (long) min_range, (long) max_range,
10888                           (long) imm_expr.X_add_number);
10889                 }
10890               INSERT_OPERAND (0, DSPSFT, *ip, imm_expr.X_add_number);
10891               imm_expr.X_op = O_absent;
10892               s = expr_end;
10893               continue;
10894
10895             case '\'': /* DSP 6-bit unsigned immediate in bit 16.  */
10896               gas_assert (!mips_opts.micromips);
10897               my_getExpression (&imm_expr, s);
10898               check_absolute_expr (ip, &imm_expr);
10899               if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
10900                 {
10901                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
10902                           OP_MASK_RDDSP,
10903                           (unsigned long) imm_expr.X_add_number);
10904                 }
10905               INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
10906               imm_expr.X_op = O_absent;
10907               s = expr_end;
10908               continue;
10909
10910             case ':': /* DSP 7-bit signed immediate in bit 19.  */
10911               gas_assert (!mips_opts.micromips);
10912               my_getExpression (&imm_expr, s);
10913               check_absolute_expr (ip, &imm_expr);
10914               min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
10915               max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
10916               if (imm_expr.X_add_number < min_range ||
10917                   imm_expr.X_add_number > max_range)
10918                 {
10919                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
10920                           (long) min_range, (long) max_range,
10921                           (long) imm_expr.X_add_number);
10922                 }
10923               INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
10924               imm_expr.X_op = O_absent;
10925               s = expr_end;
10926               continue;
10927
10928             case '@': /* DSP 10-bit signed immediate in bit 16.  */
10929               gas_assert (!mips_opts.micromips);
10930               my_getExpression (&imm_expr, s);
10931               check_absolute_expr (ip, &imm_expr);
10932               min_range = -((OP_MASK_IMM10 + 1) >> 1);
10933               max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
10934               if (imm_expr.X_add_number < min_range ||
10935                   imm_expr.X_add_number > max_range)
10936                 {
10937                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
10938                           (long) min_range, (long) max_range,
10939                           (long) imm_expr.X_add_number);
10940                 }
10941               INSERT_OPERAND (0, IMM10, *ip, imm_expr.X_add_number);
10942               imm_expr.X_op = O_absent;
10943               s = expr_end;
10944               continue;
10945
10946             case '!': /* MT usermode flag bit.  */
10947               gas_assert (!mips_opts.micromips);
10948               my_getExpression (&imm_expr, s);
10949               check_absolute_expr (ip, &imm_expr);
10950               if (imm_expr.X_add_number & ~OP_MASK_MT_U)
10951                 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
10952                         (unsigned long) imm_expr.X_add_number);
10953               INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
10954               imm_expr.X_op = O_absent;
10955               s = expr_end;
10956               continue;
10957
10958             case '$': /* MT load high flag bit.  */
10959               gas_assert (!mips_opts.micromips);
10960               my_getExpression (&imm_expr, s);
10961               check_absolute_expr (ip, &imm_expr);
10962               if (imm_expr.X_add_number & ~OP_MASK_MT_H)
10963                 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
10964                         (unsigned long) imm_expr.X_add_number);
10965               INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
10966               imm_expr.X_op = O_absent;
10967               s = expr_end;
10968               continue;
10969
10970             case '*': /* Four DSP accumulators in bits 18,19.  */
10971               gas_assert (!mips_opts.micromips);
10972               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
10973                   s[3] >= '0' && s[3] <= '3')
10974                 {
10975                   regno = s[3] - '0';
10976                   s += 4;
10977                   INSERT_OPERAND (0, MTACC_T, *ip, regno);
10978                   continue;
10979                 }
10980               else
10981                 as_bad (_("Invalid dsp/smartmips acc register"));
10982               break;
10983
10984             case '&': /* Four DSP accumulators in bits 13,14.  */
10985               gas_assert (!mips_opts.micromips);
10986               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
10987                   s[3] >= '0' && s[3] <= '3')
10988                 {
10989                   regno = s[3] - '0';
10990                   s += 4;
10991                   INSERT_OPERAND (0, MTACC_D, *ip, regno);
10992                   continue;
10993                 }
10994               else
10995                 as_bad (_("Invalid dsp/smartmips acc register"));
10996               break;
10997
10998             case '\\':          /* 3-bit bit position.  */
10999               {
11000                 unsigned long mask = (mips_opts.micromips
11001                                       ? MICROMIPSOP_MASK_3BITPOS
11002                                       : OP_MASK_3BITPOS);
11003
11004                 my_getExpression (&imm_expr, s);
11005                 check_absolute_expr (ip, &imm_expr);
11006                 if ((unsigned long) imm_expr.X_add_number > mask)
11007                   as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11008                            ip->insn_mo->name,
11009                            mask, (unsigned long) imm_expr.X_add_number);
11010                 INSERT_OPERAND (mips_opts.micromips,
11011                                 3BITPOS, *ip, imm_expr.X_add_number);
11012                 imm_expr.X_op = O_absent;
11013                 s = expr_end;
11014               }
11015               continue;
11016
11017             case ',':
11018               ++argnum;
11019               if (*s++ == *args)
11020                 continue;
11021               s--;
11022               switch (*++args)
11023                 {
11024                 case 'r':
11025                 case 'v':
11026                   INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11027                   continue;
11028
11029                 case 'w':
11030                   INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11031                   continue;
11032
11033                 case 'W':
11034                   gas_assert (!mips_opts.micromips);
11035                   INSERT_OPERAND (0, FT, *ip, lastregno);
11036                   continue;
11037
11038                 case 'V':
11039                   INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11040                   continue;
11041                 }
11042               break;
11043
11044             case '(':
11045               /* Handle optional base register.
11046                  Either the base register is omitted or
11047                  we must have a left paren.  */
11048               /* This is dependent on the next operand specifier
11049                  is a base register specification.  */
11050               gas_assert (args[1] == 'b'
11051                           || (mips_opts.micromips
11052                               && args[1] == 'm'
11053                               && (args[2] == 'l' || args[2] == 'n'
11054                                   || args[2] == 's' || args[2] == 'a')));
11055               if (*s == '\0' && args[1] == 'b')
11056                 return;
11057               /* Fall through.  */
11058
11059             case ')':           /* These must match exactly.  */
11060               if (*s++ == *args)
11061                 continue;
11062               break;
11063
11064             case '[':           /* These must match exactly.  */
11065             case ']':
11066               gas_assert (!mips_opts.micromips);
11067               if (*s++ == *args)
11068                 continue;
11069               break;
11070
11071             case '+':           /* Opcode extension character.  */
11072               switch (*++args)
11073                 {
11074                 case '1':       /* UDI immediates.  */
11075                 case '2':
11076                 case '3':
11077                 case '4':
11078                   gas_assert (!mips_opts.micromips);
11079                   {
11080                     const struct mips_immed *imm = mips_immed;
11081
11082                     while (imm->type && imm->type != *args)
11083                       ++imm;
11084                     if (! imm->type)
11085                       internalError ();
11086                     my_getExpression (&imm_expr, s);
11087                     check_absolute_expr (ip, &imm_expr);
11088                     if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11089                       {
11090                         as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11091                                  imm->desc ? imm->desc : ip->insn_mo->name,
11092                                  (unsigned long) imm_expr.X_add_number,
11093                                  (unsigned long) imm_expr.X_add_number);
11094                         imm_expr.X_add_number &= imm->mask;
11095                       }
11096                     ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11097                                         << imm->shift);
11098                     imm_expr.X_op = O_absent;
11099                     s = expr_end;
11100                   }
11101                   continue;
11102
11103                 case 'A':               /* ins/ext position, becomes LSB.  */
11104                   limlo = 0;
11105                   limhi = 31;
11106                   goto do_lsb;
11107                 case 'E':
11108                   limlo = 32;
11109                   limhi = 63;
11110                   goto do_lsb;
11111                 do_lsb:
11112                   my_getExpression (&imm_expr, s);
11113                   check_absolute_expr (ip, &imm_expr);
11114                   if ((unsigned long) imm_expr.X_add_number < limlo
11115                       || (unsigned long) imm_expr.X_add_number > limhi)
11116                     {
11117                       as_bad (_("Improper position (%lu)"),
11118                               (unsigned long) imm_expr.X_add_number);
11119                       imm_expr.X_add_number = limlo;
11120                     }
11121                   lastpos = imm_expr.X_add_number;
11122                   INSERT_OPERAND (mips_opts.micromips,
11123                                   EXTLSB, *ip, imm_expr.X_add_number);
11124                   imm_expr.X_op = O_absent;
11125                   s = expr_end;
11126                   continue;
11127
11128                 case 'B':               /* ins size, becomes MSB.  */
11129                   limlo = 1;
11130                   limhi = 32;
11131                   goto do_msb;
11132                 case 'F':
11133                   limlo = 33;
11134                   limhi = 64;
11135                   goto do_msb;
11136                 do_msb:
11137                   my_getExpression (&imm_expr, s);
11138                   check_absolute_expr (ip, &imm_expr);
11139                   /* Check for negative input so that small negative numbers
11140                      will not succeed incorrectly.  The checks against
11141                      (pos+size) transitively check "size" itself,
11142                      assuming that "pos" is reasonable.  */
11143                   if ((long) imm_expr.X_add_number < 0
11144                       || ((unsigned long) imm_expr.X_add_number
11145                           + lastpos) < limlo
11146                       || ((unsigned long) imm_expr.X_add_number
11147                           + lastpos) > limhi)
11148                     {
11149                       as_bad (_("Improper insert size (%lu, position %lu)"),
11150                               (unsigned long) imm_expr.X_add_number,
11151                               (unsigned long) lastpos);
11152                       imm_expr.X_add_number = limlo - lastpos;
11153                     }
11154                   INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11155                                   lastpos + imm_expr.X_add_number - 1);
11156                   imm_expr.X_op = O_absent;
11157                   s = expr_end;
11158                   continue;
11159
11160                 case 'C':               /* ext size, becomes MSBD.  */
11161                   limlo = 1;
11162                   limhi = 32;
11163                   goto do_msbd;
11164                 case 'G':
11165                   limlo = 33;
11166                   limhi = 64;
11167                   goto do_msbd;
11168                 case 'H':
11169                   limlo = 33;
11170                   limhi = 64;
11171                   goto do_msbd;
11172                 do_msbd:
11173                   my_getExpression (&imm_expr, s);
11174                   check_absolute_expr (ip, &imm_expr);
11175                   /* Check for negative input so that small negative numbers
11176                      will not succeed incorrectly.  The checks against
11177                      (pos+size) transitively check "size" itself,
11178                      assuming that "pos" is reasonable.  */
11179                   if ((long) imm_expr.X_add_number < 0
11180                       || ((unsigned long) imm_expr.X_add_number
11181                           + lastpos) < limlo
11182                       || ((unsigned long) imm_expr.X_add_number
11183                           + lastpos) > limhi)
11184                     {
11185                       as_bad (_("Improper extract size (%lu, position %lu)"),
11186                               (unsigned long) imm_expr.X_add_number,
11187                               (unsigned long) lastpos);
11188                       imm_expr.X_add_number = limlo - lastpos;
11189                     }
11190                   INSERT_OPERAND (mips_opts.micromips,
11191                                   EXTMSBD, *ip, imm_expr.X_add_number - 1);
11192                   imm_expr.X_op = O_absent;
11193                   s = expr_end;
11194                   continue;
11195
11196                 case 'D':
11197                   /* +D is for disassembly only; never match.  */
11198                   break;
11199
11200                 case 'I':
11201                   /* "+I" is like "I", except that imm2_expr is used.  */
11202                   my_getExpression (&imm2_expr, s);
11203                   if (imm2_expr.X_op != O_big
11204                       && imm2_expr.X_op != O_constant)
11205                   insn_error = _("absolute expression required");
11206                   if (HAVE_32BIT_GPRS)
11207                     normalize_constant_expr (&imm2_expr);
11208                   s = expr_end;
11209                   continue;
11210
11211                 case 'T': /* Coprocessor register.  */
11212                   gas_assert (!mips_opts.micromips);
11213                   /* +T is for disassembly only; never match.  */
11214                   break;
11215
11216                 case 't': /* Coprocessor register number.  */
11217                   gas_assert (!mips_opts.micromips);
11218                   if (s[0] == '$' && ISDIGIT (s[1]))
11219                     {
11220                       ++s;
11221                       regno = 0;
11222                       do
11223                         {
11224                           regno *= 10;
11225                           regno += *s - '0';
11226                           ++s;
11227                         }
11228                       while (ISDIGIT (*s));
11229                       if (regno > 31)
11230                         as_bad (_("Invalid register number (%d)"), regno);
11231                       else
11232                         {
11233                           INSERT_OPERAND (0, RT, *ip, regno);
11234                           continue;
11235                         }
11236                     }
11237                   else
11238                     as_bad (_("Invalid coprocessor 0 register number"));
11239                   break;
11240
11241                 case 'x':
11242                   /* bbit[01] and bbit[01]32 bit index.  Give error if index
11243                      is not in the valid range.  */
11244                   gas_assert (!mips_opts.micromips);
11245                   my_getExpression (&imm_expr, s);
11246                   check_absolute_expr (ip, &imm_expr);
11247                   if ((unsigned) imm_expr.X_add_number > 31)
11248                     {
11249                       as_bad (_("Improper bit index (%lu)"),
11250                               (unsigned long) imm_expr.X_add_number);
11251                       imm_expr.X_add_number = 0;
11252                     }
11253                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
11254                   imm_expr.X_op = O_absent;
11255                   s = expr_end;
11256                   continue;
11257
11258                 case 'X':
11259                   /* bbit[01] bit index when bbit is used but we generate
11260                      bbit[01]32 because the index is over 32.  Move to the
11261                      next candidate if index is not in the valid range.  */
11262                   gas_assert (!mips_opts.micromips);
11263                   my_getExpression (&imm_expr, s);
11264                   check_absolute_expr (ip, &imm_expr);
11265                   if ((unsigned) imm_expr.X_add_number < 32
11266                       || (unsigned) imm_expr.X_add_number > 63)
11267                     break;
11268                   INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
11269                   imm_expr.X_op = O_absent;
11270                   s = expr_end;
11271                   continue;
11272
11273                 case 'p':
11274                   /* cins, cins32, exts and exts32 position field.  Give error
11275                      if it's not in the valid range.  */
11276                   gas_assert (!mips_opts.micromips);
11277                   my_getExpression (&imm_expr, s);
11278                   check_absolute_expr (ip, &imm_expr);
11279                   if ((unsigned) imm_expr.X_add_number > 31)
11280                     {
11281                       as_bad (_("Improper position (%lu)"),
11282                               (unsigned long) imm_expr.X_add_number);
11283                       imm_expr.X_add_number = 0;
11284                     }
11285                   /* Make the pos explicit to simplify +S.  */
11286                   lastpos = imm_expr.X_add_number + 32;
11287                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
11288                   imm_expr.X_op = O_absent;
11289                   s = expr_end;
11290                   continue;
11291
11292                 case 'P':
11293                   /* cins, cins32, exts and exts32 position field.  Move to
11294                      the next candidate if it's not in the valid range.  */
11295                   gas_assert (!mips_opts.micromips);
11296                   my_getExpression (&imm_expr, s);
11297                   check_absolute_expr (ip, &imm_expr);
11298                   if ((unsigned) imm_expr.X_add_number < 32
11299                       || (unsigned) imm_expr.X_add_number > 63)
11300                     break;
11301                   lastpos = imm_expr.X_add_number;
11302                   INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
11303                   imm_expr.X_op = O_absent;
11304                   s = expr_end;
11305                   continue;
11306
11307                 case 's':
11308                   /* cins and exts length-minus-one field.  */
11309                   gas_assert (!mips_opts.micromips);
11310                   my_getExpression (&imm_expr, s);
11311                   check_absolute_expr (ip, &imm_expr);
11312                   if ((unsigned long) imm_expr.X_add_number > 31)
11313                     {
11314                       as_bad (_("Improper size (%lu)"),
11315                               (unsigned long) imm_expr.X_add_number);
11316                       imm_expr.X_add_number = 0;
11317                     }
11318                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11319                   imm_expr.X_op = O_absent;
11320                   s = expr_end;
11321                   continue;
11322
11323                 case 'S':
11324                   /* cins32/exts32 and cins/exts aliasing cint32/exts32
11325                      length-minus-one field.  */
11326                   gas_assert (!mips_opts.micromips);
11327                   my_getExpression (&imm_expr, s);
11328                   check_absolute_expr (ip, &imm_expr);
11329                   if ((long) imm_expr.X_add_number < 0
11330                       || (unsigned long) imm_expr.X_add_number + lastpos > 63)
11331                     {
11332                       as_bad (_("Improper size (%lu)"),
11333                               (unsigned long) imm_expr.X_add_number);
11334                       imm_expr.X_add_number = 0;
11335                     }
11336                   INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
11337                   imm_expr.X_op = O_absent;
11338                   s = expr_end;
11339                   continue;
11340
11341                 case 'Q':
11342                   /* seqi/snei immediate field.  */
11343                   gas_assert (!mips_opts.micromips);
11344                   my_getExpression (&imm_expr, s);
11345                   check_absolute_expr (ip, &imm_expr);
11346                   if ((long) imm_expr.X_add_number < -512
11347                       || (long) imm_expr.X_add_number >= 512)
11348                     {
11349                       as_bad (_("Improper immediate (%ld)"),
11350                                (long) imm_expr.X_add_number);
11351                       imm_expr.X_add_number = 0;
11352                     }
11353                   INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
11354                   imm_expr.X_op = O_absent;
11355                   s = expr_end;
11356                   continue;
11357
11358                 case 'a': /* 8-bit signed offset in bit 6 */
11359                   gas_assert (!mips_opts.micromips);
11360                   my_getExpression (&imm_expr, s);
11361                   check_absolute_expr (ip, &imm_expr);
11362                   min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
11363                   max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
11364                   if (imm_expr.X_add_number < min_range
11365                       || imm_expr.X_add_number > max_range)
11366                     {
11367                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11368                               (long) min_range, (long) max_range,
11369                               (long) imm_expr.X_add_number);
11370                     }
11371                   INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
11372                   imm_expr.X_op = O_absent;
11373                   s = expr_end;
11374                   continue;
11375
11376                 case 'b': /* 8-bit signed offset in bit 3 */
11377                   gas_assert (!mips_opts.micromips);
11378                   my_getExpression (&imm_expr, s);
11379                   check_absolute_expr (ip, &imm_expr);
11380                   min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
11381                   max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
11382                   if (imm_expr.X_add_number < min_range
11383                       || imm_expr.X_add_number > max_range)
11384                     {
11385                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11386                               (long) min_range, (long) max_range,
11387                               (long) imm_expr.X_add_number);
11388                     }
11389                   INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
11390                   imm_expr.X_op = O_absent;
11391                   s = expr_end;
11392                   continue;
11393
11394                 case 'c': /* 9-bit signed offset in bit 6 */
11395                   gas_assert (!mips_opts.micromips);
11396                   my_getExpression (&imm_expr, s);
11397                   check_absolute_expr (ip, &imm_expr);
11398                   min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
11399                   max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
11400                   /* We check the offset range before adjusted.  */
11401                   min_range <<= 4;
11402                   max_range <<= 4;
11403                   if (imm_expr.X_add_number < min_range
11404                       || imm_expr.X_add_number > max_range)
11405                     {
11406                       as_bad (_("Offset not in range %ld..%ld (%ld)"),
11407                               (long) min_range, (long) max_range,
11408                               (long) imm_expr.X_add_number);
11409                     }
11410                   if (imm_expr.X_add_number & 0xf)
11411                     {
11412                       as_bad (_("Offset not 16 bytes alignment (%ld)"),
11413                               (long) imm_expr.X_add_number);
11414                     }
11415                   /* Right shift 4 bits to adjust the offset operand.  */
11416                   INSERT_OPERAND (0, OFFSET_C, *ip,
11417                                   imm_expr.X_add_number >> 4);
11418                   imm_expr.X_op = O_absent;
11419                   s = expr_end;
11420                   continue;
11421
11422                 case 'z':
11423                   gas_assert (!mips_opts.micromips);
11424                   if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
11425                     break;
11426                   if (regno == AT && mips_opts.at)
11427                     {
11428                       if (mips_opts.at == ATREG)
11429                         as_warn (_("used $at without \".set noat\""));
11430                       else
11431                         as_warn (_("used $%u with \".set at=$%u\""),
11432                                  regno, mips_opts.at);
11433                     }
11434                   INSERT_OPERAND (0, RZ, *ip, regno);
11435                   continue;
11436
11437                 case 'Z':
11438                   gas_assert (!mips_opts.micromips);
11439                   if (!reg_lookup (&s, RTYPE_FPU, &regno))
11440                     break;
11441                   INSERT_OPERAND (0, FZ, *ip, regno);
11442                   continue;
11443
11444                 default:
11445                   as_bad (_("Internal error: bad %s opcode "
11446                             "(unknown extension operand type `+%c'): %s %s"),
11447                           mips_opts.micromips ? "microMIPS" : "MIPS",
11448                           *args, insn->name, insn->args);
11449                   /* Further processing is fruitless.  */
11450                   return;
11451                 }
11452               break;
11453
11454             case '.':           /* 10-bit offset.  */
11455               gas_assert (mips_opts.micromips);
11456             case '~':           /* 12-bit offset.  */
11457               {
11458                 int shift = *args == '.' ? 9 : 11;
11459                 size_t i;
11460
11461                 /* Check whether there is only a single bracketed expression
11462                    left.  If so, it must be the base register and the
11463                    constant must be zero.  */
11464                 if (*s == '(' && strchr (s + 1, '(') == 0)
11465                   continue;
11466
11467                 /* If this value won't fit into the offset, then go find
11468                    a macro that will generate a 16- or 32-bit offset code
11469                    pattern.  */
11470                 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
11471                 if ((i == 0 && (imm_expr.X_op != O_constant
11472                                 || imm_expr.X_add_number >= 1 << shift
11473                                 || imm_expr.X_add_number < -1 << shift))
11474                     || i > 0)
11475                   {
11476                     imm_expr.X_op = O_absent;
11477                     break;
11478                   }
11479                 if (shift == 9)
11480                   INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
11481                 else
11482                   INSERT_OPERAND (mips_opts.micromips,
11483                                   OFFSET12, *ip, imm_expr.X_add_number);
11484                 imm_expr.X_op = O_absent;
11485                 s = expr_end;
11486               }
11487               continue;
11488
11489             case '<':           /* must be at least one digit */
11490               /*
11491                * According to the manual, if the shift amount is greater
11492                * than 31 or less than 0, then the shift amount should be
11493                * mod 32.  In reality the mips assembler issues an error.
11494                * We issue a warning and mask out all but the low 5 bits.
11495                */
11496               my_getExpression (&imm_expr, s);
11497               check_absolute_expr (ip, &imm_expr);
11498               if ((unsigned long) imm_expr.X_add_number > 31)
11499                 as_warn (_("Improper shift amount (%lu)"),
11500                          (unsigned long) imm_expr.X_add_number);
11501               INSERT_OPERAND (mips_opts.micromips,
11502                               SHAMT, *ip, imm_expr.X_add_number);
11503               imm_expr.X_op = O_absent;
11504               s = expr_end;
11505               continue;
11506
11507             case '>':           /* shift amount minus 32 */
11508               my_getExpression (&imm_expr, s);
11509               check_absolute_expr (ip, &imm_expr);
11510               if ((unsigned long) imm_expr.X_add_number < 32
11511                   || (unsigned long) imm_expr.X_add_number > 63)
11512                 break;
11513               INSERT_OPERAND (mips_opts.micromips,
11514                               SHAMT, *ip, imm_expr.X_add_number - 32);
11515               imm_expr.X_op = O_absent;
11516               s = expr_end;
11517               continue;
11518
11519             case 'k':           /* CACHE code.  */
11520             case 'h':           /* PREFX code.  */
11521             case '1':           /* SYNC type.  */
11522               my_getExpression (&imm_expr, s);
11523               check_absolute_expr (ip, &imm_expr);
11524               if ((unsigned long) imm_expr.X_add_number > 31)
11525                 as_warn (_("Invalid value for `%s' (%lu)"),
11526                          ip->insn_mo->name,
11527                          (unsigned long) imm_expr.X_add_number);
11528               switch (*args)
11529                 {
11530                 case 'k':
11531                   if (mips_fix_cn63xxp1
11532                       && !mips_opts.micromips
11533                       && strcmp ("pref", insn->name) == 0)
11534                     switch (imm_expr.X_add_number)
11535                       {
11536                       case 5:
11537                       case 25:
11538                       case 26:
11539                       case 27:
11540                       case 28:
11541                       case 29:
11542                       case 30:
11543                       case 31:  /* These are ok.  */
11544                         break;
11545
11546                       default:  /* The rest must be changed to 28.  */
11547                         imm_expr.X_add_number = 28;
11548                         break;
11549                       }
11550                   INSERT_OPERAND (mips_opts.micromips,
11551                                   CACHE, *ip, imm_expr.X_add_number);
11552                   break;
11553                 case 'h':
11554                   INSERT_OPERAND (mips_opts.micromips,
11555                                   PREFX, *ip, imm_expr.X_add_number);
11556                   break;
11557                 case '1':
11558                   INSERT_OPERAND (mips_opts.micromips,
11559                                   STYPE, *ip, imm_expr.X_add_number);
11560                   break;
11561                 }
11562               imm_expr.X_op = O_absent;
11563               s = expr_end;
11564               continue;
11565
11566             case 'c':           /* BREAK code.  */
11567               {
11568                 unsigned long mask = (mips_opts.micromips
11569                                       ? MICROMIPSOP_MASK_CODE
11570                                       : OP_MASK_CODE);
11571
11572                 my_getExpression (&imm_expr, s);
11573                 check_absolute_expr (ip, &imm_expr);
11574                 if ((unsigned long) imm_expr.X_add_number > mask)
11575                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11576                            ip->insn_mo->name,
11577                            mask, (unsigned long) imm_expr.X_add_number);
11578                 INSERT_OPERAND (mips_opts.micromips,
11579                                 CODE, *ip, imm_expr.X_add_number);
11580                 imm_expr.X_op = O_absent;
11581                 s = expr_end;
11582               }
11583               continue;
11584
11585             case 'q':           /* Lower BREAK code.  */
11586               {
11587                 unsigned long mask = (mips_opts.micromips
11588                                       ? MICROMIPSOP_MASK_CODE2
11589                                       : OP_MASK_CODE2);
11590
11591                 my_getExpression (&imm_expr, s);
11592                 check_absolute_expr (ip, &imm_expr);
11593                 if ((unsigned long) imm_expr.X_add_number > mask)
11594                   as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
11595                            ip->insn_mo->name,
11596                            mask, (unsigned long) imm_expr.X_add_number);
11597                 INSERT_OPERAND (mips_opts.micromips,
11598                                 CODE2, *ip, imm_expr.X_add_number);
11599                 imm_expr.X_op = O_absent;
11600                 s = expr_end;
11601               }
11602               continue;
11603
11604             case 'B':           /* 20- or 10-bit syscall/break/wait code.  */
11605               {
11606                 unsigned long mask = (mips_opts.micromips
11607                                       ? MICROMIPSOP_MASK_CODE10
11608                                       : OP_MASK_CODE20);
11609
11610                 my_getExpression (&imm_expr, s);
11611                 check_absolute_expr (ip, &imm_expr);
11612                 if ((unsigned long) imm_expr.X_add_number > mask)
11613                   as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11614                            ip->insn_mo->name,
11615                            mask, (unsigned long) imm_expr.X_add_number);
11616                 if (mips_opts.micromips)
11617                   INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
11618                 else
11619                   INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
11620                 imm_expr.X_op = O_absent;
11621                 s = expr_end;
11622               }
11623               continue;
11624
11625             case 'C':           /* 25- or 23-bit coprocessor code.  */
11626               {
11627                 unsigned long mask = (mips_opts.micromips
11628                                       ? MICROMIPSOP_MASK_COPZ
11629                                       : OP_MASK_COPZ);
11630
11631                 my_getExpression (&imm_expr, s);
11632                 check_absolute_expr (ip, &imm_expr);
11633                 if ((unsigned long) imm_expr.X_add_number > mask)
11634                   as_warn (_("Coproccesor code > %u bits (%lu)"),
11635                            mips_opts.micromips ? 23U : 25U,
11636                            (unsigned long) imm_expr.X_add_number);
11637                 INSERT_OPERAND (mips_opts.micromips,
11638                                 COPZ, *ip, imm_expr.X_add_number);
11639                 imm_expr.X_op = O_absent;
11640                 s = expr_end;
11641               }
11642               continue;
11643
11644             case 'J':           /* 19-bit WAIT code.  */
11645               gas_assert (!mips_opts.micromips);
11646               my_getExpression (&imm_expr, s);
11647               check_absolute_expr (ip, &imm_expr);
11648               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
11649                 {
11650                   as_warn (_("Illegal 19-bit code (%lu)"),
11651                            (unsigned long) imm_expr.X_add_number);
11652                   imm_expr.X_add_number &= OP_MASK_CODE19;
11653                 }
11654               INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
11655               imm_expr.X_op = O_absent;
11656               s = expr_end;
11657               continue;
11658
11659             case 'P':           /* Performance register.  */
11660               gas_assert (!mips_opts.micromips);
11661               my_getExpression (&imm_expr, s);
11662               check_absolute_expr (ip, &imm_expr);
11663               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
11664                 as_warn (_("Invalid performance register (%lu)"),
11665                          (unsigned long) imm_expr.X_add_number);
11666               INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
11667               imm_expr.X_op = O_absent;
11668               s = expr_end;
11669               continue;
11670
11671             case 'G':           /* Coprocessor destination register.  */
11672               {
11673                 unsigned long opcode = ip->insn_opcode;
11674                 unsigned long mask;
11675                 unsigned int types;
11676                 int cop0;
11677
11678                 if (mips_opts.micromips)
11679                   {
11680                     mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
11681                              | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
11682                              | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
11683                     opcode &= mask;
11684                     switch (opcode)
11685                       {
11686                       case 0x000000fc:                          /* mfc0  */
11687                       case 0x000002fc:                          /* mtc0  */
11688                       case 0x580000fc:                          /* dmfc0 */
11689                       case 0x580002fc:                          /* dmtc0 */
11690                         cop0 = 1;
11691                         break;
11692                       default:
11693                         cop0 = 0;
11694                         break;
11695                       }
11696                   }
11697                 else
11698                   {
11699                     opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
11700                     cop0 = opcode == OP_OP_COP0;
11701                   }
11702                 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
11703                 ok = reg_lookup (&s, types, &regno);
11704                 if (mips_opts.micromips)
11705                   INSERT_OPERAND (1, RS, *ip, regno);
11706                 else
11707                   INSERT_OPERAND (0, RD, *ip, regno);
11708                 if (ok)
11709                   {
11710                     lastregno = regno;
11711                     continue;
11712                   }
11713               }
11714               break;
11715
11716             case 'y':           /* ALNV.PS source register.  */
11717               gas_assert (mips_opts.micromips);
11718               goto do_reg;
11719             case 'x':           /* Ignore register name.  */
11720             case 'U':           /* Destination register (CLO/CLZ).  */
11721             case 'g':           /* Coprocessor destination register.  */
11722               gas_assert (!mips_opts.micromips);
11723             case 'b':           /* Base register.  */
11724             case 'd':           /* Destination register.  */
11725             case 's':           /* Source register.  */
11726             case 't':           /* Target register.  */
11727             case 'r':           /* Both target and source.  */
11728             case 'v':           /* Both dest and source.  */
11729             case 'w':           /* Both dest and target.  */
11730             case 'E':           /* Coprocessor target register.  */
11731             case 'K':           /* RDHWR destination register.  */
11732             case 'z':           /* Must be zero register.  */
11733             do_reg:
11734               s_reset = s;
11735               if (*args == 'E' || *args == 'K')
11736                 ok = reg_lookup (&s, RTYPE_NUM, &regno);
11737               else
11738                 {
11739                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
11740                   if (regno == AT && mips_opts.at)
11741                     {
11742                       if (mips_opts.at == ATREG)
11743                         as_warn (_("Used $at without \".set noat\""));
11744                       else
11745                         as_warn (_("Used $%u with \".set at=$%u\""),
11746                                  regno, mips_opts.at);
11747                     }
11748                 }
11749               if (ok)
11750                 {
11751                   c = *args;
11752                   if (*s == ' ')
11753                     ++s;
11754                   if (args[1] != *s)
11755                     {
11756                       if (c == 'r' || c == 'v' || c == 'w')
11757                         {
11758                           regno = lastregno;
11759                           s = s_reset;
11760                           ++args;
11761                         }
11762                     }
11763                   /* 'z' only matches $0.  */
11764                   if (c == 'z' && regno != 0)
11765                     break;
11766
11767                   if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
11768                     {
11769                       if (regno == lastregno)
11770                         {
11771                           insn_error
11772                             = _("Source and destination must be different");
11773                           continue;
11774                         }
11775                       if (regno == 31 && lastregno == 0xffffffff)
11776                         {
11777                           insn_error
11778                             = _("A destination register must be supplied");
11779                           continue;
11780                         }
11781                     }
11782                   /* Now that we have assembled one operand, we use the args
11783                      string to figure out where it goes in the instruction.  */
11784                   switch (c)
11785                     {
11786                     case 'r':
11787                     case 's':
11788                     case 'v':
11789                     case 'b':
11790                       INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
11791                       break;
11792
11793                     case 'K':
11794                       if (mips_opts.micromips)
11795                         INSERT_OPERAND (1, RS, *ip, regno);
11796                       else
11797                         INSERT_OPERAND (0, RD, *ip, regno);
11798                       break;
11799
11800                     case 'd':
11801                     case 'g':
11802                       INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
11803                       break;
11804
11805                     case 'U':
11806                       gas_assert (!mips_opts.micromips);
11807                       INSERT_OPERAND (0, RD, *ip, regno);
11808                       INSERT_OPERAND (0, RT, *ip, regno);
11809                       break;
11810
11811                     case 'w':
11812                     case 't':
11813                     case 'E':
11814                       INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
11815                       break;
11816
11817                     case 'y':
11818                       gas_assert (mips_opts.micromips);
11819                       INSERT_OPERAND (1, RS3, *ip, regno);
11820                       break;
11821
11822                     case 'x':
11823                       /* This case exists because on the r3000 trunc
11824                          expands into a macro which requires a gp
11825                          register.  On the r6000 or r4000 it is
11826                          assembled into a single instruction which
11827                          ignores the register.  Thus the insn version
11828                          is MIPS_ISA2 and uses 'x', and the macro
11829                          version is MIPS_ISA1 and uses 't'.  */
11830                       break;
11831
11832                     case 'z':
11833                       /* This case is for the div instruction, which
11834                          acts differently if the destination argument
11835                          is $0.  This only matches $0, and is checked
11836                          outside the switch.  */
11837                       break;
11838                     }
11839                   lastregno = regno;
11840                   continue;
11841                 }
11842               switch (*args++)
11843                 {
11844                 case 'r':
11845                 case 'v':
11846                   INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
11847                   continue;
11848
11849                 case 'w':
11850                   INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
11851                   continue;
11852                 }
11853               break;
11854
11855             case 'O':           /* MDMX alignment immediate constant.  */
11856               gas_assert (!mips_opts.micromips);
11857               my_getExpression (&imm_expr, s);
11858               check_absolute_expr (ip, &imm_expr);
11859               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
11860                 as_warn (_("Improper align amount (%ld), using low bits"),
11861                          (long) imm_expr.X_add_number);
11862               INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
11863               imm_expr.X_op = O_absent;
11864               s = expr_end;
11865               continue;
11866
11867             case 'Q':           /* MDMX vector, element sel, or const.  */
11868               if (s[0] != '$')
11869                 {
11870                   /* MDMX Immediate.  */
11871                   gas_assert (!mips_opts.micromips);
11872                   my_getExpression (&imm_expr, s);
11873                   check_absolute_expr (ip, &imm_expr);
11874                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
11875                     as_warn (_("Invalid MDMX Immediate (%ld)"),
11876                              (long) imm_expr.X_add_number);
11877                   INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
11878                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
11879                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
11880                   else
11881                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
11882                   imm_expr.X_op = O_absent;
11883                   s = expr_end;
11884                   continue;
11885                 }
11886               /* Not MDMX Immediate.  Fall through.  */
11887             case 'X':           /* MDMX destination register.  */
11888             case 'Y':           /* MDMX source register.  */
11889             case 'Z':           /* MDMX target register.  */
11890               is_mdmx = 1;
11891             case 'W':
11892               gas_assert (!mips_opts.micromips);
11893             case 'D':           /* Floating point destination register.  */
11894             case 'S':           /* Floating point source register.  */
11895             case 'T':           /* Floating point target register.  */
11896             case 'R':           /* Floating point source register.  */
11897             case 'V':
11898               rtype = RTYPE_FPU;
11899               if (is_mdmx
11900                   || (mips_opts.ase_mdmx
11901                       && (ip->insn_mo->pinfo & FP_D)
11902                       && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
11903                                                 | INSN_COPROC_MEMORY_DELAY
11904                                                 | INSN_LOAD_COPROC_DELAY
11905                                                 | INSN_LOAD_MEMORY_DELAY
11906                                                 | INSN_STORE_MEMORY))))
11907                 rtype |= RTYPE_VEC;
11908               s_reset = s;
11909               if (reg_lookup (&s, rtype, &regno))
11910                 {
11911                   if ((regno & 1) != 0
11912                       && HAVE_32BIT_FPRS
11913                       && !mips_oddfpreg_ok (ip->insn_mo, argnum))
11914                     as_warn (_("Float register should be even, was %d"),
11915                              regno);
11916
11917                   c = *args;
11918                   if (*s == ' ')
11919                     ++s;
11920                   if (args[1] != *s)
11921                     {
11922                       if (c == 'V' || c == 'W')
11923                         {
11924                           regno = lastregno;
11925                           s = s_reset;
11926                           ++args;
11927                         }
11928                     }
11929                   switch (c)
11930                     {
11931                     case 'D':
11932                     case 'X':
11933                       INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
11934                       break;
11935
11936                     case 'V':
11937                     case 'S':
11938                     case 'Y':
11939                       INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
11940                       break;
11941
11942                     case 'Q':
11943                       /* This is like 'Z', but also needs to fix the MDMX
11944                          vector/scalar select bits.  Note that the
11945                          scalar immediate case is handled above.  */
11946                       if (*s == '[')
11947                         {
11948                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
11949                           int max_el = (is_qh ? 3 : 7);
11950                           s++;
11951                           my_getExpression(&imm_expr, s);
11952                           check_absolute_expr (ip, &imm_expr);
11953                           s = expr_end;
11954                           if (imm_expr.X_add_number > max_el)
11955                             as_bad (_("Bad element selector %ld"),
11956                                     (long) imm_expr.X_add_number);
11957                           imm_expr.X_add_number &= max_el;
11958                           ip->insn_opcode |= (imm_expr.X_add_number
11959                                               << (OP_SH_VSEL +
11960                                                   (is_qh ? 2 : 1)));
11961                           imm_expr.X_op = O_absent;
11962                           if (*s != ']')
11963                             as_warn (_("Expecting ']' found '%s'"), s);
11964                           else
11965                             s++;
11966                         }
11967                       else
11968                         {
11969                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
11970                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
11971                                                 << OP_SH_VSEL);
11972                           else
11973                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
11974                                                 OP_SH_VSEL);
11975                         }
11976                       /* Fall through.  */
11977                     case 'W':
11978                     case 'T':
11979                     case 'Z':
11980                       INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
11981                       break;
11982
11983                     case 'R':
11984                       INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
11985                       break;
11986                     }
11987                   lastregno = regno;
11988                   continue;
11989                 }
11990
11991               switch (*args++)
11992                 {
11993                 case 'V':
11994                   INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
11995                   continue;
11996
11997                 case 'W':
11998                   INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
11999                   continue;
12000                 }
12001               break;
12002
12003             case 'I':
12004               my_getExpression (&imm_expr, s);
12005               if (imm_expr.X_op != O_big
12006                   && imm_expr.X_op != O_constant)
12007                 insn_error = _("absolute expression required");
12008               if (HAVE_32BIT_GPRS)
12009                 normalize_constant_expr (&imm_expr);
12010               s = expr_end;
12011               continue;
12012
12013             case 'A':
12014               my_getExpression (&offset_expr, s);
12015               normalize_address_expr (&offset_expr);
12016               *imm_reloc = BFD_RELOC_32;
12017               s = expr_end;
12018               continue;
12019
12020             case 'F':
12021             case 'L':
12022             case 'f':
12023             case 'l':
12024               {
12025                 int f64;
12026                 int using_gprs;
12027                 char *save_in;
12028                 char *err;
12029                 unsigned char temp[8];
12030                 int len;
12031                 unsigned int length;
12032                 segT seg;
12033                 subsegT subseg;
12034                 char *p;
12035
12036                 /* These only appear as the last operand in an
12037                    instruction, and every instruction that accepts
12038                    them in any variant accepts them in all variants.
12039                    This means we don't have to worry about backing out
12040                    any changes if the instruction does not match.
12041
12042                    The difference between them is the size of the
12043                    floating point constant and where it goes.  For 'F'
12044                    and 'L' the constant is 64 bits; for 'f' and 'l' it
12045                    is 32 bits.  Where the constant is placed is based
12046                    on how the MIPS assembler does things:
12047                     F -- .rdata
12048                     L -- .lit8
12049                     f -- immediate value
12050                     l -- .lit4
12051
12052                     The .lit4 and .lit8 sections are only used if
12053                     permitted by the -G argument.
12054
12055                     The code below needs to know whether the target register
12056                     is 32 or 64 bits wide.  It relies on the fact 'f' and
12057                     'F' are used with GPR-based instructions and 'l' and
12058                     'L' are used with FPR-based instructions.  */
12059
12060                 f64 = *args == 'F' || *args == 'L';
12061                 using_gprs = *args == 'F' || *args == 'f';
12062
12063                 save_in = input_line_pointer;
12064                 input_line_pointer = s;
12065                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12066                 length = len;
12067                 s = input_line_pointer;
12068                 input_line_pointer = save_in;
12069                 if (err != NULL && *err != '\0')
12070                   {
12071                     as_bad (_("Bad floating point constant: %s"), err);
12072                     memset (temp, '\0', sizeof temp);
12073                     length = f64 ? 8 : 4;
12074                   }
12075
12076                 gas_assert (length == (unsigned) (f64 ? 8 : 4));
12077
12078                 if (*args == 'f'
12079                     || (*args == 'l'
12080                         && (g_switch_value < 4
12081                             || (temp[0] == 0 && temp[1] == 0)
12082                             || (temp[2] == 0 && temp[3] == 0))))
12083                   {
12084                     imm_expr.X_op = O_constant;
12085                     if (!target_big_endian)
12086                       imm_expr.X_add_number = bfd_getl32 (temp);
12087                     else
12088                       imm_expr.X_add_number = bfd_getb32 (temp);
12089                   }
12090                 else if (length > 4
12091                          && !mips_disable_float_construction
12092                          /* Constants can only be constructed in GPRs and
12093                             copied to FPRs if the GPRs are at least as wide
12094                             as the FPRs.  Force the constant into memory if
12095                             we are using 64-bit FPRs but the GPRs are only
12096                             32 bits wide.  */
12097                          && (using_gprs
12098                              || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
12099                          && ((temp[0] == 0 && temp[1] == 0)
12100                              || (temp[2] == 0 && temp[3] == 0))
12101                          && ((temp[4] == 0 && temp[5] == 0)
12102                              || (temp[6] == 0 && temp[7] == 0)))
12103                   {
12104                     /* The value is simple enough to load with a couple of
12105                        instructions.  If using 32-bit registers, set
12106                        imm_expr to the high order 32 bits and offset_expr to
12107                        the low order 32 bits.  Otherwise, set imm_expr to
12108                        the entire 64 bit constant.  */
12109                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
12110                       {
12111                         imm_expr.X_op = O_constant;
12112                         offset_expr.X_op = O_constant;
12113                         if (!target_big_endian)
12114                           {
12115                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
12116                             offset_expr.X_add_number = bfd_getl32 (temp);
12117                           }
12118                         else
12119                           {
12120                             imm_expr.X_add_number = bfd_getb32 (temp);
12121                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
12122                           }
12123                         if (offset_expr.X_add_number == 0)
12124                           offset_expr.X_op = O_absent;
12125                       }
12126                     else if (sizeof (imm_expr.X_add_number) > 4)
12127                       {
12128                         imm_expr.X_op = O_constant;
12129                         if (!target_big_endian)
12130                           imm_expr.X_add_number = bfd_getl64 (temp);
12131                         else
12132                           imm_expr.X_add_number = bfd_getb64 (temp);
12133                       }
12134                     else
12135                       {
12136                         imm_expr.X_op = O_big;
12137                         imm_expr.X_add_number = 4;
12138                         if (!target_big_endian)
12139                           {
12140                             generic_bignum[0] = bfd_getl16 (temp);
12141                             generic_bignum[1] = bfd_getl16 (temp + 2);
12142                             generic_bignum[2] = bfd_getl16 (temp + 4);
12143                             generic_bignum[3] = bfd_getl16 (temp + 6);
12144                           }
12145                         else
12146                           {
12147                             generic_bignum[0] = bfd_getb16 (temp + 6);
12148                             generic_bignum[1] = bfd_getb16 (temp + 4);
12149                             generic_bignum[2] = bfd_getb16 (temp + 2);
12150                             generic_bignum[3] = bfd_getb16 (temp);
12151                           }
12152                       }
12153                   }
12154                 else
12155                   {
12156                     const char *newname;
12157                     segT new_seg;
12158
12159                     /* Switch to the right section.  */
12160                     seg = now_seg;
12161                     subseg = now_subseg;
12162                     switch (*args)
12163                       {
12164                       default: /* unused default case avoids warnings.  */
12165                       case 'L':
12166                         newname = RDATA_SECTION_NAME;
12167                         if (g_switch_value >= 8)
12168                           newname = ".lit8";
12169                         break;
12170                       case 'F':
12171                         newname = RDATA_SECTION_NAME;
12172                         break;
12173                       case 'l':
12174                         gas_assert (g_switch_value >= 4);
12175                         newname = ".lit4";
12176                         break;
12177                       }
12178                     new_seg = subseg_new (newname, (subsegT) 0);
12179                     if (IS_ELF)
12180                       bfd_set_section_flags (stdoutput, new_seg,
12181                                              (SEC_ALLOC
12182                                               | SEC_LOAD
12183                                               | SEC_READONLY
12184                                               | SEC_DATA));
12185                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
12186                     if (IS_ELF && strncmp (TARGET_OS, "elf", 3) != 0)
12187                       record_alignment (new_seg, 4);
12188                     else
12189                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
12190                     if (seg == now_seg)
12191                       as_bad (_("Can't use floating point insn in this section"));
12192
12193                     /* Set the argument to the current address in the
12194                        section.  */
12195                     offset_expr.X_op = O_symbol;
12196                     offset_expr.X_add_symbol = symbol_temp_new_now ();
12197                     offset_expr.X_add_number = 0;
12198
12199                     /* Put the floating point number into the section.  */
12200                     p = frag_more ((int) length);
12201                     memcpy (p, temp, length);
12202
12203                     /* Switch back to the original section.  */
12204                     subseg_set (seg, subseg);
12205                   }
12206               }
12207               continue;
12208
12209             case 'i':           /* 16-bit unsigned immediate.  */
12210             case 'j':           /* 16-bit signed immediate.  */
12211               *imm_reloc = BFD_RELOC_LO16;
12212               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12213                 {
12214                   int more;
12215                   offsetT minval, maxval;
12216
12217                   more = (insn + 1 < past
12218                           && strcmp (insn->name, insn[1].name) == 0);
12219
12220                   /* If the expression was written as an unsigned number,
12221                      only treat it as signed if there are no more
12222                      alternatives.  */
12223                   if (more
12224                       && *args == 'j'
12225                       && sizeof (imm_expr.X_add_number) <= 4
12226                       && imm_expr.X_op == O_constant
12227                       && imm_expr.X_add_number < 0
12228                       && imm_expr.X_unsigned
12229                       && HAVE_64BIT_GPRS)
12230                     break;
12231
12232                   /* For compatibility with older assemblers, we accept
12233                      0x8000-0xffff as signed 16-bit numbers when only
12234                      signed numbers are allowed.  */
12235                   if (*args == 'i')
12236                     minval = 0, maxval = 0xffff;
12237                   else if (more)
12238                     minval = -0x8000, maxval = 0x7fff;
12239                   else
12240                     minval = -0x8000, maxval = 0xffff;
12241
12242                   if (imm_expr.X_op != O_constant
12243                       || imm_expr.X_add_number < minval
12244                       || imm_expr.X_add_number > maxval)
12245                     {
12246                       if (more)
12247                         break;
12248                       if (imm_expr.X_op == O_constant
12249                           || imm_expr.X_op == O_big)
12250                         as_bad (_("Expression out of range"));
12251                     }
12252                 }
12253               s = expr_end;
12254               continue;
12255
12256             case 'o':           /* 16-bit offset.  */
12257               offset_reloc[0] = BFD_RELOC_LO16;
12258               offset_reloc[1] = BFD_RELOC_UNUSED;
12259               offset_reloc[2] = BFD_RELOC_UNUSED;
12260
12261               /* Check whether there is only a single bracketed expression
12262                  left.  If so, it must be the base register and the
12263                  constant must be zero.  */
12264               if (*s == '(' && strchr (s + 1, '(') == 0)
12265                 {
12266                   offset_expr.X_op = O_constant;
12267                   offset_expr.X_add_number = 0;
12268                   continue;
12269                 }
12270
12271               /* If this value won't fit into a 16 bit offset, then go
12272                  find a macro that will generate the 32 bit offset
12273                  code pattern.  */
12274               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
12275                   && (offset_expr.X_op != O_constant
12276                       || offset_expr.X_add_number >= 0x8000
12277                       || offset_expr.X_add_number < -0x8000))
12278                 break;
12279
12280               s = expr_end;
12281               continue;
12282
12283             case 'p':           /* PC-relative offset.  */
12284               *offset_reloc = BFD_RELOC_16_PCREL_S2;
12285               my_getExpression (&offset_expr, s);
12286               s = expr_end;
12287               continue;
12288
12289             case 'u':           /* Upper 16 bits.  */
12290               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
12291                   && imm_expr.X_op == O_constant
12292                   && (imm_expr.X_add_number < 0
12293                       || imm_expr.X_add_number >= 0x10000))
12294                 as_bad (_("lui expression (%lu) not in range 0..65535"),
12295                         (unsigned long) imm_expr.X_add_number);
12296               s = expr_end;
12297               continue;
12298
12299             case 'a':           /* 26-bit address.  */
12300               *offset_reloc = BFD_RELOC_MIPS_JMP;
12301               my_getExpression (&offset_expr, s);
12302               s = expr_end;
12303               continue;
12304
12305             case 'N':           /* 3-bit branch condition code.  */
12306             case 'M':           /* 3-bit compare condition code.  */
12307               rtype = RTYPE_CCC;
12308               if (ip->insn_mo->pinfo & (FP_D | FP_S))
12309                 rtype |= RTYPE_FCC;
12310               if (!reg_lookup (&s, rtype, &regno))
12311                 break;
12312               if ((strcmp (str + strlen (str) - 3, ".ps") == 0
12313                    || strcmp (str + strlen (str) - 5, "any2f") == 0
12314                    || strcmp (str + strlen (str) - 5, "any2t") == 0)
12315                   && (regno & 1) != 0)
12316                 as_warn (_("Condition code register should be even for %s, "
12317                            "was %d"),
12318                          str, regno);
12319               if ((strcmp (str + strlen (str) - 5, "any4f") == 0
12320                    || strcmp (str + strlen (str) - 5, "any4t") == 0)
12321                   && (regno & 3) != 0)
12322                 as_warn (_("Condition code register should be 0 or 4 for %s, "
12323                            "was %d"),
12324                          str, regno);
12325               if (*args == 'N')
12326                 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
12327               else
12328                 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
12329               continue;
12330
12331             case 'H':
12332               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
12333                 s += 2;
12334               if (ISDIGIT (*s))
12335                 {
12336                   c = 0;
12337                   do
12338                     {
12339                       c *= 10;
12340                       c += *s - '0';
12341                       ++s;
12342                     }
12343                   while (ISDIGIT (*s));
12344                 }
12345               else
12346                 c = 8; /* Invalid sel value.  */
12347
12348               if (c > 7)
12349                 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
12350               INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
12351               continue;
12352
12353             case 'e':
12354               gas_assert (!mips_opts.micromips);
12355               /* Must be at least one digit.  */
12356               my_getExpression (&imm_expr, s);
12357               check_absolute_expr (ip, &imm_expr);
12358
12359               if ((unsigned long) imm_expr.X_add_number
12360                   > (unsigned long) OP_MASK_VECBYTE)
12361                 {
12362                   as_bad (_("bad byte vector index (%ld)"),
12363                            (long) imm_expr.X_add_number);
12364                   imm_expr.X_add_number = 0;
12365                 }
12366
12367               INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
12368               imm_expr.X_op = O_absent;
12369               s = expr_end;
12370               continue;
12371
12372             case '%':
12373               gas_assert (!mips_opts.micromips);
12374               my_getExpression (&imm_expr, s);
12375               check_absolute_expr (ip, &imm_expr);
12376
12377               if ((unsigned long) imm_expr.X_add_number
12378                   > (unsigned long) OP_MASK_VECALIGN)
12379                 {
12380                   as_bad (_("bad byte vector index (%ld)"),
12381                            (long) imm_expr.X_add_number);
12382                   imm_expr.X_add_number = 0;
12383                 }
12384
12385               INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
12386               imm_expr.X_op = O_absent;
12387               s = expr_end;
12388               continue;
12389
12390             case 'm':           /* Opcode extension character.  */
12391               gas_assert (mips_opts.micromips);
12392               c = *++args;
12393               switch (c)
12394                 {
12395                 case 'r':
12396                   if (strncmp (s, "$pc", 3) == 0)
12397                     {
12398                       s += 3;
12399                       continue;
12400                     }
12401                   break;
12402
12403                 case 'a':
12404                 case 'b':
12405                 case 'c':
12406                 case 'd':
12407                 case 'e':
12408                 case 'f':
12409                 case 'g':
12410                 case 'h':
12411                 case 'i':
12412                 case 'j':
12413                 case 'l':
12414                 case 'm':
12415                 case 'n':
12416                 case 'p':
12417                 case 'q':
12418                 case 's':
12419                 case 't':
12420                 case 'x':
12421                 case 'y':
12422                 case 'z':
12423                   s_reset = s;
12424                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
12425                   if (regno == AT && mips_opts.at)
12426                     {
12427                       if (mips_opts.at == ATREG)
12428                         as_warn (_("Used $at without \".set noat\""));
12429                       else
12430                         as_warn (_("Used $%u with \".set at=$%u\""),
12431                                  regno, mips_opts.at);
12432                     }
12433                   if (!ok)
12434                     {
12435                       if (c == 'c')
12436                         {
12437                           gas_assert (args[1] == ',');
12438                           regno = lastregno;
12439                           ++args;
12440                         }
12441                       else if (c == 't')
12442                         {
12443                           gas_assert (args[1] == ',');
12444                           ++args;
12445                           continue;                     /* Nothing to do.  */
12446                         }
12447                       else
12448                         break;
12449                     }
12450
12451                   if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
12452                     {
12453                       if (regno == lastregno)
12454                         {
12455                           insn_error
12456                             = _("Source and destination must be different");
12457                           continue;
12458                         }
12459                       if (regno == 31 && lastregno == 0xffffffff)
12460                         {
12461                           insn_error
12462                             = _("A destination register must be supplied");
12463                           continue;
12464                         }
12465                     }
12466
12467                   if (*s == ' ')
12468                     ++s;
12469                   if (args[1] != *s)
12470                     {
12471                       if (c == 'e')
12472                         {
12473                           gas_assert (args[1] == ',');
12474                           regno = lastregno;
12475                           s = s_reset;
12476                           ++args;
12477                         }
12478                       else if (c == 't')
12479                         {
12480                           gas_assert (args[1] == ',');
12481                           s = s_reset;
12482                           ++args;
12483                           continue;                     /* Nothing to do.  */
12484                         }
12485                     }
12486
12487                   /* Make sure regno is the same as lastregno.  */
12488                   if (c == 't' && regno != lastregno)
12489                     break;
12490
12491                   /* Make sure regno is the same as destregno.  */
12492                   if (c == 'x' && regno != destregno)
12493                     break;
12494
12495                   /* We need to save regno, before regno maps to the
12496                      microMIPS register encoding.  */
12497                   lastregno = regno;
12498
12499                   if (c == 'f')
12500                     destregno = regno;
12501
12502                   switch (c)
12503                     {
12504                       case 'a':
12505                         if (regno != GP)
12506                           regno = ILLEGAL_REG;
12507                         break;
12508
12509                       case 'b':
12510                         regno = mips32_to_micromips_reg_b_map[regno];
12511                         break;
12512
12513                       case 'c':
12514                         regno = mips32_to_micromips_reg_c_map[regno];
12515                         break;
12516
12517                       case 'd':
12518                         regno = mips32_to_micromips_reg_d_map[regno];
12519                         break;
12520
12521                       case 'e':
12522                         regno = mips32_to_micromips_reg_e_map[regno];
12523                         break;
12524
12525                       case 'f':
12526                         regno = mips32_to_micromips_reg_f_map[regno];
12527                         break;
12528
12529                       case 'g':
12530                         regno = mips32_to_micromips_reg_g_map[regno];
12531                         break;
12532
12533                       case 'h':
12534                         regno = mips32_to_micromips_reg_h_map[regno];
12535                         break;
12536
12537                       case 'i':
12538                         switch (EXTRACT_OPERAND (1, MI, *ip))
12539                           {
12540                             case 4:
12541                               if (regno == 21)
12542                                 regno = 3;
12543                               else if (regno == 22)
12544                                 regno = 4;
12545                               else if (regno == 5)
12546                                 regno = 5;
12547                               else if (regno == 6)
12548                                 regno = 6;
12549                               else if (regno == 7)
12550                                 regno = 7;
12551                               else
12552                                 regno = ILLEGAL_REG;
12553                               break;
12554
12555                             case 5:
12556                               if (regno == 6)
12557                                 regno = 0;
12558                               else if (regno == 7)
12559                                 regno = 1;
12560                               else
12561                                 regno = ILLEGAL_REG;
12562                               break;
12563
12564                             case 6:
12565                               if (regno == 7)
12566                                 regno = 2;
12567                               else
12568                                 regno = ILLEGAL_REG;
12569                               break;
12570
12571                             default:
12572                               regno = ILLEGAL_REG;
12573                               break;
12574                           }
12575                         break;
12576
12577                       case 'l':
12578                         regno = mips32_to_micromips_reg_l_map[regno];
12579                         break;
12580
12581                       case 'm':
12582                         regno = mips32_to_micromips_reg_m_map[regno];
12583                         break;
12584
12585                       case 'n':
12586                         regno = mips32_to_micromips_reg_n_map[regno];
12587                         break;
12588
12589                       case 'q':
12590                         regno = mips32_to_micromips_reg_q_map[regno];
12591                         break;
12592
12593                       case 's':
12594                         if (regno != SP)
12595                           regno = ILLEGAL_REG;
12596                         break;
12597
12598                       case 'y':
12599                         if (regno != 31)
12600                           regno = ILLEGAL_REG;
12601                         break;
12602
12603                       case 'z':
12604                         if (regno != ZERO)
12605                           regno = ILLEGAL_REG;
12606                         break;
12607
12608                       case 'j': /* Do nothing.  */
12609                       case 'p':
12610                       case 't':
12611                       case 'x':
12612                         break;
12613
12614                       default:
12615                         internalError ();
12616                     }
12617
12618                   if (regno == ILLEGAL_REG)
12619                     break;
12620
12621                   switch (c)
12622                     {
12623                       case 'b':
12624                         INSERT_OPERAND (1, MB, *ip, regno);
12625                         break;
12626
12627                       case 'c':
12628                         INSERT_OPERAND (1, MC, *ip, regno);
12629                         break;
12630
12631                       case 'd':
12632                         INSERT_OPERAND (1, MD, *ip, regno);
12633                         break;
12634
12635                       case 'e':
12636                         INSERT_OPERAND (1, ME, *ip, regno);
12637                         break;
12638
12639                       case 'f':
12640                         INSERT_OPERAND (1, MF, *ip, regno);
12641                         break;
12642
12643                       case 'g':
12644                         INSERT_OPERAND (1, MG, *ip, regno);
12645                         break;
12646
12647                       case 'h':
12648                         INSERT_OPERAND (1, MH, *ip, regno);
12649                         break;
12650
12651                       case 'i':
12652                         INSERT_OPERAND (1, MI, *ip, regno);
12653                         break;
12654
12655                       case 'j':
12656                         INSERT_OPERAND (1, MJ, *ip, regno);
12657                         break;
12658
12659                       case 'l':
12660                         INSERT_OPERAND (1, ML, *ip, regno);
12661                         break;
12662
12663                       case 'm':
12664                         INSERT_OPERAND (1, MM, *ip, regno);
12665                         break;
12666
12667                       case 'n':
12668                         INSERT_OPERAND (1, MN, *ip, regno);
12669                         break;
12670
12671                       case 'p':
12672                         INSERT_OPERAND (1, MP, *ip, regno);
12673                         break;
12674
12675                       case 'q':
12676                         INSERT_OPERAND (1, MQ, *ip, regno);
12677                         break;
12678
12679                       case 'a': /* Do nothing.  */
12680                       case 's': /* Do nothing.  */
12681                       case 't': /* Do nothing.  */
12682                       case 'x': /* Do nothing.  */
12683                       case 'y': /* Do nothing.  */
12684                       case 'z': /* Do nothing.  */
12685                         break;
12686
12687                       default:
12688                         internalError ();
12689                     }
12690                   continue;
12691
12692                 case 'A':
12693                   {
12694                     bfd_reloc_code_real_type r[3];
12695                     expressionS ep;
12696                     int imm;
12697
12698                     /* Check whether there is only a single bracketed
12699                        expression left.  If so, it must be the base register
12700                        and the constant must be zero.  */
12701                     if (*s == '(' && strchr (s + 1, '(') == 0)
12702                       {
12703                         INSERT_OPERAND (1, IMMA, *ip, 0);
12704                         continue;
12705                       }
12706
12707                     if (my_getSmallExpression (&ep, r, s) > 0
12708                         || !expr_const_in_range (&ep, -64, 64, 2))
12709                       break;
12710
12711                     imm = ep.X_add_number >> 2;
12712                     INSERT_OPERAND (1, IMMA, *ip, imm);
12713                   }
12714                   s = expr_end;
12715                   continue;
12716
12717                 case 'B':
12718                   {
12719                     bfd_reloc_code_real_type r[3];
12720                     expressionS ep;
12721                     int imm;
12722
12723                     if (my_getSmallExpression (&ep, r, s) > 0
12724                         || ep.X_op != O_constant)
12725                       break;
12726
12727                     for (imm = 0; imm < 8; imm++)
12728                       if (micromips_imm_b_map[imm] == ep.X_add_number)
12729                         break;
12730                     if (imm >= 8)
12731                       break;
12732
12733                     INSERT_OPERAND (1, IMMB, *ip, imm);
12734                   }
12735                   s = expr_end;
12736                   continue;
12737
12738                 case 'C':
12739                   {
12740                     bfd_reloc_code_real_type r[3];
12741                     expressionS ep;
12742                     int imm;
12743
12744                     if (my_getSmallExpression (&ep, r, s) > 0
12745                         || ep.X_op != O_constant)
12746                       break;
12747
12748                     for (imm = 0; imm < 16; imm++)
12749                       if (micromips_imm_c_map[imm] == ep.X_add_number)
12750                         break;
12751                     if (imm >= 16)
12752                       break;
12753
12754                     INSERT_OPERAND (1, IMMC, *ip, imm);
12755                   }
12756                   s = expr_end;
12757                   continue;
12758
12759                 case 'D':       /* pc relative offset */
12760                 case 'E':       /* pc relative offset */
12761                   my_getExpression (&offset_expr, s);
12762                   if (offset_expr.X_op == O_register)
12763                     break;
12764
12765                   if (!forced_insn_length)
12766                     *offset_reloc = (int) BFD_RELOC_UNUSED + c;
12767                   else if (c == 'D')
12768                     *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
12769                   else
12770                     *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
12771                   s = expr_end;
12772                   continue;
12773
12774                 case 'F':
12775                   {
12776                     bfd_reloc_code_real_type r[3];
12777                     expressionS ep;
12778                     int imm;
12779
12780                     if (my_getSmallExpression (&ep, r, s) > 0
12781                         || !expr_const_in_range (&ep, 0, 16, 0))
12782                       break;
12783
12784                     imm = ep.X_add_number;
12785                     INSERT_OPERAND (1, IMMF, *ip, imm);
12786                   }
12787                   s = expr_end;
12788                   continue;
12789
12790                 case 'G':
12791                   {
12792                     bfd_reloc_code_real_type r[3];
12793                     expressionS ep;
12794                     int imm;
12795
12796                     /* Check whether there is only a single bracketed
12797                        expression left.  If so, it must be the base register
12798                        and the constant must be zero.  */
12799                     if (*s == '(' && strchr (s + 1, '(') == 0)
12800                       {
12801                         INSERT_OPERAND (1, IMMG, *ip, 0);
12802                         continue;
12803                       }
12804
12805                     if (my_getSmallExpression (&ep, r, s) > 0
12806                         || !expr_const_in_range (&ep, -1, 15, 0))
12807                       break;
12808
12809                     imm = ep.X_add_number & 15;
12810                     INSERT_OPERAND (1, IMMG, *ip, imm);
12811                   }
12812                   s = expr_end;
12813                   continue;
12814
12815                 case 'H':
12816                   {
12817                     bfd_reloc_code_real_type r[3];
12818                     expressionS ep;
12819                     int imm;
12820
12821                     /* Check whether there is only a single bracketed
12822                        expression left.  If so, it must be the base register
12823                        and the constant must be zero.  */
12824                     if (*s == '(' && strchr (s + 1, '(') == 0)
12825                       {
12826                         INSERT_OPERAND (1, IMMH, *ip, 0);
12827                         continue;
12828                       }
12829
12830                     if (my_getSmallExpression (&ep, r, s) > 0
12831                         || !expr_const_in_range (&ep, 0, 16, 1))
12832                       break;
12833
12834                     imm = ep.X_add_number >> 1;
12835                     INSERT_OPERAND (1, IMMH, *ip, imm);
12836                   }
12837                   s = expr_end;
12838                   continue;
12839
12840                 case 'I':
12841                   {
12842                     bfd_reloc_code_real_type r[3];
12843                     expressionS ep;
12844                     int imm;
12845
12846                     if (my_getSmallExpression (&ep, r, s) > 0
12847                         || !expr_const_in_range (&ep, -1, 127, 0))
12848                       break;
12849
12850                     imm = ep.X_add_number & 127;
12851                     INSERT_OPERAND (1, IMMI, *ip, imm);
12852                   }
12853                   s = expr_end;
12854                   continue;
12855
12856                 case 'J':
12857                   {
12858                     bfd_reloc_code_real_type r[3];
12859                     expressionS ep;
12860                     int imm;
12861
12862                     /* Check whether there is only a single bracketed
12863                        expression left.  If so, it must be the base register
12864                        and the constant must be zero.  */
12865                     if (*s == '(' && strchr (s + 1, '(') == 0)
12866                       {
12867                         INSERT_OPERAND (1, IMMJ, *ip, 0);
12868                         continue;
12869                       }
12870
12871                     if (my_getSmallExpression (&ep, r, s) > 0
12872                         || !expr_const_in_range (&ep, 0, 16, 2))
12873                       break;
12874
12875                     imm = ep.X_add_number >> 2;
12876                     INSERT_OPERAND (1, IMMJ, *ip, imm);
12877                   }
12878                   s = expr_end;
12879                   continue;
12880
12881                 case 'L':
12882                   {
12883                     bfd_reloc_code_real_type r[3];
12884                     expressionS ep;
12885                     int imm;
12886
12887                     /* Check whether there is only a single bracketed
12888                        expression left.  If so, it must be the base register
12889                        and the constant must be zero.  */
12890                     if (*s == '(' && strchr (s + 1, '(') == 0)
12891                       {
12892                         INSERT_OPERAND (1, IMML, *ip, 0);
12893                         continue;
12894                       }
12895
12896                     if (my_getSmallExpression (&ep, r, s) > 0
12897                         || !expr_const_in_range (&ep, 0, 16, 0))
12898                       break;
12899
12900                     imm = ep.X_add_number;
12901                     INSERT_OPERAND (1, IMML, *ip, imm);
12902                   }
12903                   s = expr_end;
12904                   continue;
12905
12906                 case 'M':
12907                   {
12908                     bfd_reloc_code_real_type r[3];
12909                     expressionS ep;
12910                     int imm;
12911
12912                     if (my_getSmallExpression (&ep, r, s) > 0
12913                         || !expr_const_in_range (&ep, 1, 9, 0))
12914                       break;
12915
12916                     imm = ep.X_add_number & 7;
12917                     INSERT_OPERAND (1, IMMM, *ip, imm);
12918                   }
12919                   s = expr_end;
12920                   continue;
12921
12922                 case 'N':       /* Register list for lwm and swm.  */
12923                   {
12924                     /* A comma-separated list of registers and/or
12925                        dash-separated contiguous ranges including
12926                        both ra and a set of one or more registers
12927                        starting at s0 up to s3 which have to be
12928                        consecutive, e.g.:
12929
12930                        s0, ra
12931                        s0, s1, ra, s2, s3
12932                        s0-s2, ra
12933
12934                        and any permutations of these.  */
12935                     unsigned int reglist;
12936                     int imm;
12937
12938                     if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
12939                       break;
12940
12941                     if ((reglist & 0xfff1ffff) != 0x80010000)
12942                       break;
12943
12944                     reglist = (reglist >> 17) & 7;
12945                     reglist += 1;
12946                     if ((reglist & -reglist) != reglist)
12947                       break;
12948
12949                     imm = ffs (reglist) - 1;
12950                     INSERT_OPERAND (1, IMMN, *ip, imm);
12951                   }
12952                   continue;
12953
12954                 case 'O':       /* sdbbp 4-bit code.  */
12955                   {
12956                     bfd_reloc_code_real_type r[3];
12957                     expressionS ep;
12958                     int imm;
12959
12960                     if (my_getSmallExpression (&ep, r, s) > 0
12961                         || !expr_const_in_range (&ep, 0, 16, 0))
12962                       break;
12963
12964                     imm = ep.X_add_number;
12965                     INSERT_OPERAND (1, IMMO, *ip, imm);
12966                   }
12967                   s = expr_end;
12968                   continue;
12969
12970                 case 'P':
12971                   {
12972                     bfd_reloc_code_real_type r[3];
12973                     expressionS ep;
12974                     int imm;
12975
12976                     if (my_getSmallExpression (&ep, r, s) > 0
12977                         || !expr_const_in_range (&ep, 0, 32, 2))
12978                       break;
12979
12980                     imm = ep.X_add_number >> 2;
12981                     INSERT_OPERAND (1, IMMP, *ip, imm);
12982                   }
12983                   s = expr_end;
12984                   continue;
12985
12986                 case 'Q':
12987                   {
12988                     bfd_reloc_code_real_type r[3];
12989                     expressionS ep;
12990                     int imm;
12991
12992                     if (my_getSmallExpression (&ep, r, s) > 0
12993                         || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
12994                       break;
12995
12996                     imm = ep.X_add_number >> 2;
12997                     INSERT_OPERAND (1, IMMQ, *ip, imm);
12998                   }
12999                   s = expr_end;
13000                   continue;
13001
13002                 case 'U':
13003                   {
13004                     bfd_reloc_code_real_type r[3];
13005                     expressionS ep;
13006                     int imm;
13007
13008                     /* Check whether there is only a single bracketed
13009                        expression left.  If so, it must be the base register
13010                        and the constant must be zero.  */
13011                     if (*s == '(' && strchr (s + 1, '(') == 0)
13012                       {
13013                         INSERT_OPERAND (1, IMMU, *ip, 0);
13014                         continue;
13015                       }
13016
13017                     if (my_getSmallExpression (&ep, r, s) > 0
13018                         || !expr_const_in_range (&ep, 0, 32, 2))
13019                       break;
13020
13021                     imm = ep.X_add_number >> 2;
13022                     INSERT_OPERAND (1, IMMU, *ip, imm);
13023                   }
13024                   s = expr_end;
13025                   continue;
13026
13027                 case 'W':
13028                   {
13029                     bfd_reloc_code_real_type r[3];
13030                     expressionS ep;
13031                     int imm;
13032
13033                     if (my_getSmallExpression (&ep, r, s) > 0
13034                         || !expr_const_in_range (&ep, 0, 64, 2))
13035                       break;
13036
13037                     imm = ep.X_add_number >> 2;
13038                     INSERT_OPERAND (1, IMMW, *ip, imm);
13039                   }
13040                   s = expr_end;
13041                   continue;
13042
13043                 case 'X':
13044                   {
13045                     bfd_reloc_code_real_type r[3];
13046                     expressionS ep;
13047                     int imm;
13048
13049                     if (my_getSmallExpression (&ep, r, s) > 0
13050                         || !expr_const_in_range (&ep, -8, 8, 0))
13051                       break;
13052
13053                     imm = ep.X_add_number;
13054                     INSERT_OPERAND (1, IMMX, *ip, imm);
13055                   }
13056                   s = expr_end;
13057                   continue;
13058
13059                 case 'Y':
13060                   {
13061                     bfd_reloc_code_real_type r[3];
13062                     expressionS ep;
13063                     int imm;
13064
13065                     if (my_getSmallExpression (&ep, r, s) > 0
13066                         || expr_const_in_range (&ep, -2, 2, 2)
13067                         || !expr_const_in_range (&ep, -258, 258, 2))
13068                       break;
13069
13070                     imm = ep.X_add_number >> 2;
13071                     imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13072                     INSERT_OPERAND (1, IMMY, *ip, imm);
13073                   }
13074                   s = expr_end;
13075                   continue;
13076
13077                 case 'Z':
13078                   {
13079                     bfd_reloc_code_real_type r[3];
13080                     expressionS ep;
13081
13082                     if (my_getSmallExpression (&ep, r, s) > 0
13083                         || !expr_const_in_range (&ep, 0, 1, 0))
13084                       break;
13085                   }
13086                   s = expr_end;
13087                   continue;
13088
13089                 default:
13090                   as_bad (_("Internal error: bad microMIPS opcode "
13091                             "(unknown extension operand type `m%c'): %s %s"),
13092                           *args, insn->name, insn->args);
13093                   /* Further processing is fruitless.  */
13094                   return;
13095                 }
13096               break;
13097
13098             case 'n':           /* Register list for 32-bit lwm and swm.  */
13099               gas_assert (mips_opts.micromips);
13100               {
13101                 /* A comma-separated list of registers and/or
13102                    dash-separated contiguous ranges including
13103                    at least one of ra and a set of one or more
13104                    registers starting at s0 up to s7 and then
13105                    s8 which have to be consecutive, e.g.:
13106
13107                    ra
13108                    s0
13109                    ra, s0, s1, s2
13110                    s0-s8
13111                    s0-s5, ra
13112
13113                    and any permutations of these.  */
13114                 unsigned int reglist;
13115                 int imm;
13116                 int ra;
13117
13118                 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13119                   break;
13120
13121                 if ((reglist & 0x3f00ffff) != 0)
13122                   break;
13123
13124                 ra = (reglist >> 27) & 0x10;
13125                 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13126                 reglist += 1;
13127                 if ((reglist & -reglist) != reglist)
13128                   break;
13129
13130                 imm = (ffs (reglist) - 1) | ra;
13131                 INSERT_OPERAND (1, RT, *ip, imm);
13132                 imm_expr.X_op = O_absent;
13133               }
13134               continue;
13135
13136             case '|':           /* 4-bit trap code.  */
13137               gas_assert (mips_opts.micromips);
13138               my_getExpression (&imm_expr, s);
13139               check_absolute_expr (ip, &imm_expr);
13140               if ((unsigned long) imm_expr.X_add_number
13141                   > MICROMIPSOP_MASK_TRAP)
13142                 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13143                         (unsigned long) imm_expr.X_add_number,
13144                         ip->insn_mo->name);
13145               INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
13146               imm_expr.X_op = O_absent;
13147               s = expr_end;
13148               continue;
13149
13150             default:
13151               as_bad (_("Bad char = '%c'\n"), *args);
13152               internalError ();
13153             }
13154           break;
13155         }
13156       /* Args don't match.  */
13157       s = argsStart;
13158       insn_error = _("Illegal operands");
13159       if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
13160         {
13161           ++insn;
13162           continue;
13163         }
13164       else if (wrong_delay_slot_insns && need_delay_slot_ok)
13165         {
13166           gas_assert (firstinsn);
13167           need_delay_slot_ok = FALSE;
13168           past = insn + 1;
13169           insn = firstinsn;
13170           continue;
13171         }
13172       return;
13173     }
13174 }
13175
13176 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13177
13178 /* This routine assembles an instruction into its binary format when
13179    assembling for the mips16.  As a side effect, it sets one of the
13180    global variables imm_reloc or offset_reloc to the type of relocation
13181    to do if one of the operands is an address expression.  It also sets
13182    forced_insn_length to the resulting instruction size in bytes if the
13183    user explicitly requested a small or extended instruction.  */
13184
13185 static void
13186 mips16_ip (char *str, struct mips_cl_insn *ip)
13187 {
13188   char *s;
13189   const char *args;
13190   struct mips_opcode *insn;
13191   char *argsstart;
13192   unsigned int regno;
13193   unsigned int lastregno = 0;
13194   char *s_reset;
13195   size_t i;
13196
13197   insn_error = NULL;
13198
13199   forced_insn_length = 0;
13200
13201   for (s = str; ISLOWER (*s); ++s)
13202     ;
13203   switch (*s)
13204     {
13205     case '\0':
13206       break;
13207
13208     case ' ':
13209       *s++ = '\0';
13210       break;
13211
13212     case '.':
13213       if (s[1] == 't' && s[2] == ' ')
13214         {
13215           *s = '\0';
13216           forced_insn_length = 2;
13217           s += 3;
13218           break;
13219         }
13220       else if (s[1] == 'e' && s[2] == ' ')
13221         {
13222           *s = '\0';
13223           forced_insn_length = 4;
13224           s += 3;
13225           break;
13226         }
13227       /* Fall through.  */
13228     default:
13229       insn_error = _("unknown opcode");
13230       return;
13231     }
13232
13233   if (mips_opts.noautoextend && !forced_insn_length)
13234     forced_insn_length = 2;
13235
13236   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13237     {
13238       insn_error = _("unrecognized opcode");
13239       return;
13240     }
13241
13242   argsstart = s;
13243   for (;;)
13244     {
13245       bfd_boolean ok;
13246
13247       gas_assert (strcmp (insn->name, str) == 0);
13248
13249       ok = is_opcode_valid_16 (insn);
13250       if (! ok)
13251         {
13252           if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13253               && strcmp (insn->name, insn[1].name) == 0)
13254             {
13255               ++insn;
13256               continue;
13257             }
13258           else
13259             {
13260               if (!insn_error)
13261                 {
13262                   static char buf[100];
13263                   sprintf (buf,
13264                            _("Opcode not supported on this processor: %s (%s)"),
13265                            mips_cpu_info_from_arch (mips_opts.arch)->name,
13266                            mips_cpu_info_from_isa (mips_opts.isa)->name);
13267                   insn_error = buf;
13268                 }
13269               return;
13270             }
13271         }
13272
13273       create_insn (ip, insn);
13274       imm_expr.X_op = O_absent;
13275       imm_reloc[0] = BFD_RELOC_UNUSED;
13276       imm_reloc[1] = BFD_RELOC_UNUSED;
13277       imm_reloc[2] = BFD_RELOC_UNUSED;
13278       imm2_expr.X_op = O_absent;
13279       offset_expr.X_op = O_absent;
13280       offset_reloc[0] = BFD_RELOC_UNUSED;
13281       offset_reloc[1] = BFD_RELOC_UNUSED;
13282       offset_reloc[2] = BFD_RELOC_UNUSED;
13283       for (args = insn->args; 1; ++args)
13284         {
13285           int c;
13286
13287           if (*s == ' ')
13288             ++s;
13289
13290           /* In this switch statement we call break if we did not find
13291              a match, continue if we did find a match, or return if we
13292              are done.  */
13293
13294           c = *args;
13295           switch (c)
13296             {
13297             case '\0':
13298               if (*s == '\0')
13299                 {
13300                   /* Stuff the immediate value in now, if we can.  */
13301                   if (imm_expr.X_op == O_constant
13302                       && *imm_reloc > BFD_RELOC_UNUSED
13303                       && *imm_reloc != BFD_RELOC_MIPS16_GOT16
13304                       && *imm_reloc != BFD_RELOC_MIPS16_CALL16
13305                       && insn->pinfo != INSN_MACRO)
13306                     {
13307                       valueT tmp;
13308
13309                       switch (*offset_reloc)
13310                         {
13311                           case BFD_RELOC_MIPS16_HI16_S:
13312                             tmp = (imm_expr.X_add_number + 0x8000) >> 16;
13313                             break;
13314
13315                           case BFD_RELOC_MIPS16_HI16:
13316                             tmp = imm_expr.X_add_number >> 16;
13317                             break;
13318
13319                           case BFD_RELOC_MIPS16_LO16:
13320                             tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
13321                                   - 0x8000;
13322                             break;
13323
13324                           case BFD_RELOC_UNUSED:
13325                             tmp = imm_expr.X_add_number;
13326                             break;
13327
13328                           default:
13329                             internalError ();
13330                         }
13331                       *offset_reloc = BFD_RELOC_UNUSED;
13332
13333                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
13334                                     tmp, TRUE, forced_insn_length == 2,
13335                                     forced_insn_length == 4, &ip->insn_opcode,
13336                                     &ip->use_extend, &ip->extend);
13337                       imm_expr.X_op = O_absent;
13338                       *imm_reloc = BFD_RELOC_UNUSED;
13339                     }
13340
13341                   return;
13342                 }
13343               break;
13344
13345             case ',':
13346               if (*s++ == c)
13347                 continue;
13348               s--;
13349               switch (*++args)
13350                 {
13351                 case 'v':
13352                   MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13353                   continue;
13354                 case 'w':
13355                   MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13356                   continue;
13357                 }
13358               break;
13359
13360             case '(':
13361             case ')':
13362               if (*s++ == c)
13363                 continue;
13364               break;
13365
13366             case 'v':
13367             case 'w':
13368               if (s[0] != '$')
13369                 {
13370                   if (c == 'v')
13371                     MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13372                   else
13373                     MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13374                   ++args;
13375                   continue;
13376                 }
13377               /* Fall through.  */
13378             case 'x':
13379             case 'y':
13380             case 'z':
13381             case 'Z':
13382             case '0':
13383             case 'S':
13384             case 'R':
13385             case 'X':
13386             case 'Y':
13387               s_reset = s;
13388               if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
13389                 {
13390                   if (c == 'v' || c == 'w')
13391                     {
13392                       if (c == 'v')
13393                         MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
13394                       else
13395                         MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
13396                       ++args;
13397                       continue;
13398                     }
13399                   break;
13400                 }
13401
13402               if (*s == ' ')
13403                 ++s;
13404               if (args[1] != *s)
13405                 {
13406                   if (c == 'v' || c == 'w')
13407                     {
13408                       regno = mips16_to_32_reg_map[lastregno];
13409                       s = s_reset;
13410                       ++args;
13411                     }
13412                 }
13413
13414               switch (c)
13415                 {
13416                 case 'x':
13417                 case 'y':
13418                 case 'z':
13419                 case 'v':
13420                 case 'w':
13421                 case 'Z':
13422                   regno = mips32_to_16_reg_map[regno];
13423                   break;
13424
13425                 case '0':
13426                   if (regno != 0)
13427                     regno = ILLEGAL_REG;
13428                   break;
13429
13430                 case 'S':
13431                   if (regno != SP)
13432                     regno = ILLEGAL_REG;
13433                   break;
13434
13435                 case 'R':
13436                   if (regno != RA)
13437                     regno = ILLEGAL_REG;
13438                   break;
13439
13440                 case 'X':
13441                 case 'Y':
13442                   if (regno == AT && mips_opts.at)
13443                     {
13444                       if (mips_opts.at == ATREG)
13445                         as_warn (_("used $at without \".set noat\""));
13446                       else
13447                         as_warn (_("used $%u with \".set at=$%u\""),
13448                                  regno, mips_opts.at);
13449                     }
13450                   break;
13451
13452                 default:
13453                   internalError ();
13454                 }
13455
13456               if (regno == ILLEGAL_REG)
13457                 break;
13458
13459               switch (c)
13460                 {
13461                 case 'x':
13462                 case 'v':
13463                   MIPS16_INSERT_OPERAND (RX, *ip, regno);
13464                   break;
13465                 case 'y':
13466                 case 'w':
13467                   MIPS16_INSERT_OPERAND (RY, *ip, regno);
13468                   break;
13469                 case 'z':
13470                   MIPS16_INSERT_OPERAND (RZ, *ip, regno);
13471                   break;
13472                 case 'Z':
13473                   MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
13474                 case '0':
13475                 case 'S':
13476                 case 'R':
13477                   break;
13478                 case 'X':
13479                   MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
13480                   break;
13481                 case 'Y':
13482                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
13483                   MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
13484                   break;
13485                 default:
13486                   internalError ();
13487                 }
13488
13489               lastregno = regno;
13490               continue;
13491
13492             case 'P':
13493               if (strncmp (s, "$pc", 3) == 0)
13494                 {
13495                   s += 3;
13496                   continue;
13497                 }
13498               break;
13499
13500             case '5':
13501             case 'H':
13502             case 'W':
13503             case 'D':
13504             case 'j':
13505             case 'V':
13506             case 'C':
13507             case 'U':
13508             case 'k':
13509             case 'K':
13510               i = my_getSmallExpression (&imm_expr, imm_reloc, s);
13511               if (i > 0)
13512                 {
13513                   if (imm_expr.X_op != O_constant)
13514                     {
13515                       forced_insn_length = 4;
13516                       ip->use_extend = TRUE;
13517                       ip->extend = 0;
13518                     }
13519                   else
13520                     {
13521                       /* We need to relax this instruction.  */
13522                       *offset_reloc = *imm_reloc;
13523                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13524                     }
13525                   s = expr_end;
13526                   continue;
13527                 }
13528               *imm_reloc = BFD_RELOC_UNUSED;
13529               /* Fall through.  */
13530             case '<':
13531             case '>':
13532             case '[':
13533             case ']':
13534             case '4':
13535             case '8':
13536               my_getExpression (&imm_expr, s);
13537               if (imm_expr.X_op == O_register)
13538                 {
13539                   /* What we thought was an expression turned out to
13540                      be a register.  */
13541
13542                   if (s[0] == '(' && args[1] == '(')
13543                     {
13544                       /* It looks like the expression was omitted
13545                          before a register indirection, which means
13546                          that the expression is implicitly zero.  We
13547                          still set up imm_expr, so that we handle
13548                          explicit extensions correctly.  */
13549                       imm_expr.X_op = O_constant;
13550                       imm_expr.X_add_number = 0;
13551                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13552                       continue;
13553                     }
13554
13555                   break;
13556                 }
13557
13558               /* We need to relax this instruction.  */
13559               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
13560               s = expr_end;
13561               continue;
13562
13563             case 'p':
13564             case 'q':
13565             case 'A':
13566             case 'B':
13567             case 'E':
13568               /* We use offset_reloc rather than imm_reloc for the PC
13569                  relative operands.  This lets macros with both
13570                  immediate and address operands work correctly.  */
13571               my_getExpression (&offset_expr, s);
13572
13573               if (offset_expr.X_op == O_register)
13574                 break;
13575
13576               /* We need to relax this instruction.  */
13577               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13578               s = expr_end;
13579               continue;
13580
13581             case '6':           /* break code */
13582               my_getExpression (&imm_expr, s);
13583               check_absolute_expr (ip, &imm_expr);
13584               if ((unsigned long) imm_expr.X_add_number > 63)
13585                 as_warn (_("Invalid value for `%s' (%lu)"),
13586                          ip->insn_mo->name,
13587                          (unsigned long) imm_expr.X_add_number);
13588               MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
13589               imm_expr.X_op = O_absent;
13590               s = expr_end;
13591               continue;
13592
13593             case 'a':           /* 26 bit address */
13594               my_getExpression (&offset_expr, s);
13595               s = expr_end;
13596               *offset_reloc = BFD_RELOC_MIPS16_JMP;
13597               ip->insn_opcode <<= 16;
13598               continue;
13599
13600             case 'l':           /* register list for entry macro */
13601             case 'L':           /* register list for exit macro */
13602               {
13603                 int mask;
13604
13605                 if (c == 'l')
13606                   mask = 0;
13607                 else
13608                   mask = 7 << 3;
13609                 while (*s != '\0')
13610                   {
13611                     unsigned int freg, reg1, reg2;
13612
13613                     while (*s == ' ' || *s == ',')
13614                       ++s;
13615                     if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13616                       freg = 0;
13617                     else if (reg_lookup (&s, RTYPE_FPU, &reg1))
13618                       freg = 1;
13619                     else
13620                       {
13621                         as_bad (_("can't parse register list"));
13622                         break;
13623                       }
13624                     if (*s == ' ')
13625                       ++s;
13626                     if (*s != '-')
13627                       reg2 = reg1;
13628                     else
13629                       {
13630                         ++s;
13631                         if (!reg_lookup (&s, freg ? RTYPE_FPU 
13632                                          : (RTYPE_GP | RTYPE_NUM), &reg2))
13633                           {
13634                             as_bad (_("invalid register list"));
13635                             break;
13636                           }
13637                       }
13638                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
13639                       {
13640                         mask &= ~ (7 << 3);
13641                         mask |= 5 << 3;
13642                       }
13643                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
13644                       {
13645                         mask &= ~ (7 << 3);
13646                         mask |= 6 << 3;
13647                       }
13648                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
13649                       mask |= (reg2 - 3) << 3;
13650                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
13651                       mask |= (reg2 - 15) << 1;
13652                     else if (reg1 == RA && reg2 == RA)
13653                       mask |= 1;
13654                     else
13655                       {
13656                         as_bad (_("invalid register list"));
13657                         break;
13658                       }
13659                   }
13660                 /* The mask is filled in in the opcode table for the
13661                    benefit of the disassembler.  We remove it before
13662                    applying the actual mask.  */
13663                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
13664                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
13665               }
13666             continue;
13667
13668             case 'm':           /* Register list for save insn.  */
13669             case 'M':           /* Register list for restore insn.  */
13670               {
13671                 int opcode = 0;
13672                 int framesz = 0, seen_framesz = 0;
13673                 int nargs = 0, statics = 0, sregs = 0;
13674
13675                 while (*s != '\0')
13676                   {
13677                     unsigned int reg1, reg2;
13678
13679                     SKIP_SPACE_TABS (s);
13680                     while (*s == ',')
13681                       ++s;
13682                     SKIP_SPACE_TABS (s);
13683
13684                     my_getExpression (&imm_expr, s);
13685                     if (imm_expr.X_op == O_constant)
13686                       {
13687                         /* Handle the frame size.  */
13688                         if (seen_framesz)
13689                           {
13690                             as_bad (_("more than one frame size in list"));
13691                             break;
13692                           }
13693                         seen_framesz = 1;
13694                         framesz = imm_expr.X_add_number;
13695                         imm_expr.X_op = O_absent;
13696                         s = expr_end;
13697                         continue;
13698                       }
13699
13700                     if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
13701                       {
13702                         as_bad (_("can't parse register list"));
13703                         break;
13704                       }
13705
13706                     while (*s == ' ')
13707                       ++s;
13708
13709                     if (*s != '-')
13710                       reg2 = reg1;
13711                     else
13712                       {
13713                         ++s;
13714                         if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
13715                             || reg2 < reg1)
13716                           {
13717                             as_bad (_("can't parse register list"));
13718                             break;
13719                           }
13720                       }
13721
13722                     while (reg1 <= reg2)
13723                       {
13724                         if (reg1 >= 4 && reg1 <= 7)
13725                           {
13726                             if (!seen_framesz)
13727                                 /* args $a0-$a3 */
13728                                 nargs |= 1 << (reg1 - 4);
13729                             else
13730                                 /* statics $a0-$a3 */
13731                                 statics |= 1 << (reg1 - 4);
13732                           }
13733                         else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
13734                           {
13735                             /* $s0-$s8 */
13736                             sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
13737                           }
13738                         else if (reg1 == 31)
13739                           {
13740                             /* Add $ra to insn.  */
13741                             opcode |= 0x40;
13742                           }
13743                         else
13744                           {
13745                             as_bad (_("unexpected register in list"));
13746                             break;
13747                           }
13748                         if (++reg1 == 24)
13749                           reg1 = 30;
13750                       }
13751                   }
13752
13753                 /* Encode args/statics combination.  */
13754                 if (nargs & statics)
13755                   as_bad (_("arg/static registers overlap"));
13756                 else if (nargs == 0xf)
13757                   /* All $a0-$a3 are args.  */
13758                   opcode |= MIPS16_ALL_ARGS << 16;
13759                 else if (statics == 0xf)
13760                   /* All $a0-$a3 are statics.  */
13761                   opcode |= MIPS16_ALL_STATICS << 16;
13762                 else 
13763                   {
13764                     int narg = 0, nstat = 0;
13765
13766                     /* Count arg registers.  */
13767                     while (nargs & 0x1)
13768                       {
13769                         nargs >>= 1;
13770                         narg++;
13771                       }
13772                     if (nargs != 0)
13773                       as_bad (_("invalid arg register list"));
13774
13775                     /* Count static registers.  */
13776                     while (statics & 0x8)
13777                       {
13778                         statics = (statics << 1) & 0xf;
13779                         nstat++;
13780                       }
13781                     if (statics != 0) 
13782                       as_bad (_("invalid static register list"));
13783
13784                     /* Encode args/statics.  */
13785                     opcode |= ((narg << 2) | nstat) << 16;
13786                   }
13787
13788                 /* Encode $s0/$s1.  */
13789                 if (sregs & (1 << 0))           /* $s0 */
13790                   opcode |= 0x20;
13791                 if (sregs & (1 << 1))           /* $s1 */
13792                   opcode |= 0x10;
13793                 sregs >>= 2;
13794
13795                 if (sregs != 0)
13796                   {
13797                     /* Count regs $s2-$s8.  */
13798                     int nsreg = 0;
13799                     while (sregs & 1)
13800                       {
13801                         sregs >>= 1;
13802                         nsreg++;
13803                       }
13804                     if (sregs != 0)
13805                       as_bad (_("invalid static register list"));
13806                     /* Encode $s2-$s8. */
13807                     opcode |= nsreg << 24;
13808                   }
13809
13810                 /* Encode frame size.  */
13811                 if (!seen_framesz)
13812                   as_bad (_("missing frame size"));
13813                 else if ((framesz & 7) != 0 || framesz < 0
13814                          || framesz > 0xff * 8)
13815                   as_bad (_("invalid frame size"));
13816                 else if (framesz != 128 || (opcode >> 16) != 0)
13817                   {
13818                     framesz /= 8;
13819                     opcode |= (((framesz & 0xf0) << 16)
13820                              | (framesz & 0x0f));
13821                   }
13822
13823                 /* Finally build the instruction.  */
13824                 if ((opcode >> 16) != 0 || framesz == 0)
13825                   {
13826                     ip->use_extend = TRUE;
13827                     ip->extend = opcode >> 16;
13828                   }
13829                 ip->insn_opcode |= opcode & 0x7f;
13830               }
13831             continue;
13832
13833             case 'e':           /* extend code */
13834               my_getExpression (&imm_expr, s);
13835               check_absolute_expr (ip, &imm_expr);
13836               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
13837                 {
13838                   as_warn (_("Invalid value for `%s' (%lu)"),
13839                            ip->insn_mo->name,
13840                            (unsigned long) imm_expr.X_add_number);
13841                   imm_expr.X_add_number &= 0x7ff;
13842                 }
13843               ip->insn_opcode |= imm_expr.X_add_number;
13844               imm_expr.X_op = O_absent;
13845               s = expr_end;
13846               continue;
13847
13848             default:
13849               internalError ();
13850             }
13851           break;
13852         }
13853
13854       /* Args don't match.  */
13855       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
13856           strcmp (insn->name, insn[1].name) == 0)
13857         {
13858           ++insn;
13859           s = argsstart;
13860           continue;
13861         }
13862
13863       insn_error = _("illegal operands");
13864
13865       return;
13866     }
13867 }
13868
13869 /* This structure holds information we know about a mips16 immediate
13870    argument type.  */
13871
13872 struct mips16_immed_operand
13873 {
13874   /* The type code used in the argument string in the opcode table.  */
13875   int type;
13876   /* The number of bits in the short form of the opcode.  */
13877   int nbits;
13878   /* The number of bits in the extended form of the opcode.  */
13879   int extbits;
13880   /* The amount by which the short form is shifted when it is used;
13881      for example, the sw instruction has a shift count of 2.  */
13882   int shift;
13883   /* The amount by which the short form is shifted when it is stored
13884      into the instruction code.  */
13885   int op_shift;
13886   /* Non-zero if the short form is unsigned.  */
13887   int unsp;
13888   /* Non-zero if the extended form is unsigned.  */
13889   int extu;
13890   /* Non-zero if the value is PC relative.  */
13891   int pcrel;
13892 };
13893
13894 /* The mips16 immediate operand types.  */
13895
13896 static const struct mips16_immed_operand mips16_immed_operands[] =
13897 {
13898   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
13899   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
13900   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
13901   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
13902   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
13903   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
13904   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
13905   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
13906   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
13907   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
13908   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
13909   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
13910   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
13911   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
13912   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
13913   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
13914   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
13915   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
13916   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
13917   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
13918   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
13919 };
13920
13921 #define MIPS16_NUM_IMMED \
13922   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
13923
13924 /* Handle a mips16 instruction with an immediate value.  This or's the
13925    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
13926    whether an extended value is needed; if one is needed, it sets
13927    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
13928    If SMALL is true, an unextended opcode was explicitly requested.
13929    If EXT is true, an extended opcode was explicitly requested.  If
13930    WARN is true, warn if EXT does not match reality.  */
13931
13932 static void
13933 mips16_immed (char *file, unsigned int line, int type, offsetT val,
13934               bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
13935               unsigned long *insn, bfd_boolean *use_extend,
13936               unsigned short *extend)
13937 {
13938   const struct mips16_immed_operand *op;
13939   int mintiny, maxtiny;
13940   bfd_boolean needext;
13941
13942   op = mips16_immed_operands;
13943   while (op->type != type)
13944     {
13945       ++op;
13946       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13947     }
13948
13949   if (op->unsp)
13950     {
13951       if (type == '<' || type == '>' || type == '[' || type == ']')
13952         {
13953           mintiny = 1;
13954           maxtiny = 1 << op->nbits;
13955         }
13956       else
13957         {
13958           mintiny = 0;
13959           maxtiny = (1 << op->nbits) - 1;
13960         }
13961     }
13962   else
13963     {
13964       mintiny = - (1 << (op->nbits - 1));
13965       maxtiny = (1 << (op->nbits - 1)) - 1;
13966     }
13967
13968   /* Branch offsets have an implicit 0 in the lowest bit.  */
13969   if (type == 'p' || type == 'q')
13970     val /= 2;
13971
13972   if ((val & ((1 << op->shift) - 1)) != 0
13973       || val < (mintiny << op->shift)
13974       || val > (maxtiny << op->shift))
13975     needext = TRUE;
13976   else
13977     needext = FALSE;
13978
13979   if (warn && ext && ! needext)
13980     as_warn_where (file, line,
13981                    _("extended operand requested but not required"));
13982   if (small && needext)
13983     as_bad_where (file, line, _("invalid unextended operand value"));
13984
13985   if (small || (! ext && ! needext))
13986     {
13987       int insnval;
13988
13989       *use_extend = FALSE;
13990       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
13991       insnval <<= op->op_shift;
13992       *insn |= insnval;
13993     }
13994   else
13995     {
13996       long minext, maxext;
13997       int extval;
13998
13999       if (op->extu)
14000         {
14001           minext = 0;
14002           maxext = (1 << op->extbits) - 1;
14003         }
14004       else
14005         {
14006           minext = - (1 << (op->extbits - 1));
14007           maxext = (1 << (op->extbits - 1)) - 1;
14008         }
14009       if (val < minext || val > maxext)
14010         as_bad_where (file, line,
14011                       _("operand value out of range for instruction"));
14012
14013       *use_extend = TRUE;
14014       if (op->extbits == 16)
14015         {
14016           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14017           val &= 0x1f;
14018         }
14019       else if (op->extbits == 15)
14020         {
14021           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14022           val &= 0xf;
14023         }
14024       else
14025         {
14026           extval = ((val & 0x1f) << 6) | (val & 0x20);
14027           val = 0;
14028         }
14029
14030       *extend = (unsigned short) extval;
14031       *insn |= val;
14032     }
14033 }
14034 \f
14035 struct percent_op_match
14036 {
14037   const char *str;
14038   bfd_reloc_code_real_type reloc;
14039 };
14040
14041 static const struct percent_op_match mips_percent_op[] =
14042 {
14043   {"%lo", BFD_RELOC_LO16},
14044 #ifdef OBJ_ELF
14045   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14046   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14047   {"%call16", BFD_RELOC_MIPS_CALL16},
14048   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14049   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14050   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14051   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14052   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14053   {"%got", BFD_RELOC_MIPS_GOT16},
14054   {"%gp_rel", BFD_RELOC_GPREL16},
14055   {"%half", BFD_RELOC_16},
14056   {"%highest", BFD_RELOC_MIPS_HIGHEST},
14057   {"%higher", BFD_RELOC_MIPS_HIGHER},
14058   {"%neg", BFD_RELOC_MIPS_SUB},
14059   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14060   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14061   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14062   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14063   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14064   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14065   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
14066 #endif
14067   {"%hi", BFD_RELOC_HI16_S}
14068 };
14069
14070 static const struct percent_op_match mips16_percent_op[] =
14071 {
14072   {"%lo", BFD_RELOC_MIPS16_LO16},
14073   {"%gprel", BFD_RELOC_MIPS16_GPREL},
14074   {"%got", BFD_RELOC_MIPS16_GOT16},
14075   {"%call16", BFD_RELOC_MIPS16_CALL16},
14076   {"%hi", BFD_RELOC_MIPS16_HI16_S}
14077 };
14078
14079
14080 /* Return true if *STR points to a relocation operator.  When returning true,
14081    move *STR over the operator and store its relocation code in *RELOC.
14082    Leave both *STR and *RELOC alone when returning false.  */
14083
14084 static bfd_boolean
14085 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
14086 {
14087   const struct percent_op_match *percent_op;
14088   size_t limit, i;
14089
14090   if (mips_opts.mips16)
14091     {
14092       percent_op = mips16_percent_op;
14093       limit = ARRAY_SIZE (mips16_percent_op);
14094     }
14095   else
14096     {
14097       percent_op = mips_percent_op;
14098       limit = ARRAY_SIZE (mips_percent_op);
14099     }
14100
14101   for (i = 0; i < limit; i++)
14102     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
14103       {
14104         int len = strlen (percent_op[i].str);
14105
14106         if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14107           continue;
14108
14109         *str += strlen (percent_op[i].str);
14110         *reloc = percent_op[i].reloc;
14111
14112         /* Check whether the output BFD supports this relocation.
14113            If not, issue an error and fall back on something safe.  */
14114         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
14115           {
14116             as_bad (_("relocation %s isn't supported by the current ABI"),
14117                     percent_op[i].str);
14118             *reloc = BFD_RELOC_UNUSED;
14119           }
14120         return TRUE;
14121       }
14122   return FALSE;
14123 }
14124
14125
14126 /* Parse string STR as a 16-bit relocatable operand.  Store the
14127    expression in *EP and the relocations in the array starting
14128    at RELOC.  Return the number of relocation operators used.
14129
14130    On exit, EXPR_END points to the first character after the expression.  */
14131
14132 static size_t
14133 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14134                        char *str)
14135 {
14136   bfd_reloc_code_real_type reversed_reloc[3];
14137   size_t reloc_index, i;
14138   int crux_depth, str_depth;
14139   char *crux;
14140
14141   /* Search for the start of the main expression, recoding relocations
14142      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
14143      of the main expression and with CRUX_DEPTH containing the number
14144      of open brackets at that point.  */
14145   reloc_index = -1;
14146   str_depth = 0;
14147   do
14148     {
14149       reloc_index++;
14150       crux = str;
14151       crux_depth = str_depth;
14152
14153       /* Skip over whitespace and brackets, keeping count of the number
14154          of brackets.  */
14155       while (*str == ' ' || *str == '\t' || *str == '(')
14156         if (*str++ == '(')
14157           str_depth++;
14158     }
14159   while (*str == '%'
14160          && reloc_index < (HAVE_NEWABI ? 3 : 1)
14161          && parse_relocation (&str, &reversed_reloc[reloc_index]));
14162
14163   my_getExpression (ep, crux);
14164   str = expr_end;
14165
14166   /* Match every open bracket.  */
14167   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
14168     if (*str++ == ')')
14169       crux_depth--;
14170
14171   if (crux_depth > 0)
14172     as_bad (_("unclosed '('"));
14173
14174   expr_end = str;
14175
14176   if (reloc_index != 0)
14177     {
14178       prev_reloc_op_frag = frag_now;
14179       for (i = 0; i < reloc_index; i++)
14180         reloc[i] = reversed_reloc[reloc_index - 1 - i];
14181     }
14182
14183   return reloc_index;
14184 }
14185
14186 static void
14187 my_getExpression (expressionS *ep, char *str)
14188 {
14189   char *save_in;
14190
14191   save_in = input_line_pointer;
14192   input_line_pointer = str;
14193   expression (ep);
14194   expr_end = input_line_pointer;
14195   input_line_pointer = save_in;
14196 }
14197
14198 char *
14199 md_atof (int type, char *litP, int *sizeP)
14200 {
14201   return ieee_md_atof (type, litP, sizeP, target_big_endian);
14202 }
14203
14204 void
14205 md_number_to_chars (char *buf, valueT val, int n)
14206 {
14207   if (target_big_endian)
14208     number_to_chars_bigendian (buf, val, n);
14209   else
14210     number_to_chars_littleendian (buf, val, n);
14211 }
14212 \f
14213 #ifdef OBJ_ELF
14214 static int support_64bit_objects(void)
14215 {
14216   const char **list, **l;
14217   int yes;
14218
14219   list = bfd_target_list ();
14220   for (l = list; *l != NULL; l++)
14221     if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14222         || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
14223       break;
14224   yes = (*l != NULL);
14225   free (list);
14226   return yes;
14227 }
14228 #endif /* OBJ_ELF */
14229
14230 const char *md_shortopts = "O::g::G:";
14231
14232 enum options
14233   {
14234     OPTION_MARCH = OPTION_MD_BASE,
14235     OPTION_MTUNE,
14236     OPTION_MIPS1,
14237     OPTION_MIPS2,
14238     OPTION_MIPS3,
14239     OPTION_MIPS4,
14240     OPTION_MIPS5,
14241     OPTION_MIPS32,
14242     OPTION_MIPS64,
14243     OPTION_MIPS32R2,
14244     OPTION_MIPS64R2,
14245     OPTION_MIPS16,
14246     OPTION_NO_MIPS16,
14247     OPTION_MIPS3D,
14248     OPTION_NO_MIPS3D,
14249     OPTION_MDMX,
14250     OPTION_NO_MDMX,
14251     OPTION_DSP,
14252     OPTION_NO_DSP,
14253     OPTION_MT,
14254     OPTION_NO_MT,
14255     OPTION_SMARTMIPS,
14256     OPTION_NO_SMARTMIPS,
14257     OPTION_DSPR2,
14258     OPTION_NO_DSPR2,
14259     OPTION_MICROMIPS,
14260     OPTION_NO_MICROMIPS,
14261     OPTION_MCU,
14262     OPTION_NO_MCU,
14263     OPTION_COMPAT_ARCH_BASE,
14264     OPTION_M4650,
14265     OPTION_NO_M4650,
14266     OPTION_M4010,
14267     OPTION_NO_M4010,
14268     OPTION_M4100,
14269     OPTION_NO_M4100,
14270     OPTION_M3900,
14271     OPTION_NO_M3900,
14272     OPTION_M7000_HILO_FIX,
14273     OPTION_MNO_7000_HILO_FIX, 
14274     OPTION_FIX_24K,
14275     OPTION_NO_FIX_24K,
14276     OPTION_FIX_LOONGSON2F_JUMP,
14277     OPTION_NO_FIX_LOONGSON2F_JUMP,
14278     OPTION_FIX_LOONGSON2F_NOP,
14279     OPTION_NO_FIX_LOONGSON2F_NOP,
14280     OPTION_FIX_VR4120,
14281     OPTION_NO_FIX_VR4120,
14282     OPTION_FIX_VR4130,
14283     OPTION_NO_FIX_VR4130,
14284     OPTION_FIX_CN63XXP1,
14285     OPTION_NO_FIX_CN63XXP1,
14286     OPTION_TRAP,
14287     OPTION_BREAK,
14288     OPTION_EB,
14289     OPTION_EL,
14290     OPTION_FP32,
14291     OPTION_GP32,
14292     OPTION_CONSTRUCT_FLOATS,
14293     OPTION_NO_CONSTRUCT_FLOATS,
14294     OPTION_FP64,
14295     OPTION_GP64,
14296     OPTION_RELAX_BRANCH,
14297     OPTION_NO_RELAX_BRANCH,
14298     OPTION_MSHARED,
14299     OPTION_MNO_SHARED,
14300     OPTION_MSYM32,
14301     OPTION_MNO_SYM32,
14302     OPTION_SOFT_FLOAT,
14303     OPTION_HARD_FLOAT,
14304     OPTION_SINGLE_FLOAT,
14305     OPTION_DOUBLE_FLOAT,
14306     OPTION_32,
14307 #ifdef OBJ_ELF
14308     OPTION_CALL_SHARED,
14309     OPTION_CALL_NONPIC,
14310     OPTION_NON_SHARED,
14311     OPTION_XGOT,
14312     OPTION_MABI,
14313     OPTION_N32,
14314     OPTION_64,
14315     OPTION_MDEBUG,
14316     OPTION_NO_MDEBUG,
14317     OPTION_PDR,
14318     OPTION_NO_PDR,
14319     OPTION_MVXWORKS_PIC,
14320 #endif /* OBJ_ELF */
14321     OPTION_END_OF_ENUM    
14322   };
14323   
14324 struct option md_longopts[] =
14325 {
14326   /* Options which specify architecture.  */
14327   {"march", required_argument, NULL, OPTION_MARCH},
14328   {"mtune", required_argument, NULL, OPTION_MTUNE},
14329   {"mips0", no_argument, NULL, OPTION_MIPS1},
14330   {"mips1", no_argument, NULL, OPTION_MIPS1},
14331   {"mips2", no_argument, NULL, OPTION_MIPS2},
14332   {"mips3", no_argument, NULL, OPTION_MIPS3},
14333   {"mips4", no_argument, NULL, OPTION_MIPS4},
14334   {"mips5", no_argument, NULL, OPTION_MIPS5},
14335   {"mips32", no_argument, NULL, OPTION_MIPS32},
14336   {"mips64", no_argument, NULL, OPTION_MIPS64},
14337   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
14338   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
14339
14340   /* Options which specify Application Specific Extensions (ASEs).  */
14341   {"mips16", no_argument, NULL, OPTION_MIPS16},
14342   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
14343   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
14344   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
14345   {"mdmx", no_argument, NULL, OPTION_MDMX},
14346   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
14347   {"mdsp", no_argument, NULL, OPTION_DSP},
14348   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
14349   {"mmt", no_argument, NULL, OPTION_MT},
14350   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
14351   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
14352   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
14353   {"mdspr2", no_argument, NULL, OPTION_DSPR2},
14354   {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
14355   {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
14356   {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
14357   {"mmcu", no_argument, NULL, OPTION_MCU},
14358   {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
14359
14360   /* Old-style architecture options.  Don't add more of these.  */
14361   {"m4650", no_argument, NULL, OPTION_M4650},
14362   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
14363   {"m4010", no_argument, NULL, OPTION_M4010},
14364   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
14365   {"m4100", no_argument, NULL, OPTION_M4100},
14366   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
14367   {"m3900", no_argument, NULL, OPTION_M3900},
14368   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
14369
14370   /* Options which enable bug fixes.  */
14371   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
14372   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14373   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
14374   {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
14375   {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
14376   {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
14377   {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
14378   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
14379   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
14380   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
14381   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
14382   {"mfix-24k",    no_argument, NULL, OPTION_FIX_24K},
14383   {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
14384   {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
14385   {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
14386
14387   /* Miscellaneous options.  */
14388   {"trap", no_argument, NULL, OPTION_TRAP},
14389   {"no-break", no_argument, NULL, OPTION_TRAP},
14390   {"break", no_argument, NULL, OPTION_BREAK},
14391   {"no-trap", no_argument, NULL, OPTION_BREAK},
14392   {"EB", no_argument, NULL, OPTION_EB},
14393   {"EL", no_argument, NULL, OPTION_EL},
14394   {"mfp32", no_argument, NULL, OPTION_FP32},
14395   {"mgp32", no_argument, NULL, OPTION_GP32},
14396   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
14397   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
14398   {"mfp64", no_argument, NULL, OPTION_FP64},
14399   {"mgp64", no_argument, NULL, OPTION_GP64},
14400   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
14401   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
14402   {"mshared", no_argument, NULL, OPTION_MSHARED},
14403   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
14404   {"msym32", no_argument, NULL, OPTION_MSYM32},
14405   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
14406   {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
14407   {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
14408   {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
14409   {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
14410
14411   /* Strictly speaking this next option is ELF specific,
14412      but we allow it for other ports as well in order to
14413      make testing easier.  */
14414   {"32",          no_argument, NULL, OPTION_32},
14415   
14416   /* ELF-specific options.  */
14417 #ifdef OBJ_ELF
14418   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
14419   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
14420   {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
14421   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
14422   {"xgot",        no_argument, NULL, OPTION_XGOT},
14423   {"mabi", required_argument, NULL, OPTION_MABI},
14424   {"n32",         no_argument, NULL, OPTION_N32},
14425   {"64",          no_argument, NULL, OPTION_64},
14426   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
14427   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
14428   {"mpdr", no_argument, NULL, OPTION_PDR},
14429   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
14430   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
14431 #endif /* OBJ_ELF */
14432
14433   {NULL, no_argument, NULL, 0}
14434 };
14435 size_t md_longopts_size = sizeof (md_longopts);
14436
14437 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14438    NEW_VALUE.  Warn if another value was already specified.  Note:
14439    we have to defer parsing the -march and -mtune arguments in order
14440    to handle 'from-abi' correctly, since the ABI might be specified
14441    in a later argument.  */
14442
14443 static void
14444 mips_set_option_string (const char **string_ptr, const char *new_value)
14445 {
14446   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14447     as_warn (_("A different %s was already specified, is now %s"),
14448              string_ptr == &mips_arch_string ? "-march" : "-mtune",
14449              new_value);
14450
14451   *string_ptr = new_value;
14452 }
14453
14454 int
14455 md_parse_option (int c, char *arg)
14456 {
14457   switch (c)
14458     {
14459     case OPTION_CONSTRUCT_FLOATS:
14460       mips_disable_float_construction = 0;
14461       break;
14462
14463     case OPTION_NO_CONSTRUCT_FLOATS:
14464       mips_disable_float_construction = 1;
14465       break;
14466
14467     case OPTION_TRAP:
14468       mips_trap = 1;
14469       break;
14470
14471     case OPTION_BREAK:
14472       mips_trap = 0;
14473       break;
14474
14475     case OPTION_EB:
14476       target_big_endian = 1;
14477       break;
14478
14479     case OPTION_EL:
14480       target_big_endian = 0;
14481       break;
14482
14483     case 'O':
14484       if (arg == NULL)
14485         mips_optimize = 1;
14486       else if (arg[0] == '0')
14487         mips_optimize = 0;
14488       else if (arg[0] == '1')
14489         mips_optimize = 1;
14490       else
14491         mips_optimize = 2;
14492       break;
14493
14494     case 'g':
14495       if (arg == NULL)
14496         mips_debug = 2;
14497       else
14498         mips_debug = atoi (arg);
14499       break;
14500
14501     case OPTION_MIPS1:
14502       file_mips_isa = ISA_MIPS1;
14503       break;
14504
14505     case OPTION_MIPS2:
14506       file_mips_isa = ISA_MIPS2;
14507       break;
14508
14509     case OPTION_MIPS3:
14510       file_mips_isa = ISA_MIPS3;
14511       break;
14512
14513     case OPTION_MIPS4:
14514       file_mips_isa = ISA_MIPS4;
14515       break;
14516
14517     case OPTION_MIPS5:
14518       file_mips_isa = ISA_MIPS5;
14519       break;
14520
14521     case OPTION_MIPS32:
14522       file_mips_isa = ISA_MIPS32;
14523       break;
14524
14525     case OPTION_MIPS32R2:
14526       file_mips_isa = ISA_MIPS32R2;
14527       break;
14528
14529     case OPTION_MIPS64R2:
14530       file_mips_isa = ISA_MIPS64R2;
14531       break;
14532
14533     case OPTION_MIPS64:
14534       file_mips_isa = ISA_MIPS64;
14535       break;
14536
14537     case OPTION_MTUNE:
14538       mips_set_option_string (&mips_tune_string, arg);
14539       break;
14540
14541     case OPTION_MARCH:
14542       mips_set_option_string (&mips_arch_string, arg);
14543       break;
14544
14545     case OPTION_M4650:
14546       mips_set_option_string (&mips_arch_string, "4650");
14547       mips_set_option_string (&mips_tune_string, "4650");
14548       break;
14549
14550     case OPTION_NO_M4650:
14551       break;
14552
14553     case OPTION_M4010:
14554       mips_set_option_string (&mips_arch_string, "4010");
14555       mips_set_option_string (&mips_tune_string, "4010");
14556       break;
14557
14558     case OPTION_NO_M4010:
14559       break;
14560
14561     case OPTION_M4100:
14562       mips_set_option_string (&mips_arch_string, "4100");
14563       mips_set_option_string (&mips_tune_string, "4100");
14564       break;
14565
14566     case OPTION_NO_M4100:
14567       break;
14568
14569     case OPTION_M3900:
14570       mips_set_option_string (&mips_arch_string, "3900");
14571       mips_set_option_string (&mips_tune_string, "3900");
14572       break;
14573
14574     case OPTION_NO_M3900:
14575       break;
14576
14577     case OPTION_MDMX:
14578       mips_opts.ase_mdmx = 1;
14579       break;
14580
14581     case OPTION_NO_MDMX:
14582       mips_opts.ase_mdmx = 0;
14583       break;
14584
14585     case OPTION_DSP:
14586       mips_opts.ase_dsp = 1;
14587       mips_opts.ase_dspr2 = 0;
14588       break;
14589
14590     case OPTION_NO_DSP:
14591       mips_opts.ase_dsp = 0;
14592       mips_opts.ase_dspr2 = 0;
14593       break;
14594
14595     case OPTION_DSPR2:
14596       mips_opts.ase_dspr2 = 1;
14597       mips_opts.ase_dsp = 1;
14598       break;
14599
14600     case OPTION_NO_DSPR2:
14601       mips_opts.ase_dspr2 = 0;
14602       mips_opts.ase_dsp = 0;
14603       break;
14604
14605     case OPTION_MT:
14606       mips_opts.ase_mt = 1;
14607       break;
14608
14609     case OPTION_NO_MT:
14610       mips_opts.ase_mt = 0;
14611       break;
14612
14613     case OPTION_MCU:
14614       mips_opts.ase_mcu = 1;
14615       break;
14616
14617     case OPTION_NO_MCU:
14618       mips_opts.ase_mcu = 0;
14619       break;
14620
14621     case OPTION_MICROMIPS:
14622       if (mips_opts.mips16 == 1)
14623         {
14624           as_bad (_("-mmicromips cannot be used with -mips16"));
14625           return 0;
14626         }
14627       mips_opts.micromips = 1;
14628       mips_no_prev_insn ();
14629       break;
14630
14631     case OPTION_NO_MICROMIPS:
14632       mips_opts.micromips = 0;
14633       mips_no_prev_insn ();
14634       break;
14635
14636     case OPTION_MIPS16:
14637       if (mips_opts.micromips == 1)
14638         {
14639           as_bad (_("-mips16 cannot be used with -micromips"));
14640           return 0;
14641         }
14642       mips_opts.mips16 = 1;
14643       mips_no_prev_insn ();
14644       break;
14645
14646     case OPTION_NO_MIPS16:
14647       mips_opts.mips16 = 0;
14648       mips_no_prev_insn ();
14649       break;
14650
14651     case OPTION_MIPS3D:
14652       mips_opts.ase_mips3d = 1;
14653       break;
14654
14655     case OPTION_NO_MIPS3D:
14656       mips_opts.ase_mips3d = 0;
14657       break;
14658
14659     case OPTION_SMARTMIPS:
14660       mips_opts.ase_smartmips = 1;
14661       break;
14662
14663     case OPTION_NO_SMARTMIPS:
14664       mips_opts.ase_smartmips = 0;
14665       break;
14666
14667     case OPTION_FIX_24K:
14668       mips_fix_24k = 1;
14669       break;
14670
14671     case OPTION_NO_FIX_24K:
14672       mips_fix_24k = 0;
14673       break;
14674
14675     case OPTION_FIX_LOONGSON2F_JUMP:
14676       mips_fix_loongson2f_jump = TRUE;
14677       break;
14678
14679     case OPTION_NO_FIX_LOONGSON2F_JUMP:
14680       mips_fix_loongson2f_jump = FALSE;
14681       break;
14682
14683     case OPTION_FIX_LOONGSON2F_NOP:
14684       mips_fix_loongson2f_nop = TRUE;
14685       break;
14686
14687     case OPTION_NO_FIX_LOONGSON2F_NOP:
14688       mips_fix_loongson2f_nop = FALSE;
14689       break;
14690
14691     case OPTION_FIX_VR4120:
14692       mips_fix_vr4120 = 1;
14693       break;
14694
14695     case OPTION_NO_FIX_VR4120:
14696       mips_fix_vr4120 = 0;
14697       break;
14698
14699     case OPTION_FIX_VR4130:
14700       mips_fix_vr4130 = 1;
14701       break;
14702
14703     case OPTION_NO_FIX_VR4130:
14704       mips_fix_vr4130 = 0;
14705       break;
14706
14707     case OPTION_FIX_CN63XXP1:
14708       mips_fix_cn63xxp1 = TRUE;
14709       break;
14710
14711     case OPTION_NO_FIX_CN63XXP1:
14712       mips_fix_cn63xxp1 = FALSE;
14713       break;
14714
14715     case OPTION_RELAX_BRANCH:
14716       mips_relax_branch = 1;
14717       break;
14718
14719     case OPTION_NO_RELAX_BRANCH:
14720       mips_relax_branch = 0;
14721       break;
14722
14723     case OPTION_MSHARED:
14724       mips_in_shared = TRUE;
14725       break;
14726
14727     case OPTION_MNO_SHARED:
14728       mips_in_shared = FALSE;
14729       break;
14730
14731     case OPTION_MSYM32:
14732       mips_opts.sym32 = TRUE;
14733       break;
14734
14735     case OPTION_MNO_SYM32:
14736       mips_opts.sym32 = FALSE;
14737       break;
14738
14739 #ifdef OBJ_ELF
14740       /* When generating ELF code, we permit -KPIC and -call_shared to
14741          select SVR4_PIC, and -non_shared to select no PIC.  This is
14742          intended to be compatible with Irix 5.  */
14743     case OPTION_CALL_SHARED:
14744       if (!IS_ELF)
14745         {
14746           as_bad (_("-call_shared is supported only for ELF format"));
14747           return 0;
14748         }
14749       mips_pic = SVR4_PIC;
14750       mips_abicalls = TRUE;
14751       break;
14752
14753     case OPTION_CALL_NONPIC:
14754       if (!IS_ELF)
14755         {
14756           as_bad (_("-call_nonpic is supported only for ELF format"));
14757           return 0;
14758         }
14759       mips_pic = NO_PIC;
14760       mips_abicalls = TRUE;
14761       break;
14762
14763     case OPTION_NON_SHARED:
14764       if (!IS_ELF)
14765         {
14766           as_bad (_("-non_shared is supported only for ELF format"));
14767           return 0;
14768         }
14769       mips_pic = NO_PIC;
14770       mips_abicalls = FALSE;
14771       break;
14772
14773       /* The -xgot option tells the assembler to use 32 bit offsets
14774          when accessing the got in SVR4_PIC mode.  It is for Irix
14775          compatibility.  */
14776     case OPTION_XGOT:
14777       mips_big_got = 1;
14778       break;
14779 #endif /* OBJ_ELF */
14780
14781     case 'G':
14782       g_switch_value = atoi (arg);
14783       g_switch_seen = 1;
14784       break;
14785
14786       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14787          and -mabi=64.  */
14788     case OPTION_32:
14789       if (IS_ELF)
14790         mips_abi = O32_ABI;
14791       /* We silently ignore -32 for non-ELF targets.  This greatly
14792          simplifies the construction of the MIPS GAS test cases.  */
14793       break;
14794
14795 #ifdef OBJ_ELF
14796     case OPTION_N32:
14797       if (!IS_ELF)
14798         {
14799           as_bad (_("-n32 is supported for ELF format only"));
14800           return 0;
14801         }
14802       mips_abi = N32_ABI;
14803       break;
14804
14805     case OPTION_64:
14806       if (!IS_ELF)
14807         {
14808           as_bad (_("-64 is supported for ELF format only"));
14809           return 0;
14810         }
14811       mips_abi = N64_ABI;
14812       if (!support_64bit_objects())
14813         as_fatal (_("No compiled in support for 64 bit object file format"));
14814       break;
14815 #endif /* OBJ_ELF */
14816
14817     case OPTION_GP32:
14818       file_mips_gp32 = 1;
14819       break;
14820
14821     case OPTION_GP64:
14822       file_mips_gp32 = 0;
14823       break;
14824
14825     case OPTION_FP32:
14826       file_mips_fp32 = 1;
14827       break;
14828
14829     case OPTION_FP64:
14830       file_mips_fp32 = 0;
14831       break;
14832
14833     case OPTION_SINGLE_FLOAT:
14834       file_mips_single_float = 1;
14835       break;
14836
14837     case OPTION_DOUBLE_FLOAT:
14838       file_mips_single_float = 0;
14839       break;
14840
14841     case OPTION_SOFT_FLOAT:
14842       file_mips_soft_float = 1;
14843       break;
14844
14845     case OPTION_HARD_FLOAT:
14846       file_mips_soft_float = 0;
14847       break;
14848
14849 #ifdef OBJ_ELF
14850     case OPTION_MABI:
14851       if (!IS_ELF)
14852         {
14853           as_bad (_("-mabi is supported for ELF format only"));
14854           return 0;
14855         }
14856       if (strcmp (arg, "32") == 0)
14857         mips_abi = O32_ABI;
14858       else if (strcmp (arg, "o64") == 0)
14859         mips_abi = O64_ABI;
14860       else if (strcmp (arg, "n32") == 0)
14861         mips_abi = N32_ABI;
14862       else if (strcmp (arg, "64") == 0)
14863         {
14864           mips_abi = N64_ABI;
14865           if (! support_64bit_objects())
14866             as_fatal (_("No compiled in support for 64 bit object file "
14867                         "format"));
14868         }
14869       else if (strcmp (arg, "eabi") == 0)
14870         mips_abi = EABI_ABI;
14871       else
14872         {
14873           as_fatal (_("invalid abi -mabi=%s"), arg);
14874           return 0;
14875         }
14876       break;
14877 #endif /* OBJ_ELF */
14878
14879     case OPTION_M7000_HILO_FIX:
14880       mips_7000_hilo_fix = TRUE;
14881       break;
14882
14883     case OPTION_MNO_7000_HILO_FIX:
14884       mips_7000_hilo_fix = FALSE;
14885       break;
14886
14887 #ifdef OBJ_ELF
14888     case OPTION_MDEBUG:
14889       mips_flag_mdebug = TRUE;
14890       break;
14891
14892     case OPTION_NO_MDEBUG:
14893       mips_flag_mdebug = FALSE;
14894       break;
14895
14896     case OPTION_PDR:
14897       mips_flag_pdr = TRUE;
14898       break;
14899
14900     case OPTION_NO_PDR:
14901       mips_flag_pdr = FALSE;
14902       break;
14903
14904     case OPTION_MVXWORKS_PIC:
14905       mips_pic = VXWORKS_PIC;
14906       break;
14907 #endif /* OBJ_ELF */
14908
14909     default:
14910       return 0;
14911     }
14912
14913     mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14914
14915   return 1;
14916 }
14917 \f
14918 /* Set up globals to generate code for the ISA or processor
14919    described by INFO.  */
14920
14921 static void
14922 mips_set_architecture (const struct mips_cpu_info *info)
14923 {
14924   if (info != 0)
14925     {
14926       file_mips_arch = info->cpu;
14927       mips_opts.arch = info->cpu;
14928       mips_opts.isa = info->isa;
14929     }
14930 }
14931
14932
14933 /* Likewise for tuning.  */
14934
14935 static void
14936 mips_set_tune (const struct mips_cpu_info *info)
14937 {
14938   if (info != 0)
14939     mips_tune = info->cpu;
14940 }
14941
14942
14943 void
14944 mips_after_parse_args (void)
14945 {
14946   const struct mips_cpu_info *arch_info = 0;
14947   const struct mips_cpu_info *tune_info = 0;
14948
14949   /* GP relative stuff not working for PE */
14950   if (strncmp (TARGET_OS, "pe", 2) == 0)
14951     {
14952       if (g_switch_seen && g_switch_value != 0)
14953         as_bad (_("-G not supported in this configuration."));
14954       g_switch_value = 0;
14955     }
14956
14957   if (mips_abi == NO_ABI)
14958     mips_abi = MIPS_DEFAULT_ABI;
14959
14960   /* The following code determines the architecture and register size.
14961      Similar code was added to GCC 3.3 (see override_options() in
14962      config/mips/mips.c).  The GAS and GCC code should be kept in sync
14963      as much as possible.  */
14964
14965   if (mips_arch_string != 0)
14966     arch_info = mips_parse_cpu ("-march", mips_arch_string);
14967
14968   if (file_mips_isa != ISA_UNKNOWN)
14969     {
14970       /* Handle -mipsN.  At this point, file_mips_isa contains the
14971          ISA level specified by -mipsN, while arch_info->isa contains
14972          the -march selection (if any).  */
14973       if (arch_info != 0)
14974         {
14975           /* -march takes precedence over -mipsN, since it is more descriptive.
14976              There's no harm in specifying both as long as the ISA levels
14977              are the same.  */
14978           if (file_mips_isa != arch_info->isa)
14979             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
14980                     mips_cpu_info_from_isa (file_mips_isa)->name,
14981                     mips_cpu_info_from_isa (arch_info->isa)->name);
14982         }
14983       else
14984         arch_info = mips_cpu_info_from_isa (file_mips_isa);
14985     }
14986
14987   if (arch_info == 0)
14988     arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14989
14990   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
14991     as_bad (_("-march=%s is not compatible with the selected ABI"),
14992             arch_info->name);
14993
14994   mips_set_architecture (arch_info);
14995
14996   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
14997   if (mips_tune_string != 0)
14998     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
14999
15000   if (tune_info == 0)
15001     mips_set_tune (arch_info);
15002   else
15003     mips_set_tune (tune_info);
15004
15005   if (file_mips_gp32 >= 0)
15006     {
15007       /* The user specified the size of the integer registers.  Make sure
15008          it agrees with the ABI and ISA.  */
15009       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15010         as_bad (_("-mgp64 used with a 32-bit processor"));
15011       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15012         as_bad (_("-mgp32 used with a 64-bit ABI"));
15013       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15014         as_bad (_("-mgp64 used with a 32-bit ABI"));
15015     }
15016   else
15017     {
15018       /* Infer the integer register size from the ABI and processor.
15019          Restrict ourselves to 32-bit registers if that's all the
15020          processor has, or if the ABI cannot handle 64-bit registers.  */
15021       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15022                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
15023     }
15024
15025   switch (file_mips_fp32)
15026     {
15027     default:
15028     case -1:
15029       /* No user specified float register size.
15030          ??? GAS treats single-float processors as though they had 64-bit
15031          float registers (although it complains when double-precision
15032          instructions are used).  As things stand, saying they have 32-bit
15033          registers would lead to spurious "register must be even" messages.
15034          So here we assume float registers are never smaller than the
15035          integer ones.  */
15036       if (file_mips_gp32 == 0)
15037         /* 64-bit integer registers implies 64-bit float registers.  */
15038         file_mips_fp32 = 0;
15039       else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
15040                && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15041         /* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
15042         file_mips_fp32 = 0;
15043       else
15044         /* 32-bit float registers.  */
15045         file_mips_fp32 = 1;
15046       break;
15047
15048     /* The user specified the size of the float registers.  Check if it
15049        agrees with the ABI and ISA.  */
15050     case 0:
15051       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15052         as_bad (_("-mfp64 used with a 32-bit fpu"));
15053       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15054                && !ISA_HAS_MXHC1 (mips_opts.isa))
15055         as_warn (_("-mfp64 used with a 32-bit ABI"));
15056       break;
15057     case 1:
15058       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15059         as_warn (_("-mfp32 used with a 64-bit ABI"));
15060       break;
15061     }
15062
15063   /* End of GCC-shared inference code.  */
15064
15065   /* This flag is set when we have a 64-bit capable CPU but use only
15066      32-bit wide registers.  Note that EABI does not use it.  */
15067   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15068       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15069           || mips_abi == O32_ABI))
15070     mips_32bitmode = 1;
15071
15072   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15073     as_bad (_("trap exception not supported at ISA 1"));
15074
15075   /* If the selected architecture includes support for ASEs, enable
15076      generation of code for them.  */
15077   if (mips_opts.mips16 == -1)
15078     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
15079   if (mips_opts.micromips == -1)
15080     mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
15081   if (mips_opts.ase_mips3d == -1)
15082     mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
15083                             && file_mips_fp32 == 0) ? 1 : 0;
15084   if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
15085     as_bad (_("-mfp32 used with -mips3d"));
15086
15087   if (mips_opts.ase_mdmx == -1)
15088     mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
15089                           && file_mips_fp32 == 0) ? 1 : 0;
15090   if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
15091     as_bad (_("-mfp32 used with -mdmx"));
15092
15093   if (mips_opts.ase_smartmips == -1)
15094     mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
15095   if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
15096     as_warn (_("%s ISA does not support SmartMIPS"), 
15097              mips_cpu_info_from_isa (mips_opts.isa)->name);
15098
15099   if (mips_opts.ase_dsp == -1)
15100     mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15101   if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
15102     as_warn (_("%s ISA does not support DSP ASE"), 
15103              mips_cpu_info_from_isa (mips_opts.isa)->name);
15104
15105   if (mips_opts.ase_dspr2 == -1)
15106     {
15107       mips_opts.ase_dspr2 = (arch_info->flags & MIPS_CPU_ASE_DSPR2) ? 1 : 0;
15108       mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
15109     }
15110   if (mips_opts.ase_dspr2 && !ISA_SUPPORTS_DSPR2_ASE)
15111     as_warn (_("%s ISA does not support DSP R2 ASE"),
15112              mips_cpu_info_from_isa (mips_opts.isa)->name);
15113
15114   if (mips_opts.ase_mt == -1)
15115     mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
15116   if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
15117     as_warn (_("%s ISA does not support MT ASE"),
15118              mips_cpu_info_from_isa (mips_opts.isa)->name);
15119
15120   if (mips_opts.ase_mcu == -1)
15121     mips_opts.ase_mcu = (arch_info->flags & MIPS_CPU_ASE_MCU) ? 1 : 0;
15122   if (mips_opts.ase_mcu && !ISA_SUPPORTS_MCU_ASE)
15123       as_warn (_("%s ISA does not support MCU ASE"),
15124                mips_cpu_info_from_isa (mips_opts.isa)->name);
15125
15126   file_mips_isa = mips_opts.isa;
15127   file_ase_mips3d = mips_opts.ase_mips3d;
15128   file_ase_mdmx = mips_opts.ase_mdmx;
15129   file_ase_smartmips = mips_opts.ase_smartmips;
15130   file_ase_dsp = mips_opts.ase_dsp;
15131   file_ase_dspr2 = mips_opts.ase_dspr2;
15132   file_ase_mt = mips_opts.ase_mt;
15133   mips_opts.gp32 = file_mips_gp32;
15134   mips_opts.fp32 = file_mips_fp32;
15135   mips_opts.soft_float = file_mips_soft_float;
15136   mips_opts.single_float = file_mips_single_float;
15137
15138   if (mips_flag_mdebug < 0)
15139     {
15140 #ifdef OBJ_MAYBE_ECOFF
15141       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
15142         mips_flag_mdebug = 1;
15143       else
15144 #endif /* OBJ_MAYBE_ECOFF */
15145         mips_flag_mdebug = 0;
15146     }
15147 }
15148 \f
15149 void
15150 mips_init_after_args (void)
15151 {
15152   /* initialize opcodes */
15153   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
15154   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
15155 }
15156
15157 long
15158 md_pcrel_from (fixS *fixP)
15159 {
15160   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15161   switch (fixP->fx_r_type)
15162     {
15163     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15164     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15165       /* Return the address of the delay slot.  */
15166       return addr + 2;
15167
15168     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15169     case BFD_RELOC_MICROMIPS_JMP:
15170     case BFD_RELOC_16_PCREL_S2:
15171     case BFD_RELOC_MIPS_JMP:
15172       /* Return the address of the delay slot.  */
15173       return addr + 4;
15174
15175     default:
15176       /* We have no relocation type for PC relative MIPS16 instructions.  */
15177       if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15178         as_bad_where (fixP->fx_file, fixP->fx_line,
15179                       _("PC relative MIPS16 instruction references a different section"));
15180       return addr;
15181     }
15182 }
15183
15184 /* This is called before the symbol table is processed.  In order to
15185    work with gcc when using mips-tfile, we must keep all local labels.
15186    However, in other cases, we want to discard them.  If we were
15187    called with -g, but we didn't see any debugging information, it may
15188    mean that gcc is smuggling debugging information through to
15189    mips-tfile, in which case we must generate all local labels.  */
15190
15191 void
15192 mips_frob_file_before_adjust (void)
15193 {
15194 #ifndef NO_ECOFF_DEBUGGING
15195   if (ECOFF_DEBUGGING
15196       && mips_debug != 0
15197       && ! ecoff_debugging_seen)
15198     flag_keep_locals = 1;
15199 #endif
15200 }
15201
15202 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
15203    the corresponding LO16 reloc.  This is called before md_apply_fix and
15204    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
15205    relocation operators.
15206
15207    For our purposes, a %lo() expression matches a %got() or %hi()
15208    expression if:
15209
15210       (a) it refers to the same symbol; and
15211       (b) the offset applied in the %lo() expression is no lower than
15212           the offset applied in the %got() or %hi().
15213
15214    (b) allows us to cope with code like:
15215
15216         lui     $4,%hi(foo)
15217         lh      $4,%lo(foo+2)($4)
15218
15219    ...which is legal on RELA targets, and has a well-defined behaviour
15220    if the user knows that adding 2 to "foo" will not induce a carry to
15221    the high 16 bits.
15222
15223    When several %lo()s match a particular %got() or %hi(), we use the
15224    following rules to distinguish them:
15225
15226      (1) %lo()s with smaller offsets are a better match than %lo()s with
15227          higher offsets.
15228
15229      (2) %lo()s with no matching %got() or %hi() are better than those
15230          that already have a matching %got() or %hi().
15231
15232      (3) later %lo()s are better than earlier %lo()s.
15233
15234    These rules are applied in order.
15235
15236    (1) means, among other things, that %lo()s with identical offsets are
15237    chosen if they exist.
15238
15239    (2) means that we won't associate several high-part relocations with
15240    the same low-part relocation unless there's no alternative.  Having
15241    several high parts for the same low part is a GNU extension; this rule
15242    allows careful users to avoid it.
15243
15244    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
15245    with the last high-part relocation being at the front of the list.
15246    It therefore makes sense to choose the last matching low-part
15247    relocation, all other things being equal.  It's also easier
15248    to code that way.  */
15249
15250 void
15251 mips_frob_file (void)
15252 {
15253   struct mips_hi_fixup *l;
15254   bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
15255
15256   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15257     {
15258       segment_info_type *seginfo;
15259       bfd_boolean matched_lo_p;
15260       fixS **hi_pos, **lo_pos, **pos;
15261
15262       gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
15263
15264       /* If a GOT16 relocation turns out to be against a global symbol,
15265          there isn't supposed to be a matching LO.  */
15266       if (got16_reloc_p (l->fixp->fx_r_type)
15267           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
15268         continue;
15269
15270       /* Check quickly whether the next fixup happens to be a matching %lo.  */
15271       if (fixup_has_matching_lo_p (l->fixp))
15272         continue;
15273
15274       seginfo = seg_info (l->seg);
15275
15276       /* Set HI_POS to the position of this relocation in the chain.
15277          Set LO_POS to the position of the chosen low-part relocation.
15278          MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15279          relocation that matches an immediately-preceding high-part
15280          relocation.  */
15281       hi_pos = NULL;
15282       lo_pos = NULL;
15283       matched_lo_p = FALSE;
15284       looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
15285
15286       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15287         {
15288           if (*pos == l->fixp)
15289             hi_pos = pos;
15290
15291           if ((*pos)->fx_r_type == looking_for_rtype
15292               && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
15293               && (*pos)->fx_offset >= l->fixp->fx_offset
15294               && (lo_pos == NULL
15295                   || (*pos)->fx_offset < (*lo_pos)->fx_offset
15296                   || (!matched_lo_p
15297                       && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15298             lo_pos = pos;
15299
15300           matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15301                           && fixup_has_matching_lo_p (*pos));
15302         }
15303
15304       /* If we found a match, remove the high-part relocation from its
15305          current position and insert it before the low-part relocation.
15306          Make the offsets match so that fixup_has_matching_lo_p()
15307          will return true.
15308
15309          We don't warn about unmatched high-part relocations since some
15310          versions of gcc have been known to emit dead "lui ...%hi(...)"
15311          instructions.  */
15312       if (lo_pos != NULL)
15313         {
15314           l->fixp->fx_offset = (*lo_pos)->fx_offset;
15315           if (l->fixp->fx_next != *lo_pos)
15316             {
15317               *hi_pos = l->fixp->fx_next;
15318               l->fixp->fx_next = *lo_pos;
15319               *lo_pos = l->fixp;
15320             }
15321         }
15322     }
15323 }
15324
15325 /* We may have combined relocations without symbols in the N32/N64 ABI.
15326    We have to prevent gas from dropping them.  */
15327
15328 int
15329 mips_force_relocation (fixS *fixp)
15330 {
15331   if (generic_force_reloc (fixp))
15332     return 1;
15333
15334   /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15335      so that the linker relaxation can update targets.  */
15336   if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15337       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15338       || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15339     return 1;
15340
15341   if (HAVE_NEWABI
15342       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
15343       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
15344           || hi16_reloc_p (fixp->fx_r_type)
15345           || lo16_reloc_p (fixp->fx_r_type)))
15346     return 1;
15347
15348   return 0;
15349 }
15350
15351 /* Apply a fixup to the object file.  */
15352
15353 void
15354 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
15355 {
15356   bfd_byte *buf;
15357   long insn;
15358   reloc_howto_type *howto;
15359
15360   /* We ignore generic BFD relocations we don't know about.  */
15361   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15362   if (! howto)
15363     return;
15364
15365   gas_assert (fixP->fx_size == 2
15366               || fixP->fx_size == 4
15367               || fixP->fx_r_type == BFD_RELOC_16
15368               || fixP->fx_r_type == BFD_RELOC_64
15369               || fixP->fx_r_type == BFD_RELOC_CTOR
15370               || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
15371               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
15372               || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15373               || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15374               || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
15375
15376   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
15377
15378   gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15379               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15380               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15381               || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
15382
15383   /* Don't treat parts of a composite relocation as done.  There are two
15384      reasons for this:
15385
15386      (1) The second and third parts will be against 0 (RSS_UNDEF) but
15387          should nevertheless be emitted if the first part is.
15388
15389      (2) In normal usage, composite relocations are never assembly-time
15390          constants.  The easiest way of dealing with the pathological
15391          exceptions is to generate a relocation against STN_UNDEF and
15392          leave everything up to the linker.  */
15393   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
15394     fixP->fx_done = 1;
15395
15396   switch (fixP->fx_r_type)
15397     {
15398     case BFD_RELOC_MIPS_TLS_GD:
15399     case BFD_RELOC_MIPS_TLS_LDM:
15400     case BFD_RELOC_MIPS_TLS_DTPREL32:
15401     case BFD_RELOC_MIPS_TLS_DTPREL64:
15402     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15403     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15404     case BFD_RELOC_MIPS_TLS_GOTTPREL:
15405     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15406     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
15407     case BFD_RELOC_MICROMIPS_TLS_GD:
15408     case BFD_RELOC_MICROMIPS_TLS_LDM:
15409     case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15410     case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15411     case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15412     case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15413     case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
15414       S_SET_THREAD_LOCAL (fixP->fx_addsy);
15415       /* fall through */
15416
15417     case BFD_RELOC_MIPS_JMP:
15418     case BFD_RELOC_MIPS_SHIFT5:
15419     case BFD_RELOC_MIPS_SHIFT6:
15420     case BFD_RELOC_MIPS_GOT_DISP:
15421     case BFD_RELOC_MIPS_GOT_PAGE:
15422     case BFD_RELOC_MIPS_GOT_OFST:
15423     case BFD_RELOC_MIPS_SUB:
15424     case BFD_RELOC_MIPS_INSERT_A:
15425     case BFD_RELOC_MIPS_INSERT_B:
15426     case BFD_RELOC_MIPS_DELETE:
15427     case BFD_RELOC_MIPS_HIGHEST:
15428     case BFD_RELOC_MIPS_HIGHER:
15429     case BFD_RELOC_MIPS_SCN_DISP:
15430     case BFD_RELOC_MIPS_REL16:
15431     case BFD_RELOC_MIPS_RELGOT:
15432     case BFD_RELOC_MIPS_JALR:
15433     case BFD_RELOC_HI16:
15434     case BFD_RELOC_HI16_S:
15435     case BFD_RELOC_GPREL16:
15436     case BFD_RELOC_MIPS_LITERAL:
15437     case BFD_RELOC_MIPS_CALL16:
15438     case BFD_RELOC_MIPS_GOT16:
15439     case BFD_RELOC_GPREL32:
15440     case BFD_RELOC_MIPS_GOT_HI16:
15441     case BFD_RELOC_MIPS_GOT_LO16:
15442     case BFD_RELOC_MIPS_CALL_HI16:
15443     case BFD_RELOC_MIPS_CALL_LO16:
15444     case BFD_RELOC_MIPS16_GPREL:
15445     case BFD_RELOC_MIPS16_GOT16:
15446     case BFD_RELOC_MIPS16_CALL16:
15447     case BFD_RELOC_MIPS16_HI16:
15448     case BFD_RELOC_MIPS16_HI16_S:
15449     case BFD_RELOC_MIPS16_JMP:
15450     case BFD_RELOC_MICROMIPS_JMP:
15451     case BFD_RELOC_MICROMIPS_GOT_DISP:
15452     case BFD_RELOC_MICROMIPS_GOT_PAGE:
15453     case BFD_RELOC_MICROMIPS_GOT_OFST:
15454     case BFD_RELOC_MICROMIPS_SUB:
15455     case BFD_RELOC_MICROMIPS_HIGHEST:
15456     case BFD_RELOC_MICROMIPS_HIGHER:
15457     case BFD_RELOC_MICROMIPS_SCN_DISP:
15458     case BFD_RELOC_MICROMIPS_JALR:
15459     case BFD_RELOC_MICROMIPS_HI16:
15460     case BFD_RELOC_MICROMIPS_HI16_S:
15461     case BFD_RELOC_MICROMIPS_GPREL16:
15462     case BFD_RELOC_MICROMIPS_LITERAL:
15463     case BFD_RELOC_MICROMIPS_CALL16:
15464     case BFD_RELOC_MICROMIPS_GOT16:
15465     case BFD_RELOC_MICROMIPS_GOT_HI16:
15466     case BFD_RELOC_MICROMIPS_GOT_LO16:
15467     case BFD_RELOC_MICROMIPS_CALL_HI16:
15468     case BFD_RELOC_MICROMIPS_CALL_LO16:
15469       /* Nothing needed to do.  The value comes from the reloc entry.  */
15470       break;
15471
15472     case BFD_RELOC_64:
15473       /* This is handled like BFD_RELOC_32, but we output a sign
15474          extended value if we are only 32 bits.  */
15475       if (fixP->fx_done)
15476         {
15477           if (8 <= sizeof (valueT))
15478             md_number_to_chars ((char *) buf, *valP, 8);
15479           else
15480             {
15481               valueT hiv;
15482
15483               if ((*valP & 0x80000000) != 0)
15484                 hiv = 0xffffffff;
15485               else
15486                 hiv = 0;
15487               md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
15488                                   *valP, 4);
15489               md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
15490                                   hiv, 4);
15491             }
15492         }
15493       break;
15494
15495     case BFD_RELOC_RVA:
15496     case BFD_RELOC_32:
15497     case BFD_RELOC_16:
15498       /* If we are deleting this reloc entry, we must fill in the
15499          value now.  This can happen if we have a .word which is not
15500          resolved when it appears but is later defined.  */
15501       if (fixP->fx_done)
15502         md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
15503       break;
15504
15505     case BFD_RELOC_LO16:
15506     case BFD_RELOC_MIPS16_LO16:
15507     case BFD_RELOC_MICROMIPS_LO16:
15508       /* FIXME: Now that embedded-PIC is gone, some of this code/comment
15509          may be safe to remove, but if so it's not obvious.  */
15510       /* When handling an embedded PIC switch statement, we can wind
15511          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
15512       if (fixP->fx_done)
15513         {
15514           if (*valP + 0x8000 > 0xffff)
15515             as_bad_where (fixP->fx_file, fixP->fx_line,
15516                           _("relocation overflow"));
15517           /* 32-bit microMIPS instructions are divided into two halfwords.
15518              Relocations always refer to the second halfword, regardless
15519              of endianness.  */
15520           if (target_big_endian || fixP->fx_r_type == BFD_RELOC_MICROMIPS_LO16)
15521             buf += 2;
15522           md_number_to_chars ((char *) buf, *valP, 2);
15523         }
15524       break;
15525
15526     case BFD_RELOC_16_PCREL_S2:
15527       if ((*valP & 0x3) != 0)
15528         as_bad_where (fixP->fx_file, fixP->fx_line,
15529                       _("Branch to misaligned address (%lx)"), (long) *valP);
15530
15531       /* We need to save the bits in the instruction since fixup_segment()
15532          might be deleting the relocation entry (i.e., a branch within
15533          the current segment).  */
15534       if (! fixP->fx_done)
15535         break;
15536
15537       /* Update old instruction data.  */
15538       if (target_big_endian)
15539         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
15540       else
15541         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
15542
15543       if (*valP + 0x20000 <= 0x3ffff)
15544         {
15545           insn |= (*valP >> 2) & 0xffff;
15546           md_number_to_chars ((char *) buf, insn, 4);
15547         }
15548       else if (mips_pic == NO_PIC
15549                && fixP->fx_done
15550                && fixP->fx_frag->fr_address >= text_section->vma
15551                && (fixP->fx_frag->fr_address
15552                    < text_section->vma + bfd_get_section_size (text_section))
15553                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
15554                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
15555                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
15556         {
15557           /* The branch offset is too large.  If this is an
15558              unconditional branch, and we are not generating PIC code,
15559              we can convert it to an absolute jump instruction.  */
15560           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
15561             insn = 0x0c000000;  /* jal */
15562           else
15563             insn = 0x08000000;  /* j */
15564           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15565           fixP->fx_done = 0;
15566           fixP->fx_addsy = section_symbol (text_section);
15567           *valP += md_pcrel_from (fixP);
15568           md_number_to_chars ((char *) buf, insn, 4);
15569         }
15570       else
15571         {
15572           /* If we got here, we have branch-relaxation disabled,
15573              and there's nothing we can do to fix this instruction
15574              without turning it into a longer sequence.  */
15575           as_bad_where (fixP->fx_file, fixP->fx_line,
15576                         _("Branch out of range"));
15577         }
15578       break;
15579
15580     case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15581     case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15582     case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15583       /* We adjust the offset back to even.  */
15584       if ((*valP & 0x1) != 0)
15585         --(*valP);
15586
15587       if (! fixP->fx_done)
15588         break;
15589
15590       /* Should never visit here, because we keep the relocation.  */
15591       abort ();
15592       break;
15593
15594     case BFD_RELOC_VTABLE_INHERIT:
15595       fixP->fx_done = 0;
15596       if (fixP->fx_addsy
15597           && !S_IS_DEFINED (fixP->fx_addsy)
15598           && !S_IS_WEAK (fixP->fx_addsy))
15599         S_SET_WEAK (fixP->fx_addsy);
15600       break;
15601
15602     case BFD_RELOC_VTABLE_ENTRY:
15603       fixP->fx_done = 0;
15604       break;
15605
15606     default:
15607       internalError ();
15608     }
15609
15610   /* Remember value for tc_gen_reloc.  */
15611   fixP->fx_addnumber = *valP;
15612 }
15613
15614 static symbolS *
15615 get_symbol (void)
15616 {
15617   int c;
15618   char *name;
15619   symbolS *p;
15620
15621   name = input_line_pointer;
15622   c = get_symbol_end ();
15623   p = (symbolS *) symbol_find_or_make (name);
15624   *input_line_pointer = c;
15625   return p;
15626 }
15627
15628 /* Align the current frag to a given power of two.  If a particular
15629    fill byte should be used, FILL points to an integer that contains
15630    that byte, otherwise FILL is null.
15631
15632    The MIPS assembler also automatically adjusts any preceding
15633    label.  */
15634
15635 static void
15636 mips_align (int to, int *fill, symbolS *label)
15637 {
15638   mips_emit_delays ();
15639   mips_record_compressed_mode ();
15640   if (fill == NULL && subseg_text_p (now_seg))
15641     frag_align_code (to, 0);
15642   else
15643     frag_align (to, fill ? *fill : 0, 0);
15644   record_alignment (now_seg, to);
15645   if (label != NULL)
15646     {
15647       gas_assert (S_GET_SEGMENT (label) == now_seg);
15648       symbol_set_frag (label, frag_now);
15649       S_SET_VALUE (label, (valueT) frag_now_fix ());
15650     }
15651 }
15652
15653 /* Align to a given power of two.  .align 0 turns off the automatic
15654    alignment used by the data creating pseudo-ops.  */
15655
15656 static void
15657 s_align (int x ATTRIBUTE_UNUSED)
15658 {
15659   int temp, fill_value, *fill_ptr;
15660   long max_alignment = 28;
15661
15662   /* o Note that the assembler pulls down any immediately preceding label
15663        to the aligned address.
15664      o It's not documented but auto alignment is reinstated by
15665        a .align pseudo instruction.
15666      o Note also that after auto alignment is turned off the mips assembler
15667        issues an error on attempt to assemble an improperly aligned data item.
15668        We don't.  */
15669
15670   temp = get_absolute_expression ();
15671   if (temp > max_alignment)
15672     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
15673   else if (temp < 0)
15674     {
15675       as_warn (_("Alignment negative: 0 assumed."));
15676       temp = 0;
15677     }
15678   if (*input_line_pointer == ',')
15679     {
15680       ++input_line_pointer;
15681       fill_value = get_absolute_expression ();
15682       fill_ptr = &fill_value;
15683     }
15684   else
15685     fill_ptr = 0;
15686   if (temp)
15687     {
15688       segment_info_type *si = seg_info (now_seg);
15689       struct insn_label_list *l = si->label_list;
15690       /* Auto alignment should be switched on by next section change.  */
15691       auto_align = 1;
15692       mips_align (temp, fill_ptr, l != NULL ? l->label : NULL);
15693     }
15694   else
15695     {
15696       auto_align = 0;
15697     }
15698
15699   demand_empty_rest_of_line ();
15700 }
15701
15702 static void
15703 s_change_sec (int sec)
15704 {
15705   segT seg;
15706
15707 #ifdef OBJ_ELF
15708   /* The ELF backend needs to know that we are changing sections, so
15709      that .previous works correctly.  We could do something like check
15710      for an obj_section_change_hook macro, but that might be confusing
15711      as it would not be appropriate to use it in the section changing
15712      functions in read.c, since obj-elf.c intercepts those.  FIXME:
15713      This should be cleaner, somehow.  */
15714   if (IS_ELF)
15715     obj_elf_section_change_hook ();
15716 #endif
15717
15718   mips_emit_delays ();
15719
15720   switch (sec)
15721     {
15722     case 't':
15723       s_text (0);
15724       break;
15725     case 'd':
15726       s_data (0);
15727       break;
15728     case 'b':
15729       subseg_set (bss_section, (subsegT) get_absolute_expression ());
15730       demand_empty_rest_of_line ();
15731       break;
15732
15733     case 'r':
15734       seg = subseg_new (RDATA_SECTION_NAME,
15735                         (subsegT) get_absolute_expression ());
15736       if (IS_ELF)
15737         {
15738           bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15739                                                   | SEC_READONLY | SEC_RELOC
15740                                                   | SEC_DATA));
15741           if (strncmp (TARGET_OS, "elf", 3) != 0)
15742             record_alignment (seg, 4);
15743         }
15744       demand_empty_rest_of_line ();
15745       break;
15746
15747     case 's':
15748       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
15749       if (IS_ELF)
15750         {
15751           bfd_set_section_flags (stdoutput, seg,
15752                                  SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15753           if (strncmp (TARGET_OS, "elf", 3) != 0)
15754             record_alignment (seg, 4);
15755         }
15756       demand_empty_rest_of_line ();
15757       break;
15758
15759     case 'B':
15760       seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
15761       if (IS_ELF)
15762         {
15763           bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15764           if (strncmp (TARGET_OS, "elf", 3) != 0)
15765             record_alignment (seg, 4);
15766         }
15767       demand_empty_rest_of_line ();
15768       break;
15769     }
15770
15771   auto_align = 1;
15772 }
15773
15774 void
15775 s_change_section (int ignore ATTRIBUTE_UNUSED)
15776 {
15777 #ifdef OBJ_ELF
15778   char *section_name;
15779   char c;
15780   char next_c = 0;
15781   int section_type;
15782   int section_flag;
15783   int section_entry_size;
15784   int section_alignment;
15785
15786   if (!IS_ELF)
15787     return;
15788
15789   section_name = input_line_pointer;
15790   c = get_symbol_end ();
15791   if (c)
15792     next_c = *(input_line_pointer + 1);
15793
15794   /* Do we have .section Name<,"flags">?  */
15795   if (c != ',' || (c == ',' && next_c == '"'))
15796     {
15797       /* just after name is now '\0'.  */
15798       *input_line_pointer = c;
15799       input_line_pointer = section_name;
15800       obj_elf_section (ignore);
15801       return;
15802     }
15803   input_line_pointer++;
15804
15805   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
15806   if (c == ',')
15807     section_type = get_absolute_expression ();
15808   else
15809     section_type = 0;
15810   if (*input_line_pointer++ == ',')
15811     section_flag = get_absolute_expression ();
15812   else
15813     section_flag = 0;
15814   if (*input_line_pointer++ == ',')
15815     section_entry_size = get_absolute_expression ();
15816   else
15817     section_entry_size = 0;
15818   if (*input_line_pointer++ == ',')
15819     section_alignment = get_absolute_expression ();
15820   else
15821     section_alignment = 0;
15822   /* FIXME: really ignore?  */
15823   (void) section_alignment;
15824
15825   section_name = xstrdup (section_name);
15826
15827   /* When using the generic form of .section (as implemented by obj-elf.c),
15828      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
15829      traditionally had to fall back on the more common @progbits instead.
15830
15831      There's nothing really harmful in this, since bfd will correct
15832      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
15833      means that, for backwards compatibility, the special_section entries
15834      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15835
15836      Even so, we shouldn't force users of the MIPS .section syntax to
15837      incorrectly label the sections as SHT_PROGBITS.  The best compromise
15838      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15839      generic type-checking code.  */
15840   if (section_type == SHT_MIPS_DWARF)
15841     section_type = SHT_PROGBITS;
15842
15843   obj_elf_change_section (section_name, section_type, section_flag,
15844                           section_entry_size, 0, 0, 0);
15845
15846   if (now_seg->name != section_name)
15847     free (section_name);
15848 #endif /* OBJ_ELF */
15849 }
15850
15851 void
15852 mips_enable_auto_align (void)
15853 {
15854   auto_align = 1;
15855 }
15856
15857 static void
15858 s_cons (int log_size)
15859 {
15860   segment_info_type *si = seg_info (now_seg);
15861   struct insn_label_list *l = si->label_list;
15862   symbolS *label;
15863
15864   label = l != NULL ? l->label : NULL;
15865   mips_emit_delays ();
15866   if (log_size > 0 && auto_align)
15867     mips_align (log_size, 0, label);
15868   cons (1 << log_size);
15869   mips_clear_insn_labels ();
15870 }
15871
15872 static void
15873 s_float_cons (int type)
15874 {
15875   segment_info_type *si = seg_info (now_seg);
15876   struct insn_label_list *l = si->label_list;
15877   symbolS *label;
15878
15879   label = l != NULL ? l->label : NULL;
15880
15881   mips_emit_delays ();
15882
15883   if (auto_align)
15884     {
15885       if (type == 'd')
15886         mips_align (3, 0, label);
15887       else
15888         mips_align (2, 0, label);
15889     }
15890
15891   float_cons (type);
15892   mips_clear_insn_labels ();
15893 }
15894
15895 /* Handle .globl.  We need to override it because on Irix 5 you are
15896    permitted to say
15897        .globl foo .text
15898    where foo is an undefined symbol, to mean that foo should be
15899    considered to be the address of a function.  */
15900
15901 static void
15902 s_mips_globl (int x ATTRIBUTE_UNUSED)
15903 {
15904   char *name;
15905   int c;
15906   symbolS *symbolP;
15907   flagword flag;
15908
15909   do
15910     {
15911       name = input_line_pointer;
15912       c = get_symbol_end ();
15913       symbolP = symbol_find_or_make (name);
15914       S_SET_EXTERNAL (symbolP);
15915
15916       *input_line_pointer = c;
15917       SKIP_WHITESPACE ();
15918
15919       /* On Irix 5, every global symbol that is not explicitly labelled as
15920          being a function is apparently labelled as being an object.  */
15921       flag = BSF_OBJECT;
15922
15923       if (!is_end_of_line[(unsigned char) *input_line_pointer]
15924           && (*input_line_pointer != ','))
15925         {
15926           char *secname;
15927           asection *sec;
15928
15929           secname = input_line_pointer;
15930           c = get_symbol_end ();
15931           sec = bfd_get_section_by_name (stdoutput, secname);
15932           if (sec == NULL)
15933             as_bad (_("%s: no such section"), secname);
15934           *input_line_pointer = c;
15935
15936           if (sec != NULL && (sec->flags & SEC_CODE) != 0)
15937             flag = BSF_FUNCTION;
15938         }
15939
15940       symbol_get_bfdsym (symbolP)->flags |= flag;
15941
15942       c = *input_line_pointer;
15943       if (c == ',')
15944         {
15945           input_line_pointer++;
15946           SKIP_WHITESPACE ();
15947           if (is_end_of_line[(unsigned char) *input_line_pointer])
15948             c = '\n';
15949         }
15950     }
15951   while (c == ',');
15952
15953   demand_empty_rest_of_line ();
15954 }
15955
15956 static void
15957 s_option (int x ATTRIBUTE_UNUSED)
15958 {
15959   char *opt;
15960   char c;
15961
15962   opt = input_line_pointer;
15963   c = get_symbol_end ();
15964
15965   if (*opt == 'O')
15966     {
15967       /* FIXME: What does this mean?  */
15968     }
15969   else if (strncmp (opt, "pic", 3) == 0)
15970     {
15971       int i;
15972
15973       i = atoi (opt + 3);
15974       if (i == 0)
15975         mips_pic = NO_PIC;
15976       else if (i == 2)
15977         {
15978           mips_pic = SVR4_PIC;
15979           mips_abicalls = TRUE;
15980         }
15981       else
15982         as_bad (_(".option pic%d not supported"), i);
15983
15984       if (mips_pic == SVR4_PIC)
15985         {
15986           if (g_switch_seen && g_switch_value != 0)
15987             as_warn (_("-G may not be used with SVR4 PIC code"));
15988           g_switch_value = 0;
15989           bfd_set_gp_size (stdoutput, 0);
15990         }
15991     }
15992   else
15993     as_warn (_("Unrecognized option \"%s\""), opt);
15994
15995   *input_line_pointer = c;
15996   demand_empty_rest_of_line ();
15997 }
15998
15999 /* This structure is used to hold a stack of .set values.  */
16000
16001 struct mips_option_stack
16002 {
16003   struct mips_option_stack *next;
16004   struct mips_set_options options;
16005 };
16006
16007 static struct mips_option_stack *mips_opts_stack;
16008
16009 /* Handle the .set pseudo-op.  */
16010
16011 static void
16012 s_mipsset (int x ATTRIBUTE_UNUSED)
16013 {
16014   char *name = input_line_pointer, ch;
16015
16016   while (!is_end_of_line[(unsigned char) *input_line_pointer])
16017     ++input_line_pointer;
16018   ch = *input_line_pointer;
16019   *input_line_pointer = '\0';
16020
16021   if (strcmp (name, "reorder") == 0)
16022     {
16023       if (mips_opts.noreorder)
16024         end_noreorder ();
16025     }
16026   else if (strcmp (name, "noreorder") == 0)
16027     {
16028       if (!mips_opts.noreorder)
16029         start_noreorder ();
16030     }
16031   else if (strncmp (name, "at=", 3) == 0)
16032     {
16033       char *s = name + 3;
16034
16035       if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16036         as_bad (_("Unrecognized register name `%s'"), s);
16037     }
16038   else if (strcmp (name, "at") == 0)
16039     {
16040       mips_opts.at = ATREG;
16041     }
16042   else if (strcmp (name, "noat") == 0)
16043     {
16044       mips_opts.at = ZERO;
16045     }
16046   else if (strcmp (name, "macro") == 0)
16047     {
16048       mips_opts.warn_about_macros = 0;
16049     }
16050   else if (strcmp (name, "nomacro") == 0)
16051     {
16052       if (mips_opts.noreorder == 0)
16053         as_bad (_("`noreorder' must be set before `nomacro'"));
16054       mips_opts.warn_about_macros = 1;
16055     }
16056   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16057     {
16058       mips_opts.nomove = 0;
16059     }
16060   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16061     {
16062       mips_opts.nomove = 1;
16063     }
16064   else if (strcmp (name, "bopt") == 0)
16065     {
16066       mips_opts.nobopt = 0;
16067     }
16068   else if (strcmp (name, "nobopt") == 0)
16069     {
16070       mips_opts.nobopt = 1;
16071     }
16072   else if (strcmp (name, "gp=default") == 0)
16073     mips_opts.gp32 = file_mips_gp32;
16074   else if (strcmp (name, "gp=32") == 0)
16075     mips_opts.gp32 = 1;
16076   else if (strcmp (name, "gp=64") == 0)
16077     {
16078       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
16079         as_warn (_("%s isa does not support 64-bit registers"),
16080                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16081       mips_opts.gp32 = 0;
16082     }
16083   else if (strcmp (name, "fp=default") == 0)
16084     mips_opts.fp32 = file_mips_fp32;
16085   else if (strcmp (name, "fp=32") == 0)
16086     mips_opts.fp32 = 1;
16087   else if (strcmp (name, "fp=64") == 0)
16088     {
16089       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
16090         as_warn (_("%s isa does not support 64-bit floating point registers"),
16091                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16092       mips_opts.fp32 = 0;
16093     }
16094   else if (strcmp (name, "softfloat") == 0)
16095     mips_opts.soft_float = 1;
16096   else if (strcmp (name, "hardfloat") == 0)
16097     mips_opts.soft_float = 0;
16098   else if (strcmp (name, "singlefloat") == 0)
16099     mips_opts.single_float = 1;
16100   else if (strcmp (name, "doublefloat") == 0)
16101     mips_opts.single_float = 0;
16102   else if (strcmp (name, "mips16") == 0
16103            || strcmp (name, "MIPS-16") == 0)
16104     {
16105       if (mips_opts.micromips == 1)
16106         as_fatal (_("`mips16' cannot be used with `micromips'"));
16107       mips_opts.mips16 = 1;
16108     }
16109   else if (strcmp (name, "nomips16") == 0
16110            || strcmp (name, "noMIPS-16") == 0)
16111     mips_opts.mips16 = 0;
16112   else if (strcmp (name, "micromips") == 0)
16113     {
16114       if (mips_opts.mips16 == 1)
16115         as_fatal (_("`micromips' cannot be used with `mips16'"));
16116       mips_opts.micromips = 1;
16117     }
16118   else if (strcmp (name, "nomicromips") == 0)
16119     mips_opts.micromips = 0;
16120   else if (strcmp (name, "smartmips") == 0)
16121     {
16122       if (!ISA_SUPPORTS_SMARTMIPS)
16123         as_warn (_("%s ISA does not support SmartMIPS ASE"), 
16124                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16125       mips_opts.ase_smartmips = 1;
16126     }
16127   else if (strcmp (name, "nosmartmips") == 0)
16128     mips_opts.ase_smartmips = 0;
16129   else if (strcmp (name, "mips3d") == 0)
16130     mips_opts.ase_mips3d = 1;
16131   else if (strcmp (name, "nomips3d") == 0)
16132     mips_opts.ase_mips3d = 0;
16133   else if (strcmp (name, "mdmx") == 0)
16134     mips_opts.ase_mdmx = 1;
16135   else if (strcmp (name, "nomdmx") == 0)
16136     mips_opts.ase_mdmx = 0;
16137   else if (strcmp (name, "dsp") == 0)
16138     {
16139       if (!ISA_SUPPORTS_DSP_ASE)
16140         as_warn (_("%s ISA does not support DSP ASE"), 
16141                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16142       mips_opts.ase_dsp = 1;
16143       mips_opts.ase_dspr2 = 0;
16144     }
16145   else if (strcmp (name, "nodsp") == 0)
16146     {
16147       mips_opts.ase_dsp = 0;
16148       mips_opts.ase_dspr2 = 0;
16149     }
16150   else if (strcmp (name, "dspr2") == 0)
16151     {
16152       if (!ISA_SUPPORTS_DSPR2_ASE)
16153         as_warn (_("%s ISA does not support DSP R2 ASE"),
16154                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16155       mips_opts.ase_dspr2 = 1;
16156       mips_opts.ase_dsp = 1;
16157     }
16158   else if (strcmp (name, "nodspr2") == 0)
16159     {
16160       mips_opts.ase_dspr2 = 0;
16161       mips_opts.ase_dsp = 0;
16162     }
16163   else if (strcmp (name, "mt") == 0)
16164     {
16165       if (!ISA_SUPPORTS_MT_ASE)
16166         as_warn (_("%s ISA does not support MT ASE"), 
16167                  mips_cpu_info_from_isa (mips_opts.isa)->name);
16168       mips_opts.ase_mt = 1;
16169     }
16170   else if (strcmp (name, "nomt") == 0)
16171     mips_opts.ase_mt = 0;
16172   else if (strcmp (name, "mcu") == 0)
16173     mips_opts.ase_mcu = 1;
16174   else if (strcmp (name, "nomcu") == 0)
16175     mips_opts.ase_mcu = 0;
16176   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
16177     {
16178       int reset = 0;
16179
16180       /* Permit the user to change the ISA and architecture on the fly.
16181          Needless to say, misuse can cause serious problems.  */
16182       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16183         {
16184           reset = 1;
16185           mips_opts.isa = file_mips_isa;
16186           mips_opts.arch = file_mips_arch;
16187         }
16188       else if (strncmp (name, "arch=", 5) == 0)
16189         {
16190           const struct mips_cpu_info *p;
16191
16192           p = mips_parse_cpu("internal use", name + 5);
16193           if (!p)
16194             as_bad (_("unknown architecture %s"), name + 5);
16195           else
16196             {
16197               mips_opts.arch = p->cpu;
16198               mips_opts.isa = p->isa;
16199             }
16200         }
16201       else if (strncmp (name, "mips", 4) == 0)
16202         {
16203           const struct mips_cpu_info *p;
16204
16205           p = mips_parse_cpu("internal use", name);
16206           if (!p)
16207             as_bad (_("unknown ISA level %s"), name + 4);
16208           else
16209             {
16210               mips_opts.arch = p->cpu;
16211               mips_opts.isa = p->isa;
16212             }
16213         }
16214       else
16215         as_bad (_("unknown ISA or architecture %s"), name);
16216
16217       switch (mips_opts.isa)
16218         {
16219         case  0:
16220           break;
16221         case ISA_MIPS1:
16222         case ISA_MIPS2:
16223         case ISA_MIPS32:
16224         case ISA_MIPS32R2:
16225           mips_opts.gp32 = 1;
16226           mips_opts.fp32 = 1;
16227           break;
16228         case ISA_MIPS3:
16229         case ISA_MIPS4:
16230         case ISA_MIPS5:
16231         case ISA_MIPS64:
16232         case ISA_MIPS64R2:
16233           mips_opts.gp32 = 0;
16234           mips_opts.fp32 = 0;
16235           break;
16236         default:
16237           as_bad (_("unknown ISA level %s"), name + 4);
16238           break;
16239         }
16240       if (reset)
16241         {
16242           mips_opts.gp32 = file_mips_gp32;
16243           mips_opts.fp32 = file_mips_fp32;
16244         }
16245     }
16246   else if (strcmp (name, "autoextend") == 0)
16247     mips_opts.noautoextend = 0;
16248   else if (strcmp (name, "noautoextend") == 0)
16249     mips_opts.noautoextend = 1;
16250   else if (strcmp (name, "push") == 0)
16251     {
16252       struct mips_option_stack *s;
16253
16254       s = (struct mips_option_stack *) xmalloc (sizeof *s);
16255       s->next = mips_opts_stack;
16256       s->options = mips_opts;
16257       mips_opts_stack = s;
16258     }
16259   else if (strcmp (name, "pop") == 0)
16260     {
16261       struct mips_option_stack *s;
16262
16263       s = mips_opts_stack;
16264       if (s == NULL)
16265         as_bad (_(".set pop with no .set push"));
16266       else
16267         {
16268           /* If we're changing the reorder mode we need to handle
16269              delay slots correctly.  */
16270           if (s->options.noreorder && ! mips_opts.noreorder)
16271             start_noreorder ();
16272           else if (! s->options.noreorder && mips_opts.noreorder)
16273             end_noreorder ();
16274
16275           mips_opts = s->options;
16276           mips_opts_stack = s->next;
16277           free (s);
16278         }
16279     }
16280   else if (strcmp (name, "sym32") == 0)
16281     mips_opts.sym32 = TRUE;
16282   else if (strcmp (name, "nosym32") == 0)
16283     mips_opts.sym32 = FALSE;
16284   else if (strchr (name, ','))
16285     {
16286       /* Generic ".set" directive; use the generic handler.  */
16287       *input_line_pointer = ch;
16288       input_line_pointer = name;
16289       s_set (0);
16290       return;
16291     }
16292   else
16293     {
16294       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16295     }
16296   *input_line_pointer = ch;
16297   demand_empty_rest_of_line ();
16298 }
16299
16300 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
16301    .option pic2.  It means to generate SVR4 PIC calls.  */
16302
16303 static void
16304 s_abicalls (int ignore ATTRIBUTE_UNUSED)
16305 {
16306   mips_pic = SVR4_PIC;
16307   mips_abicalls = TRUE;
16308
16309   if (g_switch_seen && g_switch_value != 0)
16310     as_warn (_("-G may not be used with SVR4 PIC code"));
16311   g_switch_value = 0;
16312
16313   bfd_set_gp_size (stdoutput, 0);
16314   demand_empty_rest_of_line ();
16315 }
16316
16317 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
16318    PIC code.  It sets the $gp register for the function based on the
16319    function address, which is in the register named in the argument.
16320    This uses a relocation against _gp_disp, which is handled specially
16321    by the linker.  The result is:
16322         lui     $gp,%hi(_gp_disp)
16323         addiu   $gp,$gp,%lo(_gp_disp)
16324         addu    $gp,$gp,.cpload argument
16325    The .cpload argument is normally $25 == $t9.
16326
16327    The -mno-shared option changes this to:
16328         lui     $gp,%hi(__gnu_local_gp)
16329         addiu   $gp,$gp,%lo(__gnu_local_gp)
16330    and the argument is ignored.  This saves an instruction, but the
16331    resulting code is not position independent; it uses an absolute
16332    address for __gnu_local_gp.  Thus code assembled with -mno-shared
16333    can go into an ordinary executable, but not into a shared library.  */
16334
16335 static void
16336 s_cpload (int ignore ATTRIBUTE_UNUSED)
16337 {
16338   expressionS ex;
16339   int reg;
16340   int in_shared;
16341
16342   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16343      .cpload is ignored.  */
16344   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16345     {
16346       s_ignore (0);
16347       return;
16348     }
16349
16350   /* .cpload should be in a .set noreorder section.  */
16351   if (mips_opts.noreorder == 0)
16352     as_warn (_(".cpload not in noreorder section"));
16353
16354   reg = tc_get_register (0);
16355
16356   /* If we need to produce a 64-bit address, we are better off using
16357      the default instruction sequence.  */
16358   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
16359
16360   ex.X_op = O_symbol;
16361   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16362                                          "__gnu_local_gp");
16363   ex.X_op_symbol = NULL;
16364   ex.X_add_number = 0;
16365
16366   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16367   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16368
16369   macro_start ();
16370   macro_build_lui (&ex, mips_gp_register);
16371   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16372                mips_gp_register, BFD_RELOC_LO16);
16373   if (in_shared)
16374     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16375                  mips_gp_register, reg);
16376   macro_end ();
16377
16378   demand_empty_rest_of_line ();
16379 }
16380
16381 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
16382      .cpsetup $reg1, offset|$reg2, label
16383
16384    If offset is given, this results in:
16385      sd         $gp, offset($sp)
16386      lui        $gp, %hi(%neg(%gp_rel(label)))
16387      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16388      daddu      $gp, $gp, $reg1
16389
16390    If $reg2 is given, this results in:
16391      daddu      $reg2, $gp, $0
16392      lui        $gp, %hi(%neg(%gp_rel(label)))
16393      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
16394      daddu      $gp, $gp, $reg1
16395    $reg1 is normally $25 == $t9.
16396
16397    The -mno-shared option replaces the last three instructions with
16398         lui     $gp,%hi(_gp)
16399         addiu   $gp,$gp,%lo(_gp)  */
16400
16401 static void
16402 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
16403 {
16404   expressionS ex_off;
16405   expressionS ex_sym;
16406   int reg1;
16407
16408   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
16409      We also need NewABI support.  */
16410   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16411     {
16412       s_ignore (0);
16413       return;
16414     }
16415
16416   reg1 = tc_get_register (0);
16417   SKIP_WHITESPACE ();
16418   if (*input_line_pointer != ',')
16419     {
16420       as_bad (_("missing argument separator ',' for .cpsetup"));
16421       return;
16422     }
16423   else
16424     ++input_line_pointer;
16425   SKIP_WHITESPACE ();
16426   if (*input_line_pointer == '$')
16427     {
16428       mips_cpreturn_register = tc_get_register (0);
16429       mips_cpreturn_offset = -1;
16430     }
16431   else
16432     {
16433       mips_cpreturn_offset = get_absolute_expression ();
16434       mips_cpreturn_register = -1;
16435     }
16436   SKIP_WHITESPACE ();
16437   if (*input_line_pointer != ',')
16438     {
16439       as_bad (_("missing argument separator ',' for .cpsetup"));
16440       return;
16441     }
16442   else
16443     ++input_line_pointer;
16444   SKIP_WHITESPACE ();
16445   expression (&ex_sym);
16446
16447   macro_start ();
16448   if (mips_cpreturn_register == -1)
16449     {
16450       ex_off.X_op = O_constant;
16451       ex_off.X_add_symbol = NULL;
16452       ex_off.X_op_symbol = NULL;
16453       ex_off.X_add_number = mips_cpreturn_offset;
16454
16455       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
16456                    BFD_RELOC_LO16, SP);
16457     }
16458   else
16459     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
16460                  mips_gp_register, 0);
16461
16462   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
16463     {
16464       macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
16465                    -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16466                    BFD_RELOC_HI16_S);
16467
16468       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16469                    mips_gp_register, -1, BFD_RELOC_GPREL16,
16470                    BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16471
16472       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16473                    mips_gp_register, reg1);
16474     }
16475   else
16476     {
16477       expressionS ex;
16478
16479       ex.X_op = O_symbol;
16480       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
16481       ex.X_op_symbol = NULL;
16482       ex.X_add_number = 0;
16483
16484       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
16485       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16486
16487       macro_build_lui (&ex, mips_gp_register);
16488       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16489                    mips_gp_register, BFD_RELOC_LO16);
16490     }
16491
16492   macro_end ();
16493
16494   demand_empty_rest_of_line ();
16495 }
16496
16497 static void
16498 s_cplocal (int ignore ATTRIBUTE_UNUSED)
16499 {
16500   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
16501      .cplocal is ignored.  */
16502   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16503     {
16504       s_ignore (0);
16505       return;
16506     }
16507
16508   mips_gp_register = tc_get_register (0);
16509   demand_empty_rest_of_line ();
16510 }
16511
16512 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
16513    offset from $sp.  The offset is remembered, and after making a PIC
16514    call $gp is restored from that location.  */
16515
16516 static void
16517 s_cprestore (int ignore ATTRIBUTE_UNUSED)
16518 {
16519   expressionS ex;
16520
16521   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16522      .cprestore is ignored.  */
16523   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
16524     {
16525       s_ignore (0);
16526       return;
16527     }
16528
16529   mips_cprestore_offset = get_absolute_expression ();
16530   mips_cprestore_valid = 1;
16531
16532   ex.X_op = O_constant;
16533   ex.X_add_symbol = NULL;
16534   ex.X_op_symbol = NULL;
16535   ex.X_add_number = mips_cprestore_offset;
16536
16537   macro_start ();
16538   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16539                                 SP, HAVE_64BIT_ADDRESSES);
16540   macro_end ();
16541
16542   demand_empty_rest_of_line ();
16543 }
16544
16545 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
16546    was given in the preceding .cpsetup, it results in:
16547      ld         $gp, offset($sp)
16548
16549    If a register $reg2 was given there, it results in:
16550      daddu      $gp, $reg2, $0  */
16551
16552 static void
16553 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
16554 {
16555   expressionS ex;
16556
16557   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16558      We also need NewABI support.  */
16559   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16560     {
16561       s_ignore (0);
16562       return;
16563     }
16564
16565   macro_start ();
16566   if (mips_cpreturn_register == -1)
16567     {
16568       ex.X_op = O_constant;
16569       ex.X_add_symbol = NULL;
16570       ex.X_op_symbol = NULL;
16571       ex.X_add_number = mips_cpreturn_offset;
16572
16573       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
16574     }
16575   else
16576     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
16577                  mips_cpreturn_register, 0);
16578   macro_end ();
16579
16580   demand_empty_rest_of_line ();
16581 }
16582
16583 /* Handle the .dtprelword and .dtpreldword pseudo-ops.  They generate
16584    a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
16585    use in DWARF debug information.  */
16586
16587 static void
16588 s_dtprel_internal (size_t bytes)
16589 {
16590   expressionS ex;
16591   char *p;
16592
16593   expression (&ex);
16594
16595   if (ex.X_op != O_symbol)
16596     {
16597       as_bad (_("Unsupported use of %s"), (bytes == 8
16598                                            ? ".dtpreldword"
16599                                            : ".dtprelword"));
16600       ignore_rest_of_line ();
16601     }
16602
16603   p = frag_more (bytes);
16604   md_number_to_chars (p, 0, bytes);
16605   fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
16606                (bytes == 8
16607                 ? BFD_RELOC_MIPS_TLS_DTPREL64
16608                 : BFD_RELOC_MIPS_TLS_DTPREL32));
16609
16610   demand_empty_rest_of_line ();
16611 }
16612
16613 /* Handle .dtprelword.  */
16614
16615 static void
16616 s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16617 {
16618   s_dtprel_internal (4);
16619 }
16620
16621 /* Handle .dtpreldword.  */
16622
16623 static void
16624 s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16625 {
16626   s_dtprel_internal (8);
16627 }
16628
16629 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
16630    code.  It sets the offset to use in gp_rel relocations.  */
16631
16632 static void
16633 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
16634 {
16635   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16636      We also need NewABI support.  */
16637   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16638     {
16639       s_ignore (0);
16640       return;
16641     }
16642
16643   mips_gprel_offset = get_absolute_expression ();
16644
16645   demand_empty_rest_of_line ();
16646 }
16647
16648 /* Handle the .gpword pseudo-op.  This is used when generating PIC
16649    code.  It generates a 32 bit GP relative reloc.  */
16650
16651 static void
16652 s_gpword (int ignore ATTRIBUTE_UNUSED)
16653 {
16654   segment_info_type *si;
16655   struct insn_label_list *l;
16656   symbolS *label;
16657   expressionS ex;
16658   char *p;
16659
16660   /* When not generating PIC code, this is treated as .word.  */
16661   if (mips_pic != SVR4_PIC)
16662     {
16663       s_cons (2);
16664       return;
16665     }
16666
16667   si = seg_info (now_seg);
16668   l = si->label_list;
16669   label = l != NULL ? l->label : NULL;
16670   mips_emit_delays ();
16671   if (auto_align)
16672     mips_align (2, 0, label);
16673
16674   expression (&ex);
16675   mips_clear_insn_labels ();
16676
16677   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16678     {
16679       as_bad (_("Unsupported use of .gpword"));
16680       ignore_rest_of_line ();
16681     }
16682
16683   p = frag_more (4);
16684   md_number_to_chars (p, 0, 4);
16685   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16686                BFD_RELOC_GPREL32);
16687
16688   demand_empty_rest_of_line ();
16689 }
16690
16691 static void
16692 s_gpdword (int ignore ATTRIBUTE_UNUSED)
16693 {
16694   segment_info_type *si;
16695   struct insn_label_list *l;
16696   symbolS *label;
16697   expressionS ex;
16698   char *p;
16699
16700   /* When not generating PIC code, this is treated as .dword.  */
16701   if (mips_pic != SVR4_PIC)
16702     {
16703       s_cons (3);
16704       return;
16705     }
16706
16707   si = seg_info (now_seg);
16708   l = si->label_list;
16709   label = l != NULL ? l->label : NULL;
16710   mips_emit_delays ();
16711   if (auto_align)
16712     mips_align (3, 0, label);
16713
16714   expression (&ex);
16715   mips_clear_insn_labels ();
16716
16717   if (ex.X_op != O_symbol || ex.X_add_number != 0)
16718     {
16719       as_bad (_("Unsupported use of .gpdword"));
16720       ignore_rest_of_line ();
16721     }
16722
16723   p = frag_more (8);
16724   md_number_to_chars (p, 0, 8);
16725   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
16726                BFD_RELOC_GPREL32)->fx_tcbit = 1;
16727
16728   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
16729   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16730            FALSE, BFD_RELOC_64)->fx_tcbit = 1;
16731
16732   demand_empty_rest_of_line ();
16733 }
16734
16735 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
16736    tables in SVR4 PIC code.  */
16737
16738 static void
16739 s_cpadd (int ignore ATTRIBUTE_UNUSED)
16740 {
16741   int reg;
16742
16743   /* This is ignored when not generating SVR4 PIC code.  */
16744   if (mips_pic != SVR4_PIC)
16745     {
16746       s_ignore (0);
16747       return;
16748     }
16749
16750   /* Add $gp to the register named as an argument.  */
16751   macro_start ();
16752   reg = tc_get_register (0);
16753   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
16754   macro_end ();
16755
16756   demand_empty_rest_of_line ();
16757 }
16758
16759 /* Handle the .insn pseudo-op.  This marks instruction labels in
16760    mips16/micromips mode.  This permits the linker to handle them specially,
16761    such as generating jalx instructions when needed.  We also make
16762    them odd for the duration of the assembly, in order to generate the
16763    right sort of code.  We will make them even in the adjust_symtab
16764    routine, while leaving them marked.  This is convenient for the
16765    debugger and the disassembler.  The linker knows to make them odd
16766    again.  */
16767
16768 static void
16769 s_insn (int ignore ATTRIBUTE_UNUSED)
16770 {
16771   mips_mark_labels ();
16772
16773   demand_empty_rest_of_line ();
16774 }
16775
16776 /* Handle a .stabn directive.  We need these in order to mark a label
16777    as being a mips16 text label correctly.  Sometimes the compiler
16778    will emit a label, followed by a .stabn, and then switch sections.
16779    If the label and .stabn are in mips16 mode, then the label is
16780    really a mips16 text label.  */
16781
16782 static void
16783 s_mips_stab (int type)
16784 {
16785   if (type == 'n')
16786     mips_mark_labels ();
16787
16788   s_stab (type);
16789 }
16790
16791 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.  */
16792
16793 static void
16794 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
16795 {
16796   char *name;
16797   int c;
16798   symbolS *symbolP;
16799   expressionS exp;
16800
16801   name = input_line_pointer;
16802   c = get_symbol_end ();
16803   symbolP = symbol_find_or_make (name);
16804   S_SET_WEAK (symbolP);
16805   *input_line_pointer = c;
16806
16807   SKIP_WHITESPACE ();
16808
16809   if (! is_end_of_line[(unsigned char) *input_line_pointer])
16810     {
16811       if (S_IS_DEFINED (symbolP))
16812         {
16813           as_bad (_("ignoring attempt to redefine symbol %s"),
16814                   S_GET_NAME (symbolP));
16815           ignore_rest_of_line ();
16816           return;
16817         }
16818
16819       if (*input_line_pointer == ',')
16820         {
16821           ++input_line_pointer;
16822           SKIP_WHITESPACE ();
16823         }
16824
16825       expression (&exp);
16826       if (exp.X_op != O_symbol)
16827         {
16828           as_bad (_("bad .weakext directive"));
16829           ignore_rest_of_line ();
16830           return;
16831         }
16832       symbol_set_value_expression (symbolP, &exp);
16833     }
16834
16835   demand_empty_rest_of_line ();
16836 }
16837
16838 /* Parse a register string into a number.  Called from the ECOFF code
16839    to parse .frame.  The argument is non-zero if this is the frame
16840    register, so that we can record it in mips_frame_reg.  */
16841
16842 int
16843 tc_get_register (int frame)
16844 {
16845   unsigned int reg;
16846
16847   SKIP_WHITESPACE ();
16848   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
16849     reg = 0;
16850   if (frame)
16851     {
16852       mips_frame_reg = reg != 0 ? reg : SP;
16853       mips_frame_reg_valid = 1;
16854       mips_cprestore_valid = 0;
16855     }
16856   return reg;
16857 }
16858
16859 valueT
16860 md_section_align (asection *seg, valueT addr)
16861 {
16862   int align = bfd_get_section_alignment (stdoutput, seg);
16863
16864   if (IS_ELF)
16865     {
16866       /* We don't need to align ELF sections to the full alignment.
16867          However, Irix 5 may prefer that we align them at least to a 16
16868          byte boundary.  We don't bother to align the sections if we
16869          are targeted for an embedded system.  */
16870       if (strncmp (TARGET_OS, "elf", 3) == 0)
16871         return addr;
16872       if (align > 4)
16873         align = 4;
16874     }
16875
16876   return ((addr + (1 << align) - 1) & (-1 << align));
16877 }
16878
16879 /* Utility routine, called from above as well.  If called while the
16880    input file is still being read, it's only an approximation.  (For
16881    example, a symbol may later become defined which appeared to be
16882    undefined earlier.)  */
16883
16884 static int
16885 nopic_need_relax (symbolS *sym, int before_relaxing)
16886 {
16887   if (sym == 0)
16888     return 0;
16889
16890   if (g_switch_value > 0)
16891     {
16892       const char *symname;
16893       int change;
16894
16895       /* Find out whether this symbol can be referenced off the $gp
16896          register.  It can be if it is smaller than the -G size or if
16897          it is in the .sdata or .sbss section.  Certain symbols can
16898          not be referenced off the $gp, although it appears as though
16899          they can.  */
16900       symname = S_GET_NAME (sym);
16901       if (symname != (const char *) NULL
16902           && (strcmp (symname, "eprol") == 0
16903               || strcmp (symname, "etext") == 0
16904               || strcmp (symname, "_gp") == 0
16905               || strcmp (symname, "edata") == 0
16906               || strcmp (symname, "_fbss") == 0
16907               || strcmp (symname, "_fdata") == 0
16908               || strcmp (symname, "_ftext") == 0
16909               || strcmp (symname, "end") == 0
16910               || strcmp (symname, "_gp_disp") == 0))
16911         change = 1;
16912       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
16913                && (0
16914 #ifndef NO_ECOFF_DEBUGGING
16915                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
16916                        && (symbol_get_obj (sym)->ecoff_extern_size
16917                            <= g_switch_value))
16918 #endif
16919                    /* We must defer this decision until after the whole
16920                       file has been read, since there might be a .extern
16921                       after the first use of this symbol.  */
16922                    || (before_relaxing
16923 #ifndef NO_ECOFF_DEBUGGING
16924                        && symbol_get_obj (sym)->ecoff_extern_size == 0
16925 #endif
16926                        && S_GET_VALUE (sym) == 0)
16927                    || (S_GET_VALUE (sym) != 0
16928                        && S_GET_VALUE (sym) <= g_switch_value)))
16929         change = 0;
16930       else
16931         {
16932           const char *segname;
16933
16934           segname = segment_name (S_GET_SEGMENT (sym));
16935           gas_assert (strcmp (segname, ".lit8") != 0
16936                   && strcmp (segname, ".lit4") != 0);
16937           change = (strcmp (segname, ".sdata") != 0
16938                     && strcmp (segname, ".sbss") != 0
16939                     && strncmp (segname, ".sdata.", 7) != 0
16940                     && strncmp (segname, ".sbss.", 6) != 0
16941                     && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
16942                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
16943         }
16944       return change;
16945     }
16946   else
16947     /* We are not optimizing for the $gp register.  */
16948     return 1;
16949 }
16950
16951
16952 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
16953
16954 static bfd_boolean
16955 pic_need_relax (symbolS *sym, asection *segtype)
16956 {
16957   asection *symsec;
16958
16959   /* Handle the case of a symbol equated to another symbol.  */
16960   while (symbol_equated_reloc_p (sym))
16961     {
16962       symbolS *n;
16963
16964       /* It's possible to get a loop here in a badly written program.  */
16965       n = symbol_get_value_expression (sym)->X_add_symbol;
16966       if (n == sym)
16967         break;
16968       sym = n;
16969     }
16970
16971   if (symbol_section_p (sym))
16972     return TRUE;
16973
16974   symsec = S_GET_SEGMENT (sym);
16975
16976   /* This must duplicate the test in adjust_reloc_syms.  */
16977   return (symsec != &bfd_und_section
16978           && symsec != &bfd_abs_section
16979           && !bfd_is_com_section (symsec)
16980           && !s_is_linkonce (sym, segtype)
16981 #ifdef OBJ_ELF
16982           /* A global or weak symbol is treated as external.  */
16983           && (!IS_ELF || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
16984 #endif
16985           );
16986 }
16987
16988
16989 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
16990    extended opcode.  SEC is the section the frag is in.  */
16991
16992 static int
16993 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
16994 {
16995   int type;
16996   const struct mips16_immed_operand *op;
16997   offsetT val;
16998   int mintiny, maxtiny;
16999   segT symsec;
17000   fragS *sym_frag;
17001
17002   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17003     return 0;
17004   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17005     return 1;
17006
17007   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17008   op = mips16_immed_operands;
17009   while (op->type != type)
17010     {
17011       ++op;
17012       gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
17013     }
17014
17015   if (op->unsp)
17016     {
17017       if (type == '<' || type == '>' || type == '[' || type == ']')
17018         {
17019           mintiny = 1;
17020           maxtiny = 1 << op->nbits;
17021         }
17022       else
17023         {
17024           mintiny = 0;
17025           maxtiny = (1 << op->nbits) - 1;
17026         }
17027     }
17028   else
17029     {
17030       mintiny = - (1 << (op->nbits - 1));
17031       maxtiny = (1 << (op->nbits - 1)) - 1;
17032     }
17033
17034   sym_frag = symbol_get_frag (fragp->fr_symbol);
17035   val = S_GET_VALUE (fragp->fr_symbol);
17036   symsec = S_GET_SEGMENT (fragp->fr_symbol);
17037
17038   if (op->pcrel)
17039     {
17040       addressT addr;
17041
17042       /* We won't have the section when we are called from
17043          mips_relax_frag.  However, we will always have been called
17044          from md_estimate_size_before_relax first.  If this is a
17045          branch to a different section, we mark it as such.  If SEC is
17046          NULL, and the frag is not marked, then it must be a branch to
17047          the same section.  */
17048       if (sec == NULL)
17049         {
17050           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17051             return 1;
17052         }
17053       else
17054         {
17055           /* Must have been called from md_estimate_size_before_relax.  */
17056           if (symsec != sec)
17057             {
17058               fragp->fr_subtype =
17059                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17060
17061               /* FIXME: We should support this, and let the linker
17062                  catch branches and loads that are out of range.  */
17063               as_bad_where (fragp->fr_file, fragp->fr_line,
17064                             _("unsupported PC relative reference to different section"));
17065
17066               return 1;
17067             }
17068           if (fragp != sym_frag && sym_frag->fr_address == 0)
17069             /* Assume non-extended on the first relaxation pass.
17070                The address we have calculated will be bogus if this is
17071                a forward branch to another frag, as the forward frag
17072                will have fr_address == 0.  */
17073             return 0;
17074         }
17075
17076       /* In this case, we know for sure that the symbol fragment is in
17077          the same section.  If the relax_marker of the symbol fragment
17078          differs from the relax_marker of this fragment, we have not
17079          yet adjusted the symbol fragment fr_address.  We want to add
17080          in STRETCH in order to get a better estimate of the address.
17081          This particularly matters because of the shift bits.  */
17082       if (stretch != 0
17083           && sym_frag->relax_marker != fragp->relax_marker)
17084         {
17085           fragS *f;
17086
17087           /* Adjust stretch for any alignment frag.  Note that if have
17088              been expanding the earlier code, the symbol may be
17089              defined in what appears to be an earlier frag.  FIXME:
17090              This doesn't handle the fr_subtype field, which specifies
17091              a maximum number of bytes to skip when doing an
17092              alignment.  */
17093           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17094             {
17095               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17096                 {
17097                   if (stretch < 0)
17098                     stretch = - ((- stretch)
17099                                  & ~ ((1 << (int) f->fr_offset) - 1));
17100                   else
17101                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17102                   if (stretch == 0)
17103                     break;
17104                 }
17105             }
17106           if (f != NULL)
17107             val += stretch;
17108         }
17109
17110       addr = fragp->fr_address + fragp->fr_fix;
17111
17112       /* The base address rules are complicated.  The base address of
17113          a branch is the following instruction.  The base address of a
17114          PC relative load or add is the instruction itself, but if it
17115          is in a delay slot (in which case it can not be extended) use
17116          the address of the instruction whose delay slot it is in.  */
17117       if (type == 'p' || type == 'q')
17118         {
17119           addr += 2;
17120
17121           /* If we are currently assuming that this frag should be
17122              extended, then, the current address is two bytes
17123              higher.  */
17124           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17125             addr += 2;
17126
17127           /* Ignore the low bit in the target, since it will be set
17128              for a text label.  */
17129           if ((val & 1) != 0)
17130             --val;
17131         }
17132       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17133         addr -= 4;
17134       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17135         addr -= 2;
17136
17137       val -= addr & ~ ((1 << op->shift) - 1);
17138
17139       /* Branch offsets have an implicit 0 in the lowest bit.  */
17140       if (type == 'p' || type == 'q')
17141         val /= 2;
17142
17143       /* If any of the shifted bits are set, we must use an extended
17144          opcode.  If the address depends on the size of this
17145          instruction, this can lead to a loop, so we arrange to always
17146          use an extended opcode.  We only check this when we are in
17147          the main relaxation loop, when SEC is NULL.  */
17148       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17149         {
17150           fragp->fr_subtype =
17151             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17152           return 1;
17153         }
17154
17155       /* If we are about to mark a frag as extended because the value
17156          is precisely maxtiny + 1, then there is a chance of an
17157          infinite loop as in the following code:
17158              la $4,foo
17159              .skip      1020
17160              .align     2
17161            foo:
17162          In this case when the la is extended, foo is 0x3fc bytes
17163          away, so the la can be shrunk, but then foo is 0x400 away, so
17164          the la must be extended.  To avoid this loop, we mark the
17165          frag as extended if it was small, and is about to become
17166          extended with a value of maxtiny + 1.  */
17167       if (val == ((maxtiny + 1) << op->shift)
17168           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17169           && sec == NULL)
17170         {
17171           fragp->fr_subtype =
17172             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17173           return 1;
17174         }
17175     }
17176   else if (symsec != absolute_section && sec != NULL)
17177     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17178
17179   if ((val & ((1 << op->shift) - 1)) != 0
17180       || val < (mintiny << op->shift)
17181       || val > (maxtiny << op->shift))
17182     return 1;
17183   else
17184     return 0;
17185 }
17186
17187 /* Compute the length of a branch sequence, and adjust the
17188    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
17189    worst-case length is computed, with UPDATE being used to indicate
17190    whether an unconditional (-1), branch-likely (+1) or regular (0)
17191    branch is to be computed.  */
17192 static int
17193 relaxed_branch_length (fragS *fragp, asection *sec, int update)
17194 {
17195   bfd_boolean toofar;
17196   int length;
17197
17198   if (fragp
17199       && S_IS_DEFINED (fragp->fr_symbol)
17200       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17201     {
17202       addressT addr;
17203       offsetT val;
17204
17205       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17206
17207       addr = fragp->fr_address + fragp->fr_fix + 4;
17208
17209       val -= addr;
17210
17211       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17212     }
17213   else if (fragp)
17214     /* If the symbol is not defined or it's in a different segment,
17215        assume the user knows what's going on and emit a short
17216        branch.  */
17217     toofar = FALSE;
17218   else
17219     toofar = TRUE;
17220
17221   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17222     fragp->fr_subtype
17223       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17224                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
17225                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17226                              RELAX_BRANCH_LINK (fragp->fr_subtype),
17227                              toofar);
17228
17229   length = 4;
17230   if (toofar)
17231     {
17232       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17233         length += 8;
17234
17235       if (mips_pic != NO_PIC)
17236         {
17237           /* Additional space for PIC loading of target address.  */
17238           length += 8;
17239           if (mips_opts.isa == ISA_MIPS1)
17240             /* Additional space for $at-stabilizing nop.  */
17241             length += 4;
17242         }
17243
17244       /* If branch is conditional.  */
17245       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17246         length += 8;
17247     }
17248
17249   return length;
17250 }
17251
17252 /* Compute the length of a branch sequence, and adjust the
17253    RELAX_MICROMIPS_TOOFAR32 bit accordingly.  If FRAGP is NULL, the
17254    worst-case length is computed, with UPDATE being used to indicate
17255    whether an unconditional (-1), or regular (0) branch is to be
17256    computed.  */
17257
17258 static int
17259 relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17260 {
17261   bfd_boolean toofar;
17262   int length;
17263
17264   if (fragp
17265       && S_IS_DEFINED (fragp->fr_symbol)
17266       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17267     {
17268       addressT addr;
17269       offsetT val;
17270
17271       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17272       /* Ignore the low bit in the target, since it will be set
17273          for a text label.  */
17274       if ((val & 1) != 0)
17275         --val;
17276
17277       addr = fragp->fr_address + fragp->fr_fix + 4;
17278
17279       val -= addr;
17280
17281       toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17282     }
17283   else if (fragp)
17284     /* If the symbol is not defined or it's in a different segment,
17285        assume the user knows what's going on and emit a short
17286        branch.  */
17287     toofar = FALSE;
17288   else
17289     toofar = TRUE;
17290
17291   if (fragp && update
17292       && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17293     fragp->fr_subtype = (toofar
17294                          ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17295                          : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17296
17297   length = 4;
17298   if (toofar)
17299     {
17300       bfd_boolean compact_known = fragp != NULL;
17301       bfd_boolean compact = FALSE;
17302       bfd_boolean uncond;
17303
17304       if (compact_known)
17305         compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17306       if (fragp)
17307         uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17308       else
17309         uncond = update < 0;
17310
17311       /* If label is out of range, we turn branch <br>:
17312
17313                 <br>    label                   # 4 bytes
17314             0:
17315
17316          into:
17317
17318                 j       label                   # 4 bytes
17319                 nop                             # 2 bytes if compact && !PIC
17320             0:
17321        */
17322       if (mips_pic == NO_PIC && (!compact_known || compact))
17323         length += 2;
17324
17325       /* If assembling PIC code, we further turn:
17326
17327                         j       label                   # 4 bytes
17328
17329          into:
17330
17331                         lw/ld   at, %got(label)(gp)     # 4 bytes
17332                         d/addiu at, %lo(label)          # 4 bytes
17333                         jr/c    at                      # 2 bytes
17334        */
17335       if (mips_pic != NO_PIC)
17336         length += 6;
17337
17338       /* If branch <br> is conditional, we prepend negated branch <brneg>:
17339
17340                         <brneg> 0f                      # 4 bytes
17341                         nop                             # 2 bytes if !compact
17342        */
17343       if (!uncond)
17344         length += (compact_known && compact) ? 4 : 6;
17345     }
17346
17347   return length;
17348 }
17349
17350 /* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17351    bit accordingly.  */
17352
17353 static int
17354 relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17355 {
17356   bfd_boolean toofar;
17357
17358   if (fragp
17359       && S_IS_DEFINED (fragp->fr_symbol)
17360       && sec == S_GET_SEGMENT (fragp->fr_symbol))
17361     {
17362       addressT addr;
17363       offsetT val;
17364       int type;
17365
17366       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17367       /* Ignore the low bit in the target, since it will be set
17368          for a text label.  */
17369       if ((val & 1) != 0)
17370         --val;
17371
17372       /* Assume this is a 2-byte branch.  */
17373       addr = fragp->fr_address + fragp->fr_fix + 2;
17374
17375       /* We try to avoid the infinite loop by not adding 2 more bytes for
17376          long branches.  */
17377
17378       val -= addr;
17379
17380       type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17381       if (type == 'D')
17382         toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17383       else if (type == 'E')
17384         toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17385       else
17386         abort ();
17387     }
17388   else
17389     /* If the symbol is not defined or it's in a different segment,
17390        we emit a normal 32-bit branch.  */
17391     toofar = TRUE;
17392
17393   if (fragp && update
17394       && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17395     fragp->fr_subtype
17396       = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17397                : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17398
17399   if (toofar)
17400     return 4;
17401
17402   return 2;
17403 }
17404
17405 /* Estimate the size of a frag before relaxing.  Unless this is the
17406    mips16, we are not really relaxing here, and the final size is
17407    encoded in the subtype information.  For the mips16, we have to
17408    decide whether we are using an extended opcode or not.  */
17409
17410 int
17411 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
17412 {
17413   int change;
17414
17415   if (RELAX_BRANCH_P (fragp->fr_subtype))
17416     {
17417
17418       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17419
17420       return fragp->fr_var;
17421     }
17422
17423   if (RELAX_MIPS16_P (fragp->fr_subtype))
17424     /* We don't want to modify the EXTENDED bit here; it might get us
17425        into infinite loops.  We change it only in mips_relax_frag().  */
17426     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
17427
17428   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17429     {
17430       int length = 4;
17431
17432       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17433         length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17434       if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17435         length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17436       fragp->fr_var = length;
17437
17438       return length;
17439     }
17440
17441   if (mips_pic == NO_PIC)
17442     change = nopic_need_relax (fragp->fr_symbol, 0);
17443   else if (mips_pic == SVR4_PIC)
17444     change = pic_need_relax (fragp->fr_symbol, segtype);
17445   else if (mips_pic == VXWORKS_PIC)
17446     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
17447     change = 0;
17448   else
17449     abort ();
17450
17451   if (change)
17452     {
17453       fragp->fr_subtype |= RELAX_USE_SECOND;
17454       return -RELAX_FIRST (fragp->fr_subtype);
17455     }
17456   else
17457     return -RELAX_SECOND (fragp->fr_subtype);
17458 }
17459
17460 /* This is called to see whether a reloc against a defined symbol
17461    should be converted into a reloc against a section.  */
17462
17463 int
17464 mips_fix_adjustable (fixS *fixp)
17465 {
17466   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17467       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17468     return 0;
17469
17470   if (fixp->fx_addsy == NULL)
17471     return 1;
17472
17473   /* If symbol SYM is in a mergeable section, relocations of the form
17474      SYM + 0 can usually be made section-relative.  The mergeable data
17475      is then identified by the section offset rather than by the symbol.
17476
17477      However, if we're generating REL LO16 relocations, the offset is split
17478      between the LO16 and parterning high part relocation.  The linker will
17479      need to recalculate the complete offset in order to correctly identify
17480      the merge data.
17481
17482      The linker has traditionally not looked for the parterning high part
17483      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17484      placed anywhere.  Rather than break backwards compatibility by changing
17485      this, it seems better not to force the issue, and instead keep the
17486      original symbol.  This will work with either linker behavior.  */
17487   if ((lo16_reloc_p (fixp->fx_r_type)
17488        || reloc_needs_lo_p (fixp->fx_r_type))
17489       && HAVE_IN_PLACE_ADDENDS
17490       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17491     return 0;
17492
17493   /* There is no place to store an in-place offset for JALR relocations.
17494      Likewise an in-range offset of PC-relative relocations may overflow
17495      the in-place relocatable field if recalculated against the start
17496      address of the symbol's containing section.  */
17497   if (HAVE_IN_PLACE_ADDENDS
17498       && (fixp->fx_pcrel || jalr_reloc_p (fixp->fx_r_type)))
17499     return 0;
17500
17501 #ifdef OBJ_ELF
17502   /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17503      to a floating-point stub.  The same is true for non-R_MIPS16_26
17504      relocations against MIPS16 functions; in this case, the stub becomes
17505      the function's canonical address.
17506
17507      Floating-point stubs are stored in unique .mips16.call.* or
17508      .mips16.fn.* sections.  If a stub T for function F is in section S,
17509      the first relocation in section S must be against F; this is how the
17510      linker determines the target function.  All relocations that might
17511      resolve to T must also be against F.  We therefore have the following
17512      restrictions, which are given in an intentionally-redundant way:
17513
17514        1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17515           symbols.
17516
17517        2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17518           if that stub might be used.
17519
17520        3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17521           symbols.
17522
17523        4. We cannot reduce a stub's relocations against MIPS16 symbols if
17524           that stub might be used.
17525
17526      There is a further restriction:
17527
17528        5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17529           R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17530           targets with in-place addends; the relocation field cannot
17531           encode the low bit.
17532
17533      For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17534      against a MIPS16 symbol.  We deal with (5) by by not reducing any
17535      such relocations on REL targets.
17536
17537      We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17538      relocation against some symbol R, no relocation against R may be
17539      reduced.  (Note that this deals with (2) as well as (1) because
17540      relocations against global symbols will never be reduced on ELF
17541      targets.)  This approach is a little simpler than trying to detect
17542      stub sections, and gives the "all or nothing" per-symbol consistency
17543      that we have for MIPS16 symbols.  */
17544   if (IS_ELF
17545       && fixp->fx_subsy == NULL
17546       && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
17547           || *symbol_get_tc (fixp->fx_addsy)
17548           || (HAVE_IN_PLACE_ADDENDS
17549               && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17550               && jmp_reloc_p (fixp->fx_r_type))))
17551     return 0;
17552 #endif
17553
17554   return 1;
17555 }
17556
17557 /* Translate internal representation of relocation info to BFD target
17558    format.  */
17559
17560 arelent **
17561 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
17562 {
17563   static arelent *retval[4];
17564   arelent *reloc;
17565   bfd_reloc_code_real_type code;
17566
17567   memset (retval, 0, sizeof(retval));
17568   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
17569   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
17570   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
17571   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17572
17573   if (fixp->fx_pcrel)
17574     {
17575       gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17576                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17577                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
17578                   || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1);
17579
17580       /* At this point, fx_addnumber is "symbol offset - pcrel address".
17581          Relocations want only the symbol offset.  */
17582       reloc->addend = fixp->fx_addnumber + reloc->address;
17583       if (!IS_ELF)
17584         {
17585           /* A gruesome hack which is a result of the gruesome gas
17586              reloc handling.  What's worse, for COFF (as opposed to
17587              ECOFF), we might need yet another copy of reloc->address.
17588              See bfd_install_relocation.  */
17589           reloc->addend += reloc->address;
17590         }
17591     }
17592   else
17593     reloc->addend = fixp->fx_addnumber;
17594
17595   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17596      entry to be used in the relocation's section offset.  */
17597   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17598     {
17599       reloc->address = reloc->addend;
17600       reloc->addend = 0;
17601     }
17602
17603   code = fixp->fx_r_type;
17604
17605   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
17606   if (reloc->howto == NULL)
17607     {
17608       as_bad_where (fixp->fx_file, fixp->fx_line,
17609                     _("Can not represent %s relocation in this object file format"),
17610                     bfd_get_reloc_code_name (code));
17611       retval[0] = NULL;
17612     }
17613
17614   return retval;
17615 }
17616
17617 /* Relax a machine dependent frag.  This returns the amount by which
17618    the current size of the frag should change.  */
17619
17620 int
17621 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
17622 {
17623   if (RELAX_BRANCH_P (fragp->fr_subtype))
17624     {
17625       offsetT old_var = fragp->fr_var;
17626
17627       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
17628
17629       return fragp->fr_var - old_var;
17630     }
17631
17632   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17633     {
17634       offsetT old_var = fragp->fr_var;
17635       offsetT new_var = 4;
17636
17637       if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17638         new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17639       if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17640         new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17641       fragp->fr_var = new_var;
17642
17643       return new_var - old_var;
17644     }
17645
17646   if (! RELAX_MIPS16_P (fragp->fr_subtype))
17647     return 0;
17648
17649   if (mips16_extended_frag (fragp, NULL, stretch))
17650     {
17651       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17652         return 0;
17653       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17654       return 2;
17655     }
17656   else
17657     {
17658       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17659         return 0;
17660       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17661       return -2;
17662     }
17663
17664   return 0;
17665 }
17666
17667 /* Convert a machine dependent frag.  */
17668
17669 void
17670 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
17671 {
17672   if (RELAX_BRANCH_P (fragp->fr_subtype))
17673     {
17674       bfd_byte *buf;
17675       unsigned long insn;
17676       expressionS exp;
17677       fixS *fixp;
17678
17679       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
17680
17681       if (target_big_endian)
17682         insn = bfd_getb32 (buf);
17683       else
17684         insn = bfd_getl32 (buf);
17685
17686       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17687         {
17688           /* We generate a fixup instead of applying it right now
17689              because, if there are linker relaxations, we're going to
17690              need the relocations.  */
17691           exp.X_op = O_symbol;
17692           exp.X_add_symbol = fragp->fr_symbol;
17693           exp.X_add_number = fragp->fr_offset;
17694
17695           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
17696                               4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
17697           fixp->fx_file = fragp->fr_file;
17698           fixp->fx_line = fragp->fr_line;
17699
17700           md_number_to_chars ((char *) buf, insn, 4);
17701           buf += 4;
17702         }
17703       else
17704         {
17705           int i;
17706
17707           as_warn_where (fragp->fr_file, fragp->fr_line,
17708                          _("Relaxed out-of-range branch into a jump"));
17709
17710           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17711             goto uncond;
17712
17713           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17714             {
17715               /* Reverse the branch.  */
17716               switch ((insn >> 28) & 0xf)
17717                 {
17718                 case 4:
17719                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
17720                      have the condition reversed by tweaking a single
17721                      bit, and their opcodes all have 0x4???????.  */
17722                   gas_assert ((insn & 0xf1000000) == 0x41000000);
17723                   insn ^= 0x00010000;
17724                   break;
17725
17726                 case 0:
17727                   /* bltz       0x04000000      bgez    0x04010000
17728                      bltzal     0x04100000      bgezal  0x04110000  */
17729                   gas_assert ((insn & 0xfc0e0000) == 0x04000000);
17730                   insn ^= 0x00010000;
17731                   break;
17732
17733                 case 1:
17734                   /* beq        0x10000000      bne     0x14000000
17735                      blez       0x18000000      bgtz    0x1c000000  */
17736                   insn ^= 0x04000000;
17737                   break;
17738
17739                 default:
17740                   abort ();
17741                 }
17742             }
17743
17744           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17745             {
17746               /* Clear the and-link bit.  */
17747               gas_assert ((insn & 0xfc1c0000) == 0x04100000);
17748
17749               /* bltzal         0x04100000      bgezal  0x04110000
17750                  bltzall        0x04120000      bgezall 0x04130000  */
17751               insn &= ~0x00100000;
17752             }
17753
17754           /* Branch over the branch (if the branch was likely) or the
17755              full jump (not likely case).  Compute the offset from the
17756              current instruction to branch to.  */
17757           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17758             i = 16;
17759           else
17760             {
17761               /* How many bytes in instructions we've already emitted?  */
17762               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
17763               /* How many bytes in instructions from here to the end?  */
17764               i = fragp->fr_var - i;
17765             }
17766           /* Convert to instruction count.  */
17767           i >>= 2;
17768           /* Branch counts from the next instruction.  */
17769           i--;
17770           insn |= i;
17771           /* Branch over the jump.  */
17772           md_number_to_chars ((char *) buf, insn, 4);
17773           buf += 4;
17774
17775           /* nop */
17776           md_number_to_chars ((char *) buf, 0, 4);
17777           buf += 4;
17778
17779           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17780             {
17781               /* beql $0, $0, 2f */
17782               insn = 0x50000000;
17783               /* Compute the PC offset from the current instruction to
17784                  the end of the variable frag.  */
17785               /* How many bytes in instructions we've already emitted?  */
17786               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
17787               /* How many bytes in instructions from here to the end?  */
17788               i = fragp->fr_var - i;
17789               /* Convert to instruction count.  */
17790               i >>= 2;
17791               /* Don't decrement i, because we want to branch over the
17792                  delay slot.  */
17793
17794               insn |= i;
17795               md_number_to_chars ((char *) buf, insn, 4);
17796               buf += 4;
17797
17798               md_number_to_chars ((char *) buf, 0, 4);
17799               buf += 4;
17800             }
17801
17802         uncond:
17803           if (mips_pic == NO_PIC)
17804             {
17805               /* j or jal.  */
17806               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
17807                       ? 0x0c000000 : 0x08000000);
17808               exp.X_op = O_symbol;
17809               exp.X_add_symbol = fragp->fr_symbol;
17810               exp.X_add_number = fragp->fr_offset;
17811
17812               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
17813                                   4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
17814               fixp->fx_file = fragp->fr_file;
17815               fixp->fx_line = fragp->fr_line;
17816
17817               md_number_to_chars ((char *) buf, insn, 4);
17818               buf += 4;
17819             }
17820           else
17821             {
17822               unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
17823
17824               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
17825               insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
17826               insn |= at << OP_SH_RT;
17827               exp.X_op = O_symbol;
17828               exp.X_add_symbol = fragp->fr_symbol;
17829               exp.X_add_number = fragp->fr_offset;
17830
17831               if (fragp->fr_offset)
17832                 {
17833                   exp.X_add_symbol = make_expr_symbol (&exp);
17834                   exp.X_add_number = 0;
17835                 }
17836
17837               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
17838                                   4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
17839               fixp->fx_file = fragp->fr_file;
17840               fixp->fx_line = fragp->fr_line;
17841
17842               md_number_to_chars ((char *) buf, insn, 4);
17843               buf += 4;
17844
17845               if (mips_opts.isa == ISA_MIPS1)
17846                 {
17847                   /* nop */
17848                   md_number_to_chars ((char *) buf, 0, 4);
17849                   buf += 4;
17850                 }
17851
17852               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
17853               insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
17854               insn |= at << OP_SH_RS | at << OP_SH_RT;
17855
17856               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
17857                                   4, &exp, FALSE, BFD_RELOC_LO16);
17858               fixp->fx_file = fragp->fr_file;
17859               fixp->fx_line = fragp->fr_line;
17860
17861               md_number_to_chars ((char *) buf, insn, 4);
17862               buf += 4;
17863
17864               /* j(al)r $at.  */
17865               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17866                 insn = 0x0000f809;
17867               else
17868                 insn = 0x00000008;
17869               insn |= at << OP_SH_RS;
17870
17871               md_number_to_chars ((char *) buf, insn, 4);
17872               buf += 4;
17873             }
17874         }
17875
17876       gas_assert (buf == (bfd_byte *)fragp->fr_literal
17877               + fragp->fr_fix + fragp->fr_var);
17878
17879       fragp->fr_fix += fragp->fr_var;
17880
17881       return;
17882     }
17883
17884   /* Relax microMIPS branches.  */
17885   if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17886     {
17887       bfd_byte *buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
17888       bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17889       bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17890       int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17891       bfd_boolean short_ds;
17892       unsigned long insn;
17893       expressionS exp;
17894       fixS *fixp;
17895
17896       exp.X_op = O_symbol;
17897       exp.X_add_symbol = fragp->fr_symbol;
17898       exp.X_add_number = fragp->fr_offset;
17899
17900       fragp->fr_fix += fragp->fr_var;
17901
17902       /* Handle 16-bit branches that fit or are forced to fit.  */
17903       if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17904         {
17905           /* We generate a fixup instead of applying it right now,
17906              because if there is linker relaxation, we're going to
17907              need the relocations.  */
17908           if (type == 'D')
17909             fixp = fix_new_exp (fragp,
17910                                 buf - (bfd_byte *) fragp->fr_literal,
17911                                 2, &exp, TRUE,
17912                                 BFD_RELOC_MICROMIPS_10_PCREL_S1);
17913           else if (type == 'E')
17914             fixp = fix_new_exp (fragp,
17915                                 buf - (bfd_byte *) fragp->fr_literal,
17916                                 2, &exp, TRUE,
17917                                 BFD_RELOC_MICROMIPS_7_PCREL_S1);
17918           else
17919             abort ();
17920
17921           fixp->fx_file = fragp->fr_file;
17922           fixp->fx_line = fragp->fr_line;
17923
17924           /* These relocations can have an addend that won't fit in
17925              2 octets.  */
17926           fixp->fx_no_overflow = 1;
17927
17928           return;
17929         }
17930
17931       /* Handle 32-bit branches that fit or are forced to fit.  */
17932       if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17933           || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17934         {
17935           /* We generate a fixup instead of applying it right now,
17936              because if there is linker relaxation, we're going to
17937              need the relocations.  */
17938           fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
17939                               4, &exp, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
17940           fixp->fx_file = fragp->fr_file;
17941           fixp->fx_line = fragp->fr_line;
17942
17943           if (type == 0)
17944             return;
17945         }
17946
17947       /* Relax 16-bit branches to 32-bit branches.  */
17948       if (type != 0)
17949         {
17950           if (target_big_endian)
17951             insn = bfd_getb16 (buf);
17952           else
17953             insn = bfd_getl16 (buf);
17954
17955           if ((insn & 0xfc00) == 0xcc00)                /* b16  */
17956             insn = 0x94000000;                          /* beq  */
17957           else if ((insn & 0xdc00) == 0x8c00)           /* beqz16/bnez16  */
17958             {
17959               unsigned long regno;
17960
17961               regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
17962               regno = micromips_to_32_reg_d_map [regno];
17963               insn = ((insn & 0x2000) << 16) | 0x94000000;      /* beq/bne  */
17964               insn |= regno << MICROMIPSOP_SH_RS;
17965             }
17966           else
17967             abort ();
17968
17969           /* Nothing else to do, just write it out.  */
17970           if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17971               || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17972             {
17973               md_number_to_chars ((char *) buf, insn >> 16, 2);
17974               buf += 2;
17975               md_number_to_chars ((char *) buf, insn & 0xffff, 2);
17976               buf += 2;
17977
17978               gas_assert (buf == ((bfd_byte *) fragp->fr_literal
17979                                   + fragp->fr_fix));
17980               return;
17981             }
17982         }
17983       else
17984         {
17985           unsigned long next;
17986
17987           if (target_big_endian)
17988             {
17989               insn = bfd_getb16 (buf);
17990               next = bfd_getb16 (buf + 2);
17991             }
17992           else
17993             {
17994               insn = bfd_getl16 (buf);
17995               next = bfd_getl16 (buf + 2);
17996             }
17997           insn = (insn << 16) | next;
17998         }
17999
18000       /* Relax 32-bit branches to a sequence of instructions.  */
18001       as_warn_where (fragp->fr_file, fragp->fr_line,
18002                      _("Relaxed out-of-range branch into a jump"));
18003
18004       /* Set the short-delay-slot bit.  */
18005       short_ds = al && (insn & 0x02000000) != 0;
18006
18007       if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18008         {
18009           symbolS *l;
18010
18011           /* Reverse the branch.  */
18012           if ((insn & 0xfc000000) == 0x94000000                 /* beq  */
18013               || (insn & 0xfc000000) == 0xb4000000)             /* bne  */
18014             insn ^= 0x20000000;
18015           else if ((insn & 0xffe00000) == 0x40000000            /* bltz  */
18016                    || (insn & 0xffe00000) == 0x40400000         /* bgez  */
18017                    || (insn & 0xffe00000) == 0x40800000         /* blez  */
18018                    || (insn & 0xffe00000) == 0x40c00000         /* bgtz  */
18019                    || (insn & 0xffe00000) == 0x40a00000         /* bnezc  */
18020                    || (insn & 0xffe00000) == 0x40e00000         /* beqzc  */
18021                    || (insn & 0xffe00000) == 0x40200000         /* bltzal  */
18022                    || (insn & 0xffe00000) == 0x40600000         /* bgezal  */
18023                    || (insn & 0xffe00000) == 0x42200000         /* bltzals  */
18024                    || (insn & 0xffe00000) == 0x42600000)        /* bgezals  */
18025             insn ^= 0x00400000;
18026           else if ((insn & 0xffe30000) == 0x43800000            /* bc1f  */
18027                    || (insn & 0xffe30000) == 0x43a00000         /* bc1t  */
18028                    || (insn & 0xffe30000) == 0x42800000         /* bc2f  */
18029                    || (insn & 0xffe30000) == 0x42a00000)        /* bc2t  */
18030             insn ^= 0x00200000;
18031           else
18032             abort ();
18033
18034           if (al)
18035             {
18036               /* Clear the and-link and short-delay-slot bits.  */
18037               gas_assert ((insn & 0xfda00000) == 0x40200000);
18038
18039               /* bltzal  0x40200000     bgezal  0x40600000  */
18040               /* bltzals 0x42200000     bgezals 0x42600000  */
18041               insn &= ~0x02200000;
18042             }
18043
18044           /* Make a label at the end for use with the branch.  */
18045           l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18046           micromips_label_inc ();
18047 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
18048           if (IS_ELF)
18049             S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
18050 #endif
18051
18052           /* Refer to it.  */
18053           fixp = fix_new (fragp, buf - (bfd_byte *) fragp->fr_literal,
18054                           4, l, 0, TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
18055           fixp->fx_file = fragp->fr_file;
18056           fixp->fx_line = fragp->fr_line;
18057
18058           /* Branch over the jump.  */
18059           md_number_to_chars ((char *) buf, insn >> 16, 2);
18060           buf += 2;
18061           md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18062           buf += 2;
18063
18064           if (!compact)
18065             {
18066               /* nop  */
18067               insn = 0x0c00;
18068               md_number_to_chars ((char *) buf, insn, 2);
18069               buf += 2;
18070             }
18071         }
18072
18073       if (mips_pic == NO_PIC)
18074         {
18075           unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s  */
18076
18077           /* j/jal/jals <sym>  R_MICROMIPS_26_S1  */
18078           insn = al ? jal : 0xd4000000;
18079
18080           fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18081                               4, &exp, FALSE, BFD_RELOC_MICROMIPS_JMP);
18082           fixp->fx_file = fragp->fr_file;
18083           fixp->fx_line = fragp->fr_line;
18084
18085           md_number_to_chars ((char *) buf, insn >> 16, 2);
18086           buf += 2;
18087           md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18088           buf += 2;
18089
18090           if (compact)
18091             {
18092               /* nop  */
18093               insn = 0x0c00;
18094               md_number_to_chars ((char *) buf, insn, 2);
18095               buf += 2;
18096             }
18097         }
18098       else
18099         {
18100           unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18101           unsigned long jalr = short_ds ? 0x45e0 : 0x45c0;      /* jalr/s  */
18102           unsigned long jr = compact ? 0x45a0 : 0x4580;         /* jr/c  */
18103
18104           /* lw/ld $at, <sym>($gp)  R_MICROMIPS_GOT16  */
18105           insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18106           insn |= at << MICROMIPSOP_SH_RT;
18107
18108           if (exp.X_add_number)
18109             {
18110               exp.X_add_symbol = make_expr_symbol (&exp);
18111               exp.X_add_number = 0;
18112             }
18113
18114           fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18115                               4, &exp, FALSE, BFD_RELOC_MICROMIPS_GOT16);
18116           fixp->fx_file = fragp->fr_file;
18117           fixp->fx_line = fragp->fr_line;
18118
18119           md_number_to_chars ((char *) buf, insn >> 16, 2);
18120           buf += 2;
18121           md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18122           buf += 2;
18123
18124           /* d/addiu $at, $at, <sym>  R_MICROMIPS_LO16  */
18125           insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18126           insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18127
18128           fixp = fix_new_exp (fragp, buf - (bfd_byte *) fragp->fr_literal,
18129                               4, &exp, FALSE, BFD_RELOC_MICROMIPS_LO16);
18130           fixp->fx_file = fragp->fr_file;
18131           fixp->fx_line = fragp->fr_line;
18132
18133           md_number_to_chars ((char *) buf, insn >> 16, 2);
18134           buf += 2;
18135           md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18136           buf += 2;
18137
18138           /* jr/jrc/jalr/jalrs $at  */
18139           insn = al ? jalr : jr;
18140           insn |= at << MICROMIPSOP_SH_MJ;
18141
18142           md_number_to_chars ((char *) buf, insn & 0xffff, 2);
18143           buf += 2;
18144         }
18145
18146       gas_assert (buf == (bfd_byte *) fragp->fr_literal + fragp->fr_fix);
18147       return;
18148     }
18149
18150   if (RELAX_MIPS16_P (fragp->fr_subtype))
18151     {
18152       int type;
18153       const struct mips16_immed_operand *op;
18154       bfd_boolean small, ext;
18155       offsetT val;
18156       bfd_byte *buf;
18157       unsigned long insn;
18158       bfd_boolean use_extend;
18159       unsigned short extend;
18160
18161       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18162       op = mips16_immed_operands;
18163       while (op->type != type)
18164         ++op;
18165
18166       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18167         {
18168           small = FALSE;
18169           ext = TRUE;
18170         }
18171       else
18172         {
18173           small = TRUE;
18174           ext = FALSE;
18175         }
18176
18177       val = resolve_symbol_value (fragp->fr_symbol);
18178       if (op->pcrel)
18179         {
18180           addressT addr;
18181
18182           addr = fragp->fr_address + fragp->fr_fix;
18183
18184           /* The rules for the base address of a PC relative reloc are
18185              complicated; see mips16_extended_frag.  */
18186           if (type == 'p' || type == 'q')
18187             {
18188               addr += 2;
18189               if (ext)
18190                 addr += 2;
18191               /* Ignore the low bit in the target, since it will be
18192                  set for a text label.  */
18193               if ((val & 1) != 0)
18194                 --val;
18195             }
18196           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18197             addr -= 4;
18198           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18199             addr -= 2;
18200
18201           addr &= ~ (addressT) ((1 << op->shift) - 1);
18202           val -= addr;
18203
18204           /* Make sure the section winds up with the alignment we have
18205              assumed.  */
18206           if (op->shift > 0)
18207             record_alignment (asec, op->shift);
18208         }
18209
18210       if (ext
18211           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18212               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18213         as_warn_where (fragp->fr_file, fragp->fr_line,
18214                        _("extended instruction in delay slot"));
18215
18216       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
18217
18218       if (target_big_endian)
18219         insn = bfd_getb16 (buf);
18220       else
18221         insn = bfd_getl16 (buf);
18222
18223       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
18224                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
18225                     small, ext, &insn, &use_extend, &extend);
18226
18227       if (use_extend)
18228         {
18229           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
18230           fragp->fr_fix += 2;
18231           buf += 2;
18232         }
18233
18234       md_number_to_chars ((char *) buf, insn, 2);
18235       fragp->fr_fix += 2;
18236       buf += 2;
18237     }
18238   else
18239     {
18240       relax_substateT subtype = fragp->fr_subtype;
18241       bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18242       bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
18243       int first, second;
18244       fixS *fixp;
18245
18246       first = RELAX_FIRST (subtype);
18247       second = RELAX_SECOND (subtype);
18248       fixp = (fixS *) fragp->fr_opcode;
18249
18250       /* If the delay slot chosen does not match the size of the instruction,
18251          then emit a warning.  */
18252       if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18253            || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18254         {
18255           relax_substateT s;
18256           const char *msg;
18257
18258           s = subtype & (RELAX_DELAY_SLOT_16BIT
18259                          | RELAX_DELAY_SLOT_SIZE_FIRST
18260                          | RELAX_DELAY_SLOT_SIZE_SECOND);
18261           msg = macro_warning (s);
18262           if (msg != NULL)
18263             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18264           subtype &= ~s;
18265         }
18266
18267       /* Possibly emit a warning if we've chosen the longer option.  */
18268       if (use_second == second_longer)
18269         {
18270           relax_substateT s;
18271           const char *msg;
18272
18273           s = (subtype
18274                & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18275           msg = macro_warning (s);
18276           if (msg != NULL)
18277             as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
18278           subtype &= ~s;
18279         }
18280
18281       /* Go through all the fixups for the first sequence.  Disable them
18282          (by marking them as done) if we're going to use the second
18283          sequence instead.  */
18284       while (fixp
18285              && fixp->fx_frag == fragp
18286              && fixp->fx_where < fragp->fr_fix - second)
18287         {
18288           if (subtype & RELAX_USE_SECOND)
18289             fixp->fx_done = 1;
18290           fixp = fixp->fx_next;
18291         }
18292
18293       /* Go through the fixups for the second sequence.  Disable them if
18294          we're going to use the first sequence, otherwise adjust their
18295          addresses to account for the relaxation.  */
18296       while (fixp && fixp->fx_frag == fragp)
18297         {
18298           if (subtype & RELAX_USE_SECOND)
18299             fixp->fx_where -= first;
18300           else
18301             fixp->fx_done = 1;
18302           fixp = fixp->fx_next;
18303         }
18304
18305       /* Now modify the frag contents.  */
18306       if (subtype & RELAX_USE_SECOND)
18307         {
18308           char *start;
18309
18310           start = fragp->fr_literal + fragp->fr_fix - first - second;
18311           memmove (start, start + first, second);
18312           fragp->fr_fix -= first;
18313         }
18314       else
18315         fragp->fr_fix -= second;
18316     }
18317 }
18318
18319 #ifdef OBJ_ELF
18320
18321 /* This function is called after the relocs have been generated.
18322    We've been storing mips16 text labels as odd.  Here we convert them
18323    back to even for the convenience of the debugger.  */
18324
18325 void
18326 mips_frob_file_after_relocs (void)
18327 {
18328   asymbol **syms;
18329   unsigned int count, i;
18330
18331   if (!IS_ELF)
18332     return;
18333
18334   syms = bfd_get_outsymbols (stdoutput);
18335   count = bfd_get_symcount (stdoutput);
18336   for (i = 0; i < count; i++, syms++)
18337     if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18338         && ((*syms)->value & 1) != 0)
18339       {
18340         (*syms)->value &= ~1;
18341         /* If the symbol has an odd size, it was probably computed
18342            incorrectly, so adjust that as well.  */
18343         if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18344           ++elf_symbol (*syms)->internal_elf_sym.st_size;
18345       }
18346 }
18347
18348 #endif
18349
18350 /* This function is called whenever a label is defined, including fake
18351    labels instantiated off the dot special symbol.  It is used when
18352    handling branch delays; if a branch has a label, we assume we cannot
18353    move it.  This also bumps the value of the symbol by 1 in compressed
18354    code.  */
18355
18356 void
18357 mips_record_label (symbolS *sym)
18358 {
18359   segment_info_type *si = seg_info (now_seg);
18360   struct insn_label_list *l;
18361
18362   if (free_insn_labels == NULL)
18363     l = (struct insn_label_list *) xmalloc (sizeof *l);
18364   else
18365     {
18366       l = free_insn_labels;
18367       free_insn_labels = l->next;
18368     }
18369
18370   l->label = sym;
18371   l->next = si->label_list;
18372   si->label_list = l;
18373 }
18374
18375 /* This function is called as tc_frob_label() whenever a label is defined
18376    and adds a DWARF-2 record we only want for true labels.  */
18377
18378 void
18379 mips_define_label (symbolS *sym)
18380 {
18381   mips_record_label (sym);
18382 #ifdef OBJ_ELF
18383   dwarf2_emit_label (sym);
18384 #endif
18385 }
18386 \f
18387 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
18388
18389 /* Some special processing for a MIPS ELF file.  */
18390
18391 void
18392 mips_elf_final_processing (void)
18393 {
18394   /* Write out the register information.  */
18395   if (mips_abi != N64_ABI)
18396     {
18397       Elf32_RegInfo s;
18398
18399       s.ri_gprmask = mips_gprmask;
18400       s.ri_cprmask[0] = mips_cprmask[0];
18401       s.ri_cprmask[1] = mips_cprmask[1];
18402       s.ri_cprmask[2] = mips_cprmask[2];
18403       s.ri_cprmask[3] = mips_cprmask[3];
18404       /* The gp_value field is set by the MIPS ELF backend.  */
18405
18406       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18407                                        ((Elf32_External_RegInfo *)
18408                                         mips_regmask_frag));
18409     }
18410   else
18411     {
18412       Elf64_Internal_RegInfo s;
18413
18414       s.ri_gprmask = mips_gprmask;
18415       s.ri_pad = 0;
18416       s.ri_cprmask[0] = mips_cprmask[0];
18417       s.ri_cprmask[1] = mips_cprmask[1];
18418       s.ri_cprmask[2] = mips_cprmask[2];
18419       s.ri_cprmask[3] = mips_cprmask[3];
18420       /* The gp_value field is set by the MIPS ELF backend.  */
18421
18422       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18423                                        ((Elf64_External_RegInfo *)
18424                                         mips_regmask_frag));
18425     }
18426
18427   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
18428      sort of BFD interface for this.  */
18429   if (mips_any_noreorder)
18430     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18431   if (mips_pic != NO_PIC)
18432     {
18433       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
18434       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18435     }
18436   if (mips_abicalls)
18437     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18438
18439   /* Set MIPS ELF flags for ASEs.  */
18440   /* We may need to define a new flag for DSP ASE, and set this flag when
18441      file_ase_dsp is true.  */
18442   /* Same for DSP R2.  */
18443   /* We may need to define a new flag for MT ASE, and set this flag when
18444      file_ase_mt is true.  */
18445   if (file_ase_mips16)
18446     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
18447   if (file_ase_micromips)
18448     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
18449 #if 0 /* XXX FIXME */
18450   if (file_ase_mips3d)
18451     elf_elfheader (stdoutput)->e_flags |= ???;
18452 #endif
18453   if (file_ase_mdmx)
18454     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
18455
18456   /* Set the MIPS ELF ABI flags.  */
18457   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
18458     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
18459   else if (mips_abi == O64_ABI)
18460     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
18461   else if (mips_abi == EABI_ABI)
18462     {
18463       if (!file_mips_gp32)
18464         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18465       else
18466         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18467     }
18468   else if (mips_abi == N32_ABI)
18469     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18470
18471   /* Nothing to do for N64_ABI.  */
18472
18473   if (mips_32bitmode)
18474     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
18475
18476 #if 0 /* XXX FIXME */
18477   /* 32 bit code with 64 bit FP registers.  */
18478   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18479     elf_elfheader (stdoutput)->e_flags |= ???;
18480 #endif
18481 }
18482
18483 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
18484 \f
18485 typedef struct proc {
18486   symbolS *func_sym;
18487   symbolS *func_end_sym;
18488   unsigned long reg_mask;
18489   unsigned long reg_offset;
18490   unsigned long fpreg_mask;
18491   unsigned long fpreg_offset;
18492   unsigned long frame_offset;
18493   unsigned long frame_reg;
18494   unsigned long pc_reg;
18495 } procS;
18496
18497 static procS cur_proc;
18498 static procS *cur_proc_ptr;
18499 static int numprocs;
18500
18501 /* Implement NOP_OPCODE.  We encode a MIPS16 nop as "1", a microMIPS nop
18502    as "2", and a normal nop as "0".  */
18503
18504 #define NOP_OPCODE_MIPS         0
18505 #define NOP_OPCODE_MIPS16       1
18506 #define NOP_OPCODE_MICROMIPS    2
18507
18508 char
18509 mips_nop_opcode (void)
18510 {
18511   if (seg_info (now_seg)->tc_segment_info_data.micromips)
18512     return NOP_OPCODE_MICROMIPS;
18513   else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18514     return NOP_OPCODE_MIPS16;
18515   else
18516     return NOP_OPCODE_MIPS;
18517 }
18518
18519 /* Fill in an rs_align_code fragment.  Unlike elsewhere we want to use
18520    32-bit microMIPS NOPs here (if applicable).  */
18521
18522 void
18523 mips_handle_align (fragS *fragp)
18524 {
18525   char nop_opcode;
18526   char *p;
18527   int bytes, size, excess;
18528   valueT opcode;
18529
18530   if (fragp->fr_type != rs_align_code)
18531     return;
18532
18533   p = fragp->fr_literal + fragp->fr_fix;
18534   nop_opcode = *p;
18535   switch (nop_opcode)
18536     {
18537     case NOP_OPCODE_MICROMIPS:
18538       opcode = micromips_nop32_insn.insn_opcode;
18539       size = 4;
18540       break;
18541     case NOP_OPCODE_MIPS16:
18542       opcode = mips16_nop_insn.insn_opcode;
18543       size = 2;
18544       break;
18545     case NOP_OPCODE_MIPS:
18546     default:
18547       opcode = nop_insn.insn_opcode;
18548       size = 4;
18549       break;
18550     }
18551
18552   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18553   excess = bytes % size;
18554
18555   /* Handle the leading part if we're not inserting a whole number of
18556      instructions, and make it the end of the fixed part of the frag.
18557      Try to fit in a short microMIPS NOP if applicable and possible,
18558      and use zeroes otherwise.  */
18559   gas_assert (excess < 4);
18560   fragp->fr_fix += excess;
18561   switch (excess)
18562     {
18563     case 3:
18564       *p++ = '\0';
18565       /* Fall through.  */
18566     case 2:
18567       if (nop_opcode == NOP_OPCODE_MICROMIPS)
18568         {
18569           md_number_to_chars (p, micromips_nop16_insn.insn_opcode, 2);
18570           p += 2;
18571           break;
18572         }
18573       *p++ = '\0';
18574       /* Fall through.  */
18575     case 1:
18576       *p++ = '\0';
18577       /* Fall through.  */
18578     case 0:
18579       break;
18580     }
18581
18582   md_number_to_chars (p, opcode, size);
18583   fragp->fr_var = size;
18584 }
18585
18586 static void
18587 md_obj_begin (void)
18588 {
18589 }
18590
18591 static void
18592 md_obj_end (void)
18593 {
18594   /* Check for premature end, nesting errors, etc.  */
18595   if (cur_proc_ptr)
18596     as_warn (_("missing .end at end of assembly"));
18597 }
18598
18599 static long
18600 get_number (void)
18601 {
18602   int negative = 0;
18603   long val = 0;
18604
18605   if (*input_line_pointer == '-')
18606     {
18607       ++input_line_pointer;
18608       negative = 1;
18609     }
18610   if (!ISDIGIT (*input_line_pointer))
18611     as_bad (_("expected simple number"));
18612   if (input_line_pointer[0] == '0')
18613     {
18614       if (input_line_pointer[1] == 'x')
18615         {
18616           input_line_pointer += 2;
18617           while (ISXDIGIT (*input_line_pointer))
18618             {
18619               val <<= 4;
18620               val |= hex_value (*input_line_pointer++);
18621             }
18622           return negative ? -val : val;
18623         }
18624       else
18625         {
18626           ++input_line_pointer;
18627           while (ISDIGIT (*input_line_pointer))
18628             {
18629               val <<= 3;
18630               val |= *input_line_pointer++ - '0';
18631             }
18632           return negative ? -val : val;
18633         }
18634     }
18635   if (!ISDIGIT (*input_line_pointer))
18636     {
18637       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18638               *input_line_pointer, *input_line_pointer);
18639       as_warn (_("invalid number"));
18640       return -1;
18641     }
18642   while (ISDIGIT (*input_line_pointer))
18643     {
18644       val *= 10;
18645       val += *input_line_pointer++ - '0';
18646     }
18647   return negative ? -val : val;
18648 }
18649
18650 /* The .file directive; just like the usual .file directive, but there
18651    is an initial number which is the ECOFF file index.  In the non-ECOFF
18652    case .file implies DWARF-2.  */
18653
18654 static void
18655 s_mips_file (int x ATTRIBUTE_UNUSED)
18656 {
18657   static int first_file_directive = 0;
18658
18659   if (ECOFF_DEBUGGING)
18660     {
18661       get_number ();
18662       s_app_file (0);
18663     }
18664   else
18665     {
18666       char *filename;
18667
18668       filename = dwarf2_directive_file (0);
18669
18670       /* Versions of GCC up to 3.1 start files with a ".file"
18671          directive even for stabs output.  Make sure that this
18672          ".file" is handled.  Note that you need a version of GCC
18673          after 3.1 in order to support DWARF-2 on MIPS.  */
18674       if (filename != NULL && ! first_file_directive)
18675         {
18676           (void) new_logical_line (filename, -1);
18677           s_app_file_string (filename, 0);
18678         }
18679       first_file_directive = 1;
18680     }
18681 }
18682
18683 /* The .loc directive, implying DWARF-2.  */
18684
18685 static void
18686 s_mips_loc (int x ATTRIBUTE_UNUSED)
18687 {
18688   if (!ECOFF_DEBUGGING)
18689     dwarf2_directive_loc (0);
18690 }
18691
18692 /* The .end directive.  */
18693
18694 static void
18695 s_mips_end (int x ATTRIBUTE_UNUSED)
18696 {
18697   symbolS *p;
18698
18699   /* Following functions need their own .frame and .cprestore directives.  */
18700   mips_frame_reg_valid = 0;
18701   mips_cprestore_valid = 0;
18702
18703   if (!is_end_of_line[(unsigned char) *input_line_pointer])
18704     {
18705       p = get_symbol ();
18706       demand_empty_rest_of_line ();
18707     }
18708   else
18709     p = NULL;
18710
18711   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18712     as_warn (_(".end not in text section"));
18713
18714   if (!cur_proc_ptr)
18715     {
18716       as_warn (_(".end directive without a preceding .ent directive."));
18717       demand_empty_rest_of_line ();
18718       return;
18719     }
18720
18721   if (p != NULL)
18722     {
18723       gas_assert (S_GET_NAME (p));
18724       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
18725         as_warn (_(".end symbol does not match .ent symbol."));
18726
18727       if (debug_type == DEBUG_STABS)
18728         stabs_generate_asm_endfunc (S_GET_NAME (p),
18729                                     S_GET_NAME (p));
18730     }
18731   else
18732     as_warn (_(".end directive missing or unknown symbol"));
18733
18734 #ifdef OBJ_ELF
18735   /* Create an expression to calculate the size of the function.  */
18736   if (p && cur_proc_ptr)
18737     {
18738       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
18739       expressionS *exp = xmalloc (sizeof (expressionS));
18740
18741       obj->size = exp;
18742       exp->X_op = O_subtract;
18743       exp->X_add_symbol = symbol_temp_new_now ();
18744       exp->X_op_symbol = p;
18745       exp->X_add_number = 0;
18746
18747       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18748     }
18749
18750   /* Generate a .pdr section.  */
18751   if (IS_ELF && !ECOFF_DEBUGGING && mips_flag_pdr)
18752     {
18753       segT saved_seg = now_seg;
18754       subsegT saved_subseg = now_subseg;
18755       expressionS exp;
18756       char *fragp;
18757
18758 #ifdef md_flush_pending_output
18759       md_flush_pending_output ();
18760 #endif
18761
18762       gas_assert (pdr_seg);
18763       subseg_set (pdr_seg, 0);
18764
18765       /* Write the symbol.  */
18766       exp.X_op = O_symbol;
18767       exp.X_add_symbol = p;
18768       exp.X_add_number = 0;
18769       emit_expr (&exp, 4);
18770
18771       fragp = frag_more (7 * 4);
18772
18773       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18774       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18775       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18776       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
18777       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
18778       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
18779       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
18780
18781       subseg_set (saved_seg, saved_subseg);
18782     }
18783 #endif /* OBJ_ELF */
18784
18785   cur_proc_ptr = NULL;
18786 }
18787
18788 /* The .aent and .ent directives.  */
18789
18790 static void
18791 s_mips_ent (int aent)
18792 {
18793   symbolS *symbolP;
18794
18795   symbolP = get_symbol ();
18796   if (*input_line_pointer == ',')
18797     ++input_line_pointer;
18798   SKIP_WHITESPACE ();
18799   if (ISDIGIT (*input_line_pointer)
18800       || *input_line_pointer == '-')
18801     get_number ();
18802
18803   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
18804     as_warn (_(".ent or .aent not in text section."));
18805
18806   if (!aent && cur_proc_ptr)
18807     as_warn (_("missing .end"));
18808
18809   if (!aent)
18810     {
18811       /* This function needs its own .frame and .cprestore directives.  */
18812       mips_frame_reg_valid = 0;
18813       mips_cprestore_valid = 0;
18814
18815       cur_proc_ptr = &cur_proc;
18816       memset (cur_proc_ptr, '\0', sizeof (procS));
18817
18818       cur_proc_ptr->func_sym = symbolP;
18819
18820       ++numprocs;
18821
18822       if (debug_type == DEBUG_STABS)
18823         stabs_generate_asm_func (S_GET_NAME (symbolP),
18824                                  S_GET_NAME (symbolP));
18825     }
18826
18827   symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
18828
18829   demand_empty_rest_of_line ();
18830 }
18831
18832 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
18833    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
18834    s_mips_frame is used so that we can set the PDR information correctly.
18835    We can't use the ecoff routines because they make reference to the ecoff
18836    symbol table (in the mdebug section).  */
18837
18838 static void
18839 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
18840 {
18841 #ifdef OBJ_ELF
18842   if (IS_ELF && !ECOFF_DEBUGGING)
18843     {
18844       long val;
18845
18846       if (cur_proc_ptr == (procS *) NULL)
18847         {
18848           as_warn (_(".frame outside of .ent"));
18849           demand_empty_rest_of_line ();
18850           return;
18851         }
18852
18853       cur_proc_ptr->frame_reg = tc_get_register (1);
18854
18855       SKIP_WHITESPACE ();
18856       if (*input_line_pointer++ != ','
18857           || get_absolute_expression_and_terminator (&val) != ',')
18858         {
18859           as_warn (_("Bad .frame directive"));
18860           --input_line_pointer;
18861           demand_empty_rest_of_line ();
18862           return;
18863         }
18864
18865       cur_proc_ptr->frame_offset = val;
18866       cur_proc_ptr->pc_reg = tc_get_register (0);
18867
18868       demand_empty_rest_of_line ();
18869     }
18870   else
18871 #endif /* OBJ_ELF */
18872     s_ignore (ignore);
18873 }
18874
18875 /* The .fmask and .mask directives. If the mdebug section is present
18876    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
18877    embedded targets, s_mips_mask is used so that we can set the PDR
18878    information correctly. We can't use the ecoff routines because they
18879    make reference to the ecoff symbol table (in the mdebug section).  */
18880
18881 static void
18882 s_mips_mask (int reg_type)
18883 {
18884 #ifdef OBJ_ELF
18885   if (IS_ELF && !ECOFF_DEBUGGING)
18886     {
18887       long mask, off;
18888
18889       if (cur_proc_ptr == (procS *) NULL)
18890         {
18891           as_warn (_(".mask/.fmask outside of .ent"));
18892           demand_empty_rest_of_line ();
18893           return;
18894         }
18895
18896       if (get_absolute_expression_and_terminator (&mask) != ',')
18897         {
18898           as_warn (_("Bad .mask/.fmask directive"));
18899           --input_line_pointer;
18900           demand_empty_rest_of_line ();
18901           return;
18902         }
18903
18904       off = get_absolute_expression ();
18905
18906       if (reg_type == 'F')
18907         {
18908           cur_proc_ptr->fpreg_mask = mask;
18909           cur_proc_ptr->fpreg_offset = off;
18910         }
18911       else
18912         {
18913           cur_proc_ptr->reg_mask = mask;
18914           cur_proc_ptr->reg_offset = off;
18915         }
18916
18917       demand_empty_rest_of_line ();
18918     }
18919   else
18920 #endif /* OBJ_ELF */
18921     s_ignore (reg_type);
18922 }
18923
18924 /* A table describing all the processors gas knows about.  Names are
18925    matched in the order listed.
18926
18927    To ease comparison, please keep this table in the same order as
18928    gcc's mips_cpu_info_table[].  */
18929 static const struct mips_cpu_info mips_cpu_info_table[] =
18930 {
18931   /* Entries for generic ISAs */
18932   { "mips1",          MIPS_CPU_IS_ISA,          ISA_MIPS1,      CPU_R3000 },
18933   { "mips2",          MIPS_CPU_IS_ISA,          ISA_MIPS2,      CPU_R6000 },
18934   { "mips3",          MIPS_CPU_IS_ISA,          ISA_MIPS3,      CPU_R4000 },
18935   { "mips4",          MIPS_CPU_IS_ISA,          ISA_MIPS4,      CPU_R8000 },
18936   { "mips5",          MIPS_CPU_IS_ISA,          ISA_MIPS5,      CPU_MIPS5 },
18937   { "mips32",         MIPS_CPU_IS_ISA,          ISA_MIPS32,     CPU_MIPS32 },
18938   { "mips32r2",       MIPS_CPU_IS_ISA,          ISA_MIPS32R2,   CPU_MIPS32R2 },
18939   { "mips64",         MIPS_CPU_IS_ISA,          ISA_MIPS64,     CPU_MIPS64 },
18940   { "mips64r2",       MIPS_CPU_IS_ISA,          ISA_MIPS64R2,   CPU_MIPS64R2 },
18941
18942   /* MIPS I */
18943   { "r3000",          0,                        ISA_MIPS1,      CPU_R3000 },
18944   { "r2000",          0,                        ISA_MIPS1,      CPU_R3000 },
18945   { "r3900",          0,                        ISA_MIPS1,      CPU_R3900 },
18946
18947   /* MIPS II */
18948   { "r6000",          0,                        ISA_MIPS2,      CPU_R6000 },
18949
18950   /* MIPS III */
18951   { "r4000",          0,                        ISA_MIPS3,      CPU_R4000 },
18952   { "r4010",          0,                        ISA_MIPS2,      CPU_R4010 },
18953   { "vr4100",         0,                        ISA_MIPS3,      CPU_VR4100 },
18954   { "vr4111",         0,                        ISA_MIPS3,      CPU_R4111 },
18955   { "vr4120",         0,                        ISA_MIPS3,      CPU_VR4120 },
18956   { "vr4130",         0,                        ISA_MIPS3,      CPU_VR4120 },
18957   { "vr4181",         0,                        ISA_MIPS3,      CPU_R4111 },
18958   { "vr4300",         0,                        ISA_MIPS3,      CPU_R4300 },
18959   { "r4400",          0,                        ISA_MIPS3,      CPU_R4400 },
18960   { "r4600",          0,                        ISA_MIPS3,      CPU_R4600 },
18961   { "orion",          0,                        ISA_MIPS3,      CPU_R4600 },
18962   { "r4650",          0,                        ISA_MIPS3,      CPU_R4650 },
18963   /* ST Microelectronics Loongson 2E and 2F cores */
18964   { "loongson2e",     0,                        ISA_MIPS3,   CPU_LOONGSON_2E },
18965   { "loongson2f",     0,                        ISA_MIPS3,   CPU_LOONGSON_2F },
18966
18967   /* MIPS IV */
18968   { "r8000",          0,                        ISA_MIPS4,      CPU_R8000 },
18969   { "r10000",         0,                        ISA_MIPS4,      CPU_R10000 },
18970   { "r12000",         0,                        ISA_MIPS4,      CPU_R12000 },
18971   { "r14000",         0,                        ISA_MIPS4,      CPU_R14000 },
18972   { "r16000",         0,                        ISA_MIPS4,      CPU_R16000 },
18973   { "vr5000",         0,                        ISA_MIPS4,      CPU_R5000 },
18974   { "vr5400",         0,                        ISA_MIPS4,      CPU_VR5400 },
18975   { "vr5500",         0,                        ISA_MIPS4,      CPU_VR5500 },
18976   { "rm5200",         0,                        ISA_MIPS4,      CPU_R5000 },
18977   { "rm5230",         0,                        ISA_MIPS4,      CPU_R5000 },
18978   { "rm5231",         0,                        ISA_MIPS4,      CPU_R5000 },
18979   { "rm5261",         0,                        ISA_MIPS4,      CPU_R5000 },
18980   { "rm5721",         0,                        ISA_MIPS4,      CPU_R5000 },
18981   { "rm7000",         0,                        ISA_MIPS4,      CPU_RM7000 },
18982   { "rm9000",         0,                        ISA_MIPS4,      CPU_RM9000 },
18983
18984   /* MIPS 32 */
18985   { "4kc",            0,                        ISA_MIPS32,     CPU_MIPS32 },
18986   { "4km",            0,                        ISA_MIPS32,     CPU_MIPS32 },
18987   { "4kp",            0,                        ISA_MIPS32,     CPU_MIPS32 },
18988   { "4ksc",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32,     CPU_MIPS32 },
18989
18990   /* MIPS 32 Release 2 */
18991   { "4kec",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
18992   { "4kem",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
18993   { "4kep",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
18994   { "4ksd",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32R2,   CPU_MIPS32R2 },
18995   { "m4k",            0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
18996   { "m4kp",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
18997   { "m14k",           MIPS_CPU_ASE_MCU,         ISA_MIPS32R2,   CPU_MIPS32R2 },
18998   { "m14kc",          MIPS_CPU_ASE_MCU,         ISA_MIPS32R2,   CPU_MIPS32R2 },
18999   { "m14ke",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19000                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19001   { "m14kec",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2 | MIPS_CPU_ASE_MCU,
19002                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19003   { "24kc",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19004   { "24kf2_1",        0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19005   { "24kf",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19006   { "24kf1_1",        0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19007   /* Deprecated forms of the above.  */
19008   { "24kfx",          0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19009   { "24kx",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
19010   /* 24KE is a 24K with DSP ASE, other ASEs are optional.  */
19011   { "24kec",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19012   { "24kef2_1",       MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19013   { "24kef",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19014   { "24kef1_1",       MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19015   /* Deprecated forms of the above.  */
19016   { "24kefx",         MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19017   { "24kex",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
19018   /* 34K is a 24K with DSP and MT ASE, other ASEs are optional.  */
19019   { "34kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19020                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19021   { "34kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19022                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19023   { "34kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19024                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19025   { "34kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19026                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19027   /* Deprecated forms of the above.  */
19028   { "34kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19029                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19030   { "34kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19031                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19032   /* 74K with DSP and DSPR2 ASE, other ASEs are optional.  */
19033   { "74kc",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19034                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19035   { "74kf2_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19036                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19037   { "74kf",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19038                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19039   { "74kf1_1",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19040                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19041   { "74kf3_2",        MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19042                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19043   /* Deprecated forms of the above.  */
19044   { "74kfx",          MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19045                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19046   { "74kx",           MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_DSPR2,
19047                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19048   /* 1004K cores are multiprocessor versions of the 34K.  */
19049   { "1004kc",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19050                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19051   { "1004kf2_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19052                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19053   { "1004kf",         MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19054                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19055   { "1004kf1_1",      MIPS_CPU_ASE_DSP | MIPS_CPU_ASE_MT,
19056                                                 ISA_MIPS32R2,   CPU_MIPS32R2 },
19057
19058   /* MIPS 64 */
19059   { "5kc",            0,                        ISA_MIPS64,     CPU_MIPS64 },
19060   { "5kf",            0,                        ISA_MIPS64,     CPU_MIPS64 },
19061   { "20kc",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64,     CPU_MIPS64 },
19062   { "25kf",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64,     CPU_MIPS64 },
19063
19064   /* Broadcom SB-1 CPU core */
19065   { "sb1",            MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19066                                                 ISA_MIPS64,     CPU_SB1 },
19067   /* Broadcom SB-1A CPU core */
19068   { "sb1a",           MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
19069                                                 ISA_MIPS64,     CPU_SB1 },
19070   
19071   { "loongson3a",     0,                        ISA_MIPS64,     CPU_LOONGSON_3A },
19072
19073   /* MIPS 64 Release 2 */
19074
19075   /* Cavium Networks Octeon CPU core */
19076   { "octeon",         0,      ISA_MIPS64R2,   CPU_OCTEON },
19077   { "octeon+",        0,      ISA_MIPS64R2,   CPU_OCTEONP },
19078   { "octeon2",        0,      ISA_MIPS64R2,   CPU_OCTEON2 },
19079
19080   /* RMI Xlr */
19081   { "xlr",            0,      ISA_MIPS64,     CPU_XLR },
19082
19083   /* End marker */
19084   { NULL, 0, 0, 0 }
19085 };
19086
19087
19088 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19089    with a final "000" replaced by "k".  Ignore case.
19090
19091    Note: this function is shared between GCC and GAS.  */
19092
19093 static bfd_boolean
19094 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
19095 {
19096   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19097     given++, canonical++;
19098
19099   return ((*given == 0 && *canonical == 0)
19100           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19101 }
19102
19103
19104 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19105    CPU name.  We've traditionally allowed a lot of variation here.
19106
19107    Note: this function is shared between GCC and GAS.  */
19108
19109 static bfd_boolean
19110 mips_matching_cpu_name_p (const char *canonical, const char *given)
19111 {
19112   /* First see if the name matches exactly, or with a final "000"
19113      turned into "k".  */
19114   if (mips_strict_matching_cpu_name_p (canonical, given))
19115     return TRUE;
19116
19117   /* If not, try comparing based on numerical designation alone.
19118      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
19119   if (TOLOWER (*given) == 'r')
19120     given++;
19121   if (!ISDIGIT (*given))
19122     return FALSE;
19123
19124   /* Skip over some well-known prefixes in the canonical name,
19125      hoping to find a number there too.  */
19126   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19127     canonical += 2;
19128   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19129     canonical += 2;
19130   else if (TOLOWER (canonical[0]) == 'r')
19131     canonical += 1;
19132
19133   return mips_strict_matching_cpu_name_p (canonical, given);
19134 }
19135
19136
19137 /* Parse an option that takes the name of a processor as its argument.
19138    OPTION is the name of the option and CPU_STRING is the argument.
19139    Return the corresponding processor enumeration if the CPU_STRING is
19140    recognized, otherwise report an error and return null.
19141
19142    A similar function exists in GCC.  */
19143
19144 static const struct mips_cpu_info *
19145 mips_parse_cpu (const char *option, const char *cpu_string)
19146 {
19147   const struct mips_cpu_info *p;
19148
19149   /* 'from-abi' selects the most compatible architecture for the given
19150      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
19151      EABIs, we have to decide whether we're using the 32-bit or 64-bit
19152      version.  Look first at the -mgp options, if given, otherwise base
19153      the choice on MIPS_DEFAULT_64BIT.
19154
19155      Treat NO_ABI like the EABIs.  One reason to do this is that the
19156      plain 'mips' and 'mips64' configs have 'from-abi' as their default
19157      architecture.  This code picks MIPS I for 'mips' and MIPS III for
19158      'mips64', just as we did in the days before 'from-abi'.  */
19159   if (strcasecmp (cpu_string, "from-abi") == 0)
19160     {
19161       if (ABI_NEEDS_32BIT_REGS (mips_abi))
19162         return mips_cpu_info_from_isa (ISA_MIPS1);
19163
19164       if (ABI_NEEDS_64BIT_REGS (mips_abi))
19165         return mips_cpu_info_from_isa (ISA_MIPS3);
19166
19167       if (file_mips_gp32 >= 0)
19168         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19169
19170       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19171                                      ? ISA_MIPS3
19172                                      : ISA_MIPS1);
19173     }
19174
19175   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
19176   if (strcasecmp (cpu_string, "default") == 0)
19177     return 0;
19178
19179   for (p = mips_cpu_info_table; p->name != 0; p++)
19180     if (mips_matching_cpu_name_p (p->name, cpu_string))
19181       return p;
19182
19183   as_bad (_("Bad value (%s) for %s"), cpu_string, option);
19184   return 0;
19185 }
19186
19187 /* Return the canonical processor information for ISA (a member of the
19188    ISA_MIPS* enumeration).  */
19189
19190 static const struct mips_cpu_info *
19191 mips_cpu_info_from_isa (int isa)
19192 {
19193   int i;
19194
19195   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19196     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
19197         && isa == mips_cpu_info_table[i].isa)
19198       return (&mips_cpu_info_table[i]);
19199
19200   return NULL;
19201 }
19202
19203 static const struct mips_cpu_info *
19204 mips_cpu_info_from_arch (int arch)
19205 {
19206   int i;
19207
19208   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19209     if (arch == mips_cpu_info_table[i].cpu)
19210       return (&mips_cpu_info_table[i]);
19211
19212   return NULL;
19213 }
19214 \f
19215 static void
19216 show (FILE *stream, const char *string, int *col_p, int *first_p)
19217 {
19218   if (*first_p)
19219     {
19220       fprintf (stream, "%24s", "");
19221       *col_p = 24;
19222     }
19223   else
19224     {
19225       fprintf (stream, ", ");
19226       *col_p += 2;
19227     }
19228
19229   if (*col_p + strlen (string) > 72)
19230     {
19231       fprintf (stream, "\n%24s", "");
19232       *col_p = 24;
19233     }
19234
19235   fprintf (stream, "%s", string);
19236   *col_p += strlen (string);
19237
19238   *first_p = 0;
19239 }
19240
19241 void
19242 md_show_usage (FILE *stream)
19243 {
19244   int column, first;
19245   size_t i;
19246
19247   fprintf (stream, _("\
19248 MIPS options:\n\
19249 -EB                     generate big endian output\n\
19250 -EL                     generate little endian output\n\
19251 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
19252 -G NUM                  allow referencing objects up to NUM bytes\n\
19253                         implicitly with the gp register [default 8]\n"));
19254   fprintf (stream, _("\
19255 -mips1                  generate MIPS ISA I instructions\n\
19256 -mips2                  generate MIPS ISA II instructions\n\
19257 -mips3                  generate MIPS ISA III instructions\n\
19258 -mips4                  generate MIPS ISA IV instructions\n\
19259 -mips5                  generate MIPS ISA V instructions\n\
19260 -mips32                 generate MIPS32 ISA instructions\n\
19261 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
19262 -mips64                 generate MIPS64 ISA instructions\n\
19263 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
19264 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
19265
19266   first = 1;
19267
19268   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19269     show (stream, mips_cpu_info_table[i].name, &column, &first);
19270   show (stream, "from-abi", &column, &first);
19271   fputc ('\n', stream);
19272
19273   fprintf (stream, _("\
19274 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19275 -no-mCPU                don't generate code specific to CPU.\n\
19276                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
19277
19278   first = 1;
19279
19280   show (stream, "3900", &column, &first);
19281   show (stream, "4010", &column, &first);
19282   show (stream, "4100", &column, &first);
19283   show (stream, "4650", &column, &first);
19284   fputc ('\n', stream);
19285
19286   fprintf (stream, _("\
19287 -mips16                 generate mips16 instructions\n\
19288 -no-mips16              do not generate mips16 instructions\n"));
19289   fprintf (stream, _("\
19290 -mmicromips             generate microMIPS instructions\n\
19291 -mno-micromips          do not generate microMIPS instructions\n"));
19292   fprintf (stream, _("\
19293 -msmartmips             generate smartmips instructions\n\
19294 -mno-smartmips          do not generate smartmips instructions\n"));  
19295   fprintf (stream, _("\
19296 -mdsp                   generate DSP instructions\n\
19297 -mno-dsp                do not generate DSP instructions\n"));
19298   fprintf (stream, _("\
19299 -mdspr2                 generate DSP R2 instructions\n\
19300 -mno-dspr2              do not generate DSP R2 instructions\n"));
19301   fprintf (stream, _("\
19302 -mmt                    generate MT instructions\n\
19303 -mno-mt                 do not generate MT instructions\n"));
19304   fprintf (stream, _("\
19305 -mmcu                   generate MCU instructions\n\
19306 -mno-mcu                do not generate MCU instructions\n"));
19307   fprintf (stream, _("\
19308 -mfix-loongson2f-jump   work around Loongson2F JUMP instructions\n\
19309 -mfix-loongson2f-nop    work around Loongson2F NOP errata\n\
19310 -mfix-vr4120            work around certain VR4120 errata\n\
19311 -mfix-vr4130            work around VR4130 mflo/mfhi errata\n\
19312 -mfix-24k               insert a nop after ERET and DERET instructions\n\
19313 -mfix-cn63xxp1          work around CN63XXP1 PREF errata\n\
19314 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
19315 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
19316 -msym32                 assume all symbols have 32-bit values\n\
19317 -O0                     remove unneeded NOPs, do not swap branches\n\
19318 -O                      remove unneeded NOPs and swap branches\n\
19319 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
19320 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
19321   fprintf (stream, _("\
19322 -mhard-float            allow floating-point instructions\n\
19323 -msoft-float            do not allow floating-point instructions\n\
19324 -msingle-float          only allow 32-bit floating-point operations\n\
19325 -mdouble-float          allow 32-bit and 64-bit floating-point operations\n\
19326 --[no-]construct-floats [dis]allow floating point values to be constructed\n"
19327                      ));
19328 #ifdef OBJ_ELF
19329   fprintf (stream, _("\
19330 -KPIC, -call_shared     generate SVR4 position independent code\n\
19331 -call_nonpic            generate non-PIC code that can operate with DSOs\n\
19332 -mvxworks-pic           generate VxWorks position independent code\n\
19333 -non_shared             do not generate code that can operate with DSOs\n\
19334 -xgot                   assume a 32 bit GOT\n\
19335 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
19336 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
19337                         position dependent (non shared) code\n\
19338 -mabi=ABI               create ABI conformant object file for:\n"));
19339
19340   first = 1;
19341
19342   show (stream, "32", &column, &first);
19343   show (stream, "o64", &column, &first);
19344   show (stream, "n32", &column, &first);
19345   show (stream, "64", &column, &first);
19346   show (stream, "eabi", &column, &first);
19347
19348   fputc ('\n', stream);
19349
19350   fprintf (stream, _("\
19351 -32                     create o32 ABI object file (default)\n\
19352 -n32                    create n32 ABI object file\n\
19353 -64                     create 64 ABI object file\n"));
19354 #endif
19355 }
19356
19357 #ifdef TE_IRIX
19358 enum dwarf2_format
19359 mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
19360 {
19361   if (HAVE_64BIT_SYMBOLS)
19362     return dwarf2_format_64bit_irix;
19363   else
19364     return dwarf2_format_32bit;
19365 }
19366 #endif
19367
19368 int
19369 mips_dwarf2_addr_size (void)
19370 {
19371   if (HAVE_64BIT_OBJECTS)
19372     return 8;
19373   else
19374     return 4;
19375 }
19376
19377 /* Standard calling conventions leave the CFA at SP on entry.  */
19378 void
19379 mips_cfi_frame_initial_instructions (void)
19380 {
19381   cfi_add_CFA_def_cfa_register (SP);
19382 }
19383
19384 int
19385 tc_mips_regname_to_dw2regnum (char *regname)
19386 {
19387   unsigned int regnum = -1;
19388   unsigned int reg;
19389
19390   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19391     regnum = reg;
19392
19393   return regnum;
19394 }